From f6f69479a559da6f4d3a7f2e4e2c588beaa2ba05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Thu, 9 Feb 2017 12:45:16 +0100 Subject: [PATCH] add time readout with error handling --- main.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/main.c b/main.c index f2d85ed..0506386 100644 --- a/main.c +++ b/main.c @@ -173,7 +173,6 @@ void main(void) { rcc_clock_setup_in_hse_8mhz_out_72mhz(); // use 8 MHz high speed external clock to generate 72 MHz internal clock - #if DEBUG // enable functionalities for easier debug DBGMCU_CR |= DBGMCU_CR_IWDG_STOP; // stop independent watchdog counter when code is halted @@ -228,10 +227,15 @@ void main(void) // verify is external RTC is running if (rtc_ds1307_oscillator_disabled()) { printf("/!\\ RTC oscillator is disabled: the battery may be empty\n"); + rtc_ds1307_oscillator_enable(); // enable oscillator again } // display date uint8_t* rtc_ds1307_time = rtc_ds1307_read_time(); // get time/date from external RTC - printf("current date: 20%02u-%02u-%02u %02u:%02u:%02u\n", rtc_ds1307_time[6], rtc_ds1307_time[5], rtc_ds1307_time[4], rtc_ds1307_time[2], rtc_ds1307_time[1], rtc_ds1307_time[0]); + if (rtc_ds1307_time==NULL) { + fprintf(stderr,"could not get time from DS1307\n"); + } else { + printf("current date: 20%02u-%02u-%02u %02u:%02u:%02u\n", rtc_ds1307_time[6], rtc_ds1307_time[5], rtc_ds1307_time[4], rtc_ds1307_time[2], rtc_ds1307_time[1], rtc_ds1307_time[0]); + } // setup TM1637 printf("setup 7-segment display: "); @@ -307,8 +311,14 @@ void main(void) if ((ticks_time%(60))==0) { // one minute passed printf("uptime: %lu.%02lu:%02lu:%02lu\n", ticks_time/(60*60*24), (ticks_time/(60*60))%24, (ticks_time%(60*60))/60, (ticks_time%60)); // display uptime } - if (!led_tm1637_time(rtc_ds1307_read_minutes(), rtc_ds1307_read_seconds())) { - fprintf(stderr,"could not set time on TM1637\n"); + rtc_ds1307_time = rtc_ds1307_read_time(); // get time/date from external RTC + if (rtc_ds1307_time==NULL) { + fprintf(stderr,"could not get time from DS1307: resetting\n"); + rtc_ds1307_setup(); // resetting periph + } else { + if (!led_tm1637_time(rtc_ds1307_time[1], rtc_ds1307_time[0])) { + fprintf(stderr,"could not set time on TM1637\n"); + } } if (!led_tm1637_on()) { // switch on display fprintf(stderr,"could not switch on TM1637\n");