Skip to content

Commit c67a0d8

Browse files
committed
M263: 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 78ae1e0 commit c67a0d8

File tree

7 files changed

+71
-11
lines changed

7 files changed

+71
-11
lines changed

targets/TARGET_NUVOTON/TARGET_M261/analogin_api.c

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

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

targets/TARGET_NUVOTON/TARGET_M261/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

@@ -128,6 +130,10 @@ void analogout_free(dac_t *obj)
128130
// Disable IP clock
129131
CLK_DisableModuleClock(modinit->clkidx);
130132
}
133+
134+
// Free up pins
135+
gpio_set(obj->pin);
136+
obj->pin = NC;
131137
}
132138

133139
void analogout_write(dac_t *obj, float value)

targets/TARGET_NUVOTON/TARGET_M261/i2c_api.c

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

107+
obj->i2c.pin_sda = sda;
108+
obj->i2c.pin_scl = scl;
109+
107110
pinmap_pinout(sda, PinMap_I2C_SDA);
108111
pinmap_pinout(scl, PinMap_I2C_SCL);
109112

targets/TARGET_NUVOTON/TARGET_M261/objects.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,21 @@ struct port_s {
4343

4444
struct analogin_s {
4545
ADCName adc;
46+
PinName pin;
4647
};
4748

4849
struct dac_s {
4950
DACName dac;
51+
PinName pin;
5052
};
5153

5254
struct serial_s {
5355
UARTName uart;
54-
56+
PinName pin_tx;
57+
PinName pin_rx;
58+
PinName pin_rts;
59+
PinName pin_cts;
60+
5561
uint32_t baudrate;
5662
uint32_t databits;
5763
uint32_t parity;
@@ -90,8 +96,10 @@ struct spi_s {
9096

9197
struct i2c_s {
9298
I2CName i2c;
99+
PinName pin_sda;
100+
PinName pin_scl;
93101
int slaveaddr_state;
94-
102+
95103
uint32_t tran_ctrl;
96104
char * tran_beg;
97105
char * tran_pos;
@@ -108,6 +116,7 @@ struct i2c_s {
108116

109117
struct pwmout_s {
110118
PWMName pwm;
119+
PinName pin;
111120
uint32_t period_us;
112121
uint32_t pulsewidth_us;
113122
};

targets/TARGET_NUVOTON/TARGET_M261/pwmout_api.c

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

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

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

124130
void pwmout_write(pwmout_t* obj, float value)

targets/TARGET_NUVOTON/TARGET_M261/serial_api.c

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

202202
struct nu_uart_var *var = (struct nu_uart_var *) modinit->var;
203203

204-
if (! var->ref_cnt) {
205-
pinmap_pinout(tx, PinMap_UART_TX);
206-
pinmap_pinout(rx, PinMap_UART_RX);
204+
obj->serial.pin_tx = tx;
205+
obj->serial.pin_rx = rx;
206+
obj->serial.pin_rts = NC;
207+
obj->serial.pin_cts = NC;
208+
209+
pinmap_pinout(tx, PinMap_UART_TX);
210+
pinmap_pinout(rx, PinMap_UART_RX);
207211

212+
if (! var->ref_cnt) {
208213
// Select IP clock source
209214
CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv);
210215
// Enable IP clock
@@ -304,6 +309,16 @@ void serial_free(serial_t *obj)
304309
int i = modinit - uart_modinit_tab;
305310
uart_modinit_mask &= ~(1 << i);
306311
}
312+
313+
// Free up pins
314+
gpio_set(obj->serial.pin_tx);
315+
gpio_set(obj->serial.pin_rx);
316+
gpio_set(obj->serial.pin_rts);
317+
gpio_set(obj->serial.pin_cts);
318+
obj->serial.pin_tx = NC;
319+
obj->serial.pin_rx = NC;
320+
obj->serial.pin_rts = NC;
321+
obj->serial.pin_cts = NC;
307322
}
308323

309324
void serial_baud(serial_t *obj, int baudrate)
@@ -350,6 +365,16 @@ void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, Pi
350365
{
351366
UART_T *uart_base = (UART_T *) NU_MODBASE(obj->serial.uart);
352367

368+
// Free up old rts/cts pins when they are different from new ones
369+
if (obj->serial.pin_rts != rxflow) {
370+
gpio_set(obj->serial.pin_rts);
371+
obj->serial.pin_rts = rxflow;
372+
}
373+
if (obj->serial.pin_cts != txflow) {
374+
gpio_set(obj->serial.pin_cts);
375+
obj->serial.pin_cts = txflow;
376+
}
377+
353378
if (rxflow != NC) {
354379
// Check if RTS pin matches.
355380
uint32_t uart_rts = pinmap_peripheral(rxflow, PinMap_UART_RTS);

targets/TARGET_NUVOTON/TARGET_M261/spi_api.c

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

144+
obj->spi.pin_mosi = mosi;
145+
obj->spi.pin_miso = miso;
146+
obj->spi.pin_sclk = sclk;
147+
obj->spi.pin_ssel = ssel;
148+
144149
pinmap_pinout(mosi, PinMap_SPI_MOSI);
145150
pinmap_pinout(miso, PinMap_SPI_MISO);
146151
pinmap_pinout(sclk, PinMap_SPI_SCLK);
@@ -154,12 +159,6 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
154159
// Reset this module
155160
SYS_ResetModule(modinit->rsetidx);
156161

157-
obj->spi.pin_mosi = mosi;
158-
obj->spi.pin_miso = miso;
159-
obj->spi.pin_sclk = sclk;
160-
obj->spi.pin_ssel = ssel;
161-
162-
163162
#if DEVICE_SPI_ASYNCH
164163
obj->spi.dma_usage = DMA_USAGE_NEVER;
165164
obj->spi.event = 0;
@@ -205,6 +204,16 @@ void spi_free(spi_t *obj)
205204
// Mark this module to be deinited.
206205
int i = modinit - spi_modinit_tab;
207206
spi_modinit_mask &= ~(1 << i);
207+
208+
// Free up pins
209+
gpio_set(obj->spi.pin_mosi);
210+
gpio_set(obj->spi.pin_miso);
211+
gpio_set(obj->spi.pin_sclk);
212+
gpio_set(obj->spi.pin_ssel);
213+
obj->spi.pin_mosi = NC;
214+
obj->spi.pin_miso = NC;
215+
obj->spi.pin_sclk = NC;
216+
obj->spi.pin_ssel = NC;
208217
}
209218

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

0 commit comments

Comments
 (0)