Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0c36f1a

Browse filesBrowse files
committed
Derive digitalPinToBitMask from pgm table
Fixes #2
1 parent 05fe492 commit 0c36f1a
Copy full SHA for 0c36f1a

File tree

3 files changed

+51
-7
lines changed
Filter options

3 files changed

+51
-7
lines changed

‎cores/arduino/Arduino.h

Copy file name to clipboardExpand all lines: cores/arduino/Arduino.h
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ bool isDoubleBondedActive(uint8_t pin);
120120

121121
#define digitalPinToPort(pin) ( (pin < NUM_TOTAL_PINS) ? pgm_read_byte(digital_pin_to_port + pin) : NOT_A_PIN )
122122
#define digitalPinToBitPosition(pin) ( (pin < NUM_TOTAL_PINS) ? pgm_read_byte(digital_pin_to_bit_position + pin) : NOT_A_PIN )
123-
#define analogPinToBitPosition(pin) ( (pin < NUM_ANALOG_INPUTS) ? pgm_read_byte(digital_pin_to_bit_position + pin + 14) : NOT_A_PIN )
124-
#define digitalPinToBitMask(pin) ( (pin < NUM_TOTAL_PINS) ? (1 << digitalPinToBitPosition(pin)) : NOT_A_PIN )
125-
#define analogPinToBitMask(pin) ( (pin < NUM_ANALOG_INPUTS) ? (1 << analogPinToBitPosition(pin)) : NOT_A_PIN )
123+
#define analogPinToBitPosition(pin) ( (pin < NUM_ANALOG_INPUTS) ? pgm_read_byte(digital_pin_to_bit_position + pin + ANALOG_INPUT_OFFSET) : NOT_A_PIN )
124+
#define digitalPinToBitMask(pin) ( (pin < NUM_TOTAL_PINS) ? pgm_read_byte(digital_pin_to_bit_mask + pin) : NOT_A_PIN )
125+
#define analogPinToBitMask(pin) ( (pin < NUM_ANALOG_INPUTS) ? pgm_read_byte(digital_pin_to_bit_mask + pin + ANALOG_INPUT_OFFSET) : NOT_A_PIN )
126126
#define digitalPinToTimer(pin) ( (pin < NUM_TOTAL_PINS) ? pgm_read_byte(digital_pin_to_timer + pin) : NOT_ON_TIMER )
127127

128128
#define portToPortStruct(port) ( (port < NUM_TOTAL_PORTS) ? ((PORT_t *)&PORTA + port) : NULL)

‎cores/arduino/wiring_digital.c

Copy file name to clipboardExpand all lines: cores/arduino/wiring_digital.c
+3-4Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,13 @@ __attribute__((weak)) bool isDoubleBondedActive(uint8_t pin __attribute__((unus
3232

3333
void pinMode(uint8_t pin, PinMode mode)
3434
{
35-
uint8_t bit_pos = digitalPinToBitPosition(pin);
35+
uint8_t bit_mask = digitalPinToBitMask(pin);
3636

37-
if ((bit_pos == NOT_A_PIN) || (mode > INPUT_PULLUP) || isDoubleBondedActive(pin)) return;
37+
if ((bit_mask == NOT_A_PIN) || (mode > INPUT_PULLUP) || isDoubleBondedActive(pin)) return;
3838

3939
PORT_t* port = digitalPinToPortStruct(pin);
4040
if(port == NULL) return;
4141

42-
uint8_t bit_mask = (1 << bit_pos);
43-
4442
if(mode == OUTPUT){
4543

4644
/* Save state */
@@ -55,6 +53,7 @@ void pinMode(uint8_t pin, PinMode mode)
5553

5654
} else { /* mode == INPUT or INPUT_PULLUP */
5755

56+
uint8_t bit_pos = digitalPinToBitPosition(pin);
5857
/* Calculate where pin control register is */
5958
uint8_t* pin_ctrl_reg = getPINnCTRLregister(port, bit_pos);
6059

‎variants/uno2018/pins_arduino.h

Copy file name to clipboardExpand all lines: variants/uno2018/pins_arduino.h
+45Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,51 @@ const uint8_t PROGMEM digital_pin_to_bit_position[] = {
250250
PIN6_bp // 40 PF6 RESET
251251
};
252252

253+
/* Use this for accessing PINnCTRL register */
254+
const uint8_t PROGMEM digital_pin_to_bit_mask[] = {
255+
PIN5_bm, // 0 PC5/USART1_Rx
256+
PIN4_bm, // 1 PC4/USART1_Tx
257+
PIN0_bm, // 2 PA0
258+
PIN5_bm, // 3 PF5
259+
PIN6_bm, // 4 PC6
260+
PIN2_bm, // 5 PB2
261+
PIN4_bm, // 6 PF4
262+
PIN1_bm, // 7 PA1
263+
PIN3_bm, // 8 PE3
264+
PIN0_bm, // 9 PB0
265+
PIN1_bm, // 10 PB1
266+
PIN0_bm, // 11 PE0
267+
PIN1_bm, // 12 PE1
268+
PIN2_bm, // 13 PE2
269+
PIN0_bm, // 14 PD0/AI0
270+
PIN1_bm, // 15 PD1AI1
271+
PIN2_bm, // 16 PD2/AI2
272+
PIN3_bm, // 17 PD3/AI3
273+
PIN4_bm, // 18 PD4/AI4
274+
PIN5_bm, // 19 PD5/AI5
275+
PIN2_bm, // 20 PA2/TWI_SDA
276+
PIN3_bm, // 21 PA3/TWI_SCL
277+
PIN3_bm, // 22 PC3
278+
PIN5_bm, // 23 PA5/NINA TX
279+
PIN4_bm, // 24 PA4/NINA RX
280+
PIN6_bm, // 25 PD6/LED_BUILTIN
281+
PIN5_bm, // 26 PB5/USART3_Rx
282+
PIN4_bm, // 27 PB4/USART3_Tx
283+
PIN6_bm, // 28 PA6/NINA GPIO0
284+
PIN7_bm, // 29 PA7/NINA RST
285+
PIN3_bm, // 30 PB3/IMU CS
286+
PIN7_bm, // 31 PC7/IMU INT
287+
PIN0_bm, // 32 PC0/MOSI
288+
PIN1_bm, // 33 PC1/MISO
289+
PIN2_bm, // 34 PC2/SCK
290+
PIN2_bm, // 35 PF2/NINA CS
291+
PIN3_bm, // 36 PF3/NINA ACK
292+
PIN0_bm, // 37 PF0 TOSC 1
293+
PIN1_bm, // 38 PF1 TOSC 2
294+
PIN7_bm, // 39 PD7 VREF
295+
PIN6_bm // 40 PF6 RESET
296+
};
297+
253298
const uint8_t PROGMEM digital_pin_to_timer[] = {
254299
NOT_ON_TIMER, // 0 PC5/USART1_Rx
255300
NOT_ON_TIMER, // 1 PC4/USART1_Tx

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.