Changeset - da7e7cc3bb06
[Not reviewed]
cortex-f0
0 13 0
Ethan Zonca - 9 years ago 2015-11-28 13:18:08
ez@ethanzonca.com
Refactor and cleanup
13 files changed with 31 insertions and 140 deletions:
0 comments (0 inline, 0 general)
Makefile
Show inline comments
 
# STM32F0xx Makefile
 
# #####################################
 
#
 
# Part of the uCtools project
 
# uctools.github.com
 
#
 
#######################################
 
# user configuration:
 
#######################################
 

	
 

	
 
# SOURCES: list of sources in the user application
 
SOURCES = main.c system/usbd_conf.c system/usbd_cdc_if.c system/usb_device.c system/usbd_desc.c system/interrupts.c system/system_stm32f0xx.c gpio.c spi.c ssd1306.c stringhelpers.c display.c system/syslib.c storage.c flash.c max31855.c max31865.c
 
#SRC = $(shell find . -name *.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 = -Isystem
 

	
 
# 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 -fno-exceptions -fdata-sections -Os
 
# USER_LDFLAGS:  user LD flags
 
USER_LDFLAGS = -fno-exceptions -ffunction-sections -fno-exceptions -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)
 
	$(CC) -Os $(CFLAGS) -c -o $@ $^
 

	
 
$(USB_BUILD_DIR):
 
	@echo $(USB_BUILD_DIR)
 
	$(MKDIR) $@
 

	
 
#######################################
 
# build the user application
 
#######################################
 

	
 
SYSTEM_BUILD_DIR = $(BUILD_DIR)/system
 

	
 
# list of user program objects
 
#OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(SOURCES:.c=.o)))
 
OBJECTS = $(addprefix $(BUILD_DIR)/,$(SOURCES:.c=.o))
 
# add an object for the startup code
 
OBJECTS += $(BUILD_DIR)/startup_stm32f042x6.o
 
OBJECTS += $(BUILD_DIR)/system/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 | $(SYSTEM_BUILD_DIR)
 
	$(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) | $(SYSTEM_BUILD_DIR)
 
	$(CC) -o $@ $(CFLAGS) $(USER_LDFLAGS) $(OBJECTS) $(USB_OBJECTS) \
 
		-L$(CUBELIB_BUILD_DIR) -static $(LIBS) -Xlinker \
 
		-Map=$(BUILD_DIR)/$(TARGET).map \
 
		-T $(LD_SCRIPT)
 
	$(SIZE) $@
 

	
 
$(SYSTEM_BUILD_DIR):
 
	@echo $(SYSTEM_BUILD_DIR)
 
	$(MKDIR) $@
 

	
 
$(BUILD_DIR)/%.o: %.c | $(BUILD_DIR) $(SYSTEM_BUILD_DIR)
 
	$(CC) $(CFLAGS) -Os -c -o $@ $^
 

	
 
$(BUILD_DIR)/%.o: %.s | $(BUILD_DIR) $(SYSTEM_BUILD_DIR)
 
	$(CC) $(CFLAGS) -c -o $@ $^
 

	
 
$(BUILD_DIR):
 
	$(MKDIR) $@
 

	
 
# 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
display.c
Show inline comments
 
#include "stm32f0xx_hal.h"
 
#include "ssd1306.h"
 
#include "stringhelpers.h"
 
#include "display.h"
 
#include "config.h"
 
#include "states.h"
 
#include "syslib.h"
 
#include "flash.h"
 
#include "gpio.h"
 

	
 
uint8_t goto_mode = 2;
 
static uint8_t goto_mode = 2;
 

	
 
// State machine
 
uint8_t sw_btn_last = 0;
 
uint8_t sw_up_last = 0;
 
uint8_t sw_down_last = 0;
 
uint8_t sw_left_last = 0;
 
uint8_t sw_right_last = 0;
 
static uint8_t sw_btn_last = 0;
 
static uint8_t sw_up_last = 0;
 
static uint8_t sw_down_last = 0;
 
static uint8_t sw_left_last = 0;
 
static 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 trigger_drawsetpoint = 1;
 
static uint8_t trigger_drawsetpoint = 1;
 

	
 
int16_t last_temp = 21245;
 
static int16_t last_temp = 21245;
 

	
 
void display_process(therm_settings_t* set, therm_status_t* status)
 
{
 
    uint8_t last_state = status->state;
 
    
 
    uint8_t temp_changed = status->temp != last_temp;
 
    last_temp = status->temp;
 

	
 
    uint8_t sw_btn = !HAL_GPIO_ReadPin(SW_BTN);
 
    uint8_t sw_up = !HAL_GPIO_ReadPin(SW_UP);
 
    uint8_t sw_down = !HAL_GPIO_ReadPin(SW_DOWN);
 
    uint8_t sw_left = !HAL_GPIO_ReadPin(SW_LEFT);
 
    uint8_t sw_right = !HAL_GPIO_ReadPin(SW_RIGHT);
 

	
 
    switch(status->state)
 
    {
 
        // Idle state
 
        case STATE_IDLE:
 
        {
 
            // Write text to OLED
 
            // [ therm :: idle ]
 
            ssd1306_DrawString("therm :: idle ", 0, 40);
 
            status->pid_enabled = 0;
 

	
 
            if(temp_changed) {
 
                char tempstr[6];
 
                itoa_fp(status->temp, status->temp_frac, tempstr);
 
                ssd1306_DrawString("Temp: ", 3, 40);
 
                ssd1306_DrawString("    ", 3, 72);
 
                ssd1306_DrawString(tempstr, 3, 72);
 
            }
 

	
 
            ssd1306_drawlogo();
 

	
 
            switch(goto_mode) {
 
                case 3:
 
                {
 
                    ssd1306_DrawString("-> loader   ", 1, 40);
 
                } break;
 

	
 
                case 2:
 
                {
 
                    ssd1306_DrawString("-> heat     ", 1, 40);
 
                } break;
 

	
 
                case 1:
 
                {
 
                    ssd1306_DrawString("-> setup    ", 1, 40);
 
                } break;
 

	
 
                case 0:
 
                {
 
                    ssd1306_DrawString("-> reset    ", 1, 40);
 
                }
 
            }
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                switch(goto_mode) {
 
                    case 3:
 
                    {
 
                        ssd1306_clearscreen();
 
                        ssd1306_DrawString("Bootloader Entered", 0, 0);
 
                        ssd1306_DrawString("Device won't boot", 2, 0);
 
                        ssd1306_DrawString("until reflashed!", 3, 0);
 
                        bootloader_enter(); // Resets into bootloader
 
                        status->state = STATE_IDLE; // Just in case
 
                    } break;
 
                    case 2:
 
                        status->state = STATE_PREHEAT_BREW;
 
                        break;
 
                    case 1:
 
                        status->state = STATE_SETP;
 
                        break;
 
                    case 0:
 
                        status->state = STATE_IDLE;
 
                        //flash_erase();
 
                        NVIC_SystemReset(); 
 
                        break;
 

	
 
                    default:
 
                        status->state = STATE_PREHEAT_BREW;
 
                }
 
            }
 
            else if(SW_UP_PRESSED && goto_mode < 3) {
 
                goto_mode++;
 
            }
 
            else if(SW_DOWN_PRESSED && goto_mode > 0) {
 
                goto_mode--;
 
            }
 

	
 

	
 
            // Event Handler
 
            // N/A
 

	
 
        } break;
 

	
 
        case STATE_SETP:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set p ]
 
            // [ p = 12         ]
 
            ssd1306_DrawString("Proportional", 0, 40);
 
            ssd1306_drawlogo();
 

	
 
            char tempstr[6];
 
            itoa(set->val.k_p, tempstr, 10);
 
            ssd1306_DrawString("P=", 1, 45);
 
            ssd1306_DrawString("    ", 1, 57);
 
            ssd1306_DrawString(tempstr, 1, 57);
 

	
 
            ssd1306_DrawString("Press to accept", 3, 40);
 
            
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_SETI;
 
            }
 
            else {
 
                user_input(&set->val.k_p);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 

	
 
        case STATE_SETI:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set i ]
 
            // [ i = 12         ]
 
            ssd1306_DrawString("Integral", 0, 40);
 
            ssd1306_drawlogo();
 

	
 
            char tempstr[6];
 
            itoa(set->val.k_i, tempstr, 10);
 
            ssd1306_DrawString("I=", 1, 45);
 
            ssd1306_DrawString("    ", 1, 57);
 
            ssd1306_DrawString(tempstr, 1, 57);
 

	
 
            ssd1306_DrawString("Press to accept", 3, 40);
 
            
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_SETD;
 
            }
 
            else {
 
                user_input(&set->val.k_i);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 

	
 
        case STATE_SETD:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set d ]
 
            // [ d = 12         ]
 
            ssd1306_DrawString("Derivative", 0, 40);
 
            ssd1306_drawlogo();
 

	
 
            char tempstr[6];
 
            itoa(set->val.k_d, tempstr, 10);
 
            ssd1306_DrawString("D=", 1, 45);
 
            ssd1306_DrawString("    ", 1, 57);
 
            ssd1306_DrawString(tempstr, 1, 57);
 

	
 
            ssd1306_DrawString("Press to accept", 3, 40);
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_SETWINDUP;
 
            }
 
            else {
 
                user_input(&set->val.k_d);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 

	
 
        case STATE_SETWINDUP:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set windup ]
 
            // [ g = 12         ]
 
            ssd1306_DrawString("Windup Guard", 0, 40);
 
            ssd1306_drawlogo();
 

	
 
