@@ -67,213 +67,203 @@ static __IO uint32_t TimingDelay;
void deinit(void)
{
HAL_DeInit();
}
volatile int i=0;
int main(void)
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
init_gpio();
MX_USB_DEVICE_Init();
// USB startup delay
HAL_Delay(1000);
HAL_GPIO_WritePin(LED_POWER, 1);
/// BEGIN IMPORT/////////////////////////////////////////
// TODO: Awesome pwm of power LED
// Configure 1ms SysTick (change if more temporal resolution needed)
//RCC_ClocksTypeDef RCC_Clocks;
//RCC_GetClocksFreq(&RCC_Clocks);
//SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
// Init SPI busses
init_spi();
// Init OLED over SPI
ssd1306_Init();
ssd1306_clearscreen();
// Startup screen
ssd1306_DrawString("therm v0.1", 1, 40);
ssd1306_DrawString("protofusion.org/therm", 3, 0);
HAL_Delay(1500);
restore_settings();
if(boottobrew)
state = STATE_PREHEAT_BREW; // Go to brew instead of idle if configured thusly
// Main loop
while(1)
// Process sensor inputs
process();
// Run state machine
machine();
/* OLD MAIN
for (;;) {
CDC_Transmit_FS("a", 1); //sizeof(str)); for(count=0; count<32000000; count++); //#endif
HAL_Delay(300);
*/
/** System Clock Configuration
void SystemClock_Config(void)
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48;
RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
__SYSCFG_CLK_ENABLE();
// Read temperature and update global temp vars
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
HAL_GPIO_WritePin(MAX_CS, 0);
HAL_Delay(100);
// This may not clock at all... might need to send 16 bits first
// SPI_I2S_SendData(SPI2, 0xAAAA); // send dummy data
uint8_t rxdatah[1] = {0x00};
uint8_t rxdatal[1] = {0x00};
HAL_SPI_Receive(&hspi1, rxdatah, 1, 100);
HAL_SPI_Receive(&hspi1, rxdatal, 1, 100);
// Deassert CS
// Release CS
HAL_Delay(1);
HAL_GPIO_WritePin(MAX_CS, 1);
//OLD: SPI_I2S_SendData(SPI2, 0xAA); // send dummy data
// OLD: uint16_t temp_pre = SPI_I2S_ReceiveData(SPI2);
// Assemble data array into one var
uint16_t temp_pre = rxdatal[0] | (rxdatah[0]<<8);
if(temp_pre & 0b0000000000000010) {
ssd1306_DrawString("Fatal Error", 3, 35);
state = STATE_TC_ERROR;
else if(temp_pre & 0b0000000000000001 && !ignore_tc_error) {
state_resume = state;
temp = 0;
temp_frac = 0;
else
if(state == STATE_TC_ERROR)
state = state_resume;
uint8_t sign = temp >> 15;// top bit is sign
temp_pre = temp_pre >> 2; // Drop 2 lowest bits
temp_frac = temp_pre & 0b11; // get fractional part
temp_frac *= 25; // each bit is .25 a degree, up to fixed point
temp_pre = temp_pre >> 2; // Drop 2 fractional bits
int8_t signint;
if(sign) {
temp = -temp_pre;
signint = -1;
else {
temp = temp_pre;
signint = 1;
if(temp_units == TEMP_UNITS_FAHRENHEIT) {
temp *= 9; // fixed point mul by 1.8
temp /= 5;
temp += 32;
// Convert to Fahrenheit
if(temp_units == TEMP_UNITS_FAHRENHEIT)
temp = signint * ((temp_pre*100) + temp_frac);
temp = temp * 1.8;
temp += 3200;
temp_frac = temp % 100;
temp /= 100;
temp_frac *= 9;
temp_frac /= 5;
temp_frac += 32;
temp += temp_frac/100; // add overflow to above
temp_frac %= 100;
// Use Celsius values
temp = temp_pre * signint;
// Print temp to cdc
CDC_Transmit_FS("Temp: ", 6);
char tempstr[6];
zitoa(temp, tempstr);
CDC_Transmit_FS(tempstr, 1); //sizeof(tempstr));
CDC_Transmit_FS(tempstr, sizeof(tempstr));
CDC_Transmit_FS("\r\n", 2);
// PID implementation
// TODO: Make struct that has the last_temp and i_state in it, pass by ref. Make struct that has other input values maybe.
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
// Proportional component
int16_t p_term = k_p * error;
// Error accumulator (integrator)
i_state += error;
// to prevent the iTerm getting huge despite lots of
// error, we use a "windup guard"
// (this happens when the machine is first turned on and
Status change: