From 06f63fbc2dd4c026f4a3a706fd39e500400fa6d4 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Sun, 11 May 2014 17:03:50 +0200 Subject: [PATCH 1/4] Generalize digital to analog pin number translation When a (digital) pin number of an analog pin (e.g "14" or "A0" on an Uno) is passed to analogRead, it was translated to the analog channel to use, by subtracting the pin number of the first analog pin. However, this used a number of hardcoded literals, depending on the CPU type, even though the CPU type isn't actually what would define the pin numbers (the selected board design or variant is). This generalizes the previous code to just subtract the value of A0, which should make it work with all current and future boards. Furthermore, this also improves the analogPinToChannel define, which a variant file can define for a non-linear mapping between analog pin numbers and the actual ADC channels. Previously, if this macro was defined, the pin number mapping described above would only happen for the 32u4 CPU. With this change, the mapping happens always, before applying analogPinToChannel. --- .../arduino/avr/cores/arduino/wiring_analog.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/wiring_analog.c b/hardware/arduino/avr/cores/arduino/wiring_analog.c index 8feead9577c..3360e546901 100644 --- a/hardware/arduino/avr/cores/arduino/wiring_analog.c +++ b/hardware/arduino/avr/cores/arduino/wiring_analog.c @@ -41,19 +41,16 @@ int analogRead(uint8_t pin) { uint8_t low, high; + // If a pin number is passed (e.g. the "digital pin number" for + // an Ax pin), then translate it to the corresponding "analog + // pin number (e.g., A0 => 0, A1 => 1, etc). + #if defined(A0) + if (pin >= A0) + pin -= A0; + #endif + #if defined(analogPinToChannel) -#if defined(__AVR_ATmega32U4__) - if (pin >= 18) pin -= 18; // allow for channel or pin numbers -#endif pin = analogPinToChannel(pin); -#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) - if (pin >= 54) pin -= 54; // allow for channel or pin numbers -#elif defined(__AVR_ATmega32U4__) - if (pin >= 18) pin -= 18; // allow for channel or pin numbers -#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) - if (pin >= 24) pin -= 24; // allow for channel or pin numbers -#else - if (pin >= 14) pin -= 14; // allow for channel or pin numbers #endif #if defined(ADCSRB) && defined(MUX5) From 95e934f8eac3bffd9564c7f97d3b0f20a57d3244 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Sun, 11 May 2014 17:12:47 +0200 Subject: [PATCH 2/4] Let analogRead return 0 on invalid pin numbers Before, when an invalid analog pin number was passed, the lower 4 bits would be used to read the ADC, effectively returning a read from another channel. However, when analogPinToChannel was defined to to a PROGMEM read (such as for the leonardo), that could cause a read from an undefined flash address. Better to just return 0. --- hardware/arduino/avr/cores/arduino/wiring_analog.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/hardware/arduino/avr/cores/arduino/wiring_analog.c b/hardware/arduino/avr/cores/arduino/wiring_analog.c index 3360e546901..32e94f80be6 100644 --- a/hardware/arduino/avr/cores/arduino/wiring_analog.c +++ b/hardware/arduino/avr/cores/arduino/wiring_analog.c @@ -49,6 +49,11 @@ int analogRead(uint8_t pin) pin -= A0; #endif + #if defined(NUM_ANALOG_INPUTS) + if (pin >= NUM_ANALOG_INPUTS) + return 0; + #endif + #if defined(analogPinToChannel) pin = analogPinToChannel(pin); #endif From 179e4dbf112c5a9872c0ba2961fc0ff4f2e489fd Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Tue, 13 May 2014 11:43:31 +0200 Subject: [PATCH 3/4] Fix comment typo in robot_motor variant This fixes part of #1857. --- hardware/arduino/avr/variants/robot_motor/pins_arduino.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hardware/arduino/avr/variants/robot_motor/pins_arduino.h b/hardware/arduino/avr/variants/robot_motor/pins_arduino.h index 7dc4a79b055..c7f1bd86c87 100644 --- a/hardware/arduino/avr/variants/robot_motor/pins_arduino.h +++ b/hardware/arduino/avr/variants/robot_motor/pins_arduino.h @@ -1,5 +1,5 @@ /* - pins_arduino.h - Pin definition functions for Arduino Robot Control Board + pins_arduino.h - Pin definition functions for Arduino Robot Motor Board Part of Arduino - http://www.arduino.cc/ Copyright (c) 2913 D. Cuartielles, X. Yang (Arduino Verkstad) From 88c2d18e97e6f7652328781ef07cefcde18eeb0c Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Sun, 11 May 2014 17:03:08 +0200 Subject: [PATCH 4/4] Define NUM_DIGITAL_PINS and NUM_ANALOG_PINS for the robot variants These are just copied from the Leonardo variant, since they seem to define the same number of pins. This fixes #1857 --- hardware/arduino/avr/variants/robot_control/pins_arduino.h | 3 +++ hardware/arduino/avr/variants/robot_motor/pins_arduino.h | 3 +++ 2 files changed, 6 insertions(+) diff --git a/hardware/arduino/avr/variants/robot_control/pins_arduino.h b/hardware/arduino/avr/variants/robot_control/pins_arduino.h index b868064eac8..e029cfdf3d3 100644 --- a/hardware/arduino/avr/variants/robot_control/pins_arduino.h +++ b/hardware/arduino/avr/variants/robot_control/pins_arduino.h @@ -28,6 +28,9 @@ #include +#define NUM_DIGITAL_PINS 30 +#define NUM_ANALOG_INPUTS 12 + #define ARDUINO_MODEL_USB_PID 0x0038 #define TX_RX_LED_INIT DDRD |= (1<<5), DDRB |= (1<<0) diff --git a/hardware/arduino/avr/variants/robot_motor/pins_arduino.h b/hardware/arduino/avr/variants/robot_motor/pins_arduino.h index c7f1bd86c87..022b8563131 100644 --- a/hardware/arduino/avr/variants/robot_motor/pins_arduino.h +++ b/hardware/arduino/avr/variants/robot_motor/pins_arduino.h @@ -28,6 +28,9 @@ #include +#define NUM_DIGITAL_PINS 30 +#define NUM_ANALOG_INPUTS 12 + #define ARDUINO_MODEL_USB_PID 0x0039 #define TX_RX_LED_INIT DDRD |= (1<<5), DDRB |= (1<<0)