Skip to content

Commit 82edf05

Browse files
committed
Generalize GD32 FMC and build system
This commit extracts the GD32 flash controller logic into the shared lib-gd32 layer, adds the new gd32_fmc API for blocking and state-machine access, and updates FlashCode to use it across GD32 families. It also removes the old H7xx-specific implementation, adds Makefile/GD32 rule plumbing for the new library layout, and includes a small GD32F10x soft UART timer update.
1 parent 875f407 commit 82edf05

12 files changed

Lines changed: 528 additions & 434 deletions

File tree

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.

lib-flashcodeinstall/Makefile.GD32

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

3-
EXTRA_INCLUDES=
3+
EXTRA_INCLUDES=../lib-gd32/include ../lib-gd32/src
44

5-
EXTRA_SRCDIR=
5+
EXTRA_SRCDIR=src/spi/gd32
66

77
include Rules.mk
88
include ../firmware-template-gd32/lib/Rules.mk
9-

0 commit comments

Comments
 (0)