Skip to content

Commit 1a5e3f7

Browse files
committed
Make g_APinDescription const
This has the effect of moving g_APinDescription out of .data (SRAM), and into .rodata (flash). Pros: sketches now have an additional 1160 bytes of SRAM available Cons: state of pins is no longer tracked; e.g. when calling digitalWrite, the full configuration is done each time. See changes in wiring_digital.c and wiring_analog.c for details. There is inevitably a performance impact for digitalRead/Write & analogRead/Write, though it is unlikely to be a noticable one.
1 parent 2e56b81 commit 1a5e3f7

File tree

5 files changed

+30
-52
lines changed

5 files changed

+30
-52
lines changed

cores/arduino/Arduino.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ typedef void (*voidFuncPtr)( void ) ;
6060

6161
/* Types used for the tables below */
6262
/* TODO - consider using smaller types to optimise storage space */
63-
typedef struct _PinDescription
63+
typedef const struct _PinDescription
6464
{
6565
uint32_t ulGPIOId; // GPIO port pin
6666
uint32_t ulGPIOPort; // GPIO port ID

cores/arduino/wiring_analog.c

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,13 @@ void analogWrite(uint8_t pin, int val)
9797
offset = ((p->ulPwmChan * QRK_PWM_N_REGS_LEN) + QRK_PWM_N_LOAD_COUNT1);
9898
MMIO_REG_VAL(QRK_PWM_BASE_ADDR + offset) = lcnt;
9999

100-
if (p->ulPinMode != PWM_MUX_MODE) {
101-
/* start the PWM output */
102-
offset = ((p->ulPwmChan * QRK_PWM_N_REGS_LEN) + QRK_PWM_N_CONTROL);
103-
SET_MMIO_MASK(QRK_PWM_BASE_ADDR + offset, QRK_PWM_CONTROL_ENABLE);
104-
105-
/* Disable pull-up and set pin mux for PWM output */
106-
SET_PIN_PULLUP(p->ulSocPin, 0);
107-
SET_PIN_MODE(p->ulSocPin, PWM_MUX_MODE);
108-
p->ulPinMode = PWM_MUX_MODE;
109-
}
100+
/* start the PWM output */
101+
offset = ((p->ulPwmChan * QRK_PWM_N_REGS_LEN) + QRK_PWM_N_CONTROL);
102+
SET_MMIO_MASK(QRK_PWM_BASE_ADDR + offset, QRK_PWM_CONTROL_ENABLE);
103+
104+
/* Disable pull-up and set pin mux for PWM output */
105+
SET_PIN_PULLUP(p->ulSocPin, 0);
106+
SET_PIN_MODE(p->ulSocPin, PWM_MUX_MODE);
110107
}
111108
}
112109
uint32_t analogRead(uint32_t pin)
@@ -120,11 +117,8 @@ uint32_t analogRead(uint32_t pin)
120117
PinDescription *p = &g_APinDescription[pin];
121118

122119
/* Disable pull-up and set pin mux for ADC output */
123-
if (p->ulPinMode != ADC_MUX_MODE) {
124-
SET_PIN_MODE(p->ulSocPin, ADC_MUX_MODE);
125-
p->ulPinMode = ADC_MUX_MODE;
126-
SET_PIN_PULLUP(p->ulSocPin,0);
127-
}
120+
SET_PIN_MODE(p->ulSocPin, ADC_MUX_MODE);
121+
SET_PIN_PULLUP(p->ulSocPin,0);
128122

129123
/* Reset sequence pointer */
130124
SET_ARC_MASK(ADC_CTRL, ADC_SEQ_PTR_RST);

cores/arduino/wiring_digital.c

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ void pinMode( uint8_t pin, uint8_t mode )
3131
PinDescription *p = &g_APinDescription[pin];
3232

