Skip to content

Commit 1d2a768

Browse files
committed
refactor the rest of the drivers
1 parent dceb4be commit 1d2a768

File tree

17 files changed

+393
-301
lines changed

17 files changed

+393
-301
lines changed

mongoose.c

Lines changed: 185 additions & 120 deletions
Large diffs are not rendered by default.

mongoose.h

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2839,18 +2839,16 @@ typedef void (*mg_tcpip_event_handler_t)(struct mg_tcpip_if *ifp, int ev,
28392839
void *ev_data);
28402840

28412841
enum {
2842-
MG_TCPIP_EV_ST_CHG, // state change uint8_t * (&ifp->state)
2843-
MG_TCPIP_EV_DHCP_DNS, // DHCP DNS assignment uint32_t *ipaddr
2844-
MG_TCPIP_EV_DHCP_SNTP, // DHCP SNTP assignment uint32_t *ipaddr
2845-
MG_TCPIP_EV_ARP, // Got ARP packet struct mg_str *
2846-
MG_TCPIP_EV_TIMER_1S, // 1 second timer NULL
2847-
MG_TCPIP_EV_WIFI_SCAN_RESULT, // Wi-Fi scan results struct
2848-
// mg_wifi_scan_bss_data *
2849-
MG_TCPIP_EV_WIFI_SCAN_END, // Wi-Fi scan has finished NULL
2850-
MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connect has failed driver and
2851-
// chip specific
2852-
MG_TCPIP_EV_DRIVER, // Driver event driver specific
2853-
MG_TCPIP_EV_USER // Starting ID for user events
2842+
MG_TCPIP_EV_ST_CHG, // state change uint8_t * (&ifp->state)
2843+
MG_TCPIP_EV_DHCP_DNS, // DHCP DNS assignment uint32_t *ipaddr
2844+
MG_TCPIP_EV_DHCP_SNTP, // DHCP SNTP assignment uint32_t *ipaddr
2845+
MG_TCPIP_EV_ARP, // Got ARP packet struct mg_str *
2846+
MG_TCPIP_EV_TIMER_1S, // 1 second timer NULL
2847+
MG_TCPIP_EV_WIFI_SCAN_RESULT, // Wi-Fi scan results struct mg_wifi_scan_bss_data *
2848+
MG_TCPIP_EV_WIFI_SCAN_END, // Wi-Fi scan has finished NULL
2849+
MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connect has failed driver and chip specific
2850+
MG_TCPIP_EV_DRIVER, // Driver event driver specific
2851+
MG_TCPIP_EV_USER // Starting ID for user events
28542852
};
28552853

28562854
// Network interface
@@ -3427,41 +3425,6 @@ struct mg_tcpip_driver_tms570_data {
34273425

34283426

34293427

3430-
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC7) && MG_ENABLE_DRIVER_XMC7
3431-
3432-
struct mg_tcpip_driver_xmc7_data {
3433-
int mdc_cr; // Valid values: -1, 0, 1, 2, 3, 4, 5
3434-
uint8_t phy_addr;
3435-
};
3436-
3437-
#ifndef MG_TCPIP_PHY_ADDR
3438-
#define MG_TCPIP_PHY_ADDR 0
3439-
#endif
3440-
3441-
#ifndef MG_DRIVER_MDC_CR
3442-
#define MG_DRIVER_MDC_CR 3
3443-
#endif
3444-
3445-
#define MG_TCPIP_DRIVER_INIT(mgr) \
3446-
do { \
3447-
static struct mg_tcpip_driver_xmc7_data driver_data_; \
3448-
static struct mg_tcpip_if mif_; \
3449-
driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \
3450-
driver_data_.phy_addr = MG_TCPIP_PHY_ADDR; \
3451-
mif_.ip = MG_TCPIP_IP; \
3452-
mif_.mask = MG_TCPIP_MASK; \
3453-
mif_.gw = MG_TCPIP_GW; \
3454-
mif_.driver = &mg_tcpip_driver_xmc7; \
3455-
mif_.driver_data = &driver_data_; \
3456-
MG_SET_MAC_ADDRESS(mif_.mac); \
3457-
mg_tcpip_init(mgr, &mif_); \
3458-
MG_INFO(("Driver: xmc7, MAC: %M", mg_print_mac, mif_.mac)); \
3459-
} while (0)
3460-
3461-
#endif
3462-
3463-
3464-
34653428
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC) && MG_ENABLE_DRIVER_XMC
34663429

