ChibiOS  20.3.4
hal_st.c
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 /**
18  * @file hal_st.c
19  * @brief ST Driver code.
20  *
21  * @addtogroup ST
22  * @{
23  */
24 
25 #include "hal.h"
26 
27 #if (OSAL_ST_MODE != OSAL_ST_MODE_NONE) || defined(__DOXYGEN__)
28 
29 /*===========================================================================*/
30 /* Driver local definitions. */
31 /*===========================================================================*/
32 
33 /*===========================================================================*/
34 /* Driver exported variables. */
35 /*===========================================================================*/
36 
37 /*===========================================================================*/
38 /* Driver local types. */
39 /*===========================================================================*/
40 
41 /*===========================================================================*/
42 /* Driver local variables. */
43 /*===========================================================================*/
44 
45 #if (ST_LLD_NUM_ALARMS > 1) || defined(__DOXYGEN__)
46 st_callback_t st_callbacks[ST_LLD_NUM_ALARMS - 1];
47 #endif
48 
49 /*===========================================================================*/
50 /* Driver local functions. */
51 /*===========================================================================*/
52 
53 /*===========================================================================*/
54 /* Driver exported functions. */
55 /*===========================================================================*/
56 
57 /**
58  * @brief ST Driver initialization.
59  * @note This function is implicitly invoked by @p halInit(), there is
60  * no need to explicitly initialize the driver.
61  *
62  * @init
63  */
64 void stInit(void) {
65 #if ST_LLD_NUM_ALARMS > 1
66  unsigned i;
67 
68  for (i = 0U; i < (unsigned)ST_LLD_NUM_ALARMS - 1U; i++) {
69  st_callbacks[i] = NULL;
70  }
71 #endif
72  st_lld_init();
73 }
74 
75 #if (OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING) || defined(__DOXYGEN__)
76 /**
77  * @brief Starts the alarm zero.
78  * @note Makes sure that no spurious alarms are triggered after
79  * this call.
80  * @note This functionality is only available in free running mode, the
81  * behavior in periodic mode is undefined.
82  *
83  * @param[in] abstime the time to be set for the first alarm
84  *
85  * @api
86  */
87 void stStartAlarm(systime_t abstime) {
88 
89  osalDbgAssert(stIsAlarmActive() == false, "already active");
90 
91  st_lld_start_alarm(abstime);
92 }
93 
94 /**
95  * @brief Stops the alarm zero interrupt.
96  * @note This functionality is only available in free running mode, the
97  * behavior in periodic mode is undefined.
98  *
99  * @api
100  */
101 void stStopAlarm(void) {
102 
104 }
105 
106 /**
107  * @brief Sets the alarm zero time.
108  * @note This functionality is only available in free running mode, the
109  * behavior in periodic mode is undefined.
110  *
111  * @param[in] abstime the time to be set for the next alarm
112  *
113  * @api
114  */
115 void stSetAlarm(systime_t abstime) {
116 
117  osalDbgAssert(stIsAlarmActive() != false, "not active");
118 
119  st_lld_set_alarm(abstime);
120 }
121 
122 /**
123  * @brief Returns the alarm zero current time.
124  * @note This functionality is only available in free running mode, the
125  * behavior in periodic mode is undefined.
126  *
127  * @return The currently set alarm time.
128  *
129  * @api
130  */
132 
133  osalDbgAssert(stIsAlarmActive() != false, "not active");
134 
135  return st_lld_get_alarm();
136 }
137 
138 /**
139  * @brief Returns the time counter value.
140  * @note This functionality is only available in free running mode, the
141  * behaviour in periodic mode is undefined.
142  *
143  * @return The counter value.
144  *
145  * @api
146  */
148 
149  return st_lld_get_counter();
150 }
151 
152 /**
153  * @brief Determines if the alarm zero is active.
154  *
155  * @return The alarm status.
156  * @retval false if the alarm is not active.
157  * @retval true is the alarm is active
158  *
159  * @api
160  */
161 bool stIsAlarmActive(void) {
162 
163  return st_lld_is_alarm_active();
164 }
165 
166 #if (ST_LLD_NUM_ALARMS > 1) || defined(__DOXYGEN__)
167 /**
168  * @brief Determines if the specified alarm is active.
169  *
170  * @param[in] alarm alarm channel number (1..ST_LLD_NUM_ALARMS)
171  * @return The alarm status.
172  * @retval false if the alarm is not active.
173  * @retval true is the alarm is active
174  *
175  * @api
176  */
177 bool stIsAlarmActiveN(unsigned alarm) {
178 
179  return st_lld_is_alarm_active_n(n);
180 }
181 
182 /**
183  * @brief Starts an additional alarm.
184  * @note Makes sure that no spurious alarms are triggered after
185  * this call.
186  * @note This functionality is only available in free running mode, the
187  * behavior in periodic mode is undefined.
188  *
189  * @param[in] abstime the time to be set for the first alarm
190  * @param[in] alarm alarm channel number (1..ST_LLD_NUM_ALARMS)
191  * @param[in] cb alarm callback
192  *
193  * @api
194  */
195 void stStartAlarmN(unsigned alarm, systime_t abstime, st_callback_t cb) {
196 
197  osalDbgCheck((alarm > 0U) && (alarm < (unsigned)ST_LLD_NUM_ALARMS));
198  osalDbgAssert(stIsAlarmActiveN(alarm) == false, "already active");
199 
200  st_callbacks[alarm - 1U] = cb;
201  st_lld_start_alarm_n(alarm, abstime);
202 }
203 
204 /**
205  * @brief Stops an additional alarm.
206  * @note This functionality is only available in free running mode, the
207  * behavior in periodic mode is undefined.
208  *
209  * @param[in] alarm alarm channel number (1..ST_LLD_NUM_ALARMS)
210  *
211  * @api
212  */
213 void stStopAlarmN(unsigned alarm) {
214 
215  osalDbgCheck((alarm > 0U) && (alarm < (unsigned)ST_LLD_NUM_ALARMS));
216 
217  st_callbacks[alarm - 1U] = NULL;
218  st_lld_stop_alarm_n(alarm);
219 }
220 
221 /**
222  * @brief Sets an additional alarm time.
223  * @note This functionality is only available in free running mode, the
224  * behavior in periodic mode is undefined.
225  *
226  * @param[in] alarm alarm channel number (1..ST_LLD_NUM_ALARMS)
227  * @param[in] abstime the time to be set for the next alarm
228  *
229  * @api
230  */
231 void stSetAlarmN(unsigned alarm, systime_t abstime) {
232 
233  osalDbgCheck((alarm > 0U) && (alarm < (unsigned)ST_LLD_NUM_ALARMS));
234  osalDbgAssert(stIsAlarmActiveN(alarm) != false, "not active");
235 
236  st_lld_set_alarm_n(alarm, abstime);
237 }
238 
239 /**
240  * @brief Returns an additional alarm current time.
241  * @note This functionality is only available in free running mode, the
242  * behavior in periodic mode is undefined.
243  *
244  * @param[in] alarm alarm channel number (1..ST_LLD_NUM_ALARMS)
245  * @return The currently set alarm time.
246  *
247  * @api
248  */
249 systime_t stGetAlarmN(unsigned alarm) {
250 
251  osalDbgCheck((alarm > 0U) && (alarm < (unsigned)ST_LLD_NUM_ALARMS));
252  osalDbgAssert(stIsAlarmActiveN(alarm) != false, "not active");
253 
254  return st_lld_get_alarm_n(alarm);
255 }
256 #endif /* ST_LLD_NUM_ALARMS > 1 */
257 
258 #endif /* OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING */
259 
260 #endif /* OSAL_ST_MODE != OSAL_ST_MODE_NONE */
261 
262 /** @} */
st_lld_start_alarm
static void st_lld_start_alarm(systime_t abstime)
Starts the alarm.
Definition: hal_st_lld.h:87
stStopAlarmN
void stStopAlarmN(unsigned alarm)
Stops an additional alarm.
Definition: hal_st.c:213
stGetCounter
systime_t stGetCounter(void)
Returns the time counter value.
Definition: hal_st.c:147
stSetAlarm
void stSetAlarm(systime_t abstime)
Sets the alarm zero time.
Definition: hal_st.c:115
stGetAlarmN
systime_t stGetAlarmN(unsigned alarm)
Returns an additional alarm current time.
Definition: hal_st.c:249
stInit
void stInit(void)
ST Driver initialization.
Definition: hal_st.c:64
st_lld_get_alarm
static systime_t st_lld_get_alarm(void)
Returns the current alarm time.
Definition: hal_st_lld.h:120
systime_t
uint64_t systime_t
Type of system time.
Definition: chtime.h:107
st_lld_get_counter
static systime_t st_lld_get_counter(void)
Returns the time counter value.
Definition: hal_st_lld.h:73
hal.h
HAL subsystem header.
stIsAlarmActiveN
bool stIsAlarmActiveN(unsigned alarm)
Determines if the specified alarm is active.
Definition: hal_st.c:177
st_lld_stop_alarm
static void st_lld_stop_alarm(void)
Stops the alarm interrupt.
Definition: hal_st_lld.h:97
st_lld_init
void st_lld_init(void)
Low level ST driver initialization.
Definition: hal_st_lld.c:62
stStartAlarmN
void stStartAlarmN(unsigned alarm, systime_t abstime, st_callback_t cb)
Starts an additional alarm.
Definition: hal_st.c:195
st_lld_set_alarm
static void st_lld_set_alarm(systime_t abstime)
Sets the alarm time.
Definition: hal_st_lld.h:108
stSetAlarmN
void stSetAlarmN(unsigned alarm, systime_t abstime)
Sets an additional alarm time.
Definition: hal_st.c:231
st_lld_is_alarm_active
static bool st_lld_is_alarm_active(void)
Determines if the alarm is active.
Definition: hal_st_lld.h:134
stIsAlarmActive
bool stIsAlarmActive(void)
Determines if the alarm zero is active.
Definition: hal_st.c:161
osalDbgCheck
#define osalDbgCheck(c)
Function parameters check.
Definition: osal.h:277
stStopAlarm
void stStopAlarm(void)
Stops the alarm zero interrupt.
Definition: hal_st.c:101
stGetAlarm
systime_t stGetAlarm(void)
Returns the alarm zero current time.
Definition: hal_st.c:131
stStartAlarm
void stStartAlarm(systime_t abstime)
Starts the alarm zero.
Definition: hal_st.c:87
osalDbgAssert
#define osalDbgAssert(c, remark)
Condition assertion.
Definition: osal.h:257