From 66f80328bc45a09dff41cc8bf4408c31dda2bd08 Mon Sep 17 00:00:00 2001 From: Adam Greenwood-Byrne Date: Tue, 26 Oct 2021 09:50:13 +0100 Subject: [PATCH] Fixed fb.c across the board, iterated on Ethernet support - still WIP --- part10-multicore/fb.c | 2 +- part11-breakout-smp/lib/fb.c | 2 +- part12-wgt/wgt/wsetmode.c | 2 +- part13-interrupts/lib/fb.c | 2 +- part14-ethernet/kernel/arp.c | 79 ++++++++++++++++++--------------- part14-ethernet/kernel/kernel.c | 58 ++++++++++++------------ part14-ethernet/lib/fb.c | 2 +- part14-ethernet/net/enc28j60.c | 26 +++++------ part14-ethernet/net/enc28j60.h | 1 - part5-framebuffer/fb.c | 2 +- part6-breakout/fb.c | 2 +- part7-bluetooth/fb.c | 2 +- part8-breakout-ble/fb.c | 2 +- part9-sound/fb.c | 2 +- 14 files changed, 95 insertions(+), 89 deletions(-) diff --git a/part10-multicore/fb.c b/part10-multicore/fb.c index 43fae91..b3e3f0d 100644 --- a/part10-multicore/fb.c +++ b/part10-multicore/fb.c @@ -15,7 +15,7 @@ void fb_init() mbox[2] = MBOX_TAG_SETPHYWH; // Tag identifier mbox[3] = 8; // Value size in bytes - mbox[4] = 8; // Value size in bytes (again!) + mbox[4] = 0; mbox[5] = 1920; // Value(width) mbox[6] = 1080; // Value(height) diff --git a/part11-breakout-smp/lib/fb.c b/part11-breakout-smp/lib/fb.c index 7fc4f24..0daa0fe 100644 --- a/part11-breakout-smp/lib/fb.c +++ b/part11-breakout-smp/lib/fb.c @@ -17,7 +17,7 @@ void fb_init() mbox[2] = MBOX_TAG_SETPHYWH; // Tag identifier mbox[3] = 8; // Value size in bytes - mbox[4] = 8; // Value size in bytes (again!) + mbox[4] = 0; mbox[5] = 1920; // Value(width) mbox[6] = 1080; // Value(height) diff --git a/part12-wgt/wgt/wsetmode.c b/part12-wgt/wgt/wsetmode.c index 7973519..df0cdfc 100644 --- a/part12-wgt/wgt/wsetmode.c +++ b/part12-wgt/wgt/wsetmode.c @@ -7,7 +7,7 @@ void vga256(void) mbox[2] = MBOX_TAG_SETPHYWH; // Tag identifier mbox[3] = 8; // Value size in bytes - mbox[4] = 8; // Value size in bytes (again!) + mbox[4] = 0; mbox[5] = 1920; // Value(width) mbox[6] = 1080; // Value(height) diff --git a/part13-interrupts/lib/fb.c b/part13-interrupts/lib/fb.c index 80ef496..2cc89f7 100644 --- a/part13-interrupts/lib/fb.c +++ b/part13-interrupts/lib/fb.c @@ -15,7 +15,7 @@ void fb_init() mbox[2] = MBOX_TAG_SETPHYWH; // Tag identifier mbox[3] = 8; // Value size in bytes - mbox[4] = 8; // Value size in bytes (again!) + mbox[4] = 0; mbox[5] = 1920; // Value(width) mbox[6] = 1080; // Value(height) diff --git a/part14-ethernet/kernel/arp.c b/part14-ethernet/kernel/arp.c index 8cb2a94..3151d64 100644 --- a/part14-ethernet/kernel/arp.c +++ b/part14-ethernet/kernel/arp.c @@ -1,6 +1,8 @@ #include "../net/enc28j60.h" #include "../include/fb.h" +ENC_HandleTypeDef handle; + // Structure for Ethernet header typedef struct { @@ -11,8 +13,8 @@ typedef struct { // Ethernet packet types -#define ARPPACKET 0x0806 -#define IPPACKET 0x0800 +#define ARPPACKET 0x0608 +#define IPPACKET 0x0008 // Structure for an ARP Packet @@ -31,12 +33,12 @@ typedef struct { // ARP OpCodes -#define ARPREPLY 0x0002 -#define ARPREQUEST 0x0001 +#define ARPREPLY 0x0200 +#define ARPREQUEST 0x0100 // ARP hardware types -#define ETHERNET 0x0001 +#define ETHERNET 0x0100 // MAC address to be assigned to the ENC28J60 @@ -135,60 +137,67 @@ void SendArpPacket(uint8_t *targetIP, uint8_t *deviceMAC) } // Send the packet - ENC_WriteBuffer((unsigned char*)&arpPacket, sizeof(ARP)); + + if (ENC_RestoreTXBuffer(&handle, sizeof(ARP)) == 0) { + debugstr("Sending ARP request... "); + debugcrlf(); + + ENC_WriteBuffer((unsigned char *)&arpPacket, sizeof(ARP)); + handle.transmitLength = sizeof(ARP); + ENC_Transmit(&handle); + } } void arp_test(void) { - unsigned int tries = 0; - ARP checkPacket; - - debugstr("Sending ARP request... "); + ARP *checkPacket; SendArpPacket(routerIP, myMAC); - debugstr("done."); - debugcrlf(); - debugstr("Waiting for ARP response... "); debugcrlf(); - while (tries < 1000) { - if (ENC_ReadBuffer((unsigned char *)&checkPacket, sizeof(ARP)) == 0) { + while (1) { + if (!ENC_GetReceivedFrame(&handle)) { continue; } - 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(); + 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 + + 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; + } } - tries++; } - - debugstr("Timed out."); - debugcrlf(); } void init_network(void) { - ENC_HandleTypeDef handle; - - debugstr("Setting MAC address to C0:FF:EE:C0:FF:EE."); - debugcrlf(); - handle.Init.DuplexMode = ETH_MODE_FULLDUPLEX; handle.Init.MACAddr = myMAC; handle.Init.ChecksumMode = ETH_CHECKSUM_BY_HARDWARE; handle.Init.InterruptEnableBits = 0; + debugstr("Setting MAC address to C0:FF:EE:C0:FF:EE."); + debugcrlf(); + + ENC_SetMacAddr(&handle); + debugstr("Starting network up."); debugcrlf(); diff --git a/part14-ethernet/kernel/kernel.c b/part14-ethernet/kernel/kernel.c index e0b4a0d..800add3 100644 --- a/part14-ethernet/kernel/kernel.c +++ b/part14-ethernet/kernel/kernel.c @@ -8,10 +8,10 @@ void initProgress(void) { drawRect(0, 0, 301, 50, 0x0f, 0); - drawString(309, 21, "Core 1", 0x0f, 1); + drawString(309, 21, "Core 0", 0x0f, 1); drawRect(0, 60, 301, 110, 0x0f, 0); - drawString(309, 81, "Core 2", 0x0f, 1); + drawString(309, 81, "Core 1", 0x0f, 1); drawRect(0, 120, 301, 170, 0x0f, 0); drawString(309, 141, "Timer 1", 0x0f, 1); @@ -28,28 +28,32 @@ void drawProgress(unsigned int core, unsigned int val) { if (val > 0) drawRect(1, (60 * core) + 1, (val * 3), (60 * core) + 49, col, 1); } -unsigned int c2_done = 0; +void core3_main(void) { + clear_core3(); // Only run once -void core2_main(void) -{ - unsigned int core2_val = 0; + // Test the network card - clear_core2(); // Only run once - - while (core2_val <= 100) { - wait_msec(0x100000); - drawProgress(1, core2_val); - core2_val++; - } - - debugstr("Core 2 done."); - debugcrlf(); - - c2_done = 1; + spi_init(); + init_network(); + arp_test(); while(1); } +void core0_main(void) +{ + unsigned int core0_val = 0; + + while (core0_val <= 100) { + wait_msec(0x100000); + drawProgress(0, core0_val); + core0_val++; + } + + debugstr("Core 0 done."); + debugcrlf(); +} + void core1_main(void) { unsigned int core1_val = 0; @@ -58,7 +62,7 @@ void core1_main(void) while (core1_val <= 100) { wait_msec(0x3FFFF); - drawProgress(0, core1_val); + drawProgress(1, core1_val); core1_val++; } @@ -144,10 +148,9 @@ void main(void) initProgress(); - // Kick it off on core 1&2 + // Kick it off on core 1 start_core1(core1_main); - start_core2(core2_main); // Kick off the timers @@ -156,18 +159,13 @@ void main(void) irq_enable(); timer_init(); - // Test the network card + // Kick it off on core 3 - spi_init(); - init_network(); - arp_test(); + start_core3(core3_main); - // The work is done - wait for timers to get done + // Kick it off on core 0 - debugstr("Core 0 done."); - debugcrlf(); - - while (!c2_done); + core0_main(); // Disable IRQs and loop endlessly diff --git a/part14-ethernet/lib/fb.c b/part14-ethernet/lib/fb.c index 80ef496..2cc89f7 100644 --- a/part14-ethernet/lib/fb.c +++ b/part14-ethernet/lib/fb.c @@ -15,7 +15,7 @@ void fb_init() mbox[2] = MBOX_TAG_SETPHYWH; // Tag identifier mbox[3] = 8; // Value size in bytes - mbox[4] = 8; // Value size in bytes (again!) + mbox[4] = 0; mbox[5] = 1920; // Value(width) mbox[6] = 1080; // Value(height) diff --git a/part14-ethernet/net/enc28j60.c b/part14-ethernet/net/enc28j60.c index 57189d3..4ce5d66 100644 --- a/part14-ethernet/net/enc28j60.c +++ b/part14-ethernet/net/enc28j60.c @@ -376,8 +376,7 @@ void enc_reset(ENC_HandleTypeDef *handle) { */ handle->bank = 0; /* Initialize the trace on the current selected bank */ - //up_mdelay(2); - HAL_Delay(2); /* >1000 µs, conforms to errata #2 */ + up_udelay(2); /* >1000 µs, conforms to errata #2 */ } /**************************************************************************** @@ -797,7 +796,7 @@ bool ENC_Start(ENC_HandleTypeDef *handle) enc_wrbreg(handle, ENC_ERXFCON, ERXFCON_UCEN | ERXFCON_CRCEN | ERXFCON_BCEN); do { - HAL_Delay(10); /* Wait for 10 ms to let the clock be ready */ + up_udelay(10); /* Wait for 10 ms to let the clock be ready */ regval = enc_rdbreg(handle, ENC_ESTAT); } while ((regval & ESTAT_CLKRDY) == 0); @@ -993,7 +992,7 @@ void ENC_WriteBuffer(void *buffer, uint16_t buflen) } /**************************************************************************** - * Function: ENC_ReadBuffer + * Function: enc_rdbuffer * * Description: * Read a buffer of data. @@ -1010,10 +1009,8 @@ void ENC_WriteBuffer(void *buffer, uint16_t buflen) * ****************************************************************************/ -uint8_t ENC_ReadBuffer(void *buffer, uint16_t buflen) +static void enc_rdbuffer(void *buffer, uint16_t buflen) { - uint8_t read_count = 0; - /* Select ENC28J60 chip */ ENC_SPI_Select(true); @@ -1024,11 +1021,9 @@ uint8_t ENC_ReadBuffer(void *buffer, uint16_t buflen) /* Then read the buffer data */ - read_count = ENC_SPI_SendBuf(NULL, buffer, buflen); + ENC_SPI_SendBuf(NULL, buffer, buflen); /* De-select ENC28J60 chip: done in ENC_SPI_SendBuf callback */ - - return read_count; } /**************************************************************************** @@ -1153,6 +1148,8 @@ void ENC_Transmit(ENC_HandleTypeDef *handle) PT_BEGIN(pt); if (handle->transmitLength != 0) { + debugstr("We're actually sending"); debugcrlf(); + /* A frame is ready for transmission */ /* Set TXRTS to send the packet in the transmit buffer */ @@ -1191,7 +1188,7 @@ void ENC_Transmit(ENC_HandleTypeDef *handle) enc_wrbreg(handle, ENC_ERDPTL, addtTsv4 & 0xff); enc_wrbreg(handle, ENC_ERDPTH, addtTsv4 >> 8); - ENC_ReadBuffer(&tsv4, 1); + enc_rdbuffer(&tsv4, 1); regval = enc_rdgreg(ENC_EIR); if (!(regval & EIR_TXERIF) || !(tsv4 & TSV_LATECOL)) { break; @@ -1201,7 +1198,10 @@ void ENC_Transmit(ENC_HandleTypeDef *handle) } while (handle->retries > 0); /* Transmission finished (but can be unsuccessful) */ handle->transmitLength = 0; + } else { + debugstr("Nothing to send"); debugcrlf(); } + debugstr("We're done sending"); debugcrlf(); PT_END(pt); } @@ -1246,7 +1246,7 @@ bool ENC_GetReceivedFrame(ENC_HandleTypeDef *handle) * and wrap to the beginning of the read buffer as necessary) */ - ENC_ReadBuffer(rsv, 6); + enc_rdbuffer(rsv, 6); /* Decode the new next packet pointer, and the RSV. The * RSV is encoded as: @@ -1285,7 +1285,7 @@ bool ENC_GetReceivedFrame(ENC_HandleTypeDef *handle) * end_rdbuffer (above). */ - ENC_ReadBuffer(handle->RxFrameInfos.buffer, handle->RxFrameInfos.length); + enc_rdbuffer(handle->RxFrameInfos.buffer, handle->RxFrameInfos.length); } } diff --git a/part14-ethernet/net/enc28j60.h b/part14-ethernet/net/enc28j60.h index c34152a..2c692fd 100644 --- a/part14-ethernet/net/enc28j60.h +++ b/part14-ethernet/net/enc28j60.h @@ -686,7 +686,6 @@ int8_t ENC_RestoreTXBuffer(ENC_HandleTypeDef *handle, uint16_t len); ****************************************************************************/ void ENC_WriteBuffer(void *buffer, uint16_t buflen); -uint8_t ENC_ReadBuffer(void *buffer, uint16_t buflen); /**************************************************************************** * Function: ENC_Transmit diff --git a/part5-framebuffer/fb.c b/part5-framebuffer/fb.c index 3b4450b..7b44d08 100644 --- a/part5-framebuffer/fb.c +++ b/part5-framebuffer/fb.c @@ -12,7 +12,7 @@ void fb_init() mbox[2] = MBOX_TAG_SETPHYWH; // Tag identifier mbox[3] = 8; // Value size in bytes - mbox[4] = 8; // Value size in bytes (again!) + mbox[4] = 0; mbox[5] = 1920; // Value(width) mbox[6] = 1080; // Value(height) diff --git a/part6-breakout/fb.c b/part6-breakout/fb.c index ff52a43..d607f7e 100644 --- a/part6-breakout/fb.c +++ b/part6-breakout/fb.c @@ -12,7 +12,7 @@ void fb_init() mbox[2] = MBOX_TAG_SETPHYWH; // Tag identifier mbox[3] = 8; // Value size in bytes - mbox[4] = 8; // Value size in bytes (again!) + mbox[4] = 0; mbox[5] = 1920; // Value(width) mbox[6] = 1080; // Value(height) diff --git a/part7-bluetooth/fb.c b/part7-bluetooth/fb.c index 43fae91..b3e3f0d 100644 --- a/part7-bluetooth/fb.c +++ b/part7-bluetooth/fb.c @@ -15,7 +15,7 @@ void fb_init() mbox[2] = MBOX_TAG_SETPHYWH; // Tag identifier mbox[3] = 8; // Value size in bytes - mbox[4] = 8; // Value size in bytes (again!) + mbox[4] = 0; mbox[5] = 1920; // Value(width) mbox[6] = 1080; // Value(height) diff --git a/part8-breakout-ble/fb.c b/part8-breakout-ble/fb.c index 55d944a..d6b9913 100644 --- a/part8-breakout-ble/fb.c +++ b/part8-breakout-ble/fb.c @@ -17,7 +17,7 @@ void fb_init() mbox[2] = MBOX_TAG_SETPHYWH; // Tag identifier mbox[3] = 8; // Value size in bytes - mbox[4] = 8; // Value size in bytes (again!) + mbox[4] = 0; mbox[5] = 1920; // Value(width) mbox[6] = 1080; // Value(height) diff --git a/part9-sound/fb.c b/part9-sound/fb.c index 43fae91..b3e3f0d 100644 --- a/part9-sound/fb.c +++ b/part9-sound/fb.c @@ -15,7 +15,7 @@ void fb_init() mbox[2] = MBOX_TAG_SETPHYWH; // Tag identifier mbox[3] = 8; // Value size in bytes - mbox[4] = 8; // Value size in bytes (again!) + mbox[4] = 0; mbox[5] = 1920; // Value(width) mbox[6] = 1080; // Value(height)