aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/ports/AVR/hal_ext_lld.c
blob: 6eccf924aaeb0a1bf8d176cf464013db8843db77 (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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
  EXT Low Level Driver for ChibiOS
  Copyright (C) 2015 Igor Stoppa <igor.stoppa@gmail.com>
  Copyright (C) 2016 Theodore Ateba

  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  AVR/hal_ext_lld.c
 * @brief AVR EXT subsystem low level driver source.
 *
 * @addtogroup EXT
 * @{
 */

#include "hal.h"

#if HAL_USE_EXT || defined(__DOXYGEN__)

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

#define EXT_EICRA_LOW_LEVEL    0
#define EXT_EICRA_BOTH_EDGES   1
#define EXT_EICRA_FALLING_EDGE 2
#define EXT_EICRA_RISING_EDGE  3

/**
 * @brief   Declares the isr for the ext channel specified
 *
 * @param[in] channel      number of the channel
 *
 * @notapi
 */
#define declare_extint_isr(channel)                                   \
OSAL_IRQ_HANDLER(INT##channel##_vect)                                 \
{                                                                     \
  OSAL_IRQ_PROLOGUE();                                                \
  EXTD1.config->channels[EXT_INT##channel##_CHANNEL].                 \
    cb(&EXTD1, EXT_INT##channel##_CHANNEL);                           \
  OSAL_IRQ_EPILOGUE();                                                \
}

/**
 * @brief   Declares the isr for the pc channel specified
 *
 * @param[in] port      number of the port
 *
 * @notapi
 */
#define declare_pcint_isr(index)                                             \
OSAL_IRQ_HANDLER(PCINT##index##_vect) {                                      \
  uint8_t changed_pins;                                                      \
  uint8_t i;                                                                 \
                                                                             \
  OSAL_IRQ_PROLOGUE();                                                       \
  EXTD1.pc_current_values[index] = (*(PINS[index])) & (*(PCMSK[index]));     \
                                                                             \
  /* XOR to find the changed pin(s) */                                       \
  changed_pins = EXTD1.pc_current_values[index] ^ EXTD1.pc_old_values[index];\
                                                                             \
  for (i = 0; i < 8; i++) {                                                  \
    if (changed_pins & (1 << i)) {                                           \
      const EXTChannelConfig *chn =                                          \
        &EXTD1.config->channels[EXT_PCINT##index##_INDEX + i];               \
                                                                             \
      if (((chn->mode & EXT_CH_MODE_RISING_EDGE) &&                          \
           ((EXTD1.pc_current_values[index] & (1 << i)) > 0)) ||             \
          ((chn->mode & EXT_CH_MODE_FALLING_EDGE) &&                         \
           ((EXTD1.pc_current_values[index] & (1 << i)) == 0))) {            \
        chn->cb(&EXTD1, EXT_PCINT##index##_INDEX + i - EXT_PCINT_MIN_INDEX); \
      }                                                                      \
    }                                                                        \
  }                                                                          \
                                                                             \
  EXTD1.pc_old_values[index] = EXTD1.pc_current_values[index];               \
  OSAL_IRQ_EPILOGUE();                                                       \
}

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

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

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

#if EXT_PC_NUM_PORTS > 0
/**
 * @brief   Vector with addresses of Ports available.
 */
volatile uint8_t * const PINS[EXT_PC_NUM_PORTS] = {
#if AVR_EXT_USE_PCINT0
  (volatile uint8_t *const)&PCINT0_PIN,
#endif
#if AVR_EXT_USE_PCINT1
  (volatile uint8_t *const)&PCINT1_PIN,
#endif
#if AVR_EXT_USE_PCINT2
(volatile uint8_t *const)&PCINT2_PIN,
#endif
#if AVR_EXT_USE_PCINT3
(volatile uint8_t *const)&PCINT3_PIN,
#endif
#if AVR_EXT_USE_PCINT4
(volatile uint8_t *const)&PCINT4_PIN,
#endif
#if AVR_EXT_USE_PCINT5
(volatile uint8_t *const)&PCINT5_PIN,
#endif
#if AVR_EXT_USE_PCINT6
(volatile uint8_t *const)&PCINT6_PIN,
#endif
#if AVR_EXT_USE_PCINT7
(volatile uint8_t *const)&PCINT7_PIN,
#endif
#if AVR_EXT_USE_PCINT8
(volatile uint8_t *const)&PCINT8_PIN,
#endif
#if AVR_EXT_USE_PCINT9
(volatile uint8_t *const)&PCINT9_PIN,
#endif
#if AVR_EXT_USE_PCINT10
(volatile uint8_t *const)&PCINT10_PIN,
#endif
};

/**
 * @brief   Vector with addresses of Port Masks available.
 */
volatile uint8_t * const PCMSK[EXT_PC_NUM_PORTS] = {
#if AVR_EXT_USE_PCINT0
  (volatile uint8_t *const)&PCMSK0,
#endif
#if AVR_EXT_USE_PCINT1
  (volatile uint8_t *const)&PCMSK1,
#endif
#if AVR_EXT_USE_PCINT2
  (volatile uint8_t *const)&PCMSK2,
#endif
#if AVR_EXT_USE_PCINT3
  (volatile uint8_t *const)&PCMSK3,
#endif
#if AVR_EXT_USE_PCINT4
  (volatile uint8_t *const)&PCMSK4,
#endif
#if AVR_EXT_USE_PCINT5
  (volatile uint8_t *const)&PCMSK5,
#endif
#if AVR_EXT_USE_PCINT6
  (volatile uint8_t *const)&PCMSK6,
#endif
#if AVR_EXT_USE_PCINT7
  (volatile uint8_t *const)&PCMSK7,
#endif
#if AVR_EXT_USE_PCINT8
  (volatile uint8_t *const)&PCMSK8,
#endif
#if AVR_EXT_USE_PCINT9
  (volatile uint8_t *const)&PCMSK9,
#endif
#if AVR_EXT_USE_PCINT10
  (volatile uint8_t *const)&PCMSK10,
#endif
};
#endif

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

#if EXT_PC_NUM_PORTS > 0
/**
 * @brief   Configures and activates the Pin Change inputs.
 *
 * @param[in] extp      pointer to the @p EXTDriver object
 *
 * @notapi
 */
static void start_pc(EXTDriver *extp) {
  uint8_t icr = 0;
  uint8_t i;

  /* For every pin */
  for (i = 0; i < EXT_PC_NUM_CHANNELS; i++) {
    uint8_t mode = extp->config->channels[i + EXT_PC_MIN_CHANNEL].mode;

    /* Only start if autostart and not disabled */
    if ((mode & EXT_CH_MODE_AUTOSTART) && ((mode & EXT_CH_MODE_EDGES_MASK) != EXT_CH_MODE_DISABLED)) {
      (*(PCMSK[i/8])) |= _BV(i & 0x07);
    }
  }

  /* For every port */
  for (i = 0; i < EXT_PC_NUM_PORTS; i++) {
    /* Only enable interrupt if at least 1 bit in the mask is set */
    if ((*(PCMSK[i])) != 0) {
      /* Enable interrupt */
      icr |= (_BV(i));
    }
  }

  /* Enables/disables the peripheral, as requested. */
#if defined(__AVR_ATmega162__)
  GICR &= ~(0x03 << 3);
  GICR |= (icr << 3);
#else
  PCICR = icr;
#endif
}

/**
 * @brief   Deactivates the PC interrupts.
 * 
 * @param[in] extp      pointer to the @p EXTDriver object   
 */
static void stop_pc(EXTDriver *extp) {
  uint8_t i;
  (void)extp;

  /* Disable pin change interrupts */
#if defined(__AVR_ATmega162__)
  GICR &= ~(0x03 << 3);
#else
  PCICR = 0;
#endif

  /* Clear masks */
  for (i = 0; i < EXT_PC_NUM_PORTS; i++) {
    (*(PCMSK[i])) = 0;
  }
}
#endif

#if EXT_INT_NUM_CHANNELS > 0
/**
 * @brief   Configures and activates the INT inputs.
 *
 * @param[in] extp      pointer to the @p EXTDriver object
 *
 * @notapi
 */
static void start_ext(EXTDriver *extp) {
#if EXT_INT_NUM_CHANNELS < 4
  uint8_t icr = 0;
#else
  uint16_t icr = 0;
#endif
  uint8_t msk = 0;
  for (expchannel_t channel = EXT_INT_MIN_CHANNEL;
       channel <= EXT_INT_MAX_CHANNEL; channel++) {
    /* Determines the triggering condition for each channel. */
    switch(extp->config->channels[channel].mode &
           ~(EXT_CH_MODE_AUTOSTART | EXT_CH_MODE_INTERNAL_PULLUP)) {
    case EXT_CH_MODE_LOW_LEVEL:
      icr |= (EXT_EICRA_LOW_LEVEL << (2 * (channel - EXT_INT_MIN_CHANNEL)));
      break;
    case EXT_CH_MODE_BOTH_EDGES:
      icr |= (EXT_EICRA_BOTH_EDGES << (2 * (channel - EXT_INT_MIN_CHANNEL)));
      break;
    case EXT_CH_MODE_RISING_EDGE:
      icr |= (EXT_EICRA_RISING_EDGE << (2 * (channel - EXT_INT_MIN_CHANNEL)));
      break;
    case EXT_CH_MODE_FALLING_EDGE:
      icr |= (EXT_EICRA_FALLING_EDGE << (2 * (channel - EXT_INT_MIN_CHANNEL)));
      break;
    default: osalDbgAssert(FALSE, "unsupported mode");
    }

    /* Determines which channel must be started right away. */
    if (extp->config->channels[channel].mode & EXT_CH_MODE_AUTOSTART) {
      msk |= (1 << (channel - EXT_INT_MIN_CHANNEL));
    }
  }
  /* Configures the peripheral. */
#if defined(__AVR_ATmega162__)
  MCUCR |= (icr & 0x0f);

  icr >>=  4;
  osalDbgAssert(((icr & 0x02) == EXT_EICRA_RISING_EDGE) || ((icr & 0x02) == EXT_EICRA_FALLING_EDGE), "INT2 only supports rising or falling edge, not both.");
  EMCUCR |= icr & 0x01;
  
  GICR |= ((msk & 0x03) << 6);
  if (icr & 0x01) {
    /* Enable INT2 */
    GICR |= (1 << 5);
  }
#else
  EICRA = icr & 0xff;
#if EXT_INT_NUM_CHANNELS > 4
  EICRB = icr >> 8;
#endif
  /* Enables/disables the peripheral, as requested. */
  EIMSK = msk;
#endif
}

/**
 * @brief   Deactivates the INT interrupts.
 * 
 * @param[in] extp      pointer to the @p EXTDriver object
 */
static void stop_ext(EXTDriver *extp) {
  (void)extp;
#if defined(__AVR_ATmega162__)
  MCUCR &= ~(0x0f);
  EMCUCR &= ~(0x01);
  GICR |= ~(0x07 << 5);
#else
  EICRA = 0;
#if EXT_INT_NUM_CHANNELS > 4
  EICRB = 0;
#endif
  /* Enables/disables the peripheral, as requested. */
  EIMSK = 0;
#endif
}
#endif

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

/*
 * Interrupt handlers for PC-type interrupts.
 */
#define EXT_PCINT_MIN_INDEX    EXT_PC_MIN_PORT

#if 0 < EXT_PC_NUM_PORTS
#define EXT_PCINT0_INDEX     EXT_PCINT_MIN_INDEX
declare_pcint_isr(0);
#endif
#if 1 < EXT_PC_NUM_PORTS
#define EXT_PCINT1_INDEX    (EXT_PCINT0_INDEX  + 1)
declare_pcint_isr(1);
#endif
#if 2 < EXT_PC_NUM_PORTS
#define EXT_PCINT2_INDEX    (EXT_PCINT1_INDEX  + 1)
declare_pcint_isr(2);
#endif
#if 3 < EXT_PC_NUM_PORTS
#define EXT_PCINT3_INDEX    (EXT_PCINT2_INDEX  + 1)
declare_pcint_isr(3);
#endif
#if 4 < EXT_PC_NUM_PORTS
#define EXT_PCINT4_INDEX    (EXT_PCINT3_INDEX  + 1)
declare_pcint_isr(4);
#endif
#if 5 < EXT_PC_NUM_PORTS
#define EXT_PCINT5_INDEX    (EXT_PCINT4_INDEX  + 1)
declare_pcint_isr(5);
#endif
#if 6 < EXT_PC_NUM_PORTS
#define EXT_PCINT6_INDEX    (EXT_PCINT5_INDEX  + 1)
declare_pcint_isr(6);
#endif
#if 7 < EXT_PC_NUM_PORTS
#define EXT_PCINT7_INDEX    (EXT_PCINT6_INDEX  + 1)
declare_pcint_isr(7);
#endif
#if 8 < EXT_PC_NUM_PORTS
#define EXT_PCINT8_INDEX    (EXT_PCINT7_INDEX  + 1)
declare_pcint_isr(8);
#endif
#if 9 < EXT_PC_NUM_PORTS
#define EXT_PCINT9_INDEX    (EXT_PCINT8_INDEX  + 1)
declare_pcint_isr(9);
#endif

/*
 * Interrupt handlers for INT-type interrupts.
 */
#if 0 < EXT_INT_NUM_CHANNELS
declare_extint_isr(0);
#endif
#if 1 < EXT_INT_NUM_CHANNELS
declare_extint_isr(1);
#endif
#if 2 < EXT_INT_NUM_CHANNELS
declare_extint_isr(2);
#endif
#if 3 < EXT_INT_NUM_CHANNELS
declare_extint_isr(3);
#endif
#if 4 < EXT_INT_NUM_CHANNELS
declare_extint_isr(4);
#endif
#if 5 < EXT_INT_NUM_CHANNELS
declare_extint_isr(5);
#endif

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

/**
 * @brief   Low level EXT driver initialization.
 *
 * @notapi
 */
void ext_lld_init(void) {
  /* Driver initialization.*/
  extObjectInit(&EXTD1);
#if EXT_PC_NUM_PORTS > 0
  for (int i = 0; i < EXT_PC_NUM_PORTS; i++) {
    EXTD1.pc_old_values[i] = 0;
  }
#endif
}

/**
 * @brief   Configures and activates the EXT peripheral.
 *
 * @param[in] extp      pointer to the @p EXTDriver object
 *
 * @notapi
 */
void ext_lld_start(EXTDriver *extp) {
#if EXT_INT_NUM_CHANNELS > 0
  start_ext(extp);
#endif
#if EXT_PC_NUM_PORTS > 0
  start_pc(extp);
#endif
}

/**
 * @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) {
    /* Disables the peripheral.*/
#if EXT_INT_NUM_CHANNELS > 0
    stop_ext(extp);
#endif
#if EXT_PC_NUM_PORTS > 0
    stop_pc(extp);
#endif
  }
}

/**
 * @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) {
  (void)extp;
#if EXT_PC_NUM_CHANNELS > 0
  if (EXT_PC_MIN_CHANNEL <= channel && channel <= EXT_PC_MAX_CHANNEL) {
    uint8_t port = (channel - EXT_PC_MIN_CHANNEL) / 8;

    /* Enable bit in mask */
    (*(PCMSK[port])) |= _BV((channel - EXT_PC_MIN_CHANNEL) % 8);

    /* Always enable interrupt */
#if defined(__AVR_ATmega162__)
    GICR |= (_BV(port) << 3);
#else
    PCICR |= _BV(port);
#endif
  }
#endif
#if EXT_PC_NUM_CHANNELS > 0 && EXT_INT_NUM_CHANNELS > 0
  else
#endif
#if EXT_INT_NUM_CHANNELS > 0
  if (channel <= EXT_INT_MAX_CHANNEL) {
#if defined(__AVR_ATmega162__)
    GICR |= ((1 << channel) << 5);
#else
    /* Enables/disables the peripheral, as requested. */
    EIMSK |= (1 << channel);
#endif
  }
#endif
}

/**
 * @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) {
  (void)extp;
#if EXT_PC_NUM_CHANNELS > 0
  if (EXT_PC_MIN_CHANNEL <= channel && channel <= EXT_PC_MAX_CHANNEL) {
    uint8_t port = (channel - EXT_PC_MIN_CHANNEL) / 8;

    /* Clear bit in mask */
    (*(PCMSK[port])) &= ~(_BV((channel - EXT_PC_MIN_CHANNEL) % 8));

    /* Disable interrupt if no bits are set */
    if ((*(PCMSK[port])) == 0) {
#if defined(__AVR_ATmega162__)
      GICR &= ~(_BV(port) << 3);
#else
      PCICR |= ~(_BV(port));
#endif
    }
  }
#endif
#if EXT_PC_NUM_CHANNELS > 0 && EXT_INT_NUM_CHANNELS > 0
  else
#endif
#if EXT_INT_NUM_CHANNELS > 0
  if (channel <= EXT_INT_MAX_CHANNEL) {
#if defined(__AVR_ATmega162__)
    GICR &= ~((1 << channel) << 5);
#else
    /* Enables/disables the peripheral, as requested. */
    EIMSK &= ~(1 << channel);
#endif
  }
#endif
}

#endif /* HAL_USE_EXT */

/** @} */