From 29b864719cab74b96ee0f11c635029c61881539d Mon Sep 17 00:00:00 2001 From: Adam Greenwood-Byrne Date: Sat, 30 Oct 2021 18:16:02 +0100 Subject: [PATCH] Added some clocking to Ethernet over SPI example --- part14-ethernet/kernel/arp.c | 41 +++++++++++++++------------------- part14-ethernet/lib/spi.c | 3 +++ part14-ethernet/net/enc28j60.c | 6 +++++ 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/part14-ethernet/kernel/arp.c b/part14-ethernet/kernel/arp.c index 6752c71..be9af49 100644 --- a/part14-ethernet/kernel/arp.c +++ b/part14-ethernet/kernel/arp.c @@ -158,30 +158,29 @@ void arp_test(void) debugcrlf(); while (1) { - if (!ENC_GetReceivedFrame(&handle)) { - continue; - } + while (!(handle.interruptFlags & EIR_PKTIF)) ENC_IRQHandler(&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 (len > 0) { - if (!memcmp(checkPacket->senderIP, routerIP, 4)) { - // Success! We have found our router's MAC address + 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; } } } @@ -191,11 +190,7 @@ void init_network(void) handle.Init.DuplexMode = ETH_MODE_HALFDUPLEX; handle.Init.MACAddr = myMAC; handle.Init.ChecksumMode = ETH_CHECKSUM_BY_HARDWARE; - handle.Init.InterruptEnableBits = EIE_LINKIE; - - debugstr("Setting MAC address to C0:FF:EE:C0:FF:EE."); - debugcrlf(); - ENC_SetMacAddr(&handle); + handle.Init.InterruptEnableBits = EIE_LINKIE | EIE_PKTIE; debugstr("Starting network up."); debugcrlf(); diff --git a/part14-ethernet/lib/spi.c b/part14-ethernet/lib/spi.c index c6b0bd5..dac006f 100644 --- a/part14-ethernet/lib/spi.c +++ b/part14-ethernet/lib/spi.c @@ -46,6 +46,9 @@ void spi_init() { 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 } void spi_chip_select(unsigned char chip_select) { diff --git a/part14-ethernet/net/enc28j60.c b/part14-ethernet/net/enc28j60.c index 05093a0..dc0c26d 100644 --- a/part14-ethernet/net/enc28j60.c +++ b/part14-ethernet/net/enc28j60.c @@ -847,6 +847,10 @@ 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, ??); */ @@ -927,6 +931,8 @@ 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 }