diff --git a/main.c b/main.c --- a/main.c +++ b/main.c @@ -1,29 +1,23 @@ #include "main.h" +#include #include "stm32f0xx_hal_conf.h" -#include "usb_device.h" +//#include "usb_device.h" #include "ssd1306.h" #include "config.h" #include "eeprom_min.h" #include "gpio.h" #include "clock.h" -#include "spi.h" - -// USB includes -//#include "hw_config.h" -//#include "usb_lib.h" -//#include "usb_desc.h" -//#include "usb_pwr.h" -//#include "stringhelpers.h" +//#include "spi.h" // TODO: Grab buttonpresses with interrupts // USB Supporting Vars -extern __IO uint8_t Receive_Buffer[64]; -extern __IO uint32_t Receive_length ; -extern __IO uint32_t length ; -uint8_t Send_Buffer[64]; -uint32_t packet_sent=1; -uint32_t packet_receive=1; +//extern __IO uint8_t Receive_Buffer[64]; +//extern __IO uint32_t Receive_length ; +//extern __IO uint32_t length ; +//uint8_t Send_Buffer[64]; +//uint32_t packet_sent=1; +//uint32_t packet_receive=1; enum tempunits { TEMP_UNITS_CELSIUS = 0, @@ -47,7 +41,7 @@ int16_t setpoint_brew = 0; int16_t setpoint_steam = 0; // HAL Variables -SPI_HandleTypeDef hspi1; +//extern SPI_HandleTypeDef spi1; // State definition @@ -92,20 +86,20 @@ int main(void) init_gpio(); // Turn on power LED - GPIO_SetBits(LED_POWER); + HAL_GPIO_WritePin(LED_POWER, 1); // TODO: Awesome pwm of power LED (TIM4_CH4 or TIM11_CH1) // Configure 1ms SysTick (change if more temporal resolution needed) - RCC_ClocksTypeDef RCC_Clocks; - RCC_GetClocksFreq(&RCC_Clocks); - SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000); + //RCC_ClocksTypeDef RCC_Clocks; + //RCC_GetClocksFreq(&RCC_Clocks); + //SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000); // Init SPI busses - init_spi(); + //init_spi(&spi1); // Init USB - init_usb(); + //MX_USB_DEVICE_Init(); // Init OLED over SPI ssd1306_Init(); @@ -139,7 +133,7 @@ int main(void) if(boottobrew) state = STATE_PREHEAT_BREW; // Go to brew instead of idle if configured thusly - GPIO_ResetBits(LED_STAT); + HAL_GPIO_WritePin(LED_STAT, 0); // Main loop while(1) @@ -157,15 +151,26 @@ int32_t temp = 0; uint8_t temp_frac = 0; uint8_t state_resume = 0; + +// FIXME: Now this needs to work 8bits at a time, or change the port mode on the fly void update_temp() { // Assert CS - GPIO_ResetBits(MAX_CS); + HAL_GPIO_WritePin(MAX_CS, 0); delay(1); // This may not clock at all... might need to send 16 bits first - SPI_I2S_SendData(SPI2, 0xAAAA); // send dummy data - //SPI_I2S_SendData(SPI2, 0xAA); // send dummy data - uint16_t temp_pre = SPI_I2S_ReceiveData(SPI2); + // SPI_I2S_SendData(SPI2, 0xAAAA); // send dummy data + + uint8_t data[2] = {0xAA, 0xAA}; + //HAL_SPI_Transmit(&spi1, data, 2, 100); + + //OLD: SPI_I2S_SendData(SPI2, 0xAA); // send dummy data + // OLD: uint16_t temp_pre = SPI_I2S_ReceiveData(SPI2); + + //HAL_SPI_Receive(&spi1, data, 2, 100); + + // Assemble data array into one var + uint16_t temp_pre = data[0] & (data[1]<<8); if(temp_pre & 0b0000000000000010) { ssd1306_DrawString("Fatal Error", 3, 35); @@ -214,7 +219,7 @@ void update_temp() { // Deassert CS delay(1); - GPIO_SetBits(MAX_CS); + HAL_GPIO_WritePin(MAX_CS, 1); } @@ -285,7 +290,7 @@ void process() if(ticks - last_led > 400) { - GPIO_ToggleBits(LED_POWER); + HAL_GPIO_TogglePin(LED_POWER); last_led = ticks; } @@ -308,11 +313,11 @@ void process() if(ssr_output > 0) { char tempstr[6]; - itoa(ssr_output, tempstr); + itoa(ssr_output, tempstr, 10); ssd1306_DrawString(tempstr, 0, 90); - GPIO_SetBits(LED_STAT); - GPIO_SetBits(SSR_PIN); + HAL_GPIO_WritePin(LED_STAT, 1); + HAL_GPIO_WritePin(SSR_PIN, 1); last_ssr_on = ticks; } } @@ -320,8 +325,8 @@ void process() // Kill SSR after elapsed period less than SSR_PERIOD if(ticks - last_ssr_on > ssr_output || ssr_output == 0) { - GPIO_ResetBits(LED_STAT); - GPIO_ResetBits(SSR_PIN); + HAL_GPIO_WritePin(LED_STAT, 0); + HAL_GPIO_WritePin(SSR_PIN, 0); } } @@ -331,7 +336,7 @@ void draw_setpoint() { ssd1306_DrawStringBig(" ", 3, 0); ssd1306_DrawStringBig(tempstr, 3, 0); ssd1306_DrawStringBig(">", 3, 74); - itoa(setpoint, tempstr); + itoa(setpoint, tempstr, 10); ssd1306_DrawStringBig(" ", 3, 90); ssd1306_DrawStringBig(tempstr, 3, 90); } @@ -431,11 +436,11 @@ void machine() uint8_t temp_changed = temp != last_temp; last_temp = temp; - uint8_t sw_btn = !GPIO_ReadInputDataBit(SW_BTN); - uint8_t sw_up = !GPIO_ReadInputDataBit(SW_UP); - uint8_t sw_down = !GPIO_ReadInputDataBit(SW_DOWN); - uint8_t sw_left = !GPIO_ReadInputDataBit(SW_LEFT); - uint8_t sw_right = !GPIO_ReadInputDataBit(SW_RIGHT); + uint8_t sw_btn = !HAL_GPIO_ReadPin(SW_BTN); + uint8_t sw_up = !HAL_GPIO_ReadPin(SW_UP); + uint8_t sw_down = !HAL_GPIO_ReadPin(SW_DOWN); + uint8_t sw_left = !HAL_GPIO_ReadPin(SW_LEFT); + uint8_t sw_right = !HAL_GPIO_ReadPin(SW_RIGHT); switch(state) { @@ -512,7 +517,7 @@ void machine() ssd1306_drawlogo(); char tempstr[6]; - itoa(k_p, tempstr); + itoa(k_p, tempstr, 10); ssd1306_DrawString("P=", 1, 45); ssd1306_DrawString(" ", 1, 57); ssd1306_DrawString(tempstr, 1, 57); @@ -541,7 +546,7 @@ void machine() ssd1306_drawlogo(); char tempstr[6]; - itoa(k_i, tempstr); + itoa(k_i, tempstr, 10); ssd1306_DrawString("I=", 1, 45); ssd1306_DrawString(" ", 1, 57); ssd1306_DrawString(tempstr, 1, 57); @@ -570,7 +575,7 @@ void machine() ssd1306_drawlogo(); char tempstr[6]; - itoa(k_d, tempstr); + itoa(k_d, tempstr, 10); ssd1306_DrawString("D=", 1, 45); ssd1306_DrawString(" ", 1, 57); ssd1306_DrawString(tempstr, 1, 57); @@ -597,16 +602,16 @@ void machine() // [ Setpoint: ### ] char tempstr[6]; - itoa(final_setpoint, tempstr); + itoa(final_setpoint, tempstr, 10); ssd1306_DrawString("Step #", 0, 0); ssd1306_DrawString(tempstr, 0, 40); ssd1306_DrawString("Duration: ", 0, 5); - itoa(step_duration[final_setpoint], tempstr); + itoa(step_duration[final_setpoint], tempstr, 10); ssd1306_DrawString(tempstr, 0, 70); ssd1306_DrawString("Setpoint: ", 0, 0); - itoa(step_setpoint[final_setpoint], tempstr); + itoa(step_setpoint[final_setpoint], tempstr, 10); ssd1306_DrawString(tempstr, 0, 70); ssd1306_DrawString("Press to accept", 3, 40); @@ -638,7 +643,7 @@ void machine() ssd1306_drawlogo(); char tempstr[6]; - itoa(windup_guard, tempstr); + itoa(windup_guard, tempstr, 10); ssd1306_DrawString("G=", 1, 45); ssd1306_DrawString(" ", 1, 57); ssd1306_DrawString(tempstr, 1, 57); @@ -679,10 +684,10 @@ void machine() if(SW_BTN_PRESSED) { state = STATE_SETUNITS; } - else if(!GPIO_ReadInputDataBit(SW_UP)) { + else if(!HAL_GPIO_ReadPin(SW_UP)) { boottobrew = 1; } - else if(!GPIO_ReadInputDataBit(SW_DOWN)) { + else if(!HAL_GPIO_ReadPin(SW_DOWN)) { boottobrew = 0; } @@ -711,10 +716,10 @@ void machine() save_settings(); state = STATE_IDLE; } - else if(!GPIO_ReadInputDataBit(SW_UP)) { + else if(!HAL_GPIO_ReadPin(SW_UP)) { temp_units = TEMP_UNITS_FAHRENHEIT; } - else if(!GPIO_ReadInputDataBit(SW_DOWN)) { + else if(!HAL_GPIO_ReadPin(SW_DOWN)) { temp_units = TEMP_UNITS_CELSIUS; }