Skip to content

Commit 8308c92

Browse files
committed
Fix GD32 build warnings and IRQ handling
Replace NVIC memset calls with explicit register writes and tighten C++ casts (use reinterpret_cast) in bootloader to avoid UB and build warnings. Add Gd32FirmwareOps.mk to set compiler warning flags and reorganize Includes.mk to use -isystem for upstream headers and clean up include ordering. Update gd32xxxx.h to support GD32F30X_XD and remove intrusive diagnostic pragmas. Adjust TIMER IRQ names for GD32F30X_XD and remove unnecessary static_cast when clearing timer interrupt flags to silence warnings and simplify code.
1 parent 902566f commit 8308c92

6 files changed

Lines changed: 28 additions & 34 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-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)