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 August 14th, 2017
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
297 // Note this will work with an Arduino Zero Pro, but not with an Arduino M0 Pro
298 // Arduino M0 Pro does not properly map pins to the board labeled pin numbers
299 #elif defined(_VARIANT_ARDUINO_ZERO_)
300 #define TOTAL_ANALOG_PINS 6
301 #define TOTAL_PINS 25 // 14 digital + 6 analog + 2 i2c + 3 spi
302 #define TOTAL_PORTS 3 // set when TOTAL_PINS > num digitial I/O pins
303 #define VERSION_BLINK_PIN LED_BUILTIN
304 //#define PIN_SERIAL1_RX 0 // already defined in zero core variant.h
305 //#define PIN_SERIAL1_TX 1 // already defined in zero core variant.h
306 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 19)
307 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) < 14 + TOTAL_ANALOG_PINS)
308 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
309 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4
310 #define IS_PIN_I2C(p) ((p) == 20 || (p) == 21) // SDA = 20, SCL = 21
311 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) // SS = A2
312 #define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1)
313 #define PIN_TO_DIGITAL(p) (p)
314 #define PIN_TO_ANALOG(p) ((p) - 14)
315 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
316 #define PIN_TO_SERVO(p) (p) // deprecated since v2.4
319 #elif defined(ARDUINO_PRIMO)
320 #define TOTAL_ANALOG_PINS 6
321 #define TOTAL_PINS 22 //14 digital + 6 analog + 2 i2c
322 #define VERSION_BLINK_PIN LED_BUILTIN
323 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < 20)
324 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) < 20)
325 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
326 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS+2)
327 #define IS_PIN_I2C(p) ((p) == PIN_WIRE_SDA || (p) == PIN_WIRE_SCL) // SDA = 20, SCL = 21
328 #define IS_PIN_SPI(p) ((p) == SS || (p)== MOSI || (p) == MISO || (p == SCK)) // 10, 11, 12, 13
329 #define PIN_TO_DIGITAL(p) (p)
330 #define PIN_TO_ANALOG(p) ((p) - 14)
331 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
332 #define PIN_TO_SERVO(p) (p)
335 #elif defined(_VARIANT_ARDUINO_101_X_)
336 #define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS
337 #define TOTAL_PINS NUM_DIGITAL_PINS // 15 digital (including ATN pin) + 6 analog
338 #define VERSION_BLINK_PIN LED_BUILTIN
339 #define PIN_SERIAL1_RX 0
340 #define PIN_SERIAL1_TX 1
341 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 20)
342 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) < 14 + TOTAL_ANALOG_PINS)
343 #define IS_PIN_PWM(p) digitalPinHasPWM(p) // 3, 5, 6, 9
344 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) // deprecated since v2.4
345 #define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL) // SDA = 18, SCL = 19
346 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
347 #define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1)
348 #define PIN_TO_DIGITAL(p) (p)
349 #define PIN_TO_ANALOG(p) ((p) - 14)
350 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
351 #define PIN_TO_SERVO(p) (p) // deprecated since v2.4
355 #elif defined(__AVR_AT90USB162__)
356 #define TOTAL_ANALOG_PINS 0
357 #define TOTAL_PINS 21 // 21 digital + no analog
358 #define VERSION_BLINK_PIN 6
359 #define PIN_SERIAL1_RX 2
360 #define PIN_SERIAL1_TX 3
361 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS)
362 #define IS_PIN_ANALOG(p) (0)
363 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
364 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
365 #define IS_PIN_I2C(p) (0)
366 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
367 #define IS_PIN_SERIAL(p) ((p) == 2 || (p) == 3)
368 #define PIN_TO_DIGITAL(p) (p)
369 #define PIN_TO_ANALOG(p) (0)
370 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
371 #define PIN_TO_SERVO(p) (p)
375 #elif defined(__AVR_ATmega32U4__) && defined(CORE_TEENSY)
376 #define TOTAL_ANALOG_PINS 12
377 #define TOTAL_PINS 25 // 11 digital + 12 analog
378 #define VERSION_BLINK_PIN 11
379 #define PIN_SERIAL1_RX 7
380 #define PIN_SERIAL1_TX 8
381 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS)
382 #define IS_PIN_ANALOG(p) ((p) >= 11 && (p) <= 22)
383 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
384 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
385 #define IS_PIN_I2C(p) ((p) == 5 || (p) == 6)
386 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
387 #define IS_PIN_SERIAL(p) ((p) == 7 || (p) == 8)
388 #define PIN_TO_DIGITAL(p) (p)
389 #define PIN_TO_ANALOG(p) (((p) < 22) ? 21 - (p) : 11)
390 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
391 #define PIN_TO_SERVO(p) (p)
394 // Teensy 3.5 and 3.6
395 // reference: https://github.com/PaulStoffregen/cores/blob/master/teensy3/pins_arduino.h
396 #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
397 #define TOTAL_ANALOG_PINS 27 // 3.5 has 27 and 3.6 has 25
398 #define TOTAL_PINS 70 // 43 digital + 21 analog-digital + 6 analog (64-69)
399 #define VERSION_BLINK_PIN 13
400 #define PIN_SERIAL1_RX 0
401 #define PIN_SERIAL1_TX 1
402 #define PIN_SERIAL2_RX 9
403 #define PIN_SERIAL2_TX 10
404 #define PIN_SERIAL3_RX 7
405 #define PIN_SERIAL3_TX 8
406 // The following 2 UARTs are not yet available via SerialFirmata
407 #define PIN_SERIAL4_RX 31
408 #define PIN_SERIAL5_TX 32
409 #define PIN_SERIAL6_RX 34
410 #define PIN_SERIAL6_TX 33
411 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 63)
412 #define IS_PIN_ANALOG(p) (((p) >= 14 && (p) <= 23) || ((p) >= 31 && (p) <= 39) || ((p) >= 49 && (p) <= 50) || ((p) >= 64 && (p) <= 69))
413 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
414 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
415 #define IS_PIN_I2C(p) ((p) == 18 || (p) == 19)
416 #define IS_PIN_SERIAL(p) (((p) > 6 && (p) < 11) || ((p) == 0 || (p) == 1))
417 #define PIN_TO_DIGITAL(p) (p)
418 // A0-A9 = D14-D23; A12-A20 = D31-D39; A23-A24 = D49-D50; A10-A11 = D64-D65; A21-A22 = D66-D67; A25-A26 = D68-D69
419 #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)))))
420 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
421 #define PIN_TO_SERVO(p) (p)
424 // Teensy 3.0, 3.1 and 3.2
425 #elif defined(__MK20DX128__) || defined(__MK20DX256__)
426 #define TOTAL_ANALOG_PINS 14
427 #define TOTAL_PINS 38 // 24 digital + 10 analog-digital + 4 analog
428 #define VERSION_BLINK_PIN 13
429 #define PIN_SERIAL1_RX 0
430 #define PIN_SERIAL1_TX 1
431 #define PIN_SERIAL2_RX 9
432 #define PIN_SERIAL2_TX 10
433 #define PIN_SERIAL3_RX 7
434 #define PIN_SERIAL3_TX 8
435 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 33)
436 #define IS_PIN_ANALOG(p) (((p) >= 14 && (p) <= 23) || ((p) >= 34 && (p) <= 38))
437 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
438 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
439 #define IS_PIN_I2C(p) ((p) == 18 || (p) == 19)
440 #define IS_PIN_SERIAL(p) (((p) > 6 && (p) < 11) || ((p) == 0 || (p) == 1))
441 #define PIN_TO_DIGITAL(p) (p)
442 #define PIN_TO_ANALOG(p) (((p) <= 23) ? (p) - 14 : (p) - 24)
443 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
444 #define PIN_TO_SERVO(p) (p)
448 #elif defined(__MKL26Z64__)
449 #define TOTAL_ANALOG_PINS 13
450 #define TOTAL_PINS 27 // 27 digital + 13 analog-digital
451 #define VERSION_BLINK_PIN 13
452 #define PIN_SERIAL1_RX 0
453 #define PIN_SERIAL1_TX 1
454 #define PIN_SERIAL2_RX 9
455 #define PIN_SERIAL2_TX 10
456 #define PIN_SERIAL3_RX 7
457 #define PIN_SERIAL3_TX 8
458 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) <= 26)
459 #define IS_PIN_ANALOG(p) ((p) >= 14)
460 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
461 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
462 #define IS_PIN_I2C(p) ((p) == 18 || (p) == 19)
463 #define IS_PIN_SERIAL(p) (((p) > 6 && (p) < 11) || ((p) == 0 || (p) == 1))
464 #define PIN_TO_DIGITAL(p) (p)
465 #define PIN_TO_ANALOG(p) ((p) - 14)
466 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
467 #define PIN_TO_SERVO(p) (p)
470 // Teensy++ 1.0 and 2.0
471 #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
472 #define TOTAL_ANALOG_PINS 8
473 #define TOTAL_PINS 46 // 38 digital + 8 analog
474 #define VERSION_BLINK_PIN 6
475 #define PIN_SERIAL1_RX 2
476 #define PIN_SERIAL1_TX 3
477 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS)
478 #define IS_PIN_ANALOG(p) ((p) >= 38 && (p) < TOTAL_PINS)
479 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
480 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
481 #define IS_PIN_I2C(p) ((p) == 0 || (p) == 1)
482 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
483 #define IS_PIN_SERIAL(p) ((p) == 2 || (p) == 3)
484 #define PIN_TO_DIGITAL(p) (p)
485 #define PIN_TO_ANALOG(p) ((p) - 38)
486 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
487 #define PIN_TO_SERVO(p) (p)
491 #elif defined(__AVR_ATmega32U4__)
492 #define TOTAL_ANALOG_PINS 12
493 #define TOTAL_PINS 30 // 14 digital + 12 analog + 4 SPI (D14-D17 on ISP header)
494 #define VERSION_BLINK_PIN 13
495 #define PIN_SERIAL1_RX 0
496 #define PIN_SERIAL1_TX 1
497 #define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS)
498 #define IS_PIN_ANALOG(p) ((p) >= 18 && (p) < TOTAL_PINS)
499 #define IS_PIN_PWM(p) ((p) == 3 || (p) == 5 || (p) == 6 || (p) == 9 || (p) == 10 || (p) == 11 || (p) == 13)
500 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
501 #define IS_PIN_I2C(p) ((p) == 2 || (p) == 3)
502 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
503 #define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1)
504 #define PIN_TO_DIGITAL(p) (p)
505 #define PIN_TO_ANALOG(p) (p) - 18
506 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
507 #define PIN_TO_SERVO(p) (p)
510 // Intel Galileo Board (gen 1 and 2) and Intel Edison
511 #elif defined(ARDUINO_LINUX)
512 #define TOTAL_ANALOG_PINS 6
513 #define TOTAL_PINS 20 // 14 digital + 6 analog
514 #define VERSION_BLINK_PIN 13
515 #define PIN_SERIAL1_RX 0
516 #define PIN_SERIAL1_TX 1
517 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 19)
518 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 19)
519 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
520 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) - 2 < MAX_SERVOS)
521 #define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL)
522 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
523 #define IS_PIN_SERIAL(p) ((p) == 0 || (p) == 1)
524 #define PIN_TO_DIGITAL(p) (p)
525 #define PIN_TO_ANALOG(p) ((p) - 14)
526 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
527 #define PIN_TO_SERVO(p) ((p) - 2)
530 // RedBearLab BLE Nano with factory switch settings (S1 - S10)
531 #elif defined(BLE_NANO)
532 #define TOTAL_ANALOG_PINS 6
533 #define TOTAL_PINS 15 // 9 digital + 3 analog
534 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 14)
535 #define IS_PIN_ANALOG(p) ((p) == 8 || (p) == 9 || (p) == 10 || (p) == 11 || (p) == 12 || (p) == 14) //A0~A5
536 #define IS_PIN_PWM(p) ((p) == 3 || (p) == 5 || (p) == 6)
537 #define IS_PIN_SERVO(p) ((p) >= 2 && (p) <= 7)
538 #define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL)
539 #define IS_PIN_SPI(p) ((p) == CS || (p) == MOSI || (p) == MISO || (p) == SCK)
540 #define PIN_TO_DIGITAL(p) (p)
541 #define PIN_TO_ANALOG(p) ((p) - 8)
542 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
543 #define PIN_TO_SERVO(p) (p)
547 #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
548 #define TOTAL_ANALOG_PINS 8
549 #define TOTAL_PINS 32 // 24 digital + 8 analog
550 #define VERSION_BLINK_PIN 0
551 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS)
552 #define IS_PIN_ANALOG(p) ((p) >= 24 && (p) < TOTAL_PINS)
553 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
554 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
555 #define IS_PIN_I2C(p) ((p) == 16 || (p) == 17)
556 #define PIN_TO_DIGITAL(p) (p)
557 #define PIN_TO_ANALOG(p) ((p) - 24)
558 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
559 #define PIN_TO_SERVO(p) ((p) - 2)
563 #elif defined(__AVR_ATmega645__)
564 #define TOTAL_ANALOG_PINS 6
565 #define TOTAL_PINS 42 // 36 digital + 6 analog
566 #define VERSION_BLINK_PIN 13
567 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS)
568 #define IS_PIN_ANALOG(p) ((p) >= 36 && (p) < TOTAL_PINS)
569 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
570 #define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS)
571 #define IS_PIN_I2C(p) ((p) == 4 || (p) == 5)
572 #define PIN_TO_DIGITAL(p) (p)
573 #define PIN_TO_ANALOG(p) ((p) - 36)
574 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
575 #define PIN_TO_SERVO(p) ((p) - 2)
578 // Pic32 chipKIT FubarinoSD
579 #elif defined(_BOARD_FUBARINO_SD_)
580 #define TOTAL_ANALOG_PINS NUM_ANALOG_PINS // 15
581 #define TOTAL_PINS NUM_DIGITAL_PINS // 45, All pins can be digital
582 #define MAX_SERVOS NUM_DIGITAL_PINS
583 #define VERSION_BLINK_PIN PIN_LED1
584 #define IS_PIN_DIGITAL(p) 1
585 #define IS_PIN_ANALOG(p) ((p) >= 30 && (p) <= 44)
586 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
587 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
588 #define IS_PIN_I2C(p) ((p) == 1 || (p) == 2)
589 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
590 #define PIN_TO_DIGITAL(p) (p)
591 #define PIN_TO_ANALOG(p) (14 - (p - 30))
592 #define PIN_TO_PWM(p) (p)
593 #define PIN_TO_SERVO(p) (p)
596 // Pic32 chipKIT FubarinoMini
597 // Note, FubarinoMini analog pin 20 will not function in Firmata as analog input due to limitation in analog mapping
598 #elif defined(_BOARD_FUBARINO_MINI_)
599 #define TOTAL_ANALOG_PINS 14 // We have to fake this because of the poor analog pin mapping planning in FubarinoMini
600 #define TOTAL_PINS NUM_DIGITAL_PINS // 33
601 #define MAX_SERVOS NUM_DIGITAL_PINS
602 #define VERSION_BLINK_PIN PIN_LED1
603 #define IS_PIN_DIGITAL(p) ((p) != 14 && (p) != 15 && (p) != 31 && (p) != 32)
604 #define IS_PIN_ANALOG(p) ((p) == 0 || ((p) >= 3 && (p) <= 13))
605 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
606 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
607 #define IS_PIN_I2C(p) ((p) == 25 || (p) == 26)
608 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
609 #define PIN_TO_DIGITAL(p) (p)
610 #define PIN_TO_ANALOG(p) (p)
611 #define PIN_TO_PWM(p) (p)
612 #define PIN_TO_SERVO(p) (p)
615 // Pic32 chipKIT UNO32
616 #elif defined(_BOARD_UNO_) && defined(__PIC32) // NOTE: no _BOARD_UNO32_ to use
617 #define TOTAL_ANALOG_PINS NUM_ANALOG_PINS // 12
618 #define TOTAL_PINS NUM_DIGITAL_PINS // 47 All pins can be digital
619 #define MAX_SERVOS NUM_DIGITAL_PINS // All pins can be servo with SoftPWMservo
620 #define VERSION_BLINK_PIN PIN_LED1
621 #define IS_PIN_DIGITAL(p) ((p) >= 2)
622 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 25)
623 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
624 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
625 #define IS_PIN_I2C(p) ((p) == 45 || (p) == 46)
626 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
627 #define PIN_TO_DIGITAL(p) (p)
628 #define PIN_TO_ANALOG(p) ((p) - 14)
629 #define PIN_TO_PWM(p) (p)
630 #define PIN_TO_SERVO(p) (p)
633 // Pic32 chipKIT DP32
634 #elif defined(_BOARD_DP32_)
635 #define TOTAL_ANALOG_PINS 15 // Really only has 9, but have to override because of mistake in variant file
636 #define TOTAL_PINS NUM_DIGITAL_PINS // 19
637 #define MAX_SERVOS NUM_DIGITAL_PINS // All pins can be servo with SoftPWMservo
638 #define VERSION_BLINK_PIN PIN_LED1
639 #define IS_PIN_DIGITAL(p) (((p) != 1) && ((p) != 4) && ((p) != 5) && ((p) != 15) && ((p) != 16))
640 #define IS_PIN_ANALOG(p) ((p) >= 6 && (p) <= 14)
641 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
642 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
643 #define IS_PIN_I2C(p) ((p) == 2 || (p) == 3)
644 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
645 #define PIN_TO_DIGITAL(p) (p)
646 #define PIN_TO_ANALOG(p) (p)
647 #define PIN_TO_PWM(p) (p)
648 #define PIN_TO_SERVO(p) (p)
651 // Pic32 chipKIT uC32
652 #elif defined(_BOARD_UC32_)
653 #define TOTAL_ANALOG_PINS NUM_ANALOG_PINS // 12
654 #define TOTAL_PINS NUM_DIGITAL_PINS // 47 All pins can be digital
655 #define MAX_SERVOS NUM_DIGITAL_PINS // All pins can be servo with SoftPWMservo
656 #define VERSION_BLINK_PIN PIN_LED1
657 #define IS_PIN_DIGITAL(p) ((p) >= 2)
658 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 25)
659 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
660 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
661 #define IS_PIN_I2C(p) ((p) == 45 || (p) == 46)
662 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
663 #define PIN_TO_DIGITAL(p) (p)
664 #define PIN_TO_ANALOG(p) ((p) - 14)
665 #define PIN_TO_PWM(p) (p)
666 #define PIN_TO_SERVO(p) (p)
669 // Pic32 chipKIT WF32
670 #elif defined(_BOARD_WF32_)
671 #define TOTAL_ANALOG_PINS NUM_ANALOG_PINS
672 #define TOTAL_PINS NUM_DIGITAL_PINS
673 #define MAX_SERVOS NUM_DIGITAL_PINS
674 #define VERSION_BLINK_PIN PIN_LED1
675 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 49) // Accounts for SD and WiFi dedicated pins
676 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 25)
677 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
678 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
679 #define IS_PIN_I2C(p) ((p) == 34 || (p) == 35)
680 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
681 #define PIN_TO_DIGITAL(p) (p)
682 #define PIN_TO_ANALOG(p) ((p) - 14)
683 #define PIN_TO_PWM(p) (p)
684 #define PIN_TO_SERVO(p) (p)
687 // Pic32 chipKIT WiFire
688 #elif defined(_BOARD_WIFIRE_)
689 #define TOTAL_ANALOG_PINS NUM_ANALOG_PINS // 14
690 #define TOTAL_PINS NUM_DIGITAL_PINS // 71
691 #define MAX_SERVOS NUM_DIGITAL_PINS
692 #define VERSION_BLINK_PIN PIN_LED1
693 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) <= 47) // Accounts for SD and WiFi dedicated pins
694 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 25)
695 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
696 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
697 #define IS_PIN_I2C(p) ((p) == 34 || (p) == 35)
698 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
699 #define PIN_TO_DIGITAL(p) (p)
700 #define PIN_TO_ANALOG(p) ((p) <= 25 ? ((p) - 14) : (p) - 36)
701 #define PIN_TO_PWM(p) (p)
702 #define PIN_TO_SERVO(p) (p)
705 // Pic32 chipKIT MAX32
706 #elif defined(_BOARD_MEGA_) && defined(__PIC32) // NOTE: no _BOARD_MAX32_ to use
707 #define TOTAL_ANALOG_PINS NUM_ANALOG_PINS // 16
708 #define TOTAL_PINS NUM_DIGITAL_PINS // 87
709 #define MAX_SERVOS NUM_DIGITAL_PINS
710 #define VERSION_BLINK_PIN PIN_LED1
711 #define IS_PIN_DIGITAL(p) ((p) >= 2)
712 #define IS_PIN_ANALOG(p) ((p) >= 54 && (p) <= 69)
713 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
714 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
715 #define IS_PIN_I2C(p) ((p) == 34 || (p) == 35)
716 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
717 #define PIN_TO_DIGITAL(p) (p)
718 #define PIN_TO_ANALOG(p) ((p) - 54)
719 #define PIN_TO_PWM(p) (p)
720 #define PIN_TO_SERVO(p) (p)
724 #elif defined(_BOARD_CHIPKIT_PI_)
725 #define TOTAL_ANALOG_PINS 16
726 #define TOTAL_PINS NUM_DIGITAL_PINS // 19
727 #define MAX_SERVOS NUM_DIGITAL_PINS
728 #define VERSION_BLINK_PIN PIN_LED1
729 #define IS_PIN_DIGITAL(p) (((p) >= 2) && ((p) <= 3) || (((p) >= 8) && ((p) <= 13)) || (((p) >= 14) && ((p) <= 17)))
730 #define IS_PIN_ANALOG(p) ((p) >= 14 && (p) <= 17)
731 #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p)
732 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
733 #define IS_PIN_I2C(p) ((p) == 16 || (p) == 17)
734 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
735 #define PIN_TO_DIGITAL(p) (p)
736 #define PIN_TO_ANALOG(p) ((p) <= 15 ? (p) - 14 : (p) - 12)
737 //#define PIN_TO_ANALOG(p) (((p) <= 16) ? ((p) - 14) : ((p) - 16))
738 #define PIN_TO_PWM(p) (p)
739 #define PIN_TO_SERVO(p) (p)
742 // Note: digital pins 9-16 are usable but not labeled on the board numerically.
743 // SS=9, MOSI=10, MISO=11, SCK=12, RX1=13, TX1=14, SCL=15, SDA=16
744 #elif defined(ARDUINO_PINOCCIO)
745 #define TOTAL_ANALOG_PINS 8
746 #define TOTAL_PINS NUM_DIGITAL_PINS // 32
747 #define VERSION_BLINK_PIN 23
748 #define PIN_SERIAL1_RX 13
749 #define PIN_SERIAL1_TX 14
750 #define IS_PIN_DIGITAL(p) (((p) >= 2) && ((p) <= 16)) || (((p) >= 24) && ((p) <= 31))
751 #define IS_PIN_ANALOG(p) ((p) >= 24 && (p) <= 31)
752 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
753 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
754 #define IS_PIN_I2C(p) ((p) == SCL || (p) == SDA)
755 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
756 #define IS_PIN_SERIAL(p) ((p) == 13 || (p) == 14)
757 #define PIN_TO_DIGITAL(p) (p)
758 #define PIN_TO_ANALOG(p) ((p) - 24)
759 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
760 #define PIN_TO_SERVO(p) ((p) - 2)
763 // note: boot mode GPIOs 0, 2 and 15 can be used as outputs, GPIOs 6-11 are in use for flash IO
764 #elif defined(ESP8266)
765 #define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS
766 #define TOTAL_PINS A0 + NUM_ANALOG_INPUTS
767 #define PIN_SERIAL_RX 3
768 #define PIN_SERIAL_TX 1
769 #define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 5) || ((p) >= 12 && (p) < A0))
770 #define IS_PIN_ANALOG(p) ((p) >= A0 && (p) < A0 + NUM_ANALOG_INPUTS)
771 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
772 #define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS)
773 #define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL)
774 #define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
775 #define IS_PIN_INTERRUPT(p) (digitalPinToInterrupt(p) > NOT_AN_INTERRUPT)
776 #define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL_RX || (p) == PIN_SERIAL_TX)
777 #define PIN_TO_DIGITAL(p) (p)
778 #define PIN_TO_ANALOG(p) ((p) - A0)
779 #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p)
780 #define PIN_TO_SERVO(p) (p)
781 #define DEFAULT_PWM_RESOLUTION 10
783 // STM32 based boards
784 #elif defined(ARDUINO_ARCH_STM32)
785 #define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS
786 #define TOTAL_PINS NUM_DIGITAL_PINS
787 #define TOTAL_PORTS MAX_NB_PORT
788 #define VERSION_BLINK_PIN LED_BUILTIN
789 // PIN_SERIALY_RX/TX defined in the variant.h
790 #define IS_PIN_DIGITAL(p) (digitalPinIsValid(p) && !pinIsSerial(p))
791 #define IS_PIN_ANALOG(p) ((p >= A0) && (p < AEND) && !pinIsSerial(p))
792 #define IS_PIN_PWM(p) (IS_PIN_DIGITAL(p) && digitalPinHasPWM(p))
793 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
794 #define IS_PIN_I2C(p) (IS_PIN_DIGITAL(p) && digitalPinHasI2C(p))
795 #define IS_PIN_SPI(p) (IS_PIN_DIGITAL(p) && digitalPinHasSPI(p))
796 #define IS_PIN_INTERRUPT(p) (IS_PIN_DIGITAL(p) && (digitalPinToInterrupt(p) > NOT_AN_INTERRUPT)))
797 #define IS_PIN_SERIAL(p) (digitalPinHasSerial(p) && !pinIsSerial(p))
798 #define PIN_TO_DIGITAL(p) (p)
799 #define PIN_TO_ANALOG(p) (p-A0)
800 #define PIN_TO_PWM(p) (p)
801 #define PIN_TO_SERVO(p) (p)
802 #define DEFAULT_PWM_RESOLUTION PWM_RESOLUTION
804 // Adafruit Bluefruit nRF52 boards
805 #elif defined(ARDUINO_NRF52_ADAFRUIT)
806 #define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS
807 #define TOTAL_PINS 32
808 #define VERSION_BLINK_PIN LED_BUILTIN
809 #define IS_PIN_DIGITAL(p) ((p) >= 2 && (p) < TOTAL_PINS)
810 #define IS_PIN_ANALOG(p) ((p) == PIN_A0 || (p) == PIN_A1 || (p) == PIN_A2 || (p) == PIN_A3 || \
811 (p) == PIN_A4 || (p) == PIN_A5 || (p) == PIN_A6 || (p) == PIN_A7)
812 #define IS_PIN_PWM(p) digitalPinHasPWM(p)
813 #define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
814 #define IS_PIN_I2C(p) ((p) == PIN_WIRE_SDA || (p) == PIN_WIRE_SCL)
815 #define IS_PIN_SPI(p) ((p) == SS || (p)== MOSI || (p) == MISO || (p == SCK))
816 #define PIN_TO_DIGITAL(p) (p)
817 #define PIN_TO_ANALOG(p) ( ((p) == PIN_A0) ? 0 : ((p) == PIN_A1) ? 1 : ((p) == PIN_A2) ? 2 : ((p) == PIN_A3) ? 3 : \
818 ((p) == PIN_A4) ? 4 : ((p) == PIN_A5) ? 5 : ((p) == PIN_A6) ? 6 : ((p) == PIN_A7) ? 7 : (127))
819 #define PIN_TO_PWM(p) (p)
820 #define PIN_TO_SERVO(p) (p)
824 #error "Please edit Boards.h with a hardware abstraction for this board"
827 // as long this is not defined for all boards:
829 #define IS_PIN_SPI(p) 0
832 #ifndef IS_PIN_SERIAL
833 #define IS_PIN_SERIAL(p) 0
836 #ifndef DEFAULT_PWM_RESOLUTION
837 #define DEFAULT_PWM_RESOLUTION 8
840 /*==============================================================================
841 * readPort() - Read an 8 bit port
842 *============================================================================*/
844 static inline unsigned char readPort(byte, byte) __attribute__((always_inline, unused));
845 static inline unsigned char readPort(byte port, byte bitmask)
847 #if defined(ARDUINO_PINOUT_OPTIMIZE)
848 if (port == 0) return (PIND & 0xFC) & bitmask; // ignore Rx/Tx 0/1
849 if (port == 1) return ((PINB & 0x3F) | ((PINC & 0x03) << 6)) & bitmask;
850 if (port == 2) return ((PINC & 0x3C) >> 2) & bitmask;
853 unsigned char out = 0, pin = port * 8;
854 if (IS_PIN_DIGITAL(pin + 0) && (bitmask & 0x01) && digitalRead(PIN_TO_DIGITAL(pin + 0))) out |= 0x01;
855 if (IS_PIN_DIGITAL(pin + 1) && (bitmask & 0x02) && digitalRead(PIN_TO_DIGITAL(pin + 1))) out |= 0x02;
856 if (IS_PIN_DIGITAL(pin + 2) && (bitmask & 0x04) && digitalRead(PIN_TO_DIGITAL(pin + 2))) out |= 0x04;
857 if (IS_PIN_DIGITAL(pin + 3) && (bitmask & 0x08) && digitalRead(PIN_TO_DIGITAL(pin + 3))) out |= 0x08;
858 if (IS_PIN_DIGITAL(pin + 4) && (bitmask & 0x10) && digitalRead(PIN_TO_DIGITAL(pin + 4))) out |= 0x10;
859 if (IS_PIN_DIGITAL(pin + 5) && (bitmask & 0x20) && digitalRead(PIN_TO_DIGITAL(pin + 5))) out |= 0x20;
860 if (IS_PIN_DIGITAL(pin + 6) && (bitmask & 0x40) && digitalRead(PIN_TO_DIGITAL(pin + 6))) out |= 0x40;
861 if (IS_PIN_DIGITAL(pin + 7) && (bitmask & 0x80) && digitalRead(PIN_TO_DIGITAL(pin + 7))) out |= 0x80;
866 /*==============================================================================
867 * writePort() - Write an 8 bit port, only touch pins specified by a bitmask
868 *============================================================================*/
870 static inline unsigned char writePort(byte, byte, byte) __attribute__((always_inline, unused));
871 static inline unsigned char writePort(byte port, byte value, byte bitmask)
873 #if defined(ARDUINO_PINOUT_OPTIMIZE)
875 bitmask = bitmask & 0xFC; // do not touch Tx & Rx pins
876 byte valD = value & bitmask;
877 byte maskD = ~bitmask;
879 PORTD = (PORTD & maskD) | valD;
881 } else if (port == 1) {
882 byte valB = (value & bitmask) & 0x3F;
883 byte valC = (value & bitmask) >> 6;
884 byte maskB = ~(bitmask & 0x3F);
885 byte maskC = ~((bitmask & 0xC0) >> 6);
887 PORTB = (PORTB & maskB) | valB;
888 PORTC = (PORTC & maskC) | valC;
890 } else if (port == 2) {
891 bitmask = bitmask & 0x0F;
892 byte valC = (value & bitmask) << 2;
893 byte maskC = ~(bitmask << 2);
895 PORTC = (PORTC & maskC) | valC;
901 if ((bitmask & 0x01)) digitalWrite(PIN_TO_DIGITAL(pin + 0), (value & 0x01));
902 if ((bitmask & 0x02)) digitalWrite(PIN_TO_DIGITAL(pin + 1), (value & 0x02));
903 if ((bitmask & 0x04)) digitalWrite(PIN_TO_DIGITAL(pin + 2), (value & 0x04));
904 if ((bitmask & 0x08)) digitalWrite(PIN_TO_DIGITAL(pin + 3), (value & 0x08));
905 if ((bitmask & 0x10)) digitalWrite(PIN_TO_DIGITAL(pin + 4), (value & 0x10));
906 if ((bitmask & 0x20)) digitalWrite(PIN_TO_DIGITAL(pin + 5), (value & 0x20));
907 if ((bitmask & 0x40)) digitalWrite(PIN_TO_DIGITAL(pin + 6), (value & 0x40));
908 if ((bitmask & 0x80)) digitalWrite(PIN_TO_DIGITAL(pin + 7), (value & 0x80));
917 #define TOTAL_PORTS ((TOTAL_PINS + 7) / 8)
921 #endif /* Firmata_Boards_h */