3333
if (mode == OUTPUT) {
34-
p->ulInputMode = OUTPUT_MODE;
3534
if (p->ulGPIOType == SS_GPIO) {
3635
uint32_t reg = p->ulGPIOBase + SS_GPIO_SWPORTA_DDR;
3736
SET_ARC_BIT(reg, p->ulGPIOId);
@@ -41,7 +40,6 @@ void pinMode( uint8_t pin, uint8_t mode )
4140
SET_MMIO_BIT(reg, p->ulGPIOId);
4241
}
4342
} else {
44-
p->ulInputMode = INPUT_MODE;
4543
if (p->ulGPIOType == SS_GPIO) {
4644
uint32_t reg = p->ulGPIOBase + SS_GPIO_SWPORTA_DDR;
4745
CLEAR_ARC_BIT(reg, p->ulGPIOId);
@@ -54,10 +52,7 @@ void pinMode( uint8_t pin, uint8_t mode )
5452

5553
/* Set SoC pin mux configuration */
5654
SET_PIN_PULLUP(p->ulSocPin, (mode == INPUT_PULLUP) ? 1 : 0);
57-
if (p->ulPinMode != GPIO_MUX_MODE) {
58-
SET_PIN_MODE(p->ulSocPin, GPIO_MUX_MODE);
59-
p->ulPinMode = GPIO_MUX_MODE;
60-
}
55+
SET_PIN_MODE(p->ulSocPin, GPIO_MUX_MODE);
6156
}
6257

6358
void digitalWrite( uint8_t pin, uint8_t val )
@@ -66,27 +61,25 @@ void digitalWrite( uint8_t pin, uint8_t val )
6661

6762
PinDescription *p = &g_APinDescription[pin];
6863

69-
if (!p->ulInputMode) {
70-
if (p->ulGPIOType == SS_GPIO) {
71-
uint32_t reg = p->ulGPIOBase + SS_GPIO_SWPORTA_DR;
72-
if (val)
73-
SET_ARC_BIT(reg, p->ulGPIOId);
74-
else
75-
CLEAR_ARC_BIT(reg, p->ulGPIOId);
76-
}
77-
else if (p->ulGPIOType == SOC_GPIO) {
78-
uint32_t reg = p->ulGPIOBase + SOC_GPIO_SWPORTA_DR;
79-
if (val)
80-
SET_MMIO_BIT(reg, p->ulGPIOId);
81-
else
82-
CLEAR_MMIO_BIT(reg, p->ulGPIOId);
83-
}
84-
} else {
85-
if (val)
86-
SET_PIN_PULLUP(p->ulSocPin,1);
87-
else
88-
SET_PIN_PULLUP(p->ulSocPin,0);
89-
}
64+
if (p->ulGPIOType == SS_GPIO) {
65+
uint32_t reg = p->ulGPIOBase + SS_GPIO_SWPORTA_DR;
66+
if (val)
67+
SET_ARC_BIT(reg, p->ulGPIOId);
68+
else
69+
CLEAR_ARC_BIT(reg, p->ulGPIOId);
70+
}
71+
else if (p->ulGPIOType == SOC_GPIO) {
72+
uint32_t reg = p->ulGPIOBase + SOC_GPIO_SWPORTA_DR;
73+
if (val)
74+
SET_MMIO_BIT(reg, p->ulGPIOId);
75+
else
76+
CLEAR_MMIO_BIT(reg, p->ulGPIOId);
77+
}
78+
79+
if (val)
80+
SET_PIN_PULLUP(p->ulSocPin,1);
81+
else
82+
SET_PIN_PULLUP(p->ulSocPin,0);
9083
}
9184

9285
int digitalRead( uint8_t pin )

libraries/CurieI2S/src/CurieI2S.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,6 @@ void Curie_I2S::muxRX(bool enable)
419419
SET_PIN_MODE(49, mux_mode); //I2S_RXD
420420
SET_PIN_MODE(51, mux_mode); //I2S_RWS
421421
SET_PIN_MODE(50, mux_mode); //I2S_RSCK
422-
g_APinDescription[I2S_RXD].ulPinMode = mux_mode;
423-
g_APinDescription[I2S_RWS].ulPinMode = mux_mode;
424-
g_APinDescription[I2S_RSCK].ulPinMode = mux_mode;
425422
}
426423

427424
void Curie_I2S::muxTX(bool enable)
@@ -436,9 +433,6 @@ void Curie_I2S::muxTX(bool enable)
436433
SET_PIN_MODE(g_APinDescription[I2S_TXD].ulSocPin, mux_mode);
437434
SET_PIN_MODE(g_APinDescription[I2S_TWS].ulSocPin, mux_mode);
438435
SET_PIN_MODE(g_APinDescription[I2S_TSCK].ulSocPin, mux_mode);
439-
g_APinDescription[I2S_TXD].ulPinMode = mux_mode;
440-
g_APinDescription[I2S_TWS].ulPinMode = mux_mode;
441-
g_APinDescription[I2S_TSCK].ulPinMode = mux_mode;
442436
}
443437

444438
void Curie_I2S::initRX()

libraries/SPI/src/SPI.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,6 @@ 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-
g_APinDescription[MOSI].ulPinMode = SPI_MUX_MODE;
98-
g_APinDescription[MISO].ulPinMode = SPI_MUX_MODE;
99-
g_APinDescription[SCK].ulPinMode = SPI_MUX_MODE;
10097

10198
}
10299
initialized++; /* reference count */

0 commit comments

Comments
 (0)