Skip to content

Commit 431c781

Browse files
bigdinotecheriknyquist
authored andcommitted
check mux mode when using digitalWrite (#335)
-check and make sure that the pin is in GPIO_MUX_MODE when doing a digitalWrite() -this has a slight performance impact of digitalWrite -it also comsumes an extra byte in SRAM per pin -it does however give us the ability to track the muxing state of each pin
1 parent 16227f5 commit 431c781

File tree

6 files changed

+31
-8
lines changed

6 files changed

+31
-8
lines changed

cores/arduino/Arduino.h

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <stdbool.h>
2727
#include <math.h>
2828

29+
#include "pins_arduino.h"
30+
2931
#include "binary.h"
3032
//#include "itoa.h"
3133

@@ -97,6 +99,8 @@ extern PinDescription g_APinDescription[] ;
9799

98100
extern uint32_t pwmPeriod[4];
99101

102+
extern uint8_t pinmuxMode[NUM_DIGITAL_PINS];
103+
100104
#ifdef __cplusplus
101105
} // extern "C"
102106

cores/arduino/wiring_analog.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,14 @@ void analogWrite(uint8_t pin, uint32_t val)
9393
/* start the PWM output */
9494
offset = ((p->ulPwmChan * QRK_PWM_N_REGS_LEN) + QRK_PWM_N_CONTROL);
9595
SET_MMIO_MASK(QRK_PWM_BASE_ADDR + offset, QRK_PWM_CONTROL_ENABLE);
96-
97-
/* Disable pull-up and set pin mux for PWM output */
98-
SET_PIN_PULLUP(p->ulSocPin, 0);
99-
SET_PIN_MODE(p->ulSocPin, PWM_MUX_MODE);
96+
97+
if(pinmuxMode[pin] != PWM_MUX_MODE)
98+
{
99+
/* Disable pull-up and set pin mux for PWM output */
100+
SET_PIN_PULLUP(p->ulSocPin, 0);
101+
SET_PIN_MODE(p->ulSocPin, PWM_MUX_MODE);
102+
pinmuxMode[pin] = PWM_MUX_MODE;
103+
}
100104
}
101105
}
102106
uint32_t analogRead(uint32_t pin)

cores/arduino/wiring_digital.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,24 @@ void pinMode( uint8_t pin, uint8_t mode )
5353
/* Set SoC pin mux configuration */
5454
SET_PIN_PULLUP(p->ulSocPin, (mode == INPUT_PULLUP) ? 1 : 0);
5555
SET_PIN_MODE(p->ulSocPin, GPIO_MUX_MODE);
56+
if(pinmuxMode[pin] != GPIO_MUX_MODE)
57+
{
58+
pinmuxMode[pin] = GPIO_MUX_MODE;
59+
}
5660
}
5761

5862
void digitalWrite( uint8_t pin, uint8_t val )
5963
{
6064
if (pin >= NUM_DIGITAL_PINS) return;
6165

6266
PinDescription *p = &g_APinDescription[pin];
63-
67+
68+
if(pinmuxMode[pin] != GPIO_MUX_MODE)
69+
{
70+
pinmuxMode[pin] = GPIO_MUX_MODE;
71+
SET_PIN_MODE(p->ulSocPin, GPIO_MUX_MODE);
72+
}
73+
6474
if (p->ulGPIOType == SS_GPIO) {
6575
uint32_t reg = p->ulGPIOBase + SS_GPIO_SWPORTA_DR;
6676
if (val)

libraries/CurieSoftwareSerial/src/SoftwareSerial.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ void SoftwareSerial::begin(long speed)
280280
{
281281
_isSOCGpio = true;
282282
}
283-
//toggling a pin takes about 62 ticks
284-
_tx_delay = _bit_delay - 62;
283+
//toggling a pin takes about 68 ticks
284+
_tx_delay = _bit_delay - 68;
285285
//reading a pin takes about 70 ticks
286286
_rx_delay_intrabit = _bit_delay - 70;
287287
//it takes about 272 ticks from when the start bit is received to when the ISR is called

libraries/SPI/src/SPI.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ void SPIClass::begin()
9494
SET_PIN_MODE(g_APinDescription[MOSI].ulSocPin, SPI_MUX_MODE);
9595
SET_PIN_MODE(g_APinDescription[MISO].ulSocPin, SPI_MUX_MODE);
9696
SET_PIN_MODE(g_APinDescription[SCK].ulSocPin, SPI_MUX_MODE);
97-
97+
pinmuxMode[MISO] = SPI_MUX_MODE;
98+
pinmuxMode[MOSI] = SPI_MUX_MODE;
99+
pinmuxMode[SCK] = SPI_MUX_MODE;
98100
}
99101
initialized++; /* reference count */
100102
interrupt_unlock(flags);

variants/arduino_101/variant.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ PinDescription g_APinDescription[]=
9999
} ;
100100

101101
uint32_t pwmPeriod[] = {PWM_PERIOD, PWM_PERIOD/2, PWM_PERIOD/2, PWM_PERIOD};
102+
103+
uint8_t pinmuxMode[];
102104
#ifdef __cplusplus
103105
}
104106
#endif
@@ -173,6 +175,7 @@ void variantGpioInit(void)
173175
for (uint8_t pin = 0; pin < NUM_DIGITAL_PINS; pin++) {
174176
PinDescription *p = &g_APinDescription[pin];
175177
SET_PIN_MODE(p->ulSocPin, p->ulPinMode);
178+
pinmuxMode[pin] = GPIO_MUX_MODE;
176179
}
177180
}
178181

0 commit comments

Comments
 (0)