Skip to content

Commit 83407ad

Browse files
committed
stmhal: Clean up rtc.c a bit.
1 parent 7533700 commit 83407ad

1 file changed

Lines changed: 68 additions & 84 deletions

File tree

stmhal/rtc.c

Lines changed: 68 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,24 @@
77
#include "qstr.h"
88
#include "obj.h"
99
#include "runtime.h"
10-
//#include "systick.h"
1110
#include "rtc.h"
1211

1312
RTC_HandleTypeDef RTCHandle;
14-
static machine_uint_t rtc_info;
1513

16-
#define RTC_INFO_USE_EXISTING (0)
17-
#define RTC_INFO_USE_LSE (1)
18-
#define RTC_INFO_USE_LSI (3)
14+
// rtc_info indicates various things about RTC startup
15+
// it's a bit of a hack at the moment
16+
static machine_uint_t rtc_info;
1917

2018
// Note: LSI is around (32KHz), these dividers should work either way
2119
// ck_spre(1Hz) = RTCCLK(LSE) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)
2220
#define RTC_ASYNCH_PREDIV (0x7f)
2321
#define RTC_SYNCH_PREDIV (0x00ff)
2422

2523
#if 0
24+
#define RTC_INFO_USE_EXISTING (0)
25+
#define RTC_INFO_USE_LSE (1)
26+
#define RTC_INFO_USE_LSI (3)
27+
2628
void rtc_init(void) {
2729
// Enable the PWR clock
2830
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
@@ -124,100 +126,82 @@ void rtc_init(void) {
124126
static void RTC_CalendarConfig(void);
125127

126128
void rtc_init(void) {
127-
/*##-1- Configure the RTC peripheral #######################################*/
128-
RTCHandle.Instance = RTC;
129+
RTCHandle.Instance = RTC;
129130

130-
/* Configure RTC prescaler and RTC data registers */
131-
/* RTC configured as follow:
131+
/* Configure RTC prescaler and RTC data registers */
132+
/* RTC configured as follow:
132133
- Hour Format = Format 24
133134
- Asynch Prediv = Value according to source clock
134135
- Synch Prediv = Value according to source clock
135136
- OutPut = Output Disable
136137
- OutPutPolarity = High Polarity
137-
- OutPutType = Open Drain */
138-
RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24;
139-
RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
140-
RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
141-
RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
142-
RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
143-
RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
144-
138+
- OutPutType = Open Drain */
139+
RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24;
140+
RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
141+
RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
142+
RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
143+
RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
144+
RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
145+
145146
machine_uint_t tick = HAL_GetTick();
146147

147-
if(HAL_RTC_Init(&RTCHandle) != HAL_OK)
148-
{
149-
/* Initialization Error */
150-
//Error_Handler();
151-
return;
152-
}
153-
148+
if (HAL_RTC_Init(&RTCHandle) != HAL_OK) {
149+
// init error
150+
rtc_info = 0xffff; // indicate error
151+
return;
152+
}
153+
154154
// record how long it took for the RTC to start up
155155
rtc_info = HAL_GetTick() - tick;
156156

157-
/*##-2- Check if Data stored in BackUp register0: No Need to reconfigure RTC#*/
158-
/* Read the Back Up Register 0 Data */
159-
if(HAL_RTCEx_BKUPRead(&RTCHandle, RTC_BKP_DR0) != 0x32F2)
160-
{
161-
/* Configure RTC Calendar */
162-
RTC_CalendarConfig();
163-
}
164-
else
165-
{
166-
/* Check if the Power On Reset flag is set */
167-
if(__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET)
168-
{
169-
/* Turn on LED2: Power on reset occured */
170-
//BSP_LED_On(LED2);
157+
// check data stored in BackUp register0
158+
if (HAL_RTCEx_BKUPRead(&RTCHandle, RTC_BKP_DR0) != 0x32f2) {
159+
// fresh reset; configure RTC Calendar
160+
RTC_CalendarConfig();
161+
} else {
162+
// RTC was previously set, so leave it alon
163+
if(__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET) {
164+
// power on reset occured
165+
rtc_info |= 0x10000;
166+
}
167+
if(__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET) {
168+
// external reset occured
169+
rtc_info |= 0x20000;
170+
}
171+
// Clear source Reset Flag
172+
__HAL_RCC_CLEAR_RESET_FLAGS();
171173
}
172-
/* Check if Pin Reset flag is set */
173-
if(__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET)
174-
{
175-
/* Turn on LED4: External reset occured */
176-
//BSP_LED_On(LED4);
174+
}
175+
176+
static void RTC_CalendarConfig(void) {
177+
// set the date to 1st Jan 2014
178+
RTC_DateTypeDef date;
179+
date.Year = 0x14;
180+
date.Month = RTC_MONTH_JANUARY;
181+
date.Date = 0x01;
182+
date.WeekDay = RTC_WEEKDAY_WEDNESDAY;
183+
184+
if(HAL_RTC_SetDate(&RTCHandle, &date, FORMAT_BCD) != HAL_OK) {
185+
// init error
186+
return;
177187
}
178-
/* Clear source Reset Flag */
179-
__HAL_RCC_CLEAR_RESET_FLAGS();
180-
}
181188

182-
}
189+
// set the time to 00:00:00
190+
RTC_TimeTypeDef time;
191+
time.Hours = 0x00;
192+
time.Minutes = 0x00;
193+
time.Seconds = 0x00;
194+
time.TimeFormat = RTC_HOURFORMAT12_AM;
195+
time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
196+
time.StoreOperation = RTC_STOREOPERATION_RESET;
197+
198+
if (HAL_RTC_SetTime(&RTCHandle, &time, FORMAT_BCD) != HAL_OK) {
199+
// init error
200+
return;
201+
}
183202

184-
static void RTC_CalendarConfig(void)
185-
{
186-
RTC_DateTypeDef sdatestructure;
187-
RTC_TimeTypeDef stimestructure;
188-
189-
/*##-1- Configure the Date #################################################*/
190-
/* Set Date: Tuesday February 18th 2014 */
191-
sdatestructure.Year = 0x14;
192-
sdatestructure.Month = RTC_MONTH_FEBRUARY;
193-
sdatestructure.Date = 0x18;
194-
sdatestructure.WeekDay = RTC_WEEKDAY_TUESDAY;
195-
196-
if(HAL_RTC_SetDate(&RTCHandle,&sdatestructure,FORMAT_BCD) != HAL_OK)
197-
{
198-
/* Initialization Error */
199-
//Error_Handler();
200-
return;
201-
}
202-
203-
/*##-2- Configure the Time #################################################*/
204-
/* Set Time: 02:00:00 */
205-
stimestructure.Hours = 0x02;
206-
stimestructure.Minutes = 0x00;
207-
stimestructure.Seconds = 0x00;
208-
stimestructure.TimeFormat = RTC_HOURFORMAT12_AM;
209-
stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ;
210-
stimestructure.StoreOperation = RTC_STOREOPERATION_RESET;
211-
212-
if(HAL_RTC_SetTime(&RTCHandle,&stimestructure,FORMAT_BCD) != HAL_OK)
213-
{
214-
/* Initialization Error */
215-
//Error_Handler();
216-
return;
217-
}
218-
219-
/*##-3- Writes a data in a RTC Backup data Register0 #######################*/
220-
HAL_RTCEx_BKUPWrite(&RTCHandle,RTC_BKP_DR0,0x32F2);
203+
// write data to indicate the RTC has been set
204+
HAL_RTCEx_BKUPWrite(&RTCHandle, RTC_BKP_DR0, 0x32f2);
221205
}
222206

223207
/******************************************************************************/

0 commit comments

Comments
 (0)