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
2022-07-27 10:52:39 +02:00
# define RGBPANEL_ENABLE 0 // if RGB panel is used
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
2022-07-27 10:52:39 +02:00
# if RGBPANEL_ENABLE
2022-07-27 10:40:36 +02:00
# include "led_rgbpanel.h" // to control RGB panels
2022-07-27 10:52:39 +02:00
# else
# define RGBPANEL_WIDTH 0
# define RGBPANEL_HEIGHT 0
# endif
2022-07-14 19:00:02 +02:00
# include "led_ws2812b.h" // control WS2812b LEDs
2022-07-27 10:40:36 +02:00
# include "radio_esp8266.h" // to receive ARTnet
2016-01-17 14:54:54 +01:00
2020-02-02 21:19:03 +01:00
/** watchdog period in ms */
2022-08-11 14:08:54 +02:00
# define WATCHDOG_PERIOD 20000
2020-02-02 21:19:03 +01:00
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) */
2022-07-25 15:42:36 +02:00
static uint32_t drv8825_goal = 0 ; /**< number of steps to reach */
static bool drv8825_reached = false ; /**< set when the goal is reached */
2022-05-24 14:45:56 +02:00
/** maximum speed (in steps/s) before the motor stalls (found empirically)
* @ note found empirically 300 @ 9 V / 180 mA , 420 @ 12 V / 150 mA
*/
2022-08-11 14:11:13 +02:00
# define DRV8825_SPEED_LIMIT 600U
2022-05-24 14:45:56 +02:00
// dials position info
2022-07-25 15:40:56 +02:00
# define DIAL_SWITCH_PIN PB4 /**< pin connected to reed switch, connected to ground when the hour dial is nearby */
2022-08-11 14:13:24 +02:00
# define DIAL_CYCLE_STEPS 4200U /**< number of steps for the hour dial to make a round */
# define DIAL_MIDNIGHT_STEPS 3207U /**< number of steps after dial detection for dials to show midnight */
2022-05-24 14:45:56 +02:00
static volatile uint32_t dial_steps = 0 ; /**< set to drv8825_steps when dial is nearby */
2022-08-11 14:16:48 +02:00
static volatile bool reed_flag = false ;
2022-07-21 21:24:13 +02:00
# define WSMATRIX_HEIGHT (2 * 8) /**< WS2812b panel height, in pixels */
# define WSMATRIX_WIDTH 32 /**< WS2812b panel width, in pixels */
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-07-27 10:06:53 +02:00
# define UNIVERSE_OFFSET 20 /**< first Art-Net universe to listen to */
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 ) {
2022-08-11 14:11:13 +02:00
gpio_set ( GPIO_PORT ( DRV8825_DIRECTION_PIN ) , GPIO_PIN ( DRV8825_DIRECTION_PIN ) ) ; // set clockwise
2022-05-24 14:45:56 +02:00
drv8825_direction = 1 ; // remember we go clockwise
} else {
2022-08-11 14:11:13 +02:00
gpio_clear ( GPIO_PORT ( DRV8825_DIRECTION_PIN ) , GPIO_PIN ( DRV8825_DIRECTION_PIN ) ) ; // set counter-clockwise
2022-05-24 14:45:56 +02:00
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
}
}
2022-07-27 10:52:39 +02:00
/** set color of the LED on the WS2812b panel
2022-07-21 21:24:13 +02:00
* @ param [ in ] ws false to display on RGB matrix , true on WS2812b panel
2022-05-24 14:45:56 +02:00
* @ 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-08-11 14:14:54 +02:00
static void wsmatrix_set ( int16_t x , int16_t y , uint8_t r , uint8_t g , uint8_t b )
2022-05-24 14:45:56 +02:00
{
2022-07-27 10:52:39 +02:00
if ( x < 0 | | x > = WSMATRIX_WIDTH ) {
2022-05-24 14:45:56 +02:00
return ;
}
2022-07-27 10:52:39 +02:00
if ( y < 0 | | y > = WSMATRIX_HEIGHT ) {
2022-05-24 14:45:56 +02:00
return ;
}
2022-08-11 14:17:19 +02:00
# define WSMATRIX_BRIGHTNESS 0x80
2022-07-27 10:52:39 +02:00
uint8_t col = 0 , row = 0 ;
if ( y < WSMATRIX_HEIGHT / 2 ) {
col = WSMATRIX_WIDTH - 1 - x ;
if ( 1 = = x % 2 ) {
row = WSMATRIX_HEIGHT / 2 - 1 - y ;
2022-07-21 21:24:13 +02:00
} else {
2022-07-27 10:52:39 +02:00
row = y ;
2022-05-24 14:45:56 +02:00
}
2022-07-27 10:52:39 +02:00
} else {
col = x + WSMATRIX_WIDTH ;
y - = WSMATRIX_HEIGHT / 2 ;
if ( 1 = = x % 2 ) {
row = WSMATRIX_HEIGHT / 2 - 1 - y ;
} else {
row = y ;
}
}
led_ws2812b_set_rgb ( col * ( WSMATRIX_HEIGHT / 2U ) + row , g * WSMATRIX_BRIGHTNESS , r * WSMATRIX_BRIGHTNESS , b * WSMATRIX_BRIGHTNESS ) ;
}
/** set color of the LED on the RGB matrix or WS2812b panel
* @ param [ in ] ws false to display on RGB matrix , true on WS2812b panel
* @ 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
*/
static void matrix_set ( bool ws , int16_t x , int16_t y , bool r , bool g , bool b )
{
if ( ws ) {
wsmatrix_set ( x , y , r , g , b ) ; // set LED color
# if RGBPANEL_ENABLE
2022-05-24 14:45:56 +02:00
} else {
2022-07-27 10:40:36 +02:00
rgbpanel_set ( x , y , r , g , b ) ; // set LED color
2022-07-27 10:52:39 +02:00
# endif
2022-05-24 14:45:56 +02:00
}
}
2022-07-21 21:24:13 +02:00
/** draw character on matrix
* @ param [ in ] ws false to display on RGB matrix , true on WS2812b panel
2022-05-25 19:57:44 +02:00
* @ 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-08-11 14:14:54 +02:00
static void matrix_putc ( bool ws , int16_t x , int16_t y , char c , enum font_name font , uint8_t red , uint8_t green , uint8_t blue )
2022-05-25 19:57:44 +02:00
{
2022-07-27 10:52:39 +02:00
# if (0 == RGBPANEL_ENABLE)
if ( ! ws ) {
return ;
}
# endif
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-07-27 10:40:36 +02:00
if ( x + fonts [ font ] . width < 0 | | x > = ( ws ? WSMATRIX_WIDTH : RGBPANEL_WIDTH ) ) {
2022-05-25 19:57:44 +02:00
return ;
}
2022-07-27 10:40:36 +02:00
if ( y + fonts [ font ] . height < 0 | | y > = ( ws ? WSMATRIX_HEIGHT : RGBPANEL_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 ) {
2022-07-21 21:24:13 +02:00
matrix_set ( ws , x + col , y + row , red , green , blue ) ;
2022-05-25 19:57:44 +02:00
} else {
2022-08-11 14:14:54 +02:00
matrix_set ( ws , x + col , y + row , 0 , 0 , 0 ) ;
2022-05-25 19:57:44 +02:00
}
}
}
}
2022-05-25 20:14:50 +02:00
/** draw text on RGB matrix
2022-07-21 21:24:13 +02:00
* @ param [ in ] ws false to display on RGB matrix , true on WS2812b panel
2022-05-25 20:14:50 +02:00
* @ 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-07-21 21:24:13 +02:00
static void matrix_puts ( bool ws , 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
{
2022-07-27 10:52:39 +02:00
# if (0 == RGBPANEL_ENABLE)
if ( ! ws ) {
return ;
}
# endif
2022-05-25 20:14:50 +02:00
// sanity checks
if ( NULL = = str ) {
return ;
}
if ( font > = FONT_MAX ) {
return ;
}
2022-07-27 10:40:36 +02:00
if ( y + fonts [ font ] . height < 0 | | y > = ( ws ? WSMATRIX_HEIGHT : RGBPANEL_HEIGHT ) ) {
2022-05-25 20:14:50 +02:00
return ;
}
const uint8_t len = strlen ( str ) ;
for ( uint8_t i = 0 ; i < len ; i + + ) {
2022-07-27 10:40:36 +02:00
if ( x > = ( ws ? WSMATRIX_WIDTH : RGBPANEL_WIDTH ) ) {
2022-05-25 20:14:50 +02:00
return ;
}
2022-05-26 10:38:23 +02:00
if ( x + fonts [ font ] . width > = 0 ) {
2022-07-21 21:24:13 +02:00
matrix_putc ( ws , x , y , str [ i ] , font , red , green , blue ) ;
2022-05-26 10:38:23 +02:00
}
2022-08-11 14:17:56 +02:00
x + = fonts [ font ] . width ;
for ( uint8_t pix_y = y ; pix_y < y + fonts [ font ] . height ; pix_y + + ) {
matrix_set ( ws , x , pix_y , false , false , false ) ;
}
x + = 1 ;
2022-05-25 20:14:50 +02: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
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 " ) ;
2022-07-21 21:24:54 +02:00
// test RGB LED matrix
matrix_set ( false , 0 , 0 , true , false , false ) ;
matrix_set ( false , 1 , 0 , false , true , false ) ;
matrix_set ( false , 2 , 0 , false , false , true ) ;
matrix_set ( false , 0 , 1 , true , false , false ) ;
matrix_set ( false , 1 , 2 , false , true , false ) ;
matrix_set ( false , 2 , 3 , false , false , true ) ;
// test WS2812B panel
matrix_set ( true , 0 , 0 , true , false , false ) ;
matrix_set ( true , 1 , 0 , false , true , false ) ;
matrix_set ( true , 2 , 0 , false , false , true ) ;
matrix_set ( true , 0 , 1 , true , false , false ) ;
matrix_set ( true , 1 , 2 , false , true , false ) ;
matrix_set ( true , 2 , 3 , false , false , true ) ;
matrix_set ( true , 0 , 15 , true , false , false ) ;
matrix_set ( true , 1 , 14 , false , true , false ) ;
matrix_set ( true , 2 , 13 , false , false , true ) ;
2022-05-24 14:45:56 +02:00
}
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 ) ;
}
2022-07-27 10:41:36 +02:00
static void command_dials ( void * argument )
{
if ( argument ) {
const uint32_t seconds = * ( uint32_t * ) argument ; // get provide value
2022-08-11 14:19:23 +02:00
drv8825_goal = seconds ;
drv8825_speed ( 100 ) ;
2022-07-27 10:41:36 +02:00
if ( seconds < 12 * 60 * 60 ) {
const uint32_t goal = DIAL_CYCLE_STEPS * seconds * 1.0 / ( 12 * 60 * 60 ) ;
if ( goal ! = drv8825_goal ) {
drv8825_goal = goal ; // set new goal
uint32_t speed = 300 ;
command_speed ( & speed ) ;
}
}
}
printf ( " dial position/goal: %u/%u \n " , drv8825_steps , drv8825_goal ) ;
}
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
{
2022-08-11 14:20:37 +02:00
. shortcut = ' D ' ,
2019-06-18 17:43:43 +02:00
. 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 ,
} ,
2022-07-27 10:41:36 +02:00
{
. shortcut = ' d ' ,
. name = " dial " ,
. command_description = " set dial position " ,
. argument = MENU_ARGUMENT_UNSIGNED ,
. argument_description = " [sec] " ,
. command_handler = & command_dials ,
} ,
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
2022-08-11 14:15:47 +02:00
iwdg_set_period_ms ( WATCHDOG_PERIOD ) ; // set independent watchdog period
iwdg_start ( ) ; // start independent watchdog
2016-10-23 17:42:27 +02:00
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
2022-08-11 14:11:13 +02:00
gpio_set ( GPIO_PORT ( DRV8825_RESET_PIN ) , GPIO_PIN ( DRV8825_RESET_PIN ) ) ; // power up driver
2022-05-24 14:45:56 +02:00
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
2022-07-25 15:40:56 +02:00
exti_reset_request ( GPIO_EXTI ( DIAL_SWITCH_PIN ) ) ; // ensure the interrupt flag is cleared
2022-05-24 14:45:56 +02:00
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 " ) ;
2022-07-14 19:00:02 +02:00
puts_debug ( " setup WS2812b LED matrix: " ) ;
led_ws2812b_setup ( ) ; // configure peripheral for communication with WS2812b LEDs
puts_debug ( " OK \n " ) ;
2022-07-27 10:52:39 +02:00
# if RGBPANEL_ENABLE
2022-05-24 14:45:56 +02:00
puts_debug ( " setup RGB matrix: " ) ;
2022-07-27 10:40:36 +02:00
rgbpanel_setup ( ) ;
2022-05-24 14:45:56 +02:00
puts_debug ( " OK \n " ) ;
2022-07-27 10:52:39 +02:00
# endif
2022-05-24 14:45:56 +02:00
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
puts_debug ( " OK \n " ) ;
2022-07-14 19:00:46 +02:00
puts_debug ( " setup ESP8266 Art-Net: " ) ;
sleep_ms ( 1000 ) ; // wit for ESP to boot
radio_esp8266_setup ( ) ; // connect to WiFi network
2022-08-11 14:20:12 +02:00
if ( ! radio_esp8266_connected ( ) ) { // check if we are connected
radio_esp8266_reset ( ) ;
}
2022-07-14 19:00:46 +02:00
radio_esp8266_listen ( true , 6454 ) ; // open UDP Art-Net
2022-08-11 14:20:12 +02:00
bool net_connected = true ; // remember we are connected
2022-07-14 19:00:46 +02:00
puts_debug ( " OK " ) ;
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-25 19:57:44 +02:00
// draw welcome text
2022-07-21 21:24:54 +02:00
matrix_puts ( false , 1 , 1 , " DACHBODEN " , FONT_KING10 , false , true , false ) ;
matrix_puts ( false , 1 , 12 , " ZEIT " , FONT_KING10 , false , true , true ) ;
matrix_puts ( false , 1 , 23 , " MASCHINE " , FONT_KING10 , true , true , false ) ;
strip_rgbw ( 0 , 0xffff , 0 , 0 ) ;
//command_matrix(NULL);
matrix_puts ( true , 1 , 0 , " MARCO " , FONT_KING8 , true , true , false ) ;
matrix_puts ( true , 1 , 8 , " ROCKS " , FONT_KING8 , false , true , true ) ;
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-07-27 10:52:39 +02:00
# if RGBPANEL_ENABLE
2022-07-27 10:40:36 +02:00
rgbpanel_clear ( ) ;
2022-07-27 10:52:39 +02:00
# endif
2022-07-21 21:24:13 +02:00
matrix_puts ( false , scroll_pos , 8 , scroll_text , FONT_KING14 , true , true , true ) ;
2022-05-26 10:39:03 +02:00
if ( scroll_pos < - 1 * ( int16_t ) strlen ( scroll_text ) * ( fonts [ FONT_KING14 ] . width + 1 ) ) {
scroll_pos = 64 ;
} else {
scroll_pos - = 1 ;
}
2022-07-21 21:24:54 +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-08-11 14:20:12 +02:00
if ( net_connected ) {
net_connected = false ; // reset flag
} else {
if ( ! radio_esp8266_connected ( ) ) { // check if we are connected
puts ( " not connected: resetting network " ) ;
radio_esp8266_reset ( ) ; // restart and try to reconnect
radio_esp8266_listen ( true , 6454 ) ; // open UDP Art-Net
}
net_connected = true ; // remember we are connected
}
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
2022-07-25 15:42:36 +02:00
puts ( " dials homed \n " ) ;
dial_steps = 0 ; // restart position counter (and clear flag)
drv8825_goal = DIAL_MIDNIGHT_STEPS ; // go to midnight
drv8825_reached = false ; // wait until it's reached
2022-08-11 14:19:23 +02:00
drv8825_speed ( 0 ) ; // stop motor
2022-07-25 15:42:36 +02:00
action = true ;
}
if ( drv8825_reached ) {
puts ( " midnight reached \n " ) ;
2022-08-11 14:19:23 +02:00
drv8825_speed ( 0 ) ; // stop motor
2022-07-25 15:42:36 +02:00
drv8825_goal = 0 ; // disable goal
drv8825_reached = false ; // clear flag
action = true ; // redo main loop
2016-08-14 21:02:38 +02:00
}
2022-07-14 19:00:46 +02:00
if ( radio_esp8266_received_len ) {
2022-08-11 14:20:12 +02:00
net_connected = true ; // we are connected since we received data
2022-07-14 19:00:46 +02:00
if ( radio_esp8266_received_len > = 18 & & 0 = = memcmp ( ( char * ) radio_esp8266_received , " Art-Net " , 7 ) ) {
const uint16_t dmx_universe = radio_esp8266_received [ 14 ] + ( radio_esp8266_received [ 15 ] < < 8 ) ;
const uint16_t dmx_length = radio_esp8266_received [ 17 ] + ( radio_esp8266_received [ 16 ] < < 8 ) ;
2022-07-27 10:06:53 +02:00
if ( radio_esp8266_received_len > = 18U + dmx_length ) {
printf ( " Art-Net packet (uni=%u, len=%u) \n " , dmx_universe , dmx_length ) ;
switch ( dmx_universe ) {
case UNIVERSE_OFFSET + 0 : // RGBW LED strip
if ( dmx_length > = 0 + 2 ) {
strip_rgbw ( ( radio_esp8266_received [ 18 + 0 ] < < 8 ) + radio_esp8266_received [ 18 + 1 ] , - 1 , - 1 , - 1 ) ;
}
if ( dmx_length > = 2 + 2 ) {
strip_rgbw ( - 1 , ( radio_esp8266_received [ 18 + 2 ] < < 8 ) + radio_esp8266_received [ 18 + 3 ] , - 1 , - 1 ) ;
}
if ( dmx_length > = 4 + 2 ) {
strip_rgbw ( - 1 , - 1 , ( radio_esp8266_received [ 18 + 4 ] < < 8 ) + radio_esp8266_received [ 18 + 5 ] , - 1 ) ;
}
if ( dmx_length > = 6 + 2 ) {
strip_rgbw ( - 1 , - 1 , - 1 , ( radio_esp8266_received [ 18 + 6 ] < < 8 ) + radio_esp8266_received [ 18 + 7 ] ) ;
}
break ;
case UNIVERSE_OFFSET + 1 : // dial position
if ( dmx_length > = 3 ) {
uint32_t dial_position = radio_esp8266_received [ 18 + 0 ] * 60 * 60 + radio_esp8266_received [ 18 + 1 ] * 60 + radio_esp8266_received [ 18 + 2 ] ;
command_dials ( & dial_position ) ;
}
break ;
case UNIVERSE_OFFSET + 2 : // text front line 1
if ( dmx_length > = 5 & & strlen ( ( char * ) & radio_esp8266_received [ 18 + 5 ] ) + 5 < dmx_length ) {
const int16_t position = ( radio_esp8266_received [ 18 + 3 ] < < 8 ) + radio_esp8266_received [ 18 + 4 ] ;
matrix_puts ( false , position , 1 , ( char * ) & radio_esp8266_received [ 18 + 5 ] , FONT_KING10 , radio_esp8266_received [ 18 + 0 ] , radio_esp8266_received [ 18 + 1 ] , radio_esp8266_received [ 18 + 2 ] ) ;
}
break ;
case UNIVERSE_OFFSET + 3 : // text front line 2
if ( dmx_length > = 5 & & strlen ( ( char * ) & radio_esp8266_received [ 18 + 5 ] ) + 5 < dmx_length ) {
const int16_t position = ( radio_esp8266_received [ 18 + 3 ] < < 8 ) + radio_esp8266_received [ 18 + 4 ] ;
matrix_puts ( false , position , 12 , ( char * ) & radio_esp8266_received [ 18 + 5 ] , FONT_KING10 , radio_esp8266_received [ 18 + 0 ] , radio_esp8266_received [ 18 + 1 ] , radio_esp8266_received [ 18 + 2 ] ) ;
}
break ;
case UNIVERSE_OFFSET + 4 : // text front line 3
if ( dmx_length > = 5 & & strlen ( ( char * ) & radio_esp8266_received [ 18 + 5 ] ) + 5 < dmx_length ) {
const int16_t position = ( radio_esp8266_received [ 18 + 3 ] < < 8 ) + radio_esp8266_received [ 18 + 4 ] ;
matrix_puts ( false , position , 23 , ( char * ) & radio_esp8266_received [ 18 + 5 ] , FONT_KING10 , radio_esp8266_received [ 18 + 0 ] , radio_esp8266_received [ 18 + 1 ] , radio_esp8266_received [ 18 + 2 ] ) ;
}
break ;
case UNIVERSE_OFFSET + 5 : // text back line 1
if ( dmx_length > = 5 & & strlen ( ( char * ) & radio_esp8266_received [ 18 + 5 ] ) + 5 < dmx_length ) {
const int16_t position = ( radio_esp8266_received [ 18 + 3 ] < < 8 ) + radio_esp8266_received [ 18 + 4 ] ;
matrix_puts ( true , position , 0 , ( char * ) & radio_esp8266_received [ 18 + 5 ] , FONT_KING8 , radio_esp8266_received [ 18 + 0 ] , radio_esp8266_received [ 18 + 1 ] , radio_esp8266_received [ 18 + 2 ] ) ;
}
break ;
case UNIVERSE_OFFSET + 6 : // back front line 2
if ( dmx_length > = 5 & & strlen ( ( char * ) & radio_esp8266_received [ 18 + 5 ] ) + 5 < dmx_length ) {
const int16_t position = ( radio_esp8266_received [ 18 + 3 ] < < 8 ) + radio_esp8266_received [ 18 + 4 ] ;
matrix_puts ( true , position , 8 , ( char * ) & radio_esp8266_received [ 18 + 5 ] , FONT_KING10 , radio_esp8266_received [ 18 + 0 ] , radio_esp8266_received [ 18 + 1 ] , radio_esp8266_received [ 18 + 2 ] ) ;
}
break ;
default :
break ;
}
}
2022-07-14 19:00:46 +02:00
}
radio_esp8266_received_len = 0 ; // reset flag
action = true ; // redo main loop
}
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
}
2022-07-25 15:42:36 +02:00
if ( ! drv8825_reached & & drv8825_goal & & drv8825_steps = = drv8825_goal ) { // we reached the set goal
drv8825_reached = true ; // notify main loop
}
2022-05-24 14:45:56 +02:00
}
}
/** 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
2022-08-11 14:16:48 +02:00
reed_flag = true ;
2022-07-27 10:40:36 +02:00
// I don't know why, but the RGBPANEL_ISR does trigger this EXTI
2022-07-25 15:40:56 +02:00
if ( gpio_get ( GPIO_PORT ( DIAL_SWITCH_PIN ) , GPIO_PIN ( DIAL_SWITCH_PIN ) ) ) { // be sure the pin is actually low
return ;
}
2022-08-11 14:13:24 +02:00
if ( drv8825_steps > DIAL_CYCLE_STEPS / 4 ) { // ignore going away debounce
2022-05-24 14:45:56 +02:00
dial_steps = drv8825_steps ; // remember on which step we are
drv8825_steps = 0 ; // restart step counter
}
}