# STM32F0xx Makefile
# #####################################
#
# Part of the uCtools project
# uctools.github.com
#######################################
# user configuration:
# SOURCES: list of sources in the user application
SOURCES = main.c usbd_conf.c usbd_cdc_if.c usb_device.c usbd_desc.c stm32f0xx_hal_msp.c stm32f0xx_it.c system_stm32f0xx.c gpio.c spi.c ssd1306.c stringhelpers.c eeprom_min.c display.c bootlib.c
SOURCES = main.c usbd_conf.c usbd_cdc_if.c usb_device.c usbd_desc.c stm32f0xx_hal_msp.c stm32f0xx_it.c system_stm32f0xx.c gpio.c spi.c ssd1306.c stringhelpers.c display.c bootlib.c
# TARGET: name of the user application
TARGET = main
# BUILD_DIR: directory to place output files in
BUILD_DIR = build
# LD_SCRIPT: location of the linker script
LD_SCRIPT = stm32f042c6_flash.ld
# USER_DEFS user defined macros
USER_DEFS = -D HSI48_VALUE=48000000 -D HSE_VALUE=16000000
# USER_INCLUDES: user defined includes
USER_INCLUDES =
# USB_INCLUDES: includes for the usb library
USB_INCLUDES = -Imiddlewares/ST/STM32_USB_Device_Library/Core/Inc
USB_INCLUDES += -Imiddlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc
# USER_CFLAGS: user C flags (enable warnings, enable debug info)
USER_CFLAGS = -Wall -g -ffunction-sections -fdata-sections -Os
# USER_LDFLAGS: user LD flags
USER_LDFLAGS = -fno-exceptions -ffunction-sections -fdata-sections -Wl,--gc-sections
# TARGET_DEVICE: device to compile for
TARGET_DEVICE = STM32F042x6
# end of user configuration
# binaries
CC = arm-none-eabi-gcc
AR = arm-none-eabi-ar
RANLIB = arm-none-eabi-ranlib
SIZE = arm-none-eabi-size
OBJCOPY = arm-none-eabi-objcopy
MKDIR = mkdir -p
# core and CPU type for Cortex M0
# ARM core type (CORE_M0, CORE_M3)
CORE = CORE_M0
# ARM CPU type (cortex-m0, cortex-m3)
CPU = cortex-m0
# where to build STM32Cube
CUBELIB_BUILD_DIR = $(BUILD_DIR)/STM32Cube
# various paths within the STmicro library
CMSIS_PATH = drivers/CMSIS
CMSIS_DEVICE_PATH = $(CMSIS_PATH)/Device/ST/STM32F0xx
DRIVER_PATH = drivers/STM32F0xx_HAL_Driver
# includes for gcc
INCLUDES = -I$(CMSIS_PATH)/Include
INCLUDES += -I$(CMSIS_DEVICE_PATH)/Include
INCLUDES += -I$(DRIVER_PATH)/Inc
INCLUDES += -I$(CURDIR)
INCLUDES += -I$(CURDIR)/usb
INCLUDES += $(USB_INCLUDES)
INCLUDES += $(USER_INCLUDES)
# macros for gcc
DEFS = -D$(CORE) $(USER_DEFS) -D$(TARGET_DEVICE)
# compile gcc flags
CFLAGS = $(DEFS) $(INCLUDES)
CFLAGS += -mcpu=$(CPU) -mthumb
CFLAGS += $(USER_CFLAGS)
# default action: build the user application
all: $(BUILD_DIR)/$(TARGET).hex
# build the st micro peripherial library
# (drivers and CMSIS)
CUBELIB = $(CUBELIB_BUILD_DIR)/libstm32cube.a
# List of stm32 driver objects
CUBELIB_DRIVER_OBJS = $(addprefix $(CUBELIB_BUILD_DIR)/, $(patsubst %.c, %.o, $(notdir $(wildcard $(DRIVER_PATH)/Src/*.c))))
# shortcut for building core library (make cubelib)
cubelib: $(CUBELIB)
$(CUBELIB): $(CUBELIB_DRIVER_OBJS)
$(AR) rv $@ $(CUBELIB_DRIVER_OBJS)
$(RANLIB) $@
$(CUBELIB_BUILD_DIR)/%.o: $(DRIVER_PATH)/Src/%.c | $(CUBELIB_BUILD_DIR)
$(CC) -c $(CFLAGS) -o $@ $^
$(CUBELIB_BUILD_DIR):
$(MKDIR) $@
# build the USB library
USB_MIDDLEWARE_PATH = ./middlewares/ST/STM32_USB_Device_Library/
USB_BUILD_DIR = $(BUILD_DIR)/usb
USB_SOURCES += usbd_ctlreq.c usbd_ioreq.c usbd_core.c usbd_cdc.c
# list of usb library objects
USB_OBJECTS += $(addprefix $(USB_BUILD_DIR)/,$(notdir $(USB_SOURCES:.c=.o)))
usb: $(USB_OBJECTS)
$(USB_BUILD_DIR)/%.o: $(USB_MIDDLEWARE_PATH)/Core/Src/%.c | $(USB_BUILD_DIR)
$(CC) -Os $(CFLAGS) -c -o $@ $^
$(USB_BUILD_DIR)/%.o: $(USB_MIDDLEWARE_PATH)/Class/CDC/Src/%.c | $(USB_BUILD_DIR)
$(USB_BUILD_DIR):
@echo $(USB_BUILD_DIR)
# build the user application
# list of user program objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(SOURCES:.c=.o)))
# add an object for the startup code
OBJECTS += $(BUILD_DIR)/startup_stm32f042x6.o
# use the periphlib core library, plus generic ones (libc, libm, libnosys)
LIBS = -lstm32cube -lc -lm -lnosys
LDFLAGS = -T $(LD_SCRIPT) -L $(CUBELIB_BUILD_DIR) $(LIBS) $(USER_LDFLAGS)
$(BUILD_DIR)/$(TARGET).hex: $(BUILD_DIR)/$(TARGET).elf
$(OBJCOPY) -O ihex $(BUILD_DIR)/$(TARGET).elf $@
$(OBJCOPY) -O binary $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).bin
$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) $(USB_OBJECTS) $(CUBELIB)
$(CC) -o $@ $(CFLAGS) $(USER_LDFLAGS) $(OBJECTS) $(USB_OBJECTS) \
-L$(CUBELIB_BUILD_DIR) -static $(LIBS) -Xlinker \
-Map=$(BUILD_DIR)/$(TARGET).map \
-T $(LD_SCRIPT)
$(SIZE) $@
$(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)
$(CC) $(CFLAGS) -Os -c -o $@ $^
$(BUILD_DIR)/%.o: %.s | $(BUILD_DIR)
$(CC) $(CFLAGS) -c -o $@ $^
$(BUILD_DIR):
# delete all user application files, keep the libraries
clean:
-rm $(BUILD_DIR)/*.o
-rm $(BUILD_DIR)/*.elf
-rm $(BUILD_DIR)/*.bin
-rm $(BUILD_DIR)/*.map
.PHONY: clean all cubelib
#include "stm32f0xx_hal.h"
#include "config.h"
#include "states.h"
#include "ssd1306.h"
#include "eeprom_min.h"
#include "gpio.h"
#include "spi.h"
#include "stringhelpers.h"
#include "display.h"
#include "usb_device.h"
#include "usbd_cdc_if.h"
// Prototypes
// Move to header file
void process();
void restore_settings();
void save_settings();
void save_setpoints();
void SystemClock_Config(void);
therm_settings_t set;
therm_status_t status;
// Globalish setting vars
SPI_HandleTypeDef hspi1;
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();
/* Unset bootloader option bytes (if set) */
void bootloader_unset(void);
/* Initialize all configured peripherals */
init_gpio();
MX_USB_DEVICE_Init();
// USB startup delay
HAL_Delay(1000);
HAL_GPIO_WritePin(LED_POWER, 1);
// 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();
// Default settings
set.boottobrew = 0;
set.temp_units = TEMP_UNITS_CELSIUS;
set.windup_guard = 1;
set.k_p = 1;
set.k_i = 1;
set.k_d = 1;
set.ignore_tc_error = 0;
set.setpoint_brew = 0;
set.setpoint_steam = 0;
// Default status
status.temp = 0;
status.temp_frac = 0;
status.state_resume = 0;
status.state = STATE_IDLE;
status.setpoint = 0;
status.pid_enabled = 0;
// Load settings (if any) from EEPROM
restore_settings();
// Go to brew instead of idle if configured thusly
if(set.boottobrew)
status.state = STATE_PREHEAT_BREW;
// Startup screen
ssd1306_DrawString("therm v0.2", 1, 40);
ssd1306_DrawString("protofusion.org/therm", 3, 0);
HAL_Delay(1500);
// Main loop
while(1)
// Process sensor inputs
process();
// Run state machine
display_process(&set, &status);
// 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();
// Grab temperature reading from MAX31855
void update_temp() {
// Assert CS
HAL_GPIO_WritePin(MAX_CS, 0);
uint8_t rxdatah[1] = {0x00};
uint8_t rxdatal[1] = {0x00};
HAL_SPI_Receive(&hspi1, rxdatah, 1, 100);
HAL_SPI_Receive(&hspi1, rxdatal, 1, 100);
// Release CS
HAL_GPIO_WritePin(MAX_CS, 1);
// Assemble data array into one var
uint16_t temp_pre = rxdatal[0] | (rxdatah[0]<<8);
if(temp_pre & 0b0000000000000010) {
HAL_Delay(100); // FIXME: remove?
status.tc_errno = 4;
status.state = STATE_TC_ERROR;
else if(temp_pre & 0b0000000000000001 && !set.ignore_tc_error) {
status.tc_errno = 1;
status.state_resume = status.state;
else
if(status.state == STATE_TC_ERROR)
status.state = status.state_resume;
uint8_t sign = status.temp >> 15;// top bit is sign
temp_pre = temp_pre >> 2; // Drop 2 lowest bits
status.temp_frac = temp_pre & 0b11; // get fractional part
status.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) {
signint = -1;
Status change: