Skip to content

Commit 7e1443a

Browse files
authored
Merge pull request #12424 from OpenNuvoton/nuvoton_gpio-irq_rtc-lxt
Nuvoton: Fix GPIO IRQ and RTC
2 parents d7f3341 + 769781c commit 7e1443a

File tree

28 files changed

+41855
-42909
lines changed

28 files changed

+41855
-42909
lines changed

targets/TARGET_NUVOTON/TARGET_M2351/PeripheralPins.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ const PinMap PinMap_GPIO[] = {
138138
{PG_14, GPIO_G, SYS_GPG_MFPH_PG14MFP_GPIO},
139139
{PG_15, GPIO_G, SYS_GPG_MFPH_PG15MFP_GPIO},
140140

141-
// GPIO H MFP
141+
// GPIO H MFP
142142
{PH_4, GPIO_H, SYS_GPH_MFPL_PH4MFP_GPIO},
143143
{PH_5, GPIO_H, SYS_GPH_MFPL_PH5MFP_GPIO},
144144
{PH_6, GPIO_H, SYS_GPH_MFPL_PH6MFP_GPIO},
@@ -646,4 +646,3 @@ const PinMap PinMap_CAN_RD[] = {
646646

647647
{NC, NC, 0}
648648
};
649-

targets/TARGET_NUVOTON/TARGET_M2351/TARGET_M23_NS/TARGET_NU_PFM_M2351_NPSA_NS/TARGET_NU_PREBUILD_SECURE/NuMaker-mbed-TZ-secure-example.hex

Lines changed: 242 additions & 243 deletions
Large diffs are not rendered by default.

targets/TARGET_NUVOTON/TARGET_M2351/TARGET_M23_NS/TARGET_NU_PFM_M2351_NS/TARGET_NU_PREBUILD_SECURE/crypto_access_control.hex

Lines changed: 8266 additions & 8494 deletions
Large diffs are not rendered by default.

targets/TARGET_NUVOTON/TARGET_M2351/TARGET_M23_NS/TARGET_NU_PFM_M2351_NS/TARGET_NU_PREBUILD_SECURE/spm_client.hex

Lines changed: 8243 additions & 8471 deletions
Large diffs are not rendered by default.

targets/TARGET_NUVOTON/TARGET_M2351/TARGET_M23_NS/TARGET_NU_PFM_M2351_NS/TARGET_NU_PREBUILD_SECURE/spm_server.hex

Lines changed: 8401 additions & 8631 deletions
Large diffs are not rendered by default.

targets/TARGET_NUVOTON/TARGET_M2351/TARGET_M23_NS/TARGET_NU_PFM_M2351_NS/TARGET_NU_PREBUILD_SECURE/spm_smoke.hex

Lines changed: 8240 additions & 8468 deletions
Large diffs are not rendered by default.

targets/TARGET_NUVOTON/TARGET_M2351/TARGET_M23_NS/TARGET_NU_PFM_M2351_NS/TARGET_NU_PREBUILD_SECURE/tfm.hex

Lines changed: 8213 additions & 8440 deletions
Large diffs are not rendered by default.

targets/TARGET_NUVOTON/TARGET_M2351/crypto/crypto-misc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ __STATIC_INLINE CRPT_T *CRYPTO_MODBASE(void)
5252
#if defined(SCU_INIT_PNSSET1_VAL) && (SCU_INIT_PNSSET1_VAL & (1<<18))
5353
return CRPT_NS;
5454
#else
55-
return CRPT;
55+
return CRPT_S;
5656
#endif
5757
}
5858

targets/TARGET_NUVOTON/TARGET_M2351/gpio_irq_api.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,13 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
182182
obj->irq_types &= ~irq_type;
183183
}
184184

185-
/* Update irq types */
185+
/* Update irq types:
186+
*
187+
* Implementations of GPIO_EnableInt(...) are inconsistent: disable or not irq type not enabled.
188+
* For consistency, disable GPIO_INT_BOTH_EDGE and then enable OR'ed irq types, GPIO_INT_RISING,
189+
* GPIO_INT_FALLING, or both.
190+
*/
191+
GPIO_DisableInt(gpio_base, pin_index);
186192
GPIO_EnableInt(gpio_base, pin_index, obj->irq_types);
187193
}
188194

targets/TARGET_NUVOTON/TARGET_M2351/rtc_api.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@
2929
#include "tfm_ns_lock.h"
3030
#endif
3131

32-
/* NOTE: BSP RTC driver judges secure/non-secure RTC by PC. This implementation cannot support non-secure RTC
33-
* controlled by secure executable. A better way would be that secure/non-secure RTC base is passed
34-
* to RTC API as an argument like most other APIs. With BSP RTC driver unchanged, we must enforce
35-
* secure RTC. */
32+
/* Secure attribution of RTC
33+
*
34+
* We need RTC to be secure for security concern.
35+
*
36+
* On M2351, configured to secure
37+
* On M2351, hard-wired to secure
38+
*/
3639
#if defined(SCU_INIT_PNSSET2_VAL) && (SCU_INIT_PNSSET2_VAL & (1 << 1))
3740
#error("Limited by BSP/RTC, we can only support secure RTC.")
3841
#endif
@@ -70,7 +73,7 @@ void rtc_write(time_t t)
7073
*
7174
* NOTE: This dependents on real hardware.
7275
*/
73-
#define NU_RTCCLK_PER_SEC ((CLK->CLKSEL3 & CLK_CLKSEL3_SC0SEL_Msk) ? __LIRC : __LXT)
76+
#define NU_RTCCLK_PER_SEC (__LXT)
7477

7578
/* Strategy for implementation of RTC HAL
7679
*
@@ -124,7 +127,7 @@ static time_t t_write = 0;
124127
/* Convert date time from H/W RTC to struct TM */
125128
static void rtc_convert_datetime_hwrtc_to_tm(struct tm *datetime_tm, const S_RTC_TIME_DATA_T *datetime_hwrtc);
126129

127-
static const struct nu_modinit_s rtc_modinit = {RTC_0, RTC_MODULE, 0, 0, 0, RTC_IRQn, NULL};
130+
static const struct nu_modinit_s rtc_modinit = {RTC_0, RTC_MODULE, CLK_CLKSEL3_RTCSEL_LXT, 0, 0, RTC_IRQn, NULL};
128131

129132
static void rtc_init_impl(void);
130133
static void rtc_free_impl(void);
@@ -151,11 +154,10 @@ static void rtc_free_impl(void)
151154

152155
static int32_t rtc_isenabled_impl(void)
153156
{
154-
// NOTE: To access (RTC) registers, clock must be enabled first.
155-
if (! (CLK->APBCLK0 & CLK_APBCLK0_RTCCKEN_Msk)) {
156-
// Enable IP clock
157-
CLK_EnableModuleClock_S(rtc_modinit.clkidx);
158-
}
157+
// To access (RTC) registers, clock must be enabled first.
158+
// For TZ, with RTC being secure, we needn't call the secure gateway versions.
159+
CLK_EnableModuleClock(rtc_modinit.clkidx);
160+
CLK_SetModuleClock(rtc_modinit.clkidx, rtc_modinit.clksrc, rtc_modinit.clkdiv);
159161

160162
RTC_T *rtc_base = (RTC_T *) NU_MODBASE(rtc_modinit.modname);
161163

targets/TARGET_NUVOTON/TARGET_M251/gpio_irq_api.c

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "pinmap.h"
2626
#include "PeripheralPins.h"
2727
#include "nu_bitutil.h"
28+
#include "mbed_assert.h"
2829

2930
#define NU_MAX_PIN_PER_PORT 16
3031

@@ -87,6 +88,7 @@ int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32
8788
}
8889

8990
obj->pin = pin;
91+
obj->irq_types = 0;
9092
obj->irq_handler = (uint32_t) handler;
9193
obj->irq_id = id;
9294

@@ -154,27 +156,38 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
154156
uint32_t port_index = NU_PINPORT(obj->pin);
155157
GPIO_T *gpio_base = NU_PORT_BASE(port_index);
156158

