Skip to content

Commit ecc0981

Browse files
committed
[bsp][stm32] Implement stm32 rtc driver to RTC framework V2.0
1 parent 2bd7e04 commit ecc0981

File tree

1 file changed

+61
-81
lines changed

1 file changed

+61
-81
lines changed

bsp/stm32/libraries/HAL_Drivers/drv_rtc.c

Lines changed: 61 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* 2018-12-04 balanceTWK first version
99
* 2020-10-14 Dozingfiretruck Porting for stm32wbxx
1010
* 2021-02-05 Meco Man fix the problem of mixing local time and UTC time
11+
* 2021-07-05 iysheng implement RTC framework V2.0
1112
*/
1213

1314
#include "board.h"
@@ -25,8 +26,6 @@
2526

2627
#define BKUP_REG_DATA 0xA5A5
2728

28-
static struct rt_device rtc;
29-
3029
static RTC_HandleTypeDef RTC_Handler;
3130

3231
RT_WEAK uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
@@ -102,34 +101,6 @@ static rt_err_t set_rtc_time_stamp(time_t time_stamp)
102101
return RT_EOK;
103102
}
104103

105-
static void rt_rtc_init(void)
106-
{
107-
#if !defined(SOC_SERIES_STM32H7) && !defined(SOC_SERIES_STM32WL) && !defined(SOC_SERIES_STM32WB)
108-
__HAL_RCC_PWR_CLK_ENABLE();
109-
#endif
110-
111-
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
112-
#ifdef BSP_RTC_USING_LSI
113-
#ifdef SOC_SERIES_STM32WB
114-
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1;
115-
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
116-
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
117-
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
118-
#else
119-
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
120-
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
121-
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
122-
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
123-
#endif
124-
#else
125-
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
126-
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
127-
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
128-
RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
129-
#endif
130-
HAL_RCC_OscConfig(&RCC_OscInitStruct);
131-
}
132-
133104
#ifdef SOC_SERIES_STM32F1
134105
/* update RTC_BKP_DRx*/
135106
static void rt_rtc_f1_bkp_update(void)
@@ -160,7 +131,7 @@ static void rt_rtc_f1_bkp_update(void)
160131
}
161132
#endif
162133

163-
static rt_err_t rt_rtc_config(struct rt_device *dev)
134+
static rt_err_t rt_rtc_config(void)
164135
{
165136
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
166137

@@ -234,81 +205,90 @@ static rt_err_t rt_rtc_config(struct rt_device *dev)
234205
return RT_EOK;
235206
}
236207

237-
static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
208+
static rt_err_t stm32_rtc_init(void)
238209
{
239-
rt_err_t result = RT_EOK;
240-
RT_ASSERT(dev != RT_NULL);
241-
switch (cmd)
210+
#if !defined(SOC_SERIES_STM32H7) && !defined(SOC_SERIES_STM32WL) && !defined(SOC_SERIES_STM32WB)
211+
__HAL_RCC_PWR_CLK_ENABLE();
212+
#endif
213+
214+
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
215+
#ifdef BSP_RTC_USING_LSI
216+
#ifdef SOC_SERIES_STM32WB
217+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1;
218+
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
219+
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
220+
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
221+
#else
222+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
223+
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
224+
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
225+
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
226+
#endif
227+
#else
228+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
229+
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
230+
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
231+
RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
232+
#endif
233+
HAL_RCC_OscConfig(&RCC_OscInitStruct);
234+
235+
if (rt_rtc_config() != RT_EOK)
242236
{
243-
case RT_DEVICE_CTRL_RTC_GET_TIME:
244-
*(rt_uint32_t *)args = get_rtc_timestamp();
245-
LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)args);
246-
break;
237+
LOG_E("rtc init failed.");
238+
return -RT_ERROR;
239+
}
247240

248-
case RT_DEVICE_CTRL_RTC_SET_TIME:
249-
if (set_rtc_time_stamp(*(rt_uint32_t *)args))
250-
{
251-
result = -RT_ERROR;
252-
}
253-
LOG_D("RTC: set rtc_time %x\n", *(rt_uint32_t *)args);
254-
break;
241+
return RT_EOK;
242+
}
243+
244+
static rt_err_t stm32_rtc_get_secs(void *args)
245+
{
246+
*(rt_uint32_t *)args = get_rtc_timestamp();
247+
LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)args);
248+
249+
return RT_EOK;
250+
}
251+
252+
static rt_err_t stm32_rtc_set_secs(void *args)
253+
{
254+
rt_err_t result = RT_EOK;
255+
256+
if (set_rtc_time_stamp(*(rt_uint32_t *)args))
257+
{
258+
result = -RT_ERROR;
255259
}
260+
LOG_D("RTC: set rtc_time %x\n", *(rt_uint32_t *)args);
256261

257262
return result;
258263
}
259264

260-
#ifdef RT_USING_DEVICE_OPS
261-
const static struct rt_device_ops rtc_ops =
265+
static const struct rt_rtc_ops stm32_rtc_ops =
262266
{
267+
stm32_rtc_init,
268+
stm32_rtc_get_secs, /* get_secs */
269+
stm32_rtc_set_secs, /* set secs */
263270
RT_NULL,
264271
RT_NULL,
265272
RT_NULL,
266273
RT_NULL,
267-
RT_NULL,
268-
rt_rtc_control
269274
};
270-
#endif
271275

272-
static rt_err_t rt_hw_rtc_register(rt_device_t device, const char *name, rt_uint32_t flag)
273-
{
274-
RT_ASSERT(device != RT_NULL);
276+
static rt_rtc_dev_t stm32_rtc_dev;
275277

276-
rt_rtc_init();
277-
if (rt_rtc_config(device) != RT_EOK)
278-
{
279-
return -RT_ERROR;
280-
}
281-
#ifdef RT_USING_DEVICE_OPS
282-
device->ops = &rtc_ops;
283-
#else
284-
device->init = RT_NULL;
285-
device->open = RT_NULL;
286-
device->close = RT_NULL;
287-
device->read = RT_NULL;
288-
device->write = RT_NULL;
289-
device->control = rt_rtc_control;
290-
#endif
291-
device->type = RT_Device_Class_RTC;
292-
device->rx_indicate = RT_NULL;
293-
device->tx_complete = RT_NULL;
294-
device->user_data = RT_NULL;
295-
296-
/* register a character device */
297-
return rt_device_register(device, name, flag);
298-
}
299-
300-
int rt_hw_rtc_init(void)
278+
static int rt_hw_rtc_init(void)
301279
{
302280
rt_err_t result;
303-
result = rt_hw_rtc_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
281+
282+
stm32_rtc_dev.ops = &stm32_rtc_ops;
283+
result = rt_rtc_dev_register(&stm32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL);
304284
if (result != RT_EOK)
305285
{
306286
LOG_E("rtc register err code: %d", result);
307287
return result;
308288
}
309289
LOG_D("rtc init success");
290+
310291
return RT_EOK;
311292
}
312293
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
313-
314294
#endif /* BSP_USING_ONCHIP_RTC */

0 commit comments

Comments
 (0)