rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
hal_gpt_lld.h
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2014 Derek Mulcahy
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 PITv1/hal_gpt_lld.h
19 * @brief KINETIS GPT subsystem low level driver header.
20 * @author andreika <prometheus.pcb@gmail.com>
21 *
22 * @addtogroup GPT
23 * @{
24 */
25
26#ifndef HAL_GPT_LLD_H_
27#define HAL_GPT_LLD_H_
28
29#if HAL_USE_GPT || defined(__DOXYGEN__)
30
31/*===========================================================================*/
32/* Driver constants. */
33/*===========================================================================*/
34
35/*===========================================================================*/
36/* Driver pre-compile time settings. */
37/*===========================================================================*/
38
39/**
40 * @name Configuration options
41 * @{
42 */
43/**
44 * @brief GPTD1 driver enable switch.
45 * @details If set to @p TRUE the support for GPTD1 is included.
46 * @note The default is @p TRUE.
47 */
48#if !defined(KINETIS_GPT_USE_PIT0) || defined(__DOXYGEN__)
49#define KINETIS_GPT_USE_PIT0 FALSE
50#endif
51
52/**
53 * @brief GPTD2 driver enable switch.
54 * @details If set to @p TRUE the support for GPTD2 is included.
55 * @note The default is @p TRUE.
56 */
57#if !defined(KINETIS_GPT_USE_PIT1) || defined(__DOXYGEN__)
58#define KINETIS_GPT_USE_PIT1 TRUE
59#endif
60
61/**
62 * @brief GPTD3 driver enable switch.
63 * @details If set to @p TRUE the support for GPTD3 is included.
64 * @note The default is @p TRUE.
65 */
66#if !defined(KINETIS_GPT_USE_PIT2) || defined(__DOXYGEN__)
67#define KINETIS_GPT_USE_PIT2 FALSE
68#endif
69
70/**
71 * @brief GPTD4 driver enable switch.
72 * @details If set to @p TRUE the support for GPTD4 is included.
73 * @note The default is @p TRUE.
74 */
75#if !defined(KINETIS_GPT_USE_PIT3) || defined(__DOXYGEN__)
76#define KINETIS_GPT_USE_PIT3 FALSE
77#endif
78
79/**
80 * @brief GPTD1 interrupt priority level setting.
81 */
82#if !defined(KINETIS_GPT_PIT0_IRQ_PRIORITY) || defined(__DOXYGEN__)
83#define KINETIS_GPT_PIT0_IRQ_PRIORITY 7
84#endif
85
86/**
87 * @brief GPTD2 interrupt priority level setting.
88 */
89#if !defined(KINETIS_GPT_PIT1_IRQ_PRIORITY) || defined(__DOXYGEN__)
90#define KINETIS_GPT_PIT1_IRQ_PRIORITY 7
91#endif
92
93/**
94 * @brief GPTD3 interrupt priority level setting.
95 */
96#if !defined(KINETIS_GPT_PIT2_IRQ_PRIORITY) || defined(__DOXYGEN__)
97#define KINETIS_GPT_PIT2_IRQ_PRIORITY 7
98#endif
99
100/**
101 * @brief GPTD4 interrupt priority level setting.
102 */
103#if !defined(KINETIS_GPT_PIT3_IRQ_PRIORITY) || defined(__DOXYGEN__)
104#define KINETIS_GPT_PIT3_IRQ_PRIORITY 7
105#endif
106
107/**
108 * @brief GPTD* common interrupt priority level setting.
109 */
110#if (KINETIS_HAS_PIT_COMMON_IRQ && !defined(KINETIS_GPT_PIT_IRQ_PRIORITY)) \
111 || defined(__DOXYGEN__)
112#define KINETIS_GPT_PIT_IRQ_PRIORITY 2
113#endif
114
115/*===========================================================================*/
116/* Derived constants and error checks. */
117/*===========================================================================*/
118
119#if KINETIS_GPT_USE_PIT0 && !KINETIS_HAS_PIT0
120#error "PIT0 not present in the selected device"
121#endif
122
123#if KINETIS_GPT_USE_PIT1 && !KINETIS_HAS_PIT1
124#error "PIT1 not present in the selected device"
125#endif
126
127#if KINETIS_GPT_USE_PIT2 && !KINETIS_HAS_PIT2
128#error "PIT2 not present in the selected device"
129#endif
130
131#if KINETIS_GPT_USE_PIT3 && !KINETIS_HAS_PIT3
132#error "PIT3 not present in the selected device"
133#endif
134
135#if !KINETIS_GPT_USE_PIT0 && !KINETIS_GPT_USE_PIT1 && \
136 !KINETIS_GPT_USE_PIT2 && !KINETIS_GPT_USE_PIT3
137#error "GPT driver activated but no PIT peripheral assigned"
138#endif
139
140#if KINETIS_GPT_USE_PIT0 && !KINETIS_HAS_PIT_COMMON_IRQ && \
141 !OSAL_IRQ_IS_VALID_PRIORITY(KINETIS_GPT_PIT0_IRQ_PRIORITY)
142#error "Invalid IRQ priority assigned to PIT0"
143#endif
144
145#if KINETIS_GPT_USE_PIT1 && !KINETIS_HAS_PIT_COMMON_IRQ && \
146 !OSAL_IRQ_IS_VALID_PRIORITY(KINETIS_GPT_PIT1_IRQ_PRIORITY)
147#error "Invalid IRQ priority assigned to PIT1"
148#endif
149
150#if KINETIS_GPT_USE_PIT2 && !KINETIS_HAS_PIT_COMMON_IRQ && \
151 !OSAL_IRQ_IS_VALID_PRIORITY(KINETIS_GPT_PIT2_IRQ_PRIORITY)
152#error "Invalid IRQ priority assigned to PIT2"
153#endif
154
155#if KINETIS_GPT_USE_PIT3 && !KINETIS_HAS_PIT_COMMON_IRQ && \
156 !OSAL_IRQ_IS_VALID_PRIORITY(KINETIS_GPT_PIT3_IRQ_PRIORITY)
157#error "Invalid IRQ priority assigned to PIT3"
158#endif
159
160#if KINETIS_HAS_PIT_COMMON_IRQ && \
161 !OSAL_IRQ_IS_VALID_PRIORITY(KINETIS_GPT_PIT_IRQ_PRIORITY)
162#error "Invalid IRQ priority assigned to PIT"
163#endif
164
165#if KINETIS_GPT_USE_PIT0 && !defined(KINETIS_PIT0_IRQ_VECTOR) && \
166 !KINETIS_HAS_PIT_COMMON_IRQ
167#error "KINETIS_PIT0_IRQ_VECTOR not defined"
168#endif
169
170#if KINETIS_GPT_USE_PIT1 && !defined(KINETIS_PIT1_IRQ_VECTOR) && \
171 !KINETIS_HAS_PIT_COMMON_IRQ
172#error "KINETIS_PIT1_IRQ_VECTOR not defined"
173#endif
174
175#if KINETIS_GPT_USE_PIT2 && !defined(KINETIS_PIT2_IRQ_VECTOR) && \
176 !KINETIS_HAS_PIT_COMMON_IRQ
177#error "KINETIS_PIT2_IRQ_VECTOR not defined"
178#endif
179
180#if KINETIS_GPT_USE_PIT3 && !defined(KINETIS_PIT3_IRQ_VECTOR) && \
181 !KINETIS_HAS_PIT_COMMON_IRQ
182#error "KINETIS_PIT3_IRQ_VECTOR not defined"
183#endif
184
185#if KINETIS_HAS_PIT_COMMON_IRQ && !defined(KINETIS_PIT_IRQ_VECTOR)
186#error "KINETIS_PIT_IRQ_VECTOR not defined"
187#endif
188
189/*===========================================================================*/
190/* Driver data structures and types. */
191/*===========================================================================*/
192
193/**
194 * @brief GPT frequency type.
195 */
196typedef uint32_t gptfreq_t;
197
198/**
199 * @brief GPT counter type.
200 */
201typedef uint32_t gptcnt_t;
202
203/**
204 * @brief Driver configuration structure.
205 * @note It could be empty on some architectures.
206 */
207typedef struct {
208 /**
209 * @brief Timer clock in Hz.
210 * @note The low level can use assertions in order to catch invalid
211 * frequency specifications.
212 */
214 /**
215 * @brief Timer callback pointer.
216 * @note This callback is invoked on GPT counter events.
217 * @note This callback can be set to @p NULL but in that case the
218 * one-shot mode cannot be used.
219 */
220 gptcallback_t callback;
221 /* End of the mandatory fields.*/
222 /* [andreika]: STM32-compatible fields */
223 /**
224 * @brief TIM CR2 register initialization data.
225 * @note The value of this field should normally be equal to zero.
226 */
227 uint32_t cr2;
228 /**
229 * @brief TIM DIER register initialization data.
230 * @note The value of this field should normally be equal to zero.
231 * @note Only the DMA-related bits can be specified in this field.
232 */
233 uint32_t dier;
234} GPTConfig;
235
236/**
237 * @brief Structure representing a GPT driver.
238 */
239struct GPTDriver {
240 /**
241 * @brief Driver state.
242 */
243 gptstate_t state;
244 /**
245 * @brief Current configuration data.
246 */
248#if defined(GPT_DRIVER_EXT_FIELDS)
249 GPT_DRIVER_EXT_FIELDS
250#endif
251 /* End of the mandatory fields.*/
252 /**
253 * @brief Timer base clock.
254 */
255 uint32_t clock;
256
257#ifdef KE1xF
258 /**
259 * @brief Channel index.
260 */
262#else
263 /**
264 * @brief Channel structure in PIT registers block.
265 */
266 struct PIT_CHANNEL *channel;
267#endif /* KE1xF */
268};
269
270/*===========================================================================*/
271/* Driver macros. */
272/*===========================================================================*/
273
274/**
275 * @brief Changes the interval of GPT peripheral.
276 * @details This function changes the interval of a running GPT unit.
277 * @pre The GPT unit must be running in continuous mode.
278 * @post The GPT unit interval is changed to the new value.
279 * @note The function has effect at the next cycle start.
280 *
281 * @param[in] gptp pointer to a @p GPTDriver object
282 * @param[in] interval new cycle time in timer ticks
283 *
284 * @notapi
285 */
286#ifdef KE1xF
287#define gpt_lld_change_interval(gptp, interval) \
288 (PIT->CHANNEL[(gptp)->channelIndex].TVAL = (uint32_t)( \
289 ( (gptp)->clock / (gptp)->config->frequency ) * \
290 ( interval ) ))
291#else
292#define gpt_lld_change_interval(gptp, interval) \
293 ((gptp)->channel->TVAL = (uint32_t)( \
294 ( (gptp)->clock / (gptp)->config->frequency ) * \
295 ( interval ) ))
296#endif
297
298/**
299 * @brief Returns the interval of GPT peripheral.
300 * @pre The GPT unit must be running in continuous mode.
301 *
302 * @param[in] gptp pointer to a @p GPTDriver object
303 * @return The current interval.
304 *
305 * @notapi
306 */
307#ifdef KE1xF
308#define gpt_lld_get_interval(gptp) \
309 ((uint32_t)( ( (uint64_t)PIT->CHANNEL[((gptp)->channelIndex].TVAL * (gptp)->config->frequency ) / \
310 ( (uint32_t)(gptp)->clock ) ))
311#else
312#define gpt_lld_get_interval(gptp) \
313 ((uint32_t)( ( (uint64_t)(gptp)->channel->TVAL * (gptp)->config->frequency ) / \
314 ( (uint32_t)(gptp)->clock ) ))
315#endif
316
317/**
318 * @brief Returns the counter value of GPT peripheral.
319 * @pre The GPT unit must be running in continuous mode.
320 * @note The nature of the counter is not defined, it may count upward
321 * or downward, it could be continuously running or not.
322 *
323 * @param[in] gptp pointer to a @p GPTDriver object
324 * @return The current counter value.
325 *
326 * @notapi
327 */
328#ifdef KE1xF
329#define gpt_lld_get_counter(gptp) ((gptcnt_t)PIT->CHANNEL[(gptp)->channelIndex].CVAL)
330#else
331/* [andreika]: it seems that something is wrong here in the original code... */
332#define gpt_lld_get_counter(gptp) ((gptcnt_t)(gptp)->pit->CHANNEL[gptp->channel].CVAL)
333#endif
334
335/*===========================================================================*/
336/* External declarations. */
337/*===========================================================================*/
338
339#if KINETIS_GPT_USE_PIT0 && !defined(__DOXYGEN__)
340extern GPTDriver GPTD1;
341#endif
342
343#if KINETIS_GPT_USE_PIT1 && !defined(__DOXYGEN__)
344extern GPTDriver GPTD2;
345#endif
346
347#if KINETIS_GPT_USE_PIT2 && !defined(__DOXYGEN__)
348extern GPTDriver GPTD3;
349#endif
350
351#if KINETIS_GPT_USE_PIT3 && !defined(__DOXYGEN__)
352extern GPTDriver GPTD4;
353#endif
354
355#ifdef __cplusplus
356extern "C" {
357#endif
358 void gpt_lld_init(void);
359 void gpt_lld_start(GPTDriver *gptp);
360 void gpt_lld_stop(GPTDriver *gptp);
362 void gpt_lld_stop_timer(GPTDriver *gptp);
363 void gpt_lld_polled_delay(GPTDriver *gptp, gptcnt_t interval);
364#ifdef __cplusplus
365}
366#endif
367
368#endif /* HAL_USE_GPT */
369
370#endif /* HAL_GPT_LLD_H_ */
371
372/** @} */
beuint32_t period
void gpt_lld_start(GPTDriver *gptp)
Configures and activates the GPT peripheral.
uint32_t dier
TIM DIER register initialization data.
void gpt_lld_polled_delay(GPTDriver *gptp, gptcnt_t interval)
Starts the timer in one shot mode and waits for completion.
void gpt_lld_stop(GPTDriver *gptp)
Deactivates the GPT peripheral.
struct PIT_CHANNEL * channel
Channel structure in PIT registers block.
GPTDriver GPTD1
GPTD1 driver identifier.
Definition hal_gpt_lld.c:43
GPTDriver GPTD3
GPTD3 driver identifier.
Definition hal_gpt_lld.c:59
void gpt_lld_start_timer(GPTDriver *gptp, gptcnt_t interval)
Starts the timer in continuous mode.
GPTDriver GPTD2
GPTD2 driver identifier.
Definition hal_gpt_lld.c:51
const GPTConfig * config
Current configuration data.
uint32_t gptcnt_t
GPT counter type.
uint32_t gptfreq_t
GPT frequency type.
gptfreq_t frequency
Timer clock in Hz.
GPTDriver GPTD4
GPTD4 driver identifier.
Definition hal_gpt_lld.c:67
uint32_t cr2
TIM CR2 register initialization data.
void gpt_lld_stop_timer(GPTDriver *gptp)
Stops the timer.
gptstate_t state
Driver state.
void gpt_lld_init(void)
Low level GPT driver initialization.
GPT_DRIVER_EXT_FIELDS uint32_t clock
Timer base clock.
gptcallback_t callback
Timer callback pointer.
int32_t channelIndex
Channel index.
Driver configuration structure.
Structure representing a GPT driver.