159+
/* We assume BSP has such coding so that we can easily add/remove either irq type. */
160+
MBED_STATIC_ASSERT(GPIO_INT_BOTH_EDGE == (GPIO_INT_RISING | GPIO_INT_FALLING),
161+
"GPIO_INT_BOTH_EDGE must be bitwise OR of GPIO_INT_RISING and GPIO_INT_FALLING");
162+
uint32_t irq_type;
157163
switch (event) {
158-
case IRQ_RISE:
159-
if (enable) {
160-
GPIO_EnableInt(gpio_base, pin_index, GPIO_INT_RISING);
161-
} else {
162-
gpio_base->INTEN &= ~(GPIO_INT_RISING << pin_index);
163-
}
164-
break;
164+
case IRQ_RISE:
165+
irq_type = GPIO_INT_RISING;
166+
break;
165167

166-
case IRQ_FALL:
167-
if (enable) {
168-
GPIO_EnableInt(gpio_base, pin_index, GPIO_INT_FALLING);
169-
} else {
170-
gpio_base->INTEN &= ~(GPIO_INT_FALLING << pin_index);
171-
}
172-
break;
168+
case IRQ_FALL:
169+
irq_type = GPIO_INT_FALLING;
170+
break;
173171

174-
case IRQ_NONE:
175-
default:
176-
break;
172+
default:
173+
irq_type = 0;
177174
}
175+
176+
/* We can handle invalid/null irq type. */
177+
if (enable) {
178+
obj->irq_types |= irq_type;
179+
} else {
180+
obj->irq_types &= ~irq_type;
181+
}
182+
183+
/* Update irq types:
184+
*
185+
* Implementations of GPIO_EnableInt(...) are inconsistent: disable or not irq type not enabled.
186+
* For consistency, disable GPIO_INT_BOTH_EDGE and then enable OR'ed irq types, GPIO_INT_RISING,
187+
* GPIO_INT_FALLING, or both.
188+
*/
189+
GPIO_DisableInt(gpio_base, pin_index);
190+
GPIO_EnableInt(gpio_base, pin_index, obj->irq_types);
178191
}
179192

180193
void gpio_irq_enable(gpio_irq_t *obj)

