/** firmware to control the cool clock * @file * @author King Kévin * @copyright SPDX-License-Identifier: GPL-3.0-or-later * @date 2016-2022 */ /* standard libraries */ #include // standard integer types #include // standard utilities #include // string utilities #include // date/time utilities #include // utilities to check chars /* STM32 (including CM3) libraries */ #include // Cortex M3 utilities #include // vector table definition #include // interrupt utilities #include // general purpose input output library #include // real-time control clock library #include // external interrupt utilities #include // real time clock utilities #include // independent watchdog utilities #include // debug utilities #include // design utilities #include // flash utilities #include // timer library #include // DMA library #include // USB definitions #define RGBPANEL_ENABLE 0 // if RGB panel is used /* own libraries */ #include "global.h" // board definitions #include "print.h" // printing utilities #include "usb_cdcacm.h" // USB CDC ACM utilities #include "terminal.h" // handle the terminal interface #include "menu.h" // menu utilities #include "font.h" // to draw text #if RGBPANEL_ENABLE #include "led_rgbpanel.h" // to control RGB panels #else #define RGBPANEL_WIDTH 0 #define RGBPANEL_HEIGHT 0 #endif #include "led_ws2812b.h" // control WS2812b LEDs #include "radio_esp8266.h" // to receive ARTnet /** watchdog period in ms */ #define WATCHDOG_PERIOD 20000 /** wakeup frequency (i.e. least number of times per second to perform the main loop) */ #define WAKEUP_FREQ 16 /** @defgroup main_flags flag set in interrupts to be processed in main task * @{ */ static volatile bool wakeup_flag = false; /**< flag set when wakeup timer triggered */ static volatile bool second_flag = false; /**< flag set when a second passed */ /** @} */ /** number of seconds since boot */ static uint32_t boot_time = 0; // 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) */ static uint32_t drv8825_goal = 0; /**< number of steps to reach */ static bool drv8825_reached = false; /**< set when the goal is reached */ /** maximum speed (in steps/s) before the motor stalls (found empirically) * @note found empirically 300 @ 9V/180mA, 420 @ 12V/150mA */ #define DRV8825_SPEED_LIMIT 600U // dials position info #define DIAL_SWITCH_PIN PB4 /**< pin connected to reed switch, connected to ground when the hour dial is nearby */ #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 */ static volatile uint32_t dial_steps = 0; /**< set to drv8825_steps when dial is nearby */ static volatile bool reed_flag = false; #define WSMATRIX_HEIGHT (2 * 8) /**< WS2812b panel height, in pixels */ #define WSMATRIX_WIDTH 32 /**< WS2812b panel width, in pixels */ // 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 */ #define UNIVERSE_OFFSET 20 /**< first Art-Net universe to listen to */ /** 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_set(GPIO_PORT(DRV8825_DIRECTION_PIN), GPIO_PIN(DRV8825_DIRECTION_PIN)); // set clockwise drv8825_direction = 1; // remember we go clockwise } else { gpio_clear(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 } } /** set color of the LED on the 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 wsmatrix_set(int16_t x, int16_t y, uint8_t r, uint8_t g, uint8_t b) { if (x < 0 || x >= WSMATRIX_WIDTH) { return; } if (y < 0 || y >= WSMATRIX_HEIGHT) { return; } #define WSMATRIX_BRIGHTNESS 0x20 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; } else { row = y; } } 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 } else { rgbpanel_set(x, y, r, g, b); // set LED color #endif } } /** draw character on matrix * @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] 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 */ 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) { #if (0 == RGBPANEL_ENABLE) if (!ws) { return; } #endif // sanity checks if (font >= FONT_MAX) { return; } if (c < ' ' || c > '~') { return; } if (x + fonts[font].width < 0 || x >= (ws ? WSMATRIX_WIDTH : RGBPANEL_WIDTH)) { return; } if (y + fonts[font].height < 0 || y >= (ws ? WSMATRIX_HEIGHT : RGBPANEL_HEIGHT)) { 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) { matrix_set(ws, x + col, y + row, red, green, blue); } else { matrix_set(ws, x + col, y + row, 0, 0, 0); } } } } /** draw text on RGB matrix * @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] 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 */ 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) { #if (0 == RGBPANEL_ENABLE) if (!ws) { return; } #endif // sanity checks if (NULL == str) { return; } if (font >= FONT_MAX) { return; } if (y + fonts[font].height < 0 || y >= (ws ? WSMATRIX_HEIGHT : RGBPANEL_HEIGHT)) { return; } const uint8_t len = strlen(str); for (uint8_t i = 0; i < len; i++) { if (x >= (ws ? WSMATRIX_WIDTH : RGBPANEL_WIDTH)) { return; } if (x + fonts[font].width >= 0) { matrix_putc(ws, x, y, str[i], font, red, green, blue); } x += fonts[font].width + 1; } } size_t putc(char c) { size_t length = 0; // number of characters printed 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 usb_cdcacm_putchar('\r'); // send CR over USB length++; // remember we printed 1 character } } usb_cdcacm_putchar(c); // send byte over USB length++; // remember we printed 1 character last_c = c; // remember last character return length; // return number of characters printed } // only print when debug is enabled #if DEBUG #define puts_debug(x) puts(x) #else #define puts_debug(x) {} #endif /** 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 */ 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) } /** 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 const uint32_t seconds = ((((((((year * 12) + month) * 31) + day) * 24) + hour) * 60) + minute) * 60 + second; // convert to number of seconds return seconds; } /** show uptime * @param[in] argument no argument required */ static void command_uptime(void* argument) { (void)argument; // we won't use the argument const uint32_t uptime = rtc_to_seconds() - boot_time; // get time from internal RTC printf("uptime: %u.%02u:%02u:%02u\n", uptime / (24 * 60 * 60), (uptime / (60 * 60)) % 24, (uptime / 60) % 60, uptime % 60); } /** show date and time * @param[in] argument date and time to set */ static void command_datetime(void* argument) { char* datetime = (char*)argument; // argument is optional date time 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 printf(malformed); return; } 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) printf(malformed); return; } 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); } } /** reset board * @param[in] argument no argument required */ 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 } /** switch to DFU bootloader * @param[in] argument no argument required */ static void command_bootloader(void* argument) { (void)argument; // we won't use the argument dfu_bootloader(); // start DFU bootloader } /** 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"); // 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); } /** 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); } static void command_dials(void* argument) { if (argument) { const uint32_t seconds = *(uint32_t*)argument; // get provide value 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); } /** 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, }, { .shortcut = 'd', .name = "date", .command_description = "show/set date and time", .argument = MENU_ARGUMENT_STRING, .argument_description = "[YYYY-MM-DD HH:MM:SS]", .command_handler = &command_datetime, }, { .shortcut = 'R', .name = "reset", .command_description = "reset board", .argument = MENU_ARGUMENT_NONE, .argument_description = NULL, .command_handler = &command_reset, }, { .shortcut = 'S', .name = "system", .command_description = "reboot into system memory", .argument = MENU_ARGUMENT_NONE, .argument_description = NULL, .command_handler = &command_system, }, { .shortcut = 'B', .name = "bootloader", .command_description = "reboot into DFU bootloader", .argument = MENU_ARGUMENT_NONE, .argument_description = NULL, .command_handler = &command_bootloader, }, { .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, }, { .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, }, { .shortcut = 'd', .name = "dial", .command_description = "set dial position", .argument = MENU_ARGUMENT_UNSIGNED, .argument_description = "[sec]", .command_handler = &command_dials, }, }; 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 } /** process user command * @param[in] str user command string (\0 ended) */ static void process_command(char* str) { // ensure actions are available if (0 == LENGTH(menu_commands)) { return; } // don't handle empty lines if (!str || 0 == strlen(str)) { 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"); } } /** program entry point * this is the firmware function started by the micro-controller */ void main(void); void main(void) { #if DEBUG // enable functionalities for easier debug 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) #else // 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 #endif iwdg_set_period_ms(WATCHDOG_PERIOD); // set independent watchdog period iwdg_start(); // start independent watchdog board_setup(); // setup board usb_cdcacm_setup(); // setup USB CDC ACM (for printing) 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 puts("\nwelcome to the World Clock controller\n"); // print welcome message #if DEBUG // show reset cause if (RCC_CSR & (RCC_CSR_LPWRRSTF | RCC_CSR_WWDGRSTF | RCC_CSR_IWDGRSTF | RCC_CSR_SFTRSTF | RCC_CSR_PORRSTF | RCC_CSR_PINRSTF)) { puts_debug("reset cause(s):"); if (RCC_CSR & RCC_CSR_LPWRRSTF) { puts_debug(" low-power"); } if (RCC_CSR & RCC_CSR_WWDGRSTF) { puts_debug(" window-watchdog"); } if (RCC_CSR & RCC_CSR_IWDGRSTF) { puts_debug(" independent-watchdog"); } if (RCC_CSR & RCC_CSR_SFTRSTF) { puts_debug(" software"); } if (RCC_CSR & RCC_CSR_PORRSTF) { puts_debug(" POR/PDR"); } if (RCC_CSR & RCC_CSR_PINRSTF) { puts_debug(" pin"); } puts_debug("\n"); RCC_CSR |= RCC_CSR_RMVF; // clear reset flags } #endif // setup RTC puts_debug("setup RTC: "); rcc_periph_clock_enable(RCC_RTC); // enable clock for RTC peripheral if (!(RCC_BDCR && RCC_BDCR_RTCEN)) { // the RTC has not been configured yet pwr_disable_backup_domain_write_protect(); // disable backup protection so we can set the RTC clock source rtc_unlock(); // enable writing RTC registers #if defined(MINIF401) rcc_osc_on(RCC_LSE); // enable LSE clock while (!rcc_is_osc_ready(RCC_LSE)); // wait until clock is ready rtc_set_prescaler(256, 128); // set clock prescaler to 32768 RCC_BDCR = (RCC_BDCR & ~(RCC_BDCR_RTCSEL_MASK << RCC_BDCR_RTCSEL_SHIFT)) | (RCC_BDCR_RTCSEL_LSE << RCC_BDCR_RTCSEL_SHIFT); // select LSE as RTC clock source #else rcc_osc_on(RCC_LSI); // enable LSI clock while (!rcc_is_osc_ready(RCC_LSI)); // wait until clock is ready rtc_set_prescaler(250, 128); // set clock prescaler to 32000 RCC_BDCR = (RCC_BDCR & ~(RCC_BDCR_RTCSEL_MASK << RCC_BDCR_RTCSEL_SHIFT)) | (RCC_BDCR_RTCSEL_LSI << RCC_BDCR_RTCSEL_SHIFT); // select LSI as RTC clock source #endif RCC_BDCR |= RCC_BDCR_RTCEN; // enable RTC rtc_lock(); // protect RTC register against writing pwr_enable_backup_domain_write_protect(); // re-enable protection now that we configured the RTC clock } boot_time = rtc_to_seconds(); // remember the start time puts_debug("OK\n"); // setup wakeup timer for periodic checks puts_debug("setup wakeup: "); // RTC needs to be configured beforehand pwr_disable_backup_domain_write_protect(); // disable backup protection so we can write to the RTC registers rtc_unlock(); // enable writing RTC registers rtc_clear_wakeup_flag(); // clear flag for fresh start #if defined(MINIF401) rtc_set_wakeup_time((32768 / 2) / WAKEUP_FREQ - 1, RTC_CR_WUCLKSEL_RTC_DIV2); // set wakeup time based on LSE (keep highest precision, also enables the wakeup timer) #else rtc_set_wakeup_time((32000 / 2) / WAKEUP_FREQ - 1, RTC_CR_WUCLKSEL_RTC_DIV2); // set wakeup time based on LSI (keep highest precision, also enables the wakeup timer) #endif rtc_enable_wakeup_timer_interrupt(); // enable interrupt rtc_lock(); // disable writing RTC registers // important: do not re-enable backup_domain_write_protect, since this will prevent clearing flags (but RTC registers do not need to be unlocked) puts_debug("OK\n"); 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 gpio_set(GPIO_PORT(DRV8825_RESET_PIN), GPIO_PIN(DRV8825_RESET_PIN)); // power up driver 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_reset_request(GPIO_EXTI(DIAL_SWITCH_PIN)); // ensure the interrupt flag is cleared 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 WS2812b LED matrix: "); led_ws2812b_setup(); // configure peripheral for communication with WS2812b LEDs puts_debug("OK\n"); #if RGBPANEL_ENABLE puts_debug("setup RGB matrix: "); rgbpanel_setup(); puts_debug("OK\n"); #endif // 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"); puts_debug("setup ESP8266 Art-Net: "); sleep_ms(1000); // wit for ESP to boot radio_esp8266_setup(); // connect to WiFi network radio_esp8266_listen(true, 6454); // open UDP Art-Net puts_debug("OK"); // setup terminal terminal_prefix = ""; // set default prefix terminal_process = &process_command; // set central function to process commands terminal_setup(); // start terminal // 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); // draw welcome text 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); sleep_ms(3000); // show the text for a tiny bit // start main loop bool action = false; // if an action has been performed don't go to sleep button_flag = false; // reset button flag led_on(); // switch LED to indicate booting completed const char* scroll_text = "DACHBODEN ZEITMASCHINE"; int16_t scroll_pos = 64; while (true) { // infinite loop iwdg_reset(); // kick the dog if (user_input_available) { // user input is available action = true; // action has been performed led_toggle(); // toggle LED char c = user_input_get(); // store receive character terminal_send(c); // send received character to terminal } if (wakeup_flag) { // time to do periodic checks wakeup_flag = false; // clear flag #if RGBPANEL_ENABLE rgbpanel_clear(); #endif matrix_puts(false, 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; } //strip_rgbw(0, 0, 0, (64 - scroll_pos) * 100); } if (second_flag) { // one second passed second_flag = false; // clear flag led_toggle(); // toggle LED to indicate if main function is stuck } 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 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 action = true; } if (drv8825_reached) { puts("midnight reached\n"); speed = 0; // stop motor command_speed(&speed); // stop motor drv8825_goal = 0; // disable goal drv8825_reached = false; // clear flag action = true; // redo main loop } if (radio_esp8266_received_len) { 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); 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; } } } radio_esp8266_received_len = 0; // reset flag action = true; // redo main loop } if (action) { // go to sleep if nothing had to be done, else recheck for activity action = false; } else { __WFI(); // go to sleep } } // main loop } /** 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 } } /** 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 } if (!drv8825_reached && drv8825_goal && drv8825_steps == drv8825_goal) { // we reached the set goal drv8825_reached = true; // notify main loop } } } /** 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 reed_flag = true; // I don't know why, but the RGBPANEL_ISR does trigger this EXTI if (gpio_get(GPIO_PORT(DIAL_SWITCH_PIN), GPIO_PIN(DIAL_SWITCH_PIN))) { // be sure the pin is actually low return; } if (drv8825_steps > DIAL_CYCLE_STEPS / 4) { // ignore going away debounce dial_steps = drv8825_steps; // remember on which step we are drv8825_steps = 0; // restart step counter } }