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")
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
7677namespace gd32 ::timers::timer_time {
7778void 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
107110extern " 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}
0 commit comments