Skip to content

Commit 9d4d99c

Browse files
committed
M263: Fix IP initialization sequence
Better IP initialization sequence: 1. Configure IP pins 2. Select IP clock source and then enable it 3. Reset the IP (SYS_ResetModule) NOTE1: IP reset takes effect regardless of IP clock. So it doesn't matter if IP clock enable is before IP reset. NOTE2: Non-configured pins may disturb IP's state, so IP pinout first and then IP reset. NOTE3: IP reset at the end of IP initialization sequence can cover unexpected situation.
1 parent 9aa69d0 commit 9d4d99c

File tree

9 files changed

+46
-46
lines changed

9 files changed

+46
-46
lines changed

targets/TARGET_NUVOTON/TARGET_M261/analogin_api.c

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

57+
// Wire pinout
58+
pinmap_pinout(pin, PinMap_ADC);
59+
5760
EADC_T *eadc_base = (EADC_T *) NU_MODBASE(obj->adc);
5861

5962
// NOTE: All channels (identified by ADCName) share a ADC module. This reset will also affect other channels of the same ADC module.
6063
if (! eadc_modinit_mask) {
61-
// Reset module
62-
SYS_ResetModule(modinit->rsetidx);
63-
6464
// Select IP clock source
6565
CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv);
6666

6767
// Enable IP clock
6868
CLK_EnableModuleClock(modinit->clkidx);
6969

70+
// Reset module
71+
SYS_ResetModule(modinit->rsetidx);
72+
7073
// Set the ADC internal sampling time, input mode as single-end and enable the A/D converter
7174
EADC_Open(eadc_base, EADC_CTL_DIFFEN_SINGLE_END);
7275
}
7376

7477
uint32_t chn = NU_MODSUBINDEX(obj->adc);
7578

76-
// Wire pinout
77-
pinmap_pinout(pin, PinMap_ADC);
78-
7979
// Configure the sample module Nmod for analog input channel Nch and software trigger source
8080
EADC_ConfigSampleModule(eadc_base, chn, EADC_SOFTWARE_TRIGGER, chn);
8181

targets/TARGET_NUVOTON/TARGET_M261/analogout_api.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ 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+
/* Wire pinout */
57+
pinmap_pinout(pin, PinMap_DAC);
58+
5659
DAC_T *dac_base = (DAC_T *) NU_MODBASE(obj->dac);
5760

5861
/* Module-level setup from here */
@@ -66,15 +69,15 @@ void analogout_init(dac_t *obj, PinName pin)
6669
* channels are deactivated.
6770
*/
6871
if ((! dac_modinit_mask[0]) && (! dac_modinit_mask[1])) {
69-
// Reset IP
70-
SYS_ResetModule(modinit->rsetidx);
71-
7272
// Select IP clock source and clock divider
7373
CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv);
7474

7575
// Enable IP clock
7676
CLK_EnableModuleClock(modinit->clkidx);
77-
77+
78+
// Reset IP
79+
SYS_ResetModule(modinit->rsetidx);
80+
7881
/* The conversion settling time is 8us when 12-bit input code transition from
7982
* lowest code (0x000) to highest code (0xFFF). */
8083
DAC_SetDelayTime(dac_base, 8);
@@ -88,9 +91,6 @@ void analogout_init(dac_t *obj, PinName pin)
8891

8992
/* Set the software trigger, enable DAC event trigger mode and enable D/A converter */
9093
DAC_Open(dac_base, chn, DAC_SOFTWARE_TRIGGER);
91-
92-
/* Wire pinout */
93-
pinmap_pinout(pin, PinMap_DAC);
9494

9595
/* Mark channel allocated */
9696
dac_modinit_mask[modidx] |= 1 << chn;

targets/TARGET_NUVOTON/TARGET_M261/dma_api.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ void dma_init(void)
5454
dma_chn_mask = ~NU_PDMA_CH_Msk;
5555
memset(dma_chn_arr, 0x00, sizeof (dma_chn_arr));
5656

57-
// Reset module
58-
SYS_ResetModule(dma_modinit.rsetidx);
59-
6057
// Enable IP clock
6158
CLK_EnableModuleClock(dma_modinit.clkidx);
6259

60+
// Reset module
61+
SYS_ResetModule(dma_modinit.rsetidx);
62+
6363
/* Check PDMA0. */
6464
PDMA_T *pdma_base = dma_modbase();
6565
if (((uint32_t) pdma_base) != PDMA0_BASE) {

targets/TARGET_NUVOTON/TARGET_M261/i2c_api.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
103103
MBED_ASSERT(modinit != NULL);
104104
MBED_ASSERT(modinit->modname == (int) obj->i2c.i2c);
105105

106-
// Reset module
107-
SYS_ResetModule(modinit->rsetidx);
106+
pinmap_pinout(sda, PinMap_I2C_SDA);
107+
pinmap_pinout(scl, PinMap_I2C_SCL);
108108

109109
// Select IP clock source
110110
CLK_EnableModuleClock(modinit->clkidx);
111111

112-
pinmap_pinout(sda, PinMap_I2C_SDA);
113-
pinmap_pinout(scl, PinMap_I2C_SCL);
112+
// Reset module
113+
SYS_ResetModule(modinit->rsetidx);
114114

115115
#if DEVICE_I2C_ASYNCH
116116
obj->i2c.dma_usage = DMA_USAGE_NEVER;

targets/TARGET_NUVOTON/TARGET_M261/lp_ticker.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ void lp_ticker_init(void)
8282
}
8383
ticker_inited = 1;
8484

