Skip to content

Commit 3da5ba8

Browse files
committed
Refactor PHY, timing, and error handling
Large refactor across lib-network to improve PHY handling, timing API usage and error reporting. Highlights: - PHY API: changed PHY address/register parameters from uint32_t to uint16_t and added kUnknown variants for Duplex/Speed enums; updated Read/Write/Config/GetId/Start signatures and implementations. - PHY logic: improved autonegotiation/advertisement handling, ParseLink logic, added more robust Status initialization and debug prints, and switched to human-readable ToString outputs. - Timing: replaced hal::Millis() with timing::Millis() and updated related includes. - Error reporting: removed console::Error usage, added network::Error helper and ERROR macro, and replaced many console error prints with ERROR(...) messages. - Misc: added ../lib-hwclock include to Rules.mk, changed some counter types to uint32_t, renamed/adjusted dp83848 source filenames, updated gd32 PTP calls to CamelCase (Gd32*), and small naming/const improvements (e.g. kPhyValue). These changes consolidate types, standardize timing and error APIs, and clarify PHY status reporting.
1 parent b52b3c6 commit 3da5ba8

17 files changed

Lines changed: 157 additions & 138 deletions

File tree

lib-network/Rules.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
$(info $$MAKE_FLAGS [${MAKE_FLAGS}])
22
$(info $$DEFINES [${DEFINES}])
33

4-
EXTRA_INCLUDES+=../lib-display/include
4+
EXTRA_INCLUDES+=../lib-hwclock/include ../lib-display/include
55

66
COND=
77

lib-network/include/emac/emac_phy.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030

