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
 
 
 
void user_input(uint16_t* to_modify);
 
void user_input_signed(int32_t* to_modify);
 
 
void gpio_init(void);
 
void gpio_led_blueblink(uint8_t num_blinks);
 
 
#endif
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;
 
	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_DISABLED;
 
	HAL_SPI_Init(&hspi1);
 
 
 
	// 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
 
@@ -182,118 +169,134 @@ static const char fontData[][5] =
 
    {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 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++)
 
        {
 
            WriteData(0x00);
 
        }
 
    }
 
    WriteData(0x00);
 
}
 
 
 
// Draw the therm logo on the left side of the OLED
 
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++)
 
@@ -394,49 +397,55 @@ void ssd1306_drawcharbig(unsigned char a
 
        data |= ((*srcPointer) & 0b00010000) >> 4; // get top 4 bits
 
 
        WriteData(data);
 
        WriteData(data);
 
 
        srcPointer++;
 
    }
 
    WriteData(0x00);
 
 
}
 
 
 
// Print a string to the display
 
void ssd1306_drawstring(const unsigned char *dataPtr, unsigned char row, unsigned char xPos)
 
{
 
    unsigned char *srcPointer;
 
 
    srcPointer = (unsigned char*)dataPtr;
 
    ssd1306_drawchar(' ',row,xPos); // NBSP must be written first before the string start
 
 
    while(1)
 
    {
 
        ssd1306_drawchar(*srcPointer,row,xPos);
 
        srcPointer++;
 
        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;
 

	
 

	
 
	if(runtime_status()->pid_enabled)
 
	{
 
		// Get ssr output for next time
 
		int16_t power_percent = pid_update();
 

	
 
		if(flash_getsettings()->val.plant_type == PLANT_COOLER)
 
			power_percent *= -1;
 

	
 
		//power-percent is 0-1000?
 
		ssr_output = power_percent; //(((uint32_t)SSR_PERIOD * (uint32_t)10 * (uint32_t)100) * power_percent) / (uint32_t)1000000;
 

	
 
		// put ssr output on display
 
		ssd1306_drawstring("      ", 0, 90); //fixme: this is bad, but I can't get the old digits to clear otherwise
 
		char tempstr[8];
 
		snprintf(tempstr, 8, "%4.1f%%", ssr_output/10.0);
 
		ssd1306_drawstring(tempstr, 0, 85);
 
	}
 
	else
 
	{
 
		ssr_output = 0.0;
 
	}
 

	
 
	return ssr_output; //ssr_output;
 
}
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;
 
	if (HAL_TIM_Base_Init(&htim17) != HAL_OK)
 
	{
 
		error_assert(ERR_PERIPHINIT);
 
	}
 

	
 
	if (HAL_TIM_OC_Init(&htim17) != HAL_OK)
 
	{
 
		error_assert(ERR_PERIPHINIT);
 
	}
 

	
 
	sConfigOC.OCMode = TIM_OCMODE_TOGGLE; //TIM_OCMODE_PWM1;
 
	sConfigOC.Pulse = 200;
 
	sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
 
	sConfigOC.OCNPolarity = TIM_OCNPOLARITY_LOW;
 
	sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
 
	sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
 
	sConfigOC.OCNIdleState = TIM_OCIDLESTATE_SET;
 
	if (HAL_TIM_OC_ConfigChannel(&htim17, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
 
	{
 
		error_assert(ERR_PERIPHINIT);
 
	}
 

	
 
	sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
 
	sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
 
	sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
 
	sBreakDeadTimeConfig.DeadTime = 0;
 
	sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
 
	sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
 
	sBreakDeadTimeConfig.BreakFilter = 0;
 
	sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
 
	if (HAL_TIMEx_ConfigBreakDeadTime(&htim17, &sBreakDeadTimeConfig) != HAL_OK)
 
	{
 
		error_assert(ERR_PERIPHINIT);
 
	}
 

	
 
    HAL_NVIC_SetPriority(TIM1_TRG_COM_TIM17_IRQn, 0, 0);
 
    HAL_NVIC_EnableIRQ(TIM1_TRG_COM_TIM17_IRQn);
 

	
 

	
 
	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
 
	HAL_FLASHEx_Erase(&eraser, &errvar);
 
 
	// Write new flash data
 
	uint16_t writectr;
 
	for(writectr = 0; writectr < 128; writectr++)// 128 bytes data
 
	{
 
		HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, eeprom_emulation+writectr,settings.data[writectr]);
 
	}
 
 
	// Write magic value to flash
 
	HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, eeprom_emulation+FLASH_MAGIC_LOC,FLASH_MAGIC_VALUE);
 
 
	// Lock flash memory
 
	HAL_FLASH_Lock();
 
	HAL_Delay(2);
 
}
 
 
 
// Restore configuration from flash memory, if any was previously saved
 
void flash_restoresettings(void)
 
{
 
	// Check for magic flash value
 
	if(eeprom_emulation[FLASH_MAGIC_LOC] == FLASH_MAGIC_VALUE)
 
	{
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;
 

	
 
	}
 

	
 
	// either return latest reading from DMA loop (NTC, etc)
 
	// or initiate a blocking read and return it.
 
	// Need to gracefully handle the timeout...
 
}
 

	
 

	
 
// Get latest temperature in requested units
 
float tempsense_gettemp(void)
 
{
 
	float temp_converted = max31856_latest_temp();
 

	
 
	if(flash_getsettings()->val.temp_units == TEMP_UNITS_FAHRENHEIT)
 
		temp_converted = temp_converted * 1.8f + 32.0f;
 

	
 
	return temp_converted;
 
}
0 comments (0 inline, 0 general)