@@ -316,219 +316,219 @@ void display_process(therm_settings_t* s
 
            char tempstr[6];
 
            itoa(set->val.temp_offset, tempstr, 10);
 
            ssd1306_DrawString("O=", 1, 45);
 
            ssd1306_DrawString("    ", 1, 57);
 
            ssd1306_DrawString(tempstr, 1, 57);
 

	
 
            ssd1306_DrawString("Press to accept", 3, 40);
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                flash_save(&set);
 
                status->state = STATE_IDLE;
 
            }
 
            else {
 
                user_input_signed(&set->val.temp_offset);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 

	
 

	
 
        case STATE_PREHEAT_BREW:
 
        {
 
            // Write text to OLED
 
            // [ therm : preheating brew ]
 
            // [ 30 => 120 C             ]
 
            ssd1306_DrawString("Preheating...", 0, 0);
 
            //ssd1306_drawlogo();
 
            draw_setpoint(status);
 

	
 
            status->pid_enabled = 1;
 
	    status->setpoint = set->val.setpoint_brew;
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
		save_setpoints(&set); // TODO: Check for mod
 
                status->state = STATE_IDLE;
 
            }
 
            else {
 
                user_input(&set->val.setpoint_brew);
 
            }
 

	
 
            // Event Handler
 
            if(status->temp >= status->setpoint) {
 
                status->state = STATE_MAINTAIN_BREW;
 
            }
 
 
 
        } break;
 

	
 
        case STATE_MAINTAIN_BREW:
 
        {
 
            // Write text to OLED
 
            // [ therm : ready to brew ]
 
            // [ 30 => 120 C           ]
 
            ssd1306_DrawString("Preheated!", 0, 0);
 
            //ssd1306_drawlogo();
 
            draw_setpoint(status);
 
            status->pid_enabled = 1;
 
	    status->setpoint = set->val.setpoint_brew;
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
		save_setpoints(&set); // TODO: Check for mod
 
                status->state = STATE_IDLE;
 
            }
 
            else {
 
                user_input(&set->val.setpoint_brew);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 

	
 
        case STATE_PREHEAT_STEAM:
 
        {
 
            // Write text to OLED
 
            // [ therm : preheating steam ]
 
            // [ 30 => 120 C           ]
 
            ssd1306_DrawString("Preheating...", 0, 0);
 
            //ssd1306_drawlogo();
 
            draw_setpoint(status);
 
            status->pid_enabled = 1;
 
	    status->setpoint = set->val.setpoint_steam;
 
	    
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_IDLE;
 
		save_setpoints(&set); // TODO: Check for mod
 
            }
 
            else {
 
                user_input(&set->val.setpoint_steam);
 
            }
 

	
 
            // Event Handler
 
            if(status->temp >= status->setpoint) {
 
                status->state = STATE_MAINTAIN_STEAM;
 
            }
 
 
 
        } break;
 

	
 
        case STATE_MAINTAIN_STEAM:
 
        {
 
            // Write text to OLED
 
            // [ therm : ready to steam ]
 
            // [ 30 => 120 C            ]
 
            ssd1306_DrawString("Ready to Steam!", 0, 0);
 
            //ssd1306_drawlogo();
 
            draw_setpoint(status);
 
            status->pid_enabled = 1;
 
	    status->setpoint = set->val.setpoint_steam;
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_IDLE;
 
		save_setpoints(&set); // TODO: Check for mod
 
            }
 
            else {
 
                user_input(&set->val.setpoint_steam);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 

	
 
        case STATE_TC_ERROR:
 
        {
 
            // Write text to OLED
 
            // [ therm : ready to steam ]
 
            // [ 30 => 120 C            ]
 
            ssd1306_DrawString("Error:              ", 0, 0);
 

	
 
            char tempstr[6];
 
            itoa(status->tc_errno, tempstr, 10);
 
            ssd1306_DrawString(tempstr, 0, 57);
 

	
 
            if(status->tc_errno == 1)
 
                ssd1306_DrawString("    TC Open Circuit", 1, 0);
 
            else if(status->tc_errno == 4)
 
                ssd1306_DrawString("    TC Short to GND", 1, 0);
 
            else if(status->tc_errno == 8)
 
                ssd1306_DrawString("    TC Short to VCC", 1, 0);
 
            else
 
                ssd1306_DrawString("#?, Unknown Error", 1, 0);
 
            ssd1306_DrawString("                    ", 2, 0);
 

	
 
            ssd1306_DrawString("-> to ignore all or", 2, 0);
 
            ssd1306_DrawString("press to continue", 3, 0);
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_IDLE;
 
            }
 
            else if(SW_RIGHT_PRESSED) {
 
                set->val.ignore_tc_error = 1;
 
                status->state = STATE_IDLE;
 
            }
 
            // Event Handler
 
            // Maybe handle if TC is plugged in
 
            // N/A
 
 
 
        } break;
 

	
 
        // Something is terribly wrong
 
        default:
 
        {
 
            status->state = STATE_IDLE;
 
            status->pid_enabled = 0;
 

	
 
        } break;
 
            
 
    }
 

	
 
    if(last_state != status->state) {
 
        // Clear screen on state change
 
        goto_mode = 2;
 
        trigger_drawsetpoint = 1;
 
        ssd1306_clearscreen();
 
    }
 

	
 
    // 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;
 
}
 

	
 

	
 
int32_t temp_last = 43002;
 
int32_t setpoint_last = 10023;
 
static int32_t temp_last = 43002;
 
static int32_t setpoint_last = 10023;
 
void draw_setpoint(therm_status_t* status) {
 
    // FIXME: need to do this when switching modes too
 
    if(status->temp != temp_last || trigger_drawsetpoint) { 
 
        char tempstr[3];
 
        itoa_fp(status->temp, status->temp_frac, tempstr);
 
        ssd1306_DrawStringBig("      ", 3, 0);
 
        ssd1306_DrawStringBig(tempstr, 3, 0);
 
    }
 

	
 
    if(trigger_drawsetpoint) 
 
        ssd1306_DrawStringBig(">", 3, 74);
 

	
 
    if(status->setpoint != setpoint_last || trigger_drawsetpoint) {
 
        char tempstr[3];
 
        itoa(status->setpoint, tempstr, 10);
 
        ssd1306_DrawStringBig("   ", 3, 90);
 
        ssd1306_DrawStringBig(tempstr, 3, 90);
 
    }
 

	
 
    trigger_drawsetpoint = 0;
 
    setpoint_last = status->setpoint;
 
    temp_last = status->temp;
 
}
 

	
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
gpio.c
Show inline comments
 
#include "gpio.h"
 
#include "config.h"
 
#include "stm32f0xx_hal_conf.h"
 
#include <inttypes.h>
 

	
 
// Increase on each press, and increase at a fast rate after duration elapsed of continuously holding down... somehow...
 
uint32_t change_time_reset = 0;
 

	
 
void user_input(uint16_t* to_modify)
 
{
 
    if(CHANGE_ELAPSED) {
 
        if(!HAL_GPIO_ReadPin(SW_UP) ) {
 
            CHANGE_RESET;
 
            (*to_modify)++;
 
        }
 
        else if(!HAL_GPIO_ReadPin(SW_DOWN) && (*to_modify) > 0) {
 
            CHANGE_RESET;
 
            (*to_modify)--;
 
        }
 
    }
 
}
 

	
 
void user_input_signed(int16_t* to_modify)
 
{
 
    // TODO: Bounds check on int16_t
 
    if(CHANGE_ELAPSED) {
 
        if(!HAL_GPIO_ReadPin(SW_UP) ) {
 
            CHANGE_RESET;
 
            (*to_modify)++;
 
        }
 
        else if(!HAL_GPIO_ReadPin(SW_DOWN)) {
 
            CHANGE_RESET;
 
            (*to_modify)--;
 
        }
 
    }
 
}
 

	
 

	
 
