Skip to content

Commit fa80455

Browse files
committed
[M252KG] Free up peripheral pins in peripheral free-up HAL API
Without free-up of peripheral pins, peripheral pins of the same peripheral may share by multiple ports after port iteration, and this peripheral may fail with pin interference.
1 parent be526aa commit fa80455

File tree

7 files changed

+69
-9
lines changed

7 files changed

+69
-9
lines changed

targets/TARGET_NUVOTON/TARGET_M251/analogin_api.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ void analogin_init(analogin_t *obj, PinName pin)
5555
MBED_ASSERT(modinit != NULL);
5656
MBED_ASSERT(modinit->modname == (int) obj->adc);
5757

58+
obj->pin = pin;
59+
5860
// Wire pinout
5961
pinmap_pinout(pin, PinMap_ADC);
6062

targets/TARGET_NUVOTON/TARGET_M251/analogout_api.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ void analogout_init(dac_t *obj, PinName pin)
5353
uint32_t chn = NU_MODSUBINDEX(obj->dac);
5454
MBED_ASSERT(chn < NU_DACCHN_MAXNUM);
5555

56+
obj->pin = pin;
57+
5658
/* Wire pinout */
5759
pinmap_pinout(pin, PinMap_DAC);
5860

@@ -119,6 +121,10 @@ void analogout_free(dac_t *obj)
119121
/* Disable IP clock */
120122
CLK_DisableModuleClock(modinit->clkidx);
121123
}
124+
125+
// Free up pins
126+
gpio_set(obj->pin);
127+
obj->pin = NC;
122128
}
123129

124130
void analogout_write(dac_t *obj, float value)

targets/TARGET_NUVOTON/TARGET_M251/i2c_api.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
9999
MBED_ASSERT(modinit != NULL);
100100
MBED_ASSERT(modinit->modname == (int) obj->i2c.i2c);
101101

102+
obj->i2c.pin_sda = sda;
103+
obj->i2c.pin_scl = scl;
104+
102105
pinmap_pinout(sda, PinMap_I2C_SDA);
103106
pinmap_pinout(scl, PinMap_I2C_SCL);
104107

