Commit 83407ad0 authored by Damien George's avatar Damien George
Browse files

stmhal: Clean up rtc.c a bit.

parent 75337003
...@@ -7,15 +7,13 @@ ...@@ -7,15 +7,13 @@
#include "qstr.h" #include "qstr.h"
#include "obj.h" #include "obj.h"
#include "runtime.h" #include "runtime.h"
//#include "systick.h"
#include "rtc.h" #include "rtc.h"
RTC_HandleTypeDef RTCHandle; RTC_HandleTypeDef RTCHandle;
static machine_uint_t rtc_info;
#define RTC_INFO_USE_EXISTING (0) // rtc_info indicates various things about RTC startup
#define RTC_INFO_USE_LSE (1) // it's a bit of a hack at the moment
#define RTC_INFO_USE_LSI (3) static machine_uint_t rtc_info;
// Note: LSI is around (32KHz), these dividers should work either way // Note: LSI is around (32KHz), these dividers should work either way
// ck_spre(1Hz) = RTCCLK(LSE) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1) // ck_spre(1Hz) = RTCCLK(LSE) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)
...@@ -23,6 +21,10 @@ static machine_uint_t rtc_info; ...@@ -23,6 +21,10 @@ static machine_uint_t rtc_info;
#define RTC_SYNCH_PREDIV (0x00ff) #define RTC_SYNCH_PREDIV (0x00ff)
#if 0 #if 0
#define RTC_INFO_USE_EXISTING (0)
#define RTC_INFO_USE_LSE (1)
#define RTC_INFO_USE_LSI (3)
void rtc_init(void) { void rtc_init(void) {
// Enable the PWR clock // Enable the PWR clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
...@@ -124,100 +126,82 @@ void rtc_init(void) { ...@@ -124,100 +126,82 @@ void rtc_init(void) {
static void RTC_CalendarConfig(void); static void RTC_CalendarConfig(void);
void rtc_init(void) { void rtc_init(void) {
/*##-1- Configure the RTC peripheral #######################################*/ RTCHandle.Instance = RTC;
RTCHandle.Instance = RTC;
/* Configure RTC prescaler and RTC data registers */ /* Configure RTC prescaler and RTC data registers */
/* RTC configured as follow: /* RTC configured as follow:
- Hour Format = Format 24 - Hour Format = Format 24
- Asynch Prediv = Value according to source clock - Asynch Prediv = Value according to source clock
- Synch Prediv = Value according to source clock - Synch Prediv = Value according to source clock
- OutPut = Output Disable - OutPut = Output Disable
- OutPutPolarity = High Polarity - OutPutPolarity = High Polarity
- OutPutType = Open Drain */ - OutPutType = Open Drain */
RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24; RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24;
RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE; RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
machine_uint_t tick = HAL_GetTick(); machine_uint_t tick = HAL_GetTick();
if(HAL_RTC_Init(&RTCHandle) != HAL_OK) if (HAL_RTC_Init(&RTCHandle) != HAL_OK) {
{ // init error
/* Initialization Error */ rtc_info = 0xffff; // indicate error
//Error_Handler(); return;
return; }
}
// record how long it took for the RTC to start up // record how long it took for the RTC to start up
rtc_info = HAL_GetTick() - tick; rtc_info = HAL_GetTick() - tick;
/*##-2- Check if Data stored in BackUp register0: No Need to reconfigure RTC#*/ // check data stored in BackUp register0
/* Read the Back Up Register 0 Data */ if (HAL_RTCEx_BKUPRead(&RTCHandle, RTC_BKP_DR0) != 0x32f2) {
if(HAL_RTCEx_BKUPRead(&RTCHandle, RTC_BKP_DR0) != 0x32F2) // fresh reset; configure RTC Calendar
{ RTC_CalendarConfig();
/* Configure RTC Calendar */ } else {
RTC_CalendarConfig(); // RTC was previously set, so leave it alon
} if(__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET) {
else // power on reset occured
{ rtc_info |= 0x10000;
/* Check if the Power On Reset flag is set */ }
if(__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET) if(__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET) {
{ // external reset occured
/* Turn on LED2: Power on reset occured */ rtc_info |= 0x20000;
//BSP_LED_On(LED2); }
// 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)
{ static void RTC_CalendarConfig(void) {
/* Turn on LED4: External reset occured */ // set the date to 1st Jan 2014
//BSP_LED_On(LED4); 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;
} }
/* Clear source Reset Flag */
__HAL_RCC_CLEAR_RESET_FLAGS();
}
} // 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;
}
static void RTC_CalendarConfig(void) // write data to indicate the RTC has been set
{ HAL_RTCEx_BKUPWrite(&RTCHandle, RTC_BKP_DR0, 0x32f2);
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);
} }
/******************************************************************************/ /******************************************************************************/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment