aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/ports/KINETIS/LLD/PORTv1/hal_ext_lld.c
blob: c678d6a6c5b2443d8ac3879350483ace5a8bd846 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
    ChibiOS - Copyright (C) 2014 Derek Mulcahy

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

/**
 * @file    PORTv1/hal_ext_lld.c
 * @brief   KINETIS EXT subsystem low level driver source.
 *
 * @addtogroup EXT
 * @{
 */

#include "hal.h"

#if HAL_USE_EXT || defined(__DOXYGEN__)

/*===========================================================================*/
/* Driver local definitions.                                                 */
/*===========================================================================*/

#define PCR_IRQC_DISABLED           0x0
#define PCR_IRQC_DMA_RISING_EDGE    0x1
#define PCR_IRQC_DMA_FALLING_EDGE   0x2
#define PCR_IRQC_DMA_EITHER_EDGE    0x3

#define PCR_IRQC_LOGIC_ZERO         0x8
#define PCR_IRQC_RISING_EDGE        0x9
#define PCR_IRQC_FALLING_EDGE       0xA
#define PCR_IRQC_EITHER_EDGE        0xB
#define PCR_IRQC_LOGIC_ONE          0xC

/*===========================================================================*/
/* Driver exported variables.                                                */
/*===========================================================================*/

/**
 * @brief   EXTD1 driver identifier.
 */
EXTDriver EXTD1;

/*===========================================================================*/
/* Driver local variables and types.                                         */
/*===========================================================================*/

/* A channel map for each channel.
 *
 * The index is the pin number.
 * The result is the channel for that pin.
 */
#if KINETIS_EXT_PORTA_WIDTH > 0
uint8_t porta_channel_map[KINETIS_EXT_PORTA_WIDTH];
#endif
#if KINETIS_EXT_PORTB_WIDTH > 0
uint8_t portb_channel_map[KINETIS_EXT_PORTB_WIDTH];
#endif
#if KINETIS_EXT_PORTC_WIDTH > 0
uint8_t portc_channel_map[KINETIS_EXT_PORTC_WIDTH];
#endif
#if KINETIS_EXT_PORTD_WIDTH > 0
uint8_t portd_channel_map[KINETIS_EXT_PORTD_WIDTH];
#endif
#if KINETIS_EXT_PORTE_WIDTH > 0
uint8_t porte_channel_map[KINETIS_EXT_PORTE_WIDTH];
#endif

/*===========================================================================*/
/* Driver local functions.                                                   */
/*===========================================================================*/

/**
 * @brief   Enables EXTI IRQ sources.
 *
 * @notapi
 */
static void ext_lld_exti_irq_enable(void) {

#if KINETIS_EXT_PORTA_WIDTH > 0
  nvicEnableVector(PINA_IRQn, KINETIS_EXT_PORTA_IRQ_PRIORITY);
#endif

#if KINETIS_EXT_HAS_COMMON_BCDE_IRQ
#if  (KINETIS_EXT_PORTB_WIDTH > 0) || (KINETIS_EXT_PORTC_WIDTH > 0) \
  || (KINETIS_EXT_PORTD_WIDTH > 0) || (KINETIS_EXT_PORTE_WIDTH > 0)
  nvicEnableVector(PINBCDE_IRQn, KINETIS_EXT_PORTD_IRQ_PRIORITY);
#endif

#elif KINETIS_EXT_HAS_COMMON_CD_IRQ /* KINETIS_EXT_HAS_COMMON_BCDE_IRQ */
#if (KINETIS_EXT_PORTC_WIDTH > 0) || (KINETIS_EXT_PORTD_WIDTH > 0)
  nvicEnableVector(PINCD_IRQn, KINETIS_EXT_PORTD_IRQ_PRIORITY);
#endif

#else /* KINETIS_EXT_HAS_COMMON_CD_IRQ */
#if KINETIS_EXT_PORTB_WIDTH > 0
  nvicEnableVector(PINB_IRQn, KINETIS_EXT_PORTB_IRQ_PRIORITY);
#endif
#if KINETIS_EXT_PORTC_WIDTH > 0
  nvicEnableVector(PINC_IRQn, KINETIS_EXT_PORTC_IRQ_PRIORITY);
#endif
#if KINETIS_EXT_PORTD_WIDTH > 0
  nvicEnableVector(PIND_IRQn, KINETIS_EXT_PORTD_IRQ_PRIORITY);
#endif
#if KINETIS_EXT_PORTE_WIDTH > 0
  nvicEnableVector(PINE_IRQn, KINETIS_EXT_PORTE_IRQ_PRIORITY);
#endif
#endif /* !KINETIS_EXT_HAS_COMMON_CD_IRQ */
}

/**
 * @brief   Disables EXTI IRQ sources.
 *
 * @notapi
 */
static void ext_lld_exti_irq_disable(void) {

#if KINETIS_EXT_PORTA_WIDTH > 0
  nvicDisableVector(PINA_IRQn);
#endif

#if KINETIS_EXT_HAS_COMMON_BCDE_IRQ
#if  (KINETIS_EXT_PORTB_WIDTH > 0) || (KINETIS_EXT_PORTC_WIDTH > 0) \
  || (KINETIS_EXT_PORTD_WIDTH > 0) || (KINETIS_EXT_PORTE_WIDTH > 0)
  nvicDisableVector(PINBCDE_IRQn);
#endif

#elif KINETIS_EXT_HAS_COMMON_CD_IRQ /* KINETIS_EXT_HAS_COMMON_BCDE_IRQ */
#if (KINETIS_EXT_PORTC_WIDTH > 0) || (KINETIS_EXT_PORTD_WIDTH > 0)
  nvicDisableVector(PINCD_IRQn);
#endif

#else /* KINETIS_EXT_HAS_COMMON_CD_IRQ */
#if KINETIS_EXT_PORTB_WIDTH > 0
  nvicDisableVector(PINB_IRQn);
#endif
#if KINETIS_EXT_PORTC_WIDTH > 0
  nvicDisableVector(PINC_IRQn);
#endif
#if KINETIS_EXT_PORTD_WIDTH > 0
  nvicDisableVector(PIND_IRQn);
#endif
#if KINETIS_EXT_PORTE_WIDTH > 0
  nvicDisableVector(PINE_IRQn);
#endif
#endif /* !KINETIS_EXT_HAS_COMMON_CD_IRQ */
}

/*===========================================================================*/
/* Driver interrupt handlers.                                                */
/*===========================================================================*/

/*
 * Generic interrupt handler.
 */
static inline void irq_handler(PORT_TypeDef * const port, const unsigned port_width, const uint8_t *channel_map) {
  unsigned pin;
  uint32_t isfr = port->ISFR;

  /* Clear all pending interrupts on this port. */
  port->ISFR = 0xFFFFFFFF;

  for (pin = 0; pin < port_width; pin++) {
    if (isfr & (1 << pin)) {
      expchannel_t channel = channel_map[pin];
      EXTD1.config->channels[channel].cb(&EXTD1, channel);
    }
  }
}

/**
 * @brief   PORTA interrupt handler.
 *
 * @isr
 */
#if defined(KINETIS_PORTA_IRQ_VECTOR) && KINETIS_EXT_PORTA_WIDTH > 0
OSAL_IRQ_HANDLER(KINETIS_PORTA_IRQ_VECTOR) {
  OSAL_IRQ_PROLOGUE();

  irq_handler(PORTA, KINETIS_EXT_PORTA_WIDTH, porta_channel_map);

  OSAL_IRQ_EPILOGUE();
}
#endif /* KINETIS_EXT_PORTA_WIDTH > 0 */

#if KINETIS_EXT_HAS_COMMON_BCDE_IRQ

#if defined(KINETIS_PORTD_IRQ_VECTOR)
OSAL_IRQ_HANDLER(KINETIS_PORTD_IRQ_VECTOR) {
  OSAL_IRQ_PROLOGUE();

#if (KINETIS_EXT_PORTB_WIDTH > 0)
  irq_handler(PORTB, KINETIS_EXT_PORTB_WIDTH, portb_channel_map);
#endif
#if (KINETIS_EXT_PORTC_WIDTH > 0)
  irq_handler(PORTC, KINETIS_EXT_PORTC_WIDTH, portc_channel_map);
#endif
#if (KINETIS_EXT_PORTD_WIDTH > 0)
  irq_handler(PORTD, KINETIS_EXT_PORTD_WIDTH, portd_channel_map);
#endif
#if (KINETIS_EXT_PORTE_WIDTH > 0)
  irq_handler(PORTE, KINETIS_EXT_PORTE_WIDTH, porte_channel_map);
#endif

  OSAL_IRQ_EPILOGUE();
}
#endif /* defined(KINETIS_PORTD_IRQ_VECTOR) */

#elif KINETIS_EXT_HAS_COMMON_CD_IRQ /* KINETIS_EXT_HAS_COMMON_BCDE_IRQ */

#if defined(KINETIS_PORTD_IRQ_VECTOR)
OSAL_IRQ_HANDLER(KINETIS_PORTD_IRQ_VECTOR) {
  OSAL_IRQ_PROLOGUE();

#if (KINETIS_EXT_PORTC_WIDTH > 0)
  irq_handler(PORTC, KINETIS_EXT_PORTC_WIDTH, portc_channel_map);
#endif
#if (KINETIS_EXT_PORTD_WIDTH > 0)
  irq_handler(PORTD, KINETIS_EXT_PORTD_WIDTH, portd_channel_map);
#endif

  OSAL_IRQ_EPILOGUE();
}
#endif /* defined(KINETIS_PORTD_IRQ_VECTOR) */


#else /* KINETIS_EXT_HAS_COMMON_CD_IRQ */

/**
 * @brief   PORTB interrupt handler.
 *
 * @isr
 */
#if defined(KINETIS_PORTB_IRQ_VECTOR) && KINETIS_EXT_PORTB_WIDTH > 0
OSAL_IRQ_HANDLER(KINETIS_PORTB_IRQ_VECTOR) {
  OSAL_IRQ_PROLOGUE();

  irq_handler(PORTB, KINETIS_EXT_PORTB_WIDTH, portb_channel_map);

  OSAL_IRQ_EPILOGUE();
}
#endif /* KINETIS_EXT_PORTB_WIDTH > 0 */

/**
 * @brief   PORTC interrupt handler.
 *
 * @isr
 */
#if defined(KINETIS_PORTC_IRQ_VECTOR) && KINETIS_EXT_PORTC_WIDTH > 0
OSAL_IRQ_HANDLER(KINETIS_PORTC_IRQ_VECTOR) {
  OSAL_IRQ_PROLOGUE();

  irq_handler(PORTC, KINETIS_EXT_PORTC_WIDTH, portc_channel_map);

  OSAL_IRQ_EPILOGUE();
}
#endif /* KINETIS_EXT_PORTC_WIDTH > 0 */

/**
 * @brief   PORTD interrupt handler.
 *
 * @isr
 */
#if defined(KINETIS_PORTD_IRQ_VECTOR) && KINETIS_EXT_PORTD_WIDTH > 0
OSAL_IRQ_HANDLER(KINETIS_PORTD_IRQ_VECTOR) {
  OSAL_IRQ_PROLOGUE();

  irq_handler(PORTD, KINETIS_EXT_PORTD_WIDTH, portd_channel_map);

  OSAL_IRQ_EPILOGUE();
}
#endif /* KINETIS_EXT_PORTD_WIDTH > 0 */

/**
 * @brief   PORTE interrupt handler.
 *
 * @isr
 */
#if defined(KINETIS_PORTE_IRQ_VECTOR) && KINETIS_EXT_PORTE_WIDTH > 0
OSAL_IRQ_HANDLER(KINETIS_PORTE_IRQ_VECTOR) {
  OSAL_IRQ_PROLOGUE();

  irq_handler(PORTE, KINETIS_EXT_PORTE_WIDTH, porte_channel_map);

  OSAL_IRQ_EPILOGUE();
}
#endif /* KINETIS_EXT_PORTE_WIDTH > 0 */

#endif /* !KINETIS_EXT_HAS_COMMON_CD_IRQ */

/*===========================================================================*/
/* Driver exported functions.                                                */
/*===========================================================================*/