85-
/* Reset module
86-
*
87-
* NOTE: We must call secure version (from non-secure domain) because SYS/CLK regions are secure.
88-
*/
89-
SYS_ResetModule(TIMER_MODINIT.rsetidx);
90-
9185
/* Select IP clock source
9286
*
9387
* NOTE: We must call secure version (from non-secure domain) because SYS/CLK regions are secure.
@@ -100,6 +94,12 @@ void lp_ticker_init(void)
10094
*/
10195
CLK_EnableModuleClock(TIMER_MODINIT.clkidx);
10296

97+
/* Reset module
98+
*
99+
* NOTE: We must call secure version (from non-secure domain) because SYS/CLK regions are secure.
100+
*/
101+
SYS_ResetModule(TIMER_MODINIT.rsetidx);
102+
103103
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);
104104

105105
// Configure clock

targets/TARGET_NUVOTON/TARGET_M261/pwmout_api.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +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-
// NOTE: All channels (identified by PWMName) share a PWM module. This reset will also affect other channels of the same PWM module.
73-
if (! ((struct nu_pwm_var *) modinit->var)->en_msk) {
74-
// Reset this module if no channel enabled
75-
SYS_ResetModule(modinit->rsetidx);
76-
}
72+
// Wire pinout
73+
pinmap_pinout(pin, PinMap_PWM);
7774

7875
uint32_t chn = NU_MODSUBINDEX(obj->pwm);
7976

@@ -85,8 +82,11 @@ void pwmout_init(pwmout_t* obj, PinName pin)
8582
CLK_EnableModuleClock(modinit->clkidx);
8683
}
8784

88-
// Wire pinout
89-
pinmap_pinout(pin, PinMap_PWM);
85+
// NOTE: All channels (identified by PWMName) share a PWM module. This reset will also affect other channels of the same PWM module.
86+
if (! ((struct nu_pwm_var *) modinit->var)->en_msk) {
87+
// Reset this module if no channel enabled
88+
SYS_ResetModule(modinit->rsetidx);
89+
}
9090

9191
// Default: period = 10 ms, pulse width = 0 ms
9292
obj->period_us = 1000 * 10;

targets/TARGET_NUVOTON/TARGET_M261/serial_api.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,16 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
201201
struct nu_uart_var *var = (struct nu_uart_var *) modinit->var;
202202

203203
if (! var->ref_cnt) {
204-
// Reset this module
205-
SYS_ResetModule(modinit->rsetidx);
204+
pinmap_pinout(tx, PinMap_UART_TX);
205+
pinmap_pinout(rx, PinMap_UART_RX);
206206

207207
// Select IP clock source
208208
CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv);
209209
// Enable IP clock
210210
CLK_EnableModuleClock(modinit->clkidx);
211211

212-
pinmap_pinout(tx, PinMap_UART_TX);
213-
pinmap_pinout(rx, PinMap_UART_RX);
212+
// Reset this module
213+
SYS_ResetModule(modinit->rsetidx);
214214

215215
// Configure baudrate
216216
int baudrate = 9600;

targets/TARGET_NUVOTON/TARGET_M261/spi_api.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,18 @@ 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-
// Reset this module
145-
SYS_ResetModule(modinit->rsetidx);
144+
pinmap_pinout(mosi, PinMap_SPI_MOSI);
145+
pinmap_pinout(miso, PinMap_SPI_MISO);
146+
pinmap_pinout(sclk, PinMap_SPI_SCLK);
147+
pinmap_pinout(ssel, PinMap_SPI_SSEL);
146148

147149
// Select IP clock source
148150
CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv);
149151
// Enable IP clock
150152
CLK_EnableModuleClock(modinit->clkidx);
151153

152-
pinmap_pinout(mosi, PinMap_SPI_MOSI);
153-
pinmap_pinout(miso, PinMap_SPI_MISO);
154-
pinmap_pinout(sclk, PinMap_SPI_SCLK);
155-
pinmap_pinout(ssel, PinMap_SPI_SSEL);
154+
// Reset this module
155+
SYS_ResetModule(modinit->rsetidx);
156156

157157
obj->spi.pin_mosi = mosi;
158158
obj->spi.pin_miso = miso;

targets/TARGET_NUVOTON/TARGET_M261/us_ticker.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ void us_ticker_init(void)
5757
}
5858
ticker_inited = 1;
5959

60-
// Reset IP
61-
SYS_ResetModule(TIMER_MODINIT.rsetidx);
62-
6360
// Select IP clock source
6461
CLK_SetModuleClock(TIMER_MODINIT.clkidx, TIMER_MODINIT.clksrc, TIMER_MODINIT.clkdiv);
6562

6663
// Enable IP clock
6764
CLK_EnableModuleClock(TIMER_MODINIT.clkidx);
6865

66+
// Reset IP
67+
SYS_ResetModule(TIMER_MODINIT.rsetidx);
68+
6969
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);
7070

7171
// Timer for normal counter

0 commit comments

Comments
 (0)