Skip to content

Commit 271aefe

Browse files
committed
Tighten typing and fix ENET init flags
Adds explicit integer/float casts and fixed-width enum/return types across GD32, NTP/PTP, SPI, TCP, and backup-register code to avoid implicit narrowing and signedness issues. It also updates ENET initialization to use `ENET_RECEIVEALL` instead of `ENET_CUSTOM`, and includes minor cleanup (IWYU keep pragma and corrected namespace comment).
1 parent 1596447 commit 271aefe

9 files changed

Lines changed: 23 additions & 23 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/device/enet/ptp/gd32_ptp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ void Gd32PtpUpdateTime(const gd32::ptp::time_t* time) {
168168
}
169169

170170
bool Gd32AdjFrequency(uint32_t adjust_ppb) {
171-
const uint64_t kAdjust = static_cast<uint64_t>(gd32::ptp::kAdjFreqBaseAddend) * adjust_ppb;
172-
const uint32_t kAddend = gd32::ptp::kAdjFreqBaseAddend + (kAdjust / 1000000000ULL);
171+
const auto kAdjust = static_cast<uint64_t>(gd32::ptp::kAdjFreqBaseAddend) * adjust_ppb;
172+
const auto kAddend = gd32::ptp::kAdjFreqBaseAddend + static_cast<uint32_t>(kAdjust / 1000000000ULL);
173173

174174
enet_ptp_timestamp_addend_config(kAddend);
175175

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-hwclock/src/gd32/internal/hwclockrtc.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@
4747
#include "gd32.h"
4848

4949
#if defined(GD32F4XX) || defined(GD32H7XX)
50-
static int BCD2DEC(int val) {
51-
return (((val) & 0x0f) + (val >> 4) * 10);
50+
static uint8_t BCD2DEC(int val) {
51+
return static_cast<uint8_t>(((val) & 0x0f) + static_cast<uint8_t>(val >> 4) * 10U);
5252
}
5353

54-
static int DEC2BCD(int val) {
55-
return ((((val) / 10) << 4) + val % 10);
54+
static uint8_t DEC2BCD(int val) {
55+
return static_cast<uint8_t>(((static_cast<uint8_t>(val) / 10U) << 4) + (static_cast<uint8_t>(val) % 10U));
5656
}
5757
#define RTC_CLOCK_SOURCE_LXTAL
5858
static rtc_parameter_struct rtc_initpara;

lib-network/src/apps/ntp/gd32/ptp/ntpclient.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ T4 - local receive timestamp of the previous response (t4)
8585

8686
namespace net::globals::ptp {
8787
extern uint32_t timestamp[2];
88-
} // namespace net::globals
88+
} // namespace net::globals::ptp
8989

90-
#define _NTPFRAC_(x) (4294U * static_cast<uint32_t>(x) + ((1981U * static_cast<uint32_t>(x)) >> 11) + ((2911U * static_cast<uint32_t>(x)) >> 28))
90+
#define _NTPFRAC_(x) (4294U * (x) + ((1981U * (x)) >> 11) + ((2911U * (x)) >> 28))
9191
#define NTPFRAC(x) _NTPFRAC_(x / 1000)
9292
// The reverse of the above, needed if we want to set our microsecond
9393
// clock (via clock_settime) based on the incoming time in NTP format.
@@ -279,11 +279,11 @@ static void UpdatePtpTime() {
279279
int32_t diff_seconds2, diff_nano_seconds2;
280280
Difference(s_ntp_client.t4, s_ntp_client.t3, diff_seconds2, diff_nano_seconds2);
281281

282-
auto offset_seconds = static_cast<int64_t>(diff_seconds1) + static_cast<int64_t>(diff_seconds2);
283-
auto offset_nano_seconds = static_cast<int64_t>(diff_nano_seconds1) + static_cast<int64_t>(diff_nano_seconds2);
282+
const auto kOffsetSeconds = static_cast<int64_t>(diff_seconds1) + static_cast<int64_t>(diff_seconds2);
283+
const auto kOffsetNanoSeconds = static_cast<int64_t>(diff_nano_seconds1) + static_cast<int64_t>(diff_nano_seconds2);
284284

285-
const int32_t kOffsetSecondsAverage = offset_seconds / 2;
286-
const int32_t kOffsetNanosverage = offset_nano_seconds / 2;
285+
const auto kOffsetSecondsAverage = static_cast<int32_t>(kOffsetSeconds / 2U);
286+
const auto kOffsetNanosverage = static_cast<int32_t>(kOffsetNanoSeconds / 2U);
287287

288288
gd32::ptp::time_t ptp_offset = {.tv_sec = kOffsetSecondsAverage, .tv_nsec = kOffsetNanosverage};
289289
gd32::NormalizeTime(&ptp_offset);

lib-network/src/core/tcp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ static void SendSegment(Tcb* tcb, const SendInfo& send_info, bool track_rtx = tr
507507
auto& r = tcb->rtx.q[(tcb->rtx.head + tcb->rtx.count) % kTcpUnackMax];
508508
r.seq = send_info.SEQ;
509509
r.len = static_cast<uint16_t>(tcb->TX.size);
510-
r.consumed = r.len + ((send_info.CTL & Control::SYN) ? 1U : 0U) + ((send_info.CTL & Control::FIN) ? 1U : 0U);
510+
r.consumed = static_cast<uint16_t>(r.len + ((send_info.CTL & Control::SYN) ? 1U : 0) + ((send_info.CTL & Control::FIN) ? 1U : 0));
511511
r.ctl = send_info.CTL;
512512
r.retries = 0;
513513
r.last_sent = timing::Millis();

lib-network/src/emac/gd32/emac.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ void AdjustLink(emac::phy::Status phy_status) {
127127
}
128128

129129
#if defined(GD32H7XX)
130-
const auto kEnetInitStatus = enet_init(ENETx, mediamode, ENET_AUTOCHECKSUM_DROP_FAILFRAMES, ENET_CUSTOM);
130+
const auto kEnetInitStatus = enet_init(ENETx, mediamode, ENET_AUTOCHECKSUM_DROP_FAILFRAMES, ENET_RECEIVEALL);
131131
#else
132-
const auto kEnetInitStatus = enet_init(mediamode, ENET_AUTOCHECKSUM_DROP_FAILFRAMES, ENET_CUSTOM);
132+
const auto kEnetInitStatus = enet_init(mediamode, ENET_AUTOCHECKSUM_DROP_FAILFRAMES, ENET_RECEIVEALL);
133133
#endif
134134

135135
if (kEnetInitStatus != SUCCESS) {

0 commit comments

Comments
 (0)