Skip to content

Commit 7932323

Browse files
committed
Harden GD32 time source selection and guards
Refines GD32 time backend wiring so only explicitly selected time implementations are built, and removes default SysTick inclusion when flags are present. Adds compile-time guards to enforce valid backend/PTP combinations, updates the SysTick millisecond symbol usage, and aligns timer-based time code with the shared timer interface (IRQ naming, startup entry point, and debug hooks). Also includes small type-safety/cleanup fixes in settimeofday paths.
1 parent c2d3bd4 commit 7932323

4 files changed

Lines changed: 38 additions & 29 deletions

File tree

lib-clib/Makefile.GD32

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,12 @@ ifneq ($(MAKE_FLAGS),)
1111
ifeq ($(findstring CONFIG_TIME_USE_TIMER,$(MAKE_FLAGS)), CONFIG_TIME_USE_TIMER)
1212
EXTRA_SRCDIR+=src/gd32/time_timer
1313
else
14-
EXTRA_SRCDIR+=src/gd32/time_systick
14+
ifeq ($(findstring CONFIG_TIME_USE_SYSTICK,$(MAKE_FLAGS)), CONFIG_TIME_USE_SYSTICK)
15+
EXTRA_SRCDIR+=src/gd32/time_systick
16+
endif
1517
endif
1618
endif
17-
ifeq ($(findstring CONFIG_HAVE_CRC32_HW,$(MAKE_FLAGS)), CONFIG_HAVE_CRC32_HW)
18-
EXTRA_SRCDIR+=src/gd32/crc32
19-
endif
2019
else
21-
EXTRA_SRCDIR+=src/gd32/time_systick
22-
2320
EXTRA_SRCDIR+=src/gd32/time_timer
2421
DEFINES+=CONFIG_TIME_USE_TIMER
2522
endif

lib-clib/src/gd32/time_ptp/time.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@
2323
* THE SOFTWARE.
2424
*/
2525

26-
#include <cstdint>
26+
#if !defined(CONFIG_NET_ENABLE_PTP)
27+
#error
28+
#endif
29+
2730
#pragma GCC push_options
2831
#pragma GCC optimize("O2")
2932

33+
#include <cstdint>
3034
#include <time.h>
3135
#include <sys/time.h>
3236
#include <cassert>
@@ -65,10 +69,10 @@ int gettimeofday(struct timeval* tv, [[maybe_unused]] struct timezone* tz) {
6569
}
6670