34673430
struct mg_tcpip_driver_xmc_data {

src/dns.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,10 @@ static void mdns_cb(struct mg_connection *c, int ev, void *ev_data) {
295295
if (n > 0) {
296296
// RFC-6762 Appendix C, RFC2181 11: m(n + 1-63), max 255 + 0x0
297297
// buf and h declared here to ease future expansion to DNS-SD
298-
char buf[sizeof(struct mg_dns_header) + 256 + sizeof(mdns_answer) + 4];
298+
uint8_t buf[sizeof(struct mg_dns_header) + 256 + sizeof(mdns_answer) + 4];
299299
struct mg_dns_header *h = (struct mg_dns_header *) buf;
300300
char local_name[63 + 7]; // name label + '.' + local label + '\0'
301-
uint8_t name_len = (uint8_t) strlen(c->fn_data);
301+
uint8_t name_len = (uint8_t) strlen((char *)c->fn_data);
302302
struct mg_dns_message dm;
303303
bool unicast = (rr.aclass & MG_BIT(15)) != 0; // QU
304304
// uint16_t q = mg_ntohs(qh->num_questions);
@@ -310,7 +310,7 @@ static void mdns_cb(struct mg_connection *c, int ev, void *ev_data) {
310310
memcpy(local_name, c->fn_data, name_len);
311311
strcpy(local_name + name_len, ".local"); // ensure proper name.local\0
312312
if (strcmp(local_name, dm.name) == 0) {
313-
char *p = &buf[sizeof(*h)];
313+
uint8_t *p = &buf[sizeof(*h)];
314314
memset(h, 0, sizeof(*h)); // clear header
315315
h->txnid = unicast ? qh->txnid : 0; // RFC-6762 18.1
316316
// RFC-6762 6: 0 questions, 1 Answer, 0 Auth, 0 Additional RRs
@@ -328,7 +328,7 @@ static void mdns_cb(struct mg_connection *c, int ev, void *ev_data) {
328328
memcpy(p, c->data, 4), p += 4;
329329
#endif
330330
if (!unicast) memcpy(&c->rem, &c->loc, sizeof(c->rem));
331-
mg_send(c, buf, p - buf); // And send it!
331+
mg_send(c, buf, (size_t)(p - buf)); // And send it!
332332
MG_DEBUG(("mDNS %c response sent", unicast ? 'U' : 'M'));
333333
}
334334
}
@@ -342,6 +342,6 @@ void mg_multicast_add(struct mg_connection *c, char *ip);
342342
struct mg_connection *mg_mdns_listen(struct mg_mgr *mgr, char *name) {
343343
struct mg_connection *c =
344344
mg_listen(mgr, "udp://224.0.0.251:5353", mdns_cb, name);
345-
if (c != NULL) mg_multicast_add(c, "224.0.0.251");
345+
if (c != NULL) mg_multicast_add(c, (char *)"224.0.0.251");
346346
return c;
347347
}

src/drivers/cyw.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,18 @@ static void cyw_handle_bdc(struct bdc_hdr *bdc, size_t len);
140140
static void cyw_handle_bdc_evnt(struct bdc_hdr *bdc, size_t len);
141141

142142
static size_t cyw_spi_poll(uint8_t *dest);
143+
static void cyw_update_hash_table(void);
143144

144145
// High-level comm stuff
145146

146147
static void cyw_poll(void) {
147148
struct sdpcm_hdr *sdpcm = (struct sdpcm_hdr *) resp;
148149
unsigned int channel;
150+
if (s_ifp->update_mac_hash_table) {
151+
// first call to _poll() is after _init(), so this is safe
152+
cyw_update_hash_table();
153+
s_ifp->update_mac_hash_table = false;
154+
}
149155
if (cyw_spi_poll((uint8_t *) resp) == 0) return; // BUS DEPENDENCY
150156
if ((sdpcm->len ^ sdpcm->_len) != 0xffff || sdpcm->len < sizeof(*sdpcm) ||
151157
sdpcm->len > 2048 - sizeof(*sdpcm))
@@ -822,6 +828,13 @@ static bool cyw_load_clm(struct mg_tcpip_driver_cyw_firmware *fw) {
822828
return cyw_load_clmll((void *) fw->clm_addr, fw->clm_len);
823829
}
824830

831+
static void cyw_update_hash_table(void) {
832+
// TODO(): read database, rebuild hash table
833+
uint32_t val = 0;
834+
val = 1; cyw_ioctl_iovar_set2_(0, "mcast_list", (uint8_t *)&val, sizeof(val), (uint8_t *)mcast_addr, sizeof(mcast_addr));
835+
mg_delayms(50);
836+
}
837+
825838
// CYW43 chip backplane specifics. All values read and written are in little
826839
// endian format
827840

@@ -1184,9 +1197,4 @@ bool mg_wifi_ap_stop(void) {
11841197
return cyw_wifi_ap_stop();
11851198
}
11861199

1187-
void mg_tcpip_driver_multicast_add(const uint8_t mcast_addr) {
1188-
val = 1; cyw_ioctl_iovar_set2_(0, "mcast_list", (uint8_t *)&val, sizeof(val), (uint8_t *)mcast_addr, sizeof(mcast_addr));
1189-
//mg_delayms(50);
1190-
}
1191-
11921200
#endif

src/drivers/imxrt.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,10 @@ static bool mg_tcpip_driver_imxrt_init(struct mg_tcpip_if *ifp) {
115115
ENET->RDAR = MG_BIT(24); // Receive Descriptors have changed
116116
ENET->TDAR = MG_BIT(24); // Transmit Descriptors have changed
117117
// ENET->OPD = 0x10014;
118-
uint32_t hash_table[2] = {0, 0};
119-
ENET->IAUR = hash_table[1];
120-
ENET->IALR = hash_table[0];
121-
#if MG_TCPIP_MCAST
122-
// RM 37.3.4.3.2
123-
// uint8_t hash64 = ((~mg_crc32(0, mcast_addr, 6)) >> 26) & 0x3f;
124-
// hash_table[((uint8_t)hash64) >> 5] |= (1 << (hash64 & 0x1f));
125-
hash_table[1] = MG_BIT(1); // above reduces to this for mDNS addr
126-
#endif
127-
ENET->GAUR = hash_table[1];
128-
ENET->GALR = hash_table[0];
118+
ENET->IAUR = 0;
119+
ENET->IALR = 0;
120+
ENET->GAUR = 0;
121+
ENET->GALR = 0;
129122
return true;
130123
}
131124

@@ -154,7 +147,22 @@ static size_t mg_tcpip_driver_imxrt_tx(const void *buf, size_t len,
154147
return len;
155148
}
156149

150+
static mg_tcpip_driver_imxrt_update_hash_table(struct mg_tcpip_if *ifp) {
151+
// TODO(): read database, rebuild hash table
152+
// RM 37.3.4.3.2
153+
uint32_t hash_table[2] = {0, 0};
154+
// uint8_t hash64 = ((~mg_crc32(0, mcast_addr, 6)) >> 26) & 0x3f;
155+
// hash_table[((uint8_t)hash64) >> 5] |= (1 << (hash64 & 0x1f));
156+
hash_table[1] = MG_BIT(1); // above reduces to this for mDNS addr
157+
ENET->GAUR = hash_table[1];
158+
ENET->GALR = hash_table[0];
159+
}
160+
157161
static bool mg_tcpip_driver_imxrt_poll(struct mg_tcpip_if *ifp, bool s1) {
162+
if (ifp->update_mac_hash_table) {
163+
mg_tcpip_driver_imxrt_update_hash_table(ifp);
164+
ifp->update_mac_hash_table = false;
165+
}
158166
if (!s1) return false;
159167
struct mg_tcpip_driver_imxrt_data *d =
160168
(struct mg_tcpip_driver_imxrt_data *) ifp->driver_data;

src/drivers/pico-w.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,9 @@ static bool mg_tcpip_driver_pico_w_init(struct mg_tcpip_if *ifp) {
1717
MG_DEBUG(("Starting AP '%s' (%u)", d->apssid, d->apchannel));
1818
if (!mg_wifi_ap_start(d->apssid, d->appass, d->apchannel)) return false;
1919
cyw43_wifi_get_mac(&cyw43_state, CYW43_ITF_STA, ifp->mac); // same MAC
20-
#if MG_TCPIP_MCAST
21-
cyw43_wifi_update_multicast_filter(&cyw43_state, (uint8_t *)mcast_addr, true);
22-
#endif
2320
} else {
2421
cyw43_arch_enable_sta_mode();
2522
cyw43_wifi_get_mac(&cyw43_state, CYW43_ITF_STA, ifp->mac);
26-
#if MG_TCPIP_MCAST
27-
cyw43_wifi_update_multicast_filter(&cyw43_state, (uint8_t *)mcast_addr, true);
28-
#endif
2923
if (d->ssid != NULL) {
3024
MG_DEBUG(("Connecting to '%s'", d->ssid));
3125
return mg_wifi_connect(d->ssid, d->pass);
@@ -57,6 +51,11 @@ static bool mg_tcpip_driver_pico_w_poll(struct mg_tcpip_if *ifp, bool s1) {
5751
s_scanning = 0;
5852
mg_tcpip_call(s_ifp, MG_TCPIP_EV_WIFI_SCAN_END, NULL);
5953
}
54+
if (ifp->update_mac_hash_table) {
55+
// first call to _poll() is after _init(), so this is safe
56+
cyw43_wifi_update_multicast_filter(&cyw43_state, (uint8_t *)mcast_addr, true);
57+
ifp->update_mac_hash_table = false;
58+
}
6059
if (!s1) return false;
6160
struct mg_tcpip_driver_pico_w_data *d =
6261
(struct mg_tcpip_driver_pico_w_data *) ifp->driver_data;

src/drivers/ra.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,7 @@ static bool mg_tcpip_driver_ra_init(struct mg_tcpip_if *ifp) {
174174
EDMAC->FDR = 0x070f; // (27.2.11)
175175
EDMAC->RMCR = MG_BIT(0); // (27.2.12)
176176
ETHERC->ECMR |= MG_BIT(6) | MG_BIT(5); // TE RE
177-
#if MG_TCPIP_MCAST
178-
EDMAC->EESIPR = MG_BIT(18) | MG_BIT(7); // FR, RMAF: Frame and mcast IRQ
179-
#else
180177
EDMAC->EESIPR = MG_BIT(18); // FR: Enable Rx (frame) IRQ
181-
#endif
182178
EDMAC->EDRRR = MG_BIT(0); // Receive Descriptors have changed
183179
EDMAC->EDTRR = MG_BIT(0); // Transmit Descriptors have changed
184180
return true;
@@ -206,6 +202,10 @@ static size_t mg_tcpip_driver_ra_tx(const void *buf, size_t len,
206202
}
207203

208204
static bool mg_tcpip_driver_ra_poll(struct mg_tcpip_if *ifp, bool s1) {
205+
if (ifp->update_mac_hash_table) {
206+
EDMAC->EESIPR = MG_BIT(18) | MG_BIT(7); // FR, RMAF: Frame and mcast IRQ
207+
ifp->update_mac_hash_table = false;
208+
}
209209
if (!s1) return false;
210210
struct mg_tcpip_driver_ra_data *d =
211211
(struct mg_tcpip_driver_ra_data *) ifp->driver_data;
@@ -232,11 +232,7 @@ static uint32_t s_rxno;
232232
void EDMAC_IRQHandler(void) {
233233
struct mg_tcpip_driver_ra_data *d =
234234
(struct mg_tcpip_driver_ra_data *) s_ifp->driver_data;
235-
#if MG_TCPIP_MCAST
236235
EDMAC->EESR = MG_BIT(18) | MG_BIT(7); // Ack IRQ in EDMAC 1st
237-
#else
238-
EDMAC->EESR = MG_BIT(18); // Ack IRQ in EDMAC 1st
239-
#endif
240236
ICU_IELSR[d->irqno] &= ~MG_BIT(16); // Ack IRQ in ICU last
241237
// Frame received, loop
242238
for (uint32_t i = 0; i < 10; i++) { // read as they arrive but not forever

src/drivers/rw612.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,7 @@ static bool mg_tcpip_driver_rw612_init(struct mg_tcpip_if *ifp) {
9292
ENET->IALR = 0;
9393
ENET->IAUR = 0;
9494
ENET->GALR = 0;
95-
#if MG_TCPIP_MCAST
96-
ENET->GAUR = MG_BIT(1); // see imxrt, it reduces to this for mDNS
97-
#else
9895
ENET->GAUR = 0;
99-
#endif
100-
10196
ENET->MSCR = ((d->mdc_cr & 0x3f) << 1) | ((d->mdc_holdtime & 7) << 8);
10297
ENET->EIMR = MG_BIT(25); // Enable RX interrupt
10398
ENET->ECR |= MG_BIT(8) | MG_BIT(1); // DBSWP, Enable
@@ -130,7 +125,18 @@ static size_t mg_tcpip_driver_rw612_tx(const void *buf, size_t len,
130125
return len;
131126
}
132127

133-
static bool mg_tcpip_driver_rw612_up(struct mg_tcpip_if *ifp) {
128+
129+
static mg_tcpip_driver_rw612_update_hash_table(struct mg_tcpip_if *ifp) {
130+
// TODO(): read database, rebuild hash table
131+
ENET->GAUR = MG_BIT(1); // see imxrt, it reduces to this for mDNS
132+
}
133+
134+
static bool mg_tcpip_driver_rw612_poll(struct mg_tcpip_if *ifp, bool s1) {
135+
if (ifp->update_mac_hash_table) {
136+
mg_tcpip_driver_rw612_update_hash_table(ifp);
137+
ifp->update_mac_hash_table = false;
138+
}
139+
if (!s1) return false;
134140
struct mg_tcpip_driver_rw612_data *d =
135141
(struct mg_tcpip_driver_rw612_data *) ifp->driver_data;
136142
uint8_t speed = MG_PHY_SPEED_10M;
@@ -186,5 +192,5 @@ void ENET_IRQHandler(void) {
186192

187193
struct mg_tcpip_driver mg_tcpip_driver_rw612 = {mg_tcpip_driver_rw612_init,
188194
mg_tcpip_driver_rw612_tx, NULL,
189-
mg_tcpip_driver_rw612_up};
195+
mg_tcpip_driver_rw612_poll};
190196
#endif

src/drivers/same54.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,6 @@ static bool mg_tcpip_driver_same54_init(struct mg_tcpip_if *ifp) {
124124
MG_U32(ifp->mac[3], ifp->mac[2], ifp->mac[1], ifp->mac[0]);
125125
GMAC_REGS->SA[0].GMAC_SAT = MG_U32(0, 0, ifp->mac[5], ifp->mac[4]);
126126

127-
#if MG_TCPIP_MCAST
128-
// Setting Hash Index for 01:00:5e:00:00:fb (multicast)
129-
// 24.6.9 Hash addressing
130-
// computed hash is 55, which means bit 23 (55 - 32) in
131-
// HRT register must be set
132-
GMAC_REGS->GMAC_HRT = MG_BIT(23);
133-
GMAC_REGS->GMAC_NCFGR |= MG_BIT(6); // enable multicast hash filtering
134-
#endif
135-
136127
GMAC_REGS->GMAC_UR &= ~GMAC_UR_MII_Msk; // Disable MII, use RMII
137128
GMAC_REGS->GMAC_NCFGR |= GMAC_NCFGR_MAXFS_Msk | GMAC_NCFGR_MTIHEN_Msk |
138129
GMAC_NCFGR_EFRHD_Msk | GMAC_NCFGR_CAF_Msk;
@@ -174,7 +165,21 @@ static size_t mg_tcpip_driver_same54_tx(const void *buf, size_t len,
174165
return len;
175166
}
176167

168+
static mg_tcpip_driver_same54_update_hash_table(struct mg_tcpip_if *ifp) {
169+
// TODO(): read database, rebuild hash table
170+
// Setting Hash Index for 01:00:5e:00:00:fb (multicast)
171+
// 24.6.9 Hash addressing
172+
// computed hash is 55, which means bit 23 (55 - 32) in
173+
// HRT register must be set
174+
GMAC_REGS->GMAC_HRT = MG_BIT(23);
175+
GMAC_REGS->GMAC_NCFGR |= MG_BIT(6); // enable multicast hash filtering
176+
}
177+
177178
static bool mg_tcpip_driver_same54_poll(struct mg_tcpip_if *ifp, bool s1) {
179+
if (ifp->update_mac_hash_table) {
180+
mg_tcpip_driver_same54_update_hash_table(ifp);
181+
ifp->update_mac_hash_table = false;
182+
}
178183
if (s1) {
179184
uint16_t bsr = eth_read_phy(MG_PHY_ADDR, MG_PHYREG_BSR);
180185
bool up = bsr & MG_PHYREGBIT_BSR_LINK_STATUS ? 1 : 0;

src/drivers/stm32f.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,6 @@ static bool mg_tcpip_driver_stm32f_init(struct mg_tcpip_if *ifp) {
152152
ETH->MACA0LR = (uint32_t) (ifp->mac[3] << 24) |
153153
((uint32_t) ifp->mac[2] << 16) |
154154
((uint32_t) ifp->mac[1] << 8) | ifp->mac[0];
155-
#if 0 //MG_TCPIP_MCAST
156-
// enable multicast
157-
ETH->MACA1LR = (uint32_t) mcast_addr[3] << 24 |
158-
(uint32_t) mcast_addr[2] << 16 |
159-
(uint32_t) mcast_addr[1] << 8 | (uint32_t) mcast_addr[0];
160-
ETH->MACA1HR = (uint32_t) mcast_addr[5] << 8 | (uint32_t) mcast_addr[4];
161-
ETH->MACA1HR |= MG_BIT(31); // AE
162-
#endif
163155
return true;
164156
}
165157

@@ -186,7 +178,20 @@ static size_t mg_tcpip_driver_stm32f_tx(const void *buf, size_t len,
186178
return len;
187179
}
188180

181+
static mg_tcpip_driver_stm32f_update_hash_table(struct mg_tcpip_if *ifp) {
182+
// TODO(): read database, rebuild hash table
183+
ETH->MACA1LR = (uint32_t) mcast_addr[3] << 24 |
184+
(uint32_t) mcast_addr[2] << 16 |
185+
(uint32_t) mcast_addr[1] << 8 | (uint32_t) mcast_addr[0];
186+
ETH->MACA1HR = (uint32_t) mcast_addr[5] << 8 | (uint32_t) mcast_addr[4];
187+
ETH->MACA1HR |= MG_BIT(31); // AE
188+
}
189+
189190
static bool mg_tcpip_driver_stm32f_poll(struct mg_tcpip_if *ifp, bool s1) {
191+
if (ifp->update_mac_hash_table) {
192+
mg_tcpip_driver_stm32f_update_hash_table(ifp);
193+
ifp->update_mac_hash_table = false;
194+
}
190195
if (!s1) return false;
191196
struct mg_tcpip_driver_stm32f_data *d =
192197
(struct mg_tcpip_driver_stm32f_data *) ifp->driver_data;

0 commit comments

Comments
 (0)