Skip to content

Commit 5007f0a

Browse files
committed
Refactor clib time code and preprocessor tags
Standardize many `#endif` comments across `lib-clib` for clearer conditional blocks, and clean up time-related sources by modernizing comments and renaming `gettimeofday`/`settimeofday` parameters for readability. The timer-backed time implementation was also updated to use macro-based IRQ handler selection and correct timer mapping for GD32H7 vs GD32F10/F30 targets, plus explicit timer init fields (`clockdivision`, `repetitioncounter`) for consistent setup.
1 parent 53aef13 commit 5007f0a

14 files changed

Lines changed: 100 additions & 113 deletions

File tree

lib-clib/src/abort.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#ifdef NDEBUG
66
#undef NDEBUG
7-
#endif
7+
#endif // NDEBUG
88

99
#include <cassert>
1010

lib-clib/src/c++/purecall.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
#ifdef NDEBUG
2727
#undef NDEBUG
28-
#endif
28+
#endif // NDEBUG
2929

3030
#include <cassert>
3131

lib-clib/src/crc32/crc32.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static void make_crc_table() {
133133
}
134134
crc_table_empty = 0;
135135
}
136-
#endif
136+
#endif // CONFIG_DYNAMIC_CRC_TABLE
137137

138138
#define DO_CRC(x) crc = tab[(crc ^ (x)) & 255] ^ (crc >> 8)
139139

