Skip to content

Commit 78baa6f

Browse files
committed
Clean up GD32 flash/PTP debug logging
Normalizes flash state enum names in the H7 flashcode path, tightens debug format usage with explicit casts, and keeps the GD32 include for IWYU. Also adds the missing GD32F450 9VEE (512KB) device mapping in gd32.json and introduces a new gd32_debug.h header.
1 parent bf40367 commit 78baa6f

4 files changed

Lines changed: 51 additions & 17 deletions

File tree

common/scripts/gd32/gd32.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@
5151
"512": "GD32F407XEXX"
5252
}
5353
},
54+
"9VEE": {
55+
"series": "GD32F450",
56+
"flash": {
57+
"512": "GD32F450VEXX"
58+
}
59+
},
5460
"9VIE": {
5561
"series": "GD32F450",
5662
"flash": {

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@
2424
*/
2525

2626
#include <cstdint>
27-
#include <stdio.h>
2827
#include <cstring>
2928
#include <cassert>
3029

3130
#include "flashcode.h"
32-
#include "gd32.h"
31+
#include "gd32.h" // IWYU pragma: keep
3332
#include "firmware/debug/debug_debug.h"
3433

3534
namespace {
@@ -38,7 +37,7 @@ constexpr auto kFlashSectorSize = 4096U;
3837
// The flash page size is 4KB for bank1
3938
constexpr auto kBanK1FlashPage = (4U * 1024U);
4039

41-
enum class State { kIdle, ERASE_BUSY, ERASE_PROGAM, WRITE_BUSY, WRITE_PROGRAM, ERROR };
40+
enum class State { kIdle, kEraseBusy, kEraseProgam, kWriteBusy, kWriteProgram, kError };
4241

4342
State s_state = State::kIdle;
4443
uint32_t s_page;
@@ -60,7 +59,7 @@ uint32_t FlashCode::GetSectorSize() const {
6059

6160
bool FlashCode::Read(uint32_t offset, uint32_t length, uint8_t* buffer, Result& result) {
6261
DEBUG_ENTRY();
63-
DEBUG_PRINTF("offset=%p[%d], len=%u[%d], data=%p[%d]", offset, (((uint32_t)(offset) & 0x3) == 0), length, (((uint32_t)(length) & 0x3) == 0), buffer, (((uint32_t)(buffer) & 0x3) == 0));
62+
DEBUG_PRINTF("offset=%x, len=%u, data=%p", static_cast<unsigned>(offset), static_cast<unsigned>(length), reinterpret_cast<void*>(buffer));
6463

6564
const auto* src = reinterpret_cast<uint32_t*>(offset + FLASH_BASE);
6665
auto* dst = reinterpret_cast<uint32_t*>(buffer);
@@ -87,11 +86,11 @@ bool FlashCode::Erase(uint32_t offset, uint32_t length, flashcode::Result& resul
8786
s_page = offset + FLASH_BASE;
8887
s_length = length;
8988
fmc_unlock();
90-
s_state = State::ERASE_BUSY;
89+
s_state = State::kEraseBusy;
9190
DEBUG_EXIT();
9291
return false;
9392
break;
94-
case State::ERASE_BUSY:
93+
case State::kEraseBusy:
9594
if (SET == fmc_flag_get(FMC_FLAG_BUSY)) {
9695
DEBUG_EXIT();
9796
return false;
@@ -104,21 +103,21 @@ bool FlashCode::Erase(uint32_t offset, uint32_t length, flashcode::Result& resul
104103
return true;
105104
}
106105

107-
s_state = State::ERASE_PROGAM;
106+
s_state = State::kEraseProgam;
108107
DEBUG_EXIT();
109108
return false;
110109
break;
111-
case State::ERASE_PROGAM:
110+
case State::kEraseProgam:
112111
if (s_length > 0) {
113-
DEBUG_PRINTF("s_nPage=%p", s_page);
112+
DEBUG_PRINTF("s_page=%p", reinterpret_cast<void*>(s_page));
114113

115114
fmc_sector_erase(s_page);
116115

117116
s_length -= kBanK1FlashPage;
118117
s_page += kBanK1FlashPage;
119118
}
120119

121-
s_state = State::ERASE_BUSY;
120+
s_state = State::kEraseBusy;
122121
DEBUG_EXIT();
123122
return false;
124123
break;
@@ -134,7 +133,7 @@ bool FlashCode::Erase(uint32_t offset, uint32_t length, flashcode::Result& resul
134133
}
135134

136135
bool FlashCode::Write(uint32_t offset, uint32_t length, const uint8_t* buffer, flashcode::Result& result) {
137-
if ((s_state == State::WRITE_PROGRAM) || (s_state == State::WRITE_BUSY)) {
136+
if ((s_state == State::kWriteProgram) || (s_state == State::kWriteBusy)) {
138137
} else {
139138
DEBUG_ENTRY();
140139
}
@@ -147,11 +146,11 @@ bool FlashCode::Write(uint32_t offset, uint32_t length, const uint8_t* buffer, f
147146
s_data = const_cast<uint32_t*>(reinterpret_cast<const uint32_t*>(buffer));
148147
s_length = length;
149148
fmc_unlock();
150-
s_state = State::WRITE_BUSY;
149+
s_state = State::kWriteBusy;
151150
DEBUG_EXIT();
152151
return false;
153152
break;
154-
case State::WRITE_BUSY:
153+
case State::kWriteBusy:
155154
if (SET == fmc_flag_get(FMC_FLAG_BUSY)) {
156155
DEBUG_EXIT();
157156
return false;
@@ -171,10 +170,10 @@ bool FlashCode::Write(uint32_t offset, uint32_t length, const uint8_t* buffer, f
171170
return true;
172171
}
173172

174-
s_state = State::WRITE_PROGRAM;
173+
s_state = State::kWriteProgram;
175174
return false;
176175
break;
177-
case State::WRITE_PROGRAM:
176+
case State::kWriteProgram:
178177
if (s_length >= 4) {
179178
if (FMC_READY == fmc_ready_wait(0xFF)) {
180179
/* set the PG bit to start program */
@@ -193,7 +192,7 @@ bool FlashCode::Write(uint32_t offset, uint32_t length, const uint8_t* buffer, f
193192
} else if (s_length > 0) {
194193
DEBUG_PUTS("Error!");
195194
}
196-
s_state = State::WRITE_BUSY;
195+
s_state = State::kWriteBusy;
197196
return false;
198197
break;
199198
default:

lib-gd32/device/enet/ptp/gd32_ptp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void Gd32PtpStart() {
9393
DEBUG_ENTRY();
9494
DEBUG_PRINTF("PTP_TICK=%u", gd32::ptp::kPtpTick);
9595
DEBUG_PRINTF("ADJ_FREQ_BASE_INCREMENT=%u", gd32::ptp::kAdjFreqBaseIncrement);
96-
DEBUG_PRINTF("ADJ_FREQ_BASE_ADDEND=x%X", gd32::ptp::kAdjFreqBaseAddend);
96+
DEBUG_PRINTF("ADJ_FREQ_BASE_ADDEND=x%X", static_cast<unsigned>(gd32::ptp::kAdjFreqBaseAddend));
9797

9898
struct tm tmbuf;
9999
memset(&tmbuf, 0, sizeof(struct tm));

lib-gd32/include/gd32_debug.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @file gd32_debug.h
3+
*
4+
*/
5+
/* Copyright (C) 2026 by Arjan van Vught mailto:info@gd32-dmx.org
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
26+
#ifndef GD32_DEBUG_H_
27+
#define GD32_DEBUG_H_
28+
29+
#endif // GD32_DEBUG_H_

0 commit comments

Comments
 (0)