2022-05-24 14:45:56 +02:00
/** firmware to control the cool clock
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
2022-05-25 17:38:34 +02:00
* @ date 2016 - 2022
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
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
2022-05-24 14:45:56 +02:00
# include <libopencm3/stm32/timer.h> // timer library
# include <libopencm3/stm32/dma.h> // DMA library
2021-09-24 16:00:55 +02:00
# include <libopencm3/usb/dwc/otg_fs.h> // USB definitions
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
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
2022-05-25 19:57:44 +02:00
# include "font.h" // to draw text
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 ;
2022-05-24 14:45:56 +02:00
// DRV8825 stepper motor driver connections
# define DRV8825_ENABLE_PIN PB13 /**< pin to enable output (active low) */
# define DRV8825_RESET_PIN PB14 /**< pin to reset and put to sleep driver (active low) */
# define DRV8825_DIRECTION_PIN PB15 /**< pin to set direction (low = clockwise) */
# define DRV8825_STEP_PIN PA15 /**< pin to move one step forward */
# define DRV8825_STEP_TIMER 2 /**< timer connected to pin */
# define DRV8825_STEP_CHANNEL 1 /**< timer channel connected to pin */
# define DRV8825_STEP_OC TIM_OC1 /**< timer output compare connected to pin */
# define DRV8825_STEP_AF GPIO_AF1 /**< alternate function for timer channel */
# define DRV8825_FAULT_PIN PB12 /**< pin pulled low on error (such as over-current) */
static volatile uint32_t drv8825_steps = 0 ; /**< incremented with each step */
static int8_t drv8825_direction = 0 ; /**< direction of the steps (1 = clockwise, -1 = counter-clockwise) */
/** maximum speed (in steps/s) before the motor stalls (found empirically)
* @ note found empirically 300 @ 9 V / 180 mA , 420 @ 12 V / 150 mA
*/
# define DRV8825_SPEED_LIMIT 420U
// dials position info
# define DIAL_SWITCH_PIN PB3 /**< pin connected to reed switch, pulled low when the hour dial is nearby */
# define DIAL_CYCLE_STEPS 11904U /**< number of steps for the hour dial to make a round */
# define DIAL_MIDNIGHT_STEPS 6557U /**< number of steps after dial detection for dials to show midnight */
static volatile uint32_t dial_steps = 0 ; /**< set to drv8825_steps when dial is nearby */
// RGB matrix pins
# define RGBMATRIX_OE_PIN PB10 /**< pin to enable output (active low) */
2022-05-25 17:38:34 +02:00
# define RGBMATRIX_A_PIN PB0 /**< pin to select line */
# define RGBMATRIX_B_PIN PB1 /**< pin to select line */
# define RGBMATRIX_C_PIN PB2 /**< pin to select line */
# define RGBMATRIX_D_PIN PB3 /**< pin to select line */
# define RGBMATRIX_CLK_PIN PA0 /**< pin to generate clock for serial data */
# define RGBMATRIX_LAT_PIN PA1 /**< pin to latch data on rising edge */
# define RGBMATRIX_R1_PIN PA2 /**< pin to enable red color on top half */
# define RGBMATRIX_G1_PIN PA3 /**< pin to enable green color on top half */
# define RGBMATRIX_B1_PIN PA4 /**< pin to enable blue color on top half */
# define RGBMATRIX_R2_PIN PA5 /**< pin to enable red color on bottom half */
# define RGBMATRIX_G2_PIN PA6 /**< pin to enable green color on bottom half */
# define RGBMATRIX_B2_PIN PA7 /**< pin to enable blue color on bottom half */
2022-05-24 14:45:56 +02:00
# define RGBMATRIX_DMA DMA2 /**< DMA used to send data to the RGB matrix (only DMA2 can be used for memory-to-memory transfer) */
# define RGBMATRIX_RCC_DMA RCC_DMA2 /**< RCC for DMA used for the RGB matrix */
# define RGBMATRIX_STREAM DMA_STREAM1 /**< stream used to send data to the RGB matrix (any stream can be used for memory-to-memory transfer) */
# define RGBMATRIX_CHANNEL DMA_SxCR_CHSEL_0 /**< channel used to send data to the RGB matrix (any channel can be used for memory-to-memory transfer) */
# define RGBMATRIX_IRQ NVIC_DMA2_STREAM1_IRQ /**< IRQ for when a line transfer is complete */
# define RGBMATRIX_ISR dma2_stream1_isr /**< ISR for when a line transfer is complete */
2022-05-25 17:38:34 +02:00
# define RGBMATRIX_HEIGHT 32 /**< number of rows in the RGB matrix */
2022-05-24 14:45:56 +02:00
# define RGBMATRIX_WIDTH 64 /**< number of columns in the RGB matrix */
2022-05-25 17:38:34 +02:00
static uint8_t rgbmatrix_data [ RGBMATRIX_HEIGHT / 2 ] [ RGBMATRIX_WIDTH * 2 ] ; /**< data to be sent to RGB matrix (one byte includes upper and lower half values, each byte has 2 clock edges) */
2022-05-25 19:37:17 +02:00
# define RGBMATRIX_TIMER 3 /**< timer to update lines */
2022-05-24 14:45:56 +02:00
2022-06-03 10:10:16 +02:00
// RGBW LED strips
# define STRIP_TIMER 4 /**< timer used for the PWM */
# define STRIP_R_PIN PB9 /**< pin used to drive gate for red channel */
# define STRIP_R_CH 4 /**< channel used for the red channel */
# define STRIP_G_PIN PB8 /**< pin used to drive gate for green channel */
# define STRIP_G_CH 3 /**< channel used for the green channel */
# define STRIP_B_PIN PB7 /**< pin used to drive gate for blue channel */
# define STRIP_B_CH 2 /**< channel used for the blue channel */
# define STRIP_W_PIN PB6 /**< pin used to drive gate for white channel */
# define STRIP_W_CH 1 /**< channel used for the white channel */
# define STRIP_AF GPIO_AF2 /**< alternate function for pin to be used as timer channel */
2022-05-24 14:45:56 +02:00
/** set motor speed and direction
* @ param [ in ] speed speed ( in Hz ) and direction ( sign )
*/
static void drv8825_speed ( int16_t speed )
{
if ( 0 = = speed ) {
timer_disable_counter ( TIM ( DRV8825_STEP_TIMER ) ) ; // stop PWM output
gpio_set ( GPIO_PORT ( DRV8825_ENABLE_PIN ) , GPIO_PIN ( DRV8825_ENABLE_PIN ) ) ; // disable motor
drv8825_direction = 0 ; // remember we stopped
} else {
if ( speed > 0 ) {
gpio_clear ( GPIO_PORT ( DRV8825_DIRECTION_PIN ) , GPIO_PIN ( DRV8825_DIRECTION_PIN ) ) ; // set clockwise
drv8825_direction = 1 ; // remember we go clockwise
} else {
gpio_set ( GPIO_PORT ( DRV8825_DIRECTION_PIN ) , GPIO_PIN ( DRV8825_DIRECTION_PIN ) ) ; // set counter-clockwise
drv8825_direction = - 1 ; // remember we go counter-clockwise
speed = - speed ; // get positive speed
}
if ( speed > ( int16_t ) DRV8825_SPEED_LIMIT ) { // enforce upper limit
speed = DRV8825_SPEED_LIMIT ;
}
timer_set_prescaler ( TIM ( DRV8825_STEP_TIMER ) , rcc_ahb_frequency / ( UINT16_MAX * speed ) - 1 ) ; // set the clock frequency
gpio_clear ( GPIO_PORT ( DRV8825_ENABLE_PIN ) , GPIO_PIN ( DRV8825_ENABLE_PIN ) ) ; // enable motor
timer_enable_counter ( TIM ( DRV8825_STEP_TIMER ) ) ; // start PWM output
}
}
/** switch off all LEDs on the RGB matrix */
static void rgbmatrix_clear ( void )
{
for ( uint8_t i = 0 ; i < LENGTH ( rgbmatrix_data ) ; i + + ) { // for each line
for ( uint8_t j = 0 ; j < LENGTH ( rgbmatrix_data [ 0 ] ) ; j + = 2 ) { // for each clock cycle
2022-05-25 17:38:34 +02:00
rgbmatrix_data [ i ] [ j + 0 ] = 0 ; // create clock falling edge
rgbmatrix_data [ i ] [ j + 1 ] = 1 ; // create clock rising edge
2022-05-24 14:45:56 +02:00
}
rgbmatrix_data [ i ] [ LENGTH ( rgbmatrix_data [ 0 ] ) - 1 ] | = ( 1 < < 1 ) ; // latch data (next line will remove the latch)
}
}
/** set color of the LED on the RGB matrix
* @ param [ in ] x horizontal position ( 0 = left )
* @ param [ in ] y vertical position ( 0 = top )
* @ param [ in ] r if the red LED should be on
* @ param [ in ] g if the green LED should be on
* @ param [ in ] b if the blue LED should be on
*/
2022-05-26 10:38:23 +02:00
static void rgbmatrix_set ( int16_t x , int16_t y , bool r , bool g , bool b )
2022-05-24 14:45:56 +02:00
{
2022-05-26 10:38:23 +02:00
if ( x < 0 | | x > = RGBMATRIX_WIDTH ) {
2022-05-24 14:45:56 +02:00
return ;
}
2022-05-26 10:38:23 +02:00
if ( y < 0 | | y > = RGBMATRIX_HEIGHT ) {
2022-05-24 14:45:56 +02:00
return ;
}
2022-05-25 17:38:34 +02:00
const uint8_t row = y % ( RGBMATRIX_HEIGHT / 2 ) ; // get the actual line/row
const uint8_t col = x * 2 ; // get the actual column
uint8_t data = 0 ; // there we will set the color bits
2022-05-24 14:45:56 +02:00
if ( y < ( RGBMATRIX_HEIGHT / 2 ) ) {
2022-05-25 17:38:34 +02:00
data = rgbmatrix_data [ row ] [ col ] & 0xe0 ; // keep lower line colors
2022-05-24 14:45:56 +02:00
if ( r ) {
data | = ( 1 < < 2 ) ;
}
if ( g ) {
data | = ( 1 < < 3 ) ;
}
if ( b ) {
data | = ( 1 < < 4 ) ;
}
} else {
2022-05-25 17:38:34 +02:00
data = rgbmatrix_data [ row ] [ col ] & 0x1c ; // keep upper line colors
2022-05-24 14:45:56 +02:00
if ( r ) {
data | = ( 1 < < 5 ) ;
}
if ( g ) {
data | = ( 1 < < 6 ) ;
}
if ( b ) {
data | = ( 1 < < 7 ) ;
}
}
2022-05-25 17:38:34 +02:00
// set data on low edge
rgbmatrix_data [ row ] [ col + 0 ] & = 0x3 ; // clear color data (don't touch clock and latch data)
rgbmatrix_data [ row ] [ col + 0 ] | = data ; // set the LED data
// set data on high edge
rgbmatrix_data [ row ] [ col + 1 ] & = 0x3 ; // clear color data (don't touch clock and latch data)
rgbmatrix_data [ row ] [ col + 1 ] | = data ; // set the LED data on clock high edge
2022-05-24 14:45:56 +02:00
}
2022-05-25 19:57:44 +02:00
/** draw character on RGB matrix
* @ param [ in ] x horizontal position ( 0 = left )
* @ param [ in ] y vertical position ( 0 = top )
* @ param [ in ] c character to draw
* @ param [ in ] font font to use
* @ param [ in ] r if the character should be drawn in red
* @ param [ in ] g if the character should be drawn in green
* @ param [ in ] b if the character should be drawn in blue
*/
2022-05-26 10:38:23 +02:00
static void rgbmatrix_putc ( int16_t x , int16_t y , char c , enum font_name font , bool red , bool green , bool blue )
2022-05-25 19:57:44 +02:00
{
// sanity checks
2022-05-26 10:38:23 +02:00
if ( font > = FONT_MAX ) {
2022-05-25 19:57:44 +02:00
return ;
}
2022-05-26 10:38:23 +02:00
if ( c < ' ' | | c > ' ~ ' ) {
2022-05-25 19:57:44 +02:00
return ;
}
2022-05-26 10:38:23 +02:00
if ( x + fonts [ font ] . width < 0 | | x > = RGBMATRIX_WIDTH ) {
2022-05-25 19:57:44 +02:00
return ;
}
2022-05-26 10:38:23 +02:00
if ( y + fonts [ font ] . height < 0 | | y > = RGBMATRIX_HEIGHT ) {
2022-05-25 19:57:44 +02:00
return ;
}
// draw character on buffer
for ( uint8_t col = 0 ; col < fonts [ font ] . width ; col + + ) {
const uint16_t column = fonts [ font ] . glyphs [ ( c - ' ' ) * fonts [ font ] . width + col ] ;
for ( uint8_t row = 0 ; row < fonts [ font ] . height ; row + + ) {
const bool dot = ( column > > ( fonts [ font ] . height - 1 - row ) ) & 0x01 ;
if ( dot ) {
rgbmatrix_set ( x + col , y + row , red , green , blue ) ;
} else {
rgbmatrix_set ( x + col , y + row , false , false , false ) ;
}
}
}
}
2022-05-25 20:14:50 +02:00
/** draw text on RGB matrix
* @ param [ in ] x horizontal position ( 0 = left )
* @ param [ in ] y vertical position ( 0 = top )
* @ param [ in ] str text to draw
* @ param [ in ] font font to use
* @ param [ in ] r if the character should be drawn in red
* @ param [ in ] g if the character should be drawn in green
* @ param [ in ] b if the character should be drawn in blue
*/
2022-05-26 10:38:23 +02:00
static void rgbmatrix_puts ( int16_t x , int16_t y , const char * str , enum font_name font , bool red , bool green , bool blue )
2022-05-25 20:14:50 +02:00
{
// sanity checks
if ( NULL = = str ) {
return ;
}
if ( font > = FONT_MAX ) {
return ;
}
2022-05-26 10:38:23 +02:00
if ( y + fonts [ font ] . height < 0 | | y > = RGBMATRIX_HEIGHT ) {
2022-05-25 20:14:50 +02:00
return ;
}
const uint8_t len = strlen ( str ) ;
for ( uint8_t i = 0 ; i < len ; i + + ) {
if ( x > = RGBMATRIX_WIDTH ) {
return ;
}
2022-05-26 10:38:23 +02:00
if ( x + fonts [ font ] . width > = 0 ) {
rgbmatrix_putc ( x , y , str [ i ] , font , red , green , blue ) ;
}
2022-05-25 20:14:50 +02:00
x + = fonts [ font ] . width + 1 ;
}
}
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
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
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
}
2021-03-24 17:33:33 +01:00
// only print when debug is enabled
# if DEBUG
# define puts_debug(x) puts(x)
# else
# define puts_debug(x) {}
# endif
2020-11-27 17:07:39 +01:00
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
if ( month > 0 ) { // month has been initialized, but starts with 1
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
if ( day > 0 ) { // day has been initialized, but starts with 1
day - - ; // fix for calculation
}
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
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
}
// 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
2022-05-24 14:45:56 +02:00
/** set motor speed and direction
* @ param [ in ] argument speed ( in Hz ) and direction ( sign )
*/
static void command_speed ( void * argument )
{
if ( NULL = = argument ) {
puts ( " speed argument required " ) ;
return ;
}
int32_t speed = * ( int32_t * ) argument ;
if ( 0 = = speed ) {
drv8825_speed ( 0 ) ; // stop motor
puts ( " motor stopped \n " ) ;
} else {
drv8825_speed ( speed ) ; // set speed
printf ( " motor speed set to %d Hz \n " , speed ) ;
}
}
/** advance motor by n steps
* @ param [ in ] argument number of steps
*/
static void command_advance ( void * argument )
{
if ( NULL = = argument ) {
puts ( " number of steps required " ) ;
return ;
}
int32_t steps = * ( int32_t * ) argument ;
printf ( " advancing %d steps \n " , steps ) ;
drv8825_speed ( 0 ) ; // stop motor to get precise count
uint32_t start = drv8825_steps ; // get current position
// WARNING does not work
if ( steps > 0 ) {
drv8825_speed ( 100 ) ; // advance slowly
if ( start + steps < DIAL_CYCLE_STEPS ) {
while ( drv8825_steps < start + steps ) ; // wait to reach point
} else {
while ( drv8825_steps > start ) ; // wait to make round
while ( drv8825_steps < ( start + steps ) % DIAL_CYCLE_STEPS ) ; // wait to reach point
}
} else {
drv8825_speed ( - 100 ) ; // reverse slowly
if ( ( int32_t ) start > - steps ) {
while ( drv8825_steps > start + steps ) ; // wait to reach point
} else {
while ( drv8825_steps < start ) ; // wait to make round
while ( drv8825_steps > ( start + steps ) % DIAL_CYCLE_STEPS ) ; // wait to reach point
}
}
drv8825_speed ( 0 ) ; // stop motor
}
/** test RGB matrix
* @ param [ in ] argument no argument required
*/
static void command_matrix ( void * argument )
{
( void ) argument ; // we won't use the argument
puts ( " test pattern sent to LED matrix \n " ) ;
rgbmatrix_set ( 0 , 0 , true , false , false ) ;
rgbmatrix_set ( 1 , 0 , false , true , false ) ;
rgbmatrix_set ( 2 , 0 , false , false , true ) ;
rgbmatrix_set ( 0 , 1 , true , false , false ) ;
rgbmatrix_set ( 1 , 2 , false , true , false ) ;
rgbmatrix_set ( 2 , 3 , false , false , true ) ;
}
2022-06-03 10:10:16 +02:00
/** set intensity of LED strip
* @ param [ in ] red red intensity ( 0 - 0xffff ) , - 1 to leave
* @ param [ in ] green green intensity ( 0 - 0xffff ) , - 1 to leave
* @ param [ in ] blue blue intensity ( 0 - 0xffff ) , - 1 to leave
* @ param [ in ] white white intensity ( 0 - 0xffff ) , - 1 to leave
*/
static void strip_rgbw ( int32_t red , int32_t green , int32_t blue , int32_t white )
{
if ( red > = 0 & & red < = 0xffff ) {
timer_set_oc_value ( TIM ( STRIP_TIMER ) , TIM_OC ( STRIP_R_CH ) , red ) ;
}
if ( green > = 0 & & green < = 0xffff ) {
timer_set_oc_value ( TIM ( STRIP_TIMER ) , TIM_OC ( STRIP_G_CH ) , green ) ;
}
if ( blue > = 0 & & blue < = 0xffff ) {
timer_set_oc_value ( TIM ( STRIP_TIMER ) , TIM_OC ( STRIP_B_CH ) , blue ) ;
}
if ( white > = 0 & & white < = 0xffff ) {
timer_set_oc_value ( TIM ( STRIP_TIMER ) , TIM_OC ( STRIP_W_CH ) , white ) ;
}
}
static void command_strip_red ( void * argument )
{
if ( ! argument ) {
printf ( " argument required \n " ) ;
return ;
}
uint16_t set = * ( uint32_t * ) argument ; // get provide value
if ( set > 100 ) {
set = 100 ; // enforce maximum
}
strip_rgbw ( 0xffff * set / 100 , - 1 , - 1 , - 1 ) ; // set light intensity
printf ( " red channel set to %u%% \n " , set ) ;
}
static void command_strip_green ( void * argument )
{
if ( ! argument ) {
printf ( " argument required \n " ) ;
return ;
}
uint16_t set = * ( uint32_t * ) argument ; // get provide value
if ( set > 100 ) {
set = 100 ; // enforce maximum
}
strip_rgbw ( - 1 , 0xffff * set / 100 , - 1 , - 1 ) ; // set light intensity
printf ( " green channel set to %u%% \n " , set ) ;
}
static void command_strip_blue ( void * argument )
{
if ( ! argument ) {
printf ( " argument required \n " ) ;
return ;
}
uint16_t set = * ( uint32_t * ) argument ; // get provide value
if ( set > 100 ) {
set = 100 ; // enforce maximum
}
strip_rgbw ( - 1 , - 1 , 0xffff * set / 100 , - 1 ) ; // set light intensity
printf ( " blue channel set to %u%% \n " , set ) ;
}
static void command_strip_white ( void * argument )
{
if ( ! argument ) {
printf ( " argument required \n " ) ;
return ;
}
uint16_t set = * ( uint32_t * ) argument ; // get provide value
if ( set > 100 ) {
set = 100 ; // enforce maximum
}
strip_rgbw ( - 1 , - 1 , - 1 , 0xffff * set / 100 ) ; // set light intensity
printf ( " white channel set to %u%% \n " , set ) ;
}
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
{
2022-05-24 14:45:56 +02:00
. shortcut = ' R ' ,
2018-04-06 17:37:17 +02:00
. 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
{
2022-05-24 14:45:56 +02: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
{
2022-05-24 14:45:56 +02: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 ,
} ,
2022-05-24 14:45:56 +02:00
{
. shortcut = ' s ' ,
. name = " speed " ,
. command_description = " set motor step frequency and direction " ,
. argument = MENU_ARGUMENT_SIGNED ,
. argument_description = " Hz " ,
. command_handler = & command_speed ,
} ,
{
. shortcut = ' a ' ,
. name = " advance " ,
. command_description = " advance dial (either direction) " ,
. argument = MENU_ARGUMENT_SIGNED ,
. argument_description = " steps " ,
. command_handler = & command_advance ,
} ,
{
. shortcut = ' m ' ,
. name = " matrix " ,
. command_description = " test RGB matrix " ,
. argument = MENU_ARGUMENT_NONE ,
. argument_description = NULL ,
. command_handler = & command_matrix ,
} ,
2022-06-03 10:10:16 +02:00
{
. shortcut = ' r ' ,
. name = " strip_red " ,
. command_description = " set LED strip red intensity " ,
. argument = MENU_ARGUMENT_UNSIGNED ,
. argument_description = " % " ,
. command_handler = & command_strip_red ,
} ,
{
. shortcut = ' g ' ,
. name = " strip_green " ,
. command_description = " set LED strip green intensity " ,
. argument = MENU_ARGUMENT_UNSIGNED ,
. argument_description = " % " ,
. command_handler = & command_strip_green ,
} ,
{
. shortcut = ' b ' ,
. name = " strip_blue " ,
. command_description = " set LED strip blue intensity " ,
. argument = MENU_ARGUMENT_UNSIGNED ,
. argument_description = " % " ,
. command_handler = & command_strip_blue ,
} ,
{
. shortcut = ' w ' ,
. name = " strip_white " ,
. command_description = " set LED strip white intensity " ,
. argument = MENU_ARGUMENT_UNSIGNED ,
. argument_description = " % " ,
. command_handler = & command_strip_white ,
} ,
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
2022-05-25 17:38:34 +02:00
if ( 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
}
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
2017-04-19 16:26:40 +02:00
usb_cdcacm_setup ( ) ; // setup USB CDC ACM (for printing)
2021-09-24 16:00:55 +02:00
OTG_FS_GCCFG | = OTG_GCCFG_NOVBUSSENS | OTG_GCCFG_PWRDWN ; // disable VBUS sensing
OTG_FS_GCCFG & = ~ ( OTG_GCCFG_VBUSBSEN | OTG_GCCFG_VBUSASEN ) ; // force USB device mode
2022-05-24 14:45:56 +02:00
puts ( " \n welcome to the World Clock controller \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 ) ) {
2021-05-14 14:50:02 +02:00
puts_debug ( " reset cause(s): " ) ;
2019-12-21 19:26:27 +01:00
if ( RCC_CSR & RCC_CSR_LPWRRSTF ) {
2021-05-14 14:50:02 +02:00
puts_debug ( " low-power " ) ;
2019-12-21 19:26:27 +01:00
}
if ( RCC_CSR & RCC_CSR_WWDGRSTF ) {
2021-05-14 14:50:02 +02:00
puts_debug ( " window-watchdog " ) ;
2019-12-21 19:26:27 +01:00
}
if ( RCC_CSR & RCC_CSR_IWDGRSTF ) {
2021-05-14 14:50:02 +02:00
puts_debug ( " independent-watchdog " ) ;
2019-12-21 19:26:27 +01:00
}
if ( RCC_CSR & RCC_CSR_SFTRSTF ) {
2021-05-14 14:50:02 +02:00
puts_debug ( " software " ) ;
2019-12-21 19:26:27 +01:00
}
if ( RCC_CSR & RCC_CSR_PORRSTF ) {
2021-05-14 14:50:02 +02:00
puts_debug ( " POR/PDR " ) ;
2019-12-21 19:26:27 +01:00
}
if ( RCC_CSR & RCC_CSR_PINRSTF ) {
2021-05-14 14:50:02 +02:00
puts_debug ( " pin " ) ;
2019-12-21 19:26:27 +01:00
}
2021-05-14 14:50:02 +02:00
puts_debug ( " \n " ) ;
2019-12-21 19:26:27 +01:00
RCC_CSR | = RCC_CSR_RMVF ; // clear reset flags
}
# endif
2017-01-30 09:44:51 +01:00
2020-11-28 15:17:52 +01:00
// setup RTC
2021-03-24 17:33:33 +01:00
puts_debug ( " setup RTC: " ) ;
2020-11-28 15:17:52 +01:00
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
2022-05-24 14:45:56 +02:00
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
2020-11-28 15:17:52 +01:00
# 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
2021-03-24 17:33:33 +01:00
puts_debug ( " OK \n " ) ;
2020-11-28 15:17:52 +01:00
2020-11-30 14:36:33 +01:00
// setup wakeup timer for periodic checks
2021-03-24 17:33:33 +01:00
puts_debug ( " setup wakeup: " ) ;
2020-11-30 14:36:33 +01:00
// 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)
2021-03-24 17:33:33 +01:00
puts_debug ( " OK \n " ) ;
2020-11-30 14:36:33 +01:00
2022-05-24 14:45:56 +02:00
puts_debug ( " setup stepper motor: " ) ;
// motor enable pin
rcc_periph_clock_enable ( GPIO_RCC ( DRV8825_ENABLE_PIN ) ) ; // enable clock for GPIO port peripheral
gpio_set ( GPIO_PORT ( DRV8825_ENABLE_PIN ) , GPIO_PIN ( DRV8825_ENABLE_PIN ) ) ; // disable motor
gpio_mode_setup ( GPIO_PORT ( DRV8825_ENABLE_PIN ) , GPIO_MODE_OUTPUT , GPIO_PUPD_NONE , GPIO_PIN ( DRV8825_ENABLE_PIN ) ) ; // set pin as output
gpio_set_output_options ( GPIO_PORT ( DRV8825_ENABLE_PIN ) , GPIO_OTYPE_PP , GPIO_OSPEED_2MHZ , GPIO_PIN ( DRV8825_ENABLE_PIN ) ) ; // set pin output as push-pull
// motor reset pin
rcc_periph_clock_enable ( GPIO_RCC ( DRV8825_RESET_PIN ) ) ; // enable clock for GPIO port peripheral
gpio_clear ( GPIO_PORT ( DRV8825_RESET_PIN ) , GPIO_PIN ( DRV8825_RESET_PIN ) ) ; // put motor into reset mode
gpio_mode_setup ( GPIO_PORT ( DRV8825_RESET_PIN ) , GPIO_MODE_OUTPUT , GPIO_PUPD_NONE , GPIO_PIN ( DRV8825_RESET_PIN ) ) ; // set pin as output
gpio_set_output_options ( GPIO_PORT ( DRV8825_RESET_PIN ) , GPIO_OTYPE_PP , GPIO_OSPEED_2MHZ , GPIO_PIN ( DRV8825_RESET_PIN ) ) ; // set pin output as push-pull
// motor direction pin
rcc_periph_clock_enable ( GPIO_RCC ( DRV8825_DIRECTION_PIN ) ) ; // enable clock for GPIO port peripheral
gpio_clear ( GPIO_PORT ( DRV8825_DIRECTION_PIN ) , GPIO_PIN ( DRV8825_DIRECTION_PIN ) ) ; // set clockwise (not really important)
gpio_mode_setup ( GPIO_PORT ( DRV8825_DIRECTION_PIN ) , GPIO_MODE_OUTPUT , GPIO_PUPD_NONE , GPIO_PIN ( DRV8825_DIRECTION_PIN ) ) ; // set pin as output
gpio_set_output_options ( GPIO_PORT ( DRV8825_DIRECTION_PIN ) , GPIO_OTYPE_PP , GPIO_OSPEED_2MHZ , GPIO_PIN ( DRV8825_DIRECTION_PIN ) ) ; // set pin output as push-pull
// motor step pin
rcc_periph_clock_enable ( GPIO_RCC ( DRV8825_STEP_PIN ) ) ; // enable clock for GPIO port peripheral
gpio_mode_setup ( GPIO_PORT ( DRV8825_STEP_PIN ) , GPIO_MODE_AF , GPIO_PUPD_NONE , GPIO_PIN ( DRV8825_STEP_PIN ) ) ; // set pin to alternate function (e.g. timer)
gpio_set_output_options ( GPIO_PORT ( DRV8825_STEP_PIN ) , GPIO_OTYPE_PP , GPIO_OSPEED_25MHZ , GPIO_PIN ( DRV8825_STEP_PIN ) ) ; // set pin to output with fast rising edge
gpio_set_af ( GPIO_PORT ( DRV8825_STEP_PIN ) , DRV8825_STEP_AF , GPIO_PIN ( DRV8825_STEP_PIN ) ) ; // set alternate timer function
rcc_periph_clock_enable ( RCC_TIM ( DRV8825_STEP_TIMER ) ) ; // enable clock for timer peripheral
rcc_periph_reset_pulse ( RST_TIM ( DRV8825_STEP_TIMER ) ) ; // reset timer state
timer_set_mode ( TIM ( DRV8825_STEP_TIMER ) , TIM_CR1_CKD_CK_INT , TIM_CR1_CMS_EDGE , TIM_CR1_DIR_UP ) ; // set timer mode, use undivided timer clock, edge alignment (simple count), and count up
timer_set_prescaler ( TIM ( DRV8825_STEP_TIMER ) , rcc_ahb_frequency / ( UINT16_MAX * 100 ) - 1 ) ; // set the clock frequency to 1.5 kHz (maximum is 250 kHz)
timer_set_period ( TIM ( DRV8825_STEP_TIMER ) , UINT16_MAX ) ; // use the whole range as period, even if we can only control up to 100 Hz
timer_set_oc_value ( TIM ( DRV8825_STEP_TIMER ) , DRV8825_STEP_OC , UINT16_MAX / 2 ) ; // duty cycle to 50% (minimum pulse duration is 1.9 µs)
timer_set_oc_mode ( TIM ( DRV8825_STEP_TIMER ) , DRV8825_STEP_OC , TIM_OCM_PWM1 ) ; // set timer to generate PWM
timer_enable_oc_output ( TIM ( DRV8825_STEP_TIMER ) , DRV8825_STEP_OC ) ; // enable output to generate the PWM signal
timer_enable_break_main_output ( TIM ( DRV8825_STEP_TIMER ) ) ; // required to enable timer, even when no dead time is used
timer_set_counter ( TIM ( DRV8825_STEP_TIMER ) , 0 ) ; // reset counter
timer_clear_flag ( TIM ( DRV8825_STEP_TIMER ) , TIM_SR_UIF ) ; // clear update (overflow) flag
timer_update_on_overflow ( TIM ( DRV8825_STEP_TIMER ) ) ; // only use counter overflow as UEV source (use overflow to count steps))
timer_enable_irq ( TIM ( DRV8825_STEP_TIMER ) , TIM_DIER_UIE ) ; // enable update interrupt for overflow
nvic_enable_irq ( NVIC_TIM_IRQ ( DRV8825_STEP_TIMER ) ) ; // catch interrupt in service routine
// motor fault pin
rcc_periph_clock_enable ( GPIO_RCC ( DRV8825_FAULT_PIN ) ) ; // enable clock for GPIO port peripheral
gpio_mode_setup ( GPIO_PORT ( DRV8825_FAULT_PIN ) , GPIO_MODE_INPUT , GPIO_PUPD_PULLUP , GPIO_PIN ( DRV8825_FAULT_PIN ) ) ; // set GPIO to input and pull up (a 10 kOhm external pull-up resistor is still required, the internal is too weak)
bool drv8825_fault = false ; // if driver reported fault
puts_debug ( " OK \n " ) ;
puts_debug ( " setup dial position: " ) ;
// dial position detection pin
rcc_periph_clock_enable ( GPIO_RCC ( DIAL_SWITCH_PIN ) ) ; // enable clock for GPIO port peripheral
gpio_mode_setup ( GPIO_PORT ( DIAL_SWITCH_PIN ) , GPIO_MODE_INPUT , GPIO_PUPD_PULLUP , GPIO_PIN ( DIAL_SWITCH_PIN ) ) ; // set GPIO to input and pull up
exti_select_source ( GPIO_EXTI ( DIAL_SWITCH_PIN ) , GPIO_PORT ( DIAL_SWITCH_PIN ) ) ; // mask external interrupt of this pin only for this port
exti_set_trigger ( GPIO_EXTI ( DIAL_SWITCH_PIN ) , EXTI_TRIGGER_FALLING ) ; // trigger when magnet on dial is nearby
exti_enable_request ( GPIO_EXTI ( DIAL_SWITCH_PIN ) ) ; // enable external interrupt
nvic_enable_irq ( GPIO_NVIC_EXTI_IRQ ( DIAL_SWITCH_PIN ) ) ; // enable interrupt
puts_debug ( " OK \n " ) ;
puts_debug ( " setup RGB matrix: " ) ;
// configure pin for output enable
rcc_periph_clock_enable ( GPIO_RCC ( RGBMATRIX_OE_PIN ) ) ; // enable clock for GPIO port peripheral
gpio_set ( GPIO_PORT ( RGBMATRIX_OE_PIN ) , GPIO_PIN ( RGBMATRIX_OE_PIN ) ) ; // disable output
gpio_set_output_options ( GPIO_PORT ( RGBMATRIX_OE_PIN ) , GPIO_OTYPE_PP , GPIO_OSPEED_50MHZ , GPIO_PIN ( RGBMATRIX_OE_PIN ) ) ; // set fast edge
gpio_mode_setup ( GPIO_PORT ( RGBMATRIX_OE_PIN ) , GPIO_MODE_OUTPUT , GPIO_PUPD_NONE , GPIO_PIN ( RGBMATRIX_OE_PIN ) ) ; // set pin as output
// configure pins for data and clock lines
2022-05-25 17:38:34 +02:00
const uint32_t rgbmatrix_serial_port = GPIO_PORT ( RGBMATRIX_LAT_PIN ) ; // common port for pins controlling the serial data
const uint16_t rgbmatrix_serial_pins = GPIO_PIN ( RGBMATRIX_LAT_PIN ) | GPIO_PIN ( RGBMATRIX_CLK_PIN ) | GPIO_PIN ( RGBMATRIX_R1_PIN ) | GPIO_PIN ( RGBMATRIX_G1_PIN ) | GPIO_PIN ( RGBMATRIX_B1_PIN ) | GPIO_PIN ( RGBMATRIX_R2_PIN ) | GPIO_PIN ( RGBMATRIX_G2_PIN ) | GPIO_PIN ( RGBMATRIX_B2_PIN ) ; // pins controlling the serial data
rcc_periph_clock_enable ( GPIO_RCC ( RGBMATRIX_LAT_PIN ) ) ; // enable clock for GPIO port peripheral
gpio_clear ( rgbmatrix_serial_port , rgbmatrix_serial_pins ) ; // disable LEDs
gpio_set_output_options ( rgbmatrix_serial_port , GPIO_OTYPE_PP , GPIO_OSPEED_50MHZ , rgbmatrix_serial_pins ) ; // set fast edge
gpio_mode_setup ( rgbmatrix_serial_port , GPIO_MODE_OUTPUT , GPIO_PUPD_NONE , rgbmatrix_serial_pins ) ; // set pin as output
2022-05-24 14:45:56 +02:00
// configure pins for address lines
rcc_periph_clock_enable ( RCC_GPIOB ) ; // enable clock for GPIO port peripheral
gpio_clear ( RCC_GPIOB , GPIO0 | GPIO1 | GPIO2 | GPIO3 ) ; // unselect line
gpio_set_output_options ( GPIOB , GPIO_OTYPE_PP , GPIO_OSPEED_50MHZ , GPIO0 | GPIO1 | GPIO2 | GPIO3 ) ; // set fast edge
gpio_mode_setup ( GPIOB , GPIO_MODE_OUTPUT , GPIO_PUPD_NONE , GPIO0 | GPIO1 | GPIO2 | GPIO3 ) ; // set pin as output
// configure DMA to sent line data
// because there is no peripheral request for data, this is a memory to memory transfer
rcc_periph_clock_enable ( RGBMATRIX_RCC_DMA ) ; // enable clock for DMA peripheral (any DMA and channel can be used)
dma_disable_stream ( RGBMATRIX_DMA , RGBMATRIX_STREAM ) ; // disable stream before re-configuring
while ( DMA_SCR ( RGBMATRIX_DMA , RGBMATRIX_STREAM ) & DMA_SxCR_EN ) ; // wait until transfer is finished before we can reconfigure
dma_stream_reset ( RGBMATRIX_DMA , RGBMATRIX_STREAM ) ; // use default values
2022-05-25 19:37:17 +02:00
//dma_set_peripheral_address(RGBMATRIX_DMA, RGBMATRIX_STREAM, (uint32_t)&rgbmatrix_data[0]); // set memory to read from (for memory-to-memory transfer, the source is the peripheral)
2022-05-24 14:45:56 +02:00
dma_set_peripheral_size ( RGBMATRIX_DMA , RGBMATRIX_STREAM , DMA_SxCR_PSIZE_8BIT ) ; // we only write the 8 first bit
dma_enable_peripheral_increment_mode ( RGBMATRIX_DMA , RGBMATRIX_STREAM ) ; // increment address of memory to read
dma_set_memory_address ( RGBMATRIX_DMA , RGBMATRIX_STREAM , ( uint32_t ) & GPIOA_ODR ) ; // set GPIOA as destination (for memory-to-memory transfer, the destination is the memory)
dma_set_memory_size ( RGBMATRIX_DMA , RGBMATRIX_STREAM , DMA_SxCR_MSIZE_8BIT ) ; // read 8 bits for transfer
dma_disable_memory_increment_mode ( RGBMATRIX_DMA , RGBMATRIX_STREAM ) ; // don't increment GPIO address
dma_set_number_of_data ( RGBMATRIX_DMA , RGBMATRIX_STREAM , LENGTH ( rgbmatrix_data [ 0 ] ) ) ; // set transfer size (one line)
dma_channel_select ( RGBMATRIX_DMA , RGBMATRIX_STREAM , RGBMATRIX_CHANNEL ) ; // set the channel for this stream
dma_set_transfer_mode ( RGBMATRIX_DMA , RGBMATRIX_STREAM , DMA_SxCR_DIR_MEM_TO_MEM ) ; // set transfer from memory to memory
dma_set_priority ( RGBMATRIX_DMA , RGBMATRIX_STREAM , DMA_SxCR_PL_LOW ) ; // there is no need to rush
2022-05-25 20:21:37 +02:00
dma_enable_transfer_complete_interrupt ( RGBMATRIX_DMA , RGBMATRIX_STREAM ) ; // interrupt when line transfer is complete
nvic_enable_irq ( RGBMATRIX_IRQ ) ; // enable interrupt
2022-05-24 14:45:56 +02:00
rgbmatrix_clear ( ) ; // clear matrix
gpio_clear ( GPIO_PORT ( RGBMATRIX_OE_PIN ) , GPIO_PIN ( RGBMATRIX_OE_PIN ) ) ; // enable output
2022-05-25 19:37:17 +02:00
// configure timer to go through rows/lines
rcc_periph_clock_enable ( RCC_TIM ( RGBMATRIX_TIMER ) ) ; // enable clock for timer domain
rcc_periph_reset_pulse ( RST_TIM ( RGBMATRIX_TIMER ) ) ; // reset timer state
timer_set_mode ( TIM ( RGBMATRIX_TIMER ) , TIM_CR1_CKD_CK_INT , TIM_CR1_CMS_EDGE , TIM_CR1_DIR_UP ) ; // set timer mode, use undivided timer clock, edge alignment (simple count), and count up
timer_set_prescaler ( TIM ( RGBMATRIX_TIMER ) , 2 - 1 ) ; // hand tuned prescale to minimize inter-line ghosting
2022-05-25 19:58:08 +02:00
timer_set_period ( TIM ( RGBMATRIX_TIMER ) , 0x9fff - 1 ) ; // hand tuned period to minimize inter-line ghosting
2022-05-25 19:37:17 +02:00
timer_clear_flag ( TIM ( RGBMATRIX_TIMER ) , TIM_SR_UIF ) ; // clear update (overflow) flag
timer_update_on_overflow ( TIM ( RGBMATRIX_TIMER ) ) ; // only use counter overflow as UEV source (use overflow as next line update indication)
timer_enable_irq ( TIM ( RGBMATRIX_TIMER ) , TIM_DIER_UIE ) ; // enable update interrupt for timer
nvic_enable_irq ( NVIC_TIM_IRQ ( RGBMATRIX_TIMER ) ) ; // catch interrupt in service routine
timer_enable_counter ( TIM ( RGBMATRIX_TIMER ) ) ; // start timer to update RGB matrix
2022-05-24 14:45:56 +02:00
puts_debug ( " OK \n " ) ;
2022-06-03 10:10:16 +02:00
// setup LED strips
puts_debug ( " setup RGBW LED strips: " ) ;
// configure pins
// red channel
rcc_periph_clock_enable ( GPIO_RCC ( STRIP_R_PIN ) ) ; // enable clock for GPIO port peripheral
gpio_clear ( GPIO_PORT ( STRIP_R_PIN ) , GPIO_PIN ( STRIP_R_PIN ) ) ; // switch off light
gpio_set_output_options ( GPIO_PORT ( STRIP_R_PIN ) , GPIO_OTYPE_PP , GPIO_OSPEED_2MHZ , GPIO_PIN ( STRIP_R_PIN ) ) ; // set slow edge
gpio_set_af ( GPIO_PORT ( STRIP_R_PIN ) , STRIP_AF , GPIO_PIN ( STRIP_R_PIN ) ) ; // set alternate function to
gpio_mode_setup ( GPIO_PORT ( STRIP_R_PIN ) , GPIO_MODE_AF , GPIO_PUPD_NONE , GPIO_PIN ( STRIP_R_PIN ) ) ; // set pin to alternate timer channel
// green channel
rcc_periph_clock_enable ( GPIO_RCC ( STRIP_G_PIN ) ) ; // enable clock for GPIO port peripheral
gpio_clear ( GPIO_PORT ( STRIP_G_PIN ) , GPIO_PIN ( STRIP_G_PIN ) ) ; // switch off light
gpio_set_output_options ( GPIO_PORT ( STRIP_G_PIN ) , GPIO_OTYPE_PP , GPIO_OSPEED_2MHZ , GPIO_PIN ( STRIP_G_PIN ) ) ; // set slow edge
gpio_set_af ( GPIO_PORT ( STRIP_G_PIN ) , STRIP_AF , GPIO_PIN ( STRIP_G_PIN ) ) ; // set alternate function to
gpio_mode_setup ( GPIO_PORT ( STRIP_G_PIN ) , GPIO_MODE_AF , GPIO_PUPD_NONE , GPIO_PIN ( STRIP_G_PIN ) ) ; // set pin as output
// blue channel
rcc_periph_clock_enable ( GPIO_RCC ( STRIP_B_PIN ) ) ; // enable clock for GPIO port peripheral
gpio_clear ( GPIO_PORT ( STRIP_B_PIN ) , GPIO_PIN ( STRIP_B_PIN ) ) ; // switch off light
gpio_set_output_options ( GPIO_PORT ( STRIP_B_PIN ) , GPIO_OTYPE_PP , GPIO_OSPEED_2MHZ , GPIO_PIN ( STRIP_B_PIN ) ) ; // set slow edge
gpio_set_af ( GPIO_PORT ( STRIP_B_PIN ) , STRIP_AF , GPIO_PIN ( STRIP_B_PIN ) ) ; // set alternate function to
gpio_mode_setup ( GPIO_PORT ( STRIP_B_PIN ) , GPIO_MODE_AF , GPIO_PUPD_NONE , GPIO_PIN ( STRIP_B_PIN ) ) ; // set pin as output
// white channel
rcc_periph_clock_enable ( GPIO_RCC ( STRIP_W_PIN ) ) ; // enable clock for GPIO port peripheral
gpio_clear ( GPIO_PORT ( STRIP_W_PIN ) , GPIO_PIN ( STRIP_W_PIN ) ) ; // switch off light
gpio_set_output_options ( GPIO_PORT ( STRIP_W_PIN ) , GPIO_OTYPE_PP , GPIO_OSPEED_2MHZ , GPIO_PIN ( STRIP_W_PIN ) ) ; // set slow edge
gpio_set_af ( GPIO_PORT ( STRIP_W_PIN ) , STRIP_AF , GPIO_PIN ( STRIP_W_PIN ) ) ; // set alternate function to
gpio_mode_setup ( GPIO_PORT ( STRIP_W_PIN ) , GPIO_MODE_AF , GPIO_PUPD_NONE , GPIO_PIN ( STRIP_W_PIN ) ) ; // set pin as output
// configure timer to generate PWM
rcc_periph_clock_enable ( RCC_TIM ( STRIP_TIMER ) ) ; // enable clock for timer peripheral
rcc_periph_reset_pulse ( RST_TIM ( STRIP_TIMER ) ) ; // reset timer state
timer_disable_counter ( TIM ( STRIP_TIMER ) ) ; // disable timer to configure it
timer_set_mode ( TIM ( STRIP_TIMER ) , TIM_CR1_CKD_CK_INT , TIM_CR1_CMS_EDGE , TIM_CR1_DIR_UP ) ; // set timer mode, use undivided timer clock, edge alignment (simple count), and count up
timer_set_prescaler ( TIM ( STRIP_TIMER ) , 10 - 1 ) ; // set presecaler for fast enough for LED PWM and less stress on MOSFET gate ( (84E6/(10 * 2**16))=1281 Hz )
// red channel
timer_set_oc_mode ( TIM ( STRIP_TIMER ) , TIM_OC ( STRIP_R_CH ) , TIM_OCM_PWM1 ) ; // set output to PWM mode
timer_enable_oc_output ( TIM ( STRIP_TIMER ) , TIM_OC ( STRIP_R_CH ) ) ; // enable PWM output
timer_set_oc_value ( TIM ( STRIP_TIMER ) , TIM_OC ( STRIP_R_CH ) , 0 ) ; // disable light
// green channel
timer_set_oc_mode ( TIM ( STRIP_TIMER ) , TIM_OC ( STRIP_G_CH ) , TIM_OCM_PWM1 ) ; // set output to PWM mode
timer_enable_oc_output ( TIM ( STRIP_TIMER ) , TIM_OC ( STRIP_G_CH ) ) ; // enable PWM output
timer_set_oc_value ( TIM ( STRIP_TIMER ) , TIM_OC ( STRIP_G_CH ) , 0 ) ; // disable light
// blue channel
timer_set_oc_mode ( TIM ( STRIP_TIMER ) , TIM_OC ( STRIP_B_CH ) , TIM_OCM_PWM1 ) ; // set output to PWM mode
timer_enable_oc_output ( TIM ( STRIP_TIMER ) , TIM_OC ( STRIP_B_CH ) ) ; // enable PWM output
timer_set_oc_value ( TIM ( STRIP_TIMER ) , TIM_OC ( STRIP_B_CH ) , 0 ) ; // disable light
// white channel
timer_set_oc_mode ( TIM ( STRIP_TIMER ) , TIM_OC ( STRIP_W_CH ) , TIM_OCM_PWM1 ) ; // set output to PWM mode
timer_enable_oc_output ( TIM ( STRIP_TIMER ) , TIM_OC ( STRIP_W_CH ) ) ; // enable PWM output
timer_set_oc_value ( TIM ( STRIP_TIMER ) , TIM_OC ( STRIP_W_CH ) , 0 ) ; // disable light
timer_enable_counter ( TIM ( STRIP_TIMER ) ) ; // enable timer to generate PWM
/*
// use comparator to time signal (without using the output), starting at slot start
timer_clear_flag ( TIM ( ONEWIRE_MASTER_TIMER ) , TIM_SR_CC1IF ) ; // clear flag
timer_set_oc_value ( TIM ( ONEWIRE_MASTER_TIMER ) , TIM_OC1 , 1 * ( rcc_ahb_frequency / 1000000 ) - 1 ) ; // use compare function to time master pulling low when reading (1 < Tlowr < 15)
timer_clear_flag ( TIM ( ONEWIRE_MASTER_TIMER ) , TIM_SR_CC2IF ) ; // clear flag
timer_set_oc_value ( TIM ( ONEWIRE_MASTER_TIMER ) , TIM_OC2 , 7 * ( rcc_ahb_frequency / 1000000 ) - 1 ) ; // use compare function to read or write 0 or 1 (1 < Trw < 15)
timer_clear_flag ( TIM ( ONEWIRE_MASTER_TIMER ) , TIM_SR_CC3IF ) ; // clear flag
timer_set_oc_value ( TIM ( ONEWIRE_MASTER_TIMER ) , TIM_OC3 , 62 * ( rcc_ahb_frequency / 1000000 ) - 1 ) ; // use compare function to end time slot (60 < Tslot < 120), this will be followed by a recovery time (end of timer)
timer_clear_flag ( TIM ( ONEWIRE_MASTER_TIMER ) , TIM_SR_CC4IF ) ; // clear flag
timer_set_oc_value ( TIM ( ONEWIRE_MASTER_TIMER ) , TIM_OC4 , ( 70 - 10 ) * ( rcc_ahb_frequency / 1000000 ) - 1 ) ; // use compare function to detect slave presence (15 < Tpdh < 60 + 60 < Tpdl < 240), with hand tuning
timer_clear_flag ( TIM ( ONEWIRE_MASTER_TIMER ) , TIM_SR_UIF ) ; // clear update (overflow) flag
timer_update_on_overflow ( TIM ( ONEWIRE_MASTER_TIMER ) ) ; // only use counter overflow as UEV source (use overflow as start time or timeout)
timer_enable_irq ( TIM ( ONEWIRE_MASTER_TIMER ) , TIM_DIER_UIE ) ; // enable update interrupt for overflow
*/
puts_debug ( " 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
2022-05-24 14:45:56 +02:00
// start motor to figure out position
gpio_set ( GPIO_PORT ( DRV8825_RESET_PIN ) , GPIO_PIN ( DRV8825_RESET_PIN ) ) ; // power up driver
int32_t speed = 300 ;
command_speed ( & speed ) ;
2022-05-25 19:57:44 +02:00
// draw welcome text
2022-05-25 20:14:50 +02:00
rgbmatrix_puts ( 1 , 1 , " DACHBODEN " , FONT_KING10 , false , true , false ) ;
rgbmatrix_puts ( 1 , 12 , " ZEIT " , FONT_KING10 , false , true , true ) ;
rgbmatrix_puts ( 1 , 23 , " MASCHINE " , FONT_KING10 , true , true , false ) ;
2022-05-26 10:39:03 +02:00
sleep_ms ( 3000 ) ; // show the text for a tiny bit
2022-05-25 19:57:44 +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
2022-05-26 10:39:03 +02:00
const char * scroll_text = " DACHBODEN ZEITMASCHINE " ;
int16_t scroll_pos = 64 ;
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
}
2020-11-30 14:36:33 +01:00
if ( wakeup_flag ) { // time to do periodic checks
wakeup_flag = false ; // clear flag
2022-05-26 10:39:03 +02:00
rgbmatrix_clear ( ) ;
rgbmatrix_puts ( scroll_pos , 8 , scroll_text , FONT_KING14 , true , true , true ) ;
if ( scroll_pos < - 1 * ( int16_t ) strlen ( scroll_text ) * ( fonts [ FONT_KING14 ] . width + 1 ) ) {
scroll_pos = 64 ;
} else {
scroll_pos - = 1 ;
}
2022-06-03 10:10:16 +02:00
strip_rgbw ( 0 , 0 , 0 , ( 64 - scroll_pos ) * 100 ) ;
2020-11-30 14:36:33 +01:00
}
if ( second_flag ) { // one second passed
second_flag = false ; // clear flag
led_toggle ( ) ; // toggle LED to indicate if main function is stuck
2022-05-24 14:45:56 +02:00
}
if ( 0 = = gpio_get ( GPIO_PORT ( DRV8825_FAULT_PIN ) , GPIO_PIN ( DRV8825_FAULT_PIN ) ) ) { // DRV8825 stepper motor error reports error
gpio_set ( GPIO_PORT ( DRV8825_ENABLE_PIN ) , GPIO_PIN ( DRV8825_ENABLE_PIN ) ) ; // disable motor
gpio_clear ( GPIO_PORT ( DRV8825_RESET_PIN ) , GPIO_PIN ( DRV8825_RESET_PIN ) ) ; // put motor to sleep
if ( ! drv8825_fault ) {
puts ( " DRV8825 fault detected \n " ) ;
drv8825_fault = true ; // remember new fault
}
}
if ( dial_steps ) { // hour dial position detected
if ( drv8825_steps > = DIAL_MIDNIGHT_STEPS ) { // wait for dial to reach midnight
speed = 0 ; // stop motor
command_speed ( & speed ) ; // stop motor
dial_steps = 0 ; // restart position counter
puts ( " midnight reached \n " ) ;
}
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
}
}
2022-05-24 14:45:56 +02:00
/** ISR triggered after a completed step */
void TIM_ISR ( DRV8825_STEP_TIMER ) ( void )
{
if ( timer_get_flag ( TIM ( DRV8825_STEP_TIMER ) , TIM_SR_UIF ) ) { // overflow update event happened
timer_clear_flag ( TIM ( DRV8825_STEP_TIMER ) , TIM_SR_UIF ) ; // clear flag
drv8825_steps + = drv8825_direction ; // increment number of steps
if ( UINT32_MAX = = drv8825_steps ) { // underflow
drv8825_steps = DIAL_CYCLE_STEPS ; // use known circumference
}
}
}
/** ISR triggered when hour dial is near reed switch
* @ note surprisingly there is very little bouncing
*/
void GPIO_EXTI_ISR ( DIAL_SWITCH_PIN ) ( void )
{
exti_reset_request ( GPIO_EXTI ( DIAL_SWITCH_PIN ) ) ; // reset interrupt
if ( drv8825_steps > dial_steps + 1000 ) { // ignore going away debounce
dial_steps = drv8825_steps ; // remember on which step we are
drv8825_steps = 0 ; // restart step counter
}
}
2022-05-25 20:21:37 +02:00
static volatile uint8_t rgbmatrix_line = 0 ; // line being transferred
/** ISR triggered when the data for the line of the RGB matrix has been transferred
* we switch line just after the data is latched , to reduce ghosting
*/
void RGBMATRIX_ISR ( void )
{
if ( dma_get_interrupt_flag ( RGBMATRIX_DMA , RGBMATRIX_STREAM , DMA_TCIF ) ) {
dma_clear_interrupt_flags ( RGBMATRIX_DMA , RGBMATRIX_STREAM , DMA_TCIF ) ;
GPIOB_ODR = ( GPIOB_ODR & 0xfff0 ) + rgbmatrix_line ; // select line (line on lower and upper half are updated at once)
rgbmatrix_line = ( rgbmatrix_line + 1 ) % ( RGBMATRIX_HEIGHT / 2 ) ; // go to next line (two lines are updated at once)
}
}
2022-05-25 19:37:17 +02:00
/** interrupt service routine called to update next line of RGB matrix
* @ note ideally the next line should be updated when the current one is complete , but the DMA is too fast .
* @ note switching lines too fast causes inter - line ghosting of the LEDs ( on the same column ) , due to capacitance and driver switching limitations
2022-05-24 14:45:56 +02:00
*/
2022-05-25 19:37:17 +02:00
void TIM_ISR ( RGBMATRIX_TIMER ) ( void )
2022-05-24 14:45:56 +02:00
{
2022-05-25 19:37:17 +02:00
if ( timer_get_flag ( TIM ( RGBMATRIX_TIMER ) , TIM_SR_UIF ) ) { // update event happened
timer_clear_flag ( TIM ( RGBMATRIX_TIMER ) , TIM_SR_UIF ) ; // clear flag
if ( DMA_SCR ( RGBMATRIX_DMA , RGBMATRIX_STREAM ) & DMA_SxCR_EN ) { // DMA is not complete
return ;
}
dma_set_peripheral_address ( RGBMATRIX_DMA , RGBMATRIX_STREAM , ( uint32_t ) & rgbmatrix_data [ rgbmatrix_line ] ) ; // set memory containing line data to be transferred (for memory-to-memory transfer, the source is the peripheral)
2022-05-25 17:38:34 +02:00
dma_enable_stream ( RGBMATRIX_DMA , RGBMATRIX_STREAM ) ; // start sending next line
2022-05-24 14:45:56 +02:00
}
}