misc: Update NO_COLOR define switch
[simavr] / simavr / sim / avr_extint.h
1 /*
2         avr_extint.h
3
4         External Interrupt Handling (for INT0-3)
5
6         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
7
8         This file is part of simavr.
9
10         simavr is free software: you can redistribute it and/or modify
11         it under the terms of the GNU General Public License as published by
12         the Free Software Foundation, either version 3 of the License, or
13         (at your option) any later version.
14
15         simavr is distributed in the hope that it will be useful,
16         but WITHOUT ANY WARRANTY; without even the implied warranty of
17         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18         GNU General Public License for more details.
19
20         You should have received a copy of the GNU General Public License
21         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24 #ifndef __AVR_EXTINT_H__
25 #define __AVR_EXTINT_H__
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 #include "sim_avr.h"
32
33
34 enum {
35         EXTINT_IRQ_OUT_INT0 = 0,
36         EXTINT_IRQ_OUT_INT1, EXTINT_IRQ_OUT_INT2, EXTINT_IRQ_OUT_INT3,
37         EXTINT_IRQ_OUT_INT4, EXTINT_IRQ_OUT_INT5, EXTINT_IRQ_OUT_INT6,
38         EXTINT_IRQ_OUT_INT7,
39         EXTINT_COUNT
40 };
41
42 // Get the internal IRQ corresponding to the INT
43 #define AVR_IOCTL_EXTINT_GETIRQ() AVR_IOCTL_DEF('i','n','t',' ')
44
45 /*
46  * This module is just a "relay" for the pin change IRQ in the IO port
47  * module. We hook up to their IRQ and raise out interrupt vectors as needed
48  *
49  * "isc" is handled, apart from the "level" mode that doesn't make sense here (?)
50  */
51 typedef struct avr_extint_t {
52         avr_io_t        io;
53
54         struct {
55                 avr_regbit_t    isc[2];         // interrupt sense control bits
56                 avr_int_vector_t vector;        // interrupt vector
57
58                 uint32_t                port_ioctl;             // ioctl to use to get port
59                 uint8_t                 port_pin;               // pin number in said port
60         }       eint[EXTINT_COUNT];
61
62 } avr_extint_t;
63
64 void avr_extint_init(avr_t * avr, avr_extint_t * p);
65
66 // Declares a typical INT into a avr_extint_t in a core.
67 // this is a shortcut since INT declarations are pretty standard.
68 // The Tinies as well as the atmega1280 are slightly different.
69 // See sim_tinyx5.h and sim_mega1280.h
70 #define AVR_EXTINT_DECLARE(_index, _portname, _portpin) \
71                 .eint[_index] = { \
72                         .port_ioctl = AVR_IOCTL_IOPORT_GETIRQ(_portname), \
73                         .port_pin = _portpin, \
74                         .isc = { AVR_IO_REGBIT(EICRA, ISC##_index##0), AVR_IO_REGBIT(EICRA, ISC##_index##1) },\
75                         .vector = { \
76                                 .enable = AVR_IO_REGBIT(EIMSK, INT##_index), \
77                                 .raised = AVR_IO_REGBIT(EIFR, INTF##_index), \
78                                 .vector = INT##_index##_vect, \
79                         },\
80                 }
81
82 #define AVR_EXTINT_MEGA_DECLARE(_index, _portname, _portpin, _EICR) \
83                 .eint[_index] = { \
84                         .port_ioctl = AVR_IOCTL_IOPORT_GETIRQ(_portname), \
85                         .port_pin = _portpin, \
86                         .isc = { AVR_IO_REGBIT(EICR##_EICR, ISC##_index##0), AVR_IO_REGBIT(EICR##_EICR, ISC##_index##1) },\
87                         .vector = { \
88                                 .enable = AVR_IO_REGBIT(EIMSK, INT##_index), \
89                                 .raised = AVR_IO_REGBIT(EIFR, INTF##_index), \
90                                 .vector = INT##_index##_vect, \
91                         },\
92                 }
93
94 #define AVR_EXTINT_TINY_DECLARE(_index, _portname, _portpin, _IFR) \
95                 .eint[_index] = { \
96                         .port_ioctl = AVR_IOCTL_IOPORT_GETIRQ(_portname), \
97                         .port_pin = _portpin, \
98                         .isc = { AVR_IO_REGBIT(MCUCR, ISC##_index##0), AVR_IO_REGBIT(MCUCR, ISC##_index##1) }, \
99                         .vector = { \
100                                 .enable = AVR_IO_REGBIT(GIMSK, INT##_index), \
101                                 .raised = AVR_IO_REGBIT(_IFR, INTF##_index), \
102                                 .vector = INT##_index##_vect, \
103                         }, \
104                 }
105
106 #ifdef __cplusplus
107 };
108 #endif
109
110 #endif /*__AVR_EXTINT_H__*/