Changeset - 752fd27f607a
[Not reviewed]
default
0 7 0
Ethan Zonca - 6 years ago 2018-12-29 17:42:05
ez@ethanzonca.com
Basic code cleanup
7 files changed with 92 insertions and 77 deletions:
0 comments (0 inline, 0 general)
inc/system/gpio.h
Show inline comments
 
#ifndef __gpio_H
 
#define __gpio_H
 
 
#include "stm32f3xx_hal.h"
 
 
 
// Helper macros
 
#define CHANGE_PERIOD_MS 100
 
#define CHANGE_ELAPSED (HAL_GetTick() - change_time_reset) > CHANGE_PERIOD_MS
 
#define CHANGE_RESET change_time_reset = HAL_GetTick()
 
 
 
// Define button push
 
#define SW_D_Pin GPIO_PIN_4
 
#define SW_D_GPIO_Port GPIOA
 
#define SW_RIGHT SW_D_GPIO_Port, SW_D_Pin
 
 
#define SW_B_Pin GPIO_PIN_5
lib/ssd1306/ssd1306.c
Show inline comments
 
//
 
// ssd1306: OLED display driver
 
//
 
 
#include "stm32f3xx_hal.h"
 
#include "ssd1306.h"
 
 
 
SPI_HandleTypeDef hspi1;
 
 
 
// SPI handle accessor
 
SPI_HandleTypeDef* spi_get()
 
{
 
    return &hspi1;
 
}
 
// Private variables
 
static SPI_HandleTypeDef hspi1;
 
 
 
// Write command to OLED
 
static void WriteCommand(unsigned char command)
 
{
 
  SSD_CS_Low();
 
  SSD_A0_Low();
 
  SPI_SendByte(command);
 
  SSD_CS_High();
 
}
 
// Private method prototypes
 
static void WriteCommand(unsigned char command);
 
static void WriteData(unsigned char data);
 
static void setStartPage(unsigned char d);
 
static void setStartColumn(unsigned char d);
 
 
// Write data to OLED
 
static void WriteData(unsigned char data)
 
{
 
  SSD_CS_Low();
 
  SSD_A0_High();
 
  SPI_SendByte(data);
 
  SSD_CS_High();
 
}
 
 
// Initialize OLED
 
void ssd1306_init(void)
 
{
 
	__SPI3_CLK_ENABLE();
 
	__GPIOA_CLK_ENABLE();
 
	__GPIOB_CLK_ENABLE();
 
	GPIO_InitTypeDef GPIO_InitStruct;
 
 
	/*Configure GPIO pins : OLED_CS_Pin OLED_RESET_Pin OLED_DC_Pin */
 
	// GPIO
 
	GPIO_InitStruct.Pin = OLED_CS_Pin|OLED_RESET_Pin|OLED_DC_Pin;
 
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 
	GPIO_InitStruct.Pull = GPIO_NOPULL;
 
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 
	HAL_GPIO_Init(OLED_DC_GPIO_Port, &GPIO_InitStruct);
 
 
@@ -224,16 +211,33 @@ static const char fontData[][5] =
 
    {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
 
	{0x22,0x5d,0x22,0x00,0x00},			//   (111) ez updown
 
	{0x14,0x3e,0x14,0x00,0x00},         //   (112) ez updown short
 
};
 
/*
 
 
 
*/
 
// Write command to OLED
 
static void WriteCommand(unsigned char command)
 
{
 
  SSD_CS_Low();
 
  SSD_A0_Low();
 
  SPI_SendByte(command);
 
  SSD_CS_High();
 
}
 
 
 
// Write data to OLED
 
static void WriteData(unsigned char data)
 
{
 
  SSD_CS_Low();
 
  SSD_A0_High();
 
  SPI_SendByte(data);
 
  SSD_CS_High();
 
}
 
 
 
// Set start page
 
static void setStartPage(unsigned char d)
 
{
 
    WriteCommand(0xB0|d);       // Set Page Start Address for Page Addressing Mode
 
                                // Default => 0xB0 (0x00)
 
@@ -245,13 +249,12 @@ static void setStartColumn(unsigned char
 
{
 
    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
 
}
 
 
 
// Therm logo
 
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},
 
@@ -436,7 +439,13 @@ void ssd1306_drawstringbig(const unsigne
 
        xPos+=12;
 
        if(*srcPointer == 0) break;
 
    }
 
}
 
 
 
// SPI handle accessor
 
SPI_HandleTypeDef* spi_get()
 
{
 
    return &hspi1;
 
}
 
 
// vim:softtabstop=4 shiftwidth=4 expandtab
src/pid.c
Show inline comments
 
@@ -2,13 +2,13 @@
 
// PID: proportional/integral/derivative controller
 
//
 

	
 
#include "pid.h"
 
#include "flash.h"
 

	
 
// PID implementation
 
// Private variables
 
static pid_state_t state;
 

	
 

	
 
// Initialize PID loop
 
void pid_init()
 
{
src/pwmout.c
Show inline comments
 
@@ -5,17 +5,19 @@
 
#include "stm32f3xx_hal.h"
 
#include "pwmout.h"
 
#include "gpio.h"
 
#include "flash.h"
 
#include "error.h"
 

	
 
TIM_HandleTypeDef htim17;
 

	
 
// Private variables
 
static TIM_HandleTypeDef htim17;
 
static uint32_t last_ssr_on = 0;
 

	
 

	
 
// Initialize hardware PWM output
 
void pwmout_init(void)
 
{
 
	GPIO_InitTypeDef GPIO_InitStruct;
 
    __HAL_RCC_TIM17_CLK_ENABLE();
 
    __HAL_RCC_GPIOB_CLK_ENABLE();
 

	
 
@@ -97,10 +99,12 @@ void pwmout_process(int16_t duty)
 
		duty = 0;
 

	
 

	
 
	htim17.Instance->CCR1 = duty; //duty;
 
}
 

	
 

	
 
// Accessor for timer handle
 
TIM_HandleTypeDef* pwmout_get_tim(void)
 
{
 
	return &htim17;
 
}
src/system/flash.c
Show inline comments
 
@@ -3,17 +3,18 @@
 
//
 
 
#include "stm32f3xx_hal.h"
 
#include "flash.h"
 
#include "config.h"
 
 
// Takes up 1 page (1k size)
 
static __attribute__((__section__(".eeprom"))) uint16_t eeprom_emulation[512];
 
 
therm_settings_t settings;
 
therm_status_t status;
 
// Private variables
 
static __attribute__((__section__(".eeprom"))) uint16_t eeprom_emulation[512]; // Takes up 1 page (1k size)
 
static therm_settings_t settings;
 
static therm_status_t status;
 
 
 
// Initialize flash and restore settings
 
void flash_init(void)
 
{
 
	flash_restoresettings();
 
}
src/system/gpio.c
Show inline comments
 
//
 
// GPIO: configure general-purpose inputs/outputs
 
//
 
 
#include "stm32f3xx_hal.h"
 
 
#include "gpio.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()
 
 
// Increase on each press, and increase at a fast rate after duration elapsed of continuously holding down... somehow...
 
static uint32_t change_time_reset = 0;
 
 
 
// Increment/decrement unsigned variable with up/down buttons
 
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)--;
 
        }
 
    }
 
}
 
 
 
