Skip to content

Commit bf80550

Browse files
committed
refactor the rest of the drivers
1 parent d3a7490 commit bf80550

File tree

13 files changed

+364
-230
lines changed

13 files changed

+364
-230
lines changed

mongoose.c

Lines changed: 182 additions & 115 deletions
Large diffs are not rendered by default.

src/drivers/cyw.c

Lines changed: 16 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 (ifp->update_mac_hash_table) {
151+
// first call to _poll() is after _init(), so this is safe
152+
cyw_update_hash_table();
153+
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))
@@ -821,6 +827,16 @@ static bool cyw_load_clmll(void *data, size_t len) {
821827
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
}
830+
if (ifp->update_mac_hash_table) {
831+
mg_tcpip_driver_xmc7_update_hash_table(ifp);
832+
ifp->update_mac_hash_table = false;
833+
}
834+
835+
static void cyw_update_hash_table(void) {
836+
// TODO(): read database, rebuild hash table
837+
val = 1; cyw_ioctl_iovar_set2_(0, "mcast_list", (uint8_t *)&val, sizeof(val), (uint8_t *)mcast_addr, sizeof(mcast_addr));
838+
mg_delayms(50);
839+
}
824840

825841
// CYW43 chip backplane specifics. All values read and written are in little
826842
// endian format
@@ -1184,9 +1200,4 @@ bool mg_wifi_ap_stop(void) {
11841200
return cyw_wifi_ap_stop();
11851201
}
11861202

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-
11921203
#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;

src/drivers/stm32h.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,6 @@ static bool mg_tcpip_driver_stm32h_init(struct mg_tcpip_if *ifp) {
142142
ETH->MACA0LR = (uint32_t) (ifp->mac[3] << 24) |
143143
((uint32_t) ifp->mac[2] << 16) |
144144
((uint32_t) ifp->mac[1] << 8) | ifp->mac[0];
145-
#if MG_TCPIP_MCAST
146-
#if MG_ENABLE_DRIVER_MCXN
147-
ETH->MACPFR = MG_BIT(4); // Pass Multicast (pass all multicast frames)
148-
#else
149-
// add mDNS / DNS-SD multicast address
150-
ETH->MACA1LR = (uint32_t) mcast_addr[3] << 24 |
151-
(uint32_t) mcast_addr[2] << 16 |
152-
(uint32_t) mcast_addr[1] << 8 | (uint32_t) mcast_addr[0];
153-
ETH->MACA1HR = (uint32_t) mcast_addr[5] << 8 | (uint32_t) mcast_addr[4];
154-
ETH->MACA1HR |= MG_BIT(31); // AE
155-
#endif
156-
#endif
157145
return true;
158146
}
159147

@@ -182,7 +170,25 @@ static size_t mg_tcpip_driver_stm32h_tx(const void *buf, size_t len,
182170
(void) ifp;
183171
}
184172

173+
static mg_tcpip_driver_stm32h_update_hash_table(struct mg_tcpip_if *ifp) {
174+
#if MG_ENABLE_DRIVER_MCXN
175+
ETH->MACPFR = MG_BIT(4); // Pass Multicast (pass all multicast frames)
176+
#else
177+
// TODO(): read database, rebuild hash table
178+
// add mDNS / DNS-SD multicast address
179+
ETH->MACA1LR = (uint32_t) mcast_addr[3] << 24 |
180+
(uint32_t) mcast_addr[2] << 16 |
181+
(uint32_t) mcast_addr[1] << 8 | (uint32_t) mcast_addr[0];
182+
ETH->MACA1HR = (uint32_t) mcast_addr[5] << 8 | (uint32_t) mcast_addr[4];
183+
ETH->MACA1HR |= MG_BIT(31); // AE
184+
#endif
185+
}
186+
185187
static bool mg_tcpip_driver_stm32h_poll(struct mg_tcpip_if *ifp, bool s1) {
188+
if (ifp->update_mac_hash_table) {
189+
mg_tcpip_driver_stm32h_update_hash_table(ifp);
190+
ifp->update_mac_hash_table = false;
191+
}
186192
if (!s1) return false;
187193
struct mg_tcpip_driver_stm32h_data *d =
188194
(struct mg_tcpip_driver_stm32h_data *) ifp->driver_data;

0 commit comments

Comments
 (0)