Skip to content

Commit ec5f8e6

Browse files
committed
Tighten GD32 type conversions
Use explicit casts and an underlying type for several GD32-facing enums and register reads to avoid implicit narrowing and signedness issues. This keeps ADC, backup, SPI bitbang, and FT245 data paths type-safe and warning-free.
1 parent 14c369c commit ec5f8e6

5 files changed

Lines changed: 17 additions & 17 deletions

File tree

lib-dmxnode/include/dmxnode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ inline static constexpr const char kLtpUpper[] = "LTP";
8080

8181
enum class Direction { kInput, kOutput, kDisable };
8282

83-
enum class FailSafe { kHold, kOff, kOn, kPlayback, kRecord };
83+
enum class FailSafe : uint8_t { kHold, kOff, kOn, kPlayback, kRecord };
8484

8585
namespace failsafe {
8686
inline static constexpr const char kHold[] = "hold";

lib-gd32/src/f/gd32_adc.cpp

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

26-
#include "gd32.h"
26+
#include "gd32.h" // IWYU pragma: keep
2727
#include "timing.h"
2828

2929
void Gd32AdcInit() {
@@ -86,20 +86,20 @@ void Gd32AdcInit() {
8686
}
8787

8888
float G32AdcGetTemp() {
89-
const float kTemperature = (1.43f - ADC_IDATA0(ADC0) * 3.3f / 4096U) * 1000U / 4.3f + 25U;
89+
const float kTemperature = (1.43f - static_cast<float>(ADC_IDATA0(ADC0)) * 3.3f / 4096U) * 1000U / 4.3f + 25U;
9090
adc_software_trigger_enable(ADC0, ADC_INSERTED_CHANNEL);
9191
return kTemperature;
9292
}
9393

9494
float Gd32AdcGetVref() {
95-
const float kVrefValue = (ADC_IDATA1(ADC0) * 3.3f / 4096U);
95+
const float kVrefValue = static_cast<float>(ADC_IDATA1(ADC0)) * 3.3f / 4096U;
9696
adc_software_trigger_enable(ADC0, ADC_INSERTED_CHANNEL);
9797
return kVrefValue;
9898
}
9999

100100
#if defined(GD32F4XX)
101101
float Gd32AdcGetVbat() {
102-
const float kVrefValue = (ADC_IDATA2(ADC0) * 3.3f / 4096U) * 4U;
102+
const float kVrefValue = (static_cast<float>(ADC_IDATA2(ADC0)) * 3.3f / 4096U) * 4U;
103103
adc_software_trigger_enable(ADC0, ADC_INSERTED_CHANNEL);
104104
return kVrefValue;
105105
}

lib-gd32/src/f/gd32_spi.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ static inline char BitbangSpiWriteRead(char c) {
265265
GPIO_BOP(SPI_BITBANG_SCK_GPIOx) = SPI_BITBANG_SCK_GPIO_PINx;
266266

267267
if ((GPIO_ISTAT(SPI_BITBANG_MISO_GPIOx) & SPI_BITBANG_MISO_GPIO_PINx) == SPI_BITBANG_MISO_GPIO_PINx) {
268-
r |= (mask);
268+
r |= static_cast<char>(mask);
269269
}
270270

271271
__ISB();

lib-gd32/src/gd32_bkp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ void bkp_data_write(bkp_data_register_enum register_number, uint16_t data) {
4444
uint16_t bkp_data_read(bkp_data_register_enum register_number) {
4545
switch (register_number) {
4646
case BKP_DATA_0:
47-
return RTC_BKP0;
47+
return static_cast<uint16_t>(RTC_BKP0);
4848
break;
4949
case BKP_DATA_1:
50-
return RTC_BKP1;
50+
return static_cast<uint16_t>(RTC_BKP1);
5151
break;
5252
default:
5353
assert(false && "Invalid register_number");

lib-usb/src/gd32/ft245rl.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,15 @@ uint8_t FT245RL_read_data()
155155
const auto kInGpioA = static_cast<uint16_t>(GPIO_ISTAT(GPIOA));
156156
const auto kInGpioB = static_cast<uint16_t>(GPIO_ISTAT(GPIOB));
157157

158-
data |= ((kInGpioA & (GPIO_PIN_6)) ? 4 : 0);
159-
data |= ((kInGpioA & (GPIO_PIN_14)) ? 8 : 0);
160-
data |= ((kInGpioA & (GPIO_PIN_15)) ? 16 : 0);
161-
162-
data |= ((kInGpioB & (GPIO_PIN_9)) ? 1 : 0);
163-
data |= ((kInGpioB & (GPIO_PIN_8)) ? 2 : 0);
164-
data |= ((kInGpioB & (GPIO_PIN_4)) ? 32 : 0);
165-
data |= ((kInGpioB & (GPIO_PIN_5)) ? 64 : 0);
166-
data |= ((kInGpioB & (GPIO_PIN_3)) ? 128 : 0);
158+
data |= ((kInGpioA & (GPIO_PIN_6)) ? 4U : 0);
159+
data |= ((kInGpioA & (GPIO_PIN_14)) ? 8U : 0);
160+
data |= ((kInGpioA & (GPIO_PIN_15)) ? 16U : 0);
161+
162+
data |= ((kInGpioB & (GPIO_PIN_9)) ? 1U : 0);
163+
data |= ((kInGpioB & (GPIO_PIN_8)) ? 2U : 0);
164+
data |= ((kInGpioB & (GPIO_PIN_4)) ? 32U : 0);
165+
data |= ((kInGpioB & (GPIO_PIN_5)) ? 64U : 0);
166+
data |= ((kInGpioB & (GPIO_PIN_3)) ? 128U : 0);
167167

168168
// Bring RD# back up so the FT245 can let go of the data.
169169
Gd32GpioSet(_RD);

0 commit comments

Comments
 (0)