void init_gpio(void) {
 
void gpio_init(void) {
 

	
 
  GPIO_InitTypeDef GPIO_InitStruct;
 

	
 
    /* GPIO Ports Clock Enable */
 
  __GPIOF_CLK_ENABLE();
 
  __GPIOA_CLK_ENABLE();
 
  __GPIOB_CLK_ENABLE();
 
  __SPI1_CLK_ENABLE();
 

	
 
   
 
  //////////////////
 
  // PORT F       //
 
  //////////////////  
 
  
 
  // PORTF OUTPUT
 
  // Configure GPIO pin : PF0 [Power LED]
 
  GPIO_InitStruct.Pin = GPIO_PIN_0;
 
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 
  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
 
  HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
 

	
 
  // PORTF UNUSED
 
  // Configure GPIO pin : PF1
 
  GPIO_InitStruct.Pin = GPIO_PIN_1;
 
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
 
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 
  HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
 

	
 
  
 
  //////////////////
 
  // PORT A       //
 
  //////////////////
 
  
 
  // PORT A OUTPUT
 
  // Configure GPIO pins : (SSR+ CS_OLED RES D/C CS_MAX)
 
  GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_15;
 
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 
  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
 
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
  
 
  // PORTA INPUT
 
  // Configure GPIO pin : PA15 
 
//  GPIO_InitStruct.Pin = GPIO_PIN_15;
 
//  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
 
//  GPIO_InitStruct.Pull = GPIO_PULLUP;
 
//  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 

	
 
  // PORTA UNUSED
 
  // Configure GPIO pins : PA0 PA8
 
  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_8;
 
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
 
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
  
 
  // USART1 [PORTA]
 
  // Configure GPIO pins : PA9 PA10
 
  GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
 
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 
  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
 
  GPIO_InitStruct.Alternate = GPIO_AF1_USART1;
 
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 

	
 
  // SPI1 [PORTA]
 
  // Configure GPIO pin : PA, MOSI, SCK 
 
  GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_5;
 
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 
  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
 
  GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
 
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
  
 
  // Configure GPIO pin: PA, MISO
 
  GPIO_InitStruct.Pin = GPIO_PIN_6;
 
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 
  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
 
  GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
 
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 

	
 
  // USB [PORTA]
 

	
 
  /** USB GPIO Configuration  
 
  PA11   ------> USB_DM
 
  PA12   ------> USB_DP
 
  */  
 
  // Configure GPIO pin : PA, D+, D-
 
  GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
 
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 
  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
 
  GPIO_InitStruct.Alternate = GPIO_AF2_USB; // Can also be AF5
 
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 

	
 
  
 
  //////////////////
 
  // PORT B       //
 
  //////////////////
 
  
 
  // PORT B UNUSED
 
  // Configure GPIO pins : PB0 PB1 PB8 
 
  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_8;
 
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
 
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 

	
 
  // PORT B INPUT
 
  // Configure GPIO pins : PB3 PB4 PB5 PB6 PB7
 
  GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6 
 
                          |GPIO_PIN_7;
 
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
 
  GPIO_InitStruct.Pull = GPIO_PULLUP;
 
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);  
 
  
 
  
 
  // Enable DMA clocks (Is AHB even the right thing???)
 
  //RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); // EMZ TODO get the right ones
 

	
 

	
 
}
 

	
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
gpio.h
Show inline comments
 
#ifndef GPIO_H
 
#define GPIO_H
 

	
 
#include <inttypes.h>
 

	
 
#define CHANGE_PERIOD_MS 100
 
#define CHANGE_ELAPSED (HAL_GetTick() - change_time_reset) > CHANGE_PERIOD_MS
 
#define CHANGE_RESET change_time_reset = HAL_GetTick()
 

	
 

	
 
void user_input(uint16_t* to_modify);
 
void user_input_signed(int16_t* to_modify);
 
void init_gpio(void);
 
void gpio_init(void);
 

	
 
#endif
 

	
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
main.c
Show inline comments
 
#include "stm32f0xx_hal.h"
 
 
#include "config.h"
 
#include "syslib.h"
 
#include "states.h"
 
#include "ssd1306.h"
 
#include "max31855.h"
 
#include "gpio.h"
 
#include "spi.h"
 
#include "flash.h"
 
#include "stringhelpers.h"
 
#include "display.h"
 
#include "storage.h"
 
 
#include "usb_device.h"
 
#include "usbd_cdc_if.h"
 
 
 
// Prototypes
 
void process();
 
 
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)
 
{
 
 
    // Initialize HAL
 
    hal_init();
 
 
    // Configure the system clock
 
    systemclock_config();
 
    systemclock_init();
 
 
    // Unset bootloader option bytes (if set)
 
    void bootloader_unset(void);
 
 
    // Init GPIO
 
    init_gpio();
 
    gpio_init();
 
 
    // Init USB (TODO: Handle plugged/unplugged with external power)
 
    MX_USB_DEVICE_Init();
 
//    set.val.usb_plugged = 
 
 
    // USB startup delay
 
    HAL_Delay(1000);
 
    HAL_GPIO_WritePin(LED_POWER, 1);
 
 
    // Enter into bootloader if up button pressed on boot
 
    if(!HAL_GPIO_ReadPin(SW_UP))
 
        bootloader_enter(); 
 
 
    // Init SPI busses
 
    init_spi();
 
    spi_init();
 
 
    // Init OLED over SPI
 
    ssd1306_Init();
 
    ssd1306_clearscreen();
 
   
 
    // Default settings 
 
    set.val.boottobrew = 0;
 
    set.val.temp_units = TEMP_UNITS_CELSIUS;
 
    set.val.windup_guard = 1;
 
    set.val.k_p = 1;
 
    set.val.k_i = 1;
 
    set.val.k_d = 1;
 
    set.val.ignore_tc_error = 0;
 
    set.val.setpoint_brew = 0;
 
    set.val.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(&set);
 
 
    // Go to brew instead of idle if configured thusly
 
    if(set.val.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);
 
 
    flash_restore(&set);
 
 
    HAL_Delay(1500);
 
    ssd1306_clearscreen();
 
 
 
    // Main loop
 
    while(1)
 
    {
 
        // Process sensor inputs
 
        process();
 
 
        // Run state machine
 
        display_process(&set, &status); 
 
    }
 
 
}
 
 
// 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;
 
int32_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 = setpoint - temp; // TODO: Use fixed point fraction
 
 
  // Proportional component
 
  int32_t p_term = k_p * error;
 
 
  // Error accumulator (integrator)
 
  i_state += error;
 
 
  // to prevent the iTerm getting huge from lots of 
 
  //  error, we use a "windup guard" 
 
  // (this happens when the machine is first turned on and
 
  // it cant help be cold despite its best efforts)
 
  // not necessary, but this makes windup guard values 
 
  // relative to the current iGain
 
  int32_t windup_guard_res = set.val.windup_guard / k_i;  
 
 
  // Calculate integral term with windup guard 
 
  if (i_state > windup_guard_res) 
 
    i_state = windup_guard_res;
 
  else if (i_state < -windup_guard_res) 
 
    i_state = -windup_guard_res;
 
 
  int32_t i_term = k_i * i_state;
 
 
  // Calculate differential term (slope since last iteration)
 
  int32_t d_term = (k_d * (status.temp - last_pid_temp));
 
 
  // Save temperature for next iteration
 
  last_pid_temp = status.temp;
 
  last_pid_temp_frac = status.temp_frac;
 
 
  int16_t result = p_term + i_term - d_term;
 
 
  // Put out tenths of percent, 0-1000. 
 
  if(result > 1000)
 
    result = 1000;
 
  else if(result < -1000)
 
    result = -1000;
 
 
  // Return feedback
 
  return result;
 
}
 
 
 
uint32_t last_ssr_on = 0;
 
uint32_t last_vcp_tx = 0;
 
uint32_t last_led = 0;
 
uint32_t last_pid = 0;
 
int16_t ssr_output = 0; // Duty cycle of ssr, 0 to SSR_PERIOD 
 
 
// Turn SSR output on/off according to set duty cycle.
 
// TODO: Eventually maybe replace with a very slow timer or something. Double-check this code...
 
void process()
 
