|
8 | 8 | * 2018-12-04 balanceTWK first version |
9 | 9 | * 2020-10-14 Dozingfiretruck Porting for stm32wbxx |
10 | 10 | * 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 |
11 | 12 | */ |
12 | 13 |
|
13 | 14 | #include "board.h" |
|
25 | 26 |
|
26 | 27 | #define BKUP_REG_DATA 0xA5A5 |
27 | 28 |
|
28 | | -static struct rt_device rtc; |
29 | | - |
30 | 29 | static RTC_HandleTypeDef RTC_Handler; |
31 | 30 |
|
32 | 31 | 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) |
102 | 101 | return RT_EOK; |
103 | 102 | } |
104 | 103 |
|
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 | | - |
133 | 104 | #ifdef SOC_SERIES_STM32F1 |
134 | 105 | /* update RTC_BKP_DRx*/ |
135 | 106 | static void rt_rtc_f1_bkp_update(void) |
@@ -160,7 +131,7 @@ static void rt_rtc_f1_bkp_update(void) |
160 | 131 | } |
161 | 132 | #endif |
162 | 133 |
|
163 | | -static rt_err_t rt_rtc_config(struct rt_device *dev) |
| 134 | +static rt_err_t rt_rtc_config(void) |
164 | 135 | { |
165 | 136 | RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; |
166 | 137 |
|
@@ -234,81 +205,90 @@ static rt_err_t rt_rtc_config(struct rt_device *dev) |
234 | 205 | return RT_EOK; |
235 | 206 | } |
236 | 207 |
|
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) |
238 | 209 | { |
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) |
242 | 236 | { |
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 | + } |
247 | 240 |
|
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; |
255 | 259 | } |
| 260 | + LOG_D("RTC: set rtc_time %x\n", *(rt_uint32_t *)args); |
256 | 261 |
|
257 | 262 | return result; |
258 | 263 | } |
259 | 264 |
|
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 = |
262 | 266 | { |
| 267 | + stm32_rtc_init, |
| 268 | + stm32_rtc_get_secs, /* get_secs */ |
| 269 | + stm32_rtc_set_secs, /* set secs */ |
263 | 270 | RT_NULL, |
264 | 271 | RT_NULL, |
265 | 272 | RT_NULL, |
266 | 273 | RT_NULL, |
267 | | - RT_NULL, |
268 | | - rt_rtc_control |
269 | 274 | }; |
270 | | -#endif |
271 | 275 |
|
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; |
275 | 277 |
|
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) |
301 | 279 | { |
302 | 280 | 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); |
304 | 284 | if (result != RT_EOK) |
305 | 285 | { |
306 | 286 | LOG_E("rtc register err code: %d", result); |
307 | 287 | return result; |
308 | 288 | } |
309 | 289 | LOG_D("rtc init success"); |
| 290 | + |
310 | 291 | return RT_EOK; |
311 | 292 | } |
312 | 293 | INIT_DEVICE_EXPORT(rt_hw_rtc_init); |
313 | | - |
314 | 294 | #endif /* BSP_USING_ONCHIP_RTC */ |
0 commit comments