Skip to content

Commit 233acde

Browse files
Thomaz, MartinWalberg, Irun
authored andcommitted
Fixed bug, portToPortStruct() was checking agains NOT_A_PORT (max value), but should check against MAX port numbers.
Even with NOT_A_PORT being 255 (previouse 0), it could return an invalid address instead of NULL.
1 parent c7f6b04 commit 233acde

File tree

1 file changed

+59
-18
lines changed

1 file changed

+59
-18
lines changed

cores/arduino/Arduino.h

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,56 @@ extern uint32_t F_CPU_CORRECTED;
6161
#define interrupts() sei()
6262
#define noInterrupts() cli()
6363

64-
uint16_t clockCyclesPerMicrosecond(uint32_t clk);
65-
uint16_t clockCyclesToMicroseconds(uint16_t cycles, uint32_t clk);
66-
uint32_t microsecondsToClockCycles(uint16_t cycles, uint32_t clk);
67-
6864
// avr-libc defines _NOP() since 1.6.2
6965
#ifndef _NOP
7066
#define _NOP() do { __asm__ volatile ("nop"); } while (0)
7167
#endif
7268

69+
typedef unsigned int word;
70+
71+
#define bit(b) (1UL << (b))
72+
73+
typedef bool boolean;
74+
typedef uint8_t byte;
75+
76+
extern uint32_t F_CPU_CORRECTED;
77+
78+
void init(void);
79+
void initVariant(void);
80+
81+
uint16_t clockCyclesPerMicrosecond(uint32_t clk);
82+
uint16_t clockCyclesToMicroseconds(uint16_t cycles, uint32_t clk);
83+
uint32_t microsecondsToClockCycles(uint16_t cycles, uint32_t clk);
84+
85+
uint16_t clockCyclesPerMicrosecondOrig();
86+
uint16_t clockCyclesToMicrosecondsOrig(uint16_t cycles);
87+
uint32_t microsecondsToClockCyclesOrig(uint16_t cycles);
88+
89+
int atexit(void (*func)()) __attribute__((weak));
90+
91+
void pinMode(uint8_t, uint8_t);
92+
void digitalWrite(uint8_t, uint8_t);
93+
int digitalRead(uint8_t);
94+
int analogRead(uint8_t);
95+
void analogReference(uint8_t mode);
96+
void analogWrite(uint8_t, int);
97+
98+
unsigned long millis(void);
99+
unsigned long micros(void);
100+
void delay(unsigned long);
101+
void delayMicroseconds(unsigned int us);
102+
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
103+
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout);
104+
105+
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
106+
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
107+
108+
void attachInterrupt(uint8_t, void (*)(void), int mode);
109+
void detachInterrupt(uint8_t);
110+
111+
void setup(void);
112+
void loop(void);
113+
73114
// Get the bit location within the hardware port of the given virtual pin.
74115
// This comes from the pins_*.c file for the active board configuration.
75116