{
 
 
    uint32_t ticks = HAL_GetTick();
 
 
    if(ticks - last_led > 400) 
 
    {
 
        last_led = ticks;
 
    }
 
 
    if((ticks - last_pid > PID_PERIOD))
 
    {
 
        #ifdef MAX31855_TC_SENSOR
 
        max31855_readtemp(&hspi1, &set, &status); // Read MAX31855
 
        max31855_readtemp(spi_get(), &set, &status); // Read MAX31855
 
        #endif
 
 
        #ifdef MAX31865_RTD_SENSOR
 
        max31865_readtemp(&set, &status);
 
        #endif
 
 
    HAL_GPIO_TogglePin(LED_POWER);
 
 
        if(status.pid_enabled) 
 
        {
 
            // Get ssr output for next time
 
            int16_t power_percent = update_pid(set.val.k_p, set.val.k_i, set.val.k_d, status.temp, status.temp_frac, status.setpoint);
 
            //power-percent is 0-1000
 
            ssr_output = power_percent; //(((uint32_t)SSR_PERIOD * (uint32_t)10 * (uint32_t)100) * power_percent) / (uint32_t)1000000;
 
        }
 
        else 
 
        {
 
            ssr_output = 0;
 
        }
 
 
        last_pid = ticks;
 
    }
 
 
    // Every 200ms, set the SSR on unless output is 0
 
    if((ticks - last_ssr_on > SSR_PERIOD))
 
    {
 
 
        // Only support heating (ssr_output > 0) right now
 
        if(ssr_output > 0) {
 
 
            char tempstr[6];
 
            itoa(ssr_output, tempstr, 10);
 
            ssd1306_DrawString(tempstr, 0, 90);
 
 
            HAL_GPIO_WritePin(SSR_PIN, 1);
 
            last_ssr_on = ticks;
 
        }
 
    }
 
    
 
    // Kill SSR after elapsed period less than SSR_PERIOD 
 
    if(ticks - last_ssr_on > ssr_output || ssr_output == 0)
 
    {
 
        HAL_GPIO_WritePin(SSR_PIN, 0);
 
    }
 
 
    if(ticks - last_vcp_tx > VCP_TX_FREQ)
 
    {
 
        // Print temp to cdc
 
        char tempstr[16];
 
        itoa_fp(status.temp, status.temp_frac, tempstr);
 
        uint8_t numlen = strlen(tempstr);
 
        tempstr[numlen] = '\r';
 
        tempstr[numlen+1] = '\n';
 
 
//        if(set.val.usb_plugged)
 
//            CDC_Transmit_FS(tempstr, numlen+2);
 
       // while(CDC_Transmit_FS("\r\n", 2) == USBD_BUSY);
 
 
        last_vcp_tx = ticks;
 
    }
 
}
 
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
spi.c
Show inline comments
 

	
 
#include "stm32f0xx_hal_conf.h"
 
#include "stm32f0xx_hal_gpio_ex.h"
 
extern SPI_HandleTypeDef hspi1;
 
SPI_HandleTypeDef hspi1;
 

	
 
void init_spi()
 
void spi_init()
 
{
 
    hspi1.Instance = SPI1;
 
    hspi1.Init.Mode = SPI_MODE_MASTER;
 
    hspi1.Init.Direction = SPI_DIRECTION_2LINES;
 
    hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
 
    hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
 
    hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
 
    hspi1.Init.NSS = SPI_NSS_SOFT;
 
    hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
 
    hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
 
    hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
 
    hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
 
    hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLED;
 
    HAL_SPI_Init(&hspi1);
 
}    
 
    
 
SPI_HandleTypeDef* spi_get()
 
{
 
    return &hspi1;
 
}
 

	
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
spi.h
Show inline comments
 
#ifndef SPI_H
 
#define SPI_H
 

	
 
#include "stm32f0xx_hal_conf.h"
 

	
 
void init_spi();
 
void spi_init();
 
SPI_HandleTypeDef* spi_get();
 

	
 
#endif
 

	
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
ssd1306.c
Show inline comments
 
#include "stm32f0xx_hal_conf.h"
 
#include "ssd1306.h"
 
 
// Write command to OLED
 
void WriteCommand(unsigned char command)
 
{
 
  SSD_CS_Low();
 
  SSD_A0_Low();
 
  SPI_SendByte(command);
 
  SSD_CS_High();
 
}
 
 
// Write data to OLED
 
void WriteData(unsigned char data)
 
{
 
  SSD_CS_Low();
 
  SSD_A0_High();
 
  SPI_SendByte(data);
 
  SSD_CS_High();
 
}
 
 
// Initialize OLED
 
void ssd1306_Init(void)
 
{
 
 
  /* Generate a reset */
 
  SSD_Reset_Low();
 
  uint32_t i;
 
  for(i=5000; i>1; i--) 
 
  SSD_Reset_High();
 
 
  WriteCommand(0xAE);
 
  WriteCommand(0xD5);
 
  WriteCommand(0x80);
 
  WriteCommand(0xA8);
 
  WriteCommand(0x1F);
 
  WriteCommand(0xD3);
 
  WriteCommand(0x00);
 
  WriteCommand(0x40 | 0x00); // line #0
 
  WriteCommand(0x8D);
 
  WriteCommand(0x14); //10 or 14 if not externalvcc
 
  WriteCommand(0x20);
 
  WriteCommand(0x00);
 
//  WriteCommand(0xA0 | 0x1); // segremap (normal)
 
  WriteCommand(0xA0); // segremap (flip)
 
//  WriteCommand(0xC8); // comscandec (normal)
 
  WriteCommand(0xC0); // comscandec (flip)
 
  WriteCommand(0xDA); // setcompins 
 
  WriteCommand(0x02);
 
  WriteCommand(0x81); // contrast
 
  WriteCommand(0x0F); // contrast value. 8f is a good one.
 
  WriteCommand(0xD9);
 
  WriteCommand(0xF1); //22 or F1 if not externalvcc
 
  WriteCommand(0xDB);
 
  WriteCommand(0x40);
 
  WriteCommand(0xA4); // dispalyallon_resume
 
  WriteCommand(0xA6); // normaldisplay
 
 
 
  WriteCommand(0xAF); // display on 
 
}
 
 
 
// Times New Roman font
 
const char fontData[][5] =
 
static const char fontData[][5] =
 