// Increment/decrement signed variable with up/down buttons
 
void user_input_signed(int32_t* to_modify)
 
{
 
	//fixme: need to cast to 16/32 bits correctly
 
    if(CHANGE_ELAPSED) {
 
        if(!HAL_GPIO_ReadPin(SW_UP) ) {
 
            CHANGE_RESET;
 
            if (*to_modify < 32768)
 
            	(*to_modify)++;
 
        }
 
        else if(!HAL_GPIO_ReadPin(SW_DOWN)) {
 
            CHANGE_RESET;
 
            if (*to_modify >= -32768)
 
            	(*to_modify)--;
 
        }
 
    }
 
}
 
// Private variables
 
// Increase on each press, and increase at a fast rate after duration elapsed of continuously holding down... somehow...
 
static uint32_t change_time_reset = 0;
 
 
 
// Initialize GPIOs
 
void gpio_init(void)
 
{
 
	GPIO_InitTypeDef GPIO_InitStruct;
 
@@ -87,6 +49,41 @@ void gpio_init(void)
 
	HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
 
 
	// Define startup State
 
	HAL_GPIO_WritePin(LED, 0);
 
 
}
 
 
 
// Increment/decrement unsigned variable with up/down buttons
 
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)--;
 
        }
 
    }
 
}
 
 
 
// Increment/decrement signed variable with up/down buttons
 
void user_input_signed(int32_t* to_modify)
 
{
 
	//fixme: need to cast to 16/32 bits correctly
 
    if(CHANGE_ELAPSED) {
 
        if(!HAL_GPIO_ReadPin(SW_UP) ) {
 
            CHANGE_RESET;
 
            if (*to_modify < 32768)
 
            	(*to_modify)++;
 
        }
 
        else if(!HAL_GPIO_ReadPin(SW_DOWN)) {
 
            CHANGE_RESET;
 
            if (*to_modify >= -32768)
 
            	(*to_modify)--;
 
        }
 
    }
 
}
src/tempsense.c
Show inline comments
 
@@ -15,14 +15,12 @@ void tempsense_init(void)
 

	
 
	max31856_init(spi_get(), TEMPSENSE_MAX_CS_PORT, TEMPSENSE_MAX_CS_PIN, 0);
 

	
 
	// Maybe don't perform temp sensor setup in here, but in readtemp?
 
	// what happens if the user changes the tempsense type while running?
 
	// we need to re-init...
 

	
 

	
 
}
 

	
 

	
 
// Request reading from configured temperature sensor
 
void tempsense_readtemp(void)
 
{
0 comments (0 inline, 0 general)