Skip to content

Commit d8151d7

Browse files
authored
Merge pull request #3590 from OpenNuvoton/nuvoton
[NUC472/M453] Export IAR project and other bugfixes
2 parents aa6d673 + 867decb commit d8151d7

File tree

10 files changed

+57
-226
lines changed

10 files changed

+57
-226
lines changed

targets/TARGET_NUVOTON/TARGET_M451/TARGET_NUMAKER_PFM_M453/objects.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,6 @@ struct pwmout_s {
117117
};
118118

119119
struct sleep_s {
120-
uint32_t start_us;
121-
uint32_t end_us;
122-
uint32_t period_us;
123120
int powerdown;
124121
};
125122

targets/TARGET_NUVOTON/TARGET_M451/device/TOOLCHAIN_GCC_ARM/m451_retarget.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111

1212
#include "M451Series.h"
1313
#include <errno.h>
14+
#include "nu_miscutil.h"
1415

1516
extern uint32_t __mbed_sbrk_start;
1617
extern uint32_t __mbed_krbs_start;
1718

19+
#define NU_HEAP_ALIGN 32
20+
1821
/**
1922
* The default implementation of _sbrk() (in common/retarget.cpp) for GCC_ARM requires one-region model (heap and stack share one region), which doesn't
2023
* fit two-region model (heap and stack are two distinct regions), for example, NUMAKER-PFM-NUC472 locates heap on external SRAM. Define __wrap__sbrk() to
@@ -23,8 +26,8 @@ extern uint32_t __mbed_krbs_start;
2326
void *__wrap__sbrk(int incr)
2427
{
2528
static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start;
26-
uint32_t heap_ind_old = heap_ind;
27-
uint32_t heap_ind_new = (heap_ind_old + incr + 7) & ~7;
29+
uint32_t heap_ind_old = NU_ALIGN_UP(heap_ind, NU_HEAP_ALIGN);
30+
uint32_t heap_ind_new = NU_ALIGN_UP(heap_ind_old + incr, NU_HEAP_ALIGN);
2831

2932
if (heap_ind_new > &__mbed_krbs_start) {
3033
errno = ENOMEM;

targets/TARGET_NUVOTON/TARGET_M451/sleep.c

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
#include "objects.h"
2626
#include "PeripheralPins.h"
2727

28-
void us_ticker_prepare_sleep(struct sleep_s *obj);
29-
void us_ticker_wakeup_from_sleep(struct sleep_s *obj);
3028
static void mbed_enter_sleep(struct sleep_s *obj);
3129
static void mbed_exit_sleep(struct sleep_s *obj);
3230

@@ -57,8 +55,7 @@ void hal_deepsleep(void)
5755
mbed_exit_sleep(&sleep_obj);
5856
}
5957

60-
61-
void mbed_enter_sleep(struct sleep_s *obj)
58+
static void mbed_enter_sleep(struct sleep_s *obj)
6259
{
6360
// Check if serial allows entering power-down mode
6461
if (obj->powerdown) {
@@ -77,16 +74,7 @@ void mbed_enter_sleep(struct sleep_s *obj)
7774
obj->powerdown = pwmout_allow_powerdown();
7875
}
7976
// TODO: Check if other peripherals allow entering power-down mode
80-
81-
obj->start_us = lp_ticker_read();
82-
// Let us_ticker prepare for power-down or reject it.
83-
us_ticker_prepare_sleep(obj);
84-
85-
// NOTE(STALE): To pass mbed-drivers test, timer requires to be fine-grained, so its implementation needs HIRC rather than LIRC/LXT as its clock source.
86-
// But as CLK_PowerDown()/CLK_Idle() is called, HIRC will be disabled and timer cannot keep counting and alarm. To overcome the dilemma,
87-
// just make CPU halt and compromise power saving.
88-
// NOTE: As CLK_PowerDown()/CLK_Idle() is called, HIRC/HXT will be disabled in normal mode, but not in ICE mode. This may cause confusion in development.
89-
77+
9078
if (obj->powerdown) { // Power-down mode (HIRC/HXT disabled, LIRC/LXT enabled)
9179
SYS_UnlockReg();
9280
CLK_PowerDown();
@@ -101,14 +89,9 @@ void mbed_enter_sleep(struct sleep_s *obj)
10189
__NOP();
10290
__NOP();
10391
__NOP();
104-
105-
obj->end_us = lp_ticker_read();
106-
obj->period_us = (obj->end_us > obj->start_us) ? (obj->end_us - obj->start_us) : (uint32_t) ((uint64_t) obj->end_us + 0xFFFFFFFFu - obj->start_us);
107-
// Let us_ticker recover from power-down.
108-
us_ticker_wakeup_from_sleep(obj);
10992
}
11093

111-
void mbed_exit_sleep(struct sleep_s *obj)
94+
static void mbed_exit_sleep(struct sleep_s *obj)
11295
{
11396
// TODO: TO BE CONTINUED
11497

targets/TARGET_NUVOTON/TARGET_M451/us_ticker.c

Lines changed: 18 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,22 @@
2727

2828
#define TMR0HIRES_CLK_PER_SEC (1000 * 1000)
2929
#define TMR1HIRES_CLK_PER_SEC (1000 * 1000)
30-
#define TMR1LORES_CLK_PER_SEC (__LIRC)
3130

3231
#define US_PER_TMR0HIRES_CLK (US_PER_SEC / TMR0HIRES_CLK_PER_SEC)
3332
#define US_PER_TMR1HIRES_CLK (US_PER_SEC / TMR1HIRES_CLK_PER_SEC)
34-
#define US_PER_TMR1LORES_CLK (US_PER_SEC / TMR1LORES_CLK_PER_SEC)
3533

3634
#define US_PER_TMR0HIRES_INT (1000 * 1000 * 10)
3735
#define TMR0HIRES_CLK_PER_TMR0HIRES_INT ((uint32_t) ((uint64_t) US_PER_TMR0HIRES_INT * TMR0HIRES_CLK_PER_SEC / US_PER_SEC))
3836

3937

40-
// Determine to use lo-res/hi-res timer according to CD period
41-
#define US_TMR_SEP_CD 1000
42-
4338
static void tmr0_vec(void);
4439
static void tmr1_vec(void);
4540
static void us_ticker_arm_cd(void);
4641

4742
static int us_ticker_inited = 0;
4843
static volatile uint32_t counter_major = 0;
49-
static volatile uint32_t pd_comp_us = 0; // Power-down compenstaion for normal counter
5044
static volatile uint32_t cd_major_minor_us = 0;
5145
static volatile uint32_t cd_minor_us = 0;
52-
static volatile int cd_hires_tmr_armed = 0; // Flag of armed or not of hi-res timer for CD counter
5346

5447
// NOTE: PCLK is set up in mbed_sdk_init(), invocation of which must be before C++ global object constructor. See init_api.c for details.
5548
// NOTE: Choose clock source of timer:
@@ -58,7 +51,6 @@ static volatile int cd_hires_tmr_armed = 0; // Flag of armed or not of hi-res ti
5851
// 3. PCLK(HXT): Less accurate but can pass mbed-drivers test.
5952
// NOTE: TIMER_0 for normal counter, TIMER_1 for countdown.
6053
static const struct nu_modinit_s timer0hires_modinit = {TIMER_0, TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_PCLK0, 0, TMR0_RST, TMR0_IRQn, (void *) tmr0_vec};
61-
static const struct nu_modinit_s timer1lores_modinit = {TIMER_1, TMR1_MODULE, CLK_CLKSEL1_TMR1SEL_LIRC, 0, TMR1_RST, TMR1_IRQn, (void *) tmr1_vec};
6254
static const struct nu_modinit_s timer1hires_modinit = {TIMER_1, TMR1_MODULE, CLK_CLKSEL1_TMR1SEL_PCLK0, 0, TMR1_RST, TMR1_IRQn, (void *) tmr1_vec};
6355

6456
#define TMR_CMP_MIN 2
@@ -71,22 +63,20 @@ void us_ticker_init(void)
7163
}
7264

7365
counter_major = 0;
74-
pd_comp_us = 0;
7566
cd_major_minor_us = 0;
7667
cd_minor_us = 0;
77-
cd_hires_tmr_armed = 0;
7868
us_ticker_inited = 1;
7969

8070
// Reset IP
8171
SYS_ResetModule(timer0hires_modinit.rsetidx);
82-
SYS_ResetModule(timer1lores_modinit.rsetidx);
72+
SYS_ResetModule(timer1hires_modinit.rsetidx);
8373

8474
// Select IP clock source
8575
CLK_SetModuleClock(timer0hires_modinit.clkidx, timer0hires_modinit.clksrc, timer0hires_modinit.clkdiv);
86-
CLK_SetModuleClock(timer1lores_modinit.clkidx, timer1lores_modinit.clksrc, timer1lores_modinit.clkdiv);
76+
CLK_SetModuleClock(timer1hires_modinit.clkidx, timer1hires_modinit.clksrc, timer1hires_modinit.clkdiv);
8777
// Enable IP clock
8878
CLK_EnableModuleClock(timer0hires_modinit.clkidx);
89-
CLK_EnableModuleClock(timer1lores_modinit.clkidx);
79+
CLK_EnableModuleClock(timer1hires_modinit.clkidx);
9080

9181
// Timer for normal counter
9282
uint32_t clk_timer0 = TIMER_GetModuleClock((TIMER_T *) NU_MODBASE(timer0hires_modinit.modname));
@@ -100,10 +90,10 @@ void us_ticker_init(void)
10090
((TIMER_T *) NU_MODBASE(timer0hires_modinit.modname))->CMP = cmp_timer0;
10191

10292
NVIC_SetVector(timer0hires_modinit.irq_n, (uint32_t) timer0hires_modinit.var);
103-
NVIC_SetVector(timer1lores_modinit.irq_n, (uint32_t) timer1lores_modinit.var);
93+
NVIC_SetVector(timer1hires_modinit.irq_n, (uint32_t) timer1hires_modinit.var);
10494

10595
NVIC_EnableIRQ(timer0hires_modinit.irq_n);
106-
NVIC_EnableIRQ(timer1lores_modinit.irq_n);
96+
NVIC_EnableIRQ(timer1hires_modinit.irq_n);
10797

10898
TIMER_EnableInt((TIMER_T *) NU_MODBASE(timer0hires_modinit.modname));
10999
TIMER_Start((TIMER_T *) NU_MODBASE(timer0hires_modinit.modname));
@@ -141,26 +131,24 @@ uint32_t us_ticker_read()
141131
}
142132
while (minor_us == 0 || minor_us == US_PER_TMR0HIRES_INT);
143133

144-
// Add power-down compensation
145-
return (major_minor_us + pd_comp_us) / US_PER_TICK;
134+
return (major_minor_us / US_PER_TICK);
146135
}
147136
while (0);
148137
}
149138

150139
void us_ticker_disable_interrupt(void)
151140
{
152-
TIMER_DisableInt((TIMER_T *) NU_MODBASE(timer1lores_modinit.modname));
141+
TIMER_DisableInt((TIMER_T *) NU_MODBASE(timer1hires_modinit.modname));
153142
}
154143

155144
void us_ticker_clear_interrupt(void)
156145
{
157-
TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(timer1lores_modinit.modname));
146+
TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(timer1hires_modinit.modname));
158147
}
159148

160149
void us_ticker_set_interrupt(timestamp_t timestamp)
161150
{
162-
TIMER_Stop((TIMER_T *) NU_MODBASE(timer1lores_modinit.modname));
163-
cd_hires_tmr_armed = 0;
151+
TIMER_Stop((TIMER_T *) NU_MODBASE(timer1hires_modinit.modname));
164152

165153
int delta = (int) (timestamp - us_ticker_read());
166154
if (delta > 0) {
@@ -173,42 +161,10 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
173161
* This event was in the past. Set the interrupt as pending, but don't process it here.
174162
* This prevents a recurive loop under heavy load which can lead to a stack overflow.
175163
*/
176-
NVIC_SetPendingIRQ(timer1lores_modinit.irq_n);
164+
NVIC_SetPendingIRQ(timer1hires_modinit.irq_n);
177165
}
178166
}
179167