{                                       // Refer to "Times New Roman" Font Database
 
                                        //   Basic Characters
 
    {0x00,0x00,0x00,0x00,0x00},         //   (  0)    - 0x0020 No-Break Space
 
    {0x00,0x00,0x4F,0x00,0x00},         //   (  1)  ! - 0x0021 Exclamation Mark
 
    {0x00,0x07,0x00,0x07,0x00},         //   (  2)  " - 0x0022 Quotation Mark
 
    {0x14,0x7F,0x14,0x7F,0x14},         //   (  3)  # - 0x0023 Number Sign
 
    {0x24,0x2A,0x7F,0x2A,0x12},         //   (  4)  $ - 0x0024 Dollar Sign
 
    {0x23,0x13,0x08,0x64,0x62},         //   (  5)  % - 0x0025 Percent Sign
 
    {0x36,0x49,0x55,0x22,0x50},         //   (  6)  & - 0x0026 Ampersand
 
    {0x00,0x05,0x03,0x00,0x00},         //   (  7)  ' - 0x0027 Apostrophe
 
    {0x00,0x1C,0x22,0x41,0x00},         //   (  8)  ( - 0x0028 Left Parenthesis
 
    {0x00,0x41,0x22,0x1C,0x00},         //   (  9)  ) - 0x0029 Right Parenthesis
 
    {0x14,0x08,0x3E,0x08,0x14},         //   ( 10)  * - 0x002A Asterisk
 
    {0x08,0x08,0x3E,0x08,0x08},         //   ( 11)  + - 0x002B Plus Sign
 
    {0x00,0x50,0x30,0x00,0x00},         //   ( 12)  , - 0x002C Comma
 
    {0x08,0x08,0x08,0x08,0x08},         //   ( 13)  - - 0x002D Hyphen-Minus
 
    {0x00,0x60,0x60,0x00,0x00},         //   ( 14)  . - 0x002E Full Stop
 
    {0x20,0x10,0x08,0x04,0x02},         //   ( 15)  / - 0x002F Solidus
 
    {0x3E,0x51,0x49,0x45,0x3E},         //   ( 16)  0 - 0x0030 Digit Zero
 
    {0x00,0x42,0x7F,0x40,0x00},         //   ( 17)  1 - 0x0031 Digit One
 
    {0x42,0x61,0x51,0x49,0x46},         //   ( 18)  2 - 0x0032 Digit Two
 
    {0x21,0x41,0x45,0x4B,0x31},         //   ( 19)  3 - 0x0033 Digit Three
 
    {0x18,0x14,0x12,0x7F,0x10},         //   ( 20)  4 - 0x0034 Digit Four
 
    {0x27,0x45,0x45,0x45,0x39},         //   ( 21)  5 - 0x0035 Digit Five
 
    {0x3C,0x4A,0x49,0x49,0x30},         //   ( 22)  6 - 0x0036 Digit Six
 
    {0x01,0x71,0x09,0x05,0x03},         //   ( 23)  7 - 0x0037 Digit Seven
 
    {0x36,0x49,0x49,0x49,0x36},         //   ( 24)  8 - 0x0038 Digit Eight
 
    {0x06,0x49,0x49,0x29,0x1E},         //   ( 25)  9 - 0x0039 Dight Nine
 
    {0x00,0x36,0x36,0x00,0x00},         //   ( 26)  : - 0x003A Colon
 
    {0x00,0x56,0x36,0x00,0x00},         //   ( 27)  ; - 0x003B Semicolon
 
    {0x08,0x14,0x22,0x41,0x00},         //   ( 28)  < - 0x003C Less-Than Sign
 
    {0x14,0x14,0x14,0x14,0x14},         //   ( 29)  = - 0x003D Equals Sign
 
    {0x00,0x41,0x22,0x14,0x08},         //   ( 30)  > - 0x003E Greater-Than Sign
 
    {0x02,0x01,0x51,0x09,0x06},         //   ( 31)  ? - 0x003F Question Mark
 
    {0x32,0x49,0x79,0x41,0x3E},         //   ( 32)  @ - 0x0040 Commercial At
 
    {0x7E,0x11,0x11,0x11,0x7E},         //   ( 33)  A - 0x0041 Latin Capital Letter A
 
    {0x7F,0x49,0x49,0x49,0x36},         //   ( 34)  B - 0x0042 Latin Capital Letter B
 
    {0x3E,0x41,0x41,0x41,0x22},         //   ( 35)  C - 0x0043 Latin Capital Letter C
 
    {0x7F,0x41,0x41,0x22,0x1C},         //   ( 36)  D - 0x0044 Latin Capital Letter D
 
    {0x7F,0x49,0x49,0x49,0x41},         //   ( 37)  E - 0x0045 Latin Capital Letter E
 
    {0x7F,0x09,0x09,0x09,0x01},         //   ( 38)  F - 0x0046 Latin Capital Letter F
 
    {0x3E,0x41,0x49,0x49,0x7A},         //   ( 39)  G - 0x0047 Latin Capital Letter G
 
    {0x7F,0x08,0x08,0x08,0x7F},         //   ( 40)  H - 0x0048 Latin Capital Letter H
 
    {0x00,0x41,0x7F,0x41,0x00},         //   ( 41)  I - 0x0049 Latin Capital Letter I
 
    {0x20,0x40,0x41,0x3F,0x01},         //   ( 42)  J - 0x004A Latin Capital Letter J
 
    {0x7F,0x08,0x14,0x22,0x41},         //   ( 43)  K - 0x004B Latin Capital Letter K
 
    {0x7F,0x40,0x40,0x40,0x40},         //   ( 44)  L - 0x004C Latin Capital Letter L
 
    {0x7F,0x02,0x0C,0x02,0x7F},         //   ( 45)  M - 0x004D Latin Capital Letter M
 
    {0x7F,0x04,0x08,0x10,0x7F},         //   ( 46)  N - 0x004E Latin Capital Letter N
 
    {0x3E,0x41,0x41,0x41,0x3E},         //   ( 47)  O - 0x004F Latin Capital Letter O
 
    {0x7F,0x09,0x09,0x09,0x06},         //   ( 48)  P - 0x0050 Latin Capital Letter P
 
    {0x3E,0x41,0x51,0x21,0x5E},         //   ( 49)  Q - 0x0051 Latin Capital Letter Q
 
    {0x7F,0x09,0x19,0x29,0x46},         //   ( 50)  R - 0x0052 Latin Capital Letter R
 
    {0x46,0x49,0x49,0x49,0x31},         //   ( 51)  S - 0x0053 Latin Capital Letter S
 
    {0x01,0x01,0x7F,0x01,0x01},         //   ( 52)  T - 0x0054 Latin Capital Letter T
 
    {0x3F,0x40,0x40,0x40,0x3F},         //   ( 53)  U - 0x0055 Latin Capital Letter U
 
    {0x1F,0x20,0x40,0x20,0x1F},         //   ( 54)  V - 0x0056 Latin Capital Letter V
 
    {0x3F,0x40,0x38,0x40,0x3F},         //   ( 55)  W - 0x0057 Latin Capital Letter W
 
    {0x63,0x14,0x08,0x14,0x63},         //   ( 56)  X - 0x0058 Latin Capital Letter X
 
    {0x07,0x08,0x70,0x08,0x07},         //   ( 57)  Y - 0x0059 Latin Capital Letter Y
 
    {0x61,0x51,0x49,0x45,0x43},         //   ( 58)  Z - 0x005A Latin Capital Letter Z
 
    {0x00,0x7F,0x41,0x41,0x00},         //   ( 59)  [ - 0x005B Left Square Bracket
 
    {0x02,0x04,0x08,0x10,0x20},         //   ( 60)  \ - 0x005C Reverse Solidus
 
    {0x00,0x41,0x41,0x7F,0x00},         //   ( 61)  ] - 0x005D Right Square Bracket
 
    {0x04,0x02,0x01,0x02,0x04},         //   ( 62)  ^ - 0x005E Circumflex Accent
 
    {0x40,0x40,0x40,0x40,0x40},         //   ( 63)  _ - 0x005F Low Line
 
    {0x01,0x02,0x04,0x00,0x00},         //   ( 64)  ` - 0x0060 Grave Accent
 
    {0x20,0x54,0x54,0x54,0x78},         //   ( 65)  a - 0x0061 Latin Small Letter A
 
    {0x7F,0x48,0x44,0x44,0x38},         //   ( 66)  b - 0x0062 Latin Small Letter B
 
    {0x38,0x44,0x44,0x44,0x20},         //   ( 67)  c - 0x0063 Latin Small Letter C
 
    {0x38,0x44,0x44,0x48,0x7F},         //   ( 68)  d - 0x0064 Latin Small Letter D
 
    {0x38,0x54,0x54,0x54,0x18},         //   ( 69)  e - 0x0065 Latin Small Letter E
 
    {0x08,0x7E,0x09,0x01,0x02},         //   ( 70)  f - 0x0066 Latin Small Letter F
 
    {0x06,0x49,0x49,0x49,0x3F},         //   ( 71)  g - 0x0067 Latin Small Letter G
 
    {0x7F,0x08,0x04,0x04,0x78},         //   ( 72)  h - 0x0068 Latin Small Letter H
 
    {0x00,0x44,0x7D,0x40,0x00},         //   ( 73)  i - 0x0069 Latin Small Letter I
 
    {0x20,0x40,0x44,0x3D,0x00},         //   ( 74)  j - 0x006A Latin Small Letter J
 
    {0x7F,0x10,0x28,0x44,0x00},         //   ( 75)  k - 0x006B Latin Small Letter K
 
    {0x00,0x41,0x7F,0x40,0x00},         //   ( 76)  l - 0x006C Latin Small Letter L
 
    {0x7C,0x04,0x18,0x04,0x7C},         //   ( 77)  m - 0x006D Latin Small Letter M
 
    {0x7C,0x08,0x04,0x04,0x78},         //   ( 78)  n - 0x006E Latin Small Letter N
 
    {0x38,0x44,0x44,0x44,0x38},         //   ( 79)  o - 0x006F Latin Small Letter O
 
    {0x7C,0x14,0x14,0x14,0x08},         //   ( 80)  p - 0x0070 Latin Small Letter P
 
    {0x08,0x14,0x14,0x18,0x7C},         //   ( 81)  q - 0x0071 Latin Small Letter Q
 
    {0x7C,0x08,0x04,0x04,0x08},         //   ( 82)  r - 0x0072 Latin Small Letter R
 
    {0x48,0x54,0x54,0x54,0x20},         //   ( 83)  s - 0x0073 Latin Small Letter S
 
    {0x04,0x3F,0x44,0x40,0x20},         //   ( 84)  t - 0x0074 Latin Small Letter T
 
    {0x3C,0x40,0x40,0x20,0x7C},         //   ( 85)  u - 0x0075 Latin Small Letter U
 
    {0x1C,0x20,0x40,0x20,0x1C},         //   ( 86)  v - 0x0076 Latin Small Letter V
 
    {0x3C,0x40,0x30,0x40,0x3C},         //   ( 87)  w - 0x0077 Latin Small Letter W
 
    {0x44,0x28,0x10,0x28,0x44},         //   ( 88)  x - 0x0078 Latin Small Letter X
 
    {0x0C,0x50,0x50,0x50,0x3C},         //   ( 89)  y - 0x0079 Latin Small Letter Y
 
    {0x44,0x64,0x54,0x4C,0x44},         //   ( 90)  z - 0x007A Latin Small Letter Z
 
    {0x00,0x08,0x36,0x41,0x00},         //   ( 91)  { - 0x007B Left Curly Bracket
 
    {0x00,0x00,0x7F,0x00,0x00},         //   ( 92)  | - 0x007C Vertical Line
 
    {0x00,0x41,0x36,0x08,0x00},         //   ( 93)  } - 0x007D Right Curly Bracket
 
    {0x02,0x01,0x02,0x04,0x02},         //   ( 94)  ~ - 0x007E Tilde
 
    {0x08,0x14,0x2A,0x14,0x22},         //   ( 95) << - 0x00AB Left-Pointing Double Angle Quotation Mark
 
    {0x00,0x02,0x05,0x02,0x00},         //   ( 96)    - 0x00B0 Degree Sign
 
//    {0x44,0x44,0x5F,0x44,0x44},         //   ( 97) +- - 0x00B1 Plus-Minus Sign
 
//    {0x7E,0x20,0x20,0x10,0x3E},         //   ( 98)  u - 0x00B5 Micro Sign
 
//    {0x22,0x14,0x2A,0x14,0x08},         //   ( 99) >> - 0x00BB Right-Pointing Double Angle Quotation Mark
 
//    {0x30,0x48,0x45,0x40,0x20},         //   (100)  ? - 0x00BF Inverted Question Mark
 
//    {0x22,0x14,0x08,0x14,0x22},         //   (101)  x - 0x00D7 Multiplcation Sign
 
//    {0x08,0x08,0x2A,0x08,0x08},         //   (102)  + - 0x00F7 Division Sign
 
//    {0x18,0x14,0x08,0x14,0x0C},         //   (103)    - 0x221E Infinity
 
//    {0x44,0x4A,0x4A,0x51,0x51},         //   (104)  < - 0x2264 Less-Than or Equal to
 
//    {0x51,0x51,0x4A,0x4A,0x44},         //   (105)  > - 0x2265 Greater-Than or Equal to
 
//    {0x54,0x14,0x64,0x08,0x70},         //   (106)  .: - RF Symbol
 
//    {0x70,0x7C,0x72,0x7C,0x70},         //   (107)  ^ - Lock symbol
 
//    {0x70,0x5C,0x52,0x54,0x70},         //   (108)  / - Unlock symbol
 
//    {0x0C,0x1E,0x3C,0x1E,0x0C},         //   (109)  <3 - Heart Symbol
 
//    {0x18,0x22,0xFF,0x12,0x0C},         //   (110)  U - USB Symbol
 
};
 
 
 
void setStartPage(unsigned char d)
 
{
 
    WriteCommand(0xB0|d);       // Set Page Start Address for Page Addressing Mode
 
                                // Default => 0xB0 (0x00)
 
}
 
/* Below are functions used to configure the OLED */
 
void setStartColumn(unsigned char d)
 
{
 
    WriteCommand(0x00+d%16);    // Set Lower Column Start Address for Page Addressing Mode
 
    WriteCommand(0x10+d/16);    // Set Higher Column Start Address for Page Addressing Mode
 
                                // Default => 0x10
 
}
 
 
 
const uint8_t row[4][32] = { 
 
 
	{0x00,0x00,0x01,0x03,0x07,0x0F,0x1E,0x3C,0x3C,0x7C,0x7C,0x7C,0xFC,0xFF,0xFF,0xFC,0xFC,0xFC,0xFC,0xFF,0x7F,0x7F,0x7F,0x3C,0x3C,0x1C,0x0C,0x06,0x03,0x01,0x00,0x00},
 
 
	{0x0F,0x7F,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x3F,0x3F,0x7F,0xFF,0xFF,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x7F,0x0F},
 
 
	{0xF0,0xFE,0xFF,0xFF,0xFF,0xC7,0x00,0x00,0x00,0x00,0x87,0xC7,0xC7,0xFF,0xFF,0x00,0x00,0x00,0x00,0x87,0x87,0xC7,0xC3,0x03,0x07,0x07,0x0F,0x7F,0xFF,0xFF,0xFE,0xF0},
 
 
	{0x00,0x00,0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0xFC,0xFE,0xFE,0xFE,0xFF,0xFF,0xFF,0x1F,0x1F,0x1F,0x1F,0xFF,0xFE,0xFE,0xFE,0xFC,0xFC,0xF8,0xF0,0xE0,0xC0,0x80,0x00,0x00},
 
 
};
 
 
void ssd1306_clearscreen()
 
{
 
    uint8_t i = 0;
 
    uint8_t page = 0;
 
    for(page = 0; page<4; page++)
 
    {
 
        setStartPage(page);
 
        setStartColumn(0);
 
        for(i = 0; i<128; i++)
 
        {
 
            WriteData(0x00);
 
        }
 
    }
 
    WriteData(0x00);
 
}
 
 
void ssd1306_drawlogo()
 
{
 
    uint8_t i = 0;
 
    setStartPage(3);
 
    setStartColumn(0);
 
    for(i = 0; i<32; i++)
 
    {
 
        WriteData(row[0][i]);
 
    }
 
 
    WriteData(0x00);
 
 
    setStartPage(2);
 
    setStartColumn(0);
 
    for(i = 0; i<32; i++)
 
    {
 
        WriteData(row[1][i]);
 
    }
 
    WriteData(0x00);
 
 
    setStartPage(1);
 
    setStartColumn(0);
 
    for(i = 0; i<32; i++)
 
    {
 
        WriteData(row[2][i]);
 
    }
 
    WriteData(0x00);
 
 
    setStartPage(0);
 
    setStartColumn(0);
 
    for(i = 0; i<32; i++)
 
    {
 
        WriteData(row[3][i]);
 
    }
system/interrupts.c
Show inline comments
 
/**
 
  ******************************************************************************
 
  * @file    stm32f0xx_it.c
 
  * @date    05/12/2014 20:22:27
 
  * @brief   Interrupt Service Routines.
 
  ******************************************************************************
 
  *
 
  * COPYRIGHT(c) 2014 STMicroelectronics
 
  *
 
  * Redistribution and use in source and binary forms, with or without modification,
 
  * are permitted provided that the following conditions are met:
 
  *   1. Redistributions of source code must retain the above copyright notice,
 
  *      this list of conditions and the following disclaimer.
 
  *   2. Redistributions in binary form must reproduce the above copyright notice,
 
  *      this list of conditions and the following disclaimer in the documentation
 
  *      and/or other materials provided with the distribution.
 
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
 
  *      may be used to endorse or promote products derived from this software
 
  *      without specific prior written permission.
 
  *
 
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
  *
 
  ******************************************************************************
 
  */
 
/* Includes ------------------------------------------------------------------*/
 
#include "stm32f0xx_hal.h"
 
#include "stm32f0xx.h"
 
#include "interrupts.h"
 
/* USER CODE BEGIN 0 */
 
 
/* USER CODE END 0 */
 
/* External variables --------------------------------------------------------*/
 
 
extern PCD_HandleTypeDef hpcd_USB_FS;
 
 
/******************************************************************************/
 
/*            Cortex-M0 Processor Interruption and Exception Handlers         */ 
 
/******************************************************************************/
 
 
/**
 
* @brief This function handles USB global Interrupt (combined with EXTI line 18).
 
*/
 
void USB_IRQHandler(void)
 
{
 
  /* USER CODE BEGIN USB_IRQn 0 */
 
 
  /* USER CODE END USB_IRQn 0 */
 
  HAL_PCD_IRQHandler(&hpcd_USB_FS);
 
  /* USER CODE BEGIN USB_IRQn 1 */
 
 
  /* USER CODE END USB_IRQn 1 */
 
}
 
 
/**
 
* @brief This function handles System tick timer.
 
*/
 
void SysTick_Handler(void)
 
{
 
  /* USER CODE BEGIN SysTick_IRQn 0 */
 
 
  /* USER CODE END SysTick_IRQn 0 */
 
  HAL_IncTick();
 
  HAL_SYSTICK_IRQHandler();
 
  /* USER CODE BEGIN SysTick_IRQn 1 */
 
 
  /* USER CODE END SysTick_IRQn 1 */
 
}
 
 
/* USER CODE BEGIN 1 */
 
 
/* USER CODE END 1 */
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
system/interrupts.h
Show inline comments
 
/**
 
  ******************************************************************************
 
  * @file    stm32f0xx_it.h
 
  * @date    05/12/2014 20:22:27
 
  * @brief   This file contains the headers of the interrupt handlers.
 
  ******************************************************************************
 
  *
 
  * COPYRIGHT(c) 2014 STMicroelectronics
 
  *
 
  * Redistribution and use in source and binary forms, with or without modification,
 
  * are permitted provided that the following conditions are met:
 
  *   1. Redistributions of source code must retain the above copyright notice,
 
  *      this list of conditions and the following disclaimer.
 
  *   2. Redistributions in binary form must reproduce the above copyright notice,
 
  *      this list of conditions and the following disclaimer in the documentation
 
  *      and/or other materials provided with the distribution.
 
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
 
  *      may be used to endorse or promote products derived from this software
 
  *      without specific prior written permission.
 
  *
 
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
  *
 
  ******************************************************************************
 
  */
 
 
/* Define to prevent recursive inclusion -------------------------------------*/
 
#ifndef __STM32F0xx_IT_H
 
#define __STM32F0xx_IT_H
 
 
#ifdef __cplusplus
 
 extern "C" {
 
#endif 
 
 
/* Includes ------------------------------------------------------------------*/
 
/* Exported types ------------------------------------------------------------*/
 
/* Exported constants --------------------------------------------------------*/
 
/* Exported macro ------------------------------------------------------------*/
 
/* Exported functions ------------------------------------------------------- */
 
 
void USB_IRQHandler(void);
 
void SysTick_Handler(void);
 
 
#ifdef __cplusplus
 
}
 
#endif
 
 
#endif /* __STM32F0xx_IT_H */
 
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
system/syslib.c
Show inline comments
 
#include "stm32f0xx_hal.h"
 

	
 
/* Notes:
 

	
 
Need to have DFU jump right to the program to unset those option bytes, or somehow have dfu-util unset them. Probably try using the :leave parameter...
 

	
 
       Flashing a binary file to address 0x8004000 of device memory and ask the device to leave DFU mode:
 
         $ dfu-util -a 0 -s 0x08004000:leave -D /path/to/image.bin
 

	
 
*/
 

	
 

	
 
// Unset bootloader option bytes 
 
void bootloader_unset(void)
 
{
 
    FLASH_OBProgramInitTypeDef OBParam;
 
 
 
    HAL_FLASHEx_OBGetConfig(&OBParam);
 
 
 
    if(OBParam.USERConfig != 0xFF)
 
    {
 
 
 
        OBParam.OptionType = OPTIONBYTE_USER;
 
        OBParam.USERConfig = 0xFF;
 
 
 
        HAL_FLASH_Unlock();
 
        HAL_FLASH_OB_Unlock();
 
        HAL_FLASHEx_OBErase();
 
        HAL_FLASHEx_OBProgram(&OBParam);
 
        HAL_FLASH_OB_Lock();
 
        HAL_FLASH_OB_Launch();
 
    }
 
}
 

	
 

	
 
// See thread: https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https%3a%2f%2fmy.st.com%2fpublic%2fSTe2ecommunities%2fmcu%2fLists%2fcortex_mx_stm32%2fJump%20to%20USB%20DFU%20Bootloader%20in%20startup%20code%20on%20STM32F042&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=185
 
// Set option bytes to enter bootloader upon reset
 
void bootloader_enter(void) {
 

	
 
    FLASH_OBProgramInitTypeDef OBParam;
 
   
 
    HAL_FLASHEx_OBGetConfig(&OBParam);
 
  
 

	
 
    // FIXME TODO: CHECK THESE OPTION BYTES, he was using an F1 processor. What about the switch flag?
 
    OBParam.OptionType = OPTIONBYTE_USER;
 
    /*Reset NBOOT0 and BOOT_SEL,  see: RM 2.5 Boot configuration*/
 
    OBParam.USERConfig = 0x77; //Sorry for magic number :)
 
  
 
    HAL_FLASH_Unlock();
 
    HAL_FLASH_OB_Unlock();
 
  
 
    HAL_FLASHEx_OBErase();
 
  
 
    HAL_FLASHEx_OBProgram(&OBParam);
 
  
 
    HAL_FLASH_OB_Lock();
 
    HAL_FLASH_Lock();
 
  
 
    HAL_FLASH_OB_Launch();
 
}
 

	
 

	
 
// Clock configuration
 
void systemclock_config(void)
 
void systemclock_init(void)
 
{
 

	
 
  RCC_OscInitTypeDef RCC_OscInitStruct;
 
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
 
  RCC_PeriphCLKInitTypeDef PeriphClkInit;
 

	
 
  // Enable HSI48 for main system clock
 
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSI14;
 
  RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
 
  RCC_OscInitStruct.HSI14State = RCC_HSI14_ON;
 
  RCC_OscInitStruct.HSI14CalibrationValue = 16;
 
  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();
 

	
 
}
 

	
system/syslib.h
Show inline comments
 
#ifndef BOOTLIB_H
 
#define BOOTLIB_H
 

	
 
void bootloader_unset(void);
 
void bootloader_enter(void);
 
void systemclock_config(void);
 
void systemclock_init(void);
 

	
 
#endif
system/usbd_conf.c
Show inline comments
 
/**
 
  ******************************************************************************
 
  * @file           : usbd_conf.c
 
  * @date           : 05/12/2014 20:22:27
 
  * @version        : v1.0_Cube
 
  * @brief          : This file implements the board support package for the USB device library
 
  ******************************************************************************
 
  *
 
  * COPYRIGHT(c) 2014 STMicroelectronics
 
  *
 
  * Redistribution and use in source and binary forms, with or without modification,
 
  * are permitted provided that the following conditions are met:
 
  * 1. Redistributions of source code must retain the above copyright notice,
 
  * this list of conditions and the following disclaimer.
 
  * 2. Redistributions in binary form must reproduce the above copyright notice,
 
  * this list of conditions and the following disclaimer in the documentation
 
  * and/or other materials provided with the distribution.
 
  * 3. Neither the name of STMicroelectronics nor the names of its contributors
 
  * may be used to endorse or promote products derived from this software
 
  * without specific prior written permission.
 
  *
 
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
  *
 
  ******************************************************************************
 
*/
 
/* Includes ------------------------------------------------------------------*/
 
#include "stm32f0xx.h"
 
#include "stm32f0xx_hal.h"
 
#include "usbd_def.h"
 
#include "usbd_core.h"
 
/* Private typedef -----------------------------------------------------------*/
 
/* Private define ------------------------------------------------------------*/
 
/* Private macro -------------------------------------------------------------*/
 
/* Private variables ---------------------------------------------------------*/
 
PCD_HandleTypeDef hpcd_USB_FS;
 
 
/* USER CODE BEGIN 0 */
 
__IO uint32_t remotewakeupon=0;
 
/* USER CODE END 0 */
 
 
/* Private function prototypes -----------------------------------------------*/
 
/* Private functions ---------------------------------------------------------*/
 
/* USER CODE BEGIN 1 */
 
static void SystemClockConfig_Resume(void);
 
/* USER CODE END 1 */
 
void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state);
 
