#include "main.h"
#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 SSR_PERIOD 200
#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
// TODO: Eliminate screen buffer since we aren't using it...
#define SW_BTN GPIOB, GPIO_Pin_3
#define SW_UP GPIOB, GPIO_Pin_4
#define SW_DOWN GPIOB, GPIO_Pin_6
#define SW_LEFT GPIOB, GPIO_Pin_5
#define SW_RIGHT GPIOB, GPIO_Pin_7
// 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;
// State definition
enum state {
STATE_IDLE = 0,
STATE_SETP,
STATE_SETI,
STATE_SETD,
STATE_PREHEAT_BREW,
STATE_MAINTAIN_BREW,
STATE_PREHEAT_STEAM,
STATE_MAINTAIN_STEAM,
};
char* itoa(int16_t i, char b[]){
char const digit[] = "0123456789";
char* p = b;
if(i<0){
*p++ = '-';
i *= -1;
}
uint16_t shifter = i;
do{ //Move to where representation ends
++p;
shifter = shifter/10;
@@ -104,157 +121,147 @@ static __IO uint32_t TimingDelay;
// Move to header file
void init_gpio();
void init_spi();
void process();
void machine();
int main(void)
{
// Init clocks
SystemInit();
// Init GPIO
init_gpio();
// Init USB
//Set_USBClock();
//USB_Interrupts_Config();
//USB_Init();
// Turn on power LED
GPIO_SetBits(LED_POWER);
RCC_ClocksTypeDef RCC_Clocks;
// TODO: Awesome pwm of power LED (TIM4_CH4 or TIM11_CH1)
// TODO: PWM of stat led (TIM3_CH2)
// SysTick end of count event each 1ms
// Configure 1ms SysTick (change if more temporal resolution needed)
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
GPIO_ResetBits(LED_STAT);
Delay(100);
Delay(500);
GPIO_ResetBits(LED_POWER);
// Init SPI busses
init_spi();
// Init OLED over SPI
ssd1306_Init();
ssd1306_block_write();
ssd1306_DrawString("therm 0.1", 0, 40);
uint8_t toggle = 0;
// Startup screen
ssd1306_DrawString("therm v0.1", 1, 40);
ssd1306_DrawString("protofusion.org/therm", 3, 0);
Delay(1500);
int16_t temp = -231;
// Main loop
while(1)
//ssd1306_block_write();
// Process sensor inputs [TODO: 5hz?]
// Process sensor inputs
process();
// Run state machine [TODO: 50hz?]
// Run state machine
machine();
// probably just passed the actual port
//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);
Delay(50);
// Read temperature and update global temp vars
int16_t temp = 0;
uint8_t temp_frac = 0;
void update_temp() {
// Assert CS
GPIO_ResetBits(MAX_CS);
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);
if(temp_pre & 0b0000000000000010) {
ssd1306_DrawString("Fatal Error", 2, 35);
else if(temp_pre & 0b0000000000000001) {
ssd1306_DrawString("TC Fault", 2, 35);
ssd1306_DrawString("Error: No TC", 2, 40);
temp = 0;
temp_frac = 0;
uint8_t sign = temp >> 15;// top bit is sign
else
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
if(sign) {
temp = -temp_pre;
else {
temp = temp_pre;
// Deassert CS
GPIO_SetBits(MAX_CS);
uint32_t ticks = 0;
uint32_t last_ssr_on = 0;
uint32_t last_led = 0;
int32_t setpoint = 0;
uint16_t k_p = 1;
uint16_t k_i = 1;
uint16_t k_d = 1;
uint8_t ssr_output = 0; // Duty cycle of ssr, 0 to SSR_PERIOD
// Process things
void process()
update_temp(); // Read MAX31855
// TODO: Add calibration offset (linear)
// Perform PID calculations
//if(
GPIO_SetBits(LED_STAT);
// Write output to SSR
if(ticks - last_led > 400)
GPIO_ToggleBits(LED_POWER);
last_led = ticks;
// Every 200ms, set the SSR on
if(ticks - last_ssr_on > SSR_PERIOD)
last_ssr_on = ticks;
// Kill SSR after elapsed period less than SSR_PERIOD
if(ticks - last_ssr_on > ssr_output || !ssr_output)
void draw_setpoint() {
char tempstr[3];
itoa_fp(temp, temp_frac, tempstr);
//ssd1306_DrawString(" ", 3, 40);
ssd1306_DrawString(tempstr, 3, 40);
ssd1306_DrawString("-> ", 3, 80);
itoa(setpoint, tempstr);
ssd1306_DrawString(" ", 3, 95);
ssd1306_DrawString(tempstr, 3, 95);
@@ -493,29 +500,27 @@ void machine()
draw_setpoint();
// Button handler
if(!GPIO_ReadInputDataBit(SW_BTN)) {
state = STATE_IDLE;
// Event Handler
// N/A
} break;
// Something is terribly wrong
default:
ssd1306_DrawString("therm :: BAD BAD", 0, 40);
if(last_state != state) {
// Clear screen on state change
@@ -534,24 +539,25 @@ void Delay(__IO uint32_t nTime)
/**
* @brief Decrements the TimingDelay variable.
* @param None
* @retval None
*/
void TimingDelay_Decrement(void)
if (TimingDelay != 0x00)
TimingDelay--;
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;
Status change: