Skip to content

Commit 92f3d69

Browse files
committed
Refactor time/FMC code and debug output
This commit mostly cleans up and standardizes low-level platform code: it aligns time API parameter names/signatures, clarifies preprocessor guards/comments, and replaces hardcoded unit literals with shared constants. It also fixes GD32 timer selection/IRQ mapping for F10X/F30X targets, adjusts flash/FMC address range handling in read paths, and tunes debug logging by removing noisy ConfigStore traces while adding targeted FlashCode/FMC diagnostics.
1 parent aa4186c commit 92f3d69

23 files changed

Lines changed: 139 additions & 163 deletions

File tree

common/include/common/utils/utils_units.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ inline constexpr uint32_t kUsPerSecond = 1'000'000U;
3636
inline constexpr uint32_t kNsPerUs = 1'000U;
3737
inline constexpr uint32_t kNsPerMs = 1'000'000U;
3838
inline constexpr uint32_t kNsPerSecond = 1'000'000'000U;
39+
40+
inline constexpr uint32_t k1KiB = 1024;
41+
3942
} // namespace common::units
4043

4144
#endif // COMMON_UTILS_UTILS_UNITS_H_

common/include/firmware/debug/debug_stack.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "timing.h"
3434
#include "firmware/debug/debug_config.h"
35+
#include "common/utils/utils_units.h"
3536

3637
extern unsigned char stack_low;
3738
extern unsigned char _sp; // NOLINT
@@ -78,11 +79,11 @@ inline void Print() {
7879
printf("\x1b[34m");
7980
}
8081

81-
if constexpr (!config::kAssertionsEnabled) {
82-
printf("Stack: Size %uKB, [%p:%p:%p], Used: %u, Free: %u [%u]", static_cast<unsigned>(kSizeBytes / 1024U), reinterpret_cast<const void*>(start_address), reinterpret_cast<const void*>(ptr),
82+
if constexpr (config::kAssertionsEnabled) {
83+
printf("Stack: Size %uKB, [%p:%p:%p], Used: %u, Free: %u [%u]", static_cast<unsigned>(kSizeBytes / common::units::k1KiB), reinterpret_cast<const void*>(start_address), reinterpret_cast<const void*>(ptr),
8384
reinterpret_cast<const void*>(end_address), static_cast<unsigned>(kUsedBytes), static_cast<unsigned>(kFreeBytes), static_cast<unsigned>(kFreePct));
8485
} else {
85-
printf("Stack: Size %uKB, Used: %u, Free: %u", static_cast<unsigned>(kSizeBytes / 1024U), static_cast<unsigned>(kUsedBytes), static_cast<unsigned>(kFreeBytes));
86+
printf("Stack: Size %uKB, Used: %u, Free: %u", static_cast<unsigned>(kSizeBytes / common::units::k1KiB), static_cast<unsigned>(kUsedBytes), static_cast<unsigned>(kFreeBytes));
8687
}
8788
printf("\x1b[39m\n");
8889
}
@@ -95,7 +96,7 @@ inline void Run() {
9596

9697
static uint32_t s_millis_previous;
9798
const auto kMillis = timing::Millis();
98-
if (kMillis - s_millis_previous >= 1000U) {
99+
if (kMillis - s_millis_previous >= common::units::kMsPerSecond) {
99100
s_millis_previous = kMillis;
100101
Print();
101102
}

include/sys/time.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file time.h
33
*
44
*/
5-
/* Copyright (C) 2020 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2020-2026 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -27,31 +27,31 @@
2727
#define SYS_TIME_H_
2828

2929
#ifndef _TIME_T
30-
#define _TIME_T
30+
#define _TIME_T
3131
typedef long time_t;
32-
#endif /* _TIME_T */
32+
#endif /* _TIME_T */
3333

34-
#ifndef _SUSECONDS_T
35-
#define _SUSECONDS_T
34+
#ifndef _SUSECONDS_T
35+
#define _SUSECONDS_T
3636
typedef long suseconds_t;
37-
#endif /* _SUSECONDS_T */
37+
#endif /* _SUSECONDS_T */
3838

39-
struct timeval {
40-
time_t tv_sec; /* seconds */
41-
suseconds_t tv_usec; /* microseconds */
39+
struct timeval { // NOLINT
40+
time_t tv_sec; // seconds
41+
suseconds_t tv_usec; // microseconds
4242
};
4343

44-
struct timezone {
45-
int tz_minuteswest; /* minutes west of Greenwich */
46-
int tz_dsttime; /* type of DST correction */
44+
struct timezone { // NOLINT
45+
int tz_minuteswest; // minutes west of Greenwich
46+
int tz_dsttime; // type of DST correction
4747
};
4848

4949
#ifdef __cplusplus
5050
extern "C" {
5151
#endif
5252

53-
extern int gettimeofday(struct timeval *tv, struct timezone *tz);
54-
extern int settimeofday(const struct timeval *tv, const struct timezone *tz);
53+
extern int gettimeofday(struct timeval* time_val, struct timezone* time_zone); // NOLINT
54+
extern int settimeofday(const struct timeval* time_val, const struct timezone* time_zone); // NOLINT
5555

5656
#ifdef __cplusplus
5757
}

include/time.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ typedef enum {
5959
extern "C" {
6060
#endif
6161

62-
extern time_t time(time_t *t);
62+
extern time_t time(time_t *timer);
6363
extern time_t mktime(struct tm *tm);
6464
extern struct tm *gmtime(const time_t *timep);
6565
extern struct tm *localtime(const time_t *timep);

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
}

0 commit comments

Comments
 (0)