stmhal: Clean up rtc.c a bit.
This commit is contained in:
parent
7533700393
commit
83407ad082
154
stmhal/rtc.c
154
stmhal/rtc.c
|
@ -7,15 +7,13 @@
|
|||
#include "qstr.h"
|
||||
#include "obj.h"
|
||||
#include "runtime.h"
|
||||
//#include "systick.h"
|
||||
#include "rtc.h"
|
||||
|
||||
RTC_HandleTypeDef RTCHandle;
|
||||
static machine_uint_t rtc_info;
|
||||
|
||||
#define RTC_INFO_USE_EXISTING (0)
|
||||
#define RTC_INFO_USE_LSE (1)
|
||||
#define RTC_INFO_USE_LSI (3)
|
||||
// rtc_info indicates various things about RTC startup
|
||||
// it's a bit of a hack at the moment
|
||||
static machine_uint_t rtc_info;
|
||||
|
||||
// Note: LSI is around (32KHz), these dividers should work either way
|
||||
// ck_spre(1Hz) = RTCCLK(LSE) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)
|
||||
|
@ -23,6 +21,10 @@ static machine_uint_t rtc_info;
|
|||
#define RTC_SYNCH_PREDIV (0x00ff)
|
||||
|
||||
#if 0
|
||||
#define RTC_INFO_USE_EXISTING (0)
|
||||
#define RTC_INFO_USE_LSE (1)
|
||||
#define RTC_INFO_USE_LSI (3)
|
||||
|
||||
void rtc_init(void) {
|
||||
// Enable the PWR clock
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
|
||||
|
@ -124,100 +126,82 @@ void rtc_init(void) {
|
|||
static void RTC_CalendarConfig(void);
|
||||
|
||||
void rtc_init(void) {
|
||||
/*##-1- Configure the RTC peripheral #######################################*/
|
||||
RTCHandle.Instance = RTC;
|
||||
RTCHandle.Instance = RTC;
|
||||
|
||||
/* Configure RTC prescaler and RTC data registers */
|
||||
/* RTC configured as follow:
|
||||
/* Configure RTC prescaler and RTC data registers */
|
||||
/* RTC configured as follow:
|
||||
- Hour Format = Format 24
|
||||
- Asynch Prediv = Value according to source clock
|
||||
- Synch Prediv = Value according to source clock
|
||||
- OutPut = Output Disable
|
||||
- OutPutPolarity = High Polarity
|
||||
- OutPutType = Open Drain */
|
||||
RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24;
|
||||
RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
|
||||
RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
|
||||
RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
|
||||
RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
|
||||
RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
|
||||
|
||||
- OutPutType = Open Drain */
|
||||
RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24;
|
||||
RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
|
||||
RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
|
||||
RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
|
||||
RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
|
||||
RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
|
||||
|
||||
machine_uint_t tick = HAL_GetTick();
|
||||
|
||||
if(HAL_RTC_Init(&RTCHandle) != HAL_OK)
|
||||
{
|
||||
/* Initialization Error */
|
||||
//Error_Handler();
|
||||
return;
|
||||
}
|
||||
|
||||
if (HAL_RTC_Init(&RTCHandle) != HAL_OK) {
|
||||
// init error
|
||||
rtc_info = 0xffff; // indicate error
|
||||
return;
|
||||
}
|
||||
|
||||
// record how long it took for the RTC to start up
|
||||
rtc_info = HAL_GetTick() - tick;
|
||||
|
||||
/*##-2- Check if Data stored in BackUp register0: No Need to reconfigure RTC#*/
|
||||
/* Read the Back Up Register 0 Data */
|
||||
if(HAL_RTCEx_BKUPRead(&RTCHandle, RTC_BKP_DR0) != 0x32F2)
|
||||
{
|
||||
/* Configure RTC Calendar */
|
||||
RTC_CalendarConfig();
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Check if the Power On Reset flag is set */
|
||||
if(__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET)
|
||||
{
|
||||
/* Turn on LED2: Power on reset occured */
|
||||
//BSP_LED_On(LED2);
|
||||
// check data stored in BackUp register0
|
||||
if (HAL_RTCEx_BKUPRead(&RTCHandle, RTC_BKP_DR0) != 0x32f2) {
|
||||
// fresh reset; configure RTC Calendar
|
||||
RTC_CalendarConfig();
|
||||
} else {
|
||||
// RTC was previously set, so leave it alon
|
||||
if(__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET) {
|
||||
// power on reset occured
|
||||
rtc_info |= 0x10000;
|
||||
}
|
||||
if(__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET) {
|
||||
// external reset occured
|
||||
rtc_info |= 0x20000;
|
||||
}
|
||||
// Clear source Reset Flag
|
||||
__HAL_RCC_CLEAR_RESET_FLAGS();
|
||||
}
|
||||
/* Check if Pin Reset flag is set */
|
||||
if(__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET)
|
||||
{
|
||||
/* Turn on LED4: External reset occured */
|
||||
//BSP_LED_On(LED4);
|
||||
}
|
||||
/* Clear source Reset Flag */
|
||||
__HAL_RCC_CLEAR_RESET_FLAGS();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static void RTC_CalendarConfig(void)
|
||||
{
|
||||
RTC_DateTypeDef sdatestructure;
|
||||
RTC_TimeTypeDef stimestructure;
|
||||
|
||||
/*##-1- Configure the Date #################################################*/
|
||||
/* Set Date: Tuesday February 18th 2014 */
|
||||
sdatestructure.Year = 0x14;
|
||||
sdatestructure.Month = RTC_MONTH_FEBRUARY;
|
||||
sdatestructure.Date = 0x18;
|
||||
sdatestructure.WeekDay = RTC_WEEKDAY_TUESDAY;
|
||||
|
||||
if(HAL_RTC_SetDate(&RTCHandle,&sdatestructure,FORMAT_BCD) != HAL_OK)
|
||||
{
|
||||
/* Initialization Error */
|
||||
//Error_Handler();
|
||||
return;
|
||||
}
|
||||
|
||||
/*##-2- Configure the Time #################################################*/
|
||||
/* Set Time: 02:00:00 */
|
||||
stimestructure.Hours = 0x02;
|
||||
stimestructure.Minutes = 0x00;
|
||||
stimestructure.Seconds = 0x00;
|
||||
stimestructure.TimeFormat = RTC_HOURFORMAT12_AM;
|
||||
stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ;
|
||||
stimestructure.StoreOperation = RTC_STOREOPERATION_RESET;
|
||||
|
||||
if(HAL_RTC_SetTime(&RTCHandle,&stimestructure,FORMAT_BCD) != HAL_OK)
|
||||
{
|
||||
/* Initialization Error */
|
||||
//Error_Handler();
|
||||
return;
|
||||
}
|
||||
|
||||
/*##-3- Writes a data in a RTC Backup data Register0 #######################*/
|
||||
HAL_RTCEx_BKUPWrite(&RTCHandle,RTC_BKP_DR0,0x32F2);
|
||||
static void RTC_CalendarConfig(void) {
|
||||
// set the date to 1st Jan 2014
|
||||
RTC_DateTypeDef date;
|
||||
date.Year = 0x14;
|
||||
date.Month = RTC_MONTH_JANUARY;
|
||||
date.Date = 0x01;
|
||||
date.WeekDay = RTC_WEEKDAY_WEDNESDAY;
|
||||
|
||||
if(HAL_RTC_SetDate(&RTCHandle, &date, FORMAT_BCD) != HAL_OK) {
|
||||
// init error
|
||||
return;
|
||||
}
|
||||
|
||||
// set the time to 00:00:00
|
||||
RTC_TimeTypeDef time;
|
||||
time.Hours = 0x00;
|
||||
time.Minutes = 0x00;
|
||||
time.Seconds = 0x00;
|
||||
time.TimeFormat = RTC_HOURFORMAT12_AM;
|
||||
time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
|
||||
time.StoreOperation = RTC_STOREOPERATION_RESET;
|
||||
|
||||
if (HAL_RTC_SetTime(&RTCHandle, &time, FORMAT_BCD) != HAL_OK) {
|
||||
// init error
|
||||
return;
|
||||
}
|
||||
|
||||
// write data to indicate the RTC has been set
|
||||
HAL_RTCEx_BKUPWrite(&RTCHandle, RTC_BKP_DR0, 0x32f2);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
|
Loading…
Reference in New Issue