diff --git a/Src/main.c b/Src/main.c index ae648e1..83ba2ba 100644 --- a/Src/main.c +++ b/Src/main.c @@ -143,6 +143,7 @@ osStaticTimerDef_t beaconTimer4ControlBlock; /* USER CODE BEGIN PV */ /* Private variables ---------------------------------------------------------*/ +int lost_power = 0; int reset_requested = 0; char serial_number[25]; char serial_number_64[17] = {0}; @@ -182,6 +183,8 @@ void configure_gpio_for_stop(void) __attribute__((noinline)); void power_down_vdd(void); void configure_wakeup_gpio(void); void enable_debug_gpio(void); +void init_rtc_date_time(void); +void init_rtc_alarm(void); /* USER CODE END PFP */ @@ -517,8 +520,11 @@ int main(void) if (HAL_OPAMP_Start(&hopamp1) != HAL_OK) Error_Handler(); if (HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED) != HAL_OK) Error_Handler(); - // Initialize the BM78 Bluetooth module the first time we boot. - if (!bm78_initialized()) bm78_initialize(); + // Initialize the BM78 Bluetooth module and the RTC date/time the first time we boot. + if (!bm78_initialized()) { + bm78_initialize(); + // init_rtc_date_time(); + } else bm78_wait_until_ready(); init_ioport(); @@ -893,9 +899,6 @@ static void MX_RTC_Init(void) /* USER CODE END RTC_Init 0 */ - RTC_TimeTypeDef sTime; - RTC_DateTypeDef sDate; - RTC_AlarmTypeDef sAlarm; /* USER CODE BEGIN RTC_Init 1 */ @@ -916,51 +919,7 @@ static void MX_RTC_Init(void) _Error_Handler(__FILE__, __LINE__); } - /**Initialize RTC and set the Time and Date - */ - sTime.Hours = 0x0; - sTime.Minutes = 0x0; - sTime.Seconds = 0x0; - sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; - sTime.StoreOperation = RTC_STOREOPERATION_RESET; - if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK) - { - _Error_Handler(__FILE__, __LINE__); - } - - sDate.WeekDay = RTC_WEEKDAY_MONDAY; - sDate.Month = RTC_MONTH_JANUARY; - sDate.Date = 0x1; - sDate.Year = 0x0; - - if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK) - { - _Error_Handler(__FILE__, __LINE__); - } - - /**Enable the Alarm A - */ - sAlarm.AlarmTime.Hours = 0x0; - sAlarm.AlarmTime.Minutes = 0x0; - sAlarm.AlarmTime.Seconds = 0x0; - sAlarm.AlarmTime.SubSeconds = 0x0; - sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; - sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET; - sAlarm.AlarmMask = RTC_ALARMMASK_NONE; - sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL; - sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE; - sAlarm.AlarmDateWeekDay = 0x1; - sAlarm.Alarm = RTC_ALARM_A; - if (HAL_RTC_SetAlarm(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK) - { - _Error_Handler(__FILE__, __LINE__); - } - - /**Enable the Alarm B - */ - sAlarm.AlarmDateWeekDay = 0x1; - sAlarm.Alarm = RTC_ALARM_B; - + init_rtc_date_time(); } /* TIM1 init function */ @@ -1303,6 +1262,66 @@ int _write(int file, char *ptr, int len) { } #endif +void init_rtc_date_time() +{ + if (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) != 0) return; + + RTC_TimeTypeDef sTime; + RTC_DateTypeDef sDate; + + /**Initialize RTC and set the Time and Date + */ + sTime.Hours = 0x0; + sTime.Minutes = 0x0; + sTime.Seconds = 0x0; + sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; + sTime.StoreOperation = RTC_STOREOPERATION_RESET; + sTime.TimeFormat = RTC_HOURFORMAT_24; + if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK) + { + _Error_Handler(__FILE__, __LINE__); + } + + sDate.WeekDay = RTC_WEEKDAY_MONDAY; + sDate.Month = RTC_MONTH_JANUARY; + sDate.Date = 0x1; + sDate.Year = 0x0; + + if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK) + { + _Error_Handler(__FILE__, __LINE__); + } +} + +void init_rtc_alarm() +{ + RTC_AlarmTypeDef sAlarm; + + /**Enable the Alarm A + */ + sAlarm.AlarmTime.Hours = 0x0; + sAlarm.AlarmTime.Minutes = 0x0; + sAlarm.AlarmTime.Seconds = 0x0; + sAlarm.AlarmTime.SubSeconds = 0x0; + sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; + sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET; + sAlarm.AlarmMask = RTC_ALARMMASK_NONE; + sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL; + sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE; + sAlarm.AlarmDateWeekDay = 0x1; + sAlarm.Alarm = RTC_ALARM_A; + if (HAL_RTC_SetAlarm(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK) + { + _Error_Handler(__FILE__, __LINE__); + } + + /**Enable the Alarm B + */ + sAlarm.AlarmDateWeekDay = 0x1; + sAlarm.Alarm = RTC_ALARM_B; + +} + /* USER CODE END 4 */ /* USER CODE BEGIN Header_StartDefaultTask */