@@ -85,19 +126,6 @@ extern const uint8_t PROGMEM digital_pin_to_interrupt[];
85126
//
86127
// These perform slightly better as macros compared to inline functions
87128
//
88-
#define digitalPinToPort(pin) ( (pin < NUM_TOTAL_PINS) ? pgm_read_byte(digital_pin_to_port + pin) : NOT_A_PIN )
89-
#define digitalPinToBitPosition(pin) ( (pin < NUM_TOTAL_PINS) ? pgm_read_byte(digital_pin_to_bit_position + pin) : NOT_A_PIN )
90-
#define analogPinToBitPosition(pin) ( (pin < NUM_ANALOG_INPUTS) ? pgm_read_byte(digital_pin_to_bit_position + pin + 14) : NOT_A_PIN )
91-
#define digitalPinToBitMask(pin) ( (pin < NUM_TOTAL_PINS) ? (1 << digitalPinToBitPosition(pin)) : NOT_A_PIN )
92-
#define analogPinToBitMask(pin) ( (pin < NUM_ANALOG_INPUTS) ? (1 << analogPinToBitPosition(pin)) : NOT_A_PIN )
93-
#define digitalPinToTimer(pin) ( (pin < NUM_TOTAL_PINS) ? pgm_read_byte(digital_pin_to_timer + pin) : NOT_ON_TIMER )
94-
95-
#define portToPortStruct(port) ( (port < NUMBER_OF_PORTS) ? ((PORT_t *)&PORTA + port) : NULL)
96-
#define digitalPinToPortStruct(pin) ( (pin < NUM_TOTAL_PINS) ? ((PORT_t *)&PORTA + digitalPinToPort(pin)) : NULL)
97-
#define getPINnCTRLregister(port_addr, bit_pos) ( ((port_addr != NULL) && (bit_pos < NOT_A_PIN)) ? ((uint8_t *)&(port_addr->PIN0CTRL) + bit_pos) : NULL )
98-
#define digitalPinToInterrupt(p) ( pgm_read_byte(digital_pin_to_interrupt + p) )
99-
#define portPinToInterrupt(port, bit_pos) ( pgm_read_byte(port_interrupt_offset + port) + bit_pos )
100-
101129
#define NOT_A_PIN 255
102130
#define NOT_A_PORT 255
103131
#define NOT_AN_INTERRUPT 255
@@ -108,7 +136,7 @@ extern const uint8_t PROGMEM digital_pin_to_interrupt[];
108136
#define PD 3
109137
#define PE 4
110138
#define PF 5
111-
#define NUMBER_OF_PORTS 6
139+
#define NUM_TOTAL_PORTS 6
112140

113141
#define NOT_ON_TIMER 0
114142
#define TIMERA0 1
@@ -117,6 +145,19 @@ extern const uint8_t PROGMEM digital_pin_to_interrupt[];
117145
#define TIMERB2 4
118146
#define TIMERB3 5
119147

148+
#define digitalPinToPort(pin) ( (pin < NUM_TOTAL_PINS) ? pgm_read_byte(digital_pin_to_port + pin) : NOT_A_PIN )
149+
#define digitalPinToBitPosition(pin) ( (pin < NUM_TOTAL_PINS) ? pgm_read_byte(digital_pin_to_bit_position + pin) : NOT_A_PIN )
150+
#define analogPinToBitPosition(pin) ( (pin < NUM_ANALOG_INPUTS) ? pgm_read_byte(digital_pin_to_bit_position + pin + 14) : NOT_A_PIN )
151+
#define digitalPinToBitMask(pin) ( (pin < NUM_TOTAL_PINS) ? (1 << digitalPinToBitPosition(pin)) : NOT_A_PIN )
152+
#define analogPinToBitMask(pin) ( (pin < NUM_ANALOG_INPUTS) ? (1 << analogPinToBitPosition(pin)) : NOT_A_PIN )
153+
#define digitalPinToTimer(pin) ( (pin < NUM_TOTAL_PINS) ? pgm_read_byte(digital_pin_to_timer + pin) : NOT_ON_TIMER )
154+
155+
#define portToPortStruct(port) ( (port < NUM_TOTAL_PORTS) ? ((PORT_t *)&PORTA + port) : NULL)
156+
#define digitalPinToPortStruct(pin) ( (pin < NUM_TOTAL_PINS) ? ((PORT_t *)&PORTA + digitalPinToPort(pin)) : NULL)
157+
#define getPINnCTRLregister(port, bit_pos) ( ((port != NULL) && (bit_pos < NOT_A_PIN)) ? ((uint8_t *)&(port->PIN0CTRL) + bit_pos) : NULL )
158+
#define digitalPinToInterrupt(p) ( pgm_read_byte(digital_pin_to_interrupt + p) )
159+
#define portPinToInterrupt(port, bit_pos) ( pgm_read_byte(port_interrupt_offset + port) + bit_pos )
160+
120161
#ifdef __cplusplus
121162
} // extern "C"
122163
#endif

0 commit comments

Comments
 (0)