From a59a53728da1cdcb47576a659a948be0f1edaf0d Mon Sep 17 00:00:00 2001 From: Adam Greenwood-Byrne Date: Sat, 30 Oct 2021 22:06:50 +0100 Subject: [PATCH] Ethernet now successfully ARPing - good milestone --- part14-ethernet/include/io.h | 2 ++ part14-ethernet/kernel/arp.c | 53 +++++++++++++++++++--------------- part14-ethernet/lib/spi.c | 15 ++++------ part14-ethernet/net/enc28j60.c | 15 +--------- part14-ethernet/net/encspi.c | 5 ++++ 5 files changed, 43 insertions(+), 47 deletions(-) diff --git a/part14-ethernet/include/io.h b/part14-ethernet/include/io.h index ff2fa98..0c79113 100644 --- a/part14-ethernet/include/io.h +++ b/part14-ethernet/include/io.h @@ -13,5 +13,7 @@ void mmio_write(long reg, unsigned int val); unsigned int mmio_read(long reg); void gpio_useAsAlt0(unsigned int pin_number); void gpio_useAsAlt3(unsigned int pin_number); +void gpio_setPinOutputBool(unsigned int pin_number, unsigned int onOrOff); +void gpio_initOutputPinWithPullNone(unsigned int pin_number); void uart_hex(unsigned int d); void uart_byte(unsigned char b); diff --git a/part14-ethernet/kernel/arp.c b/part14-ethernet/kernel/arp.c index be9af49..b3d6314 100644 --- a/part14-ethernet/kernel/arp.c +++ b/part14-ethernet/kernel/arp.c @@ -139,11 +139,12 @@ void SendArpPacket(uint8_t *targetIP, uint8_t *deviceMAC) // Send the packet if (ENC_RestoreTXBuffer(&handle, sizeof(ARP)) == 0) { - debugstr("Sending ARP request... "); + debugstr("Sending ARP request."); debugcrlf(); ENC_WriteBuffer((unsigned char *)&arpPacket, sizeof(ARP)); handle.transmitLength = sizeof(ARP); + ENC_Transmit(&handle); } } @@ -154,33 +155,32 @@ void arp_test(void) SendArpPacket(routerIP, myMAC); - debugstr("Waiting for ARP response... "); + debugstr("Waiting for ARP response."); debugcrlf(); while (1) { - while (!(handle.interruptFlags & EIR_PKTIF)) ENC_IRQHandler(&handle); + while (!ENC_GetReceivedFrame(&handle)); - debugstr("Got one... "); - ENC_GetReceivedFrame(&handle); - - //uint16_t len = handle.RxFrameInfos.length; + uint16_t len = handle.RxFrameInfos.length; uint8_t *buffer = (uint8_t *)handle.RxFrameInfos.buffer; checkPacket = (ARP *)buffer; - if (!memcmp(checkPacket->senderIP, routerIP, 4)) { - // Success! We have found our router's MAC address + if (len > 0) { + if (!memcmp(checkPacket->senderIP, routerIP, 4)) { + // Success! We have found our router's MAC address - memcpy(routerMAC, checkPacket->senderMAC, 6); - debugstr("Router MAC is "); - debughex(routerMAC[0]); - debughex(routerMAC[1]); - debughex(routerMAC[2]); - debughex(routerMAC[3]); - debughex(routerMAC[4]); - debughex(routerMAC[5]); - debugcrlf(); + memcpy(routerMAC, checkPacket->senderMAC, 6); + debugstr("Router MAC is "); + debughex(routerMAC[0]); + debughex(routerMAC[1]); + debughex(routerMAC[2]); + debughex(routerMAC[3]); + debughex(routerMAC[4]); + debughex(routerMAC[5]); + debugcrlf(); - break; + break; + } } } } @@ -197,15 +197,20 @@ void init_network(void) if (!ENC_Start(&handle)) { debugstr("Could not initialise network card."); } else { + debugstr("Setting MAC address to C0:FF:EE:C0:FF:EE."); + debugcrlf(); + + ENC_SetMacAddr(&handle); + debugstr("Network card successfully initialised."); } debugcrlf(); - debugstr("Waiting for ifup."); - debugcrlf(); + debugstr("Waiting for ifup... "); while (!(handle.LinkStatus & PHSTAT2_LSTAT)) ENC_IRQHandler(&handle); - - debugstr("Link status: "); - debughex(((handle.LinkStatus & PHSTAT2_LSTAT) != 0)); + debugstr("done."); debugcrlf(); + + // Re-enable global interrupts + ENC_EnableInterrupts(EIE_INTIE); } diff --git a/part14-ethernet/lib/spi.c b/part14-ethernet/lib/spi.c index dac006f..a03d564 100644 --- a/part14-ethernet/lib/spi.c +++ b/part14-ethernet/lib/spi.c @@ -41,18 +41,15 @@ struct Spi0Regs { #define CS_CS__SHIFT 0 void spi_init() { - gpio_useAsAlt0(7); //CS1 - gpio_useAsAlt0(8); //CS0 - gpio_useAsAlt0(9); //MISO - gpio_useAsAlt0(10); //MOSI - gpio_useAsAlt0(11); //SCLK - - REGS_SPI0->cs = 0x30; - REGS_SPI0->clock = 0x28; // 500Mhz / 40 = 12.5Mhz for ENC SPI + gpio_useAsAlt0(7); //CS1 + gpio_initOutputPinWithPullNone(8); //CS0 + gpio_useAsAlt0(9); //MISO + gpio_useAsAlt0(10); //MOSI + gpio_useAsAlt0(11); //SCLK } void spi_chip_select(unsigned char chip_select) { - REGS_SPI0->cs = (REGS_SPI0->cs & ~CS_CS) | (chip_select << CS_CS__SHIFT); + gpio_setPinOutputBool(8, chip_select); } void spi_send_recv(unsigned char *sbuffer, unsigned char *rbuffer, unsigned int size) { diff --git a/part14-ethernet/net/enc28j60.c b/part14-ethernet/net/enc28j60.c index dc0c26d..9547184 100644 --- a/part14-ethernet/net/enc28j60.c +++ b/part14-ethernet/net/enc28j60.c @@ -750,18 +750,10 @@ bool ENC_Start(ENC_HandleTypeDef *handle) * via SPI. */ - regval = enc_rdphy(handle, ENC_PHID1); - debugstr("PHID1: "); - debughex(regval); - debugcrlf(); - regval = enc_rdbreg(handle, ENC_EREVID); if (regval == 0x00 || regval == 0xff) { return false; } - debugstr("Board revision: "); - debughex(regval); - debugcrlf(); /* Initialize ECON2: Enable address auto increment. */ @@ -847,10 +839,6 @@ bool ENC_Start(ENC_HandleTypeDef *handle) enc_wrbreg(handle, ENC_MAMXFLL, (CONFIG_NET_ETH_MTU+18) & 0xff); enc_wrbreg(handle, ENC_MAMXFLH, (CONFIG_NET_ETH_MTU+18) >> 8); - /* Set the Mac Address */ - - ENC_SetMacAddr(handle); - /* Configure LEDs (No, just use the defaults for now) */ /* enc_wrphy(priv, ENC_PHLCON, ??); */ @@ -931,8 +919,6 @@ void ENC_SetMacAddr(ENC_HandleTypeDef *handle) enc_wrbreg(handle, ENC_MAADR4, handle->Init.MACAddr[3]); enc_wrbreg(handle, ENC_MAADR5, handle->Init.MACAddr[4]); enc_wrbreg(handle, ENC_MAADR6, handle->Init.MACAddr[5]); - - enc_wrbreg(handle, ENC_ECOCON, 0x2 & 0x7); // 12.5Mhz } @@ -1199,6 +1185,7 @@ void ENC_Transmit(ENC_HandleTypeDef *handle) enc_wrbreg(handle, ENC_ERDPTH, addtTsv4 >> 8); enc_rdbuffer(&tsv4, 1); + regval = enc_rdgreg(ENC_EIR); if (!(regval & EIR_TXERIF) || !(tsv4 & TSV_LATECOL)) { break; diff --git a/part14-ethernet/net/encspi.c b/part14-ethernet/net/encspi.c index 524f73f..9c1ee34 100644 --- a/part14-ethernet/net/encspi.c +++ b/part14-ethernet/net/encspi.c @@ -1,4 +1,5 @@ #include "../include/spi.h" +#include "../kernel/kernel.h" void ENC_SPI_Select(unsigned char truefalse) { spi_chip_select(!truefalse); // If it's true, select 0 (the ENC), if false, select 1 (i.e. deselect the ENC) @@ -6,14 +7,18 @@ void ENC_SPI_Select(unsigned char truefalse) { void ENC_SPI_SendBuf(unsigned char *master2slave, unsigned char *slave2master, unsigned short bufferSize) { spi_chip_select(0); + HAL_Delay(1); spi_send_recv(master2slave, slave2master, bufferSize); spi_chip_select(1); // De-select the ENC + HAL_Delay(1); } void ENC_SPI_Send(unsigned char command) { spi_chip_select(0); + HAL_Delay(1); spi_send(&command, 1); spi_chip_select(1); // De-select the ENC + HAL_Delay(1); } void ENC_SPI_SendWithoutSelection(unsigned char command) {