@@ -146,7 +146,7 @@ uint32_t crc32(uint32_t crc, const uint8_t *buf, uint32_t len) {
146146
#ifdef CONFIG_DYNAMIC_CRC_TABLE
147147
if (crc_table_empty)
148148
make_crc_table();
149-
#endif
149+
#endif // CONFIG_DYNAMIC_CRC_TABLE
150150
/* Align it */
151151
if (((reinterpret_cast<long>(b)) & 3) && len) {
152152
auto *p = reinterpret_cast<const uint8_t *>(b);

lib-clib/src/gd32/malloc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828

2929
static struct BlockBucket s_block_bucket[] __attribute__((aligned(4))) = {{0x10, 0}, {0x20, 0}, {0x40, 0}, {0x60, 0}, {0x80,0}, {0x100,0}, {0x140,0}, {0x180,0}, {0x200,0}, {0x300,0}, {0x400,0}, {0x500,0}, {0,0}};
3030

31-
#endif // GD32_MALLOC_H_
31+
#endif // GD32_MALLOC_H_

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
#if !defined(CONFIG_NET_ENABLE_PTP)
2727
#error
28-
#endif
28+
#endif // CONFIG_NET_ENABLE_PTP
2929

3030
#pragma GCC push_options
3131
#pragma GCC optimize("O2")
@@ -41,39 +41,39 @@
4141
#define enet_ptp_timestamp_function_config(x) enet_ptp_timestamp_function_config(ENETx, x)
4242
#define enet_ptp_timestamp_update_config(x, y, z) enet_ptp_timestamp_update_config(ENETx, x, y, z)
4343
#define enet_ptp_system_time_get(x) enet_ptp_system_time_get(ENETx, x)
44-
#endif
44+
#endif // GD32H7XX
4545

4646
extern "C" {
4747
/*
4848
* number of seconds and microseconds since the Epoch,
4949
* 1970-01-01 00:00:00 +0000 (UTC).
5050
*/
5151

52-
int gettimeofday(struct timeval* tv, [[maybe_unused]] struct timezone* tz) {
53-
assert(tv != 0);
52+
int gettimeofday(struct timeval* time_val, [[maybe_unused]] struct timezone* time_zone) { // NOLINT
53+
assert(time_val != nullptr);
5454

5555
enet_ptp_systime_struct systime;
5656
enet_ptp_system_time_get(&systime);
5757

58-
tv->tv_sec = static_cast<time_t>(systime.second);
58+
time_val->tv_sec = static_cast<time_t>(systime.second);
5959

60-
#if !defined(GD32F4XX)
60+
#ifndef GD32F4XX
6161
const auto kNanoSecond = systime.nanosecond;
6262
#else
6363
const auto kNanoSecond = gd32::PtpSubsecond2Nanosecond(systime.subsecond);
64-
#endif
64+
#endif // GD32F4XX
6565

66-
tv->tv_usec = static_cast<time_t>(kNanoSecond / 1000U);
66+
time_val->tv_usec = static_cast<time_t>(kNanoSecond / 1000U);
6767

6868
return 0;
6969
}
7070

71-
int settimeofday(const struct timeval* tv, [[maybe_unused]] const struct timezone* tz) {
72-
assert(tv != nullptr);
71+
int settimeofday(const struct timeval* time_val, [[maybe_unused]] const struct timezone* time_zone) { // NOLINT
72+
assert(time_val != nullptr);
7373

7474
const uint32_t kSign = ENET_PTP_ADD_TO_TIME;
75-
const auto kSecond = static_cast<uint32_t>(tv->tv_sec);
76-
const uint32_t kNanoSecond = static_cast<uint32_t>(tv->tv_usec) * 1000U;
75+
const auto kSecond = static_cast<uint32_t>(time_val->tv_sec);
76+
const uint32_t kNanoSecond = static_cast<uint32_t>(time_val->tv_usec) * 1000U;
7777
const auto kSubSecond = gd32::PtpNanosecond2Subsecond(kNanoSecond);
7878

7979
enet_ptp_timestamp_update_config(kSign, kSecond, kSubSecond);
@@ -85,10 +85,8 @@ int settimeofday(const struct timeval* tv, [[maybe_unused]] const struct timezon
8585
return -1;
8686
}
8787

88-
/*
89-
* time() returns the time as the number of seconds since the Epoch,
90-
1970-01-01 00:00:00 +0000 (UTC).
91-
*/
88+
// time() returns the time as the number of seconds since the Epoch,
89+
// 1970-01-01 00:00:00 +0000 (UTC).
9290
time_t time(time_t* __timer) { // NOLINT
9391
struct timeval tv;
9492
gettimeofday(&tv, nullptr);

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

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
#if defined(CONFIG_TIME_USE_TIMER) || defined(CONFIG_NET_ENABLE_PTP)
2727
#error
28-
#endif
28+
#endif // defined(CONFIG_TIME_USE_TIMER) || defined(CONFIG_NET_ENABLE_PTP)
2929

3030
#pragma GCC push_options
3131
#pragma GCC optimize("O2")
@@ -40,13 +40,10 @@ static uint32_t previous_systick_millis;
4040
static struct timeval s_tv;
4141

4242
extern "C" {
43-
/*
44-
* number of seconds and microseconds since the Epoch,
45-
* 1970-01-01 00:00:00 +0000 (UTC).
46-
*/
47-
48-
int gettimeofday(struct timeval* tv, [[maybe_unused]] struct timezone* tz) {
49-
assert(tv != 0);
43+
// number of seconds and microseconds since the Epoch,
44+
// 1970-01-01 00:00:00 +0000 (UTC).
45+
int gettimeofday(struct timeval* time_val, [[maybe_unused]] struct timezone* time_zone) { // NOLINT
46+
assert(time_val != nullptr);
5047

5148
const auto kCurrentSysTickMillis = gv_systick_millis;
5249

@@ -71,38 +68,33 @@ int gettimeofday(struct timeval* tv, [[maybe_unused]] struct timezone* tz) {
7168
s_tv.tv_usec -= 1000000;
7269
}
7370

74-
tv->tv_sec = s_tv.tv_sec;
75-
tv->tv_usec = s_tv.tv_usec;
71+
time_val->tv_sec = s_tv.tv_sec;
72+
time_val->tv_usec = s_tv.tv_usec;
7673

7774
return 0;
7875
}
7976

80-
int settimeofday(const struct timeval* tv, [[maybe_unused]] const struct timezone* tz) {
81-
assert(tv != 0);
82-
83-
struct timeval g;
84-
gettimeofday(&g, nullptr);
77+
int settimeofday(const struct timeval* time_val, [[maybe_unused]] const struct timezone* time_zone) { // NOLINT
78+
assert(time_val != nullptr);
8579

8680
previous_systick_millis = gv_systick_millis;
8781

88-
s_tv.tv_sec = tv->tv_sec;
89-
s_tv.tv_usec = tv->tv_usec;
82+
s_tv.tv_sec = time_val->tv_sec;
83+
s_tv.tv_usec = time_val->tv_usec;
9084

9185
return 0;
9286
}
9387

94-
/*
95-
* time() returns the time as the number of seconds since the Epoch,
96-
1970-01-01 00:00:00 +0000 (UTC).
97-
*/
88+
// time() returns the time as the number of seconds since the Epoch,
89+
// 1970-01-01 00:00:00 +0000 (UTC).
9890
time_t time(time_t* __timer) { // NOLINT
99-
struct timeval tv;
100-
gettimeofday(&tv, nullptr);
91+
struct timeval time_val;
92+
gettimeofday(&time_val, nullptr);
10193

10294
if (__timer != nullptr) {
103-
*__timer = tv.tv_sec;
95+
*__timer = time_val.tv_sec;
10496
}
10597

106-
return tv.tv_sec;
98+
return time_val.tv_sec;
10799
}
108100
}

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

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
#if !defined(CONFIG_TIME_USE_TIMER)
2727
#error
28-
#endif
28+
#endif // CONFIG_TIME_USE_TIMER
2929

3030
#pragma GCC push_options
3131
#pragma GCC optimize("O2")
@@ -39,39 +39,40 @@
3939
#include "gd32_timers.h"
4040
#include "gd32_debug.h"
4141

42+
// H7xx -> TIMER16
43+
// F10x/F30x -> TIMER0
44+
// other -> TIMER7 / TIMER7_UP_TIMER12_IRQn
45+
4246
#if defined(GD32H7XX)
43-
#define TIMERx TIMER16
44-
#define RCU_TIMERx RCU_TIMER16
45-
#define TIMERx_IRQn TIMER16_IRQn
46-
#else
47-
#define TIMERx TIMER7
48-
#define RCU_TIMERx RCU_TIMER7
49-
#if defined(GD32F10X) || defined(GD32F30X)
50-
#define TIMERx_IRQn TIMER7_UP_IRQn
47+
#define TIMERx TIMER16
48+
#define RCU_TIMERx RCU_TIMER16
49+
#define TIMERx_IRQn TIMER16_IRQn
50+
#define TIMERx_IRQ_HANDLER TIMER16_IRQHandler
5151
#else
52-
#define TIMERx_IRQn TIMER7_UP_TIMER12_IRQn
53-
#endif
54-
#endif
52+
#if defined(GD32F10X) || defined(GD32F30X) // TIMER7 does not exist on GD32F107
53+
#define TIMERx TIMER0
54+
#define RCU_TIMERx RCU_TIMER0
55+
#define TIMERx_IRQn TIMER0_UP_IRQn
56+
#define TIMERx_IRQ_HANDLER TIMER0_UP_IRQHandler
57+
#else
58+
#define TIMERx TIMER7
59+
#define RCU_TIMERx RCU_TIMER7
60+
#define TIMERx_IRQn TIMER7_UP_TIMER12_IRQn
61+
#define TIMERx_IRQ_HANDLER TIMER7_UP_TIMER12_IRQHandler
62+
#endif // defined(GD32F10X) || defined(GD32F30X)
63+
#endif // GD32H7XX
5564

56-
extern "C" {
5765
#if defined(CONFIG_TIME_USE_TIMER) // Include IRQ handler when used only
58-
#if defined(GD32H7XX)
59-
void TIMER16_IRQHandler() {
60-
#elif defined(GD32F10X) || defined(GD32F30X)
61-
void TIMER7_IRQHandler() {
62-
#else
63-
void TIMER7_UP_TIMER12_IRQHandler() {
64-
#endif
65-
const auto nIntFlag = TIMER_INTF(TIMERx);
66+
extern "C" void TIMERx_IRQ_HANDLER() {
67+
const auto kIntFlag = TIMER_INTF(TIMERx);
6668

67-
if ((nIntFlag & TIMER_INT_FLAG_UP) == TIMER_INT_FLAG_UP) {
69+
if ((kIntFlag & TIMER_INT_FLAG_UP) == TIMER_INT_FLAG_UP) {
6870
gv_seconds.timeval = gv_seconds.timeval + 1;
6971
}
7072

71-
TIMER_INTF(TIMERx) = ~nIntFlag;
72-
}
73-
#endif
73+
TIMER_INTF(TIMERx) = ~kIntFlag;
7474
}
75+
#endif // CONFIG_TIME_USE_TIMER
7576

7677
namespace gd32::timers::timer_time {
7778
void Start() {
@@ -89,6 +90,8 @@ void Start() {
8990
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
9091
timer_initpara.counterdirection = TIMER_COUNTER_UP;
9192
timer_initpara.period = (10000 - 1); // 1 second
93+
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
94+
timer_initpara.repetitioncounter = 0;
9295
timer_init(TIMERx, &timer_initpara);
9396

9497
timer_interrupt_flag_clear(TIMERx, UINT32_MAX);
@@ -100,42 +103,39 @@ void Start() {
100103

101104
timer_enable(TIMERx);
102105

103-
GD32_TIMERS_DEBUG_EXIT();
106+
GD32_TIMERS_DEBUG_EXIT();
104107
}
105108
} // namespace gd32::timers::timer_time
106109

107110
extern "C" {
108-
/*
109-
* number of seconds and microseconds since the Epoch,
110-
* 1970-01-01 00:00:00 +0000 (UTC).
111-
*/
112-
113-
int gettimeofday(struct timeval* tv, [[maybe_unused]] struct timezone* tz) {
111+
// number of seconds and microseconds since the Epoch,
112+
// 1970-01-01 00:00:00 +0000 (UTC).
113+
int gettimeofday(struct timeval* time_val, [[maybe_unused]] struct timezone* time_zone) { // NOLINT
114114
assert(tv != nullptr);
115115

116116
#if __CORTEX_M == 7
117117
__DMB();
118-
#endif
118+
#endif // __CORTEX_M == 7
119119

120-
tv->tv_sec = static_cast<time_t>(gv_seconds.timeval);
121-
tv->tv_usec = static_cast<time_t>(TIMER_CNT(TIMERx) * 100U);
120+
time_val->tv_sec = static_cast<time_t>(gv_seconds.timeval);
121+
time_val->tv_usec = static_cast<time_t>(TIMER_CNT(TIMERx) * 100U);
122122

123123
#if __CORTEX_M == 7
124124
__ISB();
125-
#endif
125+
#endif // __CORTEX_M == 7
126126

127127
return 0;
128128
}
129129

130-
int settimeofday(const struct timeval* tv, [[maybe_unused]] const struct timezone* tz) {
130+
int settimeofday(const struct timeval* time_val, [[maybe_unused]] const struct timezone* time_zone) { // NOLINT
131131
assert(tv != nullptr);
132132

133133
// Disable the timer interrupt to prevent it from triggering while we adjust the counter
134134
TIMER_DMAINTEN(TIMERx) &= (~TIMER_INT_UP);
135135
TIMER_CTL0(TIMERx) &= (~TIMER_CTL0_CEN);
136136

137-
gv_seconds.timeval = static_cast<uint32_t>(tv->tv_sec);
138-
TIMER_CNT(TIMERx) = (static_cast<uint32_t>(tv->tv_usec) / 100U) % 10000U;
137+
gv_seconds.timeval = static_cast<uint32_t>(time_val->tv_sec);
138+
TIMER_CNT(TIMERx) = (static_cast<uint32_t>(time_val->tv_usec) / 100U) % 10000U;
139139

140140
TIMER_INTF(TIMERx) = UINT32_MAX;
141141
TIMER_DMAINTEN(TIMERx) |= TIMER_INT_UP;
@@ -144,19 +144,16 @@ int settimeofday(const struct timeval* tv, [[maybe_unused]] const struct timezon
144144
return 0;
145145
}
146146

147-
/*
148-
* time() returns the time as the number of seconds since the Epoch,
149-
1970-01-01 00:00:00 +0000 (UTC).
150-
*/
151-
time_t time(time_t* __timer) // NOLINT
152-
{
153-
struct timeval tv;
154-
gettimeofday(&tv, nullptr);
147+
// time() returns the time as the number of seconds since the Epoch,
148+
// 1970-01-01 00:00:00 +0000 (UTC).
149+
time_t time(time_t* __timer) { // NOLINT
150+
struct timeval time_val;
151+
gettimeofday(&time_val, nullptr);
155152

156153
if (__timer != nullptr) {
157-
*__timer = tv.tv_sec;
154+
*__timer = time_val.tv_sec;
158155
}
159156

160-
return tv.tv_sec;
157+
return time_val.tv_sec;
161158
}
162159
}

lib-clib/src/getchar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace console {
4040
int GetChar();
4141
} // namespace console
4242
using console::GetChar;
43-
#endif
43+
#endif // CONFIG_CLIB_USE_UART0
4444

4545
extern "C" int getchar() { // NOLINT
4646
return GetChar();

lib-clib/src/log.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ typedef union {
4040
/* Natural log of 2 */
4141
#ifndef _M_LN2
4242
#define _M_LN2 0.693147180559945309417f
43-
#endif
43+
#endif // _M_LN2
4444

4545
/**
4646
* On success, the function return the base 2 logarithm of x.

0 commit comments

Comments
 (0)