Skip to content

Commit f461918

Browse files
committed
Modernize GD32 code: casts, constexpr, includes
Replace raw memsets and C-style casts with explicit loops and reinterpret_casts in bootloader and vector setup; ensure reset vector and MSP are set with correct pointer casts. Convert several FMC/flash macros to inline constexpr (and keep FMC_SIZE as a macro) and fix sector_name types in fmc_operation to use integer types without redundant casts. Update include handling to prefer -isystem for CMSIS and library headers and add Gd32FirmwareOps.mk to set additional compiler warning flags. Adjust IRQ handler/IRQn defines for GD32F30X_XD variant and simplify TIMER_INTF writes by removing static_casts; remove outdated GCC diagnostic push/pop wrappers in gd32xxxx.h.
1 parent ed4c6d7 commit f461918

8 files changed

Lines changed: 48 additions & 54 deletions

File tree

bootloader-tftp/firmware/main.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,28 +72,32 @@ int main() {
7272
// 1. Disable interrupt response.
7373
__disable_irq();
7474
// 2. Disable all enabled interrupts in NVIC.
75-
memset((uint32_t*)NVIC->ICER, 0xFF, sizeof(NVIC->ICER));
75+
for (auto& reg : NVIC->ICER) {
76+
reg = 0xFFFFFFFF;
77+
}
7678
/* 3. Disable all enabled peripherals which might generate interrupt requests.
7779
* Clear all pending interrupt flags in those peripherals.
7880
* This part is device-dependent, and you can write it by referring to device datasheet.
7981
*/
8082

8183
/* Clear all pending interrupt requests in NVIC. */
82-
memset((uint32_t*)NVIC->ICPR, 0xFF, sizeof(NVIC->ICPR));
84+
for (auto& reg : NVIC->ICPR) {
85+
reg = 0xFFFFFFFF;
86+
}
8387
// 4. Disable SysTick and clear its exception pending bit.
8488
SysTick->CTRL = 0;
8589
SCB->ICSR |= SCB_ICSR_PENDSTCLR_Msk;
8690
// 5. Load the vector table address of user application code in to VTOR.
8791
SCB->VTOR = FLASH_BASE + OFFSET_UIMAGE;
8892
// 6. Use the MSP as the current SP.
8993
// Set the MSP with the value from the vector table used by the application.
90-
__set_MSP(((unsigned int*)(SCB->VTOR))[0]);
94+
__set_MSP((reinterpret_cast<unsigned int*>((SCB->VTOR))[0]));
9195
// In thread mode, enable privileged access and use the MSP as the current SP.
9296
__set_CONTROL(0);
9397
// 7. Enable interrupts.
9498
__enable_irq();
9599
// 8. Call the reset handler
96-
const uint32_t* reset_p = (uint32_t*)(FLASH_BASE + OFFSET_UIMAGE + 4);
100+
const uint32_t* reset_p = reinterpret_cast<uint32_t*>(FLASH_BASE + OFFSET_UIMAGE + 4);
97101
asm volatile("bx %0;" : : "r"(*reset_p));
98102
}
99103

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
$(info "Gd32FirmwareOpts.mk")
2+
3+
GD32FIRMWAREOPS=-Wno-error=unused-parameter -Wno-error=unused-but-set-variable -Wno-error=conversion -Wno-error=old-style-cast

common/make/gd32/Includes.mk

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
$(info "Includes.mk")
22

