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
 
#define SW_B_GPIO_Port GPIOA
 
#define SW_UP SW_B_GPIO_Port, SW_B_Pin
 
 
#define SW_A_Pin GPIO_PIN_6
 
#define SW_A_GPIO_Port GPIOA
 
#define SW_LEFT SW_A_GPIO_Port, SW_A_Pin
 
 
#define SW_C_Pin GPIO_PIN_7
 
#define SW_C_GPIO_Port GPIOA
 
#define SW_DOWN SW_C_GPIO_Port, SW_C_Pin
 
 
#define SW_BTN_Pin GPIO_PIN_0
 
#define SW_BTN_GPIO_Port GPIOB
 
#define SW_BTN SW_BTN_GPIO_Port , SW_BTN_Pin
 
 
#define LED_PIN GPIO_PIN_6
 
#define LED_GPIO_Port GPIOB
 
#define LED LED_GPIO_Port, LED_PIN
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);
 
 
 
	// Set up MOSI/MISO/SCK
 
	GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5;
 
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 
	GPIO_InitStruct.Pull = GPIO_NOPULL;
 
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 
	GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
 
	HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
 
 
	// Set up SPI port for OLED
 
	hspi1.Instance = SPI3;
 
	hspi1.Init.Mode = SPI_MODE_MASTER;
 
	hspi1.Init.Direction = SPI_DIRECTION_2LINES;
 
	hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
 
	hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
 
	hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
 
	hspi1.Init.NSS = SPI_NSS_SOFT;
 
@@ -206,70 +193,86 @@ static const char fontData[][5] =
 
    {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 Multiplication 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
 
	{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)
 
}
 
 
 
// Set start column
 
static 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
 
}
 
 
 
// 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},
 
 
	{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},
 
 
};
 
 
 
// Clear the screen
 
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++)
 
@@ -418,25 +421,31 @@ void ssd1306_drawstring(const unsigned c
 
        xPos+=6;
 
        if(*srcPointer == 0) break;
 
    }
 
}
 
 
 
// Print a string to the display, big font
 
void ssd1306_drawstringbig(const unsigned char *dataPtr, unsigned char row, unsigned char xPos)
 
{
 
    char *srcPointer;
 
 
    srcPointer = (char*)dataPtr;
 
    ssd1306_drawcharbig(' ',row,xPos); // NBSP must be written first before the string start
 
 
    while(1)
 
    {
 
        ssd1306_drawcharbig(*srcPointer,row,xPos);
 
        srcPointer++;
 
        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
 
//
 
// 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()
 
{
 
	state.i_state = 0;
 
	state.last_pid_temp = 0;
 
	state.last_pid_temp_frac = 0;
 
}
 

	
 

	
 
// Apply PID output values
 
float pid_process(void)
 
{
 
//            #ifdef MAX31865_RTD_SENSOR
 
//            max31865_readtemp(spi_get(), &set, &status);
 
//			#else
 
//			max31855_readtemp(spi_get(), &set, &status); // Read MAX31855
 
//			#endif
 

	
 
	float ssr_output = 0;
 

	
 

	
src/pwmout.c
Show inline comments
 
//
 
// PWM Out: generate PWM waveform to control factory
 
//
 

	
 
#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();
 

	
 
	// Configure LED GPIO pins
 
	GPIO_InitStruct.Pin = SSR_PIN;
 
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; //GPIO_MODE_AF_PP;
 
	GPIO_InitStruct.Pull = GPIO_NOPULL;
 
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 
//    GPIO_InitStruct.Alternate = GPIO_AF1_TIM17;
 
	HAL_GPIO_Init(SSR_GPIO_Port, &GPIO_InitStruct);
 

	
 

	
 
	TIM_OC_InitTypeDef sConfigOC;
 
	TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;
 

	
 
	htim17.Instance = TIM17;
 
	htim17.Init.Prescaler = 6000;
 
	htim17.Init.CounterMode = TIM_COUNTERMODE_UP;
 
	htim17.Init.Period = 1000;
 
	htim17.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 
	htim17.Init.RepetitionCounter = 0;
 
@@ -79,28 +81,30 @@ void pwmout_init(void)
 
	HAL_TIM_OC_Start_IT(&htim17, TIM_CHANNEL_1);
 
    __HAL_TIM_ENABLE_IT(&htim17, TIM_IT_UPDATE);
 

	
 
}
 

	
 

	
 
// freaking integral term isn't compatible with both heating and cooling... due to the discard
 
// functionality. this is a problem.
 

	
 
// also duty cycling isn't working correctly...
 
void pwmout_process(int16_t duty)
 
{
 
	if(duty == 0)
 
	{
 
		HAL_GPIO_WritePin(SSR, 0);
 
		HAL_GPIO_WritePin(LED, 0);
 
	}
 
	if(duty < 0)
 
		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
 
//
 
// Flash: Nonvolatile storage for settings / etc
 
//
 
 
#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();
 
}
 
 
 
// Save settings to flash memory
 
void flash_savesettings()
 
{
 
	// Unlock flash memory
 
	HAL_FLASH_Unlock();
 
 
	// Initialize eraser to erase one page of flash
 
	FLASH_EraseInitTypeDef eraser =
 
	{
 
			.TypeErase = TYPEERASE_PAGES,
 
			.PageAddress = eeprom_emulation,
 
			.NbPages = 1,
 
	};
 
	uint32_t errvar = 0;
 
 
	// Erase flash page
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;
 
 
	// GPIO Ports Clock Enable
 
	__GPIOA_CLK_ENABLE();
 
	__GPIOB_CLK_ENABLE();
 
	__GPIOF_CLK_ENABLE();
 
 
 
	// Configure LED GPIO pins
 
	GPIO_InitStruct.Pin = LED_PIN;
 
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 
	GPIO_InitStruct.Pull = GPIO_NOPULL;
 
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 
	HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
 
 
 
	GPIO_InitStruct.Pin = SW_A_Pin|SW_B_Pin|SW_C_Pin|SW_D_Pin;
 
	GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
 
	GPIO_InitStruct.Pull = GPIO_PULLUP;
 
	HAL_GPIO_Init(SW_C_GPIO_Port, &GPIO_InitStruct);
 
 
	GPIO_InitStruct.Pin = SW_BTN_Pin;
 
	GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
 
	GPIO_InitStruct.Pull = GPIO_PULLUP;
 
	HAL_GPIO_Init(SW_BTN_GPIO_Port, &GPIO_InitStruct);
 
 
 
	/* EXTI interrupt init*/
 
	HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0);
 
	HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
 
 
	HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
 
	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
 
//
 
// TempSense: read temperature from TC, RTD, or NTC
 
//
 

	
 
#include "tempsense.h"
 
#include "flash.h"
 
#include "ssd1306/ssd1306.h"
 
#include "max31856/max31856.h"
 

	
 

	
 
// Initialize temperature sensor
 
void tempsense_init(void)
 
{
 
	// TODO: Rework SPI stuff... init the port in a sharedlib then pass ref to display and tempsense
 

	
 
	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)
 
{
 

	
 
	switch(flash_getsettings()->val.sensor_type)
 
	{
 
		case SENSOR_TC_K:
 
		case SENSOR_TC_E:
 
		case SENSOR_TC_N:
 
		case SENSOR_TC_R:
 
		case SENSOR_TC_S:
 
		case SENSOR_TC_T:
 
		{
 
			// Read MAX31856
 
			max31856_process();
 
		} break;
 

	
 
		case SENSOR_NTC:
 
		{
 
			// Read analog value from internal ADC, linearize the reading, etc
 
		} break;
0 comments (0 inline, 0 general)