Skip to content
This repository was archived by the owner on Feb 21, 2020. It is now read-only.

Commit d425a28

Browse files
fpistmalfran
authored andcommitted
Review analog pins numbering and pin functions
analogRead(A0) == analogRead(0) analogWrite(A0) != analogWrite(0) == analogWrite(D0) All pins available in PinMap_ADC are available with Ax alias Move pin functions to pins_arduino.c to avoid duplicated code. Pin functions naming alignement. Move init() function to pins_arduino.c to avoid duplicated code and define it as weak. Minor clean of unused code. Remove usb flags for variants w/o usb. Serialx under switch.Disable by default Now, variant files only contain source which differ for each variants. Signed-off-by: Frederic Pillon <[email protected]>
1 parent 8807b99 commit d425a28

File tree

11 files changed

+89
-70
lines changed

11 files changed

+89
-70
lines changed

Diff for: cores/arduino/Arduino.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ extern void loop( void ) ;
7373

7474

7575
// Include board variant
76-
#include "variant.h"
76+
#include "pins_arduino.h"
7777

7878
#include "wiring.h"
7979
#include "wiring_digital.h"

Diff for: cores/arduino/Tone.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static stimer_t _timer;
2828

2929
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
3030
{
31-
PinName p = digitalToPinName(_pin);
31+
PinName p = digitalPinToPinName(_pin);
3232
if(p != NC) {
3333
if((g_lastPin == NC) || (g_lastPin == p)) {
3434
_timer.pin = p;
@@ -41,7 +41,7 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
4141

4242
void noTone(uint8_t _pin)
4343
{
44-
PinName p = digitalToPinName(_pin);
44+
PinName p = digitalPinToPinName(_pin);
4545
if(p != NC) {
4646
TimerPinDeinit(&_timer);
4747
digitalWrite(_pin, 0);

Diff for: cores/arduino/WInterrupts.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
void attachInterrupt(uint32_t pin, void (*callback)(void), uint32_t mode)
2626
{
2727
uint32_t it_mode;
28-
PinName p = digitalToPinName(pin);
28+
PinName p = digitalPinToPinName(pin);
2929
GPIO_TypeDef* port = set_GPIO_Port_Clock(STM_PORT(p));
3030
if (port == NC)
3131
return;
@@ -51,7 +51,7 @@ void attachInterrupt(uint32_t pin, void (*callback)(void), uint32_t mode)
5151

5252
void detachInterrupt(uint32_t pin)
5353
{
54-
PinName p = digitalToPinName(pin);
54+
PinName p = digitalPinToPinName(pin);
5555
GPIO_TypeDef* port = get_GPIO_Port(STM_PORT(p));
5656
if (port == NC)
5757
return;

Diff for: cores/arduino/pins_arduino.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright (c) 2011 Arduino. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#include "pins_arduino.h"
20+
21+
void __libc_init_array(void);
22+
23+
WEAK uint32_t pinNametoDigitalPin(PinName p)
24+
{
25+
uint32_t i = 0;
26+
for(i = 0; i < NUM_DIGITAL_PINS; i++) {
27+
if (digitalPin[i] == p)
28+
break;
29+
}
30+
return i;
31+
}
32+
33+
WEAK void init( void )
34+
{
35+
hw_config_init();
36+
}

Diff for: cores/arduino/pins_arduino.h

+30
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,37 @@
1515
License along with this library; if not, write to the Free Software
1616
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1717
*/
18+
#ifndef _PINS_ARDUINO_H_
19+
#define _PINS_ARDUINO_H_
20+
21+
/**
22+
* Libc porting layers
23+
*/
24+
#if defined ( __GNUC__ ) /* GCC CS3 */
25+
# include <syscalls.h> /** RedHat Newlib minimal stub */
26+
#endif
1827

1928
// API compatibility
2029
#include "variant.h"
2130

31+
#define NUM_DIGITAL_PINS DEND
32+
#define NUM_ANALOG_INPUTS (AEND-A0)
33+
34+
// Convert a digital pin number Dxx to a PinName Pxy
35+
// Note: Analog pin is also a digital pin.
36+
#define digitalPinToPinName(p) ((p < NUM_DIGITAL_PINS) ? digitalPin[p] : (STM_VALID_PINNAME(p))? (PinName)p : NC)
37+
// Convert a PinName Pxy to a digital pin number
38+
uint32_t pinNametoDigitalPin(PinName p);
39+
40+
// Convert an analog pin number to a digital pin number
41+
// Used by analogRead api to have A0 == 0
42+
#define analogInputToDigitalPin(p) ((p < NUM_ANALOG_INPUTS) ? (p+A0) : p)
43+
// Convert an analog pin number Axx to a PinName Pxy
44+
#define analogInputToPinName(p) (digitalPinToPinName(analogInputToDigitalPin(p)))
45+
// All pins could manage EXTI
46+
#define digitalPinToInterrupt(p) (p)
47+
48+
#define digitalPinToPort(p) ( get_GPIO_Port(digitalPinToPinName(p)) )
49+
#define digitalPinToBitMask(p) ( STM_GPIO_PIN(digitalPinToPinName(p)) )
50+
51+
#endif /*_PINS_ARDUINO_H_*/

Diff for: cores/arduino/wiring_analog.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static inline uint32_t mapResolution(uint32_t value, uint32_t from, uint32_t to)
5252
uint32_t analogRead(uint32_t ulPin)
5353
{
5454
uint32_t value = 0;
55-
PinName p = analogToPinName(ulPin);
55+
PinName p = analogInputToPinName(ulPin);
5656
if(p != NC) {
5757
value = adc_read_value(p);
5858
value = mapResolution(value, ADC_RESOLUTION, _readResolution);
@@ -71,7 +71,7 @@ void analogOutputInit(void) {
7171
void analogWrite(uint32_t ulPin, uint32_t ulValue) {
7272

7373
uint8_t do_init = 0;
74-
PinName p = analogToPinName(ulPin);
74+
PinName p = digitalPinToPinName(ulPin);
7575
if(p != NC) {
7676
if(pin_in_pinmap(p, PinMap_DAC)) {
7777
if(is_pin_configured(p, g_anOutputPinConfigured) == false) {

Diff for: cores/arduino/wiring_digital.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extern uint32_t g_anOutputPinConfigured[MAX_NB_PORT];
3030

3131
void pinMode( uint32_t ulPin, uint32_t ulMode )
3232
{
33-
PinName p = digitalToPinName(ulPin);
33+
PinName p = digitalPinToPinName(ulPin);
3434

3535
if(p != NC) {
3636
// If the pin that support PWM or DAC output, we need to turn it off
@@ -66,7 +66,7 @@ void pinMode( uint32_t ulPin, uint32_t ulMode )
6666

6767
void digitalWrite( uint32_t ulPin, uint32_t ulVal )
6868
{
69-
PinName p = digitalToPinName(ulPin);
69+
PinName p = digitalPinToPinName(ulPin);
7070
if(p != NC) {
7171
if(is_pin_configured(p, g_digPinConfigured)) {
7272
digital_io_write(get_GPIO_Port(STM_PORT(p)), STM_GPIO_PIN(p), ulVal);
@@ -77,7 +77,7 @@ void digitalWrite( uint32_t ulPin, uint32_t ulVal )
7777
int digitalRead( uint32_t ulPin )
7878
{
7979
uint8_t level = 0;
80-
PinName p = digitalToPinName(ulPin);
80+
PinName p = digitalPinToPinName(ulPin);
8181
if(p != NC) {
8282
if(is_pin_configured(p, g_digPinConfigured)) {
8383
level = digital_io_read(get_GPIO_Port(STM_PORT(p)), STM_GPIO_PIN(p));

Diff for: libraries/SPI/SPI.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
SPIClass::SPIClass() : g_active_id(-1)
2121
{
22-
_spi.pin_miso = digitalToPinName(MISO);
23-
_spi.pin_mosi = digitalToPinName(MOSI);
24-
_spi.pin_sclk = digitalToPinName(SCLK);
22+
_spi.pin_miso = digitalPinToPinName(MISO);
23+
_spi.pin_mosi = digitalPinToPinName(MOSI);
24+
_spi.pin_sclk = digitalPinToPinName(SCLK);
2525
_spi.pin_ssel = NC;
2626
}
2727

@@ -30,12 +30,12 @@ ssel pin. Enable this pin disable software CS. See microcontroller documentation
3030
for the list of available SS pins. */
3131
SPIClass::SPIClass(uint8_t mosi, uint8_t miso, uint8_t sclk, uint8_t ssel) : g_active_id(-1)
3232
{
33-
_spi.pin_miso = digitalToPinName(miso);
34-
_spi.pin_mosi = digitalToPinName(mosi);
35-
_spi.pin_sclk = digitalToPinName(sclk);
33+
_spi.pin_miso = digitalPinToPinName(miso);
34+
_spi.pin_mosi = digitalPinToPinName(mosi);
35+
_spi.pin_sclk = digitalPinToPinName(sclk);
3636

3737
if(ssel != 0xFF) {
38-
_spi.pin_ssel = digitalToPinName(ssel);
38+
_spi.pin_ssel = digitalPinToPinName(ssel);
3939
} else {
4040
_spi.pin_ssel = NC;
4141
}

Diff for: libraries/Wire/Wire.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ void (*TwoWire::user_onReceive)(int);
4545

4646
TwoWire::TwoWire()
4747
{
48-
_i2c.sda = digitalToPinName(SDA);
49-
_i2c.scl = digitalToPinName(SCL);
48+
_i2c.sda = digitalPinToPinName(SDA);
49+
_i2c.scl = digitalPinToPinName(SCL);
5050
}
5151

5252
TwoWire::TwoWire(uint8_t sda, uint8_t scl)
5353
{
54-
_i2c.sda = digitalToPinName(sda);
55-
_i2c.scl = digitalToPinName(scl);
54+
_i2c.sda = digitalPinToPinName(sda);
55+
_i2c.scl = digitalPinToPinName(scl);
5656
}
5757

5858
// Public Methods //////////////////////////////////////////////////////////////

Diff for: variants/otto/variant.cpp

+1-18
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extern "C" {
2323
#endif
2424

2525
// Pin number
26-
const PinName digital_arduino[] = {
26+
const PinName digitalPin[] = {
2727
PA1, //D0
2828
PA0, //D1
2929
PH6, //D2
@@ -145,23 +145,6 @@ void serialEventRun(void)
145145
extern "C" {
146146
#endif
147147

148-
void __libc_init_array(void);
149-
150-
uint32_t pinNametoPinNumber(PinName p)
151-
{
152-
uint32_t i = 0;
153-
for(i = 0; i < NUM_DIGITAL_PINS; i++) {
154-
if (digital_arduino[i] == p)
155-
break;
156-
}
157-
return i;
158-
}
159-
160-
void init( void )
161-
{
162-
hw_config_init();
163-
}
164-
165148
/**
166149
* @brief System Clock Configuration
167150
* @param None

Diff for: variants/otto/variant.h

+1-31
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
#ifndef _VARIANT_ARDUINO_STM32_
2020
#define _VARIANT_ARDUINO_STM32_
2121

22-
/*----------------------------------------------------------------------------
23-
* Definitions
24-
*----------------------------------------------------------------------------*/
25-
2622
/*----------------------------------------------------------------------------
2723
* Headers
2824
*----------------------------------------------------------------------------*/
@@ -33,19 +29,12 @@
3329
extern "C"{
3430
#endif // __cplusplus
3531

36-
/**
37-
* Libc porting layers
38-
*/
39-
#if defined ( __GNUC__ ) /* GCC CS3 */
40-
# include <syscalls.h> /** RedHat Newlib minimal stub */
41-
#endif
42-
4332
/*----------------------------------------------------------------------------
4433
* Pins
4534
*----------------------------------------------------------------------------*/
4635
#include "PeripheralPins.h"
4736

48-
extern const PinName digital_arduino[];
37+
extern const PinName digitalPin[];
4938

5039
enum {
5140
D0, D1, D2, D3, D4, D5, D6, D7, D8, D9,
@@ -67,23 +56,6 @@ enum {
6756
AEND
6857
};
6958

70-
#define MAX_ANALOG_IOS (sizeof(PinMap_ADC)/sizeof(PinMap))
71-
#define MAX_DIGITAL_IOS DEND
72-
#define NUM_DIGITAL_PINS MAX_DIGITAL_IOS
73-
#define NUM_ANALOG_INPUTS (AEND - A0)
74-
75-
// Convert a digital pin number Dxx to a PinName Pxy
76-
#define digitalToPinName(p) ((p < NUM_DIGITAL_PINS) ? digital_arduino[p] : (STM_VALID_PINNAME(p))? (PinName)p : NC)
77-
// Convert an analog pin number Axx to a PinName Pxy
78-
#define analogToPinName(p) (digitalToPinName(p))
79-
// Convert an analog pin number to a digital pin number
80-
#define analogToDigital(p) (p)
81-
// Convert a PinName Pxy to a pin number
82-
uint32_t pinNametoPinNumber(PinName p);
83-
84-
#define digitalPinToPort(p) ( get_GPIO_Port(digitalToPinName(p)) )
85-
#define digitalPinToBitMask(p) ( STM_GPIO_PIN(digitalToPinName(p)) )
86-
8759
//ADC resolution is 12bits
8860
#define ADC_RESOLUTION 12
8961
#define DACC_RESOLUTION 12
@@ -170,8 +142,6 @@ uint32_t pinNametoPinNumber(PinName p);
170142
#define CODEC 0
171143
#define MCU 1
172144

173-
//Enable Firmata
174-
#define STM32 1
175145

176146
#ifdef __cplusplus
177147
} // extern "C"

0 commit comments

Comments
 (0)