Skip to content

Commit c4c9f48

Browse files
committed
[Arduino compatibility] Ax pin definition
Ax definition is now inline with Arduino style Variant has only to define the digital pin number of the first analog input (i.e. which digital pin is A0) Clean variant header include Fix #140 Signed-off-by: Frederic Pillon <[email protected]>
1 parent 113f9e6 commit c4c9f48

File tree

26 files changed

+337
-435
lines changed

26 files changed

+337
-435
lines changed

Diff for: cores/arduino/Arduino.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ void yield(void);
4545
#endif // __cplusplus
4646

4747
// Include pins variant
48-
#include "pins_arduino_var.h"
48+
#include "pins_arduino.h"
4949

5050
#endif // Arduino_h

Diff for: cores/arduino/pins_arduino.c

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818

1919
#include "pins_arduino.h"
20-
#include "pins_arduino_var.h"
2120

2221
#ifdef __cplusplus
2322
extern "C" {

Diff for: cores/arduino/pins_arduino.h

+230-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
*/
1818
#ifndef _PINS_ARDUINO_H_
1919
#define _PINS_ARDUINO_H_
20+
// Include board variant
21+
#include "variant.h"
2022

21-
#include "PeripheralPins.h"
23+
// Avoid pins number misalignment
24+
_Static_assert(NUM_DIGITAL_PINS==PEND, "NUM_DIGITAL_PINS and PEND differ!");
2225

23-
// Arduino digital pin alias
26+
// Arduino digital pins alias
2427
// GPIO port (A to K) * 16 pins: 176
2528
enum {
2629
D0, D1, D2, D3, D4, D5, D6, D7, D8, D9,
@@ -44,4 +47,229 @@ enum {
4447
DMAX
4548
};
4649

50+
// Arduino analog pins
51+
// Analog pins must be contiguous to be able to loop on each value
52+
#define MAX_ANALOG_INPUTS 20
53+
_Static_assert(NUM_ANALOG_INPUTS <= MAX_ANALOG_INPUTS, "Core NUM_ANALOG_INPUTS limited to MAX_ANALOG_INPUTS" );
54+
55+
// Defined for backward compatibility with Firmata which unfortunately use it
56+
#define AEND (NUM_ANALOG_FIRST+NUM_ANALOG_INPUTS)
57+
58+
#if NUM_ANALOG_INPUTS > 0
59+
#define PIN_A0 NUM_ANALOG_FIRST
60+
static const uint8_t A0 = PIN_A0;
61+
#endif
62+
#if NUM_ANALOG_INPUTS > 1
63+
#define PIN_A1 (PIN_A0 + 1)
64+
static const uint8_t A1 = PIN_A1;
65+
#endif
66+
#if NUM_ANALOG_INPUTS > 2
67+
#define PIN_A2 (PIN_A1 + 1)
68+
static const uint8_t A2 = PIN_A2;
69+
#endif
70+
#if NUM_ANALOG_INPUTS > 3
71+
#define PIN_A3 (PIN_A2 + 1)
72+
static const uint8_t A3 = PIN_A3;
73+
#endif
74+
#if NUM_ANALOG_INPUTS > 4
75+
#define PIN_A4 (PIN_A3 + 1)
76+
static const uint8_t A4 = PIN_A4;
77+
#endif
78+
#if NUM_ANALOG_INPUTS > 5
79+
#define PIN_A5 (PIN_A4 + 1)
80+
static const uint8_t A5 = PIN_A5;
81+
#endif
82+
#if NUM_ANALOG_INPUTS > 6
83+
#define PIN_A6 (PIN_A5 + 1)
84+
static const uint8_t A6 = PIN_A6;
85+
#endif
86+
#if NUM_ANALOG_INPUTS > 7
87+
#define PIN_A7 (PIN_A6 + 1)
88+
static const uint8_t A7 = PIN_A7;
89+
#endif
90+
#if NUM_ANALOG_INPUTS > 8
91+
#define PIN_A8 (PIN_A7 + 1)
92+
static const uint8_t A8 = PIN_A8;
93+
#endif
94+
#if NUM_ANALOG_INPUTS > 9
95+
#define PIN_A9 (PIN_A8 + 1)
96+
static const uint8_t A9 = PIN_A9;
97+
#endif
98+
#if NUM_ANALOG_INPUTS > 10
99+
#define PIN_A10 (PIN_A9 + 1)
100+
static const uint8_t A10 = PIN_A10;
101+
#endif
102+
#if NUM_ANALOG_INPUTS > 11
103+
#define PIN_A11 (PIN_A10 + 1)
104+
static const uint8_t A11 = PIN_A11;
105+
#endif
106+
#if NUM_ANALOG_INPUTS > 12
107+
#define PIN_A12 (PIN_A11 + 1)
108+
static const uint8_t A12 = PIN_A12;
109+
#endif
110+
#if NUM_ANALOG_INPUTS > 13
111+
#define PIN_A13 (PIN_A12 + 1)
112+
static const uint8_t A13 = PIN_A13;
113+
#endif
114+
#if NUM_ANALOG_INPUTS > 14
115+
#define PIN_A14 (PIN_A13 + 1)
116+
static const uint8_t A14 = PIN_A14;
117+
#endif
118+
#if NUM_ANALOG_INPUTS > 15
119+
#define PIN_A15 (PIN_A14 + 1)
120+
static const uint8_t A15 = PIN_A15;
121+
#endif
122+
#if NUM_ANALOG_INPUTS > 16
123+
#define PIN_A16 (PIN_A15 + 1)
124+
static const uint8_t A16 = PIN_A16;
125+
#endif
126+
#if NUM_ANALOG_INPUTS > 17
127+
#define PIN_A17 (PIN_A16 + 1)
128+
static const uint8_t A17 = PIN_A17;
129+
#endif
130+
#if NUM_ANALOG_INPUTS > 18
131+
#define PIN_A18 (PIN_A17 + 1)
132+
static const uint8_t A18 = PIN_A18;
133+
#endif
134+
#if NUM_ANALOG_INPUTS > 19
135+
#define PIN_A19 (PIN_A18 + 1)
136+
static const uint8_t A19 = PIN_A19;
137+
#endif
138+
139+
// Default for Arduino connector compatibility
140+
// SPI Definitions
141+
#ifndef PIN_SPI_SS
142+
#define PIN_SPI_SS 10
143+
#endif
144+
#ifndef PIN_SPI_SS1
145+
#define PIN_SPI_SS1 4
146+
#endif
147+
#ifndef PIN_SPI_SS2
148+
#define PIN_SPI_SS2 7
149+
#endif
150+
#ifndef PIN_SPI_SS3
151+
#define PIN_SPI_SS3 8
152+
#endif
153+
#ifndef PIN_SPI_MOSI
154+
#define PIN_SPI_MOSI 11
155+
#endif
156+
#ifndef PIN_SPI_MISO
157+
#define PIN_SPI_MISO 12
158+
#endif
159+
#ifndef PIN_SPI_SCK
160+
#define PIN_SPI_SCK 13
161+
#endif
162+
163+
static const uint8_t SS = PIN_SPI_SS;
164+
static const uint8_t SS1 = PIN_SPI_SS1;
165+
static const uint8_t SS2 = PIN_SPI_SS2;
166+
static const uint8_t SS3 = PIN_SPI_SS3;
167+
static const uint8_t MOSI = PIN_SPI_MOSI;
168+
static const uint8_t MISO = PIN_SPI_MISO;
169+
static const uint8_t SCK = PIN_SPI_SCK;
170+
171+
// I2C Definitions
172+
#ifndef PIN_WIRE_SDA
173+
#define PIN_WIRE_SDA 14
174+
#endif
175+
#ifndef PIN_WIRE_SCL
176+
#define PIN_WIRE_SCL 15
177+
#endif
178+
179+
static const uint8_t SDA = PIN_WIRE_SDA;
180+
static const uint8_t SCL = PIN_WIRE_SCL;
181+
182+
#ifdef __cplusplus
183+
extern "C" {
184+
#endif
185+
186+
#define NOT_AN_INTERRUPT NC // -1
187+
188+
// Convert a digital pin number Dxx to a PinName PX_n
189+
// Note: Analog pin is also a digital pin.
190+
#define digitalPinToPinName(p) (((uint32_t)p < NUM_DIGITAL_PINS) ? digitalPin[p] : NC)
191+
// Convert a PinName PX_n to a digital pin number
192+
uint32_t pinNametoDigitalPin(PinName p);
193+
194+
// Convert an analog pin number to a digital pin number
195+
// Used by analogRead api to have A0 == 0
196+
#define analogInputToDigitalPin(p) (((uint32_t)p < NUM_ANALOG_INPUTS) ? (p+A0) : p)
197+
// Convert an analog pin number Axx to a PinName PX_n
198+
#define analogInputToPinName(p) (digitalPinToPinName(analogInputToDigitalPin(p)))
199+
// All pins could manage EXTI
200+
#define digitalPinToInterrupt(p) (digitalPinIsValid(p) ? p : NOT_AN_INTERRUPT)
201+
202+
#define digitalPinHasI2C(p) (pin_in_pinmap(digitalPinToPinName(p), PinMap_I2C_SDA) ||\
203+
pin_in_pinmap(digitalPinToPinName(p), PinMap_I2C_SCL))
204+
#define digitalPinHasPWM(p) (pin_in_pinmap(digitalPinToPinName(p), PinMap_PWM))
205+
#define digitalPinHasSerial(p) (pin_in_pinmap(digitalPinToPinName(p), PinMap_UART_RX) ||\
206+
pin_in_pinmap(digitalPinToPinName(p), PinMap_UART_TX))
207+
#define digitalPinHasSPI(p) (pin_in_pinmap(digitalPinToPinName(p), PinMap_SPI_MOSI) ||\
208+
pin_in_pinmap(digitalPinToPinName(p), PinMap_SPI_MISO) ||\
209+
pin_in_pinmap(digitalPinToPinName(p), PinMap_SPI_SCLK) ||\
210+
pin_in_pinmap(digitalPinToPinName(p), PinMap_SPI_SSEL))
211+
212+
213+
#define digitalPinToPort(p) (get_GPIO_Port(STM_PORT(digitalPinToPinName(p))))
214+
#define digitalPinToBitMask(p) (STM_GPIO_PIN(digitalPinToPinName(p)))
215+
216+
#define analogInPinToBit(p) (STM_PIN(digitalPinToPinName(p)))
217+
#define portOutputRegister(P) (&(P->ODR))
218+
#define portInputRegister(P) (&(P->IDR))
219+
220+
#define portSetRegister(P) (&(P->BSRR))
221+
#if defined(STM32F2xx) || defined(STM32F4xx) || defined(STM32F7xx)
222+
// For those series reset are in the high part so << 16U needed
223+
#define portClearRegister(P) (&(P->BSRR))
224+
#else
225+
#define portClearRegister(P) (&(P->BRR))
226+
#endif
227+
228+
229+
#if defined(STM32F1xx)
230+
// Config registers split in 2 registers:
231+
// CRL: pin 0..7
232+
// CRH: pin 8..15
233+
// Return only CRL
234+
#define portModeRegister(P) (&(P->CRL))
235+
#else
236+
#define portModeRegister(P) (&(P->MODER))
237+
#endif
238+
#define portConfigRegister(P) (portModeRegister(P))
239+
240+
241+
#define digitalPinIsValid(p) (digitalPinToPinName(p) != NC)
242+
243+
// As some pin could be duplicated in digitalPin[]
244+
// return first occurence of linked PinName (PYx)
245+
#define digitalPinFirstOccurence(p) (pinNametoDigitalPin(digitalPinToPinName(p)))
246+
247+
// Specific for Firmata. As some pins could be duplicated,
248+
// ensure 'p' is not one of the serial pins
249+
#if defined(PIN_SERIAL_RX) && defined(PIN_SERIAL_TX)
250+
#define pinIsSerial(p) ((digitalPinFirstOccurence(p) == PIN_SERIAL_RX) ||\
251+
(digitalPinFirstOccurence(p) == PIN_SERIAL_TX))
252+
#endif
253+
254+
#ifdef __cplusplus
255+
}
256+
#endif
257+
258+
// Default Definitions, could be redefined in variant.h
259+
#ifndef ADC_RESOLUTION
260+
#define ADC_RESOLUTION 12
261+
#endif
262+
#ifndef DACC_RESOLUTION
263+
#define DACC_RESOLUTION 12
264+
#endif
265+
#ifndef PWM_RESOLUTION
266+
#define PWM_RESOLUTION 8
267+
#endif
268+
#ifndef PWM_FREQUENCY
269+
#define PWM_FREQUENCY 1000
270+
#endif
271+
#ifndef PWM_MAX_DUTY_CYCLE
272+
#define PWM_MAX_DUTY_CYCLE 255
273+
#endif
274+
47275
#endif /*_PINS_ARDUINO_H_*/

0 commit comments

Comments
 (0)