diff --git a/main.c b/main.c --- a/main.c +++ b/main.c @@ -3,6 +3,8 @@ #include "ssd1306.h" #include "config.h" #include "eeprom_min.h" +#include "gpio.h" +#include "spi.h" // USB includes #include "hw_config.h" @@ -23,21 +25,14 @@ uint32_t packet_receive=1; // Globalish setting vars uint8_t boottobrew = 0; -#define WINDUP_GUARD_GAIN 100 -uint16_t windup_guard = WINDUP_GUARD_GAIN; +uint16_t windup_guard = 1; uint16_t k_p = 1; uint16_t k_i = 1; uint16_t k_d = 1; -// ISR ticks var TODO: Double check functionality after volatilazation... needed on ARM? +// ISR ticks var volatile uint32_t ticks = 0; -// Increase on each press, and increase at a fast rate after duration elapsed of continuously holding down... somehow... -uint32_t change_time_reset = 0; -#define CHANGE_PERIOD_MS 100 -#define CHANGE_ELAPSED (ticks - change_time_reset) > CHANGE_PERIOD_MS -#define CHANGE_RESET change_time_reset = ticks - int16_t setpoint_brew = 0; int16_t setpoint_steam = 0; @@ -62,18 +57,15 @@ uint8_t state = STATE_IDLE; static __IO uint32_t TimingDelay; // Move to header file -void init_gpio(); -void init_spi(); void process(); void machine(); -void delay(__IO uint32_t nTime); + void restore_settings(); void save_settings(); void save_setpoints(); int main(void) { - // Init clocks SystemInit(); @@ -182,7 +174,6 @@ int16_t last_pid_temp = 0; uint8_t last_pid_temp_frac = 0; int16_t i_state = 0; - int16_t update_pid(uint16_t k_p, uint16_t k_i, uint16_t k_d, int16_t temp, uint8_t temp_frac, int16_t setpoint) { // Calculate instantaneous error @@ -200,7 +191,7 @@ int16_t update_pid(uint16_t k_p, uint16_ // it cant help be cold despite its best efforts) // not necessary, but this makes windup guard values // relative to the current iGain - int16_t windup_guard_res = WINDUP_GUARD_GAIN / k_i; + int16_t windup_guard_res = windup_guard / k_i; // Calculate integral term with windup guard if (i_state > windup_guard_res) @@ -229,8 +220,6 @@ int16_t update_pid(uint16_t k_p, uint16_ } - - uint32_t last_ssr_on = 0; uint32_t last_led = 0; int32_t setpoint = 0; @@ -314,15 +303,6 @@ uint8_t sw_right_last = 0; #define SW_LEFT_PRESSED (sw_left_last == 0 && sw_left == 1) #define SW_RIGHT_PRESSED (sw_right_last == 0 && sw_right == 1) -#define EEPROM_ADDR_WINDUP_GUARD 0x001C -#define EEPROM_ADDR_BOOTTOBREW 0x0020 -#define EEPROM_ADDR_K_P 0x0024 -#define EEPROM_ADDR_K_I 0x0028 -#define EEPROM_ADDR_K_D 0x002C - -#define EEPROM_ADDR_BREWTEMP 0x0030 -#define EEPROM_ADDR_STEAMTEMP 0x0034 - void save_settings() { Minimal_EEPROM_Unlock(); @@ -371,24 +351,14 @@ void restore_settings() Minimal_EEPROM_Lock(); } - -void user_input(uint16_t* to_modify) -{ - if(CHANGE_ELAPSED) { - if(!GPIO_ReadInputDataBit(SW_UP) ) { - CHANGE_RESET; - (*to_modify)++; - } - else if(!GPIO_ReadInputDataBit(SW_DOWN) && (*to_modify) > 0) { - CHANGE_RESET; - (*to_modify)--; - } - } -} +int16_t last_temp = 21245; void machine() { uint8_t last_state = state; + + 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); @@ -406,11 +376,13 @@ void machine() ssd1306_DrawString("therm :: idle ", 0, 40); pid_enabled = 0; - char tempstr[6]; - itoa_fp(temp, temp_frac, tempstr); - ssd1306_DrawString("Temp: ", 3, 40); - ssd1306_DrawString(" ", 3, 72); - ssd1306_DrawString(tempstr, 3, 72); + if(temp_changed) { + char tempstr[6]; + itoa_fp(temp, temp_frac, tempstr); + ssd1306_DrawString("Temp: ", 3, 40); + ssd1306_DrawString(" ", 3, 72); + ssd1306_DrawString(tempstr, 3, 72); + } ssd1306_drawlogo(); @@ -738,7 +710,6 @@ void machine() sw_right_last = sw_right; } - // Delay a number of systicks void delay(__IO uint32_t nTime) { @@ -756,154 +727,4 @@ void TimingDelay_Decrement(void) ticks++; } -void init_spi(void) -{ - SPI_InitTypeDef SPI_InitStructure; - - // OLED IC - SPI_Cmd(SPI1, DISABLE); - SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx; - 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_BaudRatePrescaler = SPI_BaudRatePrescaler_4; - SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; - SPI_InitStructure.SPI_CRCPolynomial = 7; - SPI_Init(SPI1, &SPI_InitStructure); - SPI_Cmd(SPI1, ENABLE); /* Enable the SPI */ - - - // MAX IC - SPI_Cmd(SPI2, DISABLE); - SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; - SPI_InitStructure.SPI_Mode = SPI_Mode_Master; - 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 - SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; - SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; - SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; - SPI_InitStructure.SPI_CRCPolynomial = 7; - SPI_Init(SPI2, &SPI_InitStructure); - SPI_Cmd(SPI2, ENABLE); /* Enable the SPI */ -} - -void init_gpio(void) { - - GPIO_InitTypeDef GPIO_InitStruct; - - // Enable SPI clocks - RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); - RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); - - // Enable GPIO clocks - RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC|RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_GPIOA, ENABLE); - - // Enable DMA clocks (Is AHB even the right thing???) - RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); // EMZ TODO get the right ones - - /*Configure GPIO pin : PC */ - GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13; - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; - GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; - GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; - GPIO_InitStruct.GPIO_Speed = GPIO_Speed_400KHz; - GPIO_Init(GPIOC, &GPIO_InitStruct); - - /*Configure GPIO pin : PB */ - GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_10|GPIO_Pin_12 - |GPIO_Pin_9; - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; - GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; - GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; - GPIO_InitStruct.GPIO_Speed = GPIO_Speed_400KHz; - GPIO_Init(GPIOB, &GPIO_InitStruct); - - /*Configure GPIO pin : PA */ - GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15; - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; - GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; - GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; - GPIO_InitStruct.GPIO_Speed = GPIO_Speed_400KHz; - GPIO_Init(GPIOA, &GPIO_InitStruct); - - /*Configure GPIO pin : PB */ - GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6 - |GPIO_Pin_7; - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; - GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; - GPIO_Init(GPIOB, &GPIO_InitStruct); - - /** SPI1 GPIO Configuration - PA5 ------> SPI1_SCK - PA7 ------> SPI1_MOSI - */ - - /*Enable or disable the AHB peripheral clock */ - RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); - - /*Configure GPIO pin : PA: MOSI,SCK */ - GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7; - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; - GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; - GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz; - GPIO_Init(GPIOA, &GPIO_InitStruct); - - /*Configure GPIO pin alternate function */ - GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1); - - /*Configure GPIO pin alternate function */ - GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1); - - /** SPI2 GPIO Configuration - PB13 ------> SPI2_SCK - PB14 ------> SPI2_MISO - PB15 ------> SPI2_MOSI - */ - - /*Enable or disable the AHB peripheral clock */ - RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); - -// SPI PINSSS - - /*Configure GPIO pin : PB, MOSI, SCK */ - GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_15; - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; - GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; - GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz; - GPIO_Init(GPIOB, &GPIO_InitStruct); - - GPIO_InitTypeDef GPIO_InitStruct2; -// MISO - GPIO_InitStruct2.GPIO_Pin = GPIO_Pin_14; - GPIO_InitStruct2.GPIO_Mode = GPIO_Mode_AF; - GPIO_InitStruct2.GPIO_PuPd = GPIO_PuPd_NOPULL; - GPIO_InitStruct2.GPIO_Speed = GPIO_Speed_10MHz; - GPIO_Init(GPIOB, &GPIO_InitStruct2); - - - //Configure GPIO pin alternate function - GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2); - GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2); - GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2); - - - /** USB GPIO Configuration - PA11 ------> USB_DM - PA12 ------> USB_DP - */ - - /*Enable or disable the AHB peripheral clock */ - RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); - - /*Configure GPIO pin : PA */ - GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_12; - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; - GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; - GPIO_InitStruct.GPIO_Speed = GPIO_Speed_400KHz; - GPIO_Init(GPIOA, &GPIO_InitStruct); -} - // vim:softtabstop=4 shiftwidth=4 expandtab