3131
namespace emac::phy {
3232
enum class Link { kStateDown, kStateUp };
33-
enum class Duplex { kDuplexHalf, kDuplexFull };
34-
enum class Speed { kSpeed10, kSpeed100, kSpeed1000 };
33+
enum class Duplex { kUnknown, kDuplexHalf, kDuplexFull };
34+
enum class Speed { kUnknown, kSpeed10, kSpeed100, kSpeed1000 };
3535

3636
struct Status {
3737
Link link;
@@ -50,15 +50,15 @@ struct Identifier {
5050
@{
5151
*/
5252

53-
bool GetId(uint32_t address, Identifier& phy_identifier);
54-
Link GetLink(uint32_t address);
53+
bool GetId(uint16_t address, Identifier& phy_identifier);
54+
Link GetLink(uint16_t address);
5555

5656
/**
5757
*
5858
* @param address PHY address
5959
* @return true for success, false for failure
6060
*/
61-
bool Powerdown(uint32_t address);
61+
bool Powerdown(uint16_t address);
6262

6363
/**
6464
*
@@ -67,7 +67,7 @@ bool Powerdown(uint32_t address);
6767
* @param address PHY address
6868
* @return true for success, false for failure
6969
*/
70-
bool Start(uint32_t address, Status& phy_status);
70+
bool Start(uint16_t address, Status& phy_status);
7171
/** @} */
7272

7373
/** \defgroup platform Platform implementation
@@ -81,7 +81,7 @@ bool Start(uint32_t address, Status& phy_status);
8181
* @param value Returned value
8282
* @return
8383
*/
84-
bool Read(uint32_t address, uint32_t reg, uint16_t& value);
84+
bool Read(uint16_t address, uint16_t reg, uint16_t& value);
8585

8686
/**
8787
*
@@ -90,15 +90,15 @@ bool Read(uint32_t address, uint32_t reg, uint16_t& value);
9090
* @param value Value to write
9191
* @return true for success, false for failure
9292
*/
93-
bool Write(uint32_t address, uint32_t reg, uint16_t value);
93+
bool Write(uint16_t address, uint16_t reg, uint16_t value);
9494

9595
/**
9696
* PHY interface configuration (configure SMI and reset PHY)
9797
* Called from \ref EmacConfig
9898
* @param address true for success, false for failure
9999
* @return
100100
*/
101-
bool Config(uint32_t address);
101+
bool Config(uint16_t address);
102102
/** @} */
103103

104104
/** \defgroup specific PHY specific

lib-network/include/emac/mmi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ inline constexpr uint16_t ADVERTISE_LPACK = 0x4000; /* Ack link partners
7474
inline constexpr uint16_t ADVERTISE_NPAGE = 0x8000; /* Next page bit */
7575

7676
inline constexpr uint16_t ADVERTISE_FULL = (ADVERTISE_100FULL | ADVERTISE_10FULL | ADVERTISE_CSMA);
77-
inline constexpr uint16_t ADVERTISE_ALL = (ADVERTISE_10HALF | ADVERTISE_10FULL | ADVERTISE_100HALF | ADVERTISE_100FULL);
77+
inline constexpr uint16_t ADVERTISE_ALL = (ADVERTISE_10HALF | ADVERTISE_10FULL | ADVERTISE_100HALF | ADVERTISE_100FULL | ADVERTISE_CSMA);
7878

7979
/* Link partner ability register. */
8080
inline constexpr uint16_t LPA_SLCT = 0x001f; /* Same as advertise selector */

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ static void Send() {
204204
// Only when the client receives a valid response from the server,
205205
// it will be able to send a request in the interleaved mode
206206
if (s_ntp_client.state.missed_responses > 4) {
207-
s_ntp_client.cookie_basic.seconds = random();
207+
s_ntp_client.cookie_basic.seconds = static_cast<uint32_t>(random());
208208
s_ntp_client.cookie_basic.fraction = 0;
209209

210210
s_ntp_client.request.origin_timestamp_s = 0;
@@ -244,10 +244,10 @@ static void Send() {
244244

245245
if (s_ntp_client.state.x > 0) {
246246
s_ntp_client.state.sent_a.seconds = net::globals::ptp::timestamp[1] + ntp::kJan1970;
247-
s_ntp_client.state.sent_a.fraction = NTPFRAC(gd32::ptp_subsecond_2_nanosecond(net::globals::ptp::timestamp[0]));
247+
s_ntp_client.state.sent_a.fraction = NTPFRAC(gd32::PtpSubsecond2Nanosecond(net::globals::ptp::timestamp[0]));
248248
} else {
249249
s_ntp_client.state.sent_b.seconds = net::globals::ptp::timestamp[1] + ntp::kJan1970;
250-
s_ntp_client.state.sent_b.fraction = NTPFRAC(gd32::ptp_subsecond_2_nanosecond(net::globals::ptp::timestamp[0]));
250+
s_ntp_client.state.sent_b.fraction = NTPFRAC(gd32::PtpSubsecond2Nanosecond(net::globals::ptp::timestamp[0]));
251251
}
252252

253253
s_ntp_client.state.x = -s_ntp_client.state.x;
@@ -262,7 +262,7 @@ static void Difference(const ntp::TimeStamp& start, const ntp::TimeStamp& stop,
262262
gd32::ptp::time_t r;
263263
const gd32::ptp::time_t kX = {.tv_sec = static_cast<int32_t>(stop.seconds), .tv_nsec = static_cast<int32_t>(USEC(stop.fraction) * 1000)};
264264
const gd32::ptp::time_t kY = {.tv_sec = static_cast<int32_t>(start.seconds), .tv_nsec = static_cast<int32_t>(USEC(start.fraction) * 1000)};
265-
gd32::sub_time(&r, &kX, &kY);
265+
gd32::SubTime(&r, &kX, &kY);
266266

267267
diff_seconds = r.tv_sec;
268268
diff_nano_seconds = r.tv_nsec;
@@ -286,11 +286,11 @@ static void UpdatePtpTime() {
286286
const int32_t kOffsetNanosverage = offset_nano_seconds / 2;
287287

288288
gd32::ptp::time_t ptp_offset = {.tv_sec = kOffsetSecondsAverage, .tv_nsec = kOffsetNanosverage};
289-
gd32::normalize_time(&ptp_offset);
290-
gd32_ptp_update_time(&ptp_offset);
289+
gd32::NormalizeTime(&ptp_offset);
290+
Gd32PtpUpdateTime(&ptp_offset);
291291

292292
gd32::ptp::ptptime ptp_get;
293-
gd32_ptp_get_time(&ptp_get);
293+
Gd32PtpGetTime(&ptp_get);
294294

295295
s_ntp_client.request.reference_timestamp_s = __builtin_bswap32(static_cast<uint32_t>(ptp_get.tv_sec) + ntp::kJan1970);
296296
s_ntp_client.request.reference_timestamp_f = __builtin_bswap32(NTPFRAC(ptp_get.tv_nsec));
@@ -336,7 +336,7 @@ static void UpdatePtpTime() {
336336
}
337337

338338
gd32::ptp::time_t ptp_delay;
339-
gd32::sub_time(&ptp_delay, &diff1, &diff2);
339+
gd32::SubTime(&ptp_delay, &diff1, &diff2);
340340

341341
char sign = '+';
342342

@@ -383,7 +383,7 @@ static void Process() {
383383
}
384384

385385
s_ntp_client.t4.seconds = net::globals::ptp::timestamp[1] + ntp::kJan1970;
386-
s_ntp_client.t4.fraction = NTPFRAC(gd32::ptp_subsecond_2_nanosecond(net::globals::ptp::timestamp[0]));
386+
s_ntp_client.t4.fraction = NTPFRAC(gd32::PtpSubsecond2Nanosecond(net::globals::ptp::timestamp[0]));
387387
#ifndef NDEBUG
388388
s_ntp_client.state.mode = ntp::Modes::kBasic;
389389
#endif
@@ -418,7 +418,7 @@ static void Process() {
418418
s_ntp_client.state.dst.fraction = __builtin_bswap32(kReply->receive_timestamp_f);
419419

420420
s_ntp_client.state.previous_receive.seconds = net::globals::ptp::timestamp[1] + ntp::kJan1970;
421-
s_ntp_client.state.previous_receive.fraction = NTPFRAC(gd32::ptp_subsecond_2_nanosecond(net::globals::ptp::timestamp[0]));
421+
s_ntp_client.state.previous_receive.fraction = NTPFRAC(gd32::PtpSubsecond2Nanosecond(net::globals::ptp::timestamp[0]));
422422

423423
UpdatePtpTime();
424424

lib-network/src/core/ipv4/dhcp.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,10 @@ void Inform() {
233233
DEBUG_ENTRY();
234234

235235
const auto kHandle = network::udp::Begin(network::iana::Ports::kPortDhcpClient, nullptr);
236-
#ifndef NDEBUG
237236
if (kHandle < 0) {
238-
console::Error("DHCP Inform\n");
237+
ERROR("No handle\n");
239238
return;
240239
}
241-
#endif
242240

243241
MessageInit();
244242
network::MemcpyIp(&s_dhcp_message.ciaddr[0], netif::global::netif_default.ip.addr);
@@ -674,13 +672,11 @@ bool Start() {
674672
std::memset(dhcp, 0, sizeof(struct dhcp::Dhcp));
675673
dhcp->handle = network::udp::Begin(network::iana::Ports::kPortDhcpClient, dhcp::Input);
676674

677-
#ifndef NDEBUG
678675
if (dhcp->handle < 0) {
679-
console::Error("DHCP Start\n");
676+
ERROR("No handle.\n");
680677
DEBUG_EXIT();
681678
return false;
682679
}
683-
#endif
684680

685681
MessageInit();
686682

lib-network/src/core/ipv4/igmp.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,7 @@ void static Join(uint32_t group_address) {
335335
}
336336
}
337337

338-
#ifndef NDEBUG
339-
console::Error("igmp::Join\n");
340-
#endif
338+
ERROR("Max joins reached.\n");
341339
DEBUG_ENTRY();
342340
}
343341

@@ -361,10 +359,7 @@ static void Leave(uint32_t group_address) {
361359
}
362360
}
363361

364-
#ifndef NDEBUG
365-
console::Error("igmp::Leave: ");
366-
printf(IPSTR "\n", IP2STR(group_address));
367-
#endif
362+
ERROR("Group address not found.\n");
368363
DEBUG_EXIT();
369364
}
370365

lib-network/src/core/network_private.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#define NET_NET_PRIVATE_H_
2828

2929
#include <cstdint>
30+
#include <cstdio>
3031

3132
#include "core/protocol/icmp.h"
3233
#include "core/protocol/igmp.h"
@@ -39,10 +40,6 @@
3940
#define ALIGNED __attribute__((aligned(4)))
4041
#endif
4142

42-
namespace console {
43-
void Error(const char*);
44-
}
45-
4643
namespace emac::eth {
4744
uint8_t* SendGetDmaBuffer();
4845
void Send(const uint32_t);
@@ -56,6 +53,12 @@ void FreePkt();
5653
} // namespace emac::eth
5754

5855
namespace network {
56+
inline void Error(const char* func, const char* s) {
57+
printf("%s: %s\n", func, s);
58+
}
59+
60+
#define ERROR(s) Error(__func__, (s))
61+
5962
namespace global {
6063
extern uint32_t broadcast_mask;
6164
extern uint32_t on_network_mask;

lib-network/src/core/tcp.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ void Error(const char*);
7070
#include "core/protocol/ieee.h"
7171
#include "core/protocol/tcp.h"
7272
#include "network_private.h"
73-
#include "hal.h"
74-
#include "hal_millis.h"
73+
#include "timing.h"
7574
#include "firmware/debug/debug_debug.h"
7675
#include "network_tcp.h"
7776
#include "core/protocol/ethernet.h"
@@ -337,7 +336,7 @@ static void TcpSwap32AcknumSeqnum(struct Header* const kTcp) {
337336
static void TcpInitTcb(struct Tcb* tcb, uint16_t local_port) {
338337
tcb->local_port = local_port;
339338

340-
tcb->ISS = hal::Millis();
339+
tcb->ISS = timing::Millis();
341340

342341
tcb->RCV.WND = kAdvertisedRxWnd;
343342

@@ -363,7 +362,7 @@ static void RtxOnAck(Tcb* tcb, uint32_t ack) {
363362
if (tcb->rtx.count == 0) {
364363
tcb->rtx_deadline = 0;
365364
} else {
366-
tcb->rtx_deadline = hal::Millis() + tcb->rtx_rto;
365+
tcb->rtx_deadline = timing::Millis() + tcb->rtx_rto;
367366
}
368367
}
369368

@@ -480,7 +479,7 @@ static void SendSegment(Tcb* tcb, const SendInfo& send_info, bool track_rtx = tr
480479
*data++ = Option::kKindNop;
481480
*data++ = Option::kKindTimestamp;
482481
*data++ = 10;
483-
const auto kMillis = __builtin_bswap32(hal::Millis());
482+
const auto kMillis = __builtin_bswap32(timing::Millis());
484483
memcpy(data, &kMillis, 4);
485484
data += 4;
486485
memcpy(data, &tcb->TS.recent, 4);
@@ -514,7 +513,7 @@ static void SendSegment(Tcb* tcb, const SendInfo& send_info, bool track_rtx = tr
514513
r.consumed = r.len + ((send_info.CTL & Control::SYN) ? 1U : 0U) + ((send_info.CTL & Control::FIN) ? 1U : 0U);
515514
r.ctl = send_info.CTL;
516515
r.retries = 0;
517-
r.last_sent = hal::Millis();
516+
r.last_sent = timing::Millis();
518517
r.pool_idx = (r.len != 0) ? memory::Allocator::Instance().Allocate(tcb->TX.data, r.len) : 0xFFFF;
519518

520519
tcb->rtx.count++;
@@ -674,7 +673,7 @@ __attribute__((hot)) void Run() {
674673

675674
// Client-side TIME-WAIT expiry
676675
if (tcb.state == kStateTimeWait) {
677-
if (tcb.timewait_deadline != 0 && hal::Millis() >= tcb.timewait_deadline) {
676+
if (tcb.timewait_deadline != 0 && timing::Millis() >= tcb.timewait_deadline) {
678677
NEW_STATE(&tcb, kStateClosed);
679678
FreeTcb(&tcb);
680679
continue;
@@ -694,7 +693,7 @@ __attribute__((hot)) void Run() {
694693
}
695694

696695
// ---- Retransmission timeout ----
697-
if (tcb.rtx.count > 0 && tcb.rtx_deadline != 0 && hal::Millis() >= tcb.rtx_deadline) {
696+
if (tcb.rtx.count > 0 && tcb.rtx_deadline != 0 && timing::Millis() >= tcb.rtx_deadline) {
698697
auto& r = tcb.rtx.q[tcb.rtx.head];
699698

700699
SendInfo info;
@@ -714,7 +713,7 @@ __attribute__((hot)) void Run() {
714713
tcb.TX.data = nullptr;
715714
tcb.TX.size = 0;
716715

717-
r.last_sent = hal::Millis();
716+
r.last_sent = timing::Millis();
718717
r.retries++;
719718

720719
if (r.retries > kTcpRtxMaxRetry) {
@@ -723,7 +722,7 @@ __attribute__((hot)) void Run() {
723722
}
724723

725724
tcb.rtx_rto = std::min(tcb.rtx_rto * 2U, kTcpRtoMaxMs);
726-
tcb.rtx_deadline = hal::Millis() + tcb.rtx_rto;
725+
tcb.rtx_deadline = timing::Millis() + tcb.rtx_rto;
727726
}
728727
}
729728
}
@@ -830,7 +829,7 @@ static Tcb* AcceptNewConnection(const Header* tcp_segment, uint32_t* out_index)
830829
static inline void EnterTimeWait(Tcb* tcb) {
831830
NEW_STATE(tcb, kStateTimeWait);
832831

833-
tcb->timewait_deadline = hal::Millis() + kTimeWaitMs;
832+
tcb->timewait_deadline = timing::Millis() + kTimeWaitMs;
834833

835834
// Turn off other timers
836835
tcb->rtx.count = 0; // drop unacked queue
@@ -1429,7 +1428,7 @@ __attribute__((hot)) void Input(struct Header* eth_frame) {
14291428
SendSegment(tcb, si, false);
14301429

14311430
// Restart TIME-WAIT timer
1432-
tcb->timewait_deadline = hal::Millis() + kTimeWaitMs;
1431+
tcb->timewait_deadline = timing::Millis() + kTimeWaitMs;
14331432
return;
14341433
}
14351434

lib-network/src/core/udp.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,7 @@ int32_t Begin(uint16_t localport, UdpCallbackFunctionPtr callback) {
219219
}
220220
}
221221

222-
#ifndef NDEBUG
223-
console::Error("network::udp::Begin\n");
224-
#endif
222+
ERROR("Max ports reached.\n");
225223
return -1;
226224
}
227225

@@ -241,9 +239,7 @@ int32_t End(uint16_t localport) {
241239
}
242240
}
243241

244-
#ifndef NDEBUG
245-
console::Error("network::udp::End\n");
246-
#endif
242+
ERROR("Port not found.\n");
247243
return -1;
248244
}
249245

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ void __attribute__((cold)) Start(uint8_t mac_address[], emac::phy::Link& link) {
200200
}
201201

202202
#if defined(CONFIG_NET_ENABLE_PTP)
203-
gd32_ptp_start();
203+
Gd32PtpStart();
204204
#if !defined(DISABLE_RTC)
205205
// Set the System Clock from the Hardware Clock
206206
HwClock::Get()->HcToSys();

0 commit comments

Comments
 (0)