180-
void us_ticker_prepare_sleep(struct sleep_s *obj)
181-
{
182-
// Reject power-down if hi-res timer (HIRC/HXT) is now armed for CD counter.
183-
if (obj->powerdown) {
184-
obj->powerdown = ! cd_hires_tmr_armed;
185-
}
186-
187-
core_util_critical_section_enter();
188-
189-
if (obj->powerdown) {
190-
// NOTE: On entering power-down mode, HIRC/HXT will be disabled in normal mode, but not in ICE mode. This may cause confusion in development.
191-
// To not be inconsistent due to above, always disable clock source of normal counter, and then re-enable it and make compensation on wakeup from power-down.
192-
CLK_DisableModuleClock(timer0hires_modinit.clkidx);
193-
}
194-
195-
core_util_critical_section_exit();
196-
}
197-
198-
void us_ticker_wakeup_from_sleep(struct sleep_s *obj)
199-
{
200-
core_util_critical_section_enter();
201-
202-
if (obj->powerdown) {
203-
// Calculate power-down compensation
204-
pd_comp_us += obj->period_us;
205-
206-
CLK_EnableModuleClock(timer0hires_modinit.clkidx);
207-
}
208-
209-
core_util_critical_section_exit();
210-
}
211-
212168
static void tmr0_vec(void)
213169
{
214170
TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(timer0hires_modinit.modname));
@@ -217,9 +173,8 @@ static void tmr0_vec(void)
217173

