ChibiOS/HAL 9.0.0
hal_icu.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_icu.c
19 * @brief ICU Driver code.
20 *
21 * @addtogroup ICU
22 * @{
23 */
24
25#include "hal.h"
26
27#if (HAL_USE_ICU == TRUE) || defined(__DOXYGEN__)
28
29/*===========================================================================*/
30/* Driver local definitions. */
31/*===========================================================================*/
32
33/*===========================================================================*/
34/* Driver exported variables. */
35/*===========================================================================*/
36
37/*===========================================================================*/
38/* Driver local variables and types. */
39/*===========================================================================*/
40
41/*===========================================================================*/
42/* Driver local functions. */
43/*===========================================================================*/
44
45/*===========================================================================*/
46/* Driver exported functions. */
47/*===========================================================================*/
48
49/**
50 * @brief ICU Driver initialization.
51 * @note This function is implicitly invoked by @p halInit(), there is
52 * no need to explicitly initialize the driver.
53 *
54 * @init
55 */
56void icuInit(void) {
57
59}
60
61/**
62 * @brief Initializes the standard part of a @p ICUDriver structure.
63 *
64 * @param[out] icup pointer to the @p ICUDriver object
65 *
66 * @init
67 */
69
70 icup->state = ICU_STOP;
71 icup->config = NULL;
72}
73
74/**
75 * @brief Configures and activates the ICU peripheral.
76 *
77 * @param[in] icup pointer to the @p ICUDriver object
78 * @param[in] config pointer to the @p ICUConfig object
79 * @return The operation status.
80 *
81 * @api
82 */
83msg_t icuStart(ICUDriver *icup, const ICUConfig *config) {
84 msg_t msg;
85
86 osalDbgCheck((icup != NULL) && (config != NULL));
87
89 osalDbgAssert((icup->state == ICU_STOP) || (icup->state == ICU_READY),
90 "invalid state");
91
92 icup->config = config;
93
94#if defined(ICU_LLD_ENHANCED_API)
95 msg = icu_lld_start(icup);
96 if (msg == HAL_RET_SUCCESS) {
97 icup->state = ICU_READY;
98 }
99 else {
100 icup->state = ICU_STOP;
101 }
102#else
103 icu_lld_start(icup);
104 icup->state = ICU_READY;
105 msg = HAL_RET_SUCCESS;
106#endif
107
109
110 return msg;
111}
112
113/**
114 * @brief Deactivates the ICU peripheral.
115 *
116 * @param[in] icup pointer to the @p ICUDriver object
117 *
118 * @api
119 */
120void icuStop(ICUDriver *icup) {
121
122 osalDbgCheck(icup != NULL);
123
124 osalSysLock();
125
126 osalDbgAssert((icup->state == ICU_STOP) || (icup->state == ICU_READY),
127 "invalid state");
128
129 icu_lld_stop(icup);
130 icup->config = NULL;
131 icup->state = ICU_STOP;
132
134}
135
136/**
137 * @brief Starts the input capture.
138 *
139 * @param[in] icup pointer to the @p ICUDriver object
140 *
141 * @api
142 */
144
145 osalDbgCheck(icup != NULL);
146
147 osalSysLock();
148 osalDbgAssert(icup->state == ICU_READY, "invalid state");
149 icuStartCaptureI(icup);
151}
152
153/**
154 * @brief Waits for a completed capture.
155 * @note The operation could be performed in polled mode depending on.
156 * @note In order to use this function notifications must be disabled.
157 * @pre The driver must be in @p ICU_WAITING or @p ICU_ACTIVE states.
158 * @post After the capture is available the driver is in @p ICU_ACTIVE
159 * state. If a capture fails then the driver is in @p ICU_WAITING
160 * state.
161 *
162 * @param[in] icup pointer to the @p ICUDriver object
163 * @return The capture status.
164 * @retval false if the capture is successful.
165 * @retval true if a timer overflow occurred.
166 *
167 * @api
168 */
170 bool result;
171
172 osalDbgCheck(icup != NULL);
173
174 osalSysLock();
175 osalDbgAssert((icup->state == ICU_WAITING) || (icup->state == ICU_ACTIVE),
176 "invalid state");
178 "notifications enabled");
179 result = icu_lld_wait_capture(icup);
180 icup->state = result ? ICU_WAITING : ICU_ACTIVE;
182
183 return result;
184}
185
186/**
187 * @brief Stops the input capture.
188 *
189 * @param[in] icup pointer to the @p ICUDriver object
190 *
191 * @api
192 */
194
195 osalDbgCheck(icup != NULL);
196
197 osalSysLock();
198 osalDbgAssert((icup->state == ICU_READY) || (icup->state == ICU_WAITING) ||
199 (icup->state == ICU_ACTIVE),
200 "invalid state");
201 icuStopCaptureI(icup);
203}
204
205/**
206 * @brief Enables notifications.
207 * @pre The ICU unit must have been activated using @p icuStart() and the
208 * capture started using @p icuStartCapture().
209 * @note If the notification is already enabled then the call has no effect.
210 *
211 * @param[in] icup pointer to the @p ICUDriver object
212 *
213 * @api
214 */
216
217 osalDbgCheck(icup != NULL);
218
219 osalSysLock();
220 osalDbgAssert((icup->state == ICU_WAITING) || (icup->state == ICU_ACTIVE),
221 "invalid state");
224}
225
226/**
227 * @brief Disables notifications.
228 * @pre The ICU unit must have been activated using @p icuStart() and the
229 * capture started using @p icuStartCapture().
230 * @note If the notification is already disabled then the call has no effect.
231 *
232 * @param[in] icup pointer to the @p ICUDriver object
233 *
234 * @api
235 */
237
238 osalDbgCheck(icup != NULL);
239
240 osalSysLock();
241 osalDbgAssert((icup->state == ICU_WAITING) || (icup->state == ICU_ACTIVE),
242 "invalid state");
245}
246
247#endif /* HAL_USE_ICU == TRUE */
248
249/** @} */
#define HAL_RET_SUCCESS
Definition hal.h:93
void icuObjectInit(ICUDriver *icup)
Initializes the standard part of a ICUDriver structure.
Definition hal_icu.c:68
void icuEnableNotifications(ICUDriver *icup)
Enables notifications.
Definition hal_icu.c:215
msg_t icuStart(ICUDriver *icup, const ICUConfig *config)
Configures and activates the ICU peripheral.
Definition hal_icu.c:83
bool icu_lld_wait_capture(ICUDriver *icup)
Waits for a completed capture.
#define icuDisableNotificationsI(icup)
Disables notifications.
Definition hal_icu.h:129
void icuStop(ICUDriver *icup)
Deactivates the ICU peripheral.
Definition hal_icu.c:120
void icuInit(void)
ICU Driver initialization.
Definition hal_icu.c:56
void icuStartCapture(ICUDriver *icup)
Starts the input capture.
Definition hal_icu.c:143
#define icuStopCaptureI(icup)
Stops the input capture.
Definition hal_icu.h:102
void icuDisableNotifications(ICUDriver *icup)
Disables notifications.
Definition hal_icu.c:236
void icu_lld_stop(ICUDriver *icup)
Deactivates the ICU peripheral.
#define icuEnableNotificationsI(icup)
Enables notifications.
Definition hal_icu.h:117
void icu_lld_start(ICUDriver *icup)
Configures and activates the ICU peripheral.
Definition hal_icu_lld.c:81
void icuStopCapture(ICUDriver *icup)
Stops the input capture.
Definition hal_icu.c:193
bool icuWaitCapture(ICUDriver *icup)
Waits for a completed capture.
Definition hal_icu.c:169
void icu_lld_init(void)
Low level ICU driver initialization.
Definition hal_icu_lld.c:66
#define icuStartCaptureI(icup)
Starts the input capture.
Definition hal_icu.h:90
#define icuAreNotificationsEnabledX(icup)
Check on notifications status.
Definition hal_icu.h:141
@ ICU_READY
Definition hal_icu.h:56
@ ICU_WAITING
Definition hal_icu.h:57
@ ICU_ACTIVE
Definition hal_icu.h:58
@ ICU_STOP
Definition hal_icu.h:55
static void osalSysLock(void)
Enters a critical zone from thread context.
Definition osal.h:601
static void osalSysUnlock(void)
Leaves a critical zone from thread context.
Definition osal.h:611
int32_t msg_t
Type of a message.
Definition osal.h:159
#define osalDbgAssert(c, remark)
Condition assertion.
Definition osal.h:264
#define osalDbgCheck(c)
Function parameters check.
Definition osal.h:284
HAL subsystem header.
Driver configuration structure.
Definition hal_icu_lld.h:82
Structure representing an ICU driver.
const ICUConfig * config
Current configuration data.
icustate_t state
Driver state.