3-
INCLUDES:=-I./include
4-
INCLUDES+=-I../common/include -I../include
5-
INCLUDES+=-I../firmware-template-gd32/include
6-
INCLUDES+=-I../CMSIS/Core/Include
7-
INCLUDES+=-I../lib-gd32/${FAMILY}/${FAMILY_UC}_standard_peripheral/Include
8-
INCLUDES+=-I../lib-gd32/${FAMILY}/CMSIS/GD/${FAMILY_UC}/Include
9-
INCLUDES+=-I../lib-gd32/include
3+
INCLUDES:=-I../include
4+
INCLUDES+=-isystem ../CMSIS/Core/Include
5+
INCLUDES+=-isystem ../lib-gd32/${FAMILY}/${FAMILY_UC}_standard_peripheral/Include
6+
INCLUDES+=-isystem ../lib-gd32/${FAMILY}/CMSIS/GD/${FAMILY_UC}/Include
7+
INCLUDES+=-isystem ../lib-gd32/include
8+
INCLUDES+=-isystem ../firmware-template-gd32/include
9+
INCLUDES+=-I../common/include
1010
INCLUDES+=-I../lib-hwclock/include
11+
INCLUDES+=-I./include
1112

1213
INCLUDES+=$(addprefix -I,$(EXTRA_INCLUDES))
1314

@@ -104,5 +105,5 @@ ifdef USB_HOST_MSC
104105
INCLUDES+=-I../lib-fatfs
105106
endif
106107

107-
INCLUDES:= $(strip -I../${PROJECT}/include $(sort $(INCLUDES)))
108+
#INCLUDES:= $(strip -I../${PROJECT}/include $(sort $(INCLUDES)))
108109
$(info $$INCLUDES [${INCLUDES}])