targets/TARGET_NUVOTON/TARGET_M251/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ extern "C" {
3131

3232
struct gpio_irq_s {
3333
PinName pin;
34+
uint32_t irq_types;
3435
uint32_t irq_handler;
3536
uint32_t irq_id;
3637
struct gpio_irq_s *next;

targets/TARGET_NUVOTON/TARGET_M251/rtc_api.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,10 @@ void rtc_free(void)
107107

108108
int rtc_isenabled(void)
109109
{
110-
// NOTE: To access (RTC) registers, clock must be enabled first.
111-
if (! (CLK->APBCLK0 & CLK_APBCLK0_RTCCKEN_Msk)) {
112-
// Enable IP clock
113-
CLK_EnableModuleClock(rtc_modinit.clkidx);
114-
}
110+
// To access (RTC) registers, clock must be enabled first.
111+
// For TZ, with RTC being secure, we needn't call the secure gateway versions.
112+
CLK_EnableModuleClock(rtc_modinit.clkidx);
113+
CLK_SetModuleClock(rtc_modinit.clkidx, rtc_modinit.clksrc, rtc_modinit.clkdiv);
115114

116115
RTC_T *rtc_base = (RTC_T *) NU_MODBASE(rtc_modinit.modname);
117116

targets/TARGET_NUVOTON/TARGET_M261/gpio_irq_api.c

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "pinmap.h"
2525
#include "PeripheralPins.h"
2626
#include "nu_bitutil.h"
27+
#include "mbed_assert.h"
2728

2829
#define NU_MAX_PIN_PER_PORT 16
2930

@@ -88,6 +89,7 @@ int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32
8889
}
8990

9091
obj->pin = pin;
92+
obj->irq_types = 0;
9193
obj->irq_handler = (uint32_t) handler;
9294
obj->irq_id = id;
9395

@@ -155,27 +157,38 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
155157
uint32_t port_index = NU_PINNAME_TO_PORT(obj->pin);
156158
GPIO_T *gpio_base = NU_PORT_BASE(port_index);
157159

160+
/* We assume BSP has such coding so that we can easily add/remove either irq type. */
161+
MBED_STATIC_ASSERT(GPIO_INT_BOTH_EDGE == (GPIO_INT_RISING | GPIO_INT_FALLING),
162+
"GPIO_INT_BOTH_EDGE must be bitwise OR of GPIO_INT_RISING and GPIO_INT_FALLING");
163+
uint32_t irq_type;
158164
switch (event) {
159-
case IRQ_RISE:
160-
if (enable) {
161-
GPIO_EnableInt(gpio_base, pin_index, GPIO_INT_RISING);
162-
} else {
163-
gpio_base->INTEN &= ~(GPIO_INT_RISING << pin_index);
164-
}
165-
break;
165+
case IRQ_RISE:
166+
irq_type = GPIO_INT_RISING;
167+
break;
166168

167-
case IRQ_FALL:
168-
if (enable) {
169-
GPIO_EnableInt(gpio_base, pin_index, GPIO_INT_FALLING);
170-
} else {
171-
gpio_base->INTEN &= ~(GPIO_INT_FALLING << pin_index);
172-
}
173-
break;
169+
case IRQ_FALL:
170+
irq_type = GPIO_INT_FALLING;
171+
break;
174172

175-
case IRQ_NONE:
176-
default:
177-
break;
173+
default:
174+
irq_type = 0;
178175
}
176+
177+
/* We can handle invalid/null irq type. */
178+
if (enable) {
179+
obj->irq_types |= irq_type;
180+
} else {
181+
obj->irq_types &= ~irq_type;
182+
}
183+
184+
/* Update irq types:
185+
*
186+
* Implementations of GPIO_EnableInt(...) are inconsistent: disable or not irq type not enabled.
187+
* For consistency, disable GPIO_INT_BOTH_EDGE and then enable OR'ed irq types, GPIO_INT_RISING,
188+
* GPIO_INT_FALLING, or both.
189+
*/
190+
GPIO_DisableInt(gpio_base, pin_index);
191+
GPIO_EnableInt(gpio_base, pin_index, obj->irq_types);
179192
}
180193

181194
void gpio_irq_enable(gpio_irq_t *obj)

targets/TARGET_NUVOTON/TARGET_M261/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extern "C" {
3030

3131
struct gpio_irq_s {
3232
PinName pin;
33+
uint32_t irq_types;
3334
uint32_t irq_handler;
3435
uint32_t irq_id;
3536
struct gpio_irq_s *next;

targets/TARGET_NUVOTON/TARGET_M261/rtc_api.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*
3232
* NOTE: This dependents on real hardware.
3333
*/
34-
#define NU_RTCCLK_PER_SEC ((CLK->CLKSEL3 & CLK_CLKSEL3_SC0SEL_Msk) ? __LIRC : __LXT)
34+
#define NU_RTCCLK_PER_SEC __LXT
3535

3636
/* Strategy for implementation of RTC HAL
3737
*
@@ -85,7 +85,7 @@ static time_t t_write = 0;
8585
/* Convert date time from H/W RTC to struct TM */
8686
static void rtc_convert_datetime_hwrtc_to_tm(struct tm *datetime_tm, const S_RTC_TIME_DATA_T *datetime_hwrtc);
8787

88-
static const struct nu_modinit_s rtc_modinit = {RTC_0, RTC_MODULE, 0, 0, 0, RTC_IRQn, NULL};
88+
static const struct nu_modinit_s rtc_modinit = {RTC_0, RTC_MODULE, CLK_CLKSEL3_RTCSEL_LXT, 0, 0, RTC_IRQn, NULL};
8989

9090
void rtc_init(void)
9191
{
@@ -106,11 +106,10 @@ void rtc_free(void)
106106

107107
int rtc_isenabled(void)
108108
{
109-
// NOTE: To access (RTC) registers, clock must be enabled first.
110-
if (! (CLK->APBCLK0 & CLK_APBCLK0_RTCCKEN_Msk)) {
111-
// Enable IP clock
112-
CLK_EnableModuleClock(rtc_modinit.clkidx);
113-
}
109+
// To access (RTC) registers, clock must be enabled first.
110+
// For TZ, with RTC being secure, we needn't call the secure gateway versions.
111+
CLK_EnableModuleClock(rtc_modinit.clkidx);
112+
CLK_SetModuleClock(rtc_modinit.clkidx, rtc_modinit.clksrc, rtc_modinit.clkdiv);
114113

115114
RTC_T *rtc_base = (RTC_T *) NU_MODBASE(rtc_modinit.modname);
116115

0 commit comments

Comments
 (0)