targets/TARGET_NUVOTON/TARGET_M251/objects.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,20 @@ struct port_s {
4444

4545
struct analogin_s {
4646
ADCName adc;
47+
PinName pin;
4748
};
4849

4950
struct dac_s {
5051
DACName dac;
52+
PinName pin;
5153
};
5254

5355
struct serial_s {
5456
UARTName uart;
57+
PinName pin_tx;
58+
PinName pin_rx;
59+
PinName pin_rts;
60+
PinName pin_cts;
5561

5662
uint32_t baudrate;
5763
uint32_t databits;
@@ -91,6 +97,8 @@ struct spi_s {
9197

9298
struct i2c_s {
9399
I2CName i2c;
100+
PinName pin_sda;
101+
PinName pin_scl;
94102
int slaveaddr_state;
95103

96104
uint32_t tran_ctrl;
@@ -109,6 +117,7 @@ struct i2c_s {
109117

110118
struct pwmout_s {
111119
PWMName pwm;
120+
PinName pin;
112121
uint32_t period_us;
113122
uint32_t pulsewidth_us;
114123
};

targets/TARGET_NUVOTON/TARGET_M251/pwmout_api.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ void pwmout_init(pwmout_t* obj, PinName pin)
7070
MBED_ASSERT(modinit != NULL);
7171
MBED_ASSERT(modinit->modname == (int) obj->pwm);
7272

73+
obj->pin = pin;
74+
7375
// Wire pinout
7476
pinmap_pinout(pin, PinMap_PWM);
7577

@@ -120,6 +122,10 @@ void pwmout_free(pwmout_t* obj)
120122
// Mark this module to be deinited.
121123
int i = modinit - pwm_modinit_tab;
122124
pwm_modinit_mask &= ~(1 << i);
125+
126+
// Free up pins
127+
gpio_set(obj->pin);
128+
obj->pin = NC;
123129
}
124130

125131
void pwmout_write(pwmout_t* obj, float value)

targets/TARGET_NUVOTON/TARGET_M251/serial_api.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,15 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
156156

157157
struct nu_uart_var *var = (struct nu_uart_var *) modinit->var;
158158

159-
if (! var->ref_cnt) {
160-
pinmap_pinout(tx, PinMap_UART_TX);
161-
pinmap_pinout(rx, PinMap_UART_RX);
159+
obj->serial.pin_tx = tx;
160+
obj->serial.pin_rx = rx;
161+
obj->serial.pin_rts = NC;
162+
obj->serial.pin_cts = NC;
163+
164+
pinmap_pinout(tx, PinMap_UART_TX);
165+
pinmap_pinout(rx, PinMap_UART_RX);
162166

167+
if (! var->ref_cnt) {
163168
// Select IP clock source
164169
CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv);
165170
// Enable IP clock
@@ -259,6 +264,16 @@ void serial_free(serial_t *obj)
259264
int i = modinit - uart_modinit_tab;
260265
uart_modinit_mask &= ~(1 << i);
261266
}
267+
268+
// Free up pins
269+
gpio_set(obj->serial.pin_tx);
270+
gpio_set(obj->serial.pin_rx);
271+
gpio_set(obj->serial.pin_rts);
272+
gpio_set(obj->serial.pin_cts);
273+
obj->serial.pin_tx = NC;
274+
obj->serial.pin_rx = NC;
275+
obj->serial.pin_rts = NC;
276+
obj->serial.pin_cts = NC;
262277
}
263278

264279
void serial_baud(serial_t *obj, int baudrate)
@@ -305,6 +320,16 @@ void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, Pi
305320
{
306321
UART_T *uart_base = (UART_T *) NU_MODBASE(obj->serial.uart);
307322

323+
// Free up old rts/cts pins when they are different from new ones
324+
if (obj->serial.pin_rts != rxflow) {
325+
gpio_set(obj->serial.pin_rts);
326+
obj->serial.pin_rts = rxflow;
327+
}
328+
if (obj->serial.pin_cts != txflow) {
329+
gpio_set(obj->serial.pin_cts);
330+
obj->serial.pin_cts = txflow;
331+
}
332+
308333
if (rxflow != NC) {
309334
// Check if RTS pin matches.
310335
uint32_t uart_rts = pinmap_peripheral(rxflow, PinMap_UART_RTS);

targets/TARGET_NUVOTON/TARGET_M251/spi_api.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
120120
MBED_ASSERT(modinit != NULL);
121121
MBED_ASSERT(modinit->modname == (int) obj->spi.spi);
122122

123+
obj->spi.pin_mosi = mosi;
124+
obj->spi.pin_miso = miso;
125+
obj->spi.pin_sclk = sclk;
126+
obj->spi.pin_ssel = ssel;
127+
123128
pinmap_pinout(mosi, PinMap_SPI_MOSI);
124129
pinmap_pinout(miso, PinMap_SPI_MISO);
125130
pinmap_pinout(sclk, PinMap_SPI_SCLK);
@@ -133,12 +138,6 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
133138
// Reset this module
134139
SYS_ResetModule(modinit->rsetidx);
135140

136-
obj->spi.pin_mosi = mosi;
137-
obj->spi.pin_miso = miso;
138-
obj->spi.pin_sclk = sclk;
139-
obj->spi.pin_ssel = ssel;
140-
141-
142141
#if DEVICE_SPI_ASYNCH
143142
obj->spi.dma_usage = DMA_USAGE_NEVER;
144143
obj->spi.event = 0;
@@ -184,6 +183,16 @@ void spi_free(spi_t *obj)
184183
// Mark this module to be deinited.
185184
int i = modinit - spi_modinit_tab;
186185
spi_modinit_mask &= ~(1 << i);
186+
187+
// Free up pins
188+
gpio_set(obj->spi.pin_mosi);
189+
gpio_set(obj->spi.pin_miso);
190+
gpio_set(obj->spi.pin_sclk);
191+
gpio_set(obj->spi.pin_ssel);
192+
obj->spi.pin_mosi = NC;
193+
obj->spi.pin_miso = NC;
194+
obj->spi.pin_sclk = NC;
195+
obj->spi.pin_ssel = NC;
187196
}
188197

189198
void spi_format(spi_t *obj, int bits, int mode, int slave)

0 commit comments

Comments
 (0)