lib-flashcode/src/gd32/f4xx/fmc_operation.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fmc_sector_info_struct fmc_sector_info_get(uint32_t addr) {
5353
/* bank0 area */
5454
temp = (addr - FMC_BANK0_START_ADDRESS) / SIZE_16KB;
5555
if (4U > temp) {
56-
sector_info.sector_name = (uint32_t)temp;
56+
sector_info.sector_name = temp;
5757
sector_info.sector_num = CTL_SN(temp);
5858
sector_info.sector_size = SIZE_16KB;
5959
sector_info.sector_start_addr = FMC_BANK0_START_ADDRESS + (SIZE_16KB * temp);
@@ -66,7 +66,7 @@ fmc_sector_info_struct fmc_sector_info_get(uint32_t addr) {
6666
sector_info.sector_end_addr = 0x0801FFFFU;
6767
} else {
6868
temp = (addr - FMC_BANK0_START_ADDRESS) / SIZE_128KB;
69-
sector_info.sector_name = (uint32_t)(temp + 4);
69+
sector_info.sector_name = (temp + 4);
7070
sector_info.sector_num = CTL_SN(temp + 4);
7171
sector_info.sector_size = SIZE_128KB;
7272
sector_info.sector_start_addr = FMC_BANK0_START_ADDRESS + (SIZE_128KB * temp);
@@ -76,7 +76,7 @@ fmc_sector_info_struct fmc_sector_info_get(uint32_t addr) {
7676
/* bank1 area */
7777
temp = (addr - FMC_BANK1_START_ADDRESS) / SIZE_16KB;
7878
if (4U > temp) {
79-
sector_info.sector_name = (uint32_t)(temp + 12);
79+
sector_info.sector_name = (temp + 12);
8080
sector_info.sector_num = CTL_SN(temp + 16);
8181
sector_info.sector_size = SIZE_16KB;
8282
sector_info.sector_start_addr = FMC_BANK0_START_ADDRESS + (SIZE_16KB * temp);
@@ -89,14 +89,14 @@ fmc_sector_info_struct fmc_sector_info_get(uint32_t addr) {
8989
sector_info.sector_end_addr = 0x0811FFFFU;
9090
} else if (64U > temp) {
9191
temp = (addr - FMC_BANK1_START_ADDRESS) / SIZE_128KB;
92-
sector_info.sector_name = (uint32_t)(temp + 16);
92+
sector_info.sector_name = (temp + 16);
9393
sector_info.sector_num = CTL_SN(temp + 20);
9494
sector_info.sector_size = SIZE_128KB;
9595
sector_info.sector_start_addr = FMC_BANK1_START_ADDRESS + (SIZE_128KB * temp);
9696
sector_info.sector_end_addr = sector_info.sector_start_addr + SIZE_128KB - 1;
9797
} else {
9898
temp = (addr - FMC_BANK1_START_ADDRESS) / SIZE_256KB;
99-
sector_info.sector_name = (uint32_t)(temp + 20);
99+
sector_info.sector_name = (temp + 20);
100100
sector_info.sector_num = CTL_SN(temp + 8);
101101
sector_info.sector_size = SIZE_256KB;
102102
sector_info.sector_start_addr = FMC_BANK1_START_ADDRESS + (SIZE_256KB * temp);

lib-flashcode/src/gd32/f4xx/fmc_operation.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,25 @@ typedef struct {
5050
uint32_t sector_end_addr; /*!< the end address of the sector */
5151
} fmc_sector_info_struct;
5252

53-
/* sector size */
54-
#define SIZE_16KB ((uint32_t)0x00004000U) /*!< size of 16KB*/
55-
#define SIZE_64KB ((uint32_t)0x00010000U) /*!< size of 64KB*/
56-
#define SIZE_128KB ((uint32_t)0x00020000U) /*!< size of 128KB*/
57-
#define SIZE_256KB ((uint32_t)0x00040000U) /*!< size of 256KB*/
53+
// sector size
54+
inline constexpr uint32_t SIZE_16KB = 0x00004000U; /*!< size of 16KB*/
55+
inline constexpr uint32_t SIZE_64KB = 0x00010000U; /*!< size of 64KB*/
56+
inline constexpr uint32_t SIZE_128KB = 0x00020000U; /*!< size of 128KB*/
57+
inline constexpr uint32_t SIZE_256KB = 0x00040000U; /*!< size of 256KB*/
5858

5959
/* FMC BANK address */
60-
#define FMC_START_ADDRESS FLASH_BASE /*!< FMC start address */
61-
#define FMC_BANK0_START_ADDRESS FMC_START_ADDRESS /*!< FMC BANK0 start address */
62-
#define FMC_BANK1_START_ADDRESS ((uint32_t)0x08100000U) /*!< FMC BANK1 start address */
63-
#define FMC_SIZE (*(uint16_t*)0x1FFF7A22U) /*!< FMC SIZE */
64-
#define FMC_END_ADDRESS (FLASH_BASE + (FMC_SIZE * 1024) - 1) /*!< FMC end address */
65-
#define FMC_MAX_END_ADDRESS ((uint32_t)0x08300000U) /*!< FMC maximum end address */
60+
inline constexpr uint32_t FMC_START_ADDRESS = FLASH_BASE; /*!< FMC start address */
61+
inline constexpr uint32_t FMC_BANK0_START_ADDRESS = FMC_START_ADDRESS; /*!< FMC BANK0 start address */
62+
inline constexpr uint32_t FMC_BANK1_START_ADDRESS = 0x08100000U; /*!< FMC BANK1 start address */
63+
#define FMC_SIZE (*reinterpret_cast<uint16_t*>(0x1FFF7A22U))
64+
#define FMC_END_ADDRESS (FLASH_BASE + (FMC_SIZE * 1024) - 1) /*!< FMC end address */
65+
inline constexpr uint32_t FMC_MAX_END_ADDRESS = 0x08300000U; /*!< FMC maximum end address */
6666

6767
/* FMC error message */
68-
#define FMC_WRONG_SECTOR_NAME ((uint32_t)0xFFFFFFFFU) /*!< wrong sector name*/
69-
#define FMC_WRONG_SECTOR_NUM ((uint32_t)0xFFFFFFFFU) /*!< wrong sector number*/
70-
#define FMC_INVALID_SIZE ((uint32_t)0xFFFFFFFFU) /*!< invalid sector size*/
71-
#define FMC_INVALID_ADDR ((uint32_t)0xFFFFFFFFU) /*!< invalid sector address*/
68+
inline constexpr uint32_t FMC_WRONG_SECTOR_NAME = 0xFFFFFFFFU; /*!< wrong sector name*/
69+
inline constexpr uint32_t FMC_WRONG_SECTOR_NUM = 0xFFFFFFFFU; /*!< wrong sector number*/
70+
inline constexpr uint32_t FMC_INVALID_SIZE = 0xFFFFFFFFU; /*!< invalid sector size*/
71+
inline constexpr uint32_t FMC_INVALID_ADDR = 0xFFFFFFFFU; /*!< invalid sector address*/
7272

7373
/* get the sector number, size and range of the given address */
7474
fmc_sector_info_struct fmc_sector_info_get(uint32_t addr); // NOLINT

lib-gd32/include/gd32xxxx.h

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,11 @@
2626
#ifndef GD32FXXX_H_
2727
#define GD32FXXX_H_
2828

29-
// Needed for GD32 Firmware and CMSIS
30-
31-
#ifdef __cplusplus
32-
#pragma GCC diagnostic push
33-
#pragma GCC diagnostic ignored "-Wuseless-cast"
34-
#pragma GCC diagnostic ignored "-Wold-style-cast"
35-
#pragma GCC diagnostic ignored "-Wconversion"
36-
#pragma GCC diagnostic ignored "-Wsign-conversion"
37-
#if __cplusplus > 201402
38-
// error: compound assignment with 'volatile'-qualified left operand is
39-
// deprecated
40-
#pragma GCC diagnostic ignored "-Wvolatile"
41-
#endif
42-
#endif
43-
4429
#if defined(GD32F10X_HD) || defined(GD32F10X_CL)
4530
#include "gd32f10x.h" // IWYU pragma: keep
4631
#elif defined(GD32F20X_CL)
4732
#include "gd32f20x.h" // IWYU pragma: keep
48-
#elif defined(GD32F30X_HD)
33+
#elif defined(GD32F30X_HD) || defined(GD32F30X_XD)
4934
#include "gd32f30x.h" // IWYU pragma: keep
5035
#elif defined(GD32F407) || defined(GD32F450) || defined(GD32F470)
5136
#include "gd32f4xx.h" // IWYU pragma: keep
@@ -56,8 +41,4 @@
5641
#error MCU is not supported
5742
#endif
5843

59-
#ifdef __cplusplus
60-
#pragma GCC diagnostic pop
61-
#endif
62-
6344
#endif // GD32FXXX_H_ */

lib-gd32/src/softuart0/uart0.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@
3535
#elif defined(GD32F30X)
3636
#define TIMERx TIMER7
3737
#define RCU_TIMERx RCU_TIMER7
38+
#if defined (GD32F30X_XD)
39+
#define TIMERx_IRQHandler TIMER7_UP_TIMER12_IRQHandler
40+
#define TIMERx_IRQn TIMER7_UP_TIMER12_IRQn
41+
#else
3842
#define TIMERx_IRQHandler TIMER7_UP_IRQHandler
3943
#define TIMERx_IRQn TIMER7_UP_IRQn
44+
#endif
4045
#else
4146
#define TIMERx TIMER9
4247
#define RCU_TIMERx RCU_TIMER9
@@ -147,7 +152,7 @@ void TIMERx_IRQHandler() {
147152
}
148153
}
149154

150-
TIMER_INTF(TIMERx) = static_cast<uint32_t>(~kIntFlag);
155+
TIMER_INTF(TIMERx) = ~kIntFlag;
151156
}
152157
#if defined(SOFTUART0_ENABLE_RX)
153158
void SOFTUART_RX_EXTIx_IRQHandler() {

lib-gd32/src/timer6.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void TIMER6_IRQHandler() {
4141
gv_seconds.uptime = gv_seconds.uptime + 1;
4242
}
4343

44-
TIMER_INTF(TIMER6) = static_cast<uint32_t>(~kIntFlag);
44+
TIMER_INTF(TIMER6) = ~kIntFlag;
4545
}
4646
#endif
4747
}

0 commit comments

Comments
 (0)