218174
static void tmr1_vec(void)
219175
{
220-
TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(timer1lores_modinit.modname));
176+
TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(timer1hires_modinit.modname));
221177
cd_major_minor_us = (cd_major_minor_us > cd_minor_us) ? (cd_major_minor_us - cd_minor_us) : 0;
222-
cd_hires_tmr_armed = 0;
223178
if (cd_major_minor_us == 0) {
224179
// NOTE: us_ticker_set_interrupt() may get called in us_ticker_irq_handler();
225180
us_ticker_irq_handler();
@@ -231,49 +186,22 @@ static void tmr1_vec(void)
231186

232187
static void us_ticker_arm_cd(void)
233188
{
234-
TIMER_T * timer1_base = (TIMER_T *) NU_MODBASE(timer1lores_modinit.modname);
235-
uint32_t tmr1_clk_per_sec;
236-
uint32_t us_per_tmr1_clk;
237-
238-
/**
239-
* Reserve US_TMR_SEP_CD-plus alarm period for hi-res timer
240-
* 1. period >= US_TMR_SEP_CD * 2. Divide into two rounds:
241-
* US_TMR_SEP_CD * n (lo-res timer)
242-
* US_TMR_SEP_CD + period % US_TMR_SEP_CD (hi-res timer)
243-
* 2. period < US_TMR_SEP_CD * 2. Just one round:
244-
* period (hi-res timer)
245-
*/
246-
if (cd_major_minor_us >= US_TMR_SEP_CD * 2) {
247-
cd_minor_us = cd_major_minor_us - cd_major_minor_us % US_TMR_SEP_CD - US_TMR_SEP_CD;
248-
249-
CLK_SetModuleClock(timer1lores_modinit.clkidx, timer1lores_modinit.clksrc, timer1lores_modinit.clkdiv);
250-
tmr1_clk_per_sec = TMR1LORES_CLK_PER_SEC;
251-
us_per_tmr1_clk = US_PER_TMR1LORES_CLK;
252-
253-
cd_hires_tmr_armed = 0;
254-
}
255-
else {
256-
cd_minor_us = cd_major_minor_us;
257-
258-
CLK_SetModuleClock(timer1hires_modinit.clkidx, timer1hires_modinit.clksrc, timer1hires_modinit.clkdiv);
259-
tmr1_clk_per_sec = TMR1HIRES_CLK_PER_SEC;
260-
us_per_tmr1_clk = US_PER_TMR1HIRES_CLK;
261-
262-
cd_hires_tmr_armed = 1;
263-
}
189+
TIMER_T * timer1_base = (TIMER_T *) NU_MODBASE(timer1hires_modinit.modname);
264190

191+
cd_minor_us = cd_major_minor_us;
192+
265193
// Reset 8-bit PSC counter, 24-bit up counter value and CNTEN bit
266194
timer1_base->CTL |= TIMER_CTL_RSTCNT_Msk;
267195
// One-shot mode, Clock = 1 MHz
268-
uint32_t clk_timer1 = TIMER_GetModuleClock((TIMER_T *) NU_MODBASE(timer1lores_modinit.modname));
269-
uint32_t prescale_timer1 = clk_timer1 / tmr1_clk_per_sec - 1;
196+
uint32_t clk_timer1 = TIMER_GetModuleClock((TIMER_T *) NU_MODBASE(timer1hires_modinit.modname));
197+
uint32_t prescale_timer1 = clk_timer1 / TMR1HIRES_CLK_PER_SEC - 1;
270198
MBED_ASSERT((prescale_timer1 != (uint32_t) -1) && prescale_timer1 <= 127);
271-
MBED_ASSERT((clk_timer1 % tmr1_clk_per_sec) == 0);
199+
MBED_ASSERT((clk_timer1 % TMR1HIRES_CLK_PER_SEC) == 0);
272200
// NOTE: TIMER_CTL_CNTDATEN_Msk exists in NUC472, but not in M451. In M451, TIMER_CNT is updated continuously by default.
273201
timer1_base->CTL &= ~(TIMER_CTL_OPMODE_Msk | TIMER_CTL_PSC_Msk/* | TIMER_CTL_CNTDATEN_Msk*/);
274202
timer1_base->CTL |= TIMER_ONESHOT_MODE | prescale_timer1/* | TIMER_CTL_CNTDATEN_Msk*/;
275203

276-
uint32_t cmp_timer1 = cd_minor_us / us_per_tmr1_clk;
204+
uint32_t cmp_timer1 = cd_minor_us / US_PER_TMR1HIRES_CLK;
277205
cmp_timer1 = NU_CLAMP(cmp_timer1, TMR_CMP_MIN, TMR_CMP_MAX);
278206
timer1_base->CMP = cmp_timer1;
279207

targets/TARGET_NUVOTON/TARGET_NUC472/TARGET_NUMAKER_PFM_NUC472/objects.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,6 @@ struct pwmout_s {
118118
};
119119

120120
struct sleep_s {
121-
uint32_t start_us;
122-
uint32_t end_us;
123-
uint32_t period_us;
124121
int powerdown;
125122
};
126123

targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_GCC_ARM/nuc472_retarget.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111

1212
#include "NUC472_442.h"
1313
#include <errno.h>
14+
#include "nu_miscutil.h"
1415

1516
extern uint32_t __mbed_sbrk_start;
1617
extern uint32_t __mbed_krbs_start;
1718

19+
#define NU_HEAP_ALIGN 32
20+
1821
/**
1922
* The default implementation of _sbrk() (in common/retarget.cpp) for GCC_ARM requires one-region model (heap and stack share one region), which doesn't
2023
* fit two-region model (heap and stack are two distinct regions), for example, NUMAKER-PFM-NUC472 locates heap on external SRAM. Define __wrap__sbrk() to
@@ -23,8 +26,8 @@ extern uint32_t __mbed_krbs_start;
2326
void *__wrap__sbrk(int incr)
2427
{
2528
static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start;
26-
uint32_t heap_ind_old = heap_ind;
27-
uint32_t heap_ind_new = (heap_ind_old + incr + 7) & ~7;
29+
uint32_t heap_ind_old = NU_ALIGN_UP(heap_ind, NU_HEAP_ALIGN);
30+
uint32_t heap_ind_new = NU_ALIGN_UP(heap_ind_old + incr, NU_HEAP_ALIGN);
2831

2932
if (heap_ind_new > &__mbed_krbs_start) {
3033
errno = ENOMEM;

targets/TARGET_NUVOTON/TARGET_NUC472/sleep.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
#include "objects.h"
2626
#include "PeripheralPins.h"
2727

28-
void us_ticker_prepare_sleep(struct sleep_s *obj);
29-
void us_ticker_wakeup_from_sleep(struct sleep_s *obj);
3028
static void mbed_enter_sleep(struct sleep_s *obj);
3129
static void mbed_exit_sleep(struct sleep_s *obj);
3230

@@ -77,15 +75,6 @@ static void mbed_enter_sleep(struct sleep_s *obj)
7775
}
7876
// TODO: Check if other peripherals allow entering power-down mode
7977

80-
obj->start_us = lp_ticker_read();
81-
// Let us_ticker prepare for power-down or reject it.
82-
us_ticker_prepare_sleep(obj);
83-
84-
// NOTE(STALE): To pass mbed-drivers test, timer requires to be fine-grained, so its implementation needs HIRC rather than LIRC/LXT as its clock source.
85-
// But as CLK_PowerDown()/CLK_Idle() is called, HIRC will be disabled and timer cannot keep counting and alarm. To overcome the dilemma,
86-
// just make CPU halt and compromise power saving.
87-
// NOTE: As CLK_PowerDown()/CLK_Idle() is called, HIRC/HXT will be disabled in normal mode, but not in ICE mode. This may cause confusion in development.
88-
8978
if (obj->powerdown) { // Power-down mode (HIRC/HXT disabled, LIRC/LXT enabled)
9079
SYS_UnlockReg();
9180
CLK_PowerDown();
@@ -103,11 +92,6 @@ static void mbed_enter_sleep(struct sleep_s *obj)
10392
__NOP();
10493
__NOP();
10594
__NOP();
106-
107-
obj->end_us = lp_ticker_read();
108-
obj->period_us = (obj->end_us > obj->start_us) ? (obj->end_us - obj->start_us) : (uint32_t) ((uint64_t) obj->end_us + 0xFFFFFFFFu - obj->start_us);
109-
// Let us_ticker recover from power-down.
110-
us_ticker_wakeup_from_sleep(obj);
11195
}
11296

11397
static void mbed_exit_sleep(struct sleep_s *obj)

0 commit comments

Comments
 (0)