1 /*
2  * Copyright 2021,2023 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include "fsl_bbnsm.h"
9 #include "fsl_adapter_rtc.h"
10 
11 /************************************************************************************
12 *************************************************************************************
13 * Private prototypes
14 *************************************************************************************
15 ************************************************************************************/
16 typedef struct _hal_rtc_handle_struct_t
17 {
18     uint8_t instance;
19 } hal_rtc_handle_struct_t;
20 
21 static BBNSM_Type *const s_bbnsmBase[] = BBNSM_BASE_PTRS;
22 
23 /************************************************************************************
24 *************************************************************************************
25 * Public functions
26 *************************************************************************************
27 ************************************************************************************/
HAL_RtcInit(hal_rtc_handle_t halRtcHandle,uint8_t instance)28 hal_rtc_status_t HAL_RtcInit(hal_rtc_handle_t halRtcHandle, uint8_t instance)
29 {
30     bbnsm_rtc_config_t bbnsmConfig;
31     hal_rtc_handle_struct_t *halRtcState = halRtcHandle;
32 
33     assert(halRtcHandle);
34     assert(halRtcState->instance < (sizeof(s_bbnsmBase) / sizeof(s_bbnsmBase[0])));
35 
36     halRtcState->instance = instance;
37     BBNSM_RTC_GetDefaultConfig(&bbnsmConfig);
38     BBNSM_RTC_Init(s_bbnsmBase[instance], &bbnsmConfig);
39 
40     return kStatus_HAL_RtcSuccess;
41 }
42 
43 /*! -------------------------------------------------------------------------
44  * @brief   DeInitialize the RTC part of the timer module
45  *---------------------------------------------------------------------------*/
HAL_RtcDeinit(hal_rtc_handle_t halRtcHandle)46 void HAL_RtcDeinit(hal_rtc_handle_t halRtcHandle)
47 {
48     hal_rtc_handle_struct_t *halRtcState = halRtcHandle;
49 
50     assert(halRtcHandle);
51     assert(halRtcState->instance < (sizeof(s_bbnsmBase) / sizeof(s_bbnsmBase[0])));
52     BBNSM_RTC_Deinit(s_bbnsmBase[halRtcState->instance]);
53 }
54 /*! -------------------------------------------------------------------------
55  * \brief     Sets the absolute time.
56  * \param[in] microseconds - Time in microseconds.
57  *---------------------------------------------------------------------------*/
HAL_RtcSetTime(hal_rtc_handle_t halRtcHandle,uint64_t microseconds)58 hal_rtc_status_t HAL_RtcSetTime(hal_rtc_handle_t halRtcHandle, uint64_t microseconds)
59 {
60     hal_rtc_handle_struct_t *halRtcState = halRtcHandle;
61     uint32_t seconds                     = (uint32_t)(microseconds / SECOND_TO_MICROSECOND);
62 
63     assert(halRtcHandle);
64     assert(halRtcState->instance < (sizeof(s_bbnsmBase) / sizeof(s_bbnsmBase[0])));
65     BBNSM_RTC_SetSeconds(s_bbnsmBase[halRtcState->instance], seconds);
66 
67     return kStatus_HAL_RtcSuccess;
68 }
69 
70 /*! -------------------------------------------------------------------------
71  * \brief     Gets the absolute time.
72  * \param[in] microseconds - Time in microseconds.
73  *---------------------------------------------------------------------------*/
HAL_RtcGetTime(hal_rtc_handle_t halRtcHandle)74 uint64_t HAL_RtcGetTime(hal_rtc_handle_t halRtcHandle)
75 {
76     hal_rtc_handle_struct_t *halRtcState = halRtcHandle;
77 
78     assert(halRtcState);
79     assert(halRtcState->instance < (sizeof(s_bbnsmBase) / sizeof(s_bbnsmBase[0])));
80 
81     return (uint64_t)BBNSM_RTC_GetSeconds(s_bbnsmBase[halRtcState->instance]) * SECOND_TO_MICROSECOND;
82 }
83 /*! -------------------------------------------------------------------------
84  * \brief     Sets the alarm absolute time in seconds.
85  * \param[in] microseconds - Time in seconds for the alarm.
86  *---------------------------------------------------------------------------*/
HAL_RtcSetAlarm(hal_rtc_handle_t halRtcHandle,uint64_t microseconds)87 hal_rtc_status_t HAL_RtcSetAlarm(hal_rtc_handle_t halRtcHandle, uint64_t microseconds)
88 {
89     uint32_t seconds                     = (uint32_t)(microseconds / SECOND_TO_MICROSECOND);
90     hal_rtc_handle_struct_t *halRtcState = halRtcHandle;
91 
92     assert(halRtcState);
93     assert(halRtcState->instance < (sizeof(s_bbnsmBase) / sizeof(s_bbnsmBase[0])));
94 
95     /* Set alarm time in seconds */
96     return (hal_rtc_status_t)BBNSM_RTC_SetAlarm(s_bbnsmBase[halRtcState->instance], seconds);
97 }
98 
HAL_RtcGetAlarm(hal_rtc_handle_t halRtcHandle)99 uint64_t HAL_RtcGetAlarm(hal_rtc_handle_t halRtcHandle)
100 {
101     hal_rtc_handle_struct_t *halRtcState = halRtcHandle;
102 
103     assert(halRtcState);
104     assert(halRtcState->instance < (sizeof(s_bbnsmBase) / sizeof(s_bbnsmBase[0])));
105 
106     return BBNSM_RTC_GetAlarm(s_bbnsmBase[halRtcState->instance]) * SECOND_TO_MICROSECOND;
107 }
108 
HAL_RtcGetEnabledInterrupts(hal_rtc_handle_t halRtcHandle)109 uint32_t HAL_RtcGetEnabledInterrupts(hal_rtc_handle_t halRtcHandle)
110 {
111     hal_rtc_handle_struct_t *halRtcState = halRtcHandle;
112     uint32_t bbnsm_flags                 = 0U;
113     uint32_t flags                       = 0U;
114 
115     assert(halRtcState);
116     assert(halRtcState->instance < (sizeof(s_bbnsmBase) / sizeof(s_bbnsmBase[0])));
117 
118     bbnsm_flags = BBNSM_GetEnabledInterrupts(s_bbnsmBase[halRtcState->instance]);
119     if ((bbnsm_flags & (uint32_t)kBBNSM_RTC_AlarmInterrupt) != 0x0U)
120         flags |= (uint32_t)kHAL_RTC_AlarmInterrupt;
121 
122     return flags;
123 }
124 
HAL_RtcGetStatusFlags(hal_rtc_handle_t halRtcHandle)125 uint32_t HAL_RtcGetStatusFlags(hal_rtc_handle_t halRtcHandle)
126 {
127     hal_rtc_handle_struct_t *halRtcState = halRtcHandle;
128     uint32_t bbnsm_flags                 = 0U;
129     uint32_t flags                       = 0U;
130 
131     assert(halRtcState);
132     assert(halRtcState->instance < (sizeof(s_bbnsmBase) / sizeof(s_bbnsmBase[0])));
133 
134     bbnsm_flags = BBNSM_GetStatusFlags(s_bbnsmBase[halRtcState->instance]);
135     if ((bbnsm_flags & (uint32_t)kBBNSM_RTC_AlarmInterruptFlag) != 0x0U)
136         flags |= (uint32_t)kHAL_RTC_AlarmInterruptFlag;
137 
138     return flags;
139 }
140 
HAL_RtcClearStatusFlags(hal_rtc_handle_t halRtcHandle,uint32_t flags)141 void HAL_RtcClearStatusFlags(hal_rtc_handle_t halRtcHandle, uint32_t flags)
142 {
143     hal_rtc_handle_struct_t *halRtcState = halRtcHandle;
144     uint32_t bbnsm_flags                 = 0U;
145 
146     assert(halRtcState);
147     assert(halRtcState->instance < (sizeof(s_bbnsmBase) / sizeof(s_bbnsmBase[0])));
148 
149     if ((flags & (uint32_t)kHAL_RTC_AlarmInterruptFlag) != 0x0U)
150         bbnsm_flags |= (uint32_t)kBBNSM_RTC_AlarmInterruptFlag;
151 
152     BBNSM_ClearStatusFlags(s_bbnsmBase[halRtcState->instance], bbnsm_flags);
153 }
154 
HAL_RtcEnableInterrupts(hal_rtc_handle_t halRtcHandle,uint32_t flags)155 void HAL_RtcEnableInterrupts(hal_rtc_handle_t halRtcHandle, uint32_t flags)
156 {
157     hal_rtc_handle_struct_t *halRtcState = halRtcHandle;
158     uint32_t bbnsm_flags                 = 0U;
159 
160     assert(halRtcState);
161     assert(halRtcState->instance < (sizeof(s_bbnsmBase) / sizeof(s_bbnsmBase[0])));
162 
163     if ((flags & (uint32_t)kHAL_RTC_AlarmInterrupt) != 0x0U)
164         bbnsm_flags |= (uint32_t)kBBNSM_RTC_AlarmInterruptFlag;
165 
166     BBNSM_EnableInterrupts(s_bbnsmBase[halRtcState->instance], bbnsm_flags);
167 }
168 
HAL_RtcDisableInterrupts(hal_rtc_handle_t halRtcHandle,uint32_t flags)169 void HAL_RtcDisableInterrupts(hal_rtc_handle_t halRtcHandle, uint32_t flags)
170 {
171     hal_rtc_handle_struct_t *halRtcState = halRtcHandle;
172     uint32_t bbnsm_flags                 = 0U;
173 
174     assert(halRtcState);
175     assert(halRtcState->instance < (sizeof(s_bbnsmBase) / sizeof(s_bbnsmBase[0])));
176 
177     if ((flags & (uint32_t)kHAL_RTC_AlarmInterrupt) != 0x0U)
178         bbnsm_flags |= (uint32_t)kBBNSM_RTC_AlarmInterrupt;
179 
180     BBNSM_DisableInterrupts(s_bbnsmBase[halRtcState->instance], bbnsm_flags);
181 }
182