ChibiOS 21.11.4
hal_icu.h
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.h
19 * @brief ICU Driver macros and structures.
20 *
21 * @addtogroup ICU
22 * @{
23 */
24
25#ifndef HAL_ICU_H
26#define HAL_ICU_H
27
28#if (HAL_USE_ICU == TRUE) || defined(__DOXYGEN__)
29
30/*===========================================================================*/
31/* Driver constants. */
32/*===========================================================================*/
33
34/*===========================================================================*/
35/* Driver pre-compile time settings. */
36/*===========================================================================*/
37
38#if !defined(ICU_USE_OVERFLOW_SCALING)
39#define ICU_USE_OVERFLOW_SCALING FALSE
40#endif
41
42/*===========================================================================*/
43/* Derived constants and error checks. */
44/*===========================================================================*/
45
46/*===========================================================================*/
47/* Driver data structures and types. */
48/*===========================================================================*/
49
50/**
51 * @brief Driver state machine possible states.
52 */
53typedef enum {
54 ICU_UNINIT = 0, /**< Not initialized. */
55 ICU_STOP = 1, /**< Stopped. */
56 ICU_READY = 2, /**< Ready. */
57 ICU_WAITING = 3, /**< Waiting for first front. */
58 ICU_ACTIVE = 4 /**< First front detected. */
60
61/**
62 * @brief Type of a structure representing an ICU driver.
63 */
64typedef struct ICUDriver ICUDriver;
65
66/**
67 * @brief ICU notification callback type.
68 *
69 * @param[in] icup pointer to a @p ICUDriver object
70 */
71typedef void (*icucallback_t)(ICUDriver *icup);
72
73#include "hal_icu_lld.h"
74
75/*===========================================================================*/
76/* Driver macros. */
77/*===========================================================================*/
78
79/**
80 * @name Macro Functions
81 * @{
82 */
83/**
84 * @brief Starts the input capture.
85 *
86 * @param[in] icup pointer to the @p ICUDriver object
87 *
88 * @iclass
89 */
90#define icuStartCaptureI(icup) do { \
91 icu_lld_start_capture(icup); \
92 (icup)->state = ICU_WAITING; \
93} while (false)
94
95/**
96 * @brief Stops the input capture.
97 *
98 * @param[in] icup pointer to the @p ICUDriver object
99 *
100 * @iclass
101 */
102#define icuStopCaptureI(icup) do { \
103 icu_lld_stop_capture(icup); \
104 (icup)->state = ICU_READY; \
105} while (false)
106
107/**
108 * @brief Enables notifications.
109 * @pre The ICU unit must have been activated using @p icuStart() and the
110 * capture started using @p icuStartCapture().
111 * @note If the notification is already enabled then the call has no effect.
112 *
113 * @param[in] icup pointer to the @p ICUDriver object
114 *
115 * @iclass
116 */
117#define icuEnableNotificationsI(icup) icu_lld_enable_notifications(icup)
118
119/**
120 * @brief Disables notifications.
121 * @pre The ICU unit must have been activated using @p icuStart() and the
122 * capture started using @p icuStartCapture().
123 * @note If the notification is already disabled then the call has no effect.
124 *
125 * @param[in] icup pointer to the @p ICUDriver object
126 *
127 * @iclass
128 */
129#define icuDisableNotificationsI(icup) icu_lld_disable_notifications(icup)
130
131/**
132 * @brief Check on notifications status.
133 *
134 * @param[in] icup pointer to the @p ICUDriver object
135 * @return The notifications status.
136 * @retval false if notifications are not enabled.
137 * @retval true if notifications are enabled.
138 *
139 * @notapi
140 */
141#define icuAreNotificationsEnabledX(icup) \
142 icu_lld_are_notifications_enabled(icup)
143
144/**
145 * @brief Returns the width of the latest pulse.
146 * @details The pulse width is defined as number of ticks between the start
147 * edge and the stop edge.
148 * @note This function is meant to be invoked from the width capture
149 * callback.
150 *
151 * @param[in] icup pointer to the @p ICUDriver object
152 * @return The number of ticks.
153 *
154 * @xclass
155 */
156#define icuGetWidthX(icup) icu_lld_get_width(icup)
157
158/**
159 * @brief Returns the width of the latest cycle.
160 * @details The cycle width is defined as number of ticks between a start
161 * edge and the next start edge.
162 * @note This function is meant to be invoked from the width capture
163 * callback.
164 *
165 * @param[in] icup pointer to the @p ICUDriver object
166 * @return The number of ticks.
167 *
168 * @xclass
169 */
170#define icuGetPeriodX(icup) icu_lld_get_period(icup)
171/** @} */
172
173/**
174 * @name Low level driver helper macros
175 * @{
176 */
177/**
178 * @brief Common ISR code, ICU width event.
179 *
180 * @param[in] icup pointer to the @p ICUDriver object
181 *
182 * @notapi
183 */
184#define _icu_isr_invoke_width_cb(icup) do { \
185 if (((icup)->state == ICU_ACTIVE) && \
186 ((icup)->config->width_cb != NULL)) \
187 (icup)->config->width_cb(icup); \
188} while (0)
189
190/**
191 * @brief Common ISR code, ICU period event.
192 * @note A period event brings the driver into the @p ICU_ACTIVE state.
193 *
194 * @param[in] icup pointer to the @p ICUDriver object
195 *
196 * @notapi
197 */
198#define _icu_isr_invoke_period_cb(icup) do { \
199 if (((icup)->state == ICU_ACTIVE) && \
200 ((icup)->config->period_cb != NULL)) \
201 (icup)->config->period_cb(icup); \
202 (icup)->state = ICU_ACTIVE; \
203} while (0)
204
205#if ICU_USE_OVERFLOW_SCALING == TRUE
206/**
207 * @brief Common ISR code, ICU timer overflow event.
208 * @note An overflow when in @p ICU_ACTIVE state with a callback set
209 * executes the callback code (supports overflow scaling). Otherwise
210 * overflow brings the driver back to the @p ICU_WAITING state.
211 *
212 * @param[in] icup pointer to the @p ICUDriver object
213 *
214 * @notapi
215 */
216#define _icu_isr_invoke_overflow_cb(icup) do { \
217 if ((icup)->config->overflow_cb != NULL && (icup)->state == ICU_ACTIVE) \
218 (icup)->config->overflow_cb(icup); \
219 else \
220 (icup)->state = ICU_WAITING; \
221} while (0)
222
223#else /* ICU_USE_OVERFLOW_SCALING != TRUE */
224/**
225 * @brief Common ISR code, ICU timer overflow event.
226 * @note An overflow always brings the driver back to the @p ICU_WAITING
227 * state.
228 *
229 * @param[in] icup pointer to the @p ICUDriver object
230 *
231 * @notapi
232 */
233#define _icu_isr_invoke_overflow_cb(icup) do { \
234 (icup)->config->overflow_cb(icup); \
235 (icup)->state = ICU_WAITING; \
236} while (0)
237#endif /* ICU_USE_OVERFLOW_SCALING != TRUE */
238
239/** @} */
240
241/*===========================================================================*/
242/* External declarations. */
243/*===========================================================================*/
244
245#ifdef __cplusplus
246extern "C" {
247#endif
248 void icuInit(void);
249 void icuObjectInit(ICUDriver *icup);
250 msg_t icuStart(ICUDriver *icup, const ICUConfig *config);
251 void icuStop(ICUDriver *icup);
252 void icuStartCapture(ICUDriver *icup);
253 bool icuWaitCapture(ICUDriver *icup);
254 void icuStopCapture(ICUDriver *icup);
257#ifdef __cplusplus
258}
259#endif
260
261#endif /* HAL_USE_ICU == TRUE */
262
263#endif /* HAL_ICU_H */
264
265/** @} */
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
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
icustate_t
Driver state machine possible states.
Definition hal_icu.h:53
void(* icucallback_t)(ICUDriver *icup)
ICU notification callback type.
Definition hal_icu.h:71
void icuDisableNotifications(ICUDriver *icup)
Disables notifications.
Definition hal_icu.c:236
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
@ ICU_UNINIT
Definition hal_icu.h:54
@ 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
int32_t msg_t
Definition chearly.h:88
PLATFORM ICU subsystem low level driver header.
Driver configuration structure.
Definition hal_icu_lld.h:82
Structure representing an ICU driver.
const ICUConfig * config
Current configuration data.