6771
int settimeofday(const struct timeval* tv, [[maybe_unused]] const struct timezone* tz) {
68-
assert(tv != 0);
72+
assert(tv != nullptr);
6973

7074
const uint32_t kSign = ENET_PTP_ADD_TO_TIME;
71-
const uint32_t kSecond = static_cast<uint32_t>(tv->tv_sec);
75+
const auto kSecond = static_cast<uint32_t>(tv->tv_sec);
7276
const uint32_t kNanoSecond = static_cast<uint32_t>(tv->tv_usec) * 1000U;
7377
const auto kSubSecond = gd32::PtpNanosecond2Subsecond(kNanoSecond);
7478

lib-clib/src/gd32/time_systick/time.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,18 @@
2323
* THE SOFTWARE.
2424
*/
2525

26+
#if defined(CONFIG_TIME_USE_TIMER) || defined(CONFIG_NET_ENABLE_PTP)
27+
#error
28+
#endif
29+
2630
#pragma GCC push_options
2731
#pragma GCC optimize("O2")
2832

2933
#include <sys/time.h>
3034
#include <cstdint>
3135
#include <cassert>
3236

33-
extern volatile uint32_t gv_nSysTickMillis;
37+
extern volatile uint32_t gv_systick_millis;
3438

3539
static uint32_t previous_systick_millis;
3640
static struct timeval s_tv;
@@ -44,7 +48,7 @@ extern "C" {
4448
int gettimeofday(struct timeval* tv, __attribute__((unused)) struct timezone* tz) {
4549
assert(tv != 0);
4650

47-
const auto kCurrentSysTickMillis = gv_nSysTickMillis;
51+
const auto kCurrentSysTickMillis = gv_systick_millis;
4852

4953
uint32_t millis_elapsed;
5054

@@ -79,7 +83,7 @@ int settimeofday(const struct timeval* tv, __attribute__((unused)) const struct
7983
struct timeval g;
8084
gettimeofday(&g, nullptr);
8185

82-
previous_systick_millis = gv_nSysTickMillis;
86+
previous_systick_millis = gv_systick_millis;
8387

8488
s_tv.tv_sec = tv->tv_sec;
8589
s_tv.tv_usec = tv->tv_usec;

lib-clib/src/gd32/time_timer/time.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,21 @@
2323
* THE SOFTWARE.
2424
*/
2525

26-
#include <ctime>
26+
#if !defined(CONFIG_TIME_USE_TIMER)
27+
#error
28+
#endif
29+
2730
#pragma GCC push_options
2831
#pragma GCC optimize("O2")
2932

33+
#include <ctime>
3034
#include <sys/time.h>
3135
#include <cstdint>
3236
#include <cassert>
3337

34-
#include "gd32.h"
38+
#include "gd32.h" // IWYU pragma: keep
39+
#include "gd32_timers.h"
40+
#include "gd32_debug.h"
3541

3642
#if defined(GD32H7XX)
3743
#define TIMERx TIMER16
@@ -41,17 +47,14 @@
4147
#define TIMERx TIMER7
4248
#define RCU_TIMERx RCU_TIMER7
4349
#if defined(GD32F10X) || defined(GD32F30X)
44-
#define TIMERx_IRQn TIMER7_IRQn
50+
#define TIMERx_IRQn TIMER7_UP_IRQn
4551
#else
4652
#define TIMERx_IRQn TIMER7_UP_TIMER12_IRQn
4753
#endif
4854
#endif
4955

50-
extern struct HwTimersSeconds gv_seconds;
51-
5256
extern "C" {
53-
#if !defined(CONFIG_NET_ENABLE_PTP)
54-
#if defined(CONFIG_TIME_USE_TIMER)
57+
#if defined(CONFIG_TIME_USE_TIMER) // Include IRQ handler when used only
5558
#if defined(GD32H7XX)
5659
void TIMER16_IRQHandler() {
5760
#elif defined(GD32F10X) || defined(GD32F30X)
@@ -62,20 +65,18 @@ void TIMER7_UP_TIMER12_IRQHandler() {
6265
const auto nIntFlag = TIMER_INTF(TIMERx);
6366

6467
if ((nIntFlag & TIMER_INT_FLAG_UP) == TIMER_INT_FLAG_UP) {
65-
gv_seconds.timeval++;
68+
gv_seconds.timeval = gv_seconds.timeval + 1;
6669
}
6770

68-
TIMER_INTF(TIMERx) = static_cast<uint32_t>(~nIntFlag);
71+
TIMER_INTF(TIMERx) = ~nIntFlag;
6972
}
7073
#endif
71-
#endif
7274
}
7375

74-
#if defined(GD32H7XX)
75-
void Timer16Config() {
76-
#else
77-
void Timer7Config() {
78-
#endif
76+
namespace gd32::timers::timer_time {
77+
void Start() {
78+
GD32_TIMERS_DEBUG_ENTRY();
79+
7980
gv_seconds.timeval = 0;
8081

8182
rcu_periph_clock_enable(RCU_TIMERx);
@@ -98,7 +99,10 @@ void Timer7Config() {
9899
NVIC_EnableIRQ(TIMERx_IRQn);
99100

100101
timer_enable(TIMERx);
102+
103+
GD32_TIMERS_DEBUG_EXIT();
101104
}
105+
} // namespace gd32::timers::timer_time
102106

103107
extern "C" {
104108
/*
@@ -127,8 +131,8 @@ int settimeofday(const struct timeval* tv, __attribute__((unused)) const struct
127131
assert(tv != nullptr);
128132

129133
// Disable the timer interrupt to prevent it from triggering while we adjust the counter
130-
TIMER_DMAINTEN(TIMERx) &= static_cast<uint32_t>(~TIMER_INT_UP);
131-
TIMER_CTL0(TIMERx) &= static_cast<uint32_t>(~TIMER_CTL0_CEN);
134+
TIMER_DMAINTEN(TIMERx) &= (~TIMER_INT_UP);
135+
TIMER_CTL0(TIMERx) &= (~TIMER_CTL0_CEN);
132136

133137
gv_seconds.timeval = static_cast<uint32_t>(tv->tv_sec);
134138
TIMER_CNT(TIMERx) = (static_cast<uint32_t>(tv->tv_usec) / 100U) % 10000U;

0 commit comments

Comments
 (0)