mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-01 13:10:43 +00:00
Added MDNS and driver support for multicast
This commit is contained in:
1 parent
b44193780e
commit
6f697db49d
31 files changed
+766
-168
No files matched your search
@@ -40,6 +40,10 @@ static bool cmsis_init(struct mg_tcpip_if *ifp) {
|
||||
memcpy(&addr, ifp->mac, sizeof(addr));
|
||||
mac->SetMacAddress(&addr);
|
||||
}
|
||||
#if MG_TCPIP_MCAST
|
||||
memcpy(&addr, mcast_addr, sizeof(addr));
|
||||
mac->SetAddressFilter(&addr, 1);
|
||||
#endif
|
||||
phy->PowerControl(ARM_POWER_FULL);
|
||||
phy->SetInterface(cap.media_interface);
|
||||
phy->SetMode(ARM_ETH_PHY_AUTO_NEGOTIATE);
|
||||
|
||||
@@ -784,6 +784,10 @@ static bool cyw_init(uint8_t *mac) {
|
||||
MG_ERROR(("read MAC failed"));
|
||||
}
|
||||
}
|
||||
#if MG_TCPIP_MCAST
|
||||
val = 1; if (!cyw_ioctl_iovar_set2_(0, "mcast_list", (uint8_t *)&val, sizeof(val), (uint8_t *)mcast_addr, sizeof(mcast_addr))) return false;
|
||||
mg_delayms(50);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
// clang-format on
|
||||
|
||||
@@ -115,9 +115,21 @@ static bool mg_tcpip_driver_imxrt_init(struct mg_tcpip_if *ifp) {
|
||||
ENET->RDAR = MG_BIT(24); // Receive Descriptors have changed
|
||||
ENET->TDAR = MG_BIT(24); // Transmit Descriptors have changed
|
||||
// ENET->OPD = 0x10014;
|
||||
uint32_t hash_table[2] = {0, 0};
|
||||
ENET->IAUR = hash_table[1];
|
||||
ENET->IALR = hash_table[0];
|
||||
#if MG_TCPIP_MCAST
|
||||
// RM 37.3.4.3.2
|
||||
// uint8_t hash64 = ((~mg_crc32(0, mcast_addr, 6)) >> 26) & 0x3f;
|
||||
// hash_table[((uint8_t)hash64) >> 5] |= (1 << (hash64 & 0x1f));
|
||||
hash_table[1] = MG_BIT(1); // above reduces to this for mDNS addr
|
||||
#endif
|
||||
ENET->GAUR = hash_table[1];
|
||||
ENET->GALR = hash_table[0];
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// Transmit frame
|
||||
static size_t mg_tcpip_driver_imxrt_tx(const void *buf, size_t len,
|
||||
struct mg_tcpip_if *ifp) {
|
||||
|
||||
@@ -17,9 +17,15 @@ static bool mg_tcpip_driver_pico_w_init(struct mg_tcpip_if *ifp) {
|
||||
MG_DEBUG(("Starting AP '%s' (%u)", d->apssid, d->apchannel));
|
||||
if (!mg_wifi_ap_start(d->apssid, d->appass, d->apchannel)) return false;
|
||||
cyw43_wifi_get_mac(&cyw43_state, CYW43_ITF_STA, ifp->mac); // same MAC
|
||||
#if MG_TCPIP_MCAST
|
||||
cyw43_wifi_update_multicast_filter(&cyw43_state, (uint8_t *)mcast_addr, true);
|
||||
#endif
|
||||
} else {
|
||||
cyw43_arch_enable_sta_mode();
|
||||
cyw43_wifi_get_mac(&cyw43_state, CYW43_ITF_STA, ifp->mac);
|
||||
#if MG_TCPIP_MCAST
|
||||
cyw43_wifi_update_multicast_filter(&cyw43_state, (uint8_t *)mcast_addr, true);
|
||||
#endif
|
||||
if (d->ssid != NULL) {
|
||||
MG_DEBUG(("Connecting to '%s'", d->ssid));
|
||||
return mg_wifi_connect(d->ssid, d->pass);
|
||||
|
||||
+21
-10
@@ -101,9 +101,9 @@ static uint16_t smi_rd(uint16_t header) {
|
||||
pir = 0; // read, mdc = 0
|
||||
ETHERC->PIR = pir;
|
||||
raspin(s_smispin / 2); // 1/4 clock period, 300ns max access time
|
||||
data |= (uint16_t)(ETHERC->PIR & MG_BIT(3) ? 1 : 0); // read mdio
|
||||
raspin(s_smispin / 2); // 1/4 clock period
|
||||
pir |= MG_BIT(0); // mdc = 1
|
||||
data |= (uint16_t) (ETHERC->PIR & MG_BIT(3) ? 1 : 0); // read mdio
|
||||
raspin(s_smispin / 2); // 1/4 clock period
|
||||
pir |= MG_BIT(0); // mdc = 1
|
||||
ETHERC->PIR = pir;
|
||||
raspin(s_smispin);
|
||||
}
|
||||
@@ -111,11 +111,14 @@ static uint16_t smi_rd(uint16_t header) {
|
||||
}
|
||||
|
||||
static uint16_t raeth_read_phy(uint8_t addr, uint8_t reg) {
|
||||
return smi_rd((uint16_t)((1 << 14) | (2 << 12) | (addr << 7) | (reg << 2) | (2 << 0)));
|
||||
return smi_rd(
|
||||
(uint16_t) ((1 << 14) | (2 << 12) | (addr << 7) | (reg << 2) | (2 << 0)));
|
||||
}
|
||||
|
||||
static void raeth_write_phy(uint8_t addr, uint8_t reg, uint16_t val) {
|
||||
smi_wr((uint16_t)((1 << 14) | (1 << 12) | (addr << 7) | (reg << 2) | (2 << 0)), val);
|
||||
smi_wr(
|
||||
(uint16_t) ((1 << 14) | (1 << 12) | (addr << 7) | (reg << 2) | (2 << 0)),
|
||||
val);
|
||||
}
|
||||
|
||||
// MDC clock is generated manually; as per 802.3, it must not exceed 2.5MHz
|
||||
@@ -152,7 +155,7 @@ static bool mg_tcpip_driver_ra_init(struct mg_tcpip_if *ifp) {
|
||||
|
||||
MG_DEBUG(("PHY addr: %d, smispin: %d", d->phy_addr, s_smispin));
|
||||
struct mg_phy phy = {raeth_read_phy, raeth_write_phy};
|
||||
mg_phy_init(&phy, d->phy_addr, 0); // MAC clocks PHY
|
||||
mg_phy_init(&phy, d->phy_addr, 0); // MAC clocks PHY
|
||||
|
||||
// Select RMII mode,
|
||||
ETHERC->ECMR = MG_BIT(2) | MG_BIT(1); // 100M, Full-duplex, CRC
|
||||
@@ -171,9 +174,13 @@ static bool mg_tcpip_driver_ra_init(struct mg_tcpip_if *ifp) {
|
||||
EDMAC->FDR = 0x070f; // (27.2.11)
|
||||
EDMAC->RMCR = MG_BIT(0); // (27.2.12)
|
||||
ETHERC->ECMR |= MG_BIT(6) | MG_BIT(5); // TE RE
|
||||
EDMAC->EESIPR = MG_BIT(18); // Enable Rx IRQ
|
||||
EDMAC->EDRRR = MG_BIT(0); // Receive Descriptors have changed
|
||||
EDMAC->EDTRR = MG_BIT(0); // Transmit Descriptors have changed
|
||||
#if MG_TCPIP_MCAST
|
||||
EDMAC->EESIPR = MG_BIT(18) | MG_BIT(7); // FR, RMAF: Frame and mcast IRQ
|
||||
#else
|
||||
EDMAC->EESIPR = MG_BIT(18); // FR: Enable Rx (frame) IRQ
|
||||
#endif
|
||||
EDMAC->EDRRR = MG_BIT(0); // Receive Descriptors have changed
|
||||
EDMAC->EDTRR = MG_BIT(0); // Transmit Descriptors have changed
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -225,7 +232,11 @@ static uint32_t s_rxno;
|
||||
void EDMAC_IRQHandler(void) {
|
||||
struct mg_tcpip_driver_ra_data *d =
|
||||
(struct mg_tcpip_driver_ra_data *) s_ifp->driver_data;
|
||||
EDMAC->EESR = MG_BIT(18); // Ack IRQ in EDMAC 1st
|
||||
#if MG_TCPIP_MCAST
|
||||
EDMAC->EESR = MG_BIT(18) | MG_BIT(7); // Ack IRQ in EDMAC 1st
|
||||
#else
|
||||
EDMAC->EESR = MG_BIT(18); // Ack IRQ in EDMAC 1st
|
||||
#endif
|
||||
ICU_IELSR[d->irqno] &= ~MG_BIT(16); // Ack IRQ in ICU last
|
||||
// Frame received, loop
|
||||
for (uint32_t i = 0; i < 10; i++) { // read as they arrive but not forever
|
||||
|
||||
+10
-1
@@ -89,6 +89,15 @@ static bool mg_tcpip_driver_rw612_init(struct mg_tcpip_if *ifp) {
|
||||
ENET->PALR =
|
||||
ifp->mac[0] << 24 | ifp->mac[1] << 16 | ifp->mac[2] << 8 | ifp->mac[3];
|
||||
ENET->PAUR |= (ifp->mac[4] << 24 | ifp->mac[5] << 16);
|
||||
ENET->IALR = 0;
|
||||
ENET->IAUR = 0;
|
||||
ENET->GALR = 0;
|
||||
#if MG_TCPIP_MCAST
|
||||
ENET->GAUR = MG_BIT(1); // see imxrt, it reduces to this for mDNS
|
||||
#else
|
||||
ENET->GAUR = 0;
|
||||
#endif
|
||||
|
||||
ENET->MSCR = ((d->mdc_cr & 0x3f) << 1) | ((d->mdc_holdtime & 7) << 8);
|
||||
ENET->EIMR = MG_BIT(25); // Enable RX interrupt
|
||||
ENET->ECR |= MG_BIT(8) | MG_BIT(1); // DBSWP, Enable
|
||||
@@ -157,7 +166,7 @@ static bool mg_tcpip_driver_rw612_up(struct mg_tcpip_if *ifp) {
|
||||
|
||||
void ENET_IRQHandler(void) {
|
||||
if (ENET->EIR & MG_BIT(25)) {
|
||||
ENET->EIR = MG_BIT(25); // Ack RX
|
||||
ENET->EIR = MG_BIT(25); // Ack RX
|
||||
for (uint32_t i = 0; i < 10; i++) { // read as they arrive but not forever
|
||||
if ((s_rxdesc[s_rxno][0] & MG_BIT(31)) != 0) break; // exit when done
|
||||
// skip partial/errored frames
|
||||
|
||||
@@ -124,6 +124,15 @@ static bool mg_tcpip_driver_same54_init(struct mg_tcpip_if *ifp) {
|
||||
MG_U32(ifp->mac[3], ifp->mac[2], ifp->mac[1], ifp->mac[0]);
|
||||
GMAC_REGS->SA[0].GMAC_SAT = MG_U32(0, 0, ifp->mac[5], ifp->mac[4]);
|
||||
|
||||
#if MG_TCPIP_MCAST
|
||||
// Setting Hash Index for 01:00:5e:00:00:fb (multicast)
|
||||
// 24.6.9 Hash addressing
|
||||
// computed hash is 55, which means bit 23 (55 - 32) in
|
||||
// HRT register must be set
|
||||
GMAC_REGS->GMAC_HRT = MG_BIT(23);
|
||||
GMAC_REGS->GMAC_NCFGR |= MG_BIT(6); // enable multicast hash filtering
|
||||
#endif
|
||||
|
||||
GMAC_REGS->GMAC_UR &= ~GMAC_UR_MII_Msk; // Disable MII, use RMII
|
||||
GMAC_REGS->GMAC_NCFGR |= GMAC_NCFGR_MAXFS_Msk | GMAC_NCFGR_MTIHEN_Msk |
|
||||
GMAC_NCFGR_EFRHD_Msk | GMAC_NCFGR_CAF_Msk;
|
||||
|
||||
@@ -136,7 +136,7 @@ static bool mg_tcpip_driver_stm32f_init(struct mg_tcpip_if *ifp) {
|
||||
// MG_BIT(25);
|
||||
ETH->MACIMR = MG_BIT(3) | MG_BIT(9); // Mask timestamp & PMT IT
|
||||
ETH->MACFCR = MG_BIT(7); // Disable zero quarta pause
|
||||
// ETH->MACFFR = MG_BIT(31); // Receive all
|
||||
ETH->MACFFR = MG_BIT(10); // Perfect filtering
|
||||
struct mg_phy phy = {eth_read_phy, eth_write_phy};
|
||||
mg_phy_init(&phy, phy_addr, MG_PHY_CLOCKS_MAC);
|
||||
ETH->DMARDLAR = (uint32_t) (uintptr_t) s_rxdesc; // RX descriptors
|
||||
@@ -152,6 +152,14 @@ static bool mg_tcpip_driver_stm32f_init(struct mg_tcpip_if *ifp) {
|
||||
ETH->MACA0LR = (uint32_t) (ifp->mac[3] << 24) |
|
||||
((uint32_t) ifp->mac[2] << 16) |
|
||||
((uint32_t) ifp->mac[1] << 8) | ifp->mac[0];
|
||||
#if MG_TCPIP_MCAST
|
||||
// enable multicast
|
||||
ETH->MACA1LR = (uint32_t) mcast_addr[3] << 24 |
|
||||
(uint32_t) mcast_addr[2] << 16 |
|
||||
(uint32_t) mcast_addr[1] << 8 | (uint32_t) mcast_addr[0];
|
||||
ETH->MACA1HR = (uint32_t) mcast_addr[5] << 8 | (uint32_t) mcast_addr[4];
|
||||
ETH->MACA1HR |= MG_BIT(31); // AE
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
+16
-2
@@ -2,7 +2,7 @@
|
||||
|
||||
#if MG_ENABLE_TCPIP && (MG_ENABLE_DRIVER_STM32H || MG_ENABLE_DRIVER_MCXN)
|
||||
// STM32H: vendor modded single-queue Synopsys v4.2
|
||||
// MCXNx4x: dual-queue Synopsys v5.2
|
||||
// MCXNx4x: dual-queue Synopsys v5.2 with no hash table option
|
||||
// RT1170 ENET_QOS: quad-queue Synopsys v5.1
|
||||
struct synopsys_enet_qos {
|
||||
volatile uint32_t MACCR, MACECR, MACPFR, MACWTR, MACHT0R, MACHT1R,
|
||||
@@ -102,7 +102,9 @@ static bool mg_tcpip_driver_stm32h_init(struct mg_tcpip_if *ifp) {
|
||||
ETH->DMASBMR |= MG_BIT(12); // AAL NOTE(scaprile): is this actually needed
|
||||
ETH->MACIER = 0; // Do not enable additional irq sources (reset value)
|
||||
ETH->MACTFCR = MG_BIT(7); // Disable zero-quanta pause
|
||||
// ETH->MACPFR = MG_BIT(31); // Receive all
|
||||
#if !MG_ENABLE_DRIVER_MCXN
|
||||
ETH->MACPFR = MG_BIT(10); // Perfect filtering
|
||||
#endif
|
||||
struct mg_phy phy = {eth_read_phy, eth_write_phy};
|
||||
mg_phy_init(&phy, phy_addr, phy_conf);
|
||||
ETH->DMACRDLAR =
|
||||
@@ -140,6 +142,18 @@ static bool mg_tcpip_driver_stm32h_init(struct mg_tcpip_if *ifp) {
|
||||
ETH->MACA0LR = (uint32_t) (ifp->mac[3] << 24) |
|
||||
((uint32_t) ifp->mac[2] << 16) |
|
||||
((uint32_t) ifp->mac[1] << 8) | ifp->mac[0];
|
||||
#if MG_TCPIP_MCAST
|
||||
#if MG_ENABLE_DRIVER_MCXN
|
||||
ETH->MACPFR = MG_BIT(4); // Pass Multicast (pass all multicast frames)
|
||||
#else
|
||||
// add mDNS / DNS-SD multicast address
|
||||
ETH->MACA1LR = (uint32_t) mcast_addr[3] << 24 |
|
||||
(uint32_t) mcast_addr[2] << 16 |
|
||||
(uint32_t) mcast_addr[1] << 8 | (uint32_t) mcast_addr[0];
|
||||
ETH->MACA1HR = (uint32_t) mcast_addr[5] << 8 | (uint32_t) mcast_addr[4];
|
||||
ETH->MACA1HR |= MG_BIT(31); // AE
|
||||
#endif
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
+9
-3
@@ -160,7 +160,7 @@ static bool mg_tcpip_driver_tm4c_init(struct mg_tcpip_if *ifp) {
|
||||
// EMAC->EMACDMABUSMOD = MG_BIT(13) | MG_BIT(16) | MG_BIT(22) | MG_BIT(23) | MG_BIT(25);
|
||||
EMAC->EMACIM = MG_BIT(3) | MG_BIT(9); // Mask timestamp & PMT IT
|
||||
EMAC->EMACFLOWCTL = MG_BIT(7); // Disable zero-quanta pause
|
||||
// EMAC->EMACFRAMEFLTR = MG_BIT(31); // Receive all
|
||||
EMAC->EMACFRAMEFLTR = MG_BIT(10); // Perfect filtering
|
||||
// EMAC->EMACPC defaults to internal PHY (EPHY) in MMI mode
|
||||
emac_write_phy(EPHY_ADDR, EPHYBMCR, MG_BIT(15)); // Reset internal PHY (EPHY)
|
||||
emac_write_phy(EPHY_ADDR, EPHYBMCR, MG_BIT(12)); // Set autonegotiation
|
||||
@@ -174,8 +174,14 @@ static bool mg_tcpip_driver_tm4c_init(struct mg_tcpip_if *ifp) {
|
||||
EMAC->EMACADDR0L = (uint32_t) (ifp->mac[3] << 24) |
|
||||
((uint32_t) ifp->mac[2] << 16) |
|
||||
((uint32_t) ifp->mac[1] << 8) | ifp->mac[0];
|
||||
// NOTE(scaprile) There are 3 additional slots for filtering, disabled by
|
||||
// default. This also applies to the STM32 driver (at least for F7)
|
||||
#if MG_TCPIP_MCAST
|
||||
// add mDNS / DNS-SD multicast address
|
||||
EMAC->EMACADDR1L = (uint32_t) mcast_addr[3] << 24 |
|
||||
(uint32_t) mcast_addr[2] << 16 |
|
||||
(uint32_t) mcast_addr[1] << 8 | (uint32_t) mcast_addr[0];
|
||||
EMAC->EMACADDR1H = (uint32_t) mcast_addr[5] << 8 | (uint32_t) mcast_addr[4];
|
||||
EMAC->EMACADDR1H |= MG_BIT(31); // AE
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
+11
-6
@@ -105,10 +105,6 @@ static bool mg_tcpip_driver_tms570_init(struct mg_tcpip_if *ifp) {
|
||||
while (delay-- != 0) (void) 0;
|
||||
struct mg_phy phy = {emac_read_phy, emac_write_phy};
|
||||
mg_phy_init(&phy, d->phy_addr, MG_PHY_CLOCKS_MAC);
|
||||
// set the mac address
|
||||
EMAC->MACSRCADDRHI = ifp->mac[0] | (ifp->mac[1] << 8) | (ifp->mac[2] << 16) |
|
||||
(ifp->mac[3] << 24);
|
||||
EMAC->MACSRCADDRLO = ifp->mac[4] | (ifp->mac[5] << 8);
|
||||
uint32_t channel;
|
||||
for (channel = 0; channel < 8; channel++) {
|
||||
EMAC->MACINDEX = channel;
|
||||
@@ -118,8 +114,17 @@ static bool mg_tcpip_driver_tms570_init(struct mg_tcpip_if *ifp) {
|
||||
MG_BIT(19) | (channel << 16);
|
||||
}
|
||||
EMAC->RXUNICASTSET = 1; // accept unicast frames;
|
||||
EMAC->RXMBPENABLE = MG_BIT(30) | MG_BIT(13); // CRC, broadcast;
|
||||
|
||||
|
||||
#if MG_TCPIP_MCAST
|
||||
// Setting Hash Index for 01:00:5e:00:00:fb (multicast)
|
||||
// using TMS570 XOR method (32.5.37).
|
||||
// computed hash is 55, which means bit 23 (55 - 32) in
|
||||
// HASH2 register must be set
|
||||
EMAC->MACHASH2 = MG_BIT(23);
|
||||
EMAC->RXMBPENABLE = MG_BIT(5); // enable hash filtering
|
||||
#endif
|
||||
EMAC->RXMBPENABLE |= MG_BIT(30) | MG_BIT(13); // CRC, broadcast
|
||||
|
||||
// Initialize the descriptors
|
||||
for (i = 0; i < ETH_DESC_CNT; i++) {
|
||||
if (i < ETH_DESC_CNT - 1) {
|
||||
|
||||
+2
-1
@@ -88,9 +88,10 @@ static bool w5100_init(struct mg_tcpip_if *ifp) {
|
||||
w5100_w1(s, 0x46, 0); // CR PHYCR0 -> autonegotiation
|
||||
w5100_w1(s, 0x47, 0); // CR PHYCR1 -> reset
|
||||
w5100_w1(s, 0x72, 0x00); // CR PHYLCKR -> lock PHY
|
||||
w5100_wn(s, 0x09, ifp->mac, 6); // SHAR
|
||||
w5100_w1(s, 0x1a, 6); // Sock0 RX buf size - 4KB
|
||||
w5100_w1(s, 0x1b, 6); // Sock0 TX buf size - 4KB
|
||||
w5100_w1(s, 0x400, 4); // Sock0 MR -> MACRAW
|
||||
w5100_w1(s, 0x400, 0x44); // Sock0 MR -> MACRAW, MAC filter
|
||||
w5100_w1(s, 0x401, 1); // Sock0 CR -> OPEN
|
||||
return w5100_r1(s, 0x403) == 0x42; // Sock0 SR == MACRAW
|
||||
}
|
||||
|
||||
+60
-52
@@ -4,34 +4,34 @@
|
||||
|
||||
struct ETH_GLOBAL_TypeDef {
|
||||
volatile uint32_t MAC_CONFIGURATION, MAC_FRAME_FILTER, HASH_TABLE_HIGH,
|
||||
HASH_TABLE_LOW, GMII_ADDRESS, GMII_DATA, FLOW_CONTROL, VLAN_TAG, VERSION,
|
||||
DEBUG, REMOTE_WAKE_UP_FRAME_FILTER, PMT_CONTROL_STATUS, RESERVED[2],
|
||||
INTERRUPT_STATUS, INTERRUPT_MASK, MAC_ADDRESS0_HIGH, MAC_ADDRESS0_LOW,
|
||||
MAC_ADDRESS1_HIGH, MAC_ADDRESS1_LOW, MAC_ADDRESS2_HIGH, MAC_ADDRESS2_LOW,
|
||||
MAC_ADDRESS3_HIGH, MAC_ADDRESS3_LOW, RESERVED1[40], MMC_CONTROL,
|
||||
MMC_RECEIVE_INTERRUPT, MMC_TRANSMIT_INTERRUPT, MMC_RECEIVE_INTERRUPT_MASK,
|
||||
MMC_TRANSMIT_INTERRUPT_MASK, TX_STATISTICS[26], RESERVED2,
|
||||
RX_STATISTICS_1[26], RESERVED3[6], MMC_IPC_RECEIVE_INTERRUPT_MASK,
|
||||
RESERVED4, MMC_IPC_RECEIVE_INTERRUPT, RESERVED5, RX_STATISTICS_2[30],
|
||||
RESERVED7[286], TIMESTAMP_CONTROL, SUB_SECOND_INCREMENT,
|
||||
SYSTEM_TIME_SECONDS, SYSTEM_TIME_NANOSECONDS,
|
||||
SYSTEM_TIME_SECONDS_UPDATE, SYSTEM_TIME_NANOSECONDS_UPDATE,
|
||||
TIMESTAMP_ADDEND, TARGET_TIME_SECONDS, TARGET_TIME_NANOSECONDS,
|
||||
SYSTEM_TIME_HIGHER_WORD_SECONDS, TIMESTAMP_STATUS,
|
||||
PPS_CONTROL, RESERVED8[564], BUS_MODE, TRANSMIT_POLL_DEMAND,
|
||||
RECEIVE_POLL_DEMAND, RECEIVE_DESCRIPTOR_LIST_ADDRESS,
|
||||
TRANSMIT_DESCRIPTOR_LIST_ADDRESS, STATUS, OPERATION_MODE,
|
||||
INTERRUPT_ENABLE, MISSED_FRAME_AND_BUFFER_OVERFLOW_COUNTER,
|
||||
RECEIVE_INTERRUPT_WATCHDOG_TIMER, RESERVED9, AHB_STATUS,
|
||||
RESERVED10[6], CURRENT_HOST_TRANSMIT_DESCRIPTOR,
|
||||
CURRENT_HOST_RECEIVE_DESCRIPTOR, CURRENT_HOST_TRANSMIT_BUFFER_ADDRESS,
|
||||
CURRENT_HOST_RECEIVE_BUFFER_ADDRESS, HW_FEATURE;
|
||||
HASH_TABLE_LOW, GMII_ADDRESS, GMII_DATA, FLOW_CONTROL, VLAN_TAG, VERSION,
|
||||
DEBUG, REMOTE_WAKE_UP_FRAME_FILTER, PMT_CONTROL_STATUS, RESERVED[2],
|
||||
INTERRUPT_STATUS, INTERRUPT_MASK, MAC_ADDRESS0_HIGH, MAC_ADDRESS0_LOW,
|
||||
MAC_ADDRESS1_HIGH, MAC_ADDRESS1_LOW, MAC_ADDRESS2_HIGH, MAC_ADDRESS2_LOW,
|
||||
MAC_ADDRESS3_HIGH, MAC_ADDRESS3_LOW, RESERVED1[40], MMC_CONTROL,
|
||||
MMC_RECEIVE_INTERRUPT, MMC_TRANSMIT_INTERRUPT, MMC_RECEIVE_INTERRUPT_MASK,
|
||||
MMC_TRANSMIT_INTERRUPT_MASK, TX_STATISTICS[26], RESERVED2,
|
||||
RX_STATISTICS_1[26], RESERVED3[6], MMC_IPC_RECEIVE_INTERRUPT_MASK,
|
||||
RESERVED4, MMC_IPC_RECEIVE_INTERRUPT, RESERVED5, RX_STATISTICS_2[30],
|
||||
RESERVED7[286], TIMESTAMP_CONTROL, SUB_SECOND_INCREMENT,
|
||||
SYSTEM_TIME_SECONDS, SYSTEM_TIME_NANOSECONDS, SYSTEM_TIME_SECONDS_UPDATE,
|
||||
SYSTEM_TIME_NANOSECONDS_UPDATE, TIMESTAMP_ADDEND, TARGET_TIME_SECONDS,
|
||||
TARGET_TIME_NANOSECONDS, SYSTEM_TIME_HIGHER_WORD_SECONDS,
|
||||
TIMESTAMP_STATUS, PPS_CONTROL, RESERVED8[564], BUS_MODE,
|
||||
TRANSMIT_POLL_DEMAND, RECEIVE_POLL_DEMAND,
|
||||
RECEIVE_DESCRIPTOR_LIST_ADDRESS, TRANSMIT_DESCRIPTOR_LIST_ADDRESS, STATUS,
|
||||
OPERATION_MODE, INTERRUPT_ENABLE,
|
||||
MISSED_FRAME_AND_BUFFER_OVERFLOW_COUNTER,
|
||||
RECEIVE_INTERRUPT_WATCHDOG_TIMER, RESERVED9, AHB_STATUS, RESERVED10[6],
|
||||
CURRENT_HOST_TRANSMIT_DESCRIPTOR, CURRENT_HOST_RECEIVE_DESCRIPTOR,
|
||||
CURRENT_HOST_TRANSMIT_BUFFER_ADDRESS, CURRENT_HOST_RECEIVE_BUFFER_ADDRESS,
|
||||
HW_FEATURE;
|
||||
};
|
||||
|
||||
#undef ETH0
|
||||
#define ETH0 ((struct ETH_GLOBAL_TypeDef*) 0x5000C000UL)
|
||||
#define ETH0 ((struct ETH_GLOBAL_TypeDef *) 0x5000C000UL)
|
||||
|
||||
#define ETH_PKT_SIZE 1536 // Max frame size
|
||||
#define ETH_PKT_SIZE 1536 // Max frame size
|
||||
#define ETH_DESC_CNT 4 // Descriptors count
|
||||
#define ETH_DS 4 // Descriptor size (words)
|
||||
|
||||
@@ -43,27 +43,27 @@ struct ETH_GLOBAL_TypeDef {
|
||||
|
||||
static uint8_t s_rxbuf[ETH_DESC_CNT][ETH_PKT_SIZE] ETH_RAM_SECTION;
|
||||
static uint8_t s_txbuf[ETH_DESC_CNT][ETH_PKT_SIZE] ETH_RAM_SECTION;
|
||||
static uint32_t s_rxdesc[ETH_DESC_CNT][ETH_DS] ETH_RAM_SECTION; // RX descriptors
|
||||
static uint32_t s_txdesc[ETH_DESC_CNT][ETH_DS] ETH_RAM_SECTION; // TX descriptors
|
||||
static uint8_t s_txno; // Current TX descriptor
|
||||
static uint8_t s_rxno; // Current RX descriptor
|
||||
static uint32_t s_rxdesc[ETH_DESC_CNT]
|
||||
[ETH_DS] ETH_RAM_SECTION; // RX descriptors
|
||||
static uint32_t s_txdesc[ETH_DESC_CNT]
|
||||
[ETH_DS] ETH_RAM_SECTION; // TX descriptors
|
||||
static uint8_t s_txno; // Current TX descriptor
|
||||
static uint8_t s_rxno; // Current RX descriptor
|
||||
|
||||
static struct mg_tcpip_if *s_ifp; // MIP interface
|
||||
enum { MG_PHY_ADDR = 0, MG_PHYREG_BCR = 0, MG_PHYREG_BSR = 1 };
|
||||
|
||||
static uint16_t eth_read_phy(uint8_t addr, uint8_t reg) {
|
||||
ETH0->GMII_ADDRESS = (ETH0->GMII_ADDRESS & 0x3c) |
|
||||
((uint32_t)addr << 11) |
|
||||
((uint32_t)reg << 6) | 1;
|
||||
ETH0->GMII_ADDRESS = (ETH0->GMII_ADDRESS & 0x3c) | ((uint32_t) addr << 11) |
|
||||
((uint32_t) reg << 6) | 1;
|
||||
while ((ETH0->GMII_ADDRESS & 1) != 0) (void) 0;
|
||||
return (uint16_t)(ETH0->GMII_DATA & 0xffff);
|
||||
return (uint16_t) (ETH0->GMII_DATA & 0xffff);
|
||||
}
|
||||
|
||||
static void eth_write_phy(uint8_t addr, uint8_t reg, uint16_t val) {
|
||||
ETH0->GMII_DATA = val;
|
||||
ETH0->GMII_ADDRESS = (ETH0->GMII_ADDRESS & 0x3c) |
|
||||
((uint32_t)addr << 11) |
|
||||
((uint32_t)reg << 6) | 3;
|
||||
ETH0->GMII_DATA = val;
|
||||
ETH0->GMII_ADDRESS = (ETH0->GMII_ADDRESS & 0x3c) | ((uint32_t) addr << 11) |
|
||||
((uint32_t) reg << 6) | 3;
|
||||
while ((ETH0->GMII_ADDRESS & 1) != 0) (void) 0;
|
||||
}
|
||||
|
||||
@@ -98,22 +98,30 @@ static bool mg_tcpip_driver_xmc_init(struct mg_tcpip_if *ifp) {
|
||||
|
||||
// set the MAC address
|
||||
ETH0->MAC_ADDRESS0_HIGH = MG_U32(0, 0, ifp->mac[5], ifp->mac[4]);
|
||||
ETH0->MAC_ADDRESS0_LOW =
|
||||
MG_U32(ifp->mac[3], ifp->mac[2], ifp->mac[1], ifp->mac[0]);
|
||||
ETH0->MAC_ADDRESS0_LOW =
|
||||
MG_U32(ifp->mac[3], ifp->mac[2], ifp->mac[1], ifp->mac[0]);
|
||||
|
||||
#if MG_TCPIP_MCAST
|
||||
// set the multicast address filter
|
||||
ETH0->MAC_ADDRESS1_HIGH =
|
||||
MG_U32(0, 0, mcast_addr[5], mcast_addr[4]) | MG_BIT(31);
|
||||
ETH0->MAC_ADDRESS1_LOW =
|
||||
MG_U32(mcast_addr[3], mcast_addr[2], mcast_addr[1], mcast_addr[0]);
|
||||
#endif
|
||||
|
||||
// Configure the receive filter
|
||||
ETH0->MAC_FRAME_FILTER = MG_BIT(10) | MG_BIT(2); // HFP, HMC
|
||||
ETH0->MAC_FRAME_FILTER = MG_BIT(10); // Perfect filter
|
||||
// Disable flow control
|
||||
ETH0->FLOW_CONTROL = 0;
|
||||
// Enable store and forward mode
|
||||
ETH0->OPERATION_MODE = MG_BIT(25) | MG_BIT(21); // RSF, TSF
|
||||
ETH0->OPERATION_MODE = MG_BIT(25) | MG_BIT(21); // RSF, TSF
|
||||
|
||||
// Configure DMA bus mode (AAL, USP, RPBL, PBL)
|
||||
ETH0->BUS_MODE = MG_BIT(25) | MG_BIT(23) | (32 << 17) | (32 << 8);
|
||||
ETH0->BUS_MODE = MG_BIT(25) | MG_BIT(23) | (32 << 17) | (32 << 8);
|
||||
|
||||
// init RX descriptors
|
||||
for (int i = 0; i < ETH_DESC_CNT; i++) {
|
||||
s_rxdesc[i][0] = MG_BIT(31); // OWN descriptor
|
||||
s_rxdesc[i][0] = MG_BIT(31); // OWN descriptor
|
||||
s_rxdesc[i][1] = MG_BIT(14) | ETH_PKT_SIZE;
|
||||
s_rxdesc[i][2] = (uint32_t) s_rxbuf[i];
|
||||
if (i == ETH_DESC_CNT - 1) {
|
||||
@@ -143,9 +151,9 @@ static bool mg_tcpip_driver_xmc_init(struct mg_tcpip_if *ifp) {
|
||||
ETH0->MMC_TRANSMIT_INTERRUPT_MASK = 0xFFFFFFFF;
|
||||
ETH0->MMC_RECEIVE_INTERRUPT_MASK = 0xFFFFFFFF;
|
||||
ETH0->MMC_IPC_RECEIVE_INTERRUPT_MASK = 0xFFFFFFFF;
|
||||
ETH0->INTERRUPT_MASK = MG_BIT(9) | MG_BIT(3); // TSIM, PMTIM
|
||||
ETH0->INTERRUPT_MASK = MG_BIT(9) | MG_BIT(3); // TSIM, PMTIM
|
||||
|
||||
//Enable interrupts (NIE, RIE, TIE)
|
||||
// Enable interrupts (NIE, RIE, TIE)
|
||||
ETH0->INTERRUPT_ENABLE = MG_BIT(16) | MG_BIT(6) | MG_BIT(0);
|
||||
|
||||
// Enable MAC transmission and reception (TE, RE)
|
||||
@@ -156,7 +164,7 @@ static bool mg_tcpip_driver_xmc_init(struct mg_tcpip_if *ifp) {
|
||||
}
|
||||
|
||||
static size_t mg_tcpip_driver_xmc_tx(const void *buf, size_t len,
|
||||
struct mg_tcpip_if *ifp) {
|
||||
struct mg_tcpip_if *ifp) {
|
||||
if (len > sizeof(s_txbuf[s_txno])) {
|
||||
MG_ERROR(("Frame too big, %ld", (long) len));
|
||||
len = 0; // Frame is too big
|
||||
@@ -174,7 +182,7 @@ static size_t mg_tcpip_driver_xmc_tx(const void *buf, size_t len,
|
||||
}
|
||||
|
||||
// Resume processing
|
||||
ETH0->STATUS = MG_BIT(2); // clear Transmit unavailable
|
||||
ETH0->STATUS = MG_BIT(2); // clear Transmit unavailable
|
||||
ETH0->TRANSMIT_POLL_DEMAND = 0;
|
||||
return len;
|
||||
}
|
||||
@@ -200,13 +208,13 @@ void ETH0_0_IRQHandler(void) {
|
||||
|
||||
// check if a frame was received
|
||||
if (irq_status & MG_BIT(6)) {
|
||||
for (uint8_t i = 0; i < 10; i++) { // read as they arrive, but not forever
|
||||
for (uint8_t i = 0; i < 10; i++) { // read as they arrive, but not forever
|
||||
if (s_rxdesc[s_rxno][0] & MG_BIT(31)) break;
|
||||
size_t len = (s_rxdesc[s_rxno][0] & 0x3fff0000) >> 16;
|
||||
mg_tcpip_qwrite(s_rxbuf[s_rxno], len, s_ifp);
|
||||
s_rxdesc[s_rxno][0] = MG_BIT(31); // OWN bit: handle control to DMA
|
||||
s_rxdesc[s_rxno][0] = MG_BIT(31); // OWN bit: handle control to DMA
|
||||
// Resume processing
|
||||
ETH0->STATUS = MG_BIT(7) | MG_BIT(6); // clear RU and RI
|
||||
ETH0->STATUS = MG_BIT(7) | MG_BIT(6); // clear RU and RI
|
||||
ETH0->RECEIVE_POLL_DEMAND = 0;
|
||||
if (++s_rxno >= ETH_DESC_CNT) s_rxno = 0;
|
||||
}
|
||||
@@ -224,7 +232,7 @@ void ETH0_0_IRQHandler(void) {
|
||||
}
|
||||
}
|
||||
|
||||
struct mg_tcpip_driver mg_tcpip_driver_xmc = {
|
||||
mg_tcpip_driver_xmc_init, mg_tcpip_driver_xmc_tx, NULL,
|
||||
mg_tcpip_driver_xmc_poll};
|
||||
struct mg_tcpip_driver mg_tcpip_driver_xmc = {mg_tcpip_driver_xmc_init,
|
||||
mg_tcpip_driver_xmc_tx, NULL,
|
||||
mg_tcpip_driver_xmc_poll};
|
||||
#endif
|
||||
+11
-5
@@ -66,8 +66,8 @@ static uint8_t s_rxbuf[ETH_DESC_CNT][ETH_PKT_SIZE];
|
||||
static uint8_t s_txbuf[ETH_DESC_CNT][ETH_PKT_SIZE];
|
||||
static uint32_t s_rxdesc[ETH_DESC_CNT][ETH_DS] MG_8BYTE_ALIGNED;
|
||||
static uint32_t s_txdesc[ETH_DESC_CNT][ETH_DS] MG_8BYTE_ALIGNED;
|
||||
static uint8_t s_txno MG_8BYTE_ALIGNED; // Current TX descriptor
|
||||
static uint8_t s_rxno MG_8BYTE_ALIGNED; // Current RX descriptor
|
||||
static uint8_t s_txno MG_8BYTE_ALIGNED; // Current TX descriptor
|
||||
static uint8_t s_rxno MG_8BYTE_ALIGNED; // Current RX descriptor
|
||||
|
||||
static struct mg_tcpip_if *s_ifp; // MIP interface
|
||||
enum { MG_PHY_ADDR = 0, MG_PHYREG_BCR = 0, MG_PHYREG_BSR = 1 };
|
||||
@@ -104,8 +104,7 @@ static bool mg_tcpip_driver_xmc7_init(struct mg_tcpip_if *ifp) {
|
||||
// set NSP change, ignore RX FCS, data bus width, clock rate
|
||||
// frame length 1536, full duplex, speed
|
||||
ETH0->NETWORK_CONFIG = MG_BIT(29) | MG_BIT(26) | MG_BIT(21) |
|
||||
((cr & 7) << 18) | MG_BIT(8) | MG_BIT(4) | MG_BIT(1) |
|
||||
MG_BIT(0);
|
||||
((cr & 7) << 18) | MG_BIT(8) | MG_BIT(1) | MG_BIT(0);
|
||||
|
||||
// config DMA settings: Force TX burst, Discard on Error, set RX buffer size
|
||||
// to 1536, TX_PBUF_SIZE, RX_PBUF_SIZE, AMBA_BURST_LENGTH
|
||||
@@ -142,6 +141,13 @@ static bool mg_tcpip_driver_xmc7_init(struct mg_tcpip_if *ifp) {
|
||||
ifp->mac[3] << 24 | ifp->mac[2] << 16 | ifp->mac[1] << 8 | ifp->mac[0];
|
||||
ETH0->SPEC_ADD1_TOP = ifp->mac[5] << 8 | ifp->mac[4];
|
||||
|
||||
#if MG_TCPIP_MCAST
|
||||
// set multicast MAC address
|
||||
ETH0->SPEC_ADD2_BOTTOM = mcast_addr[3] << 24 | mcast_addr[2] << 16 |
|
||||
mcast_addr[1] << 8 | mcast_addr[0];
|
||||
ETH0->SPEC_ADD2_TOP = mcast_addr[5] << 8 | mcast_addr[4];
|
||||
#endif
|
||||
|
||||
// enable MDIO, TX, RX
|
||||
ETH0->NETWORK_CONTROL = MG_BIT(4) | MG_BIT(3) | MG_BIT(2);
|
||||
|
||||
@@ -220,7 +226,7 @@ static bool mg_tcpip_driver_xmc7_poll(struct mg_tcpip_if *ifp, bool s1) {
|
||||
void ETH_IRQHandler(void) {
|
||||
uint32_t irq_status = ETH0->INT_STATUS;
|
||||
if (irq_status & MG_BIT(1)) {
|
||||
for (uint8_t i = 0; i < 10; i++) { // read as they arrive, but not forever
|
||||
for (uint8_t i = 0; i < 10; i++) { // read as they arrive, but not forever
|
||||
if ((s_rxdesc[s_rxno][0] & MG_BIT(0)) == 0) break;
|
||||
size_t len = s_rxdesc[s_rxno][1] & (MG_BIT(13) - 1);
|
||||
mg_tcpip_qwrite(s_rxbuf[s_rxno], len, s_ifp);
|
||||
|
||||
Reference in new issue
Block a user