diff --git a/.cproject b/.cproject --- a/.cproject +++ b/.cproject @@ -14,7 +14,7 @@ - + + diff --git a/inc/system/error.h b/inc/system/error.h --- a/inc/system/error.h +++ b/inc/system/error.h @@ -5,10 +5,10 @@ // If adding errors, remember to define a message for each in error.c enum error_number { - ERR_INFO = 0, - ERR_GENERIC, - ERR_PERIPHINIT, - ERR_COMMANDTIMEOUT, + ERR_INFO = 0, + ERR_GENERIC, + ERR_PERIPHINIT, + ERR_COMMANDTIMEOUT, }; void error_assert(const uint8_t errno); diff --git a/lib/max31856/max31856.c b/lib/max31856/max31856.c --- a/lib/max31856/max31856.c +++ b/lib/max31856/max31856.c @@ -25,64 +25,64 @@ static void __read_reg(uint8_t reg, uint // Initialize the MAX31856 driver void max31856_init(SPI_HandleTypeDef* spi_port, GPIO_TypeDef* cs_port, uint32_t cs_pin, uint32_t sensor_type) { - // Set CS pin references - csport = cs_port; - cspin = cs_pin; + // Set CS pin references + csport = cs_port; + cspin = cs_pin; - // Set SPI port reference - spiport = spi_port; + // Set SPI port reference + spiport = spi_port; - // Configure the CS pin for output - GPIO_InitTypeDef GPIO_InitStruct; - GPIO_InitStruct.Pin = cs_pin; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(cs_port, &GPIO_InitStruct); + // Configure the CS pin for output + GPIO_InitTypeDef GPIO_InitStruct; + GPIO_InitStruct.Pin = cs_pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(cs_port, &GPIO_InitStruct); - // MAX31856 - // - set to continuous conversion mode - // - probably no filtering, we'll do that on this side of things - // - set up to read the typical open/short faults but not the high/low alarms + // MAX31856 + // - set to continuous conversion mode + // - probably no filtering, we'll do that on this side of things + // - set up to read the typical open/short faults but not the high/low alarms - // TODO: Enable open/short detection - // Enables auto conv - __write_reg(MAX31856_CR0_REG, MAX31856_CR0_AUTOCONVERT); // MAX31856_CR0_OCFAULT0 + // TODO: Enable open/short detection + // Enables auto conv + __write_reg(MAX31856_CR0_REG, MAX31856_CR0_AUTOCONVERT); // MAX31856_CR0_OCFAULT0 - // Averaging set to 1 sample, TC type set to K - __write_reg(MAX31856_CR1_REG, MAX31856_TCTYPE_K); + // Averaging set to 1 sample, TC type set to K + __write_reg(MAX31856_CR1_REG, MAX31856_TCTYPE_K); - // sensor type - could we just mask bits off? maybe optimize the enum for this + // sensor type - could we just mask bits off? maybe optimize the enum for this } // Pull reading from the MAX31856 IC float max31856_process(void) { - uint8_t tempbuf[3]; - __read_reg(MAX31856_LTCBH_REG, tempbuf, 3); + uint8_t tempbuf[3]; + __read_reg(MAX31856_LTCBH_REG, tempbuf, 3); - volatile int32_t temp24 = tempbuf[0] << 16 | tempbuf[1] << 8 | tempbuf[2]; - if (temp24 & 0x800000) { - temp24 |= 0xFF000000; // fix sign - } + volatile int32_t temp24 = tempbuf[0] << 16 | tempbuf[1] << 8 | tempbuf[2]; + if (temp24 & 0x800000) { + temp24 |= 0xFF000000; // fix sign + } - temp24 >>= 5; // bottom 5 bits are unused + temp24 >>= 5; // bottom 5 bits are unused - float tempfloat = temp24; - tempfloat *= 0.0078125; + float tempfloat = temp24; + tempfloat *= 0.0078125; - temp_latest = tempfloat; - return tempfloat; + temp_latest = tempfloat; + return tempfloat; - // Read temperature from the MAX31856 (approx 10hz optimally) -// uint8_t data[] = {0,0,0,0}; -// HAL_SPI_Transmit(spiport, data, 1, 100); + // Read temperature from the MAX31856 (approx 10hz optimally) + // uint8_t data[] = {0,0,0,0}; + // HAL_SPI_Transmit(spiport, data, 1, 100); } @@ -90,66 +90,66 @@ float max31856_process(void) // Return latest temperature reading (unaveraged, deg C) float max31856_latest_temp(void) { - return temp_latest; + return temp_latest; } // Return average temperature reading (deg C) float max31856_avg_temp(void) { - return temp_latest; + return temp_latest; } static void __write_reg(uint8_t reg, uint8_t data) { - // Set write bit - reg |= MAX31856_WRITE_BIT; + // Set write bit + reg |= MAX31856_WRITE_BIT; - uint8_t outarr[2] = {reg, data}; - uint8_t dummyrx[2]; + uint8_t outarr[2] = {reg, data}; + uint8_t dummyrx[2]; - // Assert the bus - __cs_assert(); + // Assert the bus + __cs_assert(); - // Write data - volatile HAL_StatusTypeDef res = HAL_SPI_TransmitReceive(spiport, outarr, dummyrx, 2, 100); + // Write data + volatile HAL_StatusTypeDef res = HAL_SPI_TransmitReceive(spiport, outarr, dummyrx, 2, 100); - // Release the bus - __cs_deassert(); + // Release the bus + __cs_deassert(); } static void __read_reg(uint8_t reg, uint8_t* rxbuf, uint8_t len) { - // Transmit buffer only uses first item for reg addr - uint8_t regarr[1]; - regarr[0] = reg; + // Transmit buffer only uses first item for reg addr + uint8_t regarr[1]; + regarr[0] = reg; - uint8_t dummyrx[12] = {0}; - uint8_t dummytx[12] = {0}; + uint8_t dummyrx[12] = {0}; + uint8_t dummytx[12] = {0}; - // Assert the bus - __cs_assert(); + // Assert the bus + __cs_assert(); - // Send address - HAL_SPI_TransmitReceive(spiport, regarr, dummyrx, 1, 100); + // Send address + HAL_SPI_TransmitReceive(spiport, regarr, dummyrx, 1, 100); - // Receive data - HAL_SPI_TransmitReceive(spiport, dummytx, rxbuf, len, 100); + // Receive data + HAL_SPI_TransmitReceive(spiport, dummytx, rxbuf, len, 100); - // Release bus - __cs_deassert(); + // Release bus + __cs_deassert(); } static void __cs_assert(void) { - HAL_GPIO_WritePin(csport, cspin, 0); + HAL_GPIO_WritePin(csport, cspin, 0); } static void __cs_deassert(void) { - HAL_GPIO_WritePin(csport, cspin, 1); + HAL_GPIO_WritePin(csport, cspin, 1); } diff --git a/lib/max31856/max31856.h b/lib/max31856/max31856.h --- a/lib/max31856/max31856.h +++ b/lib/max31856/max31856.h @@ -48,16 +48,16 @@ float max31856_avg_temp(void); typedef enum { - MAX31856_TCTYPE_B = 0b0000, - MAX31856_TCTYPE_E = 0b0001, - MAX31856_TCTYPE_J = 0b0010, - MAX31856_TCTYPE_K = 0b0011, - MAX31856_TCTYPE_N = 0b0100, - MAX31856_TCTYPE_R = 0b0101, - MAX31856_TCTYPE_S = 0b0110, - MAX31856_TCTYPE_T = 0b0111, - MAX31856_VMODE_G8 = 0b1000, - MAX31856_VMODE_G32 = 0b1100, + MAX31856_TCTYPE_B = 0b0000, + MAX31856_TCTYPE_E = 0b0001, + MAX31856_TCTYPE_J = 0b0010, + MAX31856_TCTYPE_K = 0b0011, + MAX31856_TCTYPE_N = 0b0100, + MAX31856_TCTYPE_R = 0b0101, + MAX31856_TCTYPE_S = 0b0110, + MAX31856_TCTYPE_T = 0b0111, + MAX31856_VMODE_G8 = 0b1000, + MAX31856_VMODE_G32 = 0b1100, } max31856_thermocoupletype_t; diff --git a/lib/ssd1306/ssd1306.c b/lib/ssd1306/ssd1306.c --- a/lib/ssd1306/ssd1306.c +++ b/lib/ssd1306/ssd1306.c @@ -20,219 +20,219 @@ static void setStartColumn(unsigned char // Initialize OLED void ssd1306_init(void) { - __SPI3_CLK_ENABLE(); - __GPIOA_CLK_ENABLE(); - __GPIOB_CLK_ENABLE(); - GPIO_InitTypeDef GPIO_InitStruct; + __SPI3_CLK_ENABLE(); + __GPIOA_CLK_ENABLE(); + __GPIOB_CLK_ENABLE(); + GPIO_InitTypeDef GPIO_InitStruct; - // 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); + // 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 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); + // 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(); + // 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(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 + WriteCommand(0xAF); // display on } // Times New Roman font 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 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 + // 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 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(); + 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(); + SSD_CS_Low(); + SSD_A0_High(); + SPI_SendByte(data); + SSD_CS_High(); } @@ -240,7 +240,7 @@ static void WriteData(unsigned char data static void setStartPage(unsigned char d) { WriteCommand(0xB0|d); // Set Page Start Address for Page Addressing Mode - // Default => 0xB0 (0x00) + // Default => 0xB0 (0x00) } @@ -249,19 +249,19 @@ 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 + // 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}, + {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}, + {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}, + {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}, + {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}, }; diff --git a/src/display.c b/src/display.c --- a/src/display.c +++ b/src/display.c @@ -42,27 +42,27 @@ static uint8_t toggle = 0; void display_1hz(void) { - toggle = !toggle; + toggle = !toggle; } static char updown(void) { - if(toggle) - return '\x8f'; - else - return '\x90'; + if(toggle) + return '\x8f'; + else + return '\x90'; } // Display state machine void display_process(void) { - therm_status_t* status = runtime_status(); - therm_settings_t* set = flash_getsettings(); + therm_status_t* status = runtime_status(); + therm_settings_t* set = flash_getsettings(); uint8_t state_changed = status->state != last_state; last_state = status->state; - + uint8_t temp_changed = status->temp != last_temp; last_temp = status->temp; @@ -85,23 +85,23 @@ void display_process(void) if(temp_changed || state_changed) { char tempstr[16]; snprintf(tempstr, 16, "Temp: %4.1f", status->temp); -// ssd1306_drawstring(" ", 3, 40); + // ssd1306_drawstring(" ", 3, 40); ssd1306_drawstring(tempstr, 3, 40); } if (state_changed) { - ssd1306_drawlogo(); + ssd1306_drawlogo(); } switch(goto_mode) { case MODE_HEAT: { - if(set->val.plant_type == PLANT_HEATER) - ssd1306_drawstring("\x83 heat ", 1, 40); - else - ssd1306_drawstring("\x83 cool ", 1, 40); + if(set->val.plant_type == PLANT_HEATER) + ssd1306_drawstring("\x83 heat ", 1, 40); + else + ssd1306_drawstring("\x83 cool ", 1, 40); } break; @@ -115,12 +115,12 @@ void display_process(void) ssd1306_drawstring("\x83 reset ", 1, 40); } break; - #ifdef BOOTLOADER_SHORTCUT +#ifdef BOOTLOADER_SHORTCUT case MODE_BOOTLOADER: { ssd1306_drawstring("\x83 dfu ", 1, 40); } - #endif +#endif } // Button handler @@ -136,16 +136,16 @@ void display_process(void) status->state = STATE_RESET; reset_mode = RESET_REBOOT; break; - #ifdef BOOTLOADER_SHORTCUT +#ifdef BOOTLOADER_SHORTCUT case MODE_BOOTLOADER: 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 + // bootloader_enter(); // Resets into bootloader status->state = STATE_RESET; // Just in case break; - #endif +#endif default: status->state = STATE_PREHEAT; } @@ -174,7 +174,7 @@ void display_process(void) ssd1306_drawlogo(); ssd1306_drawchar(updown(), 1, 52); - ssd1306_drawstring(sensor_lookup[set->val.sensor_type], 1, 60); + ssd1306_drawstring(sensor_lookup[set->val.sensor_type], 1, 60); ssd1306_drawstring("Press to accept", 3, 40); // Button handler @@ -183,9 +183,9 @@ void display_process(void) } else { - user_input((uint16_t*)&set->val.sensor_type); + user_input((uint16_t*)&set->val.sensor_type); if(set->val.sensor_type > 6) - set->val.sensor_type = 6; + set->val.sensor_type = 6; } // Event Handler @@ -210,7 +210,7 @@ void display_process(void) ssd1306_drawstring("Thermostat", 1, 60); ssd1306_drawstring("Press to accept", 3, 40); - + // Button handler if(SW_BTN_PRESSED) { status->state = STATE_SETPLANTTYPE; @@ -223,7 +223,7 @@ void display_process(void) } // Event Handler // N/A - + } break; @@ -243,7 +243,7 @@ void display_process(void) ssd1306_drawstring("Cooler", 1, 60); ssd1306_drawstring("Press to accept", 3, 40); - + // Button handler if(SW_BTN_PRESSED) { if(set->val.control_mode == MODE_PID) @@ -259,7 +259,7 @@ void display_process(void) } // Event Handler // N/A - + } break; @@ -279,7 +279,7 @@ void display_process(void) snprintf(tempstr, 12, "P=%d", set->val.k_p); ssd1306_drawstring(tempstr, 1, 60); ssd1306_drawstring("Press to accept", 3, 40); - + // Button handler if(SW_BTN_PRESSED) { status->state = STATE_SETI; @@ -290,7 +290,7 @@ void display_process(void) // Event Handler // N/A - + } break; case STATE_SETI: @@ -308,7 +308,7 @@ void display_process(void) ssd1306_drawstring(tempstr, 1, 60); ssd1306_drawstring("Press to accept", 3, 40); - + // Button handler if(SW_BTN_PRESSED) { status->state = STATE_SETD; @@ -319,7 +319,7 @@ void display_process(void) // Event Handler // N/A - + } break; case STATE_SETD: @@ -348,7 +348,7 @@ void display_process(void) // Event Handler // N/A - + } break; @@ -407,7 +407,7 @@ void display_process(void) // Event Handler // N/A - + } break; case STATE_SETBOOTTOBREW: @@ -419,7 +419,7 @@ void display_process(void) ssd1306_drawlogo(); ssd1306_drawstring("sob=", 1, 50); - + ssd1306_drawchar(updown(), 1, 43); if(set->val.boottobrew) @@ -442,7 +442,7 @@ void display_process(void) // Event Handler // N/A - + } break; case STATE_SETUNITS: @@ -475,7 +475,7 @@ void display_process(void) // Event Handler // N/A - + } break; @@ -497,7 +497,7 @@ void display_process(void) // Button handler if(SW_BTN_PRESSED) { - flash_savesettings(); + flash_savesettings(); status->state = STATE_IDLE; } else { @@ -506,7 +506,7 @@ void display_process(void) // Event Handler // N/A - + } break; @@ -539,7 +539,7 @@ void display_process(void) if(status->temp >= status->setpoint) { status->state = STATE_MAINTAIN; } - + } break; case STATE_MAINTAIN: @@ -568,7 +568,7 @@ void display_process(void) // Event Handler // N/A - + } break; // Thermocouple error @@ -601,9 +601,9 @@ void display_process(void) // Button handler if(SW_BTN_PRESSED) { status->state = STATE_IDLE; - #ifdef MAX31865_RTD_SENSOR - max31865_clear_errors(spi_get()); - #endif +#ifdef MAX31865_RTD_SENSOR + max31865_clear_errors(spi_get()); +#endif } else if(SW_RIGHT_PRESSED) { set->val.ignore_error = 1; @@ -612,7 +612,7 @@ void display_process(void) // Event Handler // Maybe handle if TC is plugged in // N/A - + } break; @@ -627,10 +627,10 @@ void display_process(void) ssd1306_drawlogo(); switch(reset_mode) { - case RESET_DEFAULTS: - { - ssd1306_drawstring("-> defaults ", 1, 40); - } break; + case RESET_DEFAULTS: + { + ssd1306_drawstring("-> defaults ", 1, 40); + } break; case RESET_BOOTLOADER: { ssd1306_drawstring("-> bootloader ", 1, 40); @@ -655,14 +655,14 @@ void display_process(void) ssd1306_drawstring("Device won't boot", 2, 0); ssd1306_drawstring("until reflashed!", 3, 0); HAL_Delay(1000); -// bootloader_enter(); // Resets into bootloader + // bootloader_enter(); // Resets into bootloader status->state = STATE_RESET; // Just in case } break; case RESET_DEFAULTS: { status->state = STATE_RESET; -// flash_load_defaults(set); -// flash_save(set); + // flash_load_defaults(set); + // flash_save(set); NVIC_SystemReset(); } break; case RESET_REBOOT: @@ -696,7 +696,7 @@ void display_process(void) status->pid_enabled = 0; } break; - + } if(last_state != status->state) { diff --git a/src/main.c b/src/main.c --- a/src/main.c +++ b/src/main.c @@ -21,65 +21,65 @@ int main(void) { - sysclock_init(); - hal_init(); - gpio_init(); + sysclock_init(); + hal_init(); + gpio_init(); - ssd1306_init(); + ssd1306_init(); - // Startup screen + // Startup screen display_startup_screen(); HAL_Delay(2000); ssd1306_clearscreen(); - ssd1306_drawlogo(); + ssd1306_drawlogo(); // Default status - runtime_status()->temp = 0.0; - runtime_status()->state_resume = 0; - runtime_status()->state = STATE_IDLE; - runtime_status()->setpoint = 70; - runtime_status()->pid_enabled = 0; + runtime_status()->temp = 0.0; + runtime_status()->state_resume = 0; + runtime_status()->state = STATE_IDLE; + runtime_status()->setpoint = 70; + runtime_status()->pid_enabled = 0; pid_init(); pwmout_init(); flash_init(); - watchdog_init(); - tempsense_init(); + watchdog_init(); + tempsense_init(); - // Soft timers + // Soft timers uint32_t last_pid = 0; uint32_t last_thermostat = 0; uint32_t last_1hz = 0; uint32_t last_5hz = 0; - int16_t duty = 0; + int16_t duty = 0; - while (1) - { + while (1) + { - if(HAL_GetTick() - last_1hz > 750) - { - display_1hz(); - last_1hz = HAL_GetTick(); - } + if(HAL_GetTick() - last_1hz > 750) + { + display_1hz(); + last_1hz = HAL_GetTick(); + } - if(HAL_GetTick() - last_5hz > 200) - { - tempsense_readtemp(); - runtime_status()->temp = tempsense_gettemp(); - last_5hz = HAL_GetTick(); - } + if(HAL_GetTick() - last_5hz > 200) + { + tempsense_readtemp(); + runtime_status()->temp = tempsense_gettemp(); + last_5hz = HAL_GetTick(); + } if(flash_getsettings()->val.control_mode == MODE_PID && (HAL_GetTick() - last_pid > PID_PERIOD)) { - duty = pid_process(); + duty = pid_process(); last_pid = HAL_GetTick(); } // Thermostatic control if(flash_getsettings()->val.control_mode == MODE_THERMOSTAT && HAL_GetTick() - last_thermostat > SSR_PERIOD) { - duty = thermostat_process(); + duty = thermostat_process(); last_thermostat = HAL_GetTick(); } @@ -88,24 +88,24 @@ int main(void) watchdog_feed(); -// // Transmit temperature over USB-CDC on a regular basis -// if(HAL_GetTick() - 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 = HAL_GetTick(); -// } + // // Transmit temperature over USB-CDC on a regular basis + // if(HAL_GetTick() - 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 = HAL_GetTick(); + // } - } + } } diff --git a/src/pid.c b/src/pid.c --- a/src/pid.c +++ b/src/pid.c @@ -12,105 +12,105 @@ 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; + 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 + // #ifdef MAX31865_RTD_SENSOR + // max31865_readtemp(spi_get(), &set, &status); + // #else + // max31855_readtemp(spi_get(), &set, &status); // Read MAX31855 + // #endif - float ssr_output = 0; + float ssr_output = 0; - if(runtime_status()->pid_enabled) - { - // Get ssr output for next time - int16_t power_percent = pid_update(); + 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; + 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; + //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; - } + // 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; + return ssr_output; //ssr_output; } // Calculate new PID values int16_t pid_update(void) { - therm_status_t* status = runtime_status(); - therm_settings_t* set = flash_getsettings(); + therm_status_t* status = runtime_status(); + therm_settings_t* set = flash_getsettings(); - // Convert temperature to fixed point number with 1/10th resolution - float temp = status->temp; + // Convert temperature to fixed point number with 1/10th resolution + float temp = status->temp; - // Calculate instantaneous error - // EMZ FIXME: was regulating to 1 degree below the setpoint! +1 accomadates for this - int16_t error = (status->setpoint+1) - temp; + // Calculate instantaneous error + // EMZ FIXME: was regulating to 1 degree below the setpoint! +1 accomadates for this + int16_t error = (status->setpoint+1) - temp; - // Proportional component - int32_t p_term = set->val.k_p * error; + // Proportional component + int32_t p_term = set->val.k_p * error; - // Error accumulator (integrator) - state.i_state += error; + // Error accumulator (integrator) + state.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 * 10) / set->val.k_i; + // 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 * 10) / set->val.k_i; - // Calculate integral term with windup guard - if (state.i_state > windup_guard_res) - state.i_state = windup_guard_res; - else if (state.i_state < -windup_guard_res) - state.i_state = -windup_guard_res; + // Calculate integral term with windup guard + if (state.i_state > windup_guard_res) + state.i_state = windup_guard_res; + else if (state.i_state < -windup_guard_res) + state.i_state = -windup_guard_res; - // Discard I if we've achieved the setpoint (TODO: add setting disable/enable) - if(error <= 0) - state.i_state = 0; + // Discard I if we've achieved the setpoint (TODO: add setting disable/enable) + if(error <= 0) + state.i_state = 0; - int32_t i_term = set->val.k_i * state.i_state; + int32_t i_term = set->val.k_i * state.i_state; - // Calculate differential term (slope since last iteration) - int32_t d_term = (set->val.k_d * (temp - state.last_pid_temp)); + // Calculate differential term (slope since last iteration) + int32_t d_term = (set->val.k_d * (temp - state.last_pid_temp)); - // Save temperature for next iteration - state.last_pid_temp = temp; + // Save temperature for next iteration + state.last_pid_temp = temp; - int16_t result = (p_term + i_term - d_term) / 10; + int16_t result = (p_term + i_term - d_term) / 10; - // Put out tenths of percent, 0-1000. - if(result > 1000) - result = 1000; - else if(result < -1000) - result = -1000; + // Put out tenths of percent, 0-1000. + if(result > 1000) + result = 1000; + else if(result < -1000) + result = -1000; - // Return feedback - return result; + // Return feedback + return result; } diff --git a/src/pwmout.c b/src/pwmout.c --- a/src/pwmout.c +++ b/src/pwmout.c @@ -17,68 +17,68 @@ static uint32_t last_ssr_on = 0; // Initialize hardware PWM output void pwmout_init(void) { - GPIO_InitTypeDef GPIO_InitStruct; + 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); + // 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; + 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); - } + 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); - } + 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); - } + 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); - } + 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_OC_Start_IT(&htim17, TIM_CHANNEL_1); __HAL_TIM_ENABLE_IT(&htim17, TIM_IT_UPDATE); } @@ -90,21 +90,21 @@ void pwmout_init(void) // 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; + if(duty == 0) + { + HAL_GPIO_WritePin(SSR, 0); + HAL_GPIO_WritePin(LED, 0); + } + if(duty < 0) + duty = 0; - htim17.Instance->CCR1 = duty; //duty; + htim17.Instance->CCR1 = duty; //duty; } // Accessor for timer handle TIM_HandleTypeDef* pwmout_get_tim(void) { - return &htim17; + return &htim17; } diff --git a/src/system/error.c b/src/system/error.c --- a/src/system/error.c +++ b/src/system/error.c @@ -23,24 +23,24 @@ static volatile uint8_t num_errors_asser // Set the passed error flag void error_assert(const uint8_t errno) { - // Errno invalid: exceeds bit length of error register - if(errno >= 32) - return; + // Errno invalid: exceeds bit length of error register + if(errno >= 32) + return; - // Don't count info messages as true errors - if(errno > ERR_INFO) - { - // Set error flag - err_reg |= (1< ERR_INFO) + { + // Set error flag + err_reg |= (1<= 32) - return; + // Errno invalid: exceeds bit length of error register + if(errno >= 32) + return; - // Don't count info messages as true errors - if(errno > ERR_INFO) - { - // Set error flag - err_reg |= (1< ERR_INFO) + { + // Set error flag + err_reg |= (1< ERR_INFO) - { - ir_efs_send(outbuf); - error_sidechannel_addmsg(outbuf); - } + if(errno > ERR_INFO) + { + ir_efs_send(outbuf); + error_sidechannel_addmsg(outbuf); + } #endif } @@ -83,19 +83,19 @@ void error_assert_info(const uint8_t err // Check if the passed error flag has been asserted inline uint8_t error_check(const uint8_t errno) { - return (err_reg & (1< 0; + return (err_reg & (1< 0; } // Return 1 if any error has occurred inline uint8_t error_occurred(void) { - return err_reg > 0; + return err_reg > 0; } // Return the number of errors that have occurred inline uint8_t error_count(void) { - return num_errors_asserted; + return num_errors_asserted; } diff --git a/src/system/flash.c b/src/system/flash.c --- a/src/system/flash.c +++ b/src/system/flash.c @@ -16,78 +16,78 @@ static therm_status_t status; // Initialize flash and restore settings void flash_init(void) { - flash_restoresettings(); + flash_restoresettings(); } // Save settings to flash memory void flash_savesettings() { - // Unlock flash memory - HAL_FLASH_Unlock(); + // 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; + // 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); + // 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 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); + // 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); + // 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) - { - // Read page of flash into settings structure - uint16_t readctr = 0; - for(readctr = 0; readctr < 128; readctr++) - { - settings.data[readctr] = eeprom_emulation[readctr]; - } - } - // No data in flash! Set defaults here - else - { - settings.val.k_p = 100; - settings.val.k_i = 2; - settings.val.k_d = 0; - settings.val.windup_guard = 300; - settings.val.sensor_type = 1; - //torestore.values.can_id = 22; - } + // Check for magic flash value + if(eeprom_emulation[FLASH_MAGIC_LOC] == FLASH_MAGIC_VALUE) + { + // Read page of flash into settings structure + uint16_t readctr = 0; + for(readctr = 0; readctr < 128; readctr++) + { + settings.data[readctr] = eeprom_emulation[readctr]; + } + } + // No data in flash! Set defaults here + else + { + settings.val.k_p = 100; + settings.val.k_i = 2; + settings.val.k_d = 0; + settings.val.windup_guard = 300; + settings.val.sensor_type = 1; + //torestore.values.can_id = 22; + } } // Accessor to retrieve settings structure inline therm_settings_t* flash_getsettings(void) { - return &settings; + return &settings; } inline therm_status_t* runtime_status(void) { - return &status; + return &status; } diff --git a/src/system/gpio.c b/src/system/gpio.c --- a/src/system/gpio.c +++ b/src/system/gpio.c @@ -14,42 +14,42 @@ static uint32_t change_time_reset = 0; // Initialize GPIOs void gpio_init(void) { - GPIO_InitTypeDef GPIO_InitStruct; + GPIO_InitTypeDef GPIO_InitStruct; - // GPIO Ports Clock Enable - __GPIOA_CLK_ENABLE(); - __GPIOB_CLK_ENABLE(); - __GPIOF_CLK_ENABLE(); + // 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); + // 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_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); + 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); + /* 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); + HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); - // Define startup State - HAL_GPIO_WritePin(LED, 0); + // Define startup State + HAL_GPIO_WritePin(LED, 0); } @@ -73,17 +73,17 @@ void user_input(uint16_t* 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 + //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)++; + (*to_modify)++; } else if(!HAL_GPIO_ReadPin(SW_DOWN)) { CHANGE_RESET; if (*to_modify >= -32768) - (*to_modify)--; + (*to_modify)--; } } } diff --git a/src/system/interrupts.c b/src/system/interrupts.c --- a/src/system/interrupts.c +++ b/src/system/interrupts.c @@ -13,76 +13,76 @@ // Systick interrupt void SysTick_Handler(void) { - HAL_IncTick(); - HAL_SYSTICK_IRQHandler(); + HAL_IncTick(); + HAL_SYSTICK_IRQHandler(); } void EXTI9_5_IRQHandler(void) { - HAL_GPIO_EXTI_IRQHandler(SW_B_Pin); - HAL_GPIO_EXTI_IRQHandler(SW_A_Pin); - HAL_GPIO_EXTI_IRQHandler(SW_C_Pin); + HAL_GPIO_EXTI_IRQHandler(SW_B_Pin); + HAL_GPIO_EXTI_IRQHandler(SW_A_Pin); + HAL_GPIO_EXTI_IRQHandler(SW_C_Pin); } void EXTI15_10_IRQHandler(void) { - HAL_GPIO_EXTI_IRQHandler(SW_BTN_Pin); + HAL_GPIO_EXTI_IRQHandler(SW_BTN_Pin); } uint32_t last_button_press = 0; void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { - switch(GPIO_Pin) - { - case SW_BTN_Pin: - { -// if(HAL_GetTick() > last_button_press + 100) -// { -// HAL_GPIO_TogglePin(LED_RED); -// HAL_GPIO_TogglePin(GATE_DRIVE); -// last_button_press = HAL_GetTick(); -// } - } break; - } + switch(GPIO_Pin) + { + case SW_BTN_Pin: + { + // if(HAL_GetTick() > last_button_press + 100) + // { + // HAL_GPIO_TogglePin(LED_RED); + // HAL_GPIO_TogglePin(GATE_DRIVE); + // last_button_press = HAL_GetTick(); + // } + } break; + } } void TIM1_TRG_COM_TIM17_IRQHandler(void) { - /* USER CODE BEGIN TIM1_TRG_COM_TIM17_IRQn 0 */ + /* USER CODE BEGIN TIM1_TRG_COM_TIM17_IRQn 0 */ - /* USER CODE END TIM1_TRG_COM_TIM17_IRQn 0 */ + /* USER CODE END TIM1_TRG_COM_TIM17_IRQn 0 */ - HAL_TIM_IRQHandler(pwmout_get_tim()); - /* USER CODE BEGIN TIM1_TRG_COM_TIM17_IRQn 1 */ + HAL_TIM_IRQHandler(pwmout_get_tim()); + /* USER CODE BEGIN TIM1_TRG_COM_TIM17_IRQn 1 */ - /* USER CODE END TIM1_TRG_COM_TIM17_IRQn 1 */ + /* USER CODE END TIM1_TRG_COM_TIM17_IRQn 1 */ } void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim) { - if(htim == pwmout_get_tim()) - { - HAL_GPIO_WritePin(SSR, 0); - HAL_GPIO_WritePin(LED, 0); - } + if(htim == pwmout_get_tim()) + { + HAL_GPIO_WritePin(SSR, 0); + HAL_GPIO_WritePin(LED, 0); + } } void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { - if(htim == pwmout_get_tim()) - { - if(htim->Instance->CCR1 == 0) - { - HAL_GPIO_WritePin(LED, 0); - HAL_GPIO_WritePin(SSR, 0); - } - else - { - HAL_GPIO_WritePin(LED, 1); - HAL_GPIO_WritePin(SSR, 1); - } - } + if(htim == pwmout_get_tim()) + { + if(htim->Instance->CCR1 == 0) + { + HAL_GPIO_WritePin(LED, 0); + HAL_GPIO_WritePin(SSR, 0); + } + else + { + HAL_GPIO_WritePin(LED, 1); + HAL_GPIO_WritePin(SSR, 1); + } + } } diff --git a/src/system/sbrk.c b/src/system/sbrk.c --- a/src/system/sbrk.c +++ b/src/system/sbrk.c @@ -6,17 +6,17 @@ caddr_t _sbrk(int incr) { -extern char end asm("end"); -static char *heap_end; -char *prev_heap_end; + extern char end asm("end"); + static char *heap_end; + char *prev_heap_end; -if (heap_end == 0) { -heap_end = &end; + if (heap_end == 0) { + heap_end = &end; + } + + prev_heap_end = heap_end; + + heap_end += incr; + + return (caddr_t)prev_heap_end; } - -prev_heap_end = heap_end; - -heap_end += incr; - -return (caddr_t)prev_heap_end; -} diff --git a/src/system/stringhelpers.c b/src/system/stringhelpers.c --- a/src/system/stringhelpers.c +++ b/src/system/stringhelpers.c @@ -42,14 +42,14 @@ char* itoa_fp(int16_t i, uint8_t frac, c ++p; shifter = shifter/10; }while(shifter); - + ++p; // increment for decimal point do{ ++p; frac_shifter = frac_shifter/10; }while(frac_shifter); - + // Null-terminate the string *p = '\0'; diff --git a/src/system/system.c b/src/system/system.c --- a/src/system/system.c +++ b/src/system/system.c @@ -8,48 +8,48 @@ // Configure and start system clocks void sysclock_init(void) { - __SYSCFG_CLK_ENABLE(); + __SYSCFG_CLK_ENABLE(); - HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); + HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); - HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); + HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); - RCC_OscInitTypeDef RCC_OscInitStruct; - RCC_ClkInitTypeDef RCC_ClkInitStruct; - RCC_PeriphCLKInitTypeDef PeriphClkInit; + RCC_OscInitTypeDef RCC_OscInitStruct; + RCC_ClkInitTypeDef RCC_ClkInitStruct; + RCC_PeriphCLKInitTypeDef PeriphClkInit; - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; - RCC_OscInitStruct.HSEState = RCC_HSE_ON; - RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; - RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; - RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL3; - HAL_RCC_OscConfig(&RCC_OscInitStruct); + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; + RCC_OscInitStruct.HSEState = RCC_HSE_ON; + RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; + RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL3; + HAL_RCC_OscConfig(&RCC_OscInitStruct); - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK - |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; - RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; - RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; - RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; - RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; - HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1); + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK + |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; + HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1); - PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB|RCC_PERIPHCLK_ADC1|RCC_PERIPHCLK_TIM17; - PeriphClkInit.USBClockSelection = RCC_USBPLLCLK_DIV1; - PeriphClkInit.Adc1ClockSelection = RCC_ADC1PLLCLK_DIV1; - PeriphClkInit.Tim17ClockSelection = RCC_TIM17CLK_HCLK; - HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit); + PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB|RCC_PERIPHCLK_ADC1|RCC_PERIPHCLK_TIM17; + PeriphClkInit.USBClockSelection = RCC_USBPLLCLK_DIV1; + PeriphClkInit.Adc1ClockSelection = RCC_ADC1PLLCLK_DIV1; + PeriphClkInit.Tim17ClockSelection = RCC_TIM17CLK_HCLK; + HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit); - HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); + HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); - HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); + HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); - /* SysTick_IRQn interrupt configuration */ - HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); + /* SysTick_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); - __DMA1_CLK_ENABLE(); + __DMA1_CLK_ENABLE(); } diff --git a/src/system/watchdog.c b/src/system/watchdog.c --- a/src/system/watchdog.c +++ b/src/system/watchdog.c @@ -17,15 +17,15 @@ uint8_t watchdog_enabled = 0; void watchdog_init(void) { #ifdef WATCHDOG_ENABLE - // ~2 seconds? + // ~2 seconds? hiwdg.Instance = IWDG; hiwdg.Init.Prescaler = IWDG_PRESCALER_4; hiwdg.Init.Window = 4095; hiwdg.Init.Reload = 4095; HAL_IWDG_Init(&hiwdg); - watchdog_feed(); - HAL_IWDG_Start(&hiwdg); - watchdog_enabled = 1; + watchdog_feed(); + HAL_IWDG_Start(&hiwdg); + watchdog_enabled = 1; #endif } @@ -34,7 +34,7 @@ void watchdog_init(void) void watchdog_feed(void) { #ifdef WATCHDOG_ENABLE - if(watchdog_enabled) - HAL_IWDG_Refresh(&hiwdg); + if(watchdog_enabled) + HAL_IWDG_Refresh(&hiwdg); #endif } diff --git a/src/tempsense.c b/src/tempsense.c --- a/src/tempsense.c +++ b/src/tempsense.c @@ -11,13 +11,13 @@ // Initialize temperature sensor void tempsense_init(void) { - // TODO: Rework SPI stuff... init the port in a sharedlib then pass ref to display and tempsense + // 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); + 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... + // 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... } @@ -25,39 +25,39 @@ void tempsense_init(void) 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; + 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; + 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... + // 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(); + float temp_converted = max31856_latest_temp(); - if(flash_getsettings()->val.temp_units == TEMP_UNITS_FAHRENHEIT) - temp_converted = temp_converted * 1.8f + 32.0f; + if(flash_getsettings()->val.temp_units == TEMP_UNITS_FAHRENHEIT) + temp_converted = temp_converted * 1.8f + 32.0f; - return temp_converted; + return temp_converted; } diff --git a/src/thermostat.c b/src/thermostat.c --- a/src/thermostat.c +++ b/src/thermostat.c @@ -11,52 +11,52 @@ float thermostat_process(void) { -// #ifdef MAX31865_RTD_SENSOR -// max31865_readtemp(spi_get(), &set, &status); -// #else -// max31855_readtemp(spi_get(), &set, &status); // Read MAX31855 -// #endif + // #ifdef MAX31865_RTD_SENSOR + // max31865_readtemp(spi_get(), &set, &status); + // #else + // max31855_readtemp(spi_get(), &set, &status); // Read MAX31855 + // #endif - float ssr_output = 0.0; - uint8_t thermostat_plant_on = 0; + float ssr_output = 0.0; + uint8_t thermostat_plant_on = 0; - // TODO: Migrate this FxP conversion to the readtemp code or similar - float temp = runtime_status()->temp; + // TODO: Migrate this FxP conversion to the readtemp code or similar + float temp = runtime_status()->temp; - // EMZ FIXME: This could be way simpler - if(flash_getsettings()->val.plant_type == PLANT_HEATER && runtime_status()->setpoint * 10 < temp - flash_getsettings()->val.hysteresis * 10) - thermostat_plant_on = 1; - else if(flash_getsettings()->val.plant_type == PLANT_HEATER && runtime_status()->setpoint * 10 > temp + flash_getsettings()->val.hysteresis * 10) - thermostat_plant_on = 0; + // EMZ FIXME: This could be way simpler + if(flash_getsettings()->val.plant_type == PLANT_HEATER && runtime_status()->setpoint * 10 < temp - flash_getsettings()->val.hysteresis * 10) + thermostat_plant_on = 1; + else if(flash_getsettings()->val.plant_type == PLANT_HEATER && runtime_status()->setpoint * 10 > temp + flash_getsettings()->val.hysteresis * 10) + thermostat_plant_on = 0; - if(flash_getsettings()->val.plant_type == PLANT_COOLER && runtime_status()->setpoint * 10 > temp + flash_getsettings()->val.hysteresis * 10) - thermostat_plant_on = 1; - else if(flash_getsettings()->val.plant_type == PLANT_COOLER && runtime_status()->setpoint * 10 < temp - flash_getsettings()->val.hysteresis * 10) - thermostat_plant_on = 0; + if(flash_getsettings()->val.plant_type == PLANT_COOLER && runtime_status()->setpoint * 10 > temp + flash_getsettings()->val.hysteresis * 10) + thermostat_plant_on = 1; + else if(flash_getsettings()->val.plant_type == PLANT_COOLER && runtime_status()->setpoint * 10 < temp - flash_getsettings()->val.hysteresis * 10) + thermostat_plant_on = 0; - // EMZ: TODO: Refactor to output_enabled or something - if(runtime_status()->pid_enabled && thermostat_plant_on) - { - // EMZ TODO: functionalize this - // 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[6]; - itoa(ssr_output, tempstr, 10); - ssd1306_drawstring(tempstr, 0, 90); + // EMZ: TODO: Refactor to output_enabled or something + if(runtime_status()->pid_enabled && thermostat_plant_on) + { + // EMZ TODO: functionalize this + // 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[6]; + itoa(ssr_output, tempstr, 10); + ssd1306_drawstring(tempstr, 0, 90); -// HAL_GPIO_WritePin(SSR_PIN, 1); - HAL_GPIO_WritePin(LED, 1); - } - else - { -// HAL_GPIO_WritePin(SSR_PIN, 0); - HAL_GPIO_WritePin(LED, 0); - } + // HAL_GPIO_WritePin(SSR_PIN, 1); + HAL_GPIO_WritePin(LED, 1); + } + else + { + // HAL_GPIO_WritePin(SSR_PIN, 0); + HAL_GPIO_WritePin(LED, 0); + } - return ssr_output; + return ssr_output; }