@@ -18,20 +18,29 @@ 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;
// Globalish setting vars
uint8_t boottobrew = 0;
#define WINDUP_GUARD_GAIN 100
uint16_t windup_guard = WINDUP_GUARD_GAIN;
uint16_t k_p = 1;
uint16_t k_i = 1;
uint16_t k_d = 1;
// State definition
enum state {
STATE_IDLE = 0,
STATE_SETP,
STATE_SETI,
STATE_SETD,
STATE_SETWINDUP,
STATE_SETBOOTTOBREW,
STATE_PREHEAT_BREW,
STATE_MAINTAIN_BREW,
STATE_PREHEAT_STEAM,
STATE_MAINTAIN_STEAM,
};
@@ -42,12 +51,14 @@ 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();
int main(void)
{
// Init clocks
SystemInit();
@@ -61,13 +72,12 @@ int main(void)
//USB_Init();
// Turn on power LED
GPIO_SetBits(LED_POWER);
// TODO: Awesome pwm of power LED (TIM4_CH4 or TIM11_CH1)
// TODO: PWM of stat led (TIM3_CH2)
// Configure 1ms SysTick (change if more temporal resolution needed)
RCC_ClocksTypeDef RCC_Clocks;
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
@@ -80,12 +90,16 @@ int main(void)
// Startup screen
ssd1306_DrawString("therm v0.1", 1, 40);
ssd1306_DrawString("protofusion.org/therm", 3, 0);
delay(1500);
ssd1306_clearscreen();
restore_settings();
GPIO_ResetBits(LED_STAT);
// Main loop
while(1)
@@ -144,14 +158,12 @@ void update_temp() {
// PID implementation
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
int16_t error = (int16_t)setpoint - (int16_t)temp; // TODO: Use fixed point fraction
@@ -199,15 +211,12 @@ int16_t update_pid(uint16_t k_p, uint16_
uint32_t ticks = 0;
uint32_t last_ssr_on = 0;
uint32_t last_led = 0;
int32_t setpoint = 0;
int16_t ssr_output = 0; // Duty cycle of ssr, 0 to SSR_PERIOD
uint8_t pid_enabled = 0;
// Process things
void process()
@@ -281,12 +290,66 @@ uint8_t sw_right_last = 0;
#define SW_BTN_PRESSED (sw_btn_last == 0 && sw_btn == 1) // rising edge on buttonpress
#define SW_UP_PRESSED (sw_up_last == 0 && sw_up == 1)
#define SW_DOWN_PRESSED (sw_down_last == 0 && sw_down == 1)
#define SW_LEFT_PRESSED (sw_left_last == 0 && sw_left == 1)
#define SW_RIGHT_PRESSED (sw_right_last == 0 && sw_right == 1)
/*
* uint8_t boottobrew = 0;
uint16_t k_d = 1;*/
#define EEPROM_ADDR_WINDUP_GUARD 0x0808001C
#define EEPROM_ADDR_BOOTTOBREW 0x08080020
#define EEPROM_ADDR_K_P 0x8080024
#define EEPROM_ADDR_K_I 0x8080028
#define EEPROM_ADDR_K_D 0x808002C
void save_settings()
DATA_EEPROM_Unlock();
// Try programming a word at an address divisible by 4
DATA_EEPROM_ProgramWord(EEPROM_ADDR_BOOTTOBREW, boottobrew);
DATA_EEPROM_ProgramWord(EEPROM_ADDR_WINDUP_GUARD, windup_guard);
DATA_EEPROM_ProgramWord(EEPROM_ADDR_K_P, k_p);
DATA_EEPROM_ProgramWord(EEPROM_ADDR_K_I, k_i);
DATA_EEPROM_ProgramWord(EEPROM_ADDR_K_D, k_d);
DATA_EEPROM_Lock();
}
void restore_settings()
while(FLASH_GetStatus()==FLASH_BUSY);
boottobrew = (*(__IO uint32_t*)EEPROM_ADDR_BOOTTOBREW);
windup_guard = (*(__IO uint32_t*)EEPROM_ADDR_WINDUP_GUARD);
k_p = (*(__IO uint32_t*)EEPROM_ADDR_K_P);
k_i = (*(__IO uint32_t*)EEPROM_ADDR_K_I);
k_d = (*(__IO uint32_t*)EEPROM_ADDR_K_D);
char tempstr[6];
itoa(windup_guard, tempstr);
ssd1306_DrawString("Read: ", 3, 40);
ssd1306_DrawString(" ", 3, 72);
ssd1306_DrawString(tempstr, 3, 72);
void machine()
uint8_t last_state = state;
uint8_t sw_btn = !GPIO_ReadInputDataBit(SW_BTN);
uint8_t sw_up = !GPIO_ReadInputDataBit(SW_UP);
@@ -470,13 +533,13 @@ void machine()
ssd1306_DrawString(tempstr, 1, 57);
ssd1306_DrawString("Press to accept", 3, 40);
// Button handler
if(SW_BTN_PRESSED) {
state = STATE_IDLE;
state = STATE_SETBOOTTOBREW;
else if(!GPIO_ReadInputDataBit(SW_UP)) {
windup_guard++;
else if(!GPIO_ReadInputDataBit(SW_DOWN) && windup_guard > 0) {
windup_guard--;
@@ -484,12 +547,46 @@ void machine()
// Event Handler
// N/A
} break;
case STATE_SETBOOTTOBREW:
// Write text to OLED
// [ therm :: set windup ]
// [ g = 12 ]
ssd1306_DrawString("Boot to Brew", 0, 40);
ssd1306_drawlogo();
ssd1306_DrawString("btb=", 1, 45);
if(boottobrew)
ssd1306_DrawString("Enabled ", 1, 70);
else
ssd1306_DrawString("Disabled", 1, 70);
save_settings();
boottobrew = 1;
else if(!GPIO_ReadInputDataBit(SW_DOWN)) {
boottobrew = 0;
case STATE_PREHEAT_BREW:
@@ -611,14 +708,17 @@ void machine()
if(last_state != state) {
// Clear screen on state change
goto_mode = 2;
// Last buttonpress
sw_btn_last = sw_btn;
sw_up_last = sw_up;
sw_down_last = sw_down;
sw_left_last = sw_left;
sw_right_last = sw_right;
Status change: