Skip to content

Commit ab346a2

Browse files
authored
Merge pull request #6468 from blues/stm32_rtc
feat(swan_r5): adds a basic STM32 RTC implementation.
2 parents 39b8c2c + beafbc7 commit ab346a2

File tree

9 files changed

+147
-4
lines changed

9 files changed

+147
-4
lines changed

ports/stm/boards/swan_r5/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ CIRCUITPY_BLEIO = 0
7070
CIRCUITPY_BUSDEVICE = 0
7171
CIRCUITPY_KEYPAD = 1
7272
CIRCUITPY_RGBMATRIX = 0
73+
CIRCUITPY_RTC = 1

ports/stm/common-hal/rtc/RTC.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 20212 Matthew McGowan for Blues Wireless Inc
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdio.h>
28+
29+
#include "py/obj.h"
30+
#include "py/runtime.h"
31+
#include "shared/timeutils/timeutils.h"
32+
#include "shared-bindings/rtc/__init__.h"
33+
#include "common-hal/rtc/RTC.h"
34+
#include "shared-bindings/rtc/RTC.h"
35+
#include "supervisor/port.h"
36+
#include "supervisor/shared/translate/translate.h"
37+
#include "peripherals/rtc.h"
38+
39+
40+
void common_hal_rtc_set_time(timeutils_struct_time_t *tm) {
41+
stm32_peripherals_rtc_set_time(tm);
42+
}
43+
44+
void common_hal_rtc_get_time(timeutils_struct_time_t *tm) {
45+
stm32_peripherals_rtc_get_time(tm);
46+
}
47+
48+
int common_hal_rtc_get_calibration(void) {
49+
return 0;
50+
}
51+
52+
void common_hal_rtc_set_calibration(int calibration) {
53+
mp_raise_NotImplementedError_varg(translate("%q"), MP_QSTR_calibration);
54+
}

ports/stm/common-hal/rtc/RTC.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 Matthew McGowan for Blues Wireless Inc
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_STM_COMMON_HAL_RTC_RTC_H
28+
#define MICROPY_INCLUDED_STM_COMMON_HAL_RTC_RTC_H
29+
30+
extern void rtc_init(void);
31+
extern void rtc_reset(void);
32+
33+
#endif // MICROPY_INCLUDED_STM_COMMON_HAL_RTC_RTC_H

ports/stm/common-hal/rtc/__init__.c

Whitespace-only changes.

ports/stm/common-hal/rtc/__init__.h

Whitespace-only changes.

ports/stm/mpconfigport.mk

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ ifeq ($(MCU_SERIES),F4)
2424
CIRCUITPY_I2CPERIPHERAL ?= 0
2525
CIRCUITPY_NVM ?= 0
2626
CIRCUITPY_ROTARYIO ?= 0
27-
CIRCUITPY_RTC ?= 0
27+
CIRCUITPY_RTC ?= 1
2828
USB_NUM_ENDPOINT_PAIRS = 4
2929
UF2_FAMILY_ID ?= 0x57755a57
3030
endif
@@ -42,7 +42,7 @@ ifeq ($(MCU_SERIES),H7)
4242
CIRCUITPY_PULSEIO ?= 0
4343
CIRCUITPY_PWMIO ?= 0
4444
CIRCUITPY_ROTARYIO ?= 0
45-
CIRCUITPY_RTC ?= 0
45+
CIRCUITPY_RTC ?= 1
4646

4747
USB_NUM_ENDPOINT_PAIRS = 9
4848
UF2_FAMILY_ID ?= 0x6db66082
@@ -59,7 +59,7 @@ ifeq ($(MCU_SERIES),F7)
5959
CIRCUITPY_NEOPIXEL_WRITE ?= 0
6060
CIRCUITPY_NVM ?= 0
6161
CIRCUITPY_ROTARYIO ?= 0
62-
CIRCUITPY_RTC ?= 0
62+
CIRCUITPY_RTC ?= 1
6363

6464
USB_NUM_ENDPOINT_PAIRS = 6
6565
UF2_FAMILY_ID ?= 0x53b80f00
@@ -76,7 +76,7 @@ ifeq ($(MCU_SERIES),L4)
7676
CIRCUITPY_NEOPIXEL_WRITE ?= 0
7777
CIRCUITPY_NVM ?= 0
7878
CIRCUITPY_ROTARYIO ?= 0
79-
CIRCUITPY_RTC ?= 0
79+
CIRCUITPY_RTC ?= 1
8080
# todo - this varies between devices in the series
8181
# This slide deck https://www.st.com/content/ccc/resource/training/technical/product_training/98/89/c8/6c/3e/e9/49/79/STM32L4_Peripheral_USB.pdf/files/STM32L4_Peripheral_USB.pdf/jcr:content/translations/en.STM32L4_Peripheral_USB.pdf
8282
# cites 16 endpoints, 8 endpoint pairs, while section 3.39 of the L4R5 datasheet states 6 endpoint pairs.

ports/stm/peripherals/rtc.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* The MIT License (MIT)
55
*
66
* Copyright (c) 2021 Lucian Copeland for Adafruit Industries
7+
* Copyright (c) 2022 Matthew McGowan for Blues Wireless Inc
78
*
89
* Permission is hereby granted, free of charge, to any person obtaining a copy
910
* of this software and associated documentation files (the "Software"), to deal
@@ -76,6 +77,46 @@ void stm32_peripherals_rtc_init(void) {
7677
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
7778
}
7879

80+
#if CIRCUITPY_RTC
81+
void stm32_peripherals_rtc_get_time(timeutils_struct_time_t *tm) {
82+
RTC_DateTypeDef date = {0};
83+
RTC_TimeTypeDef time = {0};
84+
85+
int code;
86+
if ((code = HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN)) == HAL_OK &&
87+
(code = HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN)) == HAL_OK) {
88+
tm->tm_hour = time.Hours;
89+
tm->tm_min = time.Minutes;
90+
tm->tm_sec = time.Seconds;
91+
tm->tm_wday = date.WeekDay - 1;
92+
tm->tm_mday = date.Date;
93+
tm->tm_mon = date.Month;
94+
tm->tm_year = date.Year + 2000;
95+
tm->tm_yday = -1;
96+
}
97+
}
98+
99+
void stm32_peripherals_rtc_set_time(timeutils_struct_time_t *tm) {
100+
RTC_DateTypeDef date = {0};
101+
RTC_TimeTypeDef time = {0};
102+
103+
time.Hours = tm->tm_hour;
104+
time.Minutes = tm->tm_min;
105+
time.Seconds = tm->tm_sec;
106+
date.WeekDay = tm->tm_wday + 1;
107+
date.Date = tm->tm_mday;
108+
date.Month = tm->tm_mon;
109+
date.Year = tm->tm_year - 2000;
110+
time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
111+
time.StoreOperation = RTC_STOREOPERATION_RESET;
112+
113+
if (HAL_RTC_SetTime(&hrtc, &time, RTC_FORMAT_BIN) != HAL_OK ||
114+
HAL_RTC_SetDate(&hrtc, &date, RTC_FORMAT_BIN) != HAL_OK) {
115+
// todo - throw an exception
116+
}
117+
}
118+
#endif
119+
79120
// This function is called often for timing so we cache the seconds elapsed computation based on the
80121
// register value. The STM HAL always does shifts and conversion if we use it directly.
81122
uint64_t stm32_peripherals_rtc_raw_ticks(uint8_t *subticks) {

ports/stm/peripherals/rtc.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* The MIT License (MIT)
55
*
66
* Copyright (c) 2021 Lucian Copeland for Adafruit Industries
7+
* Copyright (c) 2022 Matthew McGowan for Blues Wireless Inc
78
*
89
* Permission is hereby granted, free of charge, to any person obtaining a copy
910
* of this software and associated documentation files (the "Software"), to deal
@@ -48,4 +49,10 @@ void stm32_peripherals_rtc_assign_alarm_callback(uint8_t alarm_idx, void (*callb
4849
void stm32_peripherals_rtc_set_alarm(uint8_t alarm_idx, uint32_t ticks);
4950
bool stm32_peripherals_rtc_alarm_triggered(uint8_t alarm_idx);
5051

52+
#if CIRCUITPY_RTC
53+
typedef struct _timeutils_struct_time_t timeutils_struct_time_t;
54+
void stm32_peripherals_rtc_get_time(timeutils_struct_time_t *tm);
55+
void stm32_peripherals_rtc_set_time(timeutils_struct_time_t *tm);
56+
#endif
57+
5158
#endif // __MICROPY_INCLUDED_STM32_PERIPHERALS_RTC_H__

ports/stm/supervisor/port.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
#if CIRCUITPY_ALARM
6262
#include "common-hal/alarm/__init__.h"
6363
#endif
64+
#if CIRCUITPY_RTC
65+
#include "shared-bindings/rtc/__init__.h"
66+
#endif
6467

6568
#include "peripherals/clocks.h"
6669
#include "peripherals/gpio.h"
@@ -241,6 +244,10 @@ void SysTick_Handler(void) {
241244

242245
void reset_port(void) {
243246
reset_all_pins();
247+
#if CIRCUITPY_RTC
248+
rtc_reset();
249+
#endif
250+
244251
#if CIRCUITPY_AUDIOPWMIO
245252
audiopwmout_reset();
246253
#endif

0 commit comments

Comments
 (0)