extern void systemclock_config(void);
 
extern void systemclock_init(void);
 
 
/*******************************************************************************
 
		       LL Driver Callbacks (PCD -> USB Device Library)
 
*******************************************************************************/
 
/* MSP Init */
 
 
void HAL_PCD_MspInit(PCD_HandleTypeDef* hpcd)
 
{
 
  if(hpcd->Instance==USB)
 
  {
 
  /* USER CODE BEGIN USB_MspInit 0 */
 
 
  /* USER CODE END USB_MspInit 0 */
 
    /* Peripheral clock enable */
 
    __USB_CLK_ENABLE();
 
    HAL_NVIC_SetPriority(USB_IRQn, 0, 0);
 
    HAL_NVIC_EnableIRQ(USB_IRQn);
 
  /* USER CODE BEGIN USB_MspInit 1 */
 
 
  /* USER CODE END USB_MspInit 1 */
 
  }
 
}
 
 
void HAL_PCD_MspDeInit(PCD_HandleTypeDef* hpcd)
 
{
 
  if(hpcd->Instance==USB)
 
  {
 
  /* USER CODE BEGIN USB_MspDeInit 0 */
 
 
  /* USER CODE END USB_MspDeInit 0 */
 
    /* Peripheral clock disable */
 
    __USB_CLK_DISABLE();
 
 
    /* Peripheral interrupt Deinit*/
 
    HAL_NVIC_DisableIRQ(USB_IRQn);
 
 
  /* USER CODE BEGIN USB_MspDeInit 1 */
 
 
  /* USER CODE END USB_MspDeInit 1 */
 
  }
 
}
 
 
/**
 
  * @brief  Setup stage callback
 
  * @param  hpcd: PCD handle
 
  * @retval None
 
  */
 
void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
 
{
 
  USBD_LL_SetupStage(hpcd->pData, (uint8_t *)hpcd->Setup);
 
}
 
 
/**
 
  * @brief  Data Out stage callback.
 
  * @param  hpcd: PCD handle
 
  * @param  epnum: Endpoint Number
 
  * @retval None
 
  */
 
void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
 
{
 
  USBD_LL_DataOutStage(hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff);
 
}
 
 
/**
 
  * @brief  Data In stage callback..
 
  * @param  hpcd: PCD handle
 
  * @param  epnum: Endpoint Number
 
  * @retval None
 
  */
 
void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
 
{
 
  USBD_LL_DataInStage(hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff);
 
}
 
 
/**
 
  * @brief  SOF callback.
 
  * @param  hpcd: PCD handle
 
  * @retval None
 
  */
 
void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
 
{
 
  USBD_LL_SOF(hpcd->pData);
 
}
 
 
/**
 
  * @brief  Reset callback.
 
  * @param  hpcd: PCD handle
 
  * @retval None
 
  */
 
void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
 
{
 
  USBD_SpeedTypeDef speed = USBD_SPEED_FULL;
 
 
  /*Set USB Current Speed*/
 
  switch (hpcd->Init.speed)
 
  {
 
  case PCD_SPEED_FULL:
 
    speed = USBD_SPEED_FULL;
 
    break;
 
 
  default:
 
    speed = USBD_SPEED_FULL;
 
    break;
 
  }
 
  USBD_LL_SetSpeed(hpcd->pData, speed);
 
 
  /*Reset Device*/
 
  USBD_LL_Reset(hpcd->pData);
 
}
 
 
/**
 
  * @brief  Suspend callback.
 
  * @param  hpcd: PCD handle
 
  * @retval None
 
  */
 
void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
 
{
 
  USBD_LL_Suspend(hpcd->pData);
 
  /*Enter in STOP mode */
 
  /* USER CODE BEGIN 2 */
 
  if (hpcd->Init.low_power_enable)
 
  {
 
    /* Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register */
 
    SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
 
  }
 
  /* USER CODE END 2 */
 
}
 
 
/**
 
  * @brief  Resume callback.
 
  * @param  hpcd: PCD handle
 
  * @retval None
 
  */
 
void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
 
{
 
  /* USER CODE BEGIN 3 */
 
  if ((hpcd->Init.low_power_enable)&&(remotewakeupon == 0))
 
  {
 
    SystemClockConfig_Resume();
 
    /* Reset SLEEPDEEP bit of Cortex System Control Register */
 
    SCB->SCR &= (uint32_t)~((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
 
  }
 
  remotewakeupon=0;
 
  /* USER CODE END 3 */
 
  USBD_LL_Resume(hpcd->pData);
 
 
}
 
 
/**
 
  * @brief  ISOC Out Incomplete callback.
 
  * @param  hpcd: PCD handle
 
  * @param  epnum: Endpoint Number
 
  * @retval None
 
  */
 
void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
 
{
 
  USBD_LL_IsoOUTIncomplete(hpcd->pData, epnum);
 
}
 
 
/**
 
  * @brief  ISOC In Incomplete callback.
 
  * @param  hpcd: PCD handle
 
  * @param  epnum: Endpoint Number
 
  * @retval None
 
  */
 
void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
 
{
 
  USBD_LL_IsoINIncomplete(hpcd->pData, epnum);
 
}
 
 
/**
 
  * @brief  Connect callback.
 
  * @param  hpcd: PCD handle
 
  * @retval None
 
  */
 
void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
 
{
 
  USBD_LL_DevConnected(hpcd->pData);
 
}
 
 
/**
 
  * @brief  Disconnect callback.
 
  * @param  hpcd: PCD handle
 
  * @retval None
 
  */
 
void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
 
{
 
  USBD_LL_DevDisconnected(hpcd->pData);
 
}
 
 
/*******************************************************************************
 
		       LL Driver Interface (USB Device Library --> PCD)
 
@@ -313,219 +313,219 @@ USBD_StatusTypeDef  USBD_LL_Stop (USBD_H
 
  * @brief  Opens an endpoint of the Low Level Driver.
 
  * @param  pdev: Device handle
 
  * @param  ep_addr: Endpoint Number
 
  * @param  ep_type: Endpoint Type
 
  * @param  ep_mps: Endpoint Max Packet Size
 
  * @retval USBD Status
 
  */
 
USBD_StatusTypeDef  USBD_LL_OpenEP  (USBD_HandleTypeDef *pdev,
 
				      uint8_t  ep_addr,
 
				      uint8_t  ep_type,
 
				      uint16_t ep_mps)
 
{
 
 
  HAL_PCD_EP_Open(pdev->pData,
 
		  ep_addr,
 
		  ep_mps,
 
		  ep_type);
 
 
  return USBD_OK;
 
}
 
 
/**
 
  * @brief  Closes an endpoint of the Low Level Driver.
 
  * @param  pdev: Device handle
 
  * @param  ep_addr: Endpoint Number
 
  * @retval USBD Status
 
  */
 
USBD_StatusTypeDef  USBD_LL_CloseEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
 
{
 
 
  HAL_PCD_EP_Close(pdev->pData, ep_addr);
 
  return USBD_OK;
 
}
 
 
/**
 
  * @brief  Flushes an endpoint of the Low Level Driver.
 
  * @param  pdev: Device handle
 
  * @param  ep_addr: Endpoint Number
 
  * @retval USBD Status
 
  */
 
USBD_StatusTypeDef  USBD_LL_FlushEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
 
{
 
 
  HAL_PCD_EP_Flush(pdev->pData, ep_addr);
 
  return USBD_OK;
 
}
 
 
/**
 
  * @brief  Sets a Stall condition on an endpoint of the Low Level Driver.
 
  * @param  pdev: Device handle
 
  * @param  ep_addr: Endpoint Number
 
  * @retval USBD Status
 
  */
 
USBD_StatusTypeDef  USBD_LL_StallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
 
{
 
 
  HAL_PCD_EP_SetStall(pdev->pData, ep_addr);
 
  return USBD_OK;
 
}
 
 
/**
 
  * @brief  Clears a Stall condition on an endpoint of the Low Level Driver.
 
  * @param  pdev: Device handle
 
  * @param  ep_addr: Endpoint Number
 
  * @retval USBD Status
 
  */
 
USBD_StatusTypeDef  USBD_LL_ClearStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
 
{
 
 
  HAL_PCD_EP_ClrStall(pdev->pData, ep_addr);
 
  return USBD_OK;
 
}
 
 
/**
 
  * @brief  Returns Stall condition.
 
  * @param  pdev: Device handle
 
  * @param  ep_addr: Endpoint Number
 
  * @retval Stall (1: Yes, 0: No)
 
  */
 
uint8_t USBD_LL_IsStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
 
{
 
  PCD_HandleTypeDef *hpcd = pdev->pData;
 
 
  if((ep_addr & 0x80) == 0x80)
 
  {
 
    return hpcd->IN_ep[ep_addr & 0x7F].is_stall;
 
  }
 
  else
 
  {
 
    return hpcd->OUT_ep[ep_addr & 0x7F].is_stall;
 
  }
 
}
 
/**
 
  * @brief  Assigns a USB address to the device.
 
  * @param  pdev: Device handle
 
  * @param  ep_addr: Endpoint Number
 
  * @retval USBD Status
 
  */
 
USBD_StatusTypeDef  USBD_LL_SetUSBAddress (USBD_HandleTypeDef *pdev, uint8_t dev_addr)
 
{
 
 
  HAL_PCD_SetAddress(pdev->pData, dev_addr);
 
  return USBD_OK;
 
}
 
 
/**
 
  * @brief  Transmits data over an endpoint.
 
  * @param  pdev: Device handle
 
  * @param  ep_addr: Endpoint Number
 
  * @param  pbuf: Pointer to data to be sent
 
  * @param  size: Data size
 
  * @retval USBD Status
 
  */
 
USBD_StatusTypeDef  USBD_LL_Transmit (USBD_HandleTypeDef *pdev,
 
				      uint8_t  ep_addr,
 
				      uint8_t  *pbuf,
 
				      uint16_t  size)
 
{
 
 
  HAL_PCD_EP_Transmit(pdev->pData, ep_addr, pbuf, size);
 
  return USBD_OK;
 
}
 
 
/**
 
  * @brief  Prepares an endpoint for reception.
 
  * @param  pdev: Device handle
 
  * @param  ep_addr: Endpoint Number
 
  * @param  pbuf: Pointer to data to be received
 
  * @param  size: Data size
 
  * @retval USBD Status
 
  */
 
USBD_StatusTypeDef  USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev,
 
					   uint8_t  ep_addr,
 
					   uint8_t  *pbuf,
 
					   uint16_t  size)
 
{
 
 
  HAL_PCD_EP_Receive(pdev->pData, ep_addr, pbuf, size);
 
  return USBD_OK;
 
}
 
 
/**
 
  * @brief  Returns the last transfered packet size.
 
  * @param  pdev: Device handle
 
  * @param  ep_addr: Endpoint Number
 
  * @retval Recived Data Size
 
  */
 
uint32_t USBD_LL_GetRxDataSize  (USBD_HandleTypeDef *pdev, uint8_t  ep_addr)
 
{
 
  return HAL_PCD_EP_GetRxCount(pdev->pData, ep_addr);
 
}
 
 
/**
 
  * @brief  Delays routine for the USB Device Library.
 
  * @param  Delay: Delay in ms
 
  * @retval None
 
  */
 
void  USBD_LL_Delay (uint32_t Delay)
 
{
 
  HAL_Delay(Delay);
 
}
 
 
/**
 
  * @brief  static single allocation.
 
  * @param  size: size of allocated memory
 
  * @retval None
 
  */
 
void *USBD_static_malloc(uint32_t size)
 
{
 
  static uint32_t mem[MAX_STATIC_ALLOC_SIZE];
 
  return mem;
 
}
 
 
/**
 
  * @brief  Dummy memory free
 
  * @param  *p pointer to allocated  memory address
 
  * @retval None
 
  */
 
void USBD_static_free(void *p)
 
{
 
 
}
 
 
/* USER CODE BEGIN 4 */
 
/**
 
  * @brief  Configures system clock after wake-up from USB Resume CallBack:
 
  *         enable HSI, PLL and select PLL as system clock source.
 
  * @param  None
 
  * @retval None
 
  */
 
static void SystemClockConfig_Resume(void)
 
{
 
	systemclock_config();
 
	systemclock_init();
 
}
 
/* USER CODE END 4 */
 
 
/**
 
* @brief Software Device Connection
 
* @param hpcd: PCD handle
 
* @param state: connection state (0 : disconnected / 1: connected)
 
* @retval None
 
*/
 
void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state)
 
{
 
/* USER CODE BEGIN 5 */
 
  if (state == 1)
 
  {
 
    /* Configure Low Connection State */
 
 
  }
 
  else
 
  {
 
    /* Configure High Connection State */
 
 
  }
 
/* USER CODE END 5 */
 
}
 
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
0 comments (0 inline, 0 general)