diff --git a/main.c b/main.c --- a/main.c +++ b/main.c @@ -2,9 +2,16 @@ #include "stm32l100c_discovery.h" #include "ssd1306.h" +// USB includes +#include "hw_config.h" +#include "usb_lib.h" +#include "usb_desc.h" +#include "usb_pwr.h" + #define LED_POWER GPIOB,GPIO_Pin_9 #define LED_STAT GPIOA,GPIO_Pin_15 +#define MAX_CS GPIOB,GPIO_Pin_12 // TODO: Grab buttonpresses with interrupts #define SW_BTN GPIOB, GPIO_Pin_3 @@ -26,6 +33,15 @@ int main(void) // Init clocks SystemInit(); + + init_gpio(); + + Set_USBClock(); + USB_Interrupts_Config(); + USB_Init(); + + GPIO_SetBits(LED_POWER); + RCC_ClocksTypeDef RCC_Clocks; // SysTick end of count event each 1ms @@ -35,11 +51,10 @@ int main(void) GPIO_ResetBits(LED_STAT); Delay(100); - init_gpio(); - ssd1306_Init(); - ssd1306_DrawPoint(3,3,1); - ssd1306_DrawPoint(5,5,0); + //ssd1306_Init(); + //ssd1306_DrawPoint(3,3,1); + //ssd1306_DrawPoint(5,5,0); GPIO_SetBits(LED_POWER); Delay(500); @@ -56,7 +71,7 @@ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SP process(); // Run state machine [TODO: 50hz?] - machine(); // this argument is sooo wrong + machine(); // probably just passed the actual port // TODO: Grab buttonpresses with interrupts @@ -72,16 +87,14 @@ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SP if(!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_3)) { GPIO_ToggleBits(LED_STAT); } -// ssd1306_DrawPoint(5,5,0); + GPIO_SetBits(LED_POWER); - Delay(150); + Delay(50); GPIO_ResetBits(LED_POWER); - Delay(150); + Delay(50); } } - - int32_t temp = 0; int32_t setpoint = 0; int32_t p = 1; @@ -92,7 +105,26 @@ int32_t d = 1; void process() { // Read MAX temp sensor - temp = 0; + GPIO_ResetBits(MAX_CS); + + // Assert CS + // This may not clock at all... might need to send 16 bits first + uint8_t retval = SPI_I2S_ReceiveData(SPI2); + + // Deassert CS + GPIO_SetBits(MAX_CS); + + if((!retval || (temp & 0x2) != 0)) + return; // Comms error + + if((temp & 0x4)!= 0) + return; // Open thermocouple + + + temp = (temp & 0x7FF8) >> 5; + + // TODO: Add calibration offset (linear) + // Perform PID calculations @@ -311,15 +343,15 @@ void init_spi(void) SPI_Cmd(SPI1, ENABLE); /* Enable the SPI */ // MAX IC - SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Rx; + //SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Rx; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; - SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; - SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; - SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; - SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; + SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b; // Andysworkshop + SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; // From andysworkshop + SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; // same + // Can be used for CS... SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; - SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; - SPI_InitStructure.SPI_CRCPolynomial = 7; + //SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; + //SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI2, &SPI_InitStructure); SPI_Cmd(SPI2, ENABLE); /* Enable the SPI */ }