Firmata firmware for Arduino
Firmata is a protocol for communicating with microcontrollers from software on a host computer
Boards.h
1 /*
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.
5 
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.
10 
11  See file LICENSE.txt for further informations on licensing terms.
12 
13  Last updated April 15th, 2018
14 */
15 
16 #ifndef Firmata_Boards_h
17 #define Firmata_Boards_h
18 
19 #include <inttypes.h>
20 
21 #if defined(ARDUINO) && ARDUINO >= 100
22 #include "Arduino.h" // for digitalRead, digitalWrite, etc
23 #else
24 #include "WProgram.h"
25 #endif
26 
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
31 #ifndef MAX_SERVOS
32 #define MAX_SERVOS 0
33 #endif
34 
35 /*
36  Firmata Hardware Abstraction Layer
37 
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.
44 
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.
50 
51 
52 General Constants:
53 
54 These constants provide basic information Firmata requires.
55 
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.
66 
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.
75 
76 VERSION_BLINK_PIN: When Firmata starts up, it will blink the version
77  number. This constant is the Arduino pin number where a
78  LED is connected.
79 
80 
81 Pin Mapping Macros:
82 
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.
89 
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.
96 
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.
100 
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.
107 
108 
109 Port Access Inline Funtions:
110 
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.
117 
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.
125 
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.
129 
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.
134 */
135 
136 /*==============================================================================
137  * Board Specific Configuration
138  *============================================================================*/
139 
140 #ifndef digitalPinHasPWM
141 #define digitalPinHasPWM(p) IS_PIN_DIGITAL(p)
142 #endif
143 
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
149 #else
150 #define TOTAL_ANALOG_PINS 8
151 #define TOTAL_PINS 22 // 14 digital + 8 analog
152 #endif
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
165 
166 
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)
180 
181 
182 // old Arduinos
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
197 
198 
199 // Arduino Mega
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)
221 
222 // Arduino UNO WiFi rev2 (ATMega 4809)
223 #elif defined(__AVR_ATmega4809__)
224 #define TOTAL_ANALOG_PINS 6
225 #define TOTAL_PINS 20 // 14 digital + 6 analog + /* 3 SPI (unexported, on ISP header) */
226 #define VERSION_BLINK_PIN 25
227 #define PIN_SERIAL1_RX 0
228 #define PIN_SERIAL1_TX 1
229 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS)
230 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) < TOTAL_PINS)
231 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
232 #define IS_PIN_SERVO(p) (p)
233 #define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL)
234 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
235 #define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1)
236 #define PIN_TO_DIGITAL(p) (p)
237 #define PIN_TO_ANALOG(p) (p) - 14
238 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
239 #define PIN_TO_SERVO(p) (p)
240 
241 // Arduino DUE
242 #elif defined(__SAM3X8E__)
243 #define TOTAL_ANALOG_PINS 12
244 #define TOTAL_PINS 66 // 54 digital + 12 analog
245 #define VERSION_BLINK_PIN 13
246 #define PIN_SERIAL1_RX 19
247 #define PIN_SERIAL1_TX 18
248 #define PIN_SERIAL2_RX 17
249 #define PIN_SERIAL2_TX 16
250 #define PIN_SERIAL3_RX 15
251 #define PIN_SERIAL3_TX 14
252 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS)
253 #define IS_PIN_ANALOG(p) ((p) >= 54 && (p) < TOTAL_PINS)
254 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
255 #define IS_PIN_SERVO(p) ((p) >= 2 && (p) - 2 < MAX_SERVOS)
256 #define IS_PIN_I2C(p) ((p) == 20 || (p) == 21) // 70 71
257 #define IS_PIN_SERIAL(p) ((p) > 13 && (p) < 20)
258 #define PIN_TO_DIGITAL(p) (p)
259 #define PIN_TO_ANALOG(p) ((p) - 54)
260 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
261 #define PIN_TO_SERVO(p) ((p) - 2)
262 
263 
264 // Arduino/Genuino MKR1000 or MKR1010
265 #elif defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010)
266 #define TOTAL_ANALOG_PINS 7
267 #define TOTAL_PINS 22 // 8 digital + 3 spi + 2 i2c + 2 uart + 7 analog
268 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 21)
269 #define IS_PIN_ANALOG(p) ((p) >= 15 && (p) < 15 + TOTAL_ANALOG_PINS)
270 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
271 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4
272 #define IS_PIN_I2C(p) ((p) == 11 || (p) == 12) // SDA = 11, SCL = 12
273 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
274 #define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL1_RX || (p) == PIN_SERIAL1_TX) //defined in variant.h RX = 13, TX = 14
275 #define PIN_TO_DIGITAL(p) (p)
276 #define PIN_TO_ANALOG(p) ((p) - 15)
277 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
278 #define PIN_TO_SERVO(p) (p) // deprecated since v2.4
279 
280 
281 // Arduino MKRZero
282 #elif defined(ARDUINO_SAMD_MKRZERO)
283 #define TOTAL_ANALOG_PINS 7
284 #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
285 #define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 21) || (p) == 32)
286 #define IS_PIN_ANALOG(p) (((p) >= 15 && (p) < 15 + TOTAL_ANALOG_PINS) || (p) == 33)
287 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
288 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4
289 #define IS_PIN_I2C(p) ((p) == 11 || (p) == 12) // SDA = 11, SCL = 12
290 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
291 #define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL1_RX || (p) == PIN_SERIAL1_TX) //defined in variant.h RX = 13, TX = 14
292 #define PIN_TO_DIGITAL(p) (p)
293 #define PIN_TO_ANALOG(p) ((p) - 15)
294 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
295 #define PIN_TO_SERVO(p) (p) // deprecated since v2.4
296 
297 // Arduino MKRFox1200
298 #elif defined(ARDUINO_SAMD_MKRFox1200)
299 #define TOTAL_ANALOG_PINS 7
300 #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
301 #define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 21))
302 #define IS_PIN_ANALOG(p) (((p) >= 15 && (p) < 15 + TOTAL_ANALOG_PINS) || (p) == 32)
303 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
304 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4
305 #define IS_PIN_I2C(p) ((p) == 11 || (p) == 12) // SDA = 11, SCL = 12
306 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
307 #define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL1_RX || (p) == PIN_SERIAL1_TX) //defined in variant.h RX = 13, TX = 14
308 #define PIN_TO_DIGITAL(p) (p)
309 #define PIN_TO_ANALOG(p) ((p) - 15)
310 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
311 #define PIN_TO_SERVO(p) (p) // deprecated since v2.4
312 
313 // Arduino MKR WAN 1300
314 #elif defined(ARDUINO_SAMD_MKRWAN1300)
315 #define TOTAL_ANALOG_PINS 7
316 #define TOTAL_PINS 33
317 #define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 21))
318 #define IS_PIN_ANALOG(p) (((p) >= 15 && (p) < 15 + TOTAL_ANALOG_PINS) || (p) == 32)
319 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
320 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4
321 #define IS_PIN_I2C(p) ((p) == 11 || (p) == 12) // SDA = 11, SCL = 12
322 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
323 #define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL1_RX || (p) == PIN_SERIAL1_TX) //defined in variant.h RX = 13, TX = 14
324 #define PIN_TO_DIGITAL(p) (p)
325 #define PIN_TO_ANALOG(p) ((p) - 15)
326 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
327 #define PIN_TO_SERVO(p) (p) // deprecated since v2.4
328 
329 // Arduino MKR GSM 1400
330 #elif defined(ARDUINO_SAMD_MKRGSM1400)
331 #define TOTAL_ANALOG_PINS 7
332 #define TOTAL_PINS 33
333 #define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 21))
334 #define IS_PIN_ANALOG(p) (((p) >= 15 && (p) < 15 + TOTAL_ANALOG_PINS) || (p) == 32)
335 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
336 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4
337 #define IS_PIN_I2C(p) ((p) == 11 || (p) == 12) // SDA = 11, SCL = 12
338 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
339 #define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL1_RX || (p) == PIN_SERIAL1_TX) //defined in variant.h RX = 13, TX = 14
340 #define PIN_TO_DIGITAL(p) (p)
341 #define PIN_TO_ANALOG(p) ((p) - 15)
342 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
343 #define PIN_TO_SERVO(p) (p) // deprecated since v2.4
344 
345 // Arduino Zero
346 // Note this will work with an Arduino Zero Pro, but not with an Arduino M0 Pro
347 // Arduino M0 Pro does not properly map pins to the board labeled pin numbers
348 #elif defined(_VARIANT_ARDUINO_ZERO_)
349 #define TOTAL_ANALOG_PINS 6
350 #define TOTAL_PINS 25 // 14 digital + 6 analog + 2 i2c + 3 spi
351 #define TOTAL_PORTS 3 // set when TOTAL_PINS > num digitial I/O pins
352 #define VERSION_BLINK_PIN LED_BUILTIN
353 //#define PIN_SERIAL1_RX 0 // already defined in zero core variant.h
354 //#define PIN_SERIAL1_TX 1 // already defined in zero core variant.h
355 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 19)
356 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) < 14 + TOTAL_ANALOG_PINS)
357 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
358 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4
359 #define IS_PIN_I2C(p) ((p) == 20 || (p) == 21) // SDA = 20, SCL = 21
360 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) // SS = A2
361 #define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1)
362 #define PIN_TO_DIGITAL(p) (p)
363 #define PIN_TO_ANALOG(p) ((p) - 14)
364 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
365 #define PIN_TO_SERVO(p) (p) // deprecated since v2.4
366 
367 // Arduino Primo
368 #elif defined(ARDUINO_PRIMO)
369 #define TOTAL_ANALOG_PINS 6
370 #define TOTAL_PINS 22 //14 digital + 6 analog + 2 i2c
371 #define VERSION_BLINK_PIN LED_BUILTIN
372 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < 20)
373 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) < 20)
374 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
375 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS+2)
376 #define IS_PIN_I2C(p) ((p) == PIN_WIRE_SDA || (p) == PIN_WIRE_SCL) // SDA = 20, SCL = 21
377 #define IS_PIN_SPI(p) ((p) == SS || (p)== MOSI || (p) == MISO || (p == SCK)) // 10, 11, 12, 13
378 #define PIN_TO_DIGITAL(p) (p)
379 #define PIN_TO_ANALOG(p) ((p) - 14)
380 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
381 #define PIN_TO_SERVO(p) (p)
382 
383 // Arduino 101
384 #elif defined(_VARIANT_ARDUINO_101_X_)
385 #define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS
386 #define TOTAL_PINS NUM_DIGITAL_PINS // 15 digital (including ATN pin) + 6 analog
387 #define VERSION_BLINK_PIN LED_BUILTIN
388 #define PIN_SERIAL1_RX 0
389 #define PIN_SERIAL1_TX 1
390 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 20)
391 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) < 14 + TOTAL_ANALOG_PINS)
392 #define IS_PIN_PWM(p) digitalPinHasPWM(p) // 3, 5, 6, 9
393 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4
394 #define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL) // SDA = 18, SCL = 19
395 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
396 #define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1)
397 #define PIN_TO_DIGITAL(p) (p)
398 #define PIN_TO_ANALOG(p) ((p) - 14)
399 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
400 #define PIN_TO_SERVO(p) (p) // deprecated since v2.4
401 
402 
403 // Teensy 1.0
404 #elif defined(__AVR_AT90USB162__)
405 #define TOTAL_ANALOG_PINS 0
406 #define TOTAL_PINS 21 // 21 digital + no analog
407 #define VERSION_BLINK_PIN 6
408 #define PIN_SERIAL1_RX 2
409 #define PIN_SERIAL1_TX 3
410 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS)
411 #define IS_PIN_ANALOG(p) (0)
412 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
413 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
414 #define IS_PIN_I2C(p) (0)
415 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
416 #define IS_PIN_SERIAL(p) ((p) == 2 || (p) == 3)
417 #define PIN_TO_DIGITAL(p) (p)
418 #define PIN_TO_ANALOG(p) (0)
419 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
420 #define PIN_TO_SERVO(p) (p)
421 
422 
423 // Teensy 2.0
424 #elif defined(__AVR_ATmega32U4__) && defined(CORE_TEENSY)
425 #define TOTAL_ANALOG_PINS 12
426 #define TOTAL_PINS 25 // 11 digital + 12 analog
427 #define VERSION_BLINK_PIN 11
428 #define PIN_SERIAL1_RX 7
429 #define PIN_SERIAL1_TX 8
430 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS)
431 #define IS_PIN_ANALOG(p) ((p) >= 11 && (p) <= 22)
432 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
433 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
434 #define IS_PIN_I2C(p) ((p) == 5 || (p) == 6)
435 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
436 #define IS_PIN_SERIAL(p) ((p) == 7 || (p) == 8)
437 #define PIN_TO_DIGITAL(p) (p)
438 #define PIN_TO_ANALOG(p) (((p) < 22) ? 21 - (p) : 11)
439 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
440 #define PIN_TO_SERVO(p) (p)
441 
442 
443 // Teensy 3.5 and 3.6
444 // reference: https://github.com/PaulStoffregen/cores/blob/master/teensy3/pins_arduino.h
445 #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
446 #define TOTAL_ANALOG_PINS 27 // 3.5 has 27 and 3.6 has 25
447 #define TOTAL_PINS 70 // 43 digital + 21 analog-digital + 6 analog (64-69)
448 #define VERSION_BLINK_PIN 13
449 #define PIN_SERIAL1_RX 0
450 #define PIN_SERIAL1_TX 1
451 #define PIN_SERIAL2_RX 9
452 #define PIN_SERIAL2_TX 10
453 #define PIN_SERIAL3_RX 7
454 #define PIN_SERIAL3_TX 8
455 #define PIN_SERIAL4_RX 31
456 #define PIN_SERIAL4_TX 32
457 #define PIN_SERIAL5_RX 34
458 #define PIN_SERIAL5_TX 33
459 #define PIN_SERIAL6_RX 47
460 #define PIN_SERIAL6_TX 48
461 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 63)
462 #define IS_PIN_ANALOG(p) (((p) >= 14 && (p) <= 23) || ((p) >= 31 && (p) <= 39) || ((p) >= 49 && (p) <= 50) || ((p) >= 64 && (p) <= 69))
463 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
464 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
465 #define IS_PIN_I2C(p) ((p) == 18 || (p) == 19)
466 #define IS_PIN_SERIAL(p) (((p) > 6 && (p) < 11) || ((p) == 0 || (p) == 1) || ((p) > 30 && (p) < 35) || ((p) == 47 || (p) == 48))
467 #define PIN_TO_DIGITAL(p) (p)
468 // A0-A9 = D14-D23; A12-A20 = D31-D39; A23-A24 = D49-D50; A10-A11 = D64-D65; A21-A22 = D66-D67; A25-A26 = D68-D69
469 #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)))))
470 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
471 #define PIN_TO_SERVO(p) (p)
472 
473 
474 // Teensy 3.0, 3.1 and 3.2
475 #elif defined(__MK20DX128__) || defined(__MK20DX256__)
476 #define TOTAL_ANALOG_PINS 14
477 #define TOTAL_PINS 38 // 24 digital + 10 analog-digital + 4 analog
478 #define VERSION_BLINK_PIN 13
479 #define PIN_SERIAL1_RX 0
480 #define PIN_SERIAL1_TX 1
481 #define PIN_SERIAL2_RX 9
482 #define PIN_SERIAL2_TX 10
483 #define PIN_SERIAL3_RX 7
484 #define PIN_SERIAL3_TX 8
485 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 33)
486 #define IS_PIN_ANALOG(p) (((p) >= 14 && (p) <= 23) || ((p) >= 34 && (p) <= 38))
487 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
488 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
489 #define IS_PIN_I2C(p) ((p) == 18 || (p) == 19)
490 #define IS_PIN_SERIAL(p) (((p) > 6 && (p) < 11) || ((p) == 0 || (p) == 1))
491 #define PIN_TO_DIGITAL(p) (p)
492 #define PIN_TO_ANALOG(p) (((p) <= 23) ? (p) - 14 : (p) - 24)
493 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
494 #define PIN_TO_SERVO(p) (p)
495 
496 
497 // Teensy-LC
498 #elif defined(__MKL26Z64__)
499 #define TOTAL_ANALOG_PINS 13
500 #define TOTAL_PINS 27 // 27 digital + 13 analog-digital
501 #define VERSION_BLINK_PIN 13
502 #define PIN_SERIAL1_RX 0
503 #define PIN_SERIAL1_TX 1
504 #define PIN_SERIAL2_RX 9
505 #define PIN_SERIAL2_TX 10
506 #define PIN_SERIAL3_RX 7
507 #define PIN_SERIAL3_TX 8
508 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 26)
509 #define IS_PIN_ANALOG(p) ((p) >= 14)
510 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
511 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
512 #define IS_PIN_I2C(p) ((p) == 18 || (p) == 19)
513 #define IS_PIN_SERIAL(p) (((p) > 6 && (p) < 11) || ((p) == 0 || (p) == 1))
514 #define PIN_TO_DIGITAL(p) (p)
515 #define PIN_TO_ANALOG(p) ((p) - 14)
516 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
517 #define PIN_TO_SERVO(p) (p)
518 
519 
520 // Teensy++ 1.0 and 2.0
521 #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
522 #define TOTAL_ANALOG_PINS 8
523 #define TOTAL_PINS 46 // 38 digital + 8 analog
524 #define VERSION_BLINK_PIN 6
525 #define PIN_SERIAL1_RX 2
526 #define PIN_SERIAL1_TX 3
527 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS)
528 #define IS_PIN_ANALOG(p) ((p) >= 38 && (p) < TOTAL_PINS)
529 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
530 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
531 #define IS_PIN_I2C(p) ((p) == 0 || (p) == 1)
532 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
533 #define IS_PIN_SERIAL(p) ((p) == 2 || (p) == 3)
534 #define PIN_TO_DIGITAL(p) (p)
535 #define PIN_TO_ANALOG(p) ((p) - 38)
536 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
537 #define PIN_TO_SERVO(p) (p)
538 
539 
540 // Leonardo
541 #elif defined(__AVR_ATmega32U4__)
542 #define TOTAL_ANALOG_PINS 12
543 #define TOTAL_PINS 30 // 14 digital + 12 analog + 4 SPI (D14-D17 on ISP header)
544 #define VERSION_BLINK_PIN 13
545 #define PIN_SERIAL1_RX 0
546 #define PIN_SERIAL1_TX 1
547 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS)
548 #define IS_PIN_ANALOG(p) ((p) >= 18 && (p) < TOTAL_PINS)
549 #define IS_PIN_PWM(p) ((p) == 3 || (p) == 5 || (p) == 6 || (p) == 9 || (p) == 10 || (p) == 11 || (p) == 13)
550 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
551 #define IS_PIN_I2C(p) ((p) == 2 || (p) == 3)
552 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
553 #define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1)
554 #define PIN_TO_DIGITAL(p) (p)
555 #define PIN_TO_ANALOG(p) (p) - 18
556 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
557 #define PIN_TO_SERVO(p) (p)
558 
559 
560 // Intel Galileo Board (gen 1 and 2) and Intel Edison
561 #elif defined(ARDUINO_LINUX)
562 #define TOTAL_ANALOG_PINS 6
563 #define TOTAL_PINS 20 // 14 digital + 6 analog
564 #define VERSION_BLINK_PIN 13
565 #define PIN_SERIAL1_RX 0
566 #define PIN_SERIAL1_TX 1
567 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 19)
568 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 19)
569 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
570 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) - 2 < MAX_SERVOS)
571 #define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL)
572 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
573 #define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1)
574 #define PIN_TO_DIGITAL(p) (p)
575 #define PIN_TO_ANALOG(p) ((p) - 14)
576 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
577 #define PIN_TO_SERVO(p) ((p) - 2)
578 
579 
580 // RedBearLab BLE Nano with factory switch settings (S1 - S10)
581 #elif defined(BLE_NANO)
582 #define TOTAL_ANALOG_PINS 6
583 #define TOTAL_PINS 15 // 9 digital + 3 analog
584 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 14)
585 #define IS_PIN_ANALOG(p) ((p) == 8 || (p) == 9 || (p) == 10 || (p) == 11 || (p) == 12 || (p) == 14) //A0~A5
586 #define IS_PIN_PWM(p) ((p) == 3 || (p) == 5 || (p) == 6)
587 #define IS_PIN_SERVO(p) ((p) >= 2 && (p) <= 7)
588 #define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL)
589 #define IS_PIN_SPI(p) ((p) == CS || (p) == MOSI || (p) == MISO || (p) == SCK)
590 #define PIN_TO_DIGITAL(p) (p)
591 #define PIN_TO_ANALOG(p) ((p) - 8)
592 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
593 #define PIN_TO_SERVO(p) (p)
594 
595 
596 // Sanguino
597 #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
598 #define TOTAL_ANALOG_PINS 8
599 #define TOTAL_PINS 32 // 24 digital + 8 analog
600 #define VERSION_BLINK_PIN 0
601 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS)
602 #define IS_PIN_ANALOG(p) ((p) >= 24 && (p) < TOTAL_PINS)
603 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
604 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
605 #define IS_PIN_I2C(p) ((p) == 16 || (p) == 17)
606 #define PIN_TO_DIGITAL(p) (p)
607 #define PIN_TO_ANALOG(p) ((p) - 24)
608 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
609 #define PIN_TO_SERVO(p) ((p) - 2)
610 
611 // Sanguino/Melzi, e.g. Creality Ender-3
612 #elif defined(__AVR_ATmega1284P__)
613 #define TOTAL_ANALOG_PINS 8
614 #define TOTAL_PINS 32
615 #define VERSION_BLINK_PIN 13
616 #define PIN_SERIAL1_RX 8 //PD0
617 #define PIN_SERIAL1_TX 9 //PD1
618 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS)
619 #define IS_PIN_ANALOG(p) ((p) >= 24 && (p) < TOTAL_PINS)
620 #define IS_PIN_PWM(p) ((p) == 3 || (p) == 4 || (p) == 6 || (p) == 7 || (p) == 12 || (p) == 13 || (p) == 14 || (p) == 15)
621 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
622 #define IS_PIN_I2C(p) ((p) == 16 || (p) == 17)
623 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
624 #define IS_PIN_SERIAL(p) ((p) == 8 || (p) == 9)
625 #define PIN_TO_DIGITAL(p) (p)
626 #define PIN_TO_ANALOG(p) (p) - 24
627 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
628 #define PIN_TO_SERVO(p) (p)
629 
630 
631 // Illuminato
632 #elif defined(__AVR_ATmega645__)
633 #define TOTAL_ANALOG_PINS 6
634 #define TOTAL_PINS 42 // 36 digital + 6 analog
635 #define VERSION_BLINK_PIN 13
636 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS)
637 #define IS_PIN_ANALOG(p) ((p) >= 36 && (p) < TOTAL_PINS)
638 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
639 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
640 #define IS_PIN_I2C(p) ((p) == 4 || (p) == 5)
641 #define PIN_TO_DIGITAL(p) (p)
642 #define PIN_TO_ANALOG(p) ((p) - 36)
643 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
644 #define PIN_TO_SERVO(p) ((p) - 2)
645 
646 
647 // Pic32 chipKIT FubarinoSD
648 #elif defined(_BOARD_FUBARINO_SD_)
649 #define TOTAL_ANALOG_PINS NUM_ANALOG_PINS // 15
650 #define TOTAL_PINS NUM_DIGITAL_PINS // 45, All pins can be digital
651 #define MAX_SERVOS NUM_DIGITAL_PINS
652 #define VERSION_BLINK_PIN PIN_LED1
653 #define IS_PIN_DIGITAL(p) 1
654 #define IS_PIN_ANALOG(p) ((p) >= 30 && (p) <= 44)
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) == 1 || (p) == 2)
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) (14 - (p - 30))
661 #define PIN_TO_PWM(p) (p)
662 #define PIN_TO_SERVO(p) (p)
663 
664 
665 // Pic32 chipKIT FubarinoMini
666 // Note, FubarinoMini analog pin 20 will not function in Firmata as analog input due to limitation in analog mapping
667 #elif defined(_BOARD_FUBARINO_MINI_)
668 #define TOTAL_ANALOG_PINS 14 // We have to fake this because of the poor analog pin mapping planning in FubarinoMini
669 #define TOTAL_PINS NUM_DIGITAL_PINS // 33
670 #define MAX_SERVOS NUM_DIGITAL_PINS
671 #define VERSION_BLINK_PIN PIN_LED1
672 #define IS_PIN_DIGITAL(p) ((p) != 14 && (p) != 15 && (p) != 31 && (p) != 32)
673 #define IS_PIN_ANALOG(p) ((p) == 0 || ((p) >= 3 && (p) <= 13))
674 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
675 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
676 #define IS_PIN_I2C(p) ((p) == 25 || (p) == 26)
677 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
678 #define PIN_TO_DIGITAL(p) (p)
679 #define PIN_TO_ANALOG(p) (p)
680 #define PIN_TO_PWM(p) (p)
681 #define PIN_TO_SERVO(p) (p)
682 
683 
684 // Pic32 chipKIT UNO32
685 #elif defined(_BOARD_UNO_) && defined(__PIC32) // NOTE: no _BOARD_UNO32_ to use
686 #define TOTAL_ANALOG_PINS NUM_ANALOG_PINS // 12
687 #define TOTAL_PINS NUM_DIGITAL_PINS // 47 All pins can be digital
688 #define MAX_SERVOS NUM_DIGITAL_PINS // All pins can be servo with SoftPWMservo
689 #define VERSION_BLINK_PIN PIN_LED1
690 #define IS_PIN_DIGITAL(p) ((p) >= 2)
691 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 25)
692 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
693 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
694 #define IS_PIN_I2C(p) ((p) == 45 || (p) == 46)
695 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
696 #define PIN_TO_DIGITAL(p) (p)
697 #define PIN_TO_ANALOG(p) ((p) - 14)
698 #define PIN_TO_PWM(p) (p)
699 #define PIN_TO_SERVO(p) (p)
700 
701 
702 // Pic32 chipKIT DP32
703 #elif defined(_BOARD_DP32_)
704 #define TOTAL_ANALOG_PINS 15 // Really only has 9, but have to override because of mistake in variant file
705 #define TOTAL_PINS NUM_DIGITAL_PINS // 19
706 #define MAX_SERVOS NUM_DIGITAL_PINS // All pins can be servo with SoftPWMservo
707 #define VERSION_BLINK_PIN PIN_LED1
708 #define IS_PIN_DIGITAL(p) (((p) != 1) && ((p) != 4) && ((p) != 5) && ((p) != 15) && ((p) != 16))
709 #define IS_PIN_ANALOG(p) ((p) >= 6 && (p) <= 14)
710 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
711 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
712 #define IS_PIN_I2C(p) ((p) == 2 || (p) == 3)
713 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
714 #define PIN_TO_DIGITAL(p) (p)
715 #define PIN_TO_ANALOG(p) (p)
716 #define PIN_TO_PWM(p) (p)
717 #define PIN_TO_SERVO(p) (p)
718 
719 
720 // Pic32 chipKIT uC32
721 #elif defined(_BOARD_UC32_)
722 #define TOTAL_ANALOG_PINS NUM_ANALOG_PINS // 12
723 #define TOTAL_PINS NUM_DIGITAL_PINS // 47 All pins can be digital
724 #define MAX_SERVOS NUM_DIGITAL_PINS // All pins can be servo with SoftPWMservo
725 #define VERSION_BLINK_PIN PIN_LED1
726 #define IS_PIN_DIGITAL(p) ((p) >= 2)
727 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 25)
728 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
729 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
730 #define IS_PIN_I2C(p) ((p) == 45 || (p) == 46)
731 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
732 #define PIN_TO_DIGITAL(p) (p)
733 #define PIN_TO_ANALOG(p) ((p) - 14)
734 #define PIN_TO_PWM(p) (p)
735 #define PIN_TO_SERVO(p) (p)
736 
737 
738 // Pic32 chipKIT WF32
739 #elif defined(_BOARD_WF32_)
740 #define TOTAL_ANALOG_PINS NUM_ANALOG_PINS
741 #define TOTAL_PINS NUM_DIGITAL_PINS
742 #define MAX_SERVOS NUM_DIGITAL_PINS
743 #define VERSION_BLINK_PIN PIN_LED1
744 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 49) // Accounts for SD and WiFi dedicated pins
745 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 25)
746 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
747 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
748 #define IS_PIN_I2C(p) ((p) == 34 || (p) == 35)
749 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
750 #define PIN_TO_DIGITAL(p) (p)
751 #define PIN_TO_ANALOG(p) ((p) - 14)
752 #define PIN_TO_PWM(p) (p)
753 #define PIN_TO_SERVO(p) (p)
754 
755 
756 // Pic32 chipKIT WiFire
757 #elif defined(_BOARD_WIFIRE_)
758 #define TOTAL_ANALOG_PINS NUM_ANALOG_PINS // 14
759 #define TOTAL_PINS NUM_DIGITAL_PINS // 71
760 #define MAX_SERVOS NUM_DIGITAL_PINS
761 #define VERSION_BLINK_PIN PIN_LED1
762 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 47) // Accounts for SD and WiFi dedicated pins
763 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 25)
764 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
765 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
766 #define IS_PIN_I2C(p) ((p) == 34 || (p) == 35)
767 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
768 #define PIN_TO_DIGITAL(p) (p)
769 #define PIN_TO_ANALOG(p) ((p) <= 25 ? ((p) - 14) : (p) - 36)
770 #define PIN_TO_PWM(p) (p)
771 #define PIN_TO_SERVO(p) (p)
772 
773 
774 // Pic32 chipKIT MAX32
775 #elif defined(_BOARD_MEGA_) && defined(__PIC32) // NOTE: no _BOARD_MAX32_ to use
776 #define TOTAL_ANALOG_PINS NUM_ANALOG_PINS // 16
777 #define TOTAL_PINS NUM_DIGITAL_PINS // 87
778 #define MAX_SERVOS NUM_DIGITAL_PINS
779 #define VERSION_BLINK_PIN PIN_LED1
780 #define IS_PIN_DIGITAL(p) ((p) >= 2)
781 #define IS_PIN_ANALOG(p) ((p) >= 54 && (p) <= 69)
782 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
783 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
784 #define IS_PIN_I2C(p) ((p) == 34 || (p) == 35)
785 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
786 #define PIN_TO_DIGITAL(p) (p)
787 #define PIN_TO_ANALOG(p) ((p) - 54)
788 #define PIN_TO_PWM(p) (p)
789 #define PIN_TO_SERVO(p) (p)
790 
791 
792 // Pic32 chipKIT Pi
793 #elif defined(_BOARD_CHIPKIT_PI_)
794 #define TOTAL_ANALOG_PINS 16
795 #define TOTAL_PINS NUM_DIGITAL_PINS // 19
796 #define MAX_SERVOS NUM_DIGITAL_PINS
797 #define VERSION_BLINK_PIN PIN_LED1
798 #define IS_PIN_DIGITAL(p) (((p) >= 2) && ((p) <= 3) || (((p) >= 8) && ((p) <= 13)) || (((p) >= 14) && ((p) <= 17)))
799 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 17)
800 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
801 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
802 #define IS_PIN_I2C(p) ((p) == 16 || (p) == 17)
803 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
804 #define PIN_TO_DIGITAL(p) (p)
805 #define PIN_TO_ANALOG(p) ((p) <= 15 ? (p) - 14 : (p) - 12)
806 //#define PIN_TO_ANALOG(p) (((p) <= 16) ? ((p) - 14) : ((p) - 16))
807 #define PIN_TO_PWM(p) (p)
808 #define PIN_TO_SERVO(p) (p)
809 
810 // Pinoccio Scout
811 // Note: digital pins 9-16 are usable but not labeled on the board numerically.
812 // SS=9, MOSI=10, MISO=11, SCK=12, RX1=13, TX1=14, SCL=15, SDA=16
813 #elif defined(ARDUINO_PINOCCIO)
814 #define TOTAL_ANALOG_PINS 8
815 #define TOTAL_PINS NUM_DIGITAL_PINS // 32
816 #define VERSION_BLINK_PIN 23
817 #define PIN_SERIAL1_RX 13
818 #define PIN_SERIAL1_TX 14
819 #define IS_PIN_DIGITAL(p) (((p) >= 2) && ((p) <= 16)) || (((p) >= 24) && ((p) <= 31))
820 #define IS_PIN_ANALOG(p) ((p) >= 24 && (p) <= 31)
821 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
822 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
823 #define IS_PIN_I2C(p) ((p) == SCL || (p) == SDA)
824 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
825 #define IS_PIN_SERIAL(p) ((p) == 13 || (p) == 14)
826 #define PIN_TO_DIGITAL(p) (p)
827 #define PIN_TO_ANALOG(p) ((p) - 24)
828 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
829 #define PIN_TO_SERVO(p) ((p) - 2)
830 
831 // ESP8266
832 // note: boot mode GPIOs 0, 2 and 15 can be used as outputs, GPIOs 6-11 are in use for flash IO
833 #elif defined(ESP8266)
834 #define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS
835 #define TOTAL_PINS A0 + NUM_ANALOG_INPUTS
836 #define PIN_SERIAL_RX 3
837 #define PIN_SERIAL_TX 1
838 #define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 5) || ((p) >= 12 && (p) < A0))
839 #define IS_PIN_ANALOG(p) ((p) >= A0 && (p) < A0 + NUM_ANALOG_INPUTS)
840 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
841 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS)
842 #define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL)
843 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
844 #define IS_PIN_INTERRUPT(p) (digitalPinToInterrupt(p) > NOT_AN_INTERRUPT)
845 #define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL_RX || (p) == PIN_SERIAL_TX)
846 #define PIN_TO_DIGITAL(p) (p)
847 #define PIN_TO_ANALOG(p) ((p) - A0)
848 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
849 #define PIN_TO_SERVO(p) (p)
850 #define DEFAULT_PWM_RESOLUTION 10
851 
852 // STM32 based boards
853 #elif defined(ARDUINO_ARCH_STM32)
854 #define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS
855 #define TOTAL_PINS NUM_DIGITAL_PINS
856 #define TOTAL_PORTS MAX_NB_PORT
857 #define VERSION_BLINK_PIN LED_BUILTIN
858 // PIN_SERIALY_RX/TX defined in the variant.h
859 #define IS_PIN_DIGITAL(p) (digitalPinIsValid(p) && !pinIsSerial(p))
860 #define IS_PIN_ANALOG(p) ((p >= A0) && (p < (A0 + TOTAL_ANALOG_PINS)) && !pinIsSerial(p))
861 #define IS_PIN_PWM(p) (IS_PIN_DIGITAL(p) && digitalPinHasPWM(p))
862 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
863 #define IS_PIN_I2C(p) (IS_PIN_DIGITAL(p) && digitalPinHasI2C(p))
864 #define IS_PIN_SPI(p) (IS_PIN_DIGITAL(p) && digitalPinHasSPI(p))
865 #define IS_PIN_INTERRUPT(p) (IS_PIN_DIGITAL(p) && (digitalPinToInterrupt(p) > NOT_AN_INTERRUPT)))
866 #define IS_PIN_SERIAL(p) (digitalPinHasSerial(p) && !pinIsSerial(p))
867 #define PIN_TO_DIGITAL(p) (p)
868 #define PIN_TO_ANALOG(p) (p-A0)
869 #define PIN_TO_PWM(p) (p)
870 #define PIN_TO_SERVO(p) (p)
871 #define DEFAULT_PWM_RESOLUTION PWM_RESOLUTION
872 
873 // Adafruit Bluefruit nRF52 boards
874 #elif defined(ARDUINO_NRF52_ADAFRUIT)
875 #define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS
876 #define TOTAL_PINS NUM_DIGITAL_PINS
877 #define VERSION_BLINK_PIN LED_BUILTIN
878 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS)
879 #define IS_PIN_ANALOG(p) ((p) == PIN_A0 || (p) == PIN_A1 || (p) == PIN_A2 || (p) == PIN_A3 || \
880  (p) == PIN_A4 || (p) == PIN_A5 || (p) == PIN_A6 || (p) == PIN_A7)
881 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
882 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
883 #define IS_PIN_I2C(p) ((p) == PIN_WIRE_SDA || (p) == PIN_WIRE_SCL)
884 #define IS_PIN_SPI(p) ((p) == SS || (p)== MOSI || (p) == MISO || (p == SCK))
885 #define PIN_TO_DIGITAL(p) (p)
886 #define PIN_TO_ANALOG(p) ( ((p) == PIN_A0) ? 0 : ((p) == PIN_A1) ? 1 : ((p) == PIN_A2) ? 2 : ((p) == PIN_A3) ? 3 : \
887  ((p) == PIN_A4) ? 4 : ((p) == PIN_A5) ? 5 : ((p) == PIN_A6) ? 6 : ((p) == PIN_A7) ? 7 : (127))
888 #define PIN_TO_PWM(p) (p)
889 #define PIN_TO_SERVO(p) (p)
890 
891 // anything else
892 #else
893 #error "Please edit Boards.h with a hardware abstraction for this board"
894 #endif
895 
896 // as long this is not defined for all boards:
897 #ifndef IS_PIN_SPI
898 #define IS_PIN_SPI(p) 0
899 #endif
900 
901 #ifndef IS_PIN_SERIAL
902 #define IS_PIN_SERIAL(p) 0
903 #endif
904 
905 #ifndef DEFAULT_PWM_RESOLUTION
906 #define DEFAULT_PWM_RESOLUTION 8
907 #endif
908 
909 /*==============================================================================
910  * readPort() - Read an 8 bit port
911  *============================================================================*/
912 
913 static inline unsigned char readPort(byte, byte) __attribute__((always_inline, unused));
914 static inline unsigned char readPort(byte port, byte bitmask)
915 {
916 #if defined(ARDUINO_PINOUT_OPTIMIZE)
917  if (port == 0) return (PIND & 0xFC) & bitmask; // ignore Rx/Tx 0/1
918  if (port == 1) return ((PINB & 0x3F) | ((PINC & 0x03) << 6)) & bitmask;
919  if (port == 2) return ((PINC & 0x3C) >> 2) & bitmask;
920  return 0;
921 #else
922  unsigned char out = 0, pin = port * 8;
923  if (IS_PIN_DIGITAL(pin + 0) && (bitmask & 0x01) && digitalRead(PIN_TO_DIGITAL(pin + 0))) out |= 0x01;
924  if (IS_PIN_DIGITAL(pin + 1) && (bitmask & 0x02) && digitalRead(PIN_TO_DIGITAL(pin + 1))) out |= 0x02;
925  if (IS_PIN_DIGITAL(pin + 2) && (bitmask & 0x04) && digitalRead(PIN_TO_DIGITAL(pin + 2))) out |= 0x04;
926  if (IS_PIN_DIGITAL(pin + 3) && (bitmask & 0x08) && digitalRead(PIN_TO_DIGITAL(pin + 3))) out |= 0x08;
927  if (IS_PIN_DIGITAL(pin + 4) && (bitmask & 0x10) && digitalRead(PIN_TO_DIGITAL(pin + 4))) out |= 0x10;
928  if (IS_PIN_DIGITAL(pin + 5) && (bitmask & 0x20) && digitalRead(PIN_TO_DIGITAL(pin + 5))) out |= 0x20;
929  if (IS_PIN_DIGITAL(pin + 6) && (bitmask & 0x40) && digitalRead(PIN_TO_DIGITAL(pin + 6))) out |= 0x40;
930  if (IS_PIN_DIGITAL(pin + 7) && (bitmask & 0x80) && digitalRead(PIN_TO_DIGITAL(pin + 7))) out |= 0x80;
931  return out;
932 #endif
933 }
934 
935 /*==============================================================================
936  * writePort() - Write an 8 bit port, only touch pins specified by a bitmask
937  *============================================================================*/
938 
939 static inline unsigned char writePort(byte, byte, byte) __attribute__((always_inline, unused));
940 static inline unsigned char writePort(byte port, byte value, byte bitmask)
941 {
942 #if defined(ARDUINO_PINOUT_OPTIMIZE)
943  if (port == 0) {
944  bitmask = bitmask & 0xFC; // do not touch Tx & Rx pins
945  byte valD = value & bitmask;
946  byte maskD = ~bitmask;
947  cli();
948  PORTD = (PORTD & maskD) | valD;
949  sei();
950  } else if (port == 1) {
951  byte valB = (value & bitmask) & 0x3F;
952  byte valC = (value & bitmask) >> 6;
953  byte maskB = ~(bitmask & 0x3F);
954  byte maskC = ~((bitmask & 0xC0) >> 6);
955  cli();
956  PORTB = (PORTB & maskB) | valB;
957  PORTC = (PORTC & maskC) | valC;
958  sei();
959  } else if (port == 2) {
960  bitmask = bitmask & 0x0F;
961  byte valC = (value & bitmask) << 2;
962  byte maskC = ~(bitmask << 2);
963  cli();
964  PORTC = (PORTC & maskC) | valC;
965  sei();
966  }
967  return 1;
968 #else
969  byte pin = port * 8;
970  if ((bitmask & 0x01)) digitalWrite(PIN_TO_DIGITAL(pin + 0), (value & 0x01));
971  if ((bitmask & 0x02)) digitalWrite(PIN_TO_DIGITAL(pin + 1), (value & 0x02));
972  if ((bitmask & 0x04)) digitalWrite(PIN_TO_DIGITAL(pin + 2), (value & 0x04));
973  if ((bitmask & 0x08)) digitalWrite(PIN_TO_DIGITAL(pin + 3), (value & 0x08));
974  if ((bitmask & 0x10)) digitalWrite(PIN_TO_DIGITAL(pin + 4), (value & 0x10));
975  if ((bitmask & 0x20)) digitalWrite(PIN_TO_DIGITAL(pin + 5), (value & 0x20));
976  if ((bitmask & 0x40)) digitalWrite(PIN_TO_DIGITAL(pin + 6), (value & 0x40));
977  if ((bitmask & 0x80)) digitalWrite(PIN_TO_DIGITAL(pin + 7), (value & 0x80));
978  return 1;
979 #endif
980 }
981 
982 
983 
984 
985 #ifndef TOTAL_PORTS
986 #define TOTAL_PORTS ((TOTAL_PINS + 7) / 8)
987 #endif
988 
989 
990 #endif /* Firmata_Boards_h */