Skip to content

Commit 92eeacb

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 <frederic.pillon@st.com>
1 parent 113f9e6 commit 92eeacb

File tree

26 files changed

+322
-423
lines changed

26 files changed

+322
-423
lines changed

cores/arduino/Arduino.h

Lines changed: 1 addition & 1 deletion
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

cores/arduino/pins_arduino.c

Lines changed: 0 additions & 1 deletion
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" {

cores/arduino/pins_arduino.h

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

0 commit comments

Comments
 (0)