diff --git a/main.c b/main.c index 5f7a1ff..aa7c37c 100644 --- a/main.c +++ b/main.c @@ -65,14 +65,17 @@ volatile uint16_t standby_timer = 0; /**< number of seconds since last wake-up/a #define FRAME_RATE 25 /**< frame rate */ volatile uint8_t frame_count = 0; /**< number of frames passed */ -#define MUX_EN_PORT B -#define MUX_EN_PIN 9 -#define MUX_S0_PORT B -#define MUX_S0_PIN 3 -#define MUX_S1_PORT B -#define MUX_S1_PIN 4 -#define MUX_S2_PORT B -#define MUX_S2_PIN 5 +#define MUX_EN_PORT B /**< port to enable multiplexer */ +#define MUX_EN_PIN 9 /**< pin to enable multiplexer */ +#define MUX_S0_PORT B /**< port to select multiplexer output */ +#define MUX_S0_PIN 3 /**< pin to select multiplexer output */ +#define MUX_S1_PORT B /**< port to select multiplexer output */ +#define MUX_S1_PIN 4 /**< pin to select multiplexer output */ +#define MUX_S2_PORT B /**< port to select multiplexer output */ +#define MUX_S2_PIN 5 /**< pin to select multiplexer output */ + +#define POWER_PORT B /**< port to control power of all devices (including this micro-controller) */ +#define POWER_PIN 8 /**< pin to control power of all devices (including this micro-controller) */ /** user input command */ static char command[32] = {0}; @@ -243,6 +246,11 @@ void main(void) { rcc_clock_setup_in_hse_8mhz_out_72mhz(); // use 8 MHz high speed external clock to generate 72 MHz internal clock + // keep power on a soon as possible + rcc_periph_clock_enable(RCC_GPIO(POWER_PORT)); // enable clock for GPIO + gpio_set_mode(GPIO(POWER_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(POWER_PIN)); // set as output to control power + gpio_set(GPIO(POWER_PORT), GPIO(POWER_PIN)); // enable power by saturating nMOS controlling power + #if DEBUG // enable functionalities for easier debug DBGMCU_CR |= DBGMCU_CR_IWDG_STOP; // stop independent watchdog counter when code is halted @@ -260,7 +268,7 @@ void main(void) usart_setup(); // setup USART for user communication cdcacm_setup(); // setup USB ACM (serial) for user communication gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON,0); // disable JTAG (but leave SWD on) since we need most of the GPIOs - printf("welcome to the STM32F1 CuVoodoo clapperboard\n"); // print welcome message + printf("welcome to the CuVoodoo clapperboard\n"); // print welcome message #if !(DEBUG) // show watchdog information @@ -274,13 +282,6 @@ void main(void) } #endif - // enable wake up - rcc_periph_clock_enable(RCC_PWR); // enable power domain clock - if (pwr_get_wakeup_flag()) { - printf("woke up from standby\n"); - } - pwr_enable_wakeup_pin(); - // disable internal RTC printf("disable internal RTC: "); rcc_periph_clock_enable(RCC_PWR); // enable power domain clock @@ -337,10 +338,7 @@ void main(void) for (uint8_t tm1637=0; tm1637<7; tm1637++) { mux_select(tm1637); if (!led_tm1637_time(88,88)) { // test TM1637 display - printf("could not send time to TM1637 %u\n", tm1637); - } - if (!led_tm1637_on()) { // switch on TM1637 display - printf("could not switch on TM1637 %u\n", tm1637); + printf("could not test TM1637 %u\n", tm1637); } } led_max7219_setup(2); // setup MAX7219 @@ -351,9 +349,6 @@ void main(void) } for (uint8_t tm1637=0; tm1637<7; tm1637++) { mux_select(tm1637); - if (!led_tm1637_text(" ")) { // clear display - printf("could not clear on TM1637 %u\n", tm1637); - } if (!led_tm1637_off()) { // switch off display printf("could not switch off TM1637 %u\n", tm1637); } @@ -435,29 +430,41 @@ void main(void) while (frame_flag) { // a frame has passed frame_flag = false; // reset flag action = true; // action has been performed - uint8_t dots = 0x40; // dot for minutes on display + char time[] = "00000000"; // time to display + time[0] += (rtc_ds1307_time[2]/10)%10; // display hours + time[1] += (rtc_ds1307_time[2])%10; // display hours + time[1] |= 0x80; // dot for minutes on display + time[2] += (rtc_ds1307_time[1]/10)%10; // display minutes + time[3] += (rtc_ds1307_time[1])%10; // display minutes if (0==(rtc_seconds%2)) { // even seconds - dots |= 0x10; // add dot for seconds + time[3] |= 0x80; // add dot for seconds } + time[4] += (rtc_seconds/10)%10; // display seconds + time[5] += (rtc_seconds)%10; // display seconds if (0==(frame_count%2)) { // even frames - dots |= 0x04; // add dot for frame + time[5] |= 0x80; // add dot for frame } - led_max7219_number(rtc_ds1307_time[2]*1000000+rtc_ds1307_time[1]*10000+rtc_seconds*100+(frame_count%100), dots, 0); // display frame time on 1nd display + time[6] += (frame_count/10)%10; // display frame + time[7] += (frame_count)%10; // display frame + led_max7219_text(time, 0); // display frame time on 1st display } while (rtc_tick_flag) { // the external RTC ticked rtc_tick_flag = false; // reset flag action = true; // action has been performed led_toggle(); // toggle LED (good to indicate if main function is stuck) -/* if (standby_timer>=STANDBY_TIMEOUT) { // standby timeout complete // go into standby mode - printf("standing by\n"); - led_max7219_off(0xff); // switch displays off - pwr_clear_wakeup_flag(); // clear flag + printf("shutting down\n"); + led_max7219_off(0xff); // switch off MAX7219 displays + for (uint8_t tm1637=0; tm1637<7; tm1637++) { // switch off TM1637 displays + mux_select(tm1637); // selecting TM1637 display + led_tm1637_off(); // switch off TM1637 display + } + gpio_clear(GPIO(POWER_PORT), GPIO(POWER_PIN)); // switch power of by disconnecting from battery SCB_SCR |= SCB_SCR_SLEEPDEEP; // enable deep sleep pwr_set_standby_mode(); // go to deep sleep + while (true); // we should be shut down at this point } -*/ if (rtc_seconds>=60) { // one minute passed rtc_ds1307_time = rtc_ds1307_read_time(); // get time/date from external RTC if (rtc_ds1307_time==NULL) { @@ -475,9 +482,6 @@ void main(void) if (!led_tm1637_time(rtc_ds1307_time[1],rtc_seconds+tm1637)) { // test TM1637 display printf("could not send time to TM1637 %u\n", tm1637); } - if (!led_tm1637_on()) { // switch on TM1637 display - printf("could not switch on TM1637 %u\n", tm1637); - } } } while (pwr_get_wakeup_flag()) { // someone is moving the clapperboard