Skip to content

Commit 2f043bb

Browse files
committed
Add GD32 FMC abstraction and build fixes
This change introduces a shared GD32 FMC API and moves flashcode support into lib-gd32, with updated debug helpers and family-specific build rules. It also adds validation flags for the network stack, improves debug bit output, and includes a few compatibility cleanups across common and libc code.
1 parent af75b08 commit 2f043bb

15 files changed

Lines changed: 531 additions & 439 deletions

File tree

common/include/common/utils/utils_string.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ inline float Atof(const char* buffer, uint32_t size) {
8181
}
8282

8383
while (size > 0 && *p >= '0' && *p <= '9') {
84-
result = result * 10.0F + static_cast<float>(*p - '0');
84+
result = (result * 10.0F) + static_cast<float>(*p - '0');
8585
++p;
8686
--size;
8787
}

common/include/firmware/debug/debug_printbits.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
namespace debug {
3636
template <typename T>
3737
requires std::unsigned_integral<T>
38-
inline void PrintBits(T value) {
38+
inline void PrintBits(T value, const char* string = nullptr) {
3939
if constexpr (!config::kDumpEnabled) {
4040
return;
4141
}
@@ -46,7 +46,10 @@ inline void PrintBits(T value) {
4646

4747
static_assert(sizeof(T) <= sizeof(unsigned));
4848

49-
printf("%.*x ", kHexDigits, static_cast<unsigned>(value));
49+
if (string != nullptr) {
50+
printf("%s :", string);
51+
}
52+
printf("0x%.*x ", kHexDigits, static_cast<unsigned>(value));
5053

5154
for (int bit_number = kMaxBitIndex; bit_number >= 0; --bit_number) {
5255
const auto kMask = static_cast<T>(1) << bit_number;

common/make/gd32/Validate.mk

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ endif
2424

2525
ifeq ($(findstring CONFIG_REMOTECONFIG_MINIMUM,$(FLAGS)),CONFIG_REMOTECONFIG_MINIMUM)
2626
DEFINES+=-DCONFIG_NET_APPS_NO_MDNS
27+
DEFINES+=-DCONFIG_UDP_NO_OPTIMIZE
28+
DEFINES+=-DDISABLE_RTC
2729
else
2830
ifeq ($(findstring NO_EMAC,$(FLAGS)),NO_EMAC)
2931
else
@@ -56,8 +58,23 @@ ifdef FATFS_MKFS
5658
DEFINES+=-DCONFIG_FATFS_MKFS
5759
endif
5860

61+
# Hardware Scenario Flag Condition Resulting Compiler Definitions (DEFINES)
62+
# Using RTL8201F ENET_LINK_CHECK is missing -DRTL8201F_LED1_LINK_ALL -DENET_LINK_CHECK_USE_INT (Uses Interrupts)
63+
# Using RTL8201F ENET_LINK_CHECK is present -DRTL8201F_LED1_LINK_ALL
64+
# Other Hardware Any -DENET_LINK_CHECK_REG_POLL (Uses Polling)
65+
66+
ifneq ($(findstring RTL8201F,$(FLAGS)),)
67+
DEFINES+=-DRTL8201F_LED1_LINK_ALL
68+
ifeq ($(findstring ENET_LINK_CHECK,$(FLAGS)),)
69+
DEFINES+=-DENET_LINK_CHECK_USE_INT
70+
endif
71+
else
72+
DEFINES+=-DENET_LINK_CHECK_REG_POLL
73+
endif
74+
5975
$(info $$DEFINES [${DEFINES}])
6076

6177
DEFINES:= $(sort $(DEFINES))
6278

6379
$(info $$DEFINES [${DEFINES}])
80+

lib-clib/src/asctime.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ static const char kWdayName[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "S
3535
static char s_buffer[kMaxAscTime + 1];
3636

3737
extern "C" char* asctime(const struct tm* p_tm) {
38-
if (!p_tm) return nullptr;
38+
if (p_tm == nullptr) {
39+
return nullptr;
40+
}
3941

4042
const char* const kWday = (p_tm->tm_wday >= 0 && p_tm->tm_wday <= 6) ? kWdayName[p_tm->tm_wday] : "???";
4143
const char* const kMon = (p_tm->tm_mon >= 0 && p_tm->tm_mon <= 11) ? kMonName[p_tm->tm_mon] : "???";

lib-flashcode/Makefile.GD32

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
DEFINES =NDEBUG
22

3-
ifneq ($(MAKE_FLAGS),)
4-
ifeq ($(findstring gd32f4xx,$(FAMILY)), gd32f4xx)
5-
EXTRA_SRCDIR=src/gd32/f4xx
6-
else
7-
ifeq ($(findstring gd32h7xx,$(FAMILY)), gd32h7xx)
8-
EXTRA_SRCDIR=src/gd32/h7xx
9-
else
10-
EXTRA_SRCDIR=src/gd32/fmc
11-
endif
12-
endif
13-
else
14-
EXTRA_SRCDIR=src/gd32/fmc
15-
endif
3+
EXTRA_INCLUDES=../lib-gd32/include ../lib-gd32/src
4+
5+
EXTRA_SRCDIR=src/spi/gd32
166

177
include Rules.mk
188
include ../firmware-template-gd32/lib/Rules.mk

lib-flashcode/src/gd32/flashcode.cpp

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

26-
#include <cstdio>
27-
#include <cassert>
26+
#include <cstdint>
27+
#include <cassert>
28+
#include <span>
2829

29-
#include "flashcode.h"
30-
#include "gd32.h"
30+
#include "flashcode.h"
31+
#include "gd32_fmc.h"
32+
#include "gd32.h" // IWYU pragma: keep
33+
34+
namespace {
35+
constexpr uint32_t k1KiB = 1024;
36+
// Backwards compatibility with SPI FLASH
37+
constexpr auto kFlashSectorSize = 4096;
38+
} // namespace
3139

3240
FlashCode::FlashCode() {
3341
FLASHCODE_DEBUG_ENTRY();
@@ -36,16 +44,38 @@ FlashCode::FlashCode() {
3644

3745
detected_ = true;
3846

39-
printf("FMC: %s %u [%u]\n", GetName(), static_cast<unsigned int>(GetSize()), static_cast<unsigned int>(GetSize() / 1024U));
47+
printf("FMC: %s %u [%u]\n", GetName(), static_cast<unsigned>(GetSize()), static_cast<unsigned>(GetSize() / k1KiB));
4048
FLASHCODE_DEBUG_EXIT();
4149
}
4250

43-
FlashCode::~FlashCode() {
44-
FLASHCODE_DEBUG_ENTRY();
51+
const char* FlashCode::GetName() const {
52+
return GD32_MCU_NAME;
53+
}
4554

46-
FLASHCODE_DEBUG_EXIT();
55+
uint32_t FlashCode::GetSize() const {
56+
return FMC_SIZE * k1KiB;
4757
}
4858

49-
const char* FlashCode::GetName() const {
50-
return GD32_MCU_NAME;
59+
uint32_t FlashCode::GetSectorSize() const {
60+
return kFlashSectorSize;
61+
}
62+
63+
bool FlashCode::Read(uint32_t offset, std::span<uint8_t> buffer, flashcode::Result& result) {
64+
const auto kStatus = gd32::fmc::Read(offset, buffer); // Blocking
65+
result = kStatus ? flashcode::Result::kOk : flashcode::Result::kError;
66+
return true;
67+
}
68+
69+
bool FlashCode::Erase(uint32_t offset, uint32_t length, flashcode::Result& result) {
70+
gd32::fmc::Result fmc_result;
71+
const auto kStatus = gd32::fmc::Erase(offset, length, fmc_result); // State-machine
72+
result = (fmc_result == gd32::fmc::Result::kOk) ? flashcode::Result::kOk : flashcode::Result::kError;
73+
return kStatus;
74+
}
75+
76+
bool FlashCode::Write(uint32_t offset, std::span<const uint8_t> buffer, flashcode::Result& result) {
77+
gd32::fmc::Result fmc_result;
78+
const auto kStatus = gd32::fmc::Write(offset, buffer, fmc_result); // State-machine
79+
result = (fmc_result == gd32::fmc::Result::kOk) ? flashcode::Result::kOk : flashcode::Result::kError;
80+
return kStatus;
5181
}

lib-flashcode/src/gd32/h7xx/flashcode.cpp

Lines changed: 0 additions & 219 deletions
This file was deleted.

0 commit comments

Comments
 (0)