Skip to content

Commit 04b7cc4

Browse files
committed
stmhal: Fix setting of RTC: was BCD now BIN encoded.
Addresses issue adafruit#592.
1 parent c17fd70 commit 04b7cc4

3 files changed

Lines changed: 46 additions & 10 deletions

File tree

stmhal/rtc.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,26 +214,26 @@ void rtc_init(void) {
214214
static void RTC_CalendarConfig(void) {
215215
// set the date to 1st Jan 2014
216216
RTC_DateTypeDef date;
217-
date.Year = 0x14;
218-
date.Month = RTC_MONTH_JANUARY;
219-
date.Date = 0x01;
217+
date.Year = 14;
218+
date.Month = 1;
219+
date.Date = 1;
220220
date.WeekDay = RTC_WEEKDAY_WEDNESDAY;
221221

222-
if(HAL_RTC_SetDate(&RTCHandle, &date, FORMAT_BCD) != HAL_OK) {
222+
if(HAL_RTC_SetDate(&RTCHandle, &date, FORMAT_BIN) != HAL_OK) {
223223
// init error
224224
return;
225225
}
226226

227227
// set the time to 00:00:00
228228
RTC_TimeTypeDef time;
229-
time.Hours = 0x00;
230-
time.Minutes = 0x00;
231-
time.Seconds = 0x00;
229+
time.Hours = 0;
230+
time.Minutes = 0;
231+
time.Seconds = 0;
232232
time.TimeFormat = RTC_HOURFORMAT12_AM;
233233
time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
234234
time.StoreOperation = RTC_STOREOPERATION_RESET;
235235

236-
if (HAL_RTC_SetTime(&RTCHandle, &time, FORMAT_BCD) != HAL_OK) {
236+
if (HAL_RTC_SetTime(&RTCHandle, &time, FORMAT_BIN) != HAL_OK) {
237237
// init error
238238
return;
239239
}
@@ -316,7 +316,7 @@ mp_obj_t pyb_rtc_datetime(uint n_args, const mp_obj_t *args) {
316316
date.Month = mp_obj_get_int(items[1]);
317317
date.Date = mp_obj_get_int(items[2]);
318318
date.WeekDay = mp_obj_get_int(items[3]);
319-
HAL_RTC_SetDate(&RTCHandle, &date, FORMAT_BCD);
319+
HAL_RTC_SetDate(&RTCHandle, &date, FORMAT_BIN);
320320

321321
RTC_TimeTypeDef time;
322322
time.Hours = mp_obj_get_int(items[4]);
@@ -326,7 +326,7 @@ mp_obj_t pyb_rtc_datetime(uint n_args, const mp_obj_t *args) {
326326
time.TimeFormat = RTC_HOURFORMAT12_AM;
327327
time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
328328
time.StoreOperation = RTC_STOREOPERATION_SET;
329-
HAL_RTC_SetTime(&RTCHandle, &time, FORMAT_BCD);
329+
HAL_RTC_SetTime(&RTCHandle, &time, FORMAT_BIN);
330330

331331
return mp_const_none;
332332
}

tests/pyb/rtc.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,28 @@
33

44
rtc = RTC()
55
print(rtc)
6+
7+
# make sure that 1 second passes correctly
68
rtc.datetime((2014, 1, 1, 1, 0, 0, 0, 0))
79
pyb.delay(1000)
810
print(rtc.datetime()[:7])
11+
12+
def set_and_print(datetime):
13+
rtc.datetime(datetime)
14+
print(rtc.datetime()[:7])
15+
16+
# make sure that setting works correctly
17+
set_and_print((2000, 1, 1, 1, 0, 0, 0, 0))
18+
set_and_print((2000, 1, 31, 1, 0, 0, 0, 0))
19+
set_and_print((2000, 12, 31, 1, 0, 0, 0, 0))
20+
set_and_print((2016, 12, 31, 1, 0, 0, 0, 0))
21+
set_and_print((2016, 12, 31, 7, 0, 0, 0, 0))
22+
set_and_print((2016, 12, 31, 7, 1, 0, 0, 0))
23+
set_and_print((2016, 12, 31, 7, 12, 0, 0, 0))
24+
set_and_print((2016, 12, 31, 7, 13, 0, 0, 0))
25+
set_and_print((2016, 12, 31, 7, 23, 0, 0, 0))
26+
set_and_print((2016, 12, 31, 7, 23, 1, 0, 0))
27+
set_and_print((2016, 12, 31, 7, 23, 59, 0, 0))
28+
set_and_print((2016, 12, 31, 7, 23, 59, 1, 0))
29+
set_and_print((2016, 12, 31, 7, 23, 59, 59, 0))
30+
set_and_print((2099, 12, 31, 7, 23, 59, 59, 0))

tests/pyb/rtc.py.exp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,16 @@
11
<RTC>
22
(2014, 1, 1, 1, 0, 0, 1)
3+
(2000, 1, 1, 1, 0, 0, 0)
4+
(2000, 1, 31, 1, 0, 0, 0)
5+
(2000, 12, 31, 1, 0, 0, 0)
6+
(2016, 12, 31, 1, 0, 0, 0)
7+
(2016, 12, 31, 7, 0, 0, 0)
8+
(2016, 12, 31, 7, 1, 0, 0)
9+
(2016, 12, 31, 7, 12, 0, 0)
10+
(2016, 12, 31, 7, 13, 0, 0)
11+
(2016, 12, 31, 7, 23, 0, 0)
12+
(2016, 12, 31, 7, 23, 1, 0)
13+
(2016, 12, 31, 7, 23, 59, 0)
14+
(2016, 12, 31, 7, 23, 59, 1)
15+
(2016, 12, 31, 7, 23, 59, 59)
16+
(2099, 12, 31, 7, 23, 59, 59)

0 commit comments

Comments
 (0)