1 /*
2   Copyright (c) 2015 Arduino LLC.  All right reserved.
3 
4   This library is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Lesser General Public
6   License as published by the Free Software Foundation; either
7   version 2.1 of the License, or (at your option) any later version.
8 
9   This library is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12   See the GNU Lesser General Public License for more details.
13 
14   You should have received a copy of the GNU Lesser General Public
15   License along with this library; if not, write to the Free Software
16   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18 
19 #pragma once
20 
21 #include <stdint.h>
22 #include <samd.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 /* Definitions and types for pins */
29 typedef enum _EAnalogChannel
30 {
31   No_ADC_Channel=-1,
32   ADC_Channel0=0,
33   ADC_Channel1=1,
34   ADC_Channel2=2,
35   ADC_Channel3=3,
36   ADC_Channel4=4,
37   ADC_Channel5=5,
38   ADC_Channel6=6,
39   ADC_Channel7=7,
40 #if defined __SAMD21J18A__
41   ADC_Channel8=8,
42   ADC_Channel9=9,
43 #endif // __SAMD21J18A__
44   ADC_Channel10=10,
45   ADC_Channel11=11,
46 #if defined __SAMD21J18A__
47   ADC_Channel12=12,
48   ADC_Channel13=13,
49   ADC_Channel14=14,
50   ADC_Channel15=15,
51 #endif // __SAMD21J18A__
52   ADC_Channel16=16,
53   ADC_Channel17=17,
54   ADC_Channel18=18,
55   ADC_Channel19=19,
56   DAC_Channel0,
57 } EAnalogChannel ;
58 
59 // Definitions for TC channels
60 typedef enum _ETCChannel
61 {
62   NOT_ON_TIMER=-1,
63   TCC0_CH0 = (0<<8)|(0),
64   TCC0_CH1 = (0<<8)|(1),
65   TCC0_CH2 = (0<<8)|(2),
66   TCC0_CH3 = (0<<8)|(3),
67   TCC0_CH4 = (0<<8)|(0), // Channel 4 is 0!
68   TCC0_CH5 = (0<<8)|(1), // Channel 5 is 1!
69   TCC0_CH6 = (0<<8)|(2), // Channel 6 is 2!
70   TCC0_CH7 = (0<<8)|(3), // Channel 7 is 3!
71   TCC1_CH0 = (1<<8)|(0),
72   TCC1_CH1 = (1<<8)|(1),
73   TCC1_CH2 = (1<<8)|(0), // Channel 2 is 0!
74   TCC1_CH3 = (1<<8)|(1), // Channel 3 is 1!
75   TCC2_CH0 = (2<<8)|(0),
76   TCC2_CH1 = (2<<8)|(1),
77   TCC2_CH2 = (2<<8)|(0), // Channel 2 is 0!
78   TCC2_CH3 = (2<<8)|(1), // Channel 3 is 1!
79   TC3_CH0  = (3<<8)|(0),
80   TC3_CH1  = (3<<8)|(1),
81   TC4_CH0  = (4<<8)|(0),
82   TC4_CH1  = (4<<8)|(1),
83   TC5_CH0  = (5<<8)|(0),
84   TC5_CH1  = (5<<8)|(1),
85 #if defined __SAMD21J18A__
86   TC6_CH0  = (6<<8)|(0),
87   TC6_CH1  = (6<<8)|(1),
88   TC7_CH0  = (7<<8)|(0),
89   TC7_CH1  = (7<<8)|(1),
90 #endif // __SAMD21J18A__
91 } ETCChannel ;
92 
93 extern const void* g_apTCInstances[TCC_INST_NUM+TC_INST_NUM] ;
94 
95 #define GetTCNumber( x ) ( (x) >> 8 )
96 #define GetTCChannelNumber( x ) ( (x) & 0xff )
97 #define GetTC( x ) ( g_apTCInstances[(x) >> 8] )
98 
99 // Definitions for PWM channels
100 typedef enum _EPWMChannel
101 {
102   NOT_ON_PWM=-1,
103   PWM0_CH0=TCC0_CH0,
104   PWM0_CH1=TCC0_CH1,
105   PWM0_CH2=TCC0_CH2,
106   PWM0_CH3=TCC0_CH3,
107   PWM0_CH4=TCC0_CH4,
108   PWM0_CH5=TCC0_CH5,
109   PWM0_CH6=TCC0_CH6,
110   PWM0_CH7=TCC0_CH7,
111   PWM1_CH0=TCC1_CH0,
112   PWM1_CH1=TCC1_CH1,
113   PWM1_CH2=TCC1_CH2,
114   PWM1_CH3=TCC1_CH3,
115   PWM2_CH0=TCC2_CH0,
116   PWM2_CH1=TCC2_CH1,
117   PWM2_CH2=TCC2_CH2,
118   PWM2_CH3=TCC2_CH3,
119   PWM3_CH0=TC3_CH0,
120   PWM3_CH1=TC3_CH1,
121   PWM4_CH0=TC4_CH0,
122   PWM4_CH1=TC4_CH1,
123   PWM5_CH0=TC5_CH0,
124   PWM5_CH1=TC5_CH1,
125 #if defined __SAMD21J18A__
126   PWM6_CH0=TC6_CH0,
127   PWM6_CH1=TC6_CH1,
128   PWM7_CH0=TC7_CH0,
129   PWM7_CH1=TC7_CH1,
130 #endif // __SAMD21J18A__
131 } EPWMChannel ;
132 
133 typedef enum _EPortType
134 {
135   NOT_A_PORT=-1,
136   PORTA=0,
137   PORTB=1,
138   PORTC=2,
139 } EPortType ;
140 
141 typedef enum
142 {
143   EXTERNAL_INT_0 = 0,
144   EXTERNAL_INT_1,
145   EXTERNAL_INT_2,
146   EXTERNAL_INT_3,
147   EXTERNAL_INT_4,
148   EXTERNAL_INT_5,
149   EXTERNAL_INT_6,
150   EXTERNAL_INT_7,
151   EXTERNAL_INT_8,
152   EXTERNAL_INT_9,
153   EXTERNAL_INT_10,
154   EXTERNAL_INT_11,
155   EXTERNAL_INT_12,
156   EXTERNAL_INT_13,
157   EXTERNAL_INT_14,
158   EXTERNAL_INT_15,
159   EXTERNAL_INT_NMI,
160   EXTERNAL_NUM_INTERRUPTS,
161   NOT_AN_INTERRUPT = -1,
162   EXTERNAL_INT_NONE = NOT_AN_INTERRUPT,
163 } EExt_Interrupts ;
164 
165 //A    B                 C       D          E      F   G   H
166 //EIC REF ADC AC PTC DAC SERCOM SERCOM_ALT TC/TCC TCC COM AC/GCLK
167 
168 typedef enum _EPioType
169 {
170   PIO_NOT_A_PIN=-1,     /* Not under control of a peripheral. */
171   PIO_EXTINT=0,         /* The pin is controlled by the associated signal of peripheral A. */
172   PIO_ANALOG,           /* The pin is controlled by the associated signal of peripheral B. */
173   PIO_SERCOM,           /* The pin is controlled by the associated signal of peripheral C. */
174   PIO_SERCOM_ALT,       /* The pin is controlled by the associated signal of peripheral D. */
175   PIO_TIMER,            /* The pin is controlled by the associated signal of peripheral E. */
176   PIO_TIMER_ALT,        /* The pin is controlled by the associated signal of peripheral F. */
177   PIO_COM,              /* The pin is controlled by the associated signal of peripheral G. */
178   PIO_AC_CLK,           /* The pin is controlled by the associated signal of peripheral H. */
179   PIO_DIGITAL,          /* The pin is controlled by PORT. */
180   PIO_INPUT,            /* The pin is controlled by PORT and is an input. */
181   PIO_INPUT_PULLUP,     /* The pin is controlled by PORT and is an input with internal pull-up resistor enabled. */
182   PIO_OUTPUT,           /* The pin is controlled by PORT and is an output. */
183 
184   PIO_PWM=PIO_TIMER,
185   PIO_PWM_ALT=PIO_TIMER_ALT,
186 } EPioType ;
187 
188 /**
189  * Pin Attributes to be OR-ed
190  */
191 #define PIN_ATTR_NONE          (0UL<<0)
192 #define PIN_ATTR_COMBO         (1UL<<0)
193 #define PIN_ATTR_ANALOG        (1UL<<1)
194 #define PIN_ATTR_DIGITAL       (1UL<<2)
195 #define PIN_ATTR_PWM           (1UL<<3)
196 #define PIN_ATTR_TIMER         (1UL<<4)
197 #define PIN_ATTR_TIMER_ALT     (1UL<<5)
198 #define PIN_ATTR_EXTINT        (1UL<<6)
199 
200 /* Types used for the table below */
201 typedef struct _PinDescription
202 {
203   EPortType       ulPort ;
204   uint32_t        ulPin ;
205   EPioType        ulPinType ;
206   uint32_t        ulPinAttribute ;
207   EAnalogChannel  ulADCChannelNumber ; /* ADC Channel number in the SAM device */
208   EPWMChannel     ulPWMChannel ;
209   ETCChannel      ulTCChannel ;
210   EExt_Interrupts ulExtInt ;
211 } PinDescription ;
212 
213 /* Pins table to be instantiated into variant.cpp */
214 extern const PinDescription g_APinDescription[] ;
215 
216 /* Generic Clock Multiplexer IDs */
217 #define GCM_DFLL48M_REF           (0x00U)
218 #define GCM_FDPLL96M_INPUT        (0x01U)
219 #define GCM_FDPLL96M_32K          (0x02U)
220 #define GCM_WDT                   (0x03U)
221 #define GCM_RTC                   (0x04U)
222 #define GCM_EIC                   (0x05U)
223 #define GCM_USB                   (0x06U)
224 #define GCM_EVSYS_CHANNEL_0       (0x07U)
225 #define GCM_EVSYS_CHANNEL_1       (0x08U)
226 #define GCM_EVSYS_CHANNEL_2       (0x09U)
227 #define GCM_EVSYS_CHANNEL_3       (0x0AU)
228 #define GCM_EVSYS_CHANNEL_4       (0x0BU)
229 #define GCM_EVSYS_CHANNEL_5       (0x0CU)
230 #define GCM_EVSYS_CHANNEL_6       (0x0DU)
231 #define GCM_EVSYS_CHANNEL_7       (0x0EU)
232 #define GCM_EVSYS_CHANNEL_8       (0x0FU)
233 #define GCM_EVSYS_CHANNEL_9       (0x10U)
234 #define GCM_EVSYS_CHANNEL_10      (0x11U)
235 #define GCM_EVSYS_CHANNEL_11      (0x12U)
236 #define GCM_SERCOMx_SLOW          (0x13U)
237 #define GCM_SERCOM0_CORE          (0x14U)
238 #define GCM_SERCOM1_CORE          (0x15U)
239 #define GCM_SERCOM2_CORE          (0x16U)
240 #define GCM_SERCOM3_CORE          (0x17U)
241 #define GCM_SERCOM4_CORE          (0x18U)
242 #define GCM_SERCOM5_CORE          (0x19U)
243 #define GCM_TCC0_TCC1             (0x1AU)
244 #define GCM_TCC2_TC3              (0x1BU)
245 #define GCM_TC4_TC5               (0x1CU)
246 #define GCM_TC6_TC7               (0x1DU)
247 #define GCM_ADC                   (0x1EU)
248 #define GCM_AC_DIG                (0x1FU)
249 #define GCM_AC_ANA                (0x20U)
250 #define GCM_DAC                   (0x21U)
251 #define GCM_PTC                   (0x22U)
252 #define GCM_I2S_0                 (0x23U)
253 #define GCM_I2S_1                 (0x24U)
254 
255 #ifdef __cplusplus
256 } // extern "C"
257 #endif
258