949 lines
42 KiB
C
949 lines
42 KiB
C
/** firmware to control the cool clock
|
|
* @file
|
|
* @author King Kévin <kingkevin@cuvoodoo.info>
|
|
* @copyright SPDX-License-Identifier: GPL-3.0-or-later
|
|
* @date 2016-2022
|
|
*/
|
|
|
|
/* standard libraries */
|
|
#include <stdint.h> // standard integer types
|
|
#include <stdlib.h> // standard utilities
|
|
#include <string.h> // string utilities
|
|
#include <time.h> // date/time utilities
|
|
#include <ctype.h> // utilities to check chars
|
|
|
|
/* STM32 (including CM3) libraries */
|
|
#include <libopencmsis/core_cm3.h> // Cortex M3 utilities
|
|
#include <libopencm3/cm3/scb.h> // vector table definition
|
|
#include <libopencm3/cm3/nvic.h> // interrupt utilities
|
|
#include <libopencm3/stm32/gpio.h> // general purpose input output library
|
|
#include <libopencm3/stm32/rcc.h> // real-time control clock library
|
|
#include <libopencm3/stm32/exti.h> // external interrupt utilities
|
|
#include <libopencm3/stm32/rtc.h> // real time clock utilities
|
|
#include <libopencm3/stm32/iwdg.h> // independent watchdog utilities
|
|
#include <libopencm3/stm32/dbgmcu.h> // debug utilities
|
|
#include <libopencm3/stm32/desig.h> // design utilities
|
|
#include <libopencm3/stm32/flash.h> // flash utilities
|
|
#include <libopencm3/stm32/timer.h> // timer library
|
|
#include <libopencm3/stm32/dma.h> // DMA library
|
|
#include <libopencm3/usb/dwc/otg_fs.h> // USB definitions
|
|
|
|
/* own libraries */
|
|
#include "global.h" // board definitions
|
|
#include "print.h" // printing utilities
|
|
#include "uart.h" // USART 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
|
|
|
|
/** watchdog period in ms */
|
|
#define WATCHDOG_PERIOD 10000
|
|
|
|
/** 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) */
|
|
/** maximum speed (in steps/s) before the motor stalls (found empirically)
|
|
* @note found empirically 300 @ 9V/180mA, 420 @ 12V/150mA
|
|
*/
|
|
#define DRV8825_SPEED_LIMIT 420U
|
|
|
|
// dials position info
|
|
#define DIAL_SWITCH_PIN PB3 /**< pin connected to reed switch, pulled low when the hour dial is nearby */
|
|
#define DIAL_CYCLE_STEPS 11904U /**< number of steps for the hour dial to make a round */
|
|
#define DIAL_MIDNIGHT_STEPS 6557U /**< number of steps after dial detection for dials to show midnight */
|
|
static volatile uint32_t dial_steps = 0; /**< set to drv8825_steps when dial is nearby */
|
|
|
|
// RGB matrix pins
|
|
#define RGBMATRIX_OE_PIN PB10 /**< pin to enable output (active low) */
|
|
#define RGBMATRIX_A_PIN PB0 /**< pin to select line */
|
|
#define RGBMATRIX_B_PIN PB1 /**< pin to select line */
|
|
#define RGBMATRIX_C_PIN PB2 /**< pin to select line */
|
|
#define RGBMATRIX_D_PIN PB3 /**< pin to select line */
|
|
#define RGBMATRIX_CLK_PIN PA0 /**< pin to generate clock for serial data */
|
|
#define RGBMATRIX_LAT_PIN PA1 /**< pin to latch data on rising edge */
|
|
#define RGBMATRIX_R1_PIN PA2 /**< pin to enable red color on top half */
|
|
#define RGBMATRIX_G1_PIN PA3 /**< pin to enable green color on top half */
|
|
#define RGBMATRIX_B1_PIN PA4 /**< pin to enable blue color on top half */
|
|
#define RGBMATRIX_R2_PIN PA5 /**< pin to enable red color on bottom half */
|
|
#define RGBMATRIX_G2_PIN PA6 /**< pin to enable green color on bottom half */
|
|
#define RGBMATRIX_B2_PIN PA7 /**< pin to enable blue color on bottom half */
|
|
|
|
#define RGBMATRIX_DMA DMA2 /**< DMA used to send data to the RGB matrix (only DMA2 can be used for memory-to-memory transfer) */
|
|
#define RGBMATRIX_RCC_DMA RCC_DMA2 /**< RCC for DMA used for the RGB matrix */
|
|
#define RGBMATRIX_STREAM DMA_STREAM1 /**< stream used to send data to the RGB matrix (any stream can be used for memory-to-memory transfer) */
|
|
#define RGBMATRIX_CHANNEL DMA_SxCR_CHSEL_0 /**< channel used to send data to the RGB matrix (any channel can be used for memory-to-memory transfer) */
|
|
#define RGBMATRIX_IRQ NVIC_DMA2_STREAM1_IRQ /**< IRQ for when a line transfer is complete */
|
|
#define RGBMATRIX_ISR dma2_stream1_isr /**< ISR for when a line transfer is complete */
|
|
#define RGBMATRIX_HEIGHT 32 /**< number of rows in the RGB matrix */
|
|
#define RGBMATRIX_WIDTH 64 /**< number of columns in the RGB matrix */
|
|
static uint8_t rgbmatrix_data[RGBMATRIX_HEIGHT / 2][RGBMATRIX_WIDTH * 2]; /**< data to be sent to RGB matrix (one byte includes upper and lower half values, each byte has 2 clock edges) */
|
|
#define RGBMATRIX_TIMER 3 /**< timer to update lines */
|
|
|
|
/** set motor speed and direction
|
|
* @param[in] speed speed (in Hz) and direction (sign)
|
|
*/
|
|
static void drv8825_speed(int16_t speed)
|
|
{
|
|
if (0 == speed) {
|
|
timer_disable_counter(TIM(DRV8825_STEP_TIMER)); // stop PWM output
|
|
gpio_set(GPIO_PORT(DRV8825_ENABLE_PIN), GPIO_PIN(DRV8825_ENABLE_PIN)); // disable motor
|
|
drv8825_direction = 0; // remember we stopped
|
|
} else {
|
|
if (speed > 0) {
|
|
gpio_clear(GPIO_PORT(DRV8825_DIRECTION_PIN), GPIO_PIN(DRV8825_DIRECTION_PIN)); // set clockwise
|
|
drv8825_direction = 1; // remember we go clockwise
|
|
} else {
|
|
gpio_set(GPIO_PORT(DRV8825_DIRECTION_PIN), GPIO_PIN(DRV8825_DIRECTION_PIN)); // set counter-clockwise
|
|
drv8825_direction = -1; // remember we go counter-clockwise
|
|
speed = -speed; // get positive speed
|
|
}
|
|
if (speed > (int16_t)DRV8825_SPEED_LIMIT) { // enforce upper limit
|
|
speed = DRV8825_SPEED_LIMIT;
|
|
}
|
|
timer_set_prescaler(TIM(DRV8825_STEP_TIMER), rcc_ahb_frequency / (UINT16_MAX * speed) - 1); // set the clock frequency
|
|
gpio_clear(GPIO_PORT(DRV8825_ENABLE_PIN), GPIO_PIN(DRV8825_ENABLE_PIN)); // enable motor
|
|
timer_enable_counter(TIM(DRV8825_STEP_TIMER)); // start PWM output
|
|
}
|
|
}
|
|
|
|
/** switch off all LEDs on the RGB matrix */
|
|
static void rgbmatrix_clear(void)
|
|
{
|
|
for (uint8_t i = 0; i < LENGTH(rgbmatrix_data); i++) { // for each line
|
|
for (uint8_t j = 0; j < LENGTH(rgbmatrix_data[0]); j += 2) { // for each clock cycle
|
|
rgbmatrix_data[i][j + 0] = 0; // create clock falling edge
|
|
rgbmatrix_data[i][j + 1] = 1; // create clock rising edge
|
|
}
|
|
rgbmatrix_data[i][LENGTH(rgbmatrix_data[0]) - 1] |= (1 << 1); // latch data (next line will remove the latch)
|
|
}
|
|
}
|
|
|
|
/** set color of the LED on the RGB matrix
|
|
* @param[in] x horizontal position (0 = left)
|
|
* @param[in] y vertical position (0 = top)
|
|
* @param[in] r if the red LED should be on
|
|
* @param[in] g if the green LED should be on
|
|
* @param[in] b if the blue LED should be on
|
|
*/
|
|
static void rgbmatrix_set(uint8_t x, uint8_t y, bool r, bool g, bool b)
|
|
{
|
|
if (x >= RGBMATRIX_WIDTH) {
|
|
return;
|
|
}
|
|
if (y >= RGBMATRIX_HEIGHT) {
|
|
return;
|
|
}
|
|
const uint8_t row = y % (RGBMATRIX_HEIGHT / 2); // get the actual line/row
|
|
const uint8_t col = x * 2; // get the actual column
|
|
uint8_t data = 0; // there we will set the color bits
|
|
if (y < (RGBMATRIX_HEIGHT / 2)) {
|
|
data = rgbmatrix_data[row][col] & 0xe0; // keep lower line colors
|
|
if (r) {
|
|
data |= (1 << 2);
|
|
}
|
|
if (g) {
|
|
data |= (1 << 3);
|
|
}
|
|
if (b) {
|
|
data |= (1 << 4);
|
|
}
|
|
} else {
|
|
data = rgbmatrix_data[row][col] & 0x1c; // keep upper line colors
|
|
if (r) {
|
|
data |= (1 << 5);
|
|
}
|
|
if (g) {
|
|
data |= (1 << 6);
|
|
}
|
|
if (b) {
|
|
data |= (1 << 7);
|
|
}
|
|
}
|
|
// set data on low edge
|
|
rgbmatrix_data[row][col + 0] &= 0x3; // clear color data (don't touch clock and latch data)
|
|
rgbmatrix_data[row][col + 0] |= data; // set the LED data
|
|
// set data on high edge
|
|
rgbmatrix_data[row][col + 1] &= 0x3; // clear color data (don't touch clock and latch data)
|
|
rgbmatrix_data[row][col + 1] |= data; // set the LED data on clock high edge
|
|
}
|
|
|
|
/** draw character on RGB matrix
|
|
* @param[in] x horizontal position (0 = left)
|
|
* @param[in] y vertical position (0 = top)
|
|
* @param[in] c character to draw
|
|
* @param[in] font font to use
|
|
* @param[in] r if the character should be drawn in red
|
|
* @param[in] g if the character should be drawn in green
|
|
* @param[in] b if the character should be drawn in blue
|
|
*/
|
|
static void rgbmatrix_putc(uint8_t x, uint8_t y, char c, enum font_name font, bool red, bool green, bool blue)
|
|
{
|
|
// sanity checks
|
|
if (x >= RGBMATRIX_WIDTH) {
|
|
return;
|
|
}
|
|
if (y >= RGBMATRIX_HEIGHT) {
|
|
return;
|
|
}
|
|
if (font >= FONT_MAX) {
|
|
return;
|
|
}
|
|
if (c < ' ' || c > '~') {
|
|
return;
|
|
}
|
|
|
|
// draw character on buffer
|
|
for (uint8_t col = 0; col < fonts[font].width; col++) {
|
|
const uint16_t column = fonts[font].glyphs[(c - ' ') * fonts[font].width + col];
|
|
for (uint8_t row = 0; row < fonts[font].height; row++) {
|
|
const bool dot = (column >> (fonts[font].height - 1 - row)) & 0x01;
|
|
if (dot) {
|
|
rgbmatrix_set(x + col, y + row, red, green, blue);
|
|
} else {
|
|
rgbmatrix_set(x + col, y + row, false, false, false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
uart_putchar_nonblocking('\r'); // send CR over USART
|
|
usb_cdcacm_putchar('\r'); // send CR over USB
|
|
length++; // remember we printed 1 character
|
|
}
|
|
}
|
|
uart_putchar_nonblocking(c); // send byte over USART
|
|
usb_cdcacm_putchar(c); // send byte over USB
|
|
length++; // remember we printed 1 character
|
|
last_c = c; // remember last character
|
|
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");
|
|
rgbmatrix_set(0, 0, true, false, false);
|
|
rgbmatrix_set(1, 0, false, true, false);
|
|
rgbmatrix_set(2, 0, false, false, true);
|
|
rgbmatrix_set(0, 1, true, false, false);
|
|
rgbmatrix_set(1, 2, false, true, false);
|
|
rgbmatrix_set(2, 3, false, false, true);
|
|
}
|
|
|
|
/** 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,
|
|
},
|
|
};
|
|
|
|
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
|
|
|
|
board_setup(); // setup board
|
|
uart_setup(); // setup USART (for printing)
|
|
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
|
|
puts_debug("OK\n");
|
|
|
|
puts_debug("setup dial position: ");
|
|
// dial position detection pin
|
|
rcc_periph_clock_enable(GPIO_RCC(DIAL_SWITCH_PIN)); // enable clock for GPIO port peripheral
|
|
gpio_mode_setup(GPIO_PORT(DIAL_SWITCH_PIN), GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO_PIN(DIAL_SWITCH_PIN)); // set GPIO to input and pull up
|
|
exti_select_source(GPIO_EXTI(DIAL_SWITCH_PIN), GPIO_PORT(DIAL_SWITCH_PIN)); // mask external interrupt of this pin only for this port
|
|
exti_set_trigger(GPIO_EXTI(DIAL_SWITCH_PIN), EXTI_TRIGGER_FALLING); // trigger when magnet on dial is nearby
|
|
exti_enable_request(GPIO_EXTI(DIAL_SWITCH_PIN)); // enable external interrupt
|
|
nvic_enable_irq(GPIO_NVIC_EXTI_IRQ(DIAL_SWITCH_PIN)); // enable interrupt
|
|
puts_debug("OK\n");
|
|
|
|
puts_debug("setup RGB matrix: ");
|
|
// configure pin for output enable
|
|
rcc_periph_clock_enable(GPIO_RCC(RGBMATRIX_OE_PIN)); // enable clock for GPIO port peripheral
|
|
gpio_set(GPIO_PORT(RGBMATRIX_OE_PIN), GPIO_PIN(RGBMATRIX_OE_PIN)); // disable output
|
|
gpio_set_output_options(GPIO_PORT(RGBMATRIX_OE_PIN), GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN(RGBMATRIX_OE_PIN)); // set fast edge
|
|
gpio_mode_setup(GPIO_PORT(RGBMATRIX_OE_PIN), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(RGBMATRIX_OE_PIN)); // set pin as output
|
|
// configure pins for data and clock lines
|
|
const uint32_t rgbmatrix_serial_port = GPIO_PORT(RGBMATRIX_LAT_PIN); // common port for pins controlling the serial data
|
|
const uint16_t rgbmatrix_serial_pins = GPIO_PIN(RGBMATRIX_LAT_PIN) | GPIO_PIN(RGBMATRIX_CLK_PIN) | GPIO_PIN(RGBMATRIX_R1_PIN) | GPIO_PIN(RGBMATRIX_G1_PIN) | GPIO_PIN(RGBMATRIX_B1_PIN) | GPIO_PIN(RGBMATRIX_R2_PIN) | GPIO_PIN(RGBMATRIX_G2_PIN) | GPIO_PIN(RGBMATRIX_B2_PIN); // pins controlling the serial data
|
|
rcc_periph_clock_enable(GPIO_RCC(RGBMATRIX_LAT_PIN)); // enable clock for GPIO port peripheral
|
|
gpio_clear(rgbmatrix_serial_port, rgbmatrix_serial_pins); // disable LEDs
|
|
gpio_set_output_options(rgbmatrix_serial_port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, rgbmatrix_serial_pins); // set fast edge
|
|
gpio_mode_setup(rgbmatrix_serial_port, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, rgbmatrix_serial_pins); // set pin as output
|
|
// configure pins for address lines
|
|
rcc_periph_clock_enable(RCC_GPIOB); // enable clock for GPIO port peripheral
|
|
gpio_clear(RCC_GPIOB, GPIO0 | GPIO1 | GPIO2 | GPIO3); // unselect line
|
|
gpio_set_output_options(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO0 | GPIO1 | GPIO2 | GPIO3); // set fast edge
|
|
gpio_mode_setup(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO0 | GPIO1 | GPIO2 | GPIO3); // set pin as output
|
|
// configure DMA to sent line data
|
|
// because there is no peripheral request for data, this is a memory to memory transfer
|
|
rcc_periph_clock_enable(RGBMATRIX_RCC_DMA); // enable clock for DMA peripheral (any DMA and channel can be used)
|
|
dma_disable_stream(RGBMATRIX_DMA, RGBMATRIX_STREAM); // disable stream before re-configuring
|
|
while (DMA_SCR(RGBMATRIX_DMA, RGBMATRIX_STREAM) & DMA_SxCR_EN); // wait until transfer is finished before we can reconfigure
|
|
dma_stream_reset(RGBMATRIX_DMA, RGBMATRIX_STREAM); // use default values
|
|
//dma_set_peripheral_address(RGBMATRIX_DMA, RGBMATRIX_STREAM, (uint32_t)&rgbmatrix_data[0]); // set memory to read from (for memory-to-memory transfer, the source is the peripheral)
|
|
dma_set_peripheral_size(RGBMATRIX_DMA, RGBMATRIX_STREAM, DMA_SxCR_PSIZE_8BIT); // we only write the 8 first bit
|
|
dma_enable_peripheral_increment_mode(RGBMATRIX_DMA, RGBMATRIX_STREAM); // increment address of memory to read
|
|
dma_set_memory_address(RGBMATRIX_DMA, RGBMATRIX_STREAM, (uint32_t) &GPIOA_ODR); // set GPIOA as destination (for memory-to-memory transfer, the destination is the memory)
|
|
dma_set_memory_size(RGBMATRIX_DMA, RGBMATRIX_STREAM, DMA_SxCR_MSIZE_8BIT); // read 8 bits for transfer
|
|
dma_disable_memory_increment_mode(RGBMATRIX_DMA, RGBMATRIX_STREAM); // don't increment GPIO address
|
|
dma_set_number_of_data(RGBMATRIX_DMA, RGBMATRIX_STREAM, LENGTH(rgbmatrix_data[0])); // set transfer size (one line)
|
|
dma_channel_select(RGBMATRIX_DMA, RGBMATRIX_STREAM, RGBMATRIX_CHANNEL); // set the channel for this stream
|
|
dma_set_transfer_mode(RGBMATRIX_DMA, RGBMATRIX_STREAM, DMA_SxCR_DIR_MEM_TO_MEM); // set transfer from memory to memory
|
|
dma_set_priority(RGBMATRIX_DMA, RGBMATRIX_STREAM, DMA_SxCR_PL_LOW); // there is no need to rush
|
|
rgbmatrix_clear(); // clear matrix
|
|
gpio_clear(GPIO_PORT(RGBMATRIX_OE_PIN), GPIO_PIN(RGBMATRIX_OE_PIN)); // enable output
|
|
|
|
// configure timer to go through rows/lines
|
|
rcc_periph_clock_enable(RCC_TIM(RGBMATRIX_TIMER)); // enable clock for timer domain
|
|
rcc_periph_reset_pulse(RST_TIM(RGBMATRIX_TIMER)); // reset timer state
|
|
timer_set_mode(TIM(RGBMATRIX_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock, edge alignment (simple count), and count up
|
|
timer_set_prescaler(TIM(RGBMATRIX_TIMER), 2 - 1); // hand tuned prescale to minimize inter-line ghosting
|
|
timer_set_period(TIM(RGBMATRIX_TIMER), 0x9fff - 1); // hand tuned period to minimize inter-line ghosting
|
|
timer_clear_flag(TIM(RGBMATRIX_TIMER), TIM_SR_UIF); // clear update (overflow) flag
|
|
timer_update_on_overflow(TIM(RGBMATRIX_TIMER)); // only use counter overflow as UEV source (use overflow as next line update indication)
|
|
timer_enable_irq(TIM(RGBMATRIX_TIMER), TIM_DIER_UIE); // enable update interrupt for timer
|
|
nvic_enable_irq(NVIC_TIM_IRQ(RGBMATRIX_TIMER)); // catch interrupt in service routine
|
|
timer_enable_counter(TIM(RGBMATRIX_TIMER)); // start timer to update RGB matrix
|
|
|
|
puts_debug("OK\n");
|
|
|
|
// 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
|
|
rgbmatrix_putc(1, 1, 'D', FONT_KING14, false, true, false);
|
|
|
|
// 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
|
|
uint16_t matrix_led = 0;
|
|
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 (second_flag) { // one second passed
|
|
second_flag = false; // clear flag
|
|
led_toggle(); // toggle LED to indicate if main function is stuck
|
|
rgbmatrix_set(matrix_led % RGBMATRIX_WIDTH, matrix_led / RGBMATRIX_WIDTH, false, false, false); // disable current LED
|
|
matrix_led += 4; // go to next LED
|
|
rgbmatrix_set(matrix_led % RGBMATRIX_WIDTH, matrix_led / RGBMATRIX_WIDTH, true, false, false); // enable next LED
|
|
}
|
|
if (0 == gpio_get(GPIO_PORT(DRV8825_FAULT_PIN), GPIO_PIN(DRV8825_FAULT_PIN))) { // DRV8825 stepper motor error reports error
|
|
gpio_set(GPIO_PORT(DRV8825_ENABLE_PIN), GPIO_PIN(DRV8825_ENABLE_PIN)); // disable motor
|
|
gpio_clear(GPIO_PORT(DRV8825_RESET_PIN), GPIO_PIN(DRV8825_RESET_PIN)); // put motor to sleep
|
|
if (!drv8825_fault) {
|
|
puts("DRV8825 fault detected\n");
|
|
drv8825_fault = true; // remember new fault
|
|
}
|
|
}
|
|
if (dial_steps) { // hour dial position detected
|
|
if (drv8825_steps >= DIAL_MIDNIGHT_STEPS) { // wait for dial to reach midnight
|
|
speed = 0; // stop motor
|
|
command_speed(&speed); // stop motor
|
|
dial_steps = 0; // restart position counter
|
|
puts("midnight reached\n");
|
|
}
|
|
}
|
|
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
|
|
}
|
|
}
|
|
}
|
|
|
|
/** ISR triggered when hour dial is near reed switch
|
|
* @note surprisingly there is very little bouncing
|
|
*/
|
|
void GPIO_EXTI_ISR(DIAL_SWITCH_PIN)(void)
|
|
{
|
|
exti_reset_request(GPIO_EXTI(DIAL_SWITCH_PIN)); // reset interrupt
|
|
if (drv8825_steps > dial_steps + 1000) { // ignore going away debounce
|
|
dial_steps = drv8825_steps; // remember on which step we are
|
|
drv8825_steps = 0; // restart step counter
|
|
}
|
|
}
|
|
|
|
/** interrupt service routine called to update next line of RGB matrix
|
|
* @note ideally the next line should be updated when the current one is complete, but the DMA is too fast.
|
|
* @note switching lines too fast causes inter-line ghosting of the LEDs (on the same column), due to capacitance and driver switching limitations
|
|
*/
|
|
void TIM_ISR(RGBMATRIX_TIMER)(void)
|
|
{
|
|
if (timer_get_flag(TIM(RGBMATRIX_TIMER), TIM_SR_UIF)) { // update event happened
|
|
timer_clear_flag(TIM(RGBMATRIX_TIMER), TIM_SR_UIF); // clear flag
|
|
if (DMA_SCR(RGBMATRIX_DMA, RGBMATRIX_STREAM) & DMA_SxCR_EN) { // DMA is not complete
|
|
return;
|
|
}
|
|
static uint8_t rgbmatrix_line = 0; // line being transferred
|
|
rgbmatrix_line = (rgbmatrix_line + 1) % (RGBMATRIX_HEIGHT / 2); // go to next line (two lines are updated at once)
|
|
GPIOB_ODR = (GPIOB_ODR & 0xfff0) + rgbmatrix_line; // select line (line on lower and upper half are updated at once)
|
|
dma_set_peripheral_address(RGBMATRIX_DMA, RGBMATRIX_STREAM, (uint32_t)&rgbmatrix_data[rgbmatrix_line]); // set memory containing line data to be transferred (for memory-to-memory transfer, the source is the peripheral)
|
|
dma_clear_interrupt_flags(RGBMATRIX_DMA, RGBMATRIX_STREAM, DMA_TCIF); // even if interrupts are not enabled, clearing this flag is required to start the DMA (I don't know why)
|
|
dma_enable_stream(RGBMATRIX_DMA, RGBMATRIX_STREAM); // start sending next line
|
|
}
|
|
}
|