/**
 * @brief   Low level EXT driver initialization.
 *
 * @notapi
 */
void ext_lld_init(void) {

  /* Driver initialization.*/
  extObjectInit(&EXTD1);
}

/**
 * @brief   Configures and activates the EXT peripheral.
 *
 * @param[in] extp      pointer to the @p EXTDriver object
 *
 * @notapi
 */
void ext_lld_start(EXTDriver *extp) {
  expchannel_t channel;

  if (extp->state == EXT_STOP)
    ext_lld_exti_irq_enable();

  /* Configuration of automatic channels.*/
  for (channel = 0; channel < EXT_MAX_CHANNELS; channel++) {

    uint32_t mode = extp->config->channels[channel].mode;
    PORT_TypeDef *port = extp->config->channels[channel].port;
    uint32_t pin = extp->config->channels[channel].pin;

    /* Initialize the channel map */
#if KINETIS_EXT_PORTA_WIDTH > 0
    if (port == PORTA)
      porta_channel_map[pin] = channel;
    else
#endif
#if KINETIS_EXT_PORTB_WIDTH > 0
    if (port == PORTB)
      portb_channel_map[pin] = channel;
    else
#endif
#if KINETIS_EXT_PORTC_WIDTH > 0
    if (port == PORTC)
      portc_channel_map[pin] = channel;
    else
#endif
#if KINETIS_EXT_PORTD_WIDTH > 0
    if (port == PORTD)
      portd_channel_map[pin] = channel;
    else
#endif
#if KINETIS_EXT_PORTE_WIDTH > 0
    if (port == PORTE)
      porte_channel_map[pin] = channel;
    else
#endif
    {}

    if (mode & EXT_CH_MODE_AUTOSTART)
      ext_lld_channel_enable(extp, channel);
    else if (port != NULL)
      ext_lld_channel_disable(extp, channel);
  }
}

/**
 * @brief   Deactivates the EXT peripheral.
 *
 * @param[in] extp      pointer to the @p EXTDriver object
 *
 * @notapi
 */
void ext_lld_stop(EXTDriver *extp) {

  if (extp->state == EXT_ACTIVE)
    ext_lld_exti_irq_disable();
}

/**
 * @brief   Enables an EXT channel.
 *
 * @param[in] extp      pointer to the @p EXTDriver object
 * @param[in] channel   channel to be enabled
 *
 * @notapi
 */
void ext_lld_channel_enable(EXTDriver *extp, expchannel_t channel) {

  uint32_t irqc;
  uint32_t mode = extp->config->channels[channel].mode;
  if (mode & EXT_CH_MODE_RISING_EDGE)
    irqc = PCR_IRQC_RISING_EDGE;
  else if (extp->config->channels[channel].mode & EXT_CH_MODE_FALLING_EDGE)
    irqc = PCR_IRQC_FALLING_EDGE;
  else if (extp->config->channels[channel].mode & EXT_CH_MODE_BOTH_EDGES)
    irqc = PCR_IRQC_EITHER_EDGE;
  else
    irqc = PCR_IRQC_DISABLED;

  PORT_TypeDef *port = extp->config->channels[channel].port;
  uint32_t pin = extp->config->channels[channel].pin;

  uint32_t pcr = port->PCR[pin];

  /* Clear all the IRQC bits */
  pcr &= ~PORTx_PCRn_IRQC_MASK;
  /* Set the required IRQC bits */
  pcr |= PORTx_PCRn_IRQC(irqc);

  port->PCR[pin] = pcr;
}

/**
 * @brief   Disables an EXT channel.
 *
 * @param[in] extp      pointer to the @p EXTDriver object
 * @param[in] channel   channel to be disabled
 *
 * @notapi
 */
void ext_lld_channel_disable(EXTDriver *extp, expchannel_t channel) {

  PORT_TypeDef *port = extp->config->channels[channel].port;
  uint32_t pin = extp->config->channels[channel].pin;
  port->PCR[pin] |= PORTx_PCRn_IRQC(PCR_IRQC_DISABLED);
}

#endif /* HAL_USE_EXT */

/** @} */