|
17 | 17 | */ |
18 | 18 | #ifndef _PINS_ARDUINO_H_ |
19 | 19 | #define _PINS_ARDUINO_H_ |
| 20 | +#include <stdbool.h> |
20 | 21 | #include <stdlib.h> /* Required for static_assert */ |
21 | 22 | // Include board variant |
22 | 23 | #include "variant.h" |
@@ -51,23 +52,33 @@ enum { |
51 | 52 | }; |
52 | 53 |
|
53 | 54 | // Arduino analog pins |
| 55 | + |
54 | 56 | #ifndef NUM_ANALOG_INPUTS |
55 | 57 | #define NUM_ANALOG_INPUTS 0 |
56 | 58 | #endif |
| 59 | + |
| 60 | +// If NUM_ANALOG_FIRST is not defined: |
| 61 | +// - Ax are not contiguous in the digitalPin array |
| 62 | +// - analogInPin array is available |
57 | 63 | #ifndef NUM_ANALOG_FIRST |
58 | | -#define NUM_ANALOG_FIRST NUM_DIGITAL_PINS |
| 64 | +#define NUM_ANALOG_FIRST (NUM_DIGITAL_PINS + 1) |
| 65 | +#define NUM_ANALOG_LAST (NUM_DIGITAL_PINS + NUM_ANALOG_INPUTS) |
| 66 | +#define NUM_ANALOG_INTERNAL_FIRST (NUM_ANALOG_LAST + 1) |
| 67 | +#else |
| 68 | +#define NUM_ANALOG_INTERNAL_FIRST (NUM_DIGITAL_PINS + 1) |
59 | 69 | #endif |
60 | 70 |
|
| 71 | +// If NUM_ANALOG_INPUTS is not defined there is no analog pins defined. |
| 72 | +// Anyway ADC internal channels are always avaialable. |
| 73 | +#if NUM_ANALOG_INPUTS > 0 |
61 | 74 | // Analog pins must be contiguous to be able to loop on each value |
62 | 75 | #define MAX_ANALOG_INPUTS 24 |
63 | 76 | _Static_assert(NUM_ANALOG_INPUTS <= MAX_ANALOG_INPUTS, |
64 | 77 | "Core NUM_ANALOG_INPUTS limited to MAX_ANALOG_INPUTS"); |
| 78 | + |
65 | 79 | _Static_assert(NUM_ANALOG_FIRST >= NUM_ANALOG_INPUTS, |
66 | 80 | "First analog pin value (A0) must be greater than or equal to NUM_ANALOG_INPUTS"); |
67 | 81 |
|
68 | | -// Defined for backward compatibility with Firmata which unfortunately use it |
69 | | -#define AEND (NUM_ANALOG_FIRST+NUM_ANALOG_INPUTS) |
70 | | - |
71 | 82 | #if NUM_ANALOG_INPUTS > 0 |
72 | 83 | #define PIN_A0 NUM_ANALOG_FIRST |
73 | 84 | static const uint8_t A0 = PIN_A0; |
@@ -164,6 +175,7 @@ static const uint8_t A22 = PIN_A22; |
164 | 175 | #define PIN_A23 (PIN_A22 + 1) |
165 | 176 | static const uint8_t A23 = PIN_A23; |
166 | 177 | #endif |
| 178 | +#endif /* NUM_ANALOG_INPUTS > 0 */ |
167 | 179 |
|
168 | 180 | // Default for Arduino connector compatibility |
169 | 181 | // SPI Definitions |
@@ -211,38 +223,56 @@ static const uint8_t SCL = PIN_WIRE_SCL; |
211 | 223 | // ADC internal channels (not a pins) |
212 | 224 | // Only used for analogRead() |
213 | 225 | #if defined(ADC_CHANNEL_TEMPSENSOR) || defined(ADC_CHANNEL_TEMPSENSOR_ADC1) |
214 | | -#define ATEMP (NUM_DIGITAL_PINS + 1) |
| 226 | +#define ATEMP (NUM_ANALOG_INTERNAL_FIRST) |
215 | 227 | #endif |
216 | 228 | #ifdef ADC_CHANNEL_VREFINT |
217 | | -#define AVREF (NUM_DIGITAL_PINS + 2) |
| 229 | +#define AVREF (NUM_ANALOG_INTERNAL_FIRST + 2) |
218 | 230 | #endif |
219 | 231 | #ifdef ADC_CHANNEL_VBAT |
220 | | -#define AVBAT (NUM_DIGITAL_PINS + 3) |
| 232 | +#define AVBAT (NUM_ANALOG_INTERNAL_FIRST + 3) |
221 | 233 | #endif |
222 | 234 | #if defined(ADC5) && defined(ADC_CHANNEL_TEMPSENSOR_ADC5) |
223 | | -#define ATEMP_ADC5 (NUM_DIGITAL_PINS + 4) |
| 235 | +#define ATEMP_ADC5 (NUM_ANALOG_INTERNAL_FIRST + 4) |
224 | 236 | #endif |
225 | 237 |
|
226 | 238 | #ifdef __cplusplus |
227 | 239 | extern "C" { |
228 | 240 | #endif |
229 | 241 | extern const PinName digitalPin[]; |
| 242 | +extern const uint32_t analogInPin[]; |
230 | 243 |
|
231 | 244 | #define NOT_AN_INTERRUPT NC // -1 |
232 | 245 |
|
233 | 246 | // Convert a digital pin number Dxx to a PinName PX_n |
234 | 247 | // Note: Analog pin is also a digital pin. |
| 248 | +#ifndef NUM_ANALOG_LAST |
235 | 249 | #define digitalPinToPinName(p) (((uint32_t)p < NUM_DIGITAL_PINS) ? digitalPin[p] : NC) |
| 250 | +#else |
| 251 | +#define digitalPinToPinName(p) (((uint32_t)p < NUM_DIGITAL_PINS) ? digitalPin[p] : \ |
| 252 | + ((uint32_t)p >= NUM_ANALOG_FIRST) && ((uint32_t)p <= NUM_ANALOG_LAST) ? \ |
| 253 | + digitalPin[analogInPin[p-NUM_ANALOG_FIRST]] : NC) |
| 254 | +#endif |
236 | 255 | // Convert a PinName PX_n to a digital pin number |
237 | 256 | uint32_t pinNametoDigitalPin(PinName p); |
238 | 257 |
|
239 | 258 | // Convert an analog pin number to a digital pin number |
240 | | -#if defined(NUM_ANALOG_INPUTS) && (NUM_ANALOG_INPUTS>0) |
| 259 | +#if NUM_ANALOG_INPUTS > 0 |
241 | 260 | // Used by analogRead api to have A0 == 0 |
| 261 | +// For contiguous analog pins definition in digitalPin array |
| 262 | +#ifndef NUM_ANALOG_LAST |
242 | 263 | #define analogInputToDigitalPin(p) (((uint32_t)p < NUM_ANALOG_INPUTS) ? (p+A0) : p) |
243 | 264 | #else |
| 265 | +// For non contiguous analog pins definition in digitalPin array |
| 266 | +#define analogInputToDigitalPin(p) ( \ |
| 267 | + ((uint32_t)p < NUM_ANALOG_INPUTS) ? analogInPin[p] : \ |
| 268 | + ((uint32_t)p >= NUM_ANALOG_FIRST) && ((uint32_t)p <= NUM_ANALOG_LAST) ? \ |
| 269 | + analogInPin[p-NUM_ANALOG_FIRST] : p) |
| 270 | +#endif // !NUM_ANALOG_LAST |
| 271 | +#else |
| 272 | +// No analog pin defined |
244 | 273 | #define analogInputToDigitalPin(p) (NUM_DIGITAL_PINS) |
245 | | -#endif |
| 274 | +#endif // NUM_ANALOG_INPUTS > 0 |
| 275 | + |
246 | 276 | // Convert an analog pin number Axx to a PinName PX_n |
247 | 277 | PinName analogInputToPinName(uint32_t pin); |
248 | 278 |
|
@@ -294,12 +324,15 @@ PinName analogInputToPinName(uint32_t pin); |
294 | 324 | // return first occurence of linked PinName (PYx) |
295 | 325 | #define digitalPinFirstOccurence(p) (pinNametoDigitalPin(digitalPinToPinName(p))) |
296 | 326 |
|
297 | | -// Specific for Firmata. As some pins could be duplicated, |
298 | | -// ensure 'p' is not one of the serial pins |
| 327 | +// Specific for Firmata. |
| 328 | +// Some pins could be duplicated, ensure 'p' is not one of the serial pins |
299 | 329 | #if defined(PIN_SERIAL_RX) && defined(PIN_SERIAL_TX) |
300 | 330 | #define pinIsSerial(p) ((digitalPinFirstOccurence(p) == PIN_SERIAL_RX) ||\ |
301 | 331 | (digitalPinFirstOccurence(p) == PIN_SERIAL_TX)) |
302 | 332 | #endif |
| 333 | +// Convenient macro to handle Analog |
| 334 | +bool pinIsAnalogInput(uint32_t pin); |
| 335 | +uint32_t digitalPinToAnalogInput(uint32_t pin); |
303 | 336 |
|
304 | 337 | #ifdef __cplusplus |
305 | 338 | } |
|
0 commit comments