2929
3030#if BSP_CFG_RTC_EN > 0
3131
32- static RTC_HandleTypeDef stm32_rtc_handle ;
33-
34- static ms_err_t __stm32_rtc_set_time (const ms_rtc_time_t * rtc_time )
32+ static ms_err_t __stm32_rtc_set_time (ms_ptr_t drv_ctx , const ms_rtc_time_t * rtc_time )
3533{
34+ RTC_HandleTypeDef * rtc_handle = drv_ctx ;
3635 RTC_DateTypeDef sdatestructure ;
3736 RTC_TimeTypeDef stimestructure ;
3837
@@ -43,7 +42,7 @@ static ms_err_t __stm32_rtc_set_time(const ms_rtc_time_t *rtc_time)
4342 sdatestructure .Month = rtc_time -> month ;
4443 sdatestructure .Date = rtc_time -> date ;
4544 sdatestructure .WeekDay = rtc_time -> weekday ;
46- HAL_RTC_SetDate (& stm32_rtc_handle , & sdatestructure , RTC_FORMAT_BIN );
45+ HAL_RTC_SetDate (rtc_handle , & sdatestructure , RTC_FORMAT_BIN );
4746
4847 /*
4948 * Set Time
@@ -54,25 +53,26 @@ static ms_err_t __stm32_rtc_set_time(const ms_rtc_time_t *rtc_time)
5453 stimestructure .TimeFormat = RTC_HOURFORMAT_24 ;
5554 stimestructure .DayLightSaving = RTC_DAYLIGHTSAVING_NONE ;
5655 stimestructure .StoreOperation = RTC_STOREOPERATION_RESET ;
57- HAL_RTC_SetTime (& stm32_rtc_handle , & stimestructure , RTC_FORMAT_BIN );
56+ HAL_RTC_SetTime (rtc_handle , & stimestructure , RTC_FORMAT_BIN );
5857
5958 return MS_ERR_NONE ;
6059}
6160
62- static ms_err_t __stm32_rtc_get_time (ms_rtc_time_t * rtc_time )
61+ static ms_err_t __stm32_rtc_get_time (ms_ptr_t drv_ctx , ms_rtc_time_t * rtc_time )
6362{
63+ RTC_HandleTypeDef * rtc_handle = drv_ctx ;
6464 RTC_DateTypeDef sdatestructureget ;
6565 RTC_TimeTypeDef stimestructureget ;
6666
6767 /*
6868 * Get the RTC current Time
6969 */
70- HAL_RTC_GetTime (& stm32_rtc_handle , & stimestructureget , RTC_FORMAT_BIN );
70+ HAL_RTC_GetTime (rtc_handle , & stimestructureget , RTC_FORMAT_BIN );
7171
7272 /*
7373 * Get the RTC current Date
7474 */
75- HAL_RTC_GetDate (& stm32_rtc_handle , & sdatestructureget , RTC_FORMAT_BIN );
75+ HAL_RTC_GetDate (rtc_handle , & sdatestructureget , RTC_FORMAT_BIN );
7676
7777 rtc_time -> year = sdatestructureget .Year ; /* base 2000 */
7878 rtc_time -> month = sdatestructureget .Month ; /* MS_RTC_MONTH_xxx */
@@ -90,7 +90,7 @@ static ms_err_t __stm32_rtc_get_time(ms_rtc_time_t *rtc_time)
9090 * @param None
9191 * @retval None
9292 */
93- static void RTC_CalendarConfig (void )
93+ static void RTC_CalendarConfig (RTC_HandleTypeDef * rtc_handle )
9494{
9595 RTC_DateTypeDef sdatestructure ;
9696 RTC_TimeTypeDef stimestructure ;
@@ -102,7 +102,7 @@ static void RTC_CalendarConfig(void)
102102 sdatestructure .Month = MS_RTC_MONTH_FEBRUARY ;
103103 sdatestructure .Date = 18 ;
104104 sdatestructure .WeekDay = MS_RTC_WEEKDAY_TUESDAY ;
105- HAL_RTC_SetDate (& stm32_rtc_handle , & sdatestructure , RTC_FORMAT_BIN );
105+ HAL_RTC_SetDate (rtc_handle , & sdatestructure , RTC_FORMAT_BIN );
106106
107107 /*
108108 * Set Time: 02:00:00
@@ -113,16 +113,18 @@ static void RTC_CalendarConfig(void)
113113 stimestructure .TimeFormat = RTC_HOURFORMAT_24 ;
114114 stimestructure .DayLightSaving = RTC_DAYLIGHTSAVING_NONE ;
115115 stimestructure .StoreOperation = RTC_STOREOPERATION_RESET ;
116- HAL_RTC_SetTime (& stm32_rtc_handle , & stimestructure , RTC_FORMAT_BIN );
116+ HAL_RTC_SetTime (rtc_handle , & stimestructure , RTC_FORMAT_BIN );
117117
118118 /*
119119 * Writes a data in a RTC Backup data Register1
120120 */
121- HAL_RTCEx_BKUPWrite (& stm32_rtc_handle , RTC_BKP_DR1 , 0x32F2 );
121+ HAL_RTCEx_BKUPWrite (rtc_handle , RTC_BKP_DR1 , 0x32F2 );
122122}
123123
124- static ms_err_t __stm32_rtc_init (void )
124+ static ms_err_t __stm32_rtc_init (ms_ptr_t drv_ctx )
125125{
126+ RTC_HandleTypeDef * rtc_handle = drv_ctx ;
127+
126128 /*
127129 * Configure RTC prescaler and RTC data registers
128130 */
@@ -135,24 +137,24 @@ static ms_err_t __stm32_rtc_init(void)
135137 * - OutPutPolarity = High Polarity
136138 * - OutPutType = Open Drain
137139 */
138- stm32_rtc_handle . Instance = RTC ;
139- stm32_rtc_handle . Init .HourFormat = RTC_HOURFORMAT_24 ;
140- stm32_rtc_handle . Init .AsynchPrediv = BSP_CFG_RTC_ASYNCH_PREDIV ;
141- stm32_rtc_handle . Init .SynchPrediv = BSP_CFG_RTC_SYNCH_PREDIV ;
142- stm32_rtc_handle . Init .OutPut = RTC_OUTPUT_DISABLE ;
143- stm32_rtc_handle . Init .OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH ;
144- stm32_rtc_handle . Init .OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN ;
145- __HAL_RTC_RESET_HANDLE_STATE (& stm32_rtc_handle );
146- HAL_RTC_Init (& stm32_rtc_handle );
140+ rtc_handle -> Instance = RTC ;
141+ rtc_handle -> Init .HourFormat = RTC_HOURFORMAT_24 ;
142+ rtc_handle -> Init .AsynchPrediv = BSP_CFG_RTC_ASYNCH_PREDIV ;
143+ rtc_handle -> Init .SynchPrediv = BSP_CFG_RTC_SYNCH_PREDIV ;
144+ rtc_handle -> Init .OutPut = RTC_OUTPUT_DISABLE ;
145+ rtc_handle -> Init .OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH ;
146+ rtc_handle -> Init .OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN ;
147+ __HAL_RTC_RESET_HANDLE_STATE (rtc_handle );
148+ HAL_RTC_Init (rtc_handle );
147149
148150 /*
149151 * Read the Back Up Register 1 Data
150152 */
151- if (HAL_RTCEx_BKUPRead (& stm32_rtc_handle , RTC_BKP_DR1 ) != 0x32F2 ) {
153+ if (HAL_RTCEx_BKUPRead (rtc_handle , RTC_BKP_DR1 ) != 0x32F2 ) {
152154 /*
153155 * Configure RTC Calendar
154156 */
155- RTC_CalendarConfig ();
157+ RTC_CalendarConfig (rtc_handle );
156158 } else {
157159 /*
158160 * Clear source Reset Flag
@@ -169,9 +171,11 @@ static ms_rtc_drv_t __stm32_rtc_drv = {
169171 .get_time = __stm32_rtc_get_time ,
170172};
171173
172- ms_rtc_drv_t * stm32_rtc_drv (void )
174+ static RTC_HandleTypeDef __stm32_rtc_handle ;
175+
176+ ms_err_t stm32_rtc_dev_create (void )
173177{
174- return & __stm32_rtc_drv ;
178+ return ms_rtc_dev_create ( MS_RTC_DEV_PATH , & __stm32_rtc_drv , & __stm32_rtc_handle ) ;
175179}
176180
177181#endif
0 commit comments