2 Boards.h - Hardware Abstraction Layer for Firmata library
3 Copyright (c) 2006-2008 Hans-Christoph Steiner. All rights reserved.
4 Copyright (C) 2009-2017 Jeff Hoefs. All rights reserved.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 See file LICENSE.txt for further informations on licensing terms.
13 Last updated April 15th, 2018
16 #ifndef Firmata_Boards_h
17 #define Firmata_Boards_h
21 #if defined(ARDUINO) && ARDUINO >= 100
22 #include "Arduino.h" // for digitalRead, digitalWrite, etc
27 // Normally Servo.h must be included before Firmata.h (which then includes
28 // this file). If Servo.h wasn't included, this allows the code to still
29 // compile, but without support for any Servos. Hopefully that's what the
30 // user intended by not including Servo.h
36 Firmata Hardware Abstraction Layer
38 Firmata is built on top of the hardware abstraction functions of Arduino,
39 specifically digitalWrite, digitalRead, analogWrite, analogRead, and
40 pinMode. While these functions offer simple integer pin numbers, Firmata
41 needs more information than is provided by Arduino. This file provides
42 all other hardware specific details. To make Firmata support a new board,
43 only this file should require editing.
45 The key concept is every "pin" implemented by Firmata may be mapped to
46 any pin as implemented by Arduino. Usually a simple 1-to-1 mapping is
47 best, but such mapping should not be assumed. This hardware abstraction
48 layer allows Firmata to implement any number of pins which map onto the
49 Arduino implemented pins in almost any arbitrary way.
54 These constants provide basic information Firmata requires.
56 TOTAL_PINS: The total number of pins Firmata implemented by Firmata.
57 Usually this will match the number of pins the Arduino functions
58 implement, including any pins pins capable of analog or digital.
59 However, Firmata may implement any number of pins. For example,
60 on Arduino Mini with 8 analog inputs, 6 of these may be used
61 for digital functions, and 2 are analog only. On such boards,
62 Firmata can implement more pins than Arduino's pinMode()
63 function, in order to accommodate those special pins. The
64 Firmata protocol supports a maximum of 128 pins, so this
65 constant must not exceed 128.
67 TOTAL_ANALOG_PINS: The total number of analog input pins implemented.
68 The Firmata protocol allows up to 16 analog inputs, accessed
69 using offsets 0 to 15. Because Firmata presents the analog
70 inputs using different offsets than the actual pin numbers
71 (a legacy of Arduino's analogRead function, and the way the
72 analog input capable pins are physically labeled on all
73 Arduino boards), the total number of analog input signals
74 must be specified. 16 is the maximum.
76 VERSION_BLINK_PIN: When Firmata starts up, it will blink the version
77 number. This constant is the Arduino pin number where a
83 These macros provide the mapping between pins as implemented by
84 Firmata protocol and the actual pin numbers used by the Arduino
85 functions. Even though such mappings are often simple, pin
86 numbers received by Firmata protocol should always be used as
87 input to these macros, and the result of the macro should be
88 used with with any Arduino function.
90 When Firmata is extended to support a new pin mode or feature,
91 a pair of macros should be added and used for all hardware
92 access. For simple 1:1 mapping, these macros add no actual
93 overhead, yet their consistent use allows source code which
94 uses them consistently to be easily adapted to all other boards
95 with different requirements.
97 IS_PIN_XXXX(pin): The IS_PIN macros resolve to true or non-zero
98 if a pin as implemented by Firmata corresponds to a pin
99 that actually implements the named feature.
101 PIN_TO_XXXX(pin): The PIN_TO macros translate pin numbers as
102 implemented by Firmata to the pin numbers needed as inputs
103 to the Arduino functions. The corresponding IS_PIN macro
104 should always be tested before using a PIN_TO macro, so
105 these macros only need to handle valid Firmata pin
106 numbers for the named feature.
109 Port Access Inline Funtions:
111 For efficiency, Firmata protocol provides access to digital
112 input and output pins grouped by 8 bit ports. When these
113 groups of 8 correspond to actual 8 bit ports as implemented
114 by the hardware, these inline functions can provide high
115 speed direct port access. Otherwise, a default implementation
116 using 8 calls to digitalWrite or digitalRead is used.
118 When porting Firmata to a new board, it is recommended to
119 use the default functions first and focus only on the constants
120 and macros above. When those are working, if optimized port
121 access is desired, these inline functions may be extended.
122 The recommended approach defines a symbol indicating which
123 optimization to use, and then conditional complication is
124 used within these functions.
126 readPort(port, bitmask): Read an 8 bit port, returning the value.
127 port: The port number, Firmata pins port*8 to port*8+7
128 bitmask: The actual pins to read, indicated by 1 bits.
130 writePort(port, value, bitmask): Write an 8 bit port.
131 port: The port number, Firmata pins port*8 to port*8+7
132 value: The 8 bit value to write
133 bitmask: The actual pins to write, indicated by 1 bits.
136 /*==============================================================================
137 * Board Specific Configuration
138 *============================================================================*/
140 #ifndef digitalPinHasPWM
141 #define digitalPinHasPWM(p) IS_PIN_DIGITAL(p)
144 // Arduino Duemilanove, Diecimila, and NG
145 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__)
146 #if defined(NUM_ANALOG_INPUTS) && NUM_ANALOG_INPUTS == 6
147 #define TOTAL_ANALOG_PINS 6
148 #define TOTAL_PINS 20 // 14 digital + 6 analog
150 #define TOTAL_ANALOG_PINS 8
151 #define TOTAL_PINS 22 // 14 digital + 8 analog
153 #define VERSION_BLINK_PIN 13
154 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 19)
155 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) < 14 + TOTAL_ANALOG_PINS)
156 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
157 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) - 2 < MAX_SERVOS)
158 #define IS_PIN_I2C(p) ((p) == 18 || (p) == 19)
159 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
160 #define PIN_TO_DIGITAL(p) (p)
161 #define PIN_TO_ANALOG(p) ((p) - 14)
162 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
163 #define PIN_TO_SERVO(p) ((p) - 2)
164 #define ARDUINO_PINOUT_OPTIMIZE 1
167 // Wiring (and board)
168 #elif defined(WIRING)
169 #define VERSION_BLINK_PIN WLED
170 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS)
171 #define IS_PIN_ANALOG(p) ((p) >= FIRST_ANALOG_PIN && (p) < (FIRST_ANALOG_PIN+TOTAL_ANALOG_PINS))
172 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
173 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
174 #define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL)
175 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
176 #define PIN_TO_DIGITAL(p) (p)
177 #define PIN_TO_ANALOG(p) ((p) - FIRST_ANALOG_PIN)
178 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
179 #define PIN_TO_SERVO(p) (p)
183 #elif defined(__AVR_ATmega8__)
184 #define TOTAL_ANALOG_PINS 6
185 #define TOTAL_PINS 20 // 14 digital + 6 analog
186 #define VERSION_BLINK_PIN 13
187 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 19)
188 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 19)
189 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
190 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) - 2 < MAX_SERVOS)
191 #define IS_PIN_I2C(p) ((p) == 18 || (p) == 19)
192 #define PIN_TO_DIGITAL(p) (p)
193 #define PIN_TO_ANALOG(p) ((p) - 14)
194 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
195 #define PIN_TO_SERVO(p) ((p) - 2)
196 #define ARDUINO_PINOUT_OPTIMIZE 1
200 #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
201 #define TOTAL_ANALOG_PINS 16
202 #define TOTAL_PINS 70 // 54 digital + 16 analog
203 #define VERSION_BLINK_PIN 13
204 #define PIN_SERIAL1_RX 19
205 #define PIN_SERIAL1_TX 18
206 #define PIN_SERIAL2_RX 17
207 #define PIN_SERIAL2_TX 16
208 #define PIN_SERIAL3_RX 15
209 #define PIN_SERIAL3_TX 14
210 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS)
211 #define IS_PIN_ANALOG(p) ((p) >= 54 && (p) < TOTAL_PINS)
212 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
213 #define IS_PIN_SERVO(p) ((p) >= 2 && (p) - 2 < MAX_SERVOS)
214 #define IS_PIN_I2C(p) ((p) == 20 || (p) == 21)
215 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
216 #define IS_PIN_SERIAL(p) ((p) > 13 && (p) < 20)
217 #define PIN_TO_DIGITAL(p) (p)
218 #define PIN_TO_ANALOG(p) ((p) - 54)
219 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
220 #define PIN_TO_SERVO(p) ((p) - 2)
224 #elif defined(__SAM3X8E__)
225 #define TOTAL_ANALOG_PINS 12
226 #define TOTAL_PINS 66 // 54 digital + 12 analog
227 #define VERSION_BLINK_PIN 13
228 #define PIN_SERIAL1_RX 19
229 #define PIN_SERIAL1_TX 18
230 #define PIN_SERIAL2_RX 17
231 #define PIN_SERIAL2_TX 16
232 #define PIN_SERIAL3_RX 15
233 #define PIN_SERIAL3_TX 14
234 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS)
235 #define IS_PIN_ANALOG(p) ((p) >= 54 && (p) < TOTAL_PINS)
236 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
237 #define IS_PIN_SERVO(p) ((p) >= 2 && (p) - 2 < MAX_SERVOS)
238 #define IS_PIN_I2C(p) ((p) == 20 || (p) == 21) // 70 71
239 #define IS_PIN_SERIAL(p) ((p) > 13 && (p) < 20)
240 #define PIN_TO_DIGITAL(p) (p)
241 #define PIN_TO_ANALOG(p) ((p) - 54)
242 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
243 #define PIN_TO_SERVO(p) ((p) - 2)
246 // Arduino/Genuino MKR1000
247 #elif defined(ARDUINO_SAMD_MKR1000)
248 #define TOTAL_ANALOG_PINS 7
249 #define TOTAL_PINS 22 // 8 digital + 3 spi + 2 i2c + 2 uart + 7 analog
250 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 21)
251 #define IS_PIN_ANALOG(p) ((p) >= 15 && (p) < 15 + TOTAL_ANALOG_PINS)
252 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
253 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4
254 #define IS_PIN_I2C(p) ((p) == 11 || (p) == 12) // SDA = 11, SCL = 12
255 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
256 #define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL1_RX || (p) == PIN_SERIAL1_TX) //defined in variant.h RX = 13, TX = 14
257 #define PIN_TO_DIGITAL(p) (p)
258 #define PIN_TO_ANALOG(p) ((p) - 15)
259 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
260 #define PIN_TO_SERVO(p) (p) // deprecated since v2.4
264 #elif defined(ARDUINO_SAMD_MKRZERO)
265 #define TOTAL_ANALOG_PINS 7
266 #define TOTAL_PINS 34 // 8 digital + 3 spi + 2 i2c + 2 uart + 7 analog + 3 usb + 1 aref + 5 sd + 1 bottom pad + 1 led + 1 battery adc
267 #define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 21) || (p) == 32)
268 #define IS_PIN_ANALOG(p) (((p) >= 15 && (p) < 15 + TOTAL_ANALOG_PINS) || (p) == 33)
269 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
270 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4
271 #define IS_PIN_I2C(p) ((p) == 11 || (p) == 12) // SDA = 11, SCL = 12
272 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
273 #define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL1_RX || (p) == PIN_SERIAL1_TX) //defined in variant.h RX = 13, TX = 14
274 #define PIN_TO_DIGITAL(p) (p)
275 #define PIN_TO_ANALOG(p) ((p) - 15)
276 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
277 #define PIN_TO_SERVO(p) (p) // deprecated since v2.4
279 // Arduino MKRFox1200
280 #elif defined(ARDUINO_SAMD_MKRFox1200)
281 #define TOTAL_ANALOG_PINS 7
282 #define TOTAL_PINS 33 // 8 digital + 3 spi + 2 i2c + 2 uart + 7 analog + 3 usb + 1 aref + 5 sd + 1 bottom pad + 1 battery adc
283 #define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 21))
284 #define IS_PIN_ANALOG(p) (((p) >= 15 && (p) < 15 + TOTAL_ANALOG_PINS) || (p) == 32)
285 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
286 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4
287 #define IS_PIN_I2C(p) ((p) == 11 || (p) == 12) // SDA = 11, SCL = 12
288 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
289 #define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL1_RX || (p) == PIN_SERIAL1_TX) //defined in variant.h RX = 13, TX = 14
290 #define PIN_TO_DIGITAL(p) (p)
291 #define PIN_TO_ANALOG(p) ((p) - 15)
292 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
293 #define PIN_TO_SERVO(p) (p) // deprecated since v2.4
295 // Arduino MKR WAN 1300
296 #elif defined(ARDUINO_SAMD_MKRWAN1300)
297 #define TOTAL_ANALOG_PINS 7
298 #define TOTAL_PINS 33
299 #define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 21))
300 #define IS_PIN_ANALOG(p) (((p) >= 15 && (p) < 15 + TOTAL_ANALOG_PINS) || (p) == 32)
301 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
302 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4
303 #define IS_PIN_I2C(p) ((p) == 11 || (p) == 12) // SDA = 11, SCL = 12
304 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
305 #define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL1_RX || (p) == PIN_SERIAL1_TX) //defined in variant.h RX = 13, TX = 14
306 #define PIN_TO_DIGITAL(p) (p)
307 #define PIN_TO_ANALOG(p) ((p) - 15)
308 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
309 #define PIN_TO_SERVO(p) (p) // deprecated since v2.4
311 // Arduino MKR GSM 1400
312 #elif defined(ARDUINO_SAMD_MKRGSM1400)
313 #define TOTAL_ANALOG_PINS 7
314 #define TOTAL_PINS 33
315 #define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 21))
316 #define IS_PIN_ANALOG(p) (((p) >= 15 && (p) < 15 + TOTAL_ANALOG_PINS) || (p) == 32)
317 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
318 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4
319 #define IS_PIN_I2C(p) ((p) == 11 || (p) == 12) // SDA = 11, SCL = 12
320 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
321 #define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL1_RX || (p) == PIN_SERIAL1_TX) //defined in variant.h RX = 13, TX = 14
322 #define PIN_TO_DIGITAL(p) (p)
323 #define PIN_TO_ANALOG(p) ((p) - 15)
324 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
325 #define PIN_TO_SERVO(p) (p) // deprecated since v2.4
328 // Note this will work with an Arduino Zero Pro, but not with an Arduino M0 Pro
329 // Arduino M0 Pro does not properly map pins to the board labeled pin numbers
330 #elif defined(_VARIANT_ARDUINO_ZERO_)
331 #define TOTAL_ANALOG_PINS 6
332 #define TOTAL_PINS 25 // 14 digital + 6 analog + 2 i2c + 3 spi
333 #define TOTAL_PORTS 3 // set when TOTAL_PINS > num digitial I/O pins
334 #define VERSION_BLINK_PIN LED_BUILTIN
335 //#define PIN_SERIAL1_RX 0 // already defined in zero core variant.h
336 //#define PIN_SERIAL1_TX 1 // already defined in zero core variant.h
337 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 19)
338 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) < 14 + TOTAL_ANALOG_PINS)
339 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
340 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4
341 #define IS_PIN_I2C(p) ((p) == 20 || (p) == 21) // SDA = 20, SCL = 21
342 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) // SS = A2
343 #define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1)
344 #define PIN_TO_DIGITAL(p) (p)
345 #define PIN_TO_ANALOG(p) ((p) - 14)
346 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
347 #define PIN_TO_SERVO(p) (p) // deprecated since v2.4
350 #elif defined(ARDUINO_PRIMO)
351 #define TOTAL_ANALOG_PINS 6
352 #define TOTAL_PINS 22 //14 digital + 6 analog + 2 i2c
353 #define VERSION_BLINK_PIN LED_BUILTIN
354 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < 20)
355 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) < 20)
356 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
357 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS+2)
358 #define IS_PIN_I2C(p) ((p) == PIN_WIRE_SDA || (p) == PIN_WIRE_SCL) // SDA = 20, SCL = 21
359 #define IS_PIN_SPI(p) ((p) == SS || (p)== MOSI || (p) == MISO || (p == SCK)) // 10, 11, 12, 13
360 #define PIN_TO_DIGITAL(p) (p)
361 #define PIN_TO_ANALOG(p) ((p) - 14)
362 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
363 #define PIN_TO_SERVO(p) (p)
366 #elif defined(_VARIANT_ARDUINO_101_X_)
367 #define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS
368 #define TOTAL_PINS NUM_DIGITAL_PINS // 15 digital (including ATN pin) + 6 analog
369 #define VERSION_BLINK_PIN LED_BUILTIN
370 #define PIN_SERIAL1_RX 0
371 #define PIN_SERIAL1_TX 1
372 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 20)
373 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) < 14 + TOTAL_ANALOG_PINS)
374 #define IS_PIN_PWM(p) digitalPinHasPWM(p) // 3, 5, 6, 9
375 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4
376 #define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL) // SDA = 18, SCL = 19
377 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
378 #define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1)
379 #define PIN_TO_DIGITAL(p) (p)
380 #define PIN_TO_ANALOG(p) ((p) - 14)
381 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
382 #define PIN_TO_SERVO(p) (p) // deprecated since v2.4
386 #elif defined(__AVR_AT90USB162__)
387 #define TOTAL_ANALOG_PINS 0
388 #define TOTAL_PINS 21 // 21 digital + no analog
389 #define VERSION_BLINK_PIN 6
390 #define PIN_SERIAL1_RX 2
391 #define PIN_SERIAL1_TX 3
392 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS)
393 #define IS_PIN_ANALOG(p) (0)
394 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
395 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
396 #define IS_PIN_I2C(p) (0)
397 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
398 #define IS_PIN_SERIAL(p) ((p) == 2 || (p) == 3)
399 #define PIN_TO_DIGITAL(p) (p)
400 #define PIN_TO_ANALOG(p) (0)
401 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
402 #define PIN_TO_SERVO(p) (p)
406 #elif defined(__AVR_ATmega32U4__) && defined(CORE_TEENSY)
407 #define TOTAL_ANALOG_PINS 12
408 #define TOTAL_PINS 25 // 11 digital + 12 analog
409 #define VERSION_BLINK_PIN 11
410 #define PIN_SERIAL1_RX 7
411 #define PIN_SERIAL1_TX 8
412 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS)
413 #define IS_PIN_ANALOG(p) ((p) >= 11 && (p) <= 22)
414 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
415 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
416 #define IS_PIN_I2C(p) ((p) == 5 || (p) == 6)
417 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
418 #define IS_PIN_SERIAL(p) ((p) == 7 || (p) == 8)
419 #define PIN_TO_DIGITAL(p) (p)
420 #define PIN_TO_ANALOG(p) (((p) < 22) ? 21 - (p) : 11)
421 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
422 #define PIN_TO_SERVO(p) (p)
425 // Teensy 3.5 and 3.6
426 // reference: https://github.com/PaulStoffregen/cores/blob/master/teensy3/pins_arduino.h
427 #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
428 #define TOTAL_ANALOG_PINS 27 // 3.5 has 27 and 3.6 has 25
429 #define TOTAL_PINS 70 // 43 digital + 21 analog-digital + 6 analog (64-69)
430 #define VERSION_BLINK_PIN 13
431 #define PIN_SERIAL1_RX 0
432 #define PIN_SERIAL1_TX 1
433 #define PIN_SERIAL2_RX 9
434 #define PIN_SERIAL2_TX 10
435 #define PIN_SERIAL3_RX 7
436 #define PIN_SERIAL3_TX 8
437 #define PIN_SERIAL4_RX 31
438 #define PIN_SERIAL4_TX 32
439 #define PIN_SERIAL5_RX 34
440 #define PIN_SERIAL5_TX 33
441 #define PIN_SERIAL6_RX 47
442 #define PIN_SERIAL6_TX 48
443 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 63)
444 #define IS_PIN_ANALOG(p) (((p) >= 14 && (p) <= 23) || ((p) >= 31 && (p) <= 39) || ((p) >= 49 && (p) <= 50) || ((p) >= 64 && (p) <= 69))
445 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
446 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
447 #define IS_PIN_I2C(p) ((p) == 18 || (p) == 19)
448 #define IS_PIN_SERIAL(p) (((p) > 6 && (p) < 11) || ((p) == 0 || (p) == 1) || ((p) > 30 && (p) < 35) || ((p) == 47 || (p) == 48))
449 #define PIN_TO_DIGITAL(p) (p)
450 // A0-A9 = D14-D23; A12-A20 = D31-D39; A23-A24 = D49-D50; A10-A11 = D64-D65; A21-A22 = D66-D67; A25-A26 = D68-D69
451 #define PIN_TO_ANALOG(p) (((p) <= 23) ? (p) - 14 : (((p) <= 39) ? (p) - 19 : (((p) <= 50) ? (p) - 26 : (((p) <= 65) ? (p) - 55 : (((p) <= 67) ? (p) - 45 : (p) - 43)))))
452 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
453 #define PIN_TO_SERVO(p) (p)
456 // Teensy 3.0, 3.1 and 3.2
457 #elif defined(__MK20DX128__) || defined(__MK20DX256__)
458 #define TOTAL_ANALOG_PINS 14
459 #define TOTAL_PINS 38 // 24 digital + 10 analog-digital + 4 analog
460 #define VERSION_BLINK_PIN 13
461 #define PIN_SERIAL1_RX 0
462 #define PIN_SERIAL1_TX 1
463 #define PIN_SERIAL2_RX 9
464 #define PIN_SERIAL2_TX 10
465 #define PIN_SERIAL3_RX 7
466 #define PIN_SERIAL3_TX 8
467 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 33)
468 #define IS_PIN_ANALOG(p) (((p) >= 14 && (p) <= 23) || ((p) >= 34 && (p) <= 38))
469 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
470 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
471 #define IS_PIN_I2C(p) ((p) == 18 || (p) == 19)
472 #define IS_PIN_SERIAL(p) (((p) > 6 && (p) < 11) || ((p) == 0 || (p) == 1))
473 #define PIN_TO_DIGITAL(p) (p)
474 #define PIN_TO_ANALOG(p) (((p) <= 23) ? (p) - 14 : (p) - 24)
475 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
476 #define PIN_TO_SERVO(p) (p)
480 #elif defined(__MKL26Z64__)
481 #define TOTAL_ANALOG_PINS 13
482 #define TOTAL_PINS 27 // 27 digital + 13 analog-digital
483 #define VERSION_BLINK_PIN 13
484 #define PIN_SERIAL1_RX 0
485 #define PIN_SERIAL1_TX 1
486 #define PIN_SERIAL2_RX 9
487 #define PIN_SERIAL2_TX 10
488 #define PIN_SERIAL3_RX 7
489 #define PIN_SERIAL3_TX 8
490 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 26)
491 #define IS_PIN_ANALOG(p) ((p) >= 14)
492 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
493 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
494 #define IS_PIN_I2C(p) ((p) == 18 || (p) == 19)
495 #define IS_PIN_SERIAL(p) (((p) > 6 && (p) < 11) || ((p) == 0 || (p) == 1))
496 #define PIN_TO_DIGITAL(p) (p)
497 #define PIN_TO_ANALOG(p) ((p) - 14)
498 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
499 #define PIN_TO_SERVO(p) (p)
502 // Teensy++ 1.0 and 2.0
503 #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
504 #define TOTAL_ANALOG_PINS 8
505 #define TOTAL_PINS 46 // 38 digital + 8 analog
506 #define VERSION_BLINK_PIN 6
507 #define PIN_SERIAL1_RX 2
508 #define PIN_SERIAL1_TX 3
509 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS)
510 #define IS_PIN_ANALOG(p) ((p) >= 38 && (p) < TOTAL_PINS)
511 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
512 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
513 #define IS_PIN_I2C(p) ((p) == 0 || (p) == 1)
514 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
515 #define IS_PIN_SERIAL(p) ((p) == 2 || (p) == 3)
516 #define PIN_TO_DIGITAL(p) (p)
517 #define PIN_TO_ANALOG(p) ((p) - 38)
518 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
519 #define PIN_TO_SERVO(p) (p)
523 #elif defined(__AVR_ATmega32U4__)
524 #define TOTAL_ANALOG_PINS 12
525 #define TOTAL_PINS 30 // 14 digital + 12 analog + 4 SPI (D14-D17 on ISP header)
526 #define VERSION_BLINK_PIN 13
527 #define PIN_SERIAL1_RX 0
528 #define PIN_SERIAL1_TX 1
529 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS)
530 #define IS_PIN_ANALOG(p) ((p) >= 18 && (p) < TOTAL_PINS)
531 #define IS_PIN_PWM(p) ((p) == 3 || (p) == 5 || (p) == 6 || (p) == 9 || (p) == 10 || (p) == 11 || (p) == 13)
532 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
533 #define IS_PIN_I2C(p) ((p) == 2 || (p) == 3)
534 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
535 #define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1)
536 #define PIN_TO_DIGITAL(p) (p)
537 #define PIN_TO_ANALOG(p) (p) - 18
538 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
539 #define PIN_TO_SERVO(p) (p)
542 // Intel Galileo Board (gen 1 and 2) and Intel Edison
543 #elif defined(ARDUINO_LINUX)
544 #define TOTAL_ANALOG_PINS 6
545 #define TOTAL_PINS 20 // 14 digital + 6 analog
546 #define VERSION_BLINK_PIN 13
547 #define PIN_SERIAL1_RX 0
548 #define PIN_SERIAL1_TX 1
549 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 19)
550 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 19)
551 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
552 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) - 2 < MAX_SERVOS)
553 #define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL)
554 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
555 #define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1)
556 #define PIN_TO_DIGITAL(p) (p)
557 #define PIN_TO_ANALOG(p) ((p) - 14)
558 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
559 #define PIN_TO_SERVO(p) ((p) - 2)
562 // RedBearLab BLE Nano with factory switch settings (S1 - S10)
563 #elif defined(BLE_NANO)
564 #define TOTAL_ANALOG_PINS 6
565 #define TOTAL_PINS 15 // 9 digital + 3 analog
566 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 14)
567 #define IS_PIN_ANALOG(p) ((p) == 8 || (p) == 9 || (p) == 10 || (p) == 11 || (p) == 12 || (p) == 14) //A0~A5
568 #define IS_PIN_PWM(p) ((p) == 3 || (p) == 5 || (p) == 6)
569 #define IS_PIN_SERVO(p) ((p) >= 2 && (p) <= 7)
570 #define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL)
571 #define IS_PIN_SPI(p) ((p) == CS || (p) == MOSI || (p) == MISO || (p) == SCK)
572 #define PIN_TO_DIGITAL(p) (p)
573 #define PIN_TO_ANALOG(p) ((p) - 8)
574 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
575 #define PIN_TO_SERVO(p) (p)
579 #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
580 #define TOTAL_ANALOG_PINS 8
581 #define TOTAL_PINS 32 // 24 digital + 8 analog
582 #define VERSION_BLINK_PIN 0
583 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS)
584 #define IS_PIN_ANALOG(p) ((p) >= 24 && (p) < TOTAL_PINS)
585 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
586 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
587 #define IS_PIN_I2C(p) ((p) == 16 || (p) == 17)
588 #define PIN_TO_DIGITAL(p) (p)
589 #define PIN_TO_ANALOG(p) ((p) - 24)
590 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
591 #define PIN_TO_SERVO(p) ((p) - 2)
595 #elif defined(__AVR_ATmega645__)
596 #define TOTAL_ANALOG_PINS 6
597 #define TOTAL_PINS 42 // 36 digital + 6 analog
598 #define VERSION_BLINK_PIN 13
599 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS)
600 #define IS_PIN_ANALOG(p) ((p) >= 36 && (p) < TOTAL_PINS)
601 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
602 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
603 #define IS_PIN_I2C(p) ((p) == 4 || (p) == 5)
604 #define PIN_TO_DIGITAL(p) (p)
605 #define PIN_TO_ANALOG(p) ((p) - 36)
606 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
607 #define PIN_TO_SERVO(p) ((p) - 2)
610 // Pic32 chipKIT FubarinoSD
611 #elif defined(_BOARD_FUBARINO_SD_)
612 #define TOTAL_ANALOG_PINS NUM_ANALOG_PINS // 15
613 #define TOTAL_PINS NUM_DIGITAL_PINS // 45, All pins can be digital
614 #define MAX_SERVOS NUM_DIGITAL_PINS
615 #define VERSION_BLINK_PIN PIN_LED1
616 #define IS_PIN_DIGITAL(p) 1
617 #define IS_PIN_ANALOG(p) ((p) >= 30 && (p) <= 44)
618 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
619 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
620 #define IS_PIN_I2C(p) ((p) == 1 || (p) == 2)
621 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
622 #define PIN_TO_DIGITAL(p) (p)
623 #define PIN_TO_ANALOG(p) (14 - (p - 30))
624 #define PIN_TO_PWM(p) (p)
625 #define PIN_TO_SERVO(p) (p)
628 // Pic32 chipKIT FubarinoMini
629 // Note, FubarinoMini analog pin 20 will not function in Firmata as analog input due to limitation in analog mapping
630 #elif defined(_BOARD_FUBARINO_MINI_)
631 #define TOTAL_ANALOG_PINS 14 // We have to fake this because of the poor analog pin mapping planning in FubarinoMini
632 #define TOTAL_PINS NUM_DIGITAL_PINS // 33
633 #define MAX_SERVOS NUM_DIGITAL_PINS
634 #define VERSION_BLINK_PIN PIN_LED1
635 #define IS_PIN_DIGITAL(p) ((p) != 14 && (p) != 15 && (p) != 31 && (p) != 32)
636 #define IS_PIN_ANALOG(p) ((p) == 0 || ((p) >= 3 && (p) <= 13))
637 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
638 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
639 #define IS_PIN_I2C(p) ((p) == 25 || (p) == 26)
640 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
641 #define PIN_TO_DIGITAL(p) (p)
642 #define PIN_TO_ANALOG(p) (p)
643 #define PIN_TO_PWM(p) (p)
644 #define PIN_TO_SERVO(p) (p)
647 // Pic32 chipKIT UNO32
648 #elif defined(_BOARD_UNO_) && defined(__PIC32) // NOTE: no _BOARD_UNO32_ to use
649 #define TOTAL_ANALOG_PINS NUM_ANALOG_PINS // 12
650 #define TOTAL_PINS NUM_DIGITAL_PINS // 47 All pins can be digital
651 #define MAX_SERVOS NUM_DIGITAL_PINS // All pins can be servo with SoftPWMservo
652 #define VERSION_BLINK_PIN PIN_LED1
653 #define IS_PIN_DIGITAL(p) ((p) >= 2)
654 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 25)
655 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
656 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
657 #define IS_PIN_I2C(p) ((p) == 45 || (p) == 46)
658 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
659 #define PIN_TO_DIGITAL(p) (p)
660 #define PIN_TO_ANALOG(p) ((p) - 14)
661 #define PIN_TO_PWM(p) (p)
662 #define PIN_TO_SERVO(p) (p)
665 // Pic32 chipKIT DP32
666 #elif defined(_BOARD_DP32_)
667 #define TOTAL_ANALOG_PINS 15 // Really only has 9, but have to override because of mistake in variant file
668 #define TOTAL_PINS NUM_DIGITAL_PINS // 19
669 #define MAX_SERVOS NUM_DIGITAL_PINS // All pins can be servo with SoftPWMservo
670 #define VERSION_BLINK_PIN PIN_LED1
671 #define IS_PIN_DIGITAL(p) (((p) != 1) && ((p) != 4) && ((p) != 5) && ((p) != 15) && ((p) != 16))
672 #define IS_PIN_ANALOG(p) ((p) >= 6 && (p) <= 14)
673 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
674 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
675 #define IS_PIN_I2C(p) ((p) == 2 || (p) == 3)
676 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
677 #define PIN_TO_DIGITAL(p) (p)
678 #define PIN_TO_ANALOG(p) (p)
679 #define PIN_TO_PWM(p) (p)
680 #define PIN_TO_SERVO(p) (p)
683 // Pic32 chipKIT uC32
684 #elif defined(_BOARD_UC32_)
685 #define TOTAL_ANALOG_PINS NUM_ANALOG_PINS // 12
686 #define TOTAL_PINS NUM_DIGITAL_PINS // 47 All pins can be digital
687 #define MAX_SERVOS NUM_DIGITAL_PINS // All pins can be servo with SoftPWMservo
688 #define VERSION_BLINK_PIN PIN_LED1
689 #define IS_PIN_DIGITAL(p) ((p) >= 2)
690 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 25)
691 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
692 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
693 #define IS_PIN_I2C(p) ((p) == 45 || (p) == 46)
694 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
695 #define PIN_TO_DIGITAL(p) (p)
696 #define PIN_TO_ANALOG(p) ((p) - 14)
697 #define PIN_TO_PWM(p) (p)
698 #define PIN_TO_SERVO(p) (p)
701 // Pic32 chipKIT WF32
702 #elif defined(_BOARD_WF32_)
703 #define TOTAL_ANALOG_PINS NUM_ANALOG_PINS
704 #define TOTAL_PINS NUM_DIGITAL_PINS
705 #define MAX_SERVOS NUM_DIGITAL_PINS
706 #define VERSION_BLINK_PIN PIN_LED1
707 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 49) // Accounts for SD and WiFi dedicated pins
708 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 25)
709 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
710 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
711 #define IS_PIN_I2C(p) ((p) == 34 || (p) == 35)
712 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
713 #define PIN_TO_DIGITAL(p) (p)
714 #define PIN_TO_ANALOG(p) ((p) - 14)
715 #define PIN_TO_PWM(p) (p)
716 #define PIN_TO_SERVO(p) (p)
719 // Pic32 chipKIT WiFire
720 #elif defined(_BOARD_WIFIRE_)
721 #define TOTAL_ANALOG_PINS NUM_ANALOG_PINS // 14
722 #define TOTAL_PINS NUM_DIGITAL_PINS // 71
723 #define MAX_SERVOS NUM_DIGITAL_PINS
724 #define VERSION_BLINK_PIN PIN_LED1
725 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 47) // Accounts for SD and WiFi dedicated pins
726 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 25)
727 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
728 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
729 #define IS_PIN_I2C(p) ((p) == 34 || (p) == 35)
730 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
731 #define PIN_TO_DIGITAL(p) (p)
732 #define PIN_TO_ANALOG(p) ((p) <= 25 ? ((p) - 14) : (p) - 36)
733 #define PIN_TO_PWM(p) (p)
734 #define PIN_TO_SERVO(p) (p)
737 // Pic32 chipKIT MAX32
738 #elif defined(_BOARD_MEGA_) && defined(__PIC32) // NOTE: no _BOARD_MAX32_ to use
739 #define TOTAL_ANALOG_PINS NUM_ANALOG_PINS // 16
740 #define TOTAL_PINS NUM_DIGITAL_PINS // 87
741 #define MAX_SERVOS NUM_DIGITAL_PINS
742 #define VERSION_BLINK_PIN PIN_LED1
743 #define IS_PIN_DIGITAL(p) ((p) >= 2)
744 #define IS_PIN_ANALOG(p) ((p) >= 54 && (p) <= 69)
745 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
746 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
747 #define IS_PIN_I2C(p) ((p) == 34 || (p) == 35)
748 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
749 #define PIN_TO_DIGITAL(p) (p)
750 #define PIN_TO_ANALOG(p) ((p) - 54)
751 #define PIN_TO_PWM(p) (p)
752 #define PIN_TO_SERVO(p) (p)
756 #elif defined(_BOARD_CHIPKIT_PI_)
757 #define TOTAL_ANALOG_PINS 16
758 #define TOTAL_PINS NUM_DIGITAL_PINS // 19
759 #define MAX_SERVOS NUM_DIGITAL_PINS
760 #define VERSION_BLINK_PIN PIN_LED1
761 #define IS_PIN_DIGITAL(p) (((p) >= 2) && ((p) <= 3) || (((p) >= 8) && ((p) <= 13)) || (((p) >= 14) && ((p) <= 17)))
762 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 17)
763 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
764 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
765 #define IS_PIN_I2C(p) ((p) == 16 || (p) == 17)
766 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
767 #define PIN_TO_DIGITAL(p) (p)
768 #define PIN_TO_ANALOG(p) ((p) <= 15 ? (p) - 14 : (p) - 12)
769 //#define PIN_TO_ANALOG(p) (((p) <= 16) ? ((p) - 14) : ((p) - 16))
770 #define PIN_TO_PWM(p) (p)
771 #define PIN_TO_SERVO(p) (p)
774 // Note: digital pins 9-16 are usable but not labeled on the board numerically.
775 // SS=9, MOSI=10, MISO=11, SCK=12, RX1=13, TX1=14, SCL=15, SDA=16
776 #elif defined(ARDUINO_PINOCCIO)
777 #define TOTAL_ANALOG_PINS 8
778 #define TOTAL_PINS NUM_DIGITAL_PINS // 32
779 #define VERSION_BLINK_PIN 23
780 #define PIN_SERIAL1_RX 13
781 #define PIN_SERIAL1_TX 14
782 #define IS_PIN_DIGITAL(p) (((p) >= 2) && ((p) <= 16)) || (((p) >= 24) && ((p) <= 31))
783 #define IS_PIN_ANALOG(p) ((p) >= 24 && (p) <= 31)
784 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
785 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
786 #define IS_PIN_I2C(p) ((p) == SCL || (p) == SDA)
787 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
788 #define IS_PIN_SERIAL(p) ((p) == 13 || (p) == 14)
789 #define PIN_TO_DIGITAL(p) (p)
790 #define PIN_TO_ANALOG(p) ((p) - 24)
791 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
792 #define PIN_TO_SERVO(p) ((p) - 2)
795 // note: boot mode GPIOs 0, 2 and 15 can be used as outputs, GPIOs 6-11 are in use for flash IO
796 #elif defined(ESP8266)
797 #define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS
798 #define TOTAL_PINS A0 + NUM_ANALOG_INPUTS
799 #define PIN_SERIAL_RX 3
800 #define PIN_SERIAL_TX 1
801 #define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 5) || ((p) >= 12 && (p) < A0))
802 #define IS_PIN_ANALOG(p) ((p) >= A0 && (p) < A0 + NUM_ANALOG_INPUTS)
803 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
804 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS)
805 #define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL)
806 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
807 #define IS_PIN_INTERRUPT(p) (digitalPinToInterrupt(p) > NOT_AN_INTERRUPT)
808 #define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL_RX || (p) == PIN_SERIAL_TX)
809 #define PIN_TO_DIGITAL(p) (p)
810 #define PIN_TO_ANALOG(p) ((p) - A0)
811 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
812 #define PIN_TO_SERVO(p) (p)
813 #define DEFAULT_PWM_RESOLUTION 10
815 // STM32 based boards
816 #elif defined(ARDUINO_ARCH_STM32)
817 #define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS
818 #define TOTAL_PINS NUM_DIGITAL_PINS
819 #define TOTAL_PORTS MAX_NB_PORT
820 #define VERSION_BLINK_PIN LED_BUILTIN
821 // PIN_SERIALY_RX/TX defined in the variant.h
822 #define IS_PIN_DIGITAL(p) (digitalPinIsValid(p) && !pinIsSerial(p))
823 #define IS_PIN_ANALOG(p) ((p >= A0) && (p < (A0 + TOTAL_ANALOG_PINS)) && !pinIsSerial(p))
824 #define IS_PIN_PWM(p) (IS_PIN_DIGITAL(p) && digitalPinHasPWM(p))
825 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
826 #define IS_PIN_I2C(p) (IS_PIN_DIGITAL(p) && digitalPinHasI2C(p))
827 #define IS_PIN_SPI(p) (IS_PIN_DIGITAL(p) && digitalPinHasSPI(p))
828 #define IS_PIN_INTERRUPT(p) (IS_PIN_DIGITAL(p) && (digitalPinToInterrupt(p) > NOT_AN_INTERRUPT)))
829 #define IS_PIN_SERIAL(p) (digitalPinHasSerial(p) && !pinIsSerial(p))
830 #define PIN_TO_DIGITAL(p) (p)
831 #define PIN_TO_ANALOG(p) (p-A0)
832 #define PIN_TO_PWM(p) (p)
833 #define PIN_TO_SERVO(p) (p)
834 #define DEFAULT_PWM_RESOLUTION PWM_RESOLUTION
836 // Adafruit Bluefruit nRF52 boards
837 #elif defined(ARDUINO_NRF52_ADAFRUIT)
838 #define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS
839 #define TOTAL_PINS 32
840 #define VERSION_BLINK_PIN LED_BUILTIN
841 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS)
842 #define IS_PIN_ANALOG(p) ((p) == PIN_A0 || (p) == PIN_A1 || (p) == PIN_A2 || (p) == PIN_A3 || \
843 (p) == PIN_A4 || (p) == PIN_A5 || (p) == PIN_A6 || (p) == PIN_A7)
844 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
845 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
846 #define IS_PIN_I2C(p) ((p) == PIN_WIRE_SDA || (p) == PIN_WIRE_SCL)
847 #define IS_PIN_SPI(p) ((p) == SS || (p)== MOSI || (p) == MISO || (p == SCK))
848 #define PIN_TO_DIGITAL(p) (p)
849 #define PIN_TO_ANALOG(p) ( ((p) == PIN_A0) ? 0 : ((p) == PIN_A1) ? 1 : ((p) == PIN_A2) ? 2 : ((p) == PIN_A3) ? 3 : \
850 ((p) == PIN_A4) ? 4 : ((p) == PIN_A5) ? 5 : ((p) == PIN_A6) ? 6 : ((p) == PIN_A7) ? 7 : (127))
851 #define PIN_TO_PWM(p) (p)
852 #define PIN_TO_SERVO(p) (p)
856 #error "Please edit Boards.h with a hardware abstraction for this board"
859 // as long this is not defined for all boards:
861 #define IS_PIN_SPI(p) 0
864 #ifndef IS_PIN_SERIAL
865 #define IS_PIN_SERIAL(p) 0
868 #ifndef DEFAULT_PWM_RESOLUTION
869 #define DEFAULT_PWM_RESOLUTION 8
872 /*==============================================================================
873 * readPort() - Read an 8 bit port
874 *============================================================================*/
876 static inline unsigned char readPort(byte, byte) __attribute__((always_inline, unused));
877 static inline unsigned char readPort(byte port, byte bitmask)
879 #if defined(ARDUINO_PINOUT_OPTIMIZE)
880 if (port == 0) return (PIND & 0xFC) & bitmask; // ignore Rx/Tx 0/1
881 if (port == 1) return ((PINB & 0x3F) | ((PINC & 0x03) << 6)) & bitmask;
882 if (port == 2) return ((PINC & 0x3C) >> 2) & bitmask;
885 unsigned char out = 0, pin = port * 8;
886 if (IS_PIN_DIGITAL(pin + 0) && (bitmask & 0x01) && digitalRead(PIN_TO_DIGITAL(pin + 0))) out |= 0x01;
887 if (IS_PIN_DIGITAL(pin + 1) && (bitmask & 0x02) && digitalRead(PIN_TO_DIGITAL(pin + 1))) out |= 0x02;
888 if (IS_PIN_DIGITAL(pin + 2) && (bitmask & 0x04) && digitalRead(PIN_TO_DIGITAL(pin + 2))) out |= 0x04;
889 if (IS_PIN_DIGITAL(pin + 3) && (bitmask & 0x08) && digitalRead(PIN_TO_DIGITAL(pin + 3))) out |= 0x08;
890 if (IS_PIN_DIGITAL(pin + 4) && (bitmask & 0x10) && digitalRead(PIN_TO_DIGITAL(pin + 4))) out |= 0x10;
891 if (IS_PIN_DIGITAL(pin + 5) && (bitmask & 0x20) && digitalRead(PIN_TO_DIGITAL(pin + 5))) out |= 0x20;
892 if (IS_PIN_DIGITAL(pin + 6) && (bitmask & 0x40) && digitalRead(PIN_TO_DIGITAL(pin + 6))) out |= 0x40;
893 if (IS_PIN_DIGITAL(pin + 7) && (bitmask & 0x80) && digitalRead(PIN_TO_DIGITAL(pin + 7))) out |= 0x80;
898 /*==============================================================================
899 * writePort() - Write an 8 bit port, only touch pins specified by a bitmask
900 *============================================================================*/
902 static inline unsigned char writePort(byte, byte, byte) __attribute__((always_inline, unused));
903 static inline unsigned char writePort(byte port, byte value, byte bitmask)
905 #if defined(ARDUINO_PINOUT_OPTIMIZE)
907 bitmask = bitmask & 0xFC; // do not touch Tx & Rx pins
908 byte valD = value & bitmask;
909 byte maskD = ~bitmask;
911 PORTD = (PORTD & maskD) | valD;
913 } else if (port == 1) {
914 byte valB = (value & bitmask) & 0x3F;
915 byte valC = (value & bitmask) >> 6;
916 byte maskB = ~(bitmask & 0x3F);
917 byte maskC = ~((bitmask & 0xC0) >> 6);
919 PORTB = (PORTB & maskB) | valB;
920 PORTC = (PORTC & maskC) | valC;
922 } else if (port == 2) {
923 bitmask = bitmask & 0x0F;
924 byte valC = (value & bitmask) << 2;
925 byte maskC = ~(bitmask << 2);
927 PORTC = (PORTC & maskC) | valC;
933 if ((bitmask & 0x01)) digitalWrite(PIN_TO_DIGITAL(pin + 0), (value & 0x01));
934 if ((bitmask & 0x02)) digitalWrite(PIN_TO_DIGITAL(pin + 1), (value & 0x02));
935 if ((bitmask & 0x04)) digitalWrite(PIN_TO_DIGITAL(pin + 2), (value & 0x04));
936 if ((bitmask & 0x08)) digitalWrite(PIN_TO_DIGITAL(pin + 3), (value & 0x08));
937 if ((bitmask & 0x10)) digitalWrite(PIN_TO_DIGITAL(pin + 4), (value & 0x10));
938 if ((bitmask & 0x20)) digitalWrite(PIN_TO_DIGITAL(pin + 5), (value & 0x20));
939 if ((bitmask & 0x40)) digitalWrite(PIN_TO_DIGITAL(pin + 6), (value & 0x40));
940 if ((bitmask & 0x80)) digitalWrite(PIN_TO_DIGITAL(pin + 7), (value & 0x80));
949 #define TOTAL_PORTS ((TOTAL_PINS + 7) / 8)
953 #endif /* Firmata_Boards_h */