2020-12-17 12:41:21 +01:00
/** firmware to raise sourdough starter (aka. levain)
2020-02-17 18:08:17 +01:00
* @ file
2016-08-14 21:02:38 +02:00
* @ author King Kévin < kingkevin @ cuvoodoo . info >
2020-06-06 14:35:55 +02:00
* @ copyright SPDX - License - Identifier : GPL - 3.0 - or - later
2020-02-17 18:08:17 +01:00
* @ date 2016 - 2020
2016-08-14 21:02:38 +02:00
*/
2016-01-17 14:54:54 +01:00
/* standard libraries */
# include <stdint.h> // standard integer types
# include <stdlib.h> // standard utilities
2016-08-14 21:02:38 +02:00
# include <string.h> // string utilities
2019-06-18 17:43:43 +02:00
# include <time.h> // date/time utilities
# include <ctype.h> // utilities to check chars
2020-12-16 02:46:14 +01:00
# include <math.h> // NaN definition
2016-01-17 14:54:54 +01:00
/* STM32 (including CM3) libraries */
2016-01-18 16:23:35 +01:00
# include <libopencmsis/core_cm3.h> // Cortex M3 utilities
2016-10-23 17:42:27 +02:00
# include <libopencm3/cm3/scb.h> // vector table definition
2016-01-29 11:25:30 +01:00
# include <libopencm3/cm3/nvic.h> // interrupt utilities
2016-10-23 17:42:27 +02:00
# include <libopencm3/stm32/gpio.h> // general purpose input output library
# include <libopencm3/stm32/rcc.h> // real-time control clock library
2016-01-29 11:25:30 +01:00
# include <libopencm3/stm32/exti.h> // external interrupt utilities
2016-08-14 21:02:38 +02:00
# include <libopencm3/stm32/rtc.h> // real time clock utilities
2016-10-23 17:42:27 +02:00
# include <libopencm3/stm32/iwdg.h> // independent watchdog utilities
# include <libopencm3/stm32/dbgmcu.h> // debug utilities
2018-04-06 17:37:17 +02:00
# include <libopencm3/stm32/desig.h> // design utilities
2016-10-23 17:42:27 +02:00
# include <libopencm3/stm32/flash.h> // flash utilities
2020-12-16 02:46:14 +01:00
# include <libopencm3/stm32/adc.h> // ADC utilities
2016-01-17 14:54:54 +01:00
/* own libraries */
2016-01-28 21:21:50 +01:00
# include "global.h" // board definitions
2017-04-03 13:32:45 +02:00
# include "print.h" // printing utilities
2018-02-18 15:18:42 +01:00
# include "uart.h" // USART utilities
2016-01-18 16:23:35 +01:00
# include "usb_cdcacm.h" // USB CDC ACM utilities
2018-04-06 17:37:17 +02:00
# include "terminal.h" // handle the terminal interface
# include "menu.h" // menu utilities
2020-12-11 00:07:19 +01:00
# include "sensor_sr04.h" // range measurement utilities
2020-12-11 20:59:39 +01:00
# include "sensor_ds18b20.h" // 1-Wire temperature sensor utilities
2020-12-12 14:12:21 +01:00
# include "oled_text.h" // OLED display utilities
2016-01-17 14:54:54 +01:00
2020-02-02 21:19:03 +01:00
/** watchdog period in ms */
# define WATCHDOG_PERIOD 10000
2020-11-30 14:36:33 +01:00
/** wakeup frequency (i.e. least number of times per second to perform the main loop) */
# define WAKEUP_FREQ 16
2016-08-14 21:02:38 +02:00
/** @defgroup main_flags flag set in interrupts to be processed in main task
* @ {
*/
2020-11-30 14:36:33 +01:00
static volatile bool wakeup_flag = false ; /**< flag set when wakeup timer triggered */
static volatile bool second_flag = false ; /**< flag set when a second passed */
2016-08-14 21:02:38 +02:00
/** @} */
2020-12-12 14:45:21 +01:00
/** number of seconds since boot */
static uint32_t boot_time = 0 ;
2020-12-16 19:36:15 +01:00
// current state
2020-12-17 12:41:21 +01:00
static uint32_t levain_time = 0 ; /**< when we start heating the yeas */
static uint32_t max_time = 0 ; /**< when the sourdough starter has grown */
2020-12-16 19:36:15 +01:00
static uint16_t current_height = 0 ; /**< current height */
static uint8_t container_height = 0 ; /**< how height the container is */
2020-12-17 12:41:21 +01:00
static uint8_t starter_height = 0 ; /**< how height the sourdough starter starter is */
static uint8_t max_height = 0 ; /**< the maximum height the sourdough starter reached */
2020-12-16 19:36:15 +01:00
static float heater_temp = NAN ; /**< heater temperature */
2020-12-17 12:41:21 +01:00
static float levain_temp = NAN ; /**< sourdough starter temperature */
2020-12-16 19:36:15 +01:00
2020-12-16 02:46:14 +01:00
/** heater control pin
* @ note connected to power nMOS gate , pulled up to 5 V
*/
# define HEATER_PIN PB12
# define heater_on() gpio_set(GPIO_PORT(HEATER_PIN), GPIO_PIN(HEATER_PIN)) // switch transistor on to let resistor heat
# define heater_off() gpio_clear(GPIO_PORT(HEATER_PIN), GPIO_PIN(HEATER_PIN)) // switch transistor off
2020-12-16 19:46:04 +01:00
/** maximum heater temperature, in °C
* @ note 50 ° C only allowed the starter to go up to 29 ° C
*/
# define HEATER_LIMIT 60.0
2020-12-16 02:46:14 +01:00
/** ADC channel connected to thermistor */
# define THERMISTOR_CHANNEL 6 // PA6
/** voltages to convert (channel 17 = internal voltage reference) */
const uint8_t channels [ ] = { ADC_CHANNEL17 , ADC_CHANNEL ( THERMISTOR_CHANNEL ) } ;
2020-12-16 19:36:15 +01:00
/** pin to control buzzer (active high, piezo-element with driver circuit) */
# define BUZZER_PIN PB2
2020-12-17 12:41:21 +01:00
/** temperature to heat the sourdough starter to, in °C */
const float levain_target = 30.0 ;
2020-12-16 02:46:14 +01:00
2017-04-03 13:32:45 +02:00
size_t putc ( char c )
2016-01-17 14:54:54 +01:00
{
2017-04-03 13:32:45 +02:00
size_t length = 0 ; // number of characters printed
2018-04-06 17:37:17 +02:00
static char last_c = 0 ; // to remember on which character we last sent
if ( ' \n ' = = c ) { // send carriage return (CR) + line feed (LF) newline for each LF
if ( ' \r ' ! = last_c ) { // CR has not already been sent
2018-02-18 15:18:42 +01:00
uart_putchar_nonblocking ( ' \r ' ) ; // send CR over USART
2017-04-19 16:24:07 +02:00
usb_cdcacm_putchar ( ' \r ' ) ; // send CR over USB
2018-04-06 17:37:17 +02:00
length + + ; // remember we printed 1 character
2016-01-17 14:54:54 +01:00
}
}
2018-04-06 17:37:17 +02:00
uart_putchar_nonblocking ( c ) ; // send byte over USART
usb_cdcacm_putchar ( c ) ; // send byte over USB
length + + ; // remember we printed 1 character
last_c = c ; // remember last character
2019-03-26 19:27:40 +01:00
return length ; // return number of characters printed
2016-01-17 14:54:54 +01:00
}
2020-12-16 02:46:14 +01:00
/** get voltage of thermistor (in V)
* @ return thermistor voltage
*/
static double thermistor_voltage ( void )
{
// read thermistor resistance using ADC (using resistor divider)
ADC_SR ( ADC1 ) = 0 ; // reset flags
uint16_t adc_values [ LENGTH ( channels ) ] ;
double voltages [ LENGTH ( channels ) ] ;
for ( uint8_t i = 0 ; i < LENGTH ( channels ) ; i + + ) {
adc_start_conversion_regular ( ADC1 ) ; // start conversion (using trigger)
while ( ! adc_eoc ( ADC1 ) ) ; // wait until conversion finished
adc_values [ i ] = adc_read_regular ( ADC1 ) ; // read voltage value (clears flag)
voltages [ i ] = adc_values [ i ] * 1.21 / adc_values [ 0 ] ; // use 1.21 V internal voltage reference to get ADC voltage
}
return voltages [ 1 ] ;
}
/** get temperature of thermistor (in °C)
* @ return thermistor temperature
*/
static double thermistor_temperature ( void )
{
// convert to °C
// calibrated using a TP101
# define TEMP1 20.5
# define VOLTAGE1 1.813
# define TEMP2 37.9
# define VOLTAGE2 1.197
# define THERMISTOR_SLOPE ((TEMP2 - TEMP1) / (VOLTAGE2 - VOLTAGE1))
# define THERMISTOR_OFFSET (TEMP1 - VOLTAGE1 * THERMISTOR_SLOPE)
return thermistor_voltage ( ) * THERMISTOR_SLOPE + THERMISTOR_OFFSET ;
}
2020-11-27 17:07:39 +01:00
// menu commands
2018-04-06 17:37:17 +02:00
/** display available commands
* @ param [ in ] argument no argument required
*/
static void command_help ( void * argument ) ;
/** show software and hardware version
* @ param [ in ] argument no argument required
*/
2020-11-27 17:07:39 +01:00
static void command_version ( void * argument )
{
( void ) argument ; // we won't use the argument
printf ( " firmware date: %04u-%02u-%02u \n " , BUILD_YEAR , BUILD_MONTH , BUILD_DAY ) ; // show firmware build date
printf ( " device serial: %08x%08x%08x \n " , DESIG_UNIQUE_ID2 , DESIG_UNIQUE_ID1 , DESIG_UNIQUE_ID0 ) ; // show complete serial (different than the one used for USB)
}
2018-04-06 17:37:17 +02:00
2020-12-12 14:45:21 +01:00
/** convert RTC date/time to number of seconds
* @ return number of seconds since 2000 - 01 - 01 00 : 00 : 00
* @ warning for simplicity I consider every month to have 31 days
*/
static uint32_t rtc_to_seconds ( void )
{
rtc_wait_for_synchro ( ) ; // wait until date/time is synchronised
const uint8_t year = ( ( RTC_DR > > RTC_DR_YT_SHIFT ) & RTC_DR_YT_MASK ) * 10 + ( ( RTC_DR > > RTC_DR_YU_SHIFT ) & RTC_DR_YU_MASK ) ; // get year
uint8_t month = ( ( RTC_DR > > RTC_DR_MT_SHIFT ) & RTC_DR_MT_MASK ) * 10 + ( ( RTC_DR > > RTC_DR_MU_SHIFT ) & RTC_DR_MU_MASK ) ; // get month
2020-12-16 02:46:14 +01:00
if ( ( RTC_ISR & RTC_ISR_INITS ) & & month > 0 ) { // month has been initialized, but starts with 1
2020-12-12 14:45:21 +01:00
month - - ; // fix for calculation
}
uint8_t day = ( ( RTC_DR > > RTC_DR_DT_SHIFT ) & RTC_DR_DT_MASK ) * 10 + ( ( RTC_DR > > RTC_DR_DU_SHIFT ) & RTC_DR_DU_MASK ) ; // get day
2020-12-16 02:46:14 +01:00
if ( ( RTC_ISR & RTC_ISR_INITS ) & & day > 0 ) { // day has been initialized, but starts with 1
2020-12-12 14:45:21 +01:00
day - - ; // fix for calculation
}
2020-12-16 02:46:14 +01:00
uint8_t hour = ( ( RTC_TR > > RTC_TR_HT_SHIFT ) & RTC_TR_HT_MASK ) * 10 + ( ( RTC_TR > > RTC_TR_HU_SHIFT ) & RTC_TR_HU_MASK ) ; // get hours
if ( RTC_TR & RTC_TR_PM ) { // PM notation is used
hour + = 12 ;
}
2020-12-12 14:45:21 +01:00
const uint8_t minute = ( ( RTC_TR > > RTC_TR_MNT_SHIFT ) & RTC_TR_MNT_MASK ) * 10 + ( ( RTC_TR > > RTC_TR_MNU_SHIFT ) & RTC_TR_MNU_MASK ) ; // get minutes
const uint8_t second = ( ( RTC_TR > > RTC_TR_ST_SHIFT ) & RTC_TR_ST_MASK ) * 10 + ( ( RTC_TR > > RTC_TR_SU_SHIFT ) & RTC_TR_SU_MASK ) ; // get seconds
2020-12-17 12:42:30 +01:00
const uint32_t seconds = ( ( ( ( ( ( ( ( year * 12 ) + month ) * 31 ) + day ) * 24 ) + hour ) * 60 ) + minute ) * 60 + second ; // convert to number of seconds
2020-12-12 14:45:21 +01:00
return seconds ;
}
2018-04-06 17:37:17 +02:00
/** show uptime
* @ param [ in ] argument no argument required
*/
2020-11-27 17:07:39 +01:00
static void command_uptime ( void * argument )
{
( void ) argument ; // we won't use the argument
2020-12-12 14:45:21 +01:00
const uint32_t uptime = rtc_to_seconds ( ) - boot_time ; // get time from internal RTC
2020-11-27 17:07:39 +01:00
printf ( " uptime: %u.%02u:%02u:%02u \n " , uptime / ( 24 * 60 * 60 ) , ( uptime / ( 60 * 60 ) ) % 24 , ( uptime / 60 ) % 60 , uptime % 60 ) ;
}
2018-04-06 17:37:17 +02:00
2019-06-18 17:43:43 +02:00
/** show date and time
* @ param [ in ] argument date and time to set
*/
2020-11-27 17:07:39 +01:00
static void command_datetime ( void * argument )
{
char * datetime = ( char * ) argument ; // argument is optional date time
2020-11-28 15:17:52 +01:00
const char * days [ ] = { " ?? " , " Mo " , " Tu " , " We " , " Th " , " Fr " , " Sa " , " Su " } ; // the days of the week
// set date
if ( datetime ) { // date has been provided
// parse date
const char * malformed = " date and time malformed, expecting YYYY-MM-DD WD HH:MM:SS \n " ;
if ( strlen ( datetime ) ! = ( 4 + 1 + 2 + 1 + 2 ) + 1 + 2 + 1 + ( 2 + 1 + 2 + 1 + 2 ) ) { // verify date/time is long enough
2020-11-27 17:07:39 +01:00
printf ( malformed ) ;
return ;
}
2020-11-28 15:17:52 +01:00
if ( ! ( isdigit ( ( int8_t ) datetime [ 0 ] ) & & isdigit ( ( int8_t ) datetime [ 1 ] ) & & isdigit ( ( int8_t ) datetime [ 2 ] ) & & isdigit ( ( int8_t ) datetime [ 3 ] ) & & \
' - ' = = datetime [ 4 ] & & \
isdigit ( ( int8_t ) datetime [ 5 ] ) & & isdigit ( ( int8_t ) datetime [ 6 ] ) & & \
' - ' = = datetime [ 7 ] & & \
isdigit ( ( int8_t ) datetime [ 8 ] ) & & isdigit ( ( int8_t ) datetime [ 9 ] ) & & \
' ' = = datetime [ 10 ] & & \
isalpha ( ( int8_t ) datetime [ 11 ] ) & & isalpha ( ( int8_t ) datetime [ 12 ] ) & & \
' ' = = datetime [ 13 ] & & \
isdigit ( ( int8_t ) datetime [ 14 ] ) & & isdigit ( ( int8_t ) datetime [ 15 ] ) & & \
' : ' = = datetime [ 16 ] & & \
isdigit ( ( int8_t ) datetime [ 17 ] ) & & isdigit ( ( int8_t ) datetime [ 18 ] ) & & \
' : ' = = datetime [ 19 ] & & \
isdigit ( ( int8_t ) datetime [ 20 ] ) & & isdigit ( ( int8_t ) datetime [ 21 ] ) ) ) { // verify format (good enough to not fail parsing)
2020-11-27 17:07:39 +01:00
printf ( malformed ) ;
return ;
}
2020-11-28 15:17:52 +01:00
const uint16_t year = strtol ( & datetime [ 0 ] , NULL , 10 ) ; // parse year
if ( year < = 2000 | | year > 2099 ) {
puts ( " year out of range \n " ) ;
return ;
}
const uint8_t month = strtol ( & datetime [ 5 ] , NULL , 10 ) ; // parse month
if ( month < 1 | | month > 12 ) {
puts ( " month out of range \n " ) ;
return ;
}
const uint8_t day = strtol ( & datetime [ 8 ] , NULL , 10 ) ; // parse day
if ( day < 1 | | day > 31 ) {
puts ( " day out of range \n " ) ;
return ;
}
const uint8_t hour = strtol ( & datetime [ 14 ] , NULL , 10 ) ; // parse hour
if ( hour > 24 ) {
puts ( " hour out of range \n " ) ;
return ;
}
const uint8_t minute = strtol ( & datetime [ 17 ] , NULL , 10 ) ; // parse minutes
if ( minute > 59 ) {
puts ( " minute out of range \n " ) ;
return ;
}
const uint8_t second = strtol ( & datetime [ 30 ] , NULL , 10 ) ; // parse seconds
if ( second > 59 ) {
puts ( " second out of range \n " ) ;
return ;
}
uint8_t week_day = 0 ;
for ( uint8_t i = 1 ; i < LENGTH ( days ) & & 0 = = week_day ; i + + ) {
if ( days [ i ] [ 0 ] = = toupper ( datetime [ 11 ] ) & & days [ i ] [ 1 ] = = tolower ( datetime [ 12 ] ) ) {
week_day = i ;
break ;
}
}
if ( 0 = = week_day ) {
puts ( " unknown week day \n " ) ;
return ;
}
uint32_t date = 0 ; // to build the date
date | = ( ( ( year - 2000 ) / 10 ) & RTC_DR_YT_MASK ) < < RTC_DR_YT_SHIFT ; // set year tenth
date | = ( ( ( year - 2000 ) % 10 ) & RTC_DR_YU_MASK ) < < RTC_DR_YU_SHIFT ; // set year unit
date | = ( ( month / 10 ) & RTC_DR_MT_MASK ) < < RTC_DR_MT_SHIFT ; // set month tenth
date | = ( ( month % 10 ) & RTC_DR_MU_MASK ) < < RTC_DR_MU_SHIFT ; // set month unit
date | = ( ( day / 10 ) & RTC_DR_DT_MASK ) < < RTC_DR_DT_SHIFT ; // set day tenth
date | = ( ( day % 10 ) & RTC_DR_DU_MASK ) < < RTC_DR_DU_SHIFT ; // set day unit
date | = ( week_day & RTC_DR_WDU_MASK ) < < RTC_DR_WDU_SHIFT ; // time day of the week
uint32_t time = 0 ; // to build the time
time = 0 ; // reset time
time | = ( ( hour / 10 ) & RTC_TR_HT_MASK ) < < RTC_TR_HT_SHIFT ; // set hour tenth
time | = ( ( hour % 10 ) & RTC_TR_HU_MASK ) < < RTC_TR_HU_SHIFT ; // set hour unit
time | = ( ( minute / 10 ) & RTC_TR_MNT_MASK ) < < RTC_TR_MNT_SHIFT ; // set minute tenth
time | = ( ( minute % 10 ) & RTC_TR_MNU_MASK ) < < RTC_TR_MNU_SHIFT ; // set minute unit
time | = ( ( second / 10 ) & RTC_TR_ST_MASK ) < < RTC_TR_ST_SHIFT ; // set second tenth
time | = ( ( second % 10 ) & RTC_TR_SU_MASK ) < < RTC_TR_SU_SHIFT ; // set second unit
// write date
pwr_disable_backup_domain_write_protect ( ) ; // disable backup protection so we can set the RTC clock source
rtc_unlock ( ) ; // enable writing RTC registers
RTC_ISR | = RTC_ISR_INIT ; // enter initialisation mode
while ( ! ( RTC_ISR & RTC_ISR_INITF ) ) ; // wait to enter initialisation mode
RTC_DR = date ; // set date
RTC_TR = time ; // set time
RTC_ISR & = ~ RTC_ISR_INIT ; // exit initialisation mode
rtc_lock ( ) ; // protect RTC register against writing
pwr_enable_backup_domain_write_protect ( ) ; // re-enable protection now that we configured the RTC clock
2020-12-16 02:46:14 +01:00
boot_time = rtc_to_seconds ( ) - boot_time ; // adjust boot time
2020-11-28 15:17:52 +01:00
}
// show date
if ( ! ( RTC_ISR & RTC_ISR_INITS ) ) { // date has not been set yet
puts ( " date/time not initialized \n " ) ;
} else {
rtc_wait_for_synchro ( ) ; // wait until date/time is synchronised
const uint8_t year = ( ( RTC_DR > > RTC_DR_YT_SHIFT ) & RTC_DR_YT_MASK ) * 10 + ( ( RTC_DR > > RTC_DR_YU_SHIFT ) & RTC_DR_YU_MASK ) ; // get year
const uint8_t month = ( ( RTC_DR > > RTC_DR_MT_SHIFT ) & RTC_DR_MT_MASK ) * 10 + ( ( RTC_DR > > RTC_DR_MU_SHIFT ) & RTC_DR_MU_MASK ) ; // get month
const uint8_t day = ( ( RTC_DR > > RTC_DR_DT_SHIFT ) & RTC_DR_DT_MASK ) * 10 + ( ( RTC_DR > > RTC_DR_DU_SHIFT ) & RTC_DR_DU_MASK ) ; // get day
const uint8_t week_day = ( ( RTC_DR > > RTC_DR_WDU_SHIFT ) & RTC_DR_WDU_MASK ) ; // get week day
const uint8_t hour = ( ( RTC_TR > > RTC_TR_HT_SHIFT ) & RTC_TR_HT_MASK ) * 10 + ( ( RTC_TR > > RTC_TR_HU_SHIFT ) & RTC_TR_HU_MASK ) ; // get hours
const uint8_t minute = ( ( RTC_TR > > RTC_TR_MNT_SHIFT ) & RTC_TR_MNT_MASK ) * 10 + ( ( RTC_TR > > RTC_TR_MNU_SHIFT ) & RTC_TR_MNU_MASK ) ; // get minutes
const uint8_t second = ( ( RTC_TR > > RTC_TR_ST_SHIFT ) & RTC_TR_ST_MASK ) * 10 + ( ( RTC_TR > > RTC_TR_SU_SHIFT ) & RTC_TR_SU_MASK ) ; // get seconds
printf ( " date: 20%02d-%02d-%02d %s %02d:%02d:%02d \n " , year , month , day , days [ week_day ] , hour , minute , second ) ;
2020-11-27 17:07:39 +01:00
}
}
2019-06-18 17:43:43 +02:00
2018-04-06 17:37:17 +02:00
/** reset board
* @ param [ in ] argument no argument required
*/
2020-11-27 17:07:39 +01:00
static void command_reset ( void * argument )
{
( void ) argument ; // we won't use the argument
scb_reset_system ( ) ; // reset device
while ( true ) ; // wait for the reset to happen
}
/** switch to system memory (e.g. embedded bootloader)
* @ param [ in ] argument no argument required
*/
static void command_system ( void * argument )
{
( void ) argument ; // we won't use the argument
system_memory ( ) ; // jump to system memory
}
2018-04-06 17:37:17 +02:00
/** switch to DFU bootloader
* @ param [ in ] argument no argument required
*/
2020-11-27 17:07:39 +01:00
static void command_bootloader ( void * argument )
{
( void ) argument ; // we won't use the argument
dfu_bootloader ( ) ; // start DFU bootloader
}
2018-04-06 17:37:17 +02:00
2020-12-16 19:36:15 +01:00
/** show current state, e.g. all current measurements
* @ param [ in ] argument no argument required
*/
static void command_state ( void * argument )
{
( void ) argument ; // we won't use the argument
puts ( " sourdough starter statistics: \n " ) ;
uint32_t height = ( ( container_height & & starter_height ) ? ( container_height - starter_height ) : 0 ) ;
printf ( " initial height: %u mm \n " , height ) ;
height = ( ( container_height & & current_height ) ? ( container_height - current_height ) : 0 ) ;
printf ( " current height: %u mm \n " , height ) ;
2020-12-17 12:41:21 +01:00
printf ( " sourdough temperature: %.02f °C \n " , levain_temp ) ;
2020-12-16 19:36:15 +01:00
printf ( " heater temperature: %.02f °C \n " , heater_temp ) ;
puts ( " heater: " ) ;
puts ( gpio_get ( GPIO_PORT ( HEATER_PIN ) , GPIO_PIN ( HEATER_PIN ) ) ? " on \n " : " off \n " ) ;
2020-12-17 12:41:21 +01:00
uint32_t time = ( levain_time ? ( rtc_to_seconds ( ) - levain_time ) : 0 ) ;
printf ( " current time: %02u:%02u:%02u \n " , time / ( 60 * 60 ) , ( time / 60 ) % 60 , time % 60 ) ;
2020-12-16 19:36:15 +01:00
height = ( ( container_height & & max_height ) ? ( container_height - max_height ) : 0 ) ;
printf ( " maximum height: %u mm \n " , height ) ;
2020-12-17 12:41:21 +01:00
time = ( max_time ? ( max_time - levain_time ) : 0 ) ;
printf ( " maximum height time: %02u:%02u:%02u \n " , time / ( 60 * 60 ) , ( time / 60 ) % 60 , time % 60 ) ;
2020-12-16 19:36:15 +01:00
}
2018-04-06 17:37:17 +02:00
/** list of all supported commands */
static const struct menu_command_t menu_commands [ ] = {
{
. shortcut = ' h ' ,
. name = " help " ,
. command_description = " display help " ,
. argument = MENU_ARGUMENT_NONE ,
. argument_description = NULL ,
. command_handler = & command_help ,
} ,
{
. shortcut = ' v ' ,
. name = " version " ,
. command_description = " show software and hardware version " ,
. argument = MENU_ARGUMENT_NONE ,
. argument_description = NULL ,
. command_handler = & command_version ,
} ,
{
. shortcut = ' u ' ,
. name = " uptime " ,
. command_description = " show uptime " ,
. argument = MENU_ARGUMENT_NONE ,
. argument_description = NULL ,
. command_handler = & command_uptime ,
} ,
2019-06-18 17:43:43 +02:00
{
. shortcut = ' d ' ,
. name = " date " ,
2019-07-29 18:33:05 +02:00
. command_description = " show/set date and time " ,
2019-06-18 17:43:43 +02:00
. argument = MENU_ARGUMENT_STRING ,
. argument_description = " [YYYY-MM-DD HH:MM:SS] " ,
. command_handler = & command_datetime ,
} ,
2018-04-06 17:37:17 +02:00
{
. shortcut = ' r ' ,
. name = " reset " ,
. command_description = " reset board " ,
. argument = MENU_ARGUMENT_NONE ,
. argument_description = NULL ,
. command_handler = & command_reset ,
} ,
2020-11-27 17:07:39 +01:00
{
2020-12-16 19:36:15 +01:00
. shortcut = ' S ' ,
2020-11-27 17:07:39 +01:00
. name = " system " ,
. command_description = " reboot into system memory " ,
. argument = MENU_ARGUMENT_NONE ,
. argument_description = NULL ,
. command_handler = & command_system ,
} ,
2018-04-06 17:37:17 +02:00
{
2020-12-16 19:36:15 +01:00
. shortcut = ' B ' ,
2018-04-06 17:37:17 +02:00
. name = " bootloader " ,
. command_description = " reboot into DFU bootloader " ,
. argument = MENU_ARGUMENT_NONE ,
. argument_description = NULL ,
. command_handler = & command_bootloader ,
} ,
2020-12-16 19:36:15 +01:00
{
. shortcut = ' s ' ,
. name = " state " ,
. command_description = " show state " ,
. argument = MENU_ARGUMENT_NONE ,
. argument_description = NULL ,
. command_handler = & command_state ,
} ,
2018-04-06 17:37:17 +02:00
} ;
static void command_help ( void * argument )
{
( void ) argument ; // we won't use the argument
printf ( " available commands: \n " ) ;
menu_print_commands ( menu_commands , LENGTH ( menu_commands ) ) ; // print global commands
}
2016-08-14 21:02:38 +02:00
/** process user command
* @ param [ in ] str user command string ( \ 0 ended )
*/
static void process_command ( char * str )
2016-01-17 14:54:54 +01:00
{
2018-04-06 17:37:17 +02:00
// ensure actions are available
2019-06-18 17:44:50 +02:00
if ( NULL = = menu_commands | | 0 = = LENGTH ( menu_commands ) ) {
2018-04-06 17:37:17 +02:00
return ;
2016-08-14 21:02:38 +02:00
}
2018-04-06 17:37:17 +02:00
// don't handle empty lines
2019-06-18 17:44:50 +02:00
if ( ! str | | 0 = = strlen ( str ) ) {
2018-04-06 17:37:17 +02:00
return ;
}
bool command_handled = false ;
if ( ! command_handled ) {
command_handled = menu_handle_command ( str , menu_commands , LENGTH ( menu_commands ) ) ; // try if this is not a global command
}
if ( ! command_handled ) {
printf ( " command not recognized. enter help to list commands \n " ) ;
2016-08-14 21:02:38 +02:00
}
2016-01-17 14:54:54 +01:00
}
2020-12-16 19:36:15 +01:00
/** use buzzer to beep
* @ param [ in ] beeps number of times to beep
*/
static void beep ( uint8_t beeps )
{
for ( uint8_t i = 0 ; i < beeps ; i + + ) {
gpio_set ( GPIO_PORT ( BUZZER_PIN ) , GPIO_PIN ( BUZZER_PIN ) ) ; // enable buzzer
sleep_ms ( 100 ) ; // buzz to show it is working
gpio_clear ( GPIO_PORT ( BUZZER_PIN ) , GPIO_PIN ( BUZZER_PIN ) ) ; // disable buzzer
if ( i + 1U < beeps ) {
sleep_ms ( 100 ) ; // buzz to show it is working
}
}
gpio_clear ( GPIO_PORT ( BUZZER_PIN ) , GPIO_PIN ( BUZZER_PIN ) ) ; // ensure buzzer is disabled
}
2016-08-14 21:02:38 +02:00
/** program entry point
* this is the firmware function started by the micro - controller
*/
2016-10-23 17:42:27 +02:00
void main ( void ) ;
void main ( void )
2018-04-06 17:37:17 +02:00
{
2016-08-14 21:02:38 +02:00
2016-10-23 17:42:27 +02:00
# if DEBUG
2017-01-30 09:44:51 +01:00
// enable functionalities for easier debug
2016-10-23 17:42:27 +02:00
DBGMCU_CR | = DBGMCU_CR_IWDG_STOP ; // stop independent watchdog counter when code is halted
DBGMCU_CR | = DBGMCU_CR_WWDG_STOP ; // stop window watchdog counter when code is halted
DBGMCU_CR | = DBGMCU_CR_STANDBY ; // allow debug also in standby mode (keep digital part and clock powered)
DBGMCU_CR | = DBGMCU_CR_STOP ; // allow debug also in stop mode (keep clock powered)
DBGMCU_CR | = DBGMCU_CR_SLEEP ; // allow debug also in sleep mode (keep clock powered)
2017-02-06 17:40:28 +01:00
# else
2017-01-30 09:44:51 +01:00
// setup watchdog to reset in case we get stuck (i.e. when an error occurred)
iwdg_set_period_ms ( WATCHDOG_PERIOD ) ; // set independent watchdog period
iwdg_start ( ) ; // start independent watchdog
2016-10-23 17:42:27 +02:00
# endif
2017-04-03 13:32:45 +02:00
board_setup ( ) ; // setup board
2018-02-18 15:18:42 +01:00
uart_setup ( ) ; // setup USART (for printing)
2017-04-19 16:26:40 +02:00
usb_cdcacm_setup ( ) ; // setup USB CDC ACM (for printing)
2020-12-17 12:41:21 +01:00
puts ( " \n welcome to the CuVoodoo elevainitor sourdough starter raiser \n " ) ; // print welcome message
2016-01-29 00:24:49 +01:00
2019-12-21 19:26:27 +01:00
# if DEBUG
// show reset cause
if ( RCC_CSR & ( RCC_CSR_LPWRRSTF | RCC_CSR_WWDGRSTF | RCC_CSR_IWDGRSTF | RCC_CSR_SFTRSTF | RCC_CSR_PORRSTF | RCC_CSR_PINRSTF ) ) {
puts ( " reset cause(s): " ) ;
if ( RCC_CSR & RCC_CSR_LPWRRSTF ) {
puts ( " low-power " ) ;
}
if ( RCC_CSR & RCC_CSR_WWDGRSTF ) {
puts ( " window-watchdog " ) ;
}
if ( RCC_CSR & RCC_CSR_IWDGRSTF ) {
puts ( " independent-watchdog " ) ;
}
if ( RCC_CSR & RCC_CSR_SFTRSTF ) {
puts ( " software " ) ;
}
if ( RCC_CSR & RCC_CSR_PORRSTF ) {
puts ( " POR/PDR " ) ;
}
if ( RCC_CSR & RCC_CSR_PINRSTF ) {
puts ( " pin " ) ;
}
putc ( ' \n ' ) ;
RCC_CSR | = RCC_CSR_RMVF ; // clear reset flags
}
# endif
2017-01-30 09:44:51 +01:00
# if !(DEBUG)
// show watchdog information
2019-12-21 19:26:58 +01:00
printf ( " setup watchdog: %.2fs " , WATCHDOG_PERIOD / 1000.0 ) ;
if ( FLASH_OBR & FLASH_OBR_OPTERR ) {
2020-12-17 12:42:46 +01:00
puts ( " (option bytes not set in flash: software watchdog used, not automatically started at reset) \n " ) ;
2019-12-21 19:26:58 +01:00
} else if ( FLASH_OBR & FLASH_OBR_WDG_SW ) {
2020-02-19 21:16:07 +01:00
puts ( " (software watchdog used, not automatically started at reset) \n " ) ;
2017-01-30 09:44:51 +01:00
} else {
2020-02-19 21:16:07 +01:00
puts ( " (hardware watchdog used, automatically started at reset) \n " ) ;
2017-01-30 09:44:51 +01:00
}
# endif
2020-11-28 15:17:52 +01:00
// setup RTC
puts ( " setup RTC: " ) ;
rcc_periph_clock_enable ( RCC_RTC ) ; // enable clock for RTC peripheral
if ( ! ( RCC_BDCR & & RCC_BDCR_RTCEN ) ) { // the RTC has not been configured yet
pwr_disable_backup_domain_write_protect ( ) ; // disable backup protection so we can set the RTC clock source
rtc_unlock ( ) ; // enable writing RTC registers
# if defined(MINIF401)
rcc_osc_on ( RCC_LSE ) ; // enable LSE clock
while ( ! rcc_is_osc_ready ( RCC_LSE ) ) ; // wait until clock is ready
rtc_set_prescaler ( 256 , 128 ) ; // set clock prescaler to 32768
RCC_BDCR = ( RCC_BDCR & ~ ( RCC_BDCR_RTCSEL_MASK < < RCC_BDCR_RTCSEL_SHIFT ) ) | ( RCC_BDCR_RTCSEL_LSE < < RCC_BDCR_RTCSEL_SHIFT ) ; // select LSE as RTC clock source
# else
rcc_osc_on ( RCC_LSI ) ; // enable LSI clock
while ( ! rcc_is_osc_ready ( RCC_LSI ) ) ; // wait until clock is ready
rtc_set_prescaler ( 250 , 128 ) ; // set clock prescaler to 32000
RCC_BDCR = ( RCC_BDCR & ~ ( RCC_BDCR_RTCSEL_MASK < < RCC_BDCR_RTCSEL_SHIFT ) ) | ( RCC_BDCR_RTCSEL_LSI < < RCC_BDCR_RTCSEL_SHIFT ) ; // select LSI as RTC clock source
# endif
RCC_BDCR | = RCC_BDCR_RTCEN ; // enable RTC
rtc_lock ( ) ; // protect RTC register against writing
pwr_enable_backup_domain_write_protect ( ) ; // re-enable protection now that we configured the RTC clock
}
2020-12-12 14:45:21 +01:00
boot_time = rtc_to_seconds ( ) ; // remember the start time
2020-11-28 15:17:52 +01:00
puts ( " OK \n " ) ;
2020-11-30 14:36:33 +01:00
// setup wakeup timer for periodic checks
puts ( " setup wakeup: " ) ;
// RTC needs to be configured beforehand
pwr_disable_backup_domain_write_protect ( ) ; // disable backup protection so we can write to the RTC registers
rtc_unlock ( ) ; // enable writing RTC registers
rtc_clear_wakeup_flag ( ) ; // clear flag for fresh start
# if defined(MINIF401)
rtc_set_wakeup_time ( ( 32768 / 2 ) / WAKEUP_FREQ - 1 , RTC_CR_WUCLKSEL_RTC_DIV2 ) ; // set wakeup time based on LSE (keep highest precision, also enables the wakeup timer)
# else
rtc_set_wakeup_time ( ( 32000 / 2 ) / WAKEUP_FREQ - 1 , RTC_CR_WUCLKSEL_RTC_DIV2 ) ; // set wakeup time based on LSI (keep highest precision, also enables the wakeup timer)
# endif
rtc_enable_wakeup_timer_interrupt ( ) ; // enable interrupt
rtc_lock ( ) ; // disable writing RTC registers
// important: do not re-enable backup_domain_write_protect, since this will prevent clearing flags (but RTC registers do not need to be unlocked)
puts ( " OK \n " ) ;
2020-12-16 02:46:14 +01:00
puts ( " setup heater: " ) ;
rcc_periph_clock_enable ( GPIO_RCC ( HEATER_PIN ) ) ; // enable clock for GPIO port domain
gpio_mode_setup ( GPIO_PORT ( HEATER_PIN ) , GPIO_MODE_INPUT , GPIO_PUPD_NONE , GPIO_PIN ( HEATER_PIN ) ) ; // set GPIO to input floating
const bool heater_ok = gpio_get ( GPIO_PORT ( HEATER_PIN ) , GPIO_PIN ( HEATER_PIN ) ) ; // pin should be put high
gpio_mode_setup ( GPIO_PORT ( HEATER_PIN ) , GPIO_MODE_OUTPUT , GPIO_PUPD_NONE , GPIO_PIN ( HEATER_PIN ) ) ; // set pin as output
gpio_set_output_options ( GPIO_PORT ( HEATER_PIN ) , GPIO_OTYPE_OD , GPIO_OSPEED_2MHZ , GPIO_PIN ( HEATER_PIN ) ) ; // set pin output as open-drain
heater_off ( ) ; // set heater off
if ( heater_ok ) {
puts ( " OK \n " ) ;
} else {
puts ( " KO \n " ) ;
}
2020-12-11 00:07:19 +01:00
puts ( " setup HC-SR04 range sensor: " ) ;
sensor_sr04_setup ( ) ; // setup peripheral
2020-12-16 02:46:14 +01:00
sensor_sr04_distance = 0 ; // clear flag
sensor_sr04_trigger ( ) ; // start measurement
while ( ! sensor_sr04_distance ) ; // wait for measurement to complete
const bool sensor_sr04_ok = ( 1 ! = sensor_sr04_distance ) ; // if no echo received, the module is not present
sensor_sr04_distance = 0 ; // clear flag
if ( sensor_sr04_ok ) {
puts ( " OK \n " ) ;
} else {
puts ( " KO \n " ) ;
}
2020-12-11 00:07:19 +01:00
2020-12-11 20:59:39 +01:00
puts ( " setup DS18B20 temperature sensor: " ) ;
sensor_ds18b20_setup ( ) ; // setup peripheral
const bool sensor_ds18b20_present = ( 1 = = sensor_ds18b20_number ( ) & & sensor_ds18b20_only ( ) ) ; // ensure there is only one ds18b20 sensor on the bus (so we don't have to use its ROM code)
if ( sensor_ds18b20_present ) {
sensor_ds18b20_precision ( 0 , 12 ) ; // use highest resolution. it requires 750 ms to do the conversion, but we only check once a second
sensor_ds18b20_convert ( 0 ) ; // start the conversion
puts ( " OK \n " ) ;
} else {
puts ( " KO \n " ) ;
}
2020-12-16 02:46:14 +01:00
puts ( " setup ADC for thermistor: " ) ;
rcc_periph_clock_enable ( RCC_ADC1 ) ; // enable clock for ADC domain
adc_power_off ( ADC1 ) ; // switch off ADC while configuring it
adc_set_right_aligned ( ADC1 ) ; // ensure it is right aligned to get the actual value in the 16-bit register
adc_enable_scan_mode ( ADC1 ) ; // use scan mode do be able to go to next discontinuous subgroup of the regular sequence
adc_enable_discontinuous_mode_regular ( ADC1 , 1 ) ; // use discontinuous mode (to go through all channels of the group, one after another)
adc_set_single_conversion_mode ( ADC1 ) ; // ensure continuous mode is not used (that's not the same as discontinuous)
adc_eoc_after_each ( ADC1 ) ; // set EOC after each conversion instead of each group
adc_set_sample_time_on_all_channels ( ADC1 , ADC_SMPR_SMP_28CYC ) ; // use at least 15 cycles to be able to sample at 12-bit resolution
adc_set_regular_sequence ( ADC1 , LENGTH ( channels ) , ( uint8_t * ) channels ) ; // set channel to convert
adc_enable_temperature_sensor ( ) ; // enable internal voltage reference
adc_power_on ( ADC1 ) ; // switch on ADC
sleep_us ( 3 ) ; // wait t_stab for the ADC to stabilize
rcc_periph_clock_enable ( RCC_ADC1_IN ( THERMISTOR_CHANNEL ) ) ; // enable clock for GPIO domain for thermistor channel
gpio_mode_setup ( ADC1_IN_PORT ( THERMISTOR_CHANNEL ) , GPIO_MODE_ANALOG , GPIO_PUPD_NONE , ADC1_IN_PIN ( THERMISTOR_CHANNEL ) ) ; // set thermistor channel as analog input for the ADC
const bool thermsitor_ok = ( thermistor_voltage ( ) > 1.0 & & thermistor_voltage ( ) < 2.0 ) ; // ensure thermistor is connected
if ( thermsitor_ok ) {
puts ( " OK \n " ) ;
} else {
puts ( " KO \n " ) ;
}
2020-12-12 14:12:21 +01:00
puts ( " setup SSD1306 OLED display: " ) ;
2020-12-16 02:46:14 +01:00
const bool oled_text_ok = oled_text_setup ( ) ; // setup display
if ( oled_text_ok ) {
2020-12-12 14:12:21 +01:00
oled_text_clear ( ) ;
2020-12-17 12:41:21 +01:00
oled_text_line ( " ELEVAINITOR " , 0 ) ;
2020-12-12 14:12:21 +01:00
oled_text_update ( ) ;
puts ( " OK \n " ) ;
} else {
puts ( " KO \n " ) ;
}
2020-12-16 19:58:26 +01:00
// show status
2020-12-16 20:05:50 +01:00
const bool all_ok = heater_ok & & sensor_sr04_ok & & sensor_ds18b20_present & & thermsitor_ok & & oled_text_ok ;
2020-12-16 19:58:26 +01:00
if ( oled_text_ok ) {
if ( all_ok ) {
oled_text_line ( " press button " , 1 ) ;
oled_text_line ( " no sourdough " , 2 ) ;
oled_text_update ( ) ;
} else {
oled_text_line ( " error " , 1 ) ;
if ( ! heater_ok ) {
oled_text_line ( " heater " , 2 ) ;
} else if ( ! sensor_sr04_ok ) {
oled_text_line ( " heater MOSFET " , 2 ) ;
} else if ( ! sensor_ds18b20_present ) {
oled_text_line ( " heater thermo. " , 2 ) ;
} else if ( ! thermsitor_ok ) {
oled_text_line ( " thermistor " , 2 ) ;
} else {
oled_text_line ( " unknown " , 2 ) ;
}
oled_text_update ( ) ;
}
}
2020-12-16 19:36:15 +01:00
puts ( " setup buzzer: " ) ;
rcc_periph_clock_enable ( GPIO_RCC ( BUZZER_PIN ) ) ; // enable clock for GPIO port domain
gpio_clear ( GPIO_PORT ( BUZZER_PIN ) , GPIO_PIN ( BUZZER_PIN ) ) ; // disable buzzer
gpio_mode_setup ( GPIO_PORT ( BUZZER_PIN ) , GPIO_MODE_OUTPUT , GPIO_PUPD_NONE , GPIO_PIN ( BUZZER_PIN ) ) ; // set pin as output
gpio_set_output_options ( GPIO_PORT ( BUZZER_PIN ) , GPIO_OTYPE_PP , GPIO_OSPEED_2MHZ , GPIO_PIN ( BUZZER_PIN ) ) ; // set pin output as push-pull
beep ( 1 ) ; // buzz to show it is working
puts ( " OK \n " ) ;
2018-04-06 17:37:17 +02:00
// setup terminal
terminal_prefix = " " ; // set default prefix
terminal_process = & process_command ; // set central function to process commands
terminal_setup ( ) ; // start terminal
2016-10-23 17:42:27 +02:00
2018-04-06 17:37:17 +02:00
// start main loop
2016-02-18 10:39:08 +01:00
bool action = false ; // if an action has been performed don't go to sleep
button_flag = false ; // reset button flag
2020-11-27 17:07:39 +01:00
led_on ( ) ; // switch LED to indicate booting completed
2020-12-17 12:41:21 +01:00
uint32_t peak_time = 0 ; // when the sourdough starter has reached its peak
2016-01-29 00:24:49 +01:00
while ( true ) { // infinite loop
2016-10-23 17:42:27 +02:00
iwdg_reset ( ) ; // kick the dog
2019-06-18 17:45:53 +02:00
if ( user_input_available ) { // user input is available
2016-02-18 10:39:08 +01:00
action = true ; // action has been performed
2016-01-29 00:24:49 +01:00
led_toggle ( ) ; // toggle LED
2018-04-06 17:37:17 +02:00
char c = user_input_get ( ) ; // store receive character
terminal_send ( c ) ; // send received character to terminal
2016-08-14 21:02:38 +02:00
}
2019-06-18 17:45:53 +02:00
if ( button_flag ) { // user pressed button
2016-08-14 21:02:38 +02:00
action = true ; // action has been performed
2020-02-19 21:16:07 +01:00
puts ( " button pressed \n " ) ;
2016-02-18 10:39:08 +01:00
led_toggle ( ) ; // toggle LED
2020-12-16 19:58:26 +01:00
if ( ! all_ok ) {
puts ( " can only start when all peripherals are ok \n " ) ;
puts ( " restart to check again \n " ) ;
2020-12-17 12:41:21 +01:00
} else if ( 0 = = container_height ) { // time to get container height
2020-12-16 02:46:14 +01:00
puts ( " container height: " ) ;
sleep_ms ( 2000 ) ; // wait for used to remove his finger from the lid
sensor_sr04_distance = 0 ; // clear measurement
sensor_sr04_trigger ( ) ; // start measurement
while ( ! sensor_sr04_distance ) ; // wait for distance measurement
if ( 1 = = sensor_sr04_distance ) {
puts ( " range sensor not available \n " ) ;
break ;
} else if ( sensor_sr04_distance > 255 ) {
puts ( " probably not on container \n " ) ;
break ;
} else { // valid height
container_height = sensor_sr04_distance ; // remember height
printf ( " %u mm \n " , container_height ) ; // display measurement
oled_text_line ( " with starter " , 2 ) ; // display next instruction
oled_text_update ( ) ; // show instruction
2020-12-16 19:36:15 +01:00
beep ( 1 ) ; // beep to indicate action completed
2020-12-16 02:46:14 +01:00
}
sensor_sr04_distance = 0 ; // clear measurement
2020-12-17 12:41:21 +01:00
} else if ( 0 = = levain_time | | 0 = = starter_height ) { // time to start monitoring
2020-12-16 02:46:14 +01:00
puts ( " starter height: " ) ;
sleep_ms ( 2000 ) ; // wait for used to remove his finger from the lid
sensor_sr04_distance = 0 ; // clear measurement
sensor_sr04_trigger ( ) ; // start measurement
while ( ! sensor_sr04_distance ) ; // wait for distance measurement
if ( 1 = = sensor_sr04_distance ) {
puts ( " range sensor not available \n " ) ;
break ;
} else if ( sensor_sr04_distance > = container_height ) {
puts ( " probably no starter \n " ) ;
break ;
} else { // valid height
starter_height = sensor_sr04_distance ; // remember height
max_height = starter_height ; // initialize the maximum height
2020-12-17 12:41:21 +01:00
levain_time = rtc_to_seconds ( ) ; // remember when we start monitoring
max_time = levain_time ; // initialize the maximum height time
2020-12-16 19:36:15 +01:00
printf ( " %u mm \n " , starter_height ) ; // display measurement
beep ( 1 ) ; // beep to indicate action completed
2020-12-16 02:46:14 +01:00
}
sensor_sr04_distance = 0 ; // clear measurement
}
2020-02-19 21:16:37 +01:00
sleep_ms ( 100 ) ; // wait a bit to remove noise and double trigger
2016-08-14 21:02:38 +02:00
button_flag = false ; // reset flag
2016-02-18 10:39:08 +01:00
}
2020-11-30 14:36:33 +01:00
if ( wakeup_flag ) { // time to do periodic checks
wakeup_flag = false ; // clear flag
}
if ( second_flag ) { // one second passed
second_flag = false ; // clear flag
2020-12-11 00:07:19 +01:00
action = true ; // action will be performed
2020-11-30 14:36:33 +01:00
led_toggle ( ) ; // toggle LED to indicate if main function is stuck
2020-12-17 12:41:21 +01:00
// get heater temperature
2020-12-16 02:46:14 +01:00
if ( sensor_ds18b20_present ) {
heater_temp = sensor_ds18b20_temperature ( 0 ) ; // get temperature
if ( 85.0 ! = heater_temp ) { // the conversion has not been completed
sensor_ds18b20_convert ( 0 ) ; // start next conversion
}
}
2020-12-17 12:41:21 +01:00
// get sourdough starter temperature
2020-12-16 02:46:14 +01:00
if ( thermsitor_ok ) {
2020-12-17 12:41:21 +01:00
levain_temp = thermistor_temperature ( ) ; // get temperature
2020-12-16 02:46:14 +01:00
}
2020-12-12 14:45:36 +01:00
// update uptime
2020-12-17 12:41:21 +01:00
if ( levain_time ) {
const uint32_t uptime = rtc_to_seconds ( ) - levain_time ; // get time from internal RTC
2020-12-16 02:46:14 +01:00
char strtime [ 12 ] ; // to store time representation
snprintf ( strtime , sizeof ( strtime ) , " %u.%02u:%02u:%02u " , uptime / ( 24 * 60 * 60 ) , ( uptime / ( 60 * 60 ) ) % 24 , ( uptime / 60 ) % 60 , uptime % 60 ) ; // get uptime representation
oled_text_line ( strtime , 1 ) ; // add text to display buffer
}
2020-12-17 12:41:21 +01:00
// get height
2020-12-16 02:46:14 +01:00
if ( sensor_sr04_ok ) { // sensor working
sensor_sr04_trigger ( ) ; // start measurement
}
2020-12-17 12:41:21 +01:00
// heat up sourdough starter
if ( levain_time & & ! isnan ( levain_temp ) & & heater_ok & & ! isnan ( heater_temp ) ) {
if ( levain_temp < levain_target - 0.5 & & 0 = = peak_time ) { // temperature not reached
2020-12-16 02:46:14 +01:00
heater_on ( ) ; // keep heating
2020-12-17 12:41:21 +01:00
} else if ( levain_temp > levain_target + 0.5 ) { // temperature reached
2020-12-16 02:46:14 +01:00
heater_off ( ) ; // stop heating
}
}
2020-12-17 12:41:21 +01:00
// turn off heater for safety
if ( ! isnan ( heater_temp ) & & heater_temp > HEATER_LIMIT ) {
2020-12-16 02:46:14 +01:00
heater_off ( ) ; // stop heating
}
2020-12-17 12:41:21 +01:00
// update how much the starter raised
2020-12-16 19:36:15 +01:00
if ( current_height & & container_height & & starter_height & & max_height & & current_height < = starter_height ) {
const float rising = ( container_height - current_height ) * 1.0 / ( container_height - starter_height ) ; // calculate ratio
2020-12-16 02:46:14 +01:00
char text [ 13 ] ; // to store text representation
2020-12-17 12:41:21 +01:00
snprintf ( text , sizeof ( text ) , " %.02fx %.01fC " , rising , levain_temp ) ; // display ratio and temperature
2020-12-16 02:46:14 +01:00
oled_text_line ( text , 2 ) ; // add text to display buffer
2020-12-17 12:41:21 +01:00
if ( current_height < max_height & & ( current_height + 5U ) > max_height ) { // a new maximum height has been reached (and is credible)
2020-12-16 19:36:15 +01:00
max_height = current_height ; // save new maximum height
2020-12-16 02:46:14 +01:00
max_time = rtc_to_seconds ( ) ; // remember the time
}
2020-12-17 12:41:21 +01:00
if ( current_height > max_height + 5 ) { // the sourdough starter has been falling again
2020-12-16 02:46:14 +01:00
if ( max_time > peak_time ) {
2020-12-16 19:36:15 +01:00
heater_off ( ) ; // stop heating
2020-12-17 12:41:21 +01:00
peak_time = max_time ; // remember the peak sourdough starter time
2020-12-16 02:46:14 +01:00
const float peak_ratio = ( container_height - max_height ) * 1.0 / ( container_height - starter_height ) ; // calculate ratio
2020-12-17 12:41:21 +01:00
const uint16_t peak_period = peak_time - levain_time ; // calculate peak time
2020-12-16 02:46:14 +01:00
snprintf ( text , sizeof ( text ) , " %.02fx %um " , peak_ratio , peak_period / 60 ) ; // display peak ratio and time
oled_text_line ( text , 3 ) ; // add text to display buffer
2020-12-16 19:36:15 +01:00
beep ( 2 ) ; // beep to indicate the peak has been reached
2020-12-16 02:46:14 +01:00
}
}
}
2020-12-17 12:41:21 +01:00
// display new data (even if nothing new)
2020-12-16 19:36:15 +01:00
if ( oled_text_ok ) {
2020-12-17 12:41:21 +01:00
oled_text_update ( ) ; // display buffered data
2020-12-16 19:36:15 +01:00
}
}
if ( sensor_sr04_distance ) { // distance measurement is available
//printf("height: %u mm\n", sensor_sr04_distance);
current_height = sensor_sr04_distance ; // save height
2020-12-11 00:07:19 +01:00
sensor_sr04_distance = 0 ; // clear flag
2016-08-14 21:02:38 +02:00
}
if ( action ) { // go to sleep if nothing had to be done, else recheck for activity
2016-02-18 10:39:08 +01:00
action = false ;
} else {
__WFI ( ) ; // go to sleep
}
2016-10-23 17:42:27 +02:00
} // main loop
2016-01-17 14:54:54 +01:00
}
2020-11-30 14:36:33 +01:00
/** interrupt service routine when the wakeup timer triggered */
void rtc_wkup_isr ( void )
{
static uint16_t tick = WAKEUP_FREQ ; // how many wakeup have occurred
exti_reset_request ( EXTI22 ) ; // clear EXTI flag used by wakeup
rtc_clear_wakeup_flag ( ) ; // clear flag
wakeup_flag = true ; // notify main loop
tick - - ; // count the number of ticks down (do it in the ISR to no miss any tick)
if ( 0 = = tick ) { // count down completed
second_flag = true ; // notify main loop a second has passed
tick = WAKEUP_FREQ ; // restart count down
}
}