rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
hal_pal_lld.h
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2014-2015 Fabio Utzig
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 GPIOv2/hal_pal_lld.h
19 * @brief PAL subsystem low level driver header.
20 * @author andreika <prometheus.pcb@gmail.com>
21 *
22 * @addtogroup PAL
23 * @{
24 */
25
26#ifndef HAL_PAL_LLD_H_
27#define HAL_PAL_LLD_H_
28
29#if HAL_USE_PAL || defined(__DOXYGEN__)
30
31/*===========================================================================*/
32/* Unsupported modes and specific modes */
33/*===========================================================================*/
34
35#define PAL_MODE_ALTERNATIVE_1 0x10
36#define PAL_MODE_ALTERNATIVE_2 0x11
37#define PAL_MODE_ALTERNATIVE_3 0x12
38#define PAL_MODE_ALTERNATIVE_4 0x13
39#define PAL_MODE_ALTERNATIVE_5 0x14
40#define PAL_MODE_ALTERNATIVE_6 0x15
41#define PAL_MODE_ALTERNATIVE_7 0x16
42
43#ifdef KE1xF
44#define PIN_MUX_ALTERNATIVE(x) PORT_PCR_MUX(x)
45// STM32-compatible macro
46#define PAL_MODE_ALTERNATE(x) (PAL_MODE_ALTERNATIVE_1 + x - 1)
47#else
48#define PIN_MUX_ALTERNATIVE(x) PORTx_PCRn_MUX(x)
49#endif
50
51/*===========================================================================*/
52/* I/O Ports Types and constants. */
53/*===========================================================================*/
54
55#define TOTAL_PORTS 5
56#define PADS_PER_PORT 32
57
58/**
59 * @brief Width, in bits, of an I/O port.
60 */
61#define PAL_IOPORTS_WIDTH 32
62
63/**
64 * @brief Whole port mask.
65 * @brief This macro specifies all the valid bits into a port.
66 */
67#define PAL_WHOLE_PORT ((ioportmask_t)0xFFFFFFFF)
68
69#ifdef KE1xF
70/* Specifies palInit() without parameter, required until all platforms will
71 be updated to the new style.*/
72#define PAL_NEW_INIT
73#endif
74
75/**
76 * @brief Digital I/O port sized unsigned type.
77 */
78typedef uint32_t ioportmask_t;
79
80/**
81 * @brief Digital I/O modes.
82 */
83typedef uint32_t iomode_t;
84
85/**
86 * @brief Type of an I/O line.
87 */
88typedef uint32_t ioline_t;
89
90/**
91 * @brief Type of an event mode.
92 */
93typedef uint32_t ioeventmode_t;
94
95
96/**
97 * @brief Port Identifier.
98 * @details This type can be a scalar or some kind of pointer, do not make
99 * any assumption about it, use the provided macros when populating
100 * variables of this type.
101 */
102typedef GPIO_TypeDef *ioportid_t;
103
104/**
105 * @brief Type of an pad identifier.
106 */
107typedef uint32_t iopadid_t;
108
109/**
110 * @brief Port Configuration.
111 * @details This structure stores the configuration parameters of all pads
112 * belonging to a port.
113 */
114typedef struct {
116 iomode_t pads[PADS_PER_PORT];
117} PortConfig;
118
119/**
120 * @brief Generic I/O ports static initializer.
121 * @details An instance of this structure must be passed to @p palInit() at
122 * system startup time in order to initialized the digital I/O
123 * subsystem. This represents only the initial setup, specific pads
124 * or whole ports can be reprogrammed at later time.
125 * @note Implementations may extend this structure to contain more,
126 * architecture dependent, fields.
127 */
128typedef struct {
129 PortConfig ports[TOTAL_PORTS];
130} PALConfig;
131
132/*===========================================================================*/
133/* I/O Ports Identifiers. */
134/*===========================================================================*/
135
136/**
137 * @brief GPIO port A identifier.
138 */
139#define IOPORT1 GPIOA
140
141/**
142 * @brief GPIO port B identifier.
143 */
144#define IOPORT2 GPIOB
145
146/**
147 * @brief GPIO port C identifier.
148 */
149#define IOPORT3 GPIOC
150
151/**
152 * @brief GPIO port D identifier.
153 */
154#define IOPORT4 GPIOD
155
156/**
157 * @brief GPIO port E identifier.
158 */
159#define IOPORT5 GPIOE
160
161/**
162 * @name Line handling macros
163 * @{
164 */
165/**
166 * @brief Forms a line identifier.
167 * @details A port/pad pair are encoded into an @p ioline_t type. The encoding
168 * of this type is platform-dependent.
169 * @note In this driver the pad number is encoded in the byte of the GPIO
170 * address that's zero on all Kinetis devices.
171 */
172#define PAL_LINE(port, pad) \
173 ((ioline_t)((uint32_t)(port) | ((uint32_t)(pad)<<20)))
174
175/**
176 * @brief Decodes a port identifier from a line identifier.
177 */
178#define PAL_PORT(line) \
179 ((GPIO_TypeDef *)(((uint32_t)(line)) & 0xF00FFFFFU))
180
181/**
182 * @brief Decodes a pad identifier from a line identifier.
183 */
184#define PAL_PAD(line) \
185 ((uint32_t)((uint32_t)(line) & 0x0FF00000U)>>20)
186
187/**
188 * @brief Decodes a port index (0..4) from a port identifier.
189 */
190#define PAL_PORT_INDEX(port) \
191 (((uint32_t)port - PORTA_BASE) >> 12)
192
193/**
194 * @brief Value identifying an invalid line.
195 */
196#define PAL_NOLINE 0U
197/** @} */
198
199/*===========================================================================*/
200/* Implementation, some of the following macros could be implemented as */
201/* functions, if so please put them in pal_lld.c. */
202/*===========================================================================*/
203
204/**
205 * @brief Low level PAL subsystem initialization.
206 *
207 * @param[in] config architecture-dependent ports configuration
208 *
209 * @notapi
210 */
211#define pal_lld_init(config) _pal_lld_init(config)
212
213/**
214 * @brief Reads the physical I/O port states.
215 *
216 * @param[in] port port identifier
217 * @return The port bits.
218 *
219 * @notapi
220 */
221#define pal_lld_readport(port) \
222 (port)->PDIR
223
224/**
225 * @brief Reads the output latch.
226 * @details The purpose of this function is to read back the latched output
227 * value.
228 *
229 * @param[in] port port identifier
230 * @return The latched logical states.
231 *
232 * @notapi
233 */
234#define pal_lld_readlatch(port) \
235 (port)->PDOR
236
237/**
238 * @brief Writes a bits mask on a I/O port.
239 *
240 * @param[in] port port identifier
241 * @param[in] bits bits to be written on the specified port
242 *
243 * @notapi
244 */
245#define pal_lld_writeport(port, bits) \
246 (port)->PDOR = (bits)
247
248/**
249 * @brief Sets a bits mask on a I/O port.
250 * @note The @ref PAL provides a default software implementation of this
251 * functionality, implement this function if can optimize it by using
252 * special hardware functionalities or special coding.
253 *
254 * @param[in] port port identifier
255 * @param[in] bits bits to be ORed on the specified port
256 *
257 * @notapi
258 */
259#define pal_lld_setport(port, bits) \
260 (port)->PSOR = (bits)
261
262/**
263 * @brief Clears a bits mask on a I/O port.
264 * @note The @ref PAL provides a default software implementation of this
265 * functionality, implement this function if can optimize it by using
266 * special hardware functionalities or special coding.
267 *
268 * @param[in] port port identifier
269 * @param[in] bits bits to be cleared on the specified port
270 *
271 * @notapi
272 */
273#define pal_lld_clearport(port, bits) \
274 (port)->PCOR = (bits)
275
276/**
277 * @brief Toggles a bits mask on a I/O port.
278 * @note The @ref PAL provides a default software implementation of this
279 * functionality, implement this function if can optimize it by using
280 * special hardware functionalities or special coding.
281 *
282 * @param[in] port port identifier
283 * @param[in] bits bits to be toggled on the specified port
284 *
285 * @notapi
286 */
287#define pal_lld_toggleport(port, bits) \
288 (port)->PTOR = (bits)
289
290/**
291 * @brief Reads a group of bits.
292 * @note The @ref PAL provides a default software implementation of this
293 * functionality, implement this function if can optimize it by using
294 * special hardware functionalities or special coding.
295 *
296 * @param[in] port port identifier
297 * @param[in] mask group mask
298 * @param[in] offset group bit offset within the port
299 * @return The group logical states.
300 *
301 * @notapi
302 */
303#define pal_lld_readgroup(port, mask, offset) 0
304
305/**
306 * @brief Writes a group of bits.
307 * @note The @ref PAL provides a default software implementation of this
308 * functionality, implement this function if can optimize it by using
309 * special hardware functionalities or special coding.
310 *
311 * @param[in] port port identifier
312 * @param[in] mask group mask
313 * @param[in] offset group bit offset within the port
314 * @param[in] bits bits to be written. Values exceeding the group width
315 * are masked.
316 *
317 * @notapi
318 */
319#define pal_lld_writegroup(port, mask, offset, bits) (void)bits
320
321/**
322 * @brief Pads group mode setup.
323 * @details This function programs a pads group belonging to the same port
324 * with the specified mode.
325 * @note Programming an unknown or unsupported mode is silently ignored.
326 *
327 * @param[in] port port identifier
328 * @param[in] mask group mask
329 * @param[in] offset group bit offset within the port
330 * @param[in] mode group mode
331 *
332 * @notapi
333 */
334#define pal_lld_setgroupmode(port, mask, offset, mode) \
335 _pal_lld_setgroupmode(port, mask << offset, mode)
336
337/**
338 * @brief Reads a logical state from an I/O pad.
339 * @note The @ref PAL provides a default software implementation of this
340 * functionality, implement this function if can optimize it by using
341 * special hardware functionalities or special coding.
342 *
343 * @param[in] port port identifier
344 * @param[in] pad pad number within the port
345 * @return The logical state.
346 * @retval PAL_LOW low logical state.
347 * @retval PAL_HIGH high logical state.
348 *
349 * @notapi
350 */
351#define pal_lld_readpad(port, pad) _pal_lld_readpad(port, pad)
352
353/**
354 * @brief Writes a logical state on an output pad.
355 * @note This function is not meant to be invoked directly by the
356 * application code.
357 * @note The @ref PAL provides a default software implementation of this
358 * functionality, implement this function if can optimize it by using
359 * special hardware functionalities or special coding.
360 *
361 * @param[in] port port identifier
362 * @param[in] pad pad number within the port
363 * @param[in] bit logical value, the value must be @p PAL_LOW or
364 * @p PAL_HIGH
365 *
366 * @notapi
367 */
368#define pal_lld_writepad(port, pad, bit) _pal_lld_writepad(port, pad, bit)
369
370/**
371 * @brief Sets a pad logical state to @p PAL_HIGH.
372 * @note The @ref PAL provides a default software implementation of this
373 * functionality, implement this function if can optimize it by using
374 * special hardware functionalities or special coding.
375 *
376 * @param[in] port port identifier
377 * @param[in] pad pad number within the port
378 *
379 * @notapi
380 */
381#define pal_lld_setpad(port, pad) (port)->PSOR = ((uint32_t) 1 << (pad))
382
383/**
384 * @brief Clears a pad logical state to @p PAL_LOW.
385 * @note The @ref PAL provides a default software implementation of this
386 * functionality, implement this function if can optimize it by using
387 * special hardware functionalities or special coding.
388 *
389 * @param[in] port port identifier
390 * @param[in] pad pad number within the port
391 *
392 * @notapi
393 */
394#define pal_lld_clearpad(port, pad) (port)->PCOR = ((uint32_t) 1 << (pad))
395
396/**
397 * @brief Toggles a pad logical state.
398 * @note The @ref PAL provides a default software implementation of this
399 * functionality, implement this function if can optimize it by using
400 * special hardware functionalities or special coding.
401 *
402 * @param[in] port port identifier
403 * @param[in] pad pad number within the port
404 *
405 * @notapi
406 */
407#define pal_lld_togglepad(port, pad) (port)->PTOR = ((uint32_t) 1 << (pad))
408
409/**
410 * @brief Pad mode setup.
411 * @details This function programs a pad with the specified mode.
412 * @note The @ref PAL provides a default software implementation of this
413 * functionality, implement this function if can optimize it by using
414 * special hardware functionalities or special coding.
415 * @note Programming an unknown or unsupported mode is silently ignored.
416 *
417 * @param[in] port port identifier
418 * @param[in] pad pad number within the port
419 * @param[in] mode pad mode
420 *
421 * @notapi
422 */
423#define pal_lld_setpadmode(port, pad, mode) \
424 _pal_lld_setpadmode(port, pad, mode)
425
426
427#if (PAL_USE_WAIT == TRUE) || (PAL_USE_CALLBACKS == TRUE)
428
429#define KINETIS_GPIO_NUM_PORTS 5
430#define KINETIS_GPIO_NUM_PADS 16
431#define KINETIS_GPIO_NUM_LINES (/*KINETIS_GPIO_NUM_PORTS * */KINETIS_GPIO_NUM_PADS)
432
433/**
434 * @brief Pad event enable.
435 * @note Programming an unknown or unsupported mode is silently ignored.
436 *
437 * @param[in] port port identifier
438 * @param[in] pad pad number within the port
439 * @param[in] mode pad event mode
440 *
441 * @notapi
442 */
443#define pal_lld_enablepadevent(port, pad, mode) \
444 _pal_lld_enablepadevent(port, pad, mode)
445
446/**
447 * @brief Pad event disable.
448 * @details This function disables previously programmed event callbacks.
449 *
450 * @param[in] port port identifier
451 * @param[in] pad pad number within the port
452 *
453 * @notapi
454 */
455#define pal_lld_disablepadevent(port, pad) \
456 _pal_lld_disablepadevent(port, pad)
457
458/**
459 * @brief Returns a PAL event structure associated to a pad.
460 *
461 * @param[in] port port identifier
462 * @param[in] pad pad number within the port
463 *
464 * @notapi
465 */
466#define pal_lld_get_pad_event(port, pad) \
467 &_pal_events[/*PAL_PORT_INDEX(port) * KINETIS_GPIO_NUM_PADS + */(pad)]
468
469/**
470 * @brief Returns a PAL event structure associated to a line.
471 *
472 * @param[in] line line identifier
473 *
474 * @notapi
475 */
476#define pal_lld_get_line_event(line) \
477 pal_lld_get_pad_event(PAL_PORT(line), PAL_PAD(line))
478
479extern palevent_t _pal_events[KINETIS_GPIO_NUM_LINES];
480#endif /* PAL_USE_WAIT */
481
482
483#ifdef __cplusplus
484extern "C" {
485#endif
486#ifdef KE1xF
487 void _pal_lld_init(void);
488#else
489 void _pal_lld_init(const PALConfig *config);
490#endif
492 ioportmask_t mask,
493 iomode_t mode);
495 uint8_t pad,
496 iomode_t mode);
497 uint8_t _pal_lld_readpad(ioportid_t port,
498 uint8_t pad);
500 uint8_t pad,
501 uint8_t bit);
502#if PAL_USE_CALLBACKS || PAL_USE_WAIT
505 ioeventmode_t mode);
507#endif
508#ifdef __cplusplus
509}
510#endif
511
512#endif /* HAL_USE_PAL */
513
514#endif /* HAL_PAL_LLD_H_ */
515
516/** @} */
uint8_t pad[3]
static ioportid_t ports[]
static constexpr persistent_config_s * config
void _pal_lld_setpadmode(ioportid_t port, uint8_t pad, iomode_t mode)
Pad mode setup.
void _pal_lld_writepad(ioportid_t port, uint8_t pad, uint8_t bit)
Writes a logical state on an output pad.
uint32_t ioeventmode_t
Type of an event mode.
Definition hal_pal_lld.h:93
uint32_t ioportmask_t
Digital I/O port sized unsigned type.
Definition hal_pal_lld.h:78
void _pal_lld_init(void)
Kinetis I/O ports configuration.
palevent_t _pal_events[KINETIS_GPIO_NUM_LINES]
Event records for the 16 GPIO EXTI channels.
Definition hal_pal_lld.c:45
uint8_t _pal_lld_readpad(ioportid_t port, uint8_t pad)
Reads a logical state from an I/O pad.
void _pal_lld_enablepadevent(ioportid_t port, iopadid_t pad, ioeventmode_t mode)
void _pal_lld_disablepadevent(ioportid_t port, iopadid_t pad)
void _pal_lld_setgroupmode(ioportid_t port, ioportmask_t mask, iomode_t mode)
Pads mode setup.
GPIO_TypeDef * ioportid_t
Port Identifier.
uint32_t iomode_t
Digital I/O modes.
Definition hal_pal_lld.h:83
uint32_t iopadid_t
Type of an pad identifier.
uint32_t ioline_t
Type of an I/O line.
Definition hal_pal_lld.h:88
Generic I/O ports static initializer.
Port Configuration.
ioportid_t port