From bca2e179f9381c4ce8376c4620b6a33af52c687d Mon Sep 17 00:00:00 2001 From: Adam Greenwood-Byrne Date: Sat, 30 Oct 2021 02:04:31 +0100 Subject: [PATCH] Iterating on the Ethernet driver implementation --- part14-ethernet/include/spi.h | 2 +- part14-ethernet/kernel/arp.c | 10 ++++------ part14-ethernet/lib/spi.c | 3 +-- part14-ethernet/net/enc28j60.c | 33 ++++++++++++++++++++++----------- part14-ethernet/net/enc28j60.h | 6 +++--- part14-ethernet/net/encspi.c | 8 ++------ 6 files changed, 33 insertions(+), 29 deletions(-) diff --git a/part14-ethernet/include/spi.h b/part14-ethernet/include/spi.h index 6454a72..c5fca8b 100644 --- a/part14-ethernet/include/spi.h +++ b/part14-ethernet/include/spi.h @@ -1,5 +1,5 @@ void spi_init(); -unsigned int spi_send_recv(unsigned char *sbuffer, unsigned char *rbuffer, unsigned int size); +void spi_send_recv(unsigned char *sbuffer, unsigned char *rbuffer, unsigned int size); void spi_send(unsigned char *data, unsigned int size); void spi_recv(unsigned char *data, unsigned int size); void spi_send_no_selection(unsigned char command); diff --git a/part14-ethernet/kernel/arp.c b/part14-ethernet/kernel/arp.c index 3151d64..9df07fa 100644 --- a/part14-ethernet/kernel/arp.c +++ b/part14-ethernet/kernel/arp.c @@ -193,18 +193,16 @@ void init_network(void) 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(); - if (!ENC_Start(&handle)) { debugstr("Could not initialise network card."); } else { debugstr("Network card successfully initialised."); } debugcrlf(); + + debugstr("Setting MAC address to C0:FF:EE:C0:FF:EE."); + debugcrlf(); + ENC_SetMacAddr(&handle); } diff --git a/part14-ethernet/lib/spi.c b/part14-ethernet/lib/spi.c index d71fc22..c6b0bd5 100644 --- a/part14-ethernet/lib/spi.c +++ b/part14-ethernet/lib/spi.c @@ -52,7 +52,7 @@ void spi_chip_select(unsigned char chip_select) { REGS_SPI0->cs = (REGS_SPI0->cs & ~CS_CS) | (chip_select << CS_CS__SHIFT); } -unsigned int spi_send_recv(unsigned char *sbuffer, unsigned char *rbuffer, unsigned int size) { +void spi_send_recv(unsigned char *sbuffer, unsigned char *rbuffer, unsigned int size) { REGS_SPI0->data_length = size; REGS_SPI0->cs = REGS_SPI0->cs | CS_CLEAR_RX | CS_CLEAR_TX | CS_TA; @@ -89,7 +89,6 @@ unsigned int spi_send_recv(unsigned char *sbuffer, unsigned char *rbuffer, unsig } REGS_SPI0->cs = (REGS_SPI0->cs & ~CS_TA); - return read_count; } void spi_send(unsigned char *data, unsigned int size) { diff --git a/part14-ethernet/net/enc28j60.c b/part14-ethernet/net/enc28j60.c index 4ce5d66..0815fc6 100644 --- a/part14-ethernet/net/enc28j60.c +++ b/part14-ethernet/net/enc28j60.c @@ -191,7 +191,7 @@ static void calibrate(void) * Software delay in µs * us: the number of µs to wait **/ -static __inline void up_udelay(uint32_t us) +void up_udelay(uint32_t us) { volatile uint32_t i; @@ -376,7 +376,8 @@ void enc_reset(ENC_HandleTypeDef *handle) { */ handle->bank = 0; /* Initialize the trace on the current selected bank */ - up_udelay(2); /* >1000 µs, conforms to errata #2 */ + //up_mdelay(2); + HAL_Delay(2); /* >1000 µs, conforms to errata #2 */ } /**************************************************************************** @@ -749,6 +750,11 @@ 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; @@ -756,7 +762,7 @@ bool ENC_Start(ENC_HandleTypeDef *handle) debugstr("Board revision: "); debughex(regval); debugcrlf(); - + /* Initialize ECON2: Enable address auto increment. */ @@ -796,7 +802,7 @@ bool ENC_Start(ENC_HandleTypeDef *handle) enc_wrbreg(handle, ENC_ERXFCON, ERXFCON_UCEN | ERXFCON_CRCEN | ERXFCON_BCEN); do { - up_udelay(10); /* Wait for 10 ms to let the clock be ready */ + HAL_Delay(10); /* Wait for 10 ms to let the clock be ready */ regval = enc_rdbreg(handle, ENC_ESTAT); } while ((regval & ESTAT_CLKRDY) == 0); @@ -879,6 +885,16 @@ bool ENC_Start(ENC_HandleTypeDef *handle) /* Enable the receiver */ enc_bfsgreg(ENC_ECON1, ECON1_RXEN); + regval = enc_rdphy(handle, ENC_PHSTAT1) & PHSTAT1_LLSTAT; + debugstr("Link status 1: "); + debughex(regval); + debugcrlf(); + + regval = enc_rdphy(handle, ENC_PHSTAT2) & PHSTAT2_LSTAT; + debugstr("Link status 2: "); + debughex(regval); + debugcrlf(); + return true; } @@ -1002,14 +1018,14 @@ void ENC_WriteBuffer(void *buffer, uint16_t buflen) * buflen - The number of bytes to read * * Returned Value: - * read_count - Number of bytes received + * None * * Assumptions: * Read pointer is set to the correct address * ****************************************************************************/ -static void enc_rdbuffer(void *buffer, uint16_t buflen) +static void enc_rdbuffer(void *buffer, int16_t buflen) { /* Select ENC28J60 chip */ @@ -1148,8 +1164,6 @@ 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 */ @@ -1198,10 +1212,7 @@ 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); } diff --git a/part14-ethernet/net/enc28j60.h b/part14-ethernet/net/enc28j60.h index 2c692fd..dc77865 100644 --- a/part14-ethernet/net/enc28j60.h +++ b/part14-ethernet/net/enc28j60.h @@ -98,10 +98,10 @@ void ENC_SPI_Send(uint8_t command); * Implement SPI buffer send and receive. Must be provided by user code * param master2slave: data to be sent from host to ENC28J60, can be NULL if we only want to receive data from slave * param slave2master: answer from ENC28J60 to host, can be NULL if we only want to send data to slave - * retval read_count: number of bytes read (if applicable) + * retval none */ -unsigned int ENC_SPI_SendBuf(uint8_t *master2slave, uint8_t *slave2master, uint16_t bufferSize); +void ENC_SPI_SendBuf(uint8_t *master2slave, uint8_t *slave2master, uint16_t bufferSize); /* Exported types ------------------------------------------------------------*/ /** @defgroup ETH_Exported_Types ETH Exported Types @@ -811,6 +811,6 @@ void ENC_GetPkcnt(ENC_HandleTypeDef *handle); * ****************************************************************************/ -//__inline void up_udelay(uint32_t us); +void up_udelay(uint32_t us); #endif /* ENC28J60_H_INCLUDED */ diff --git a/part14-ethernet/net/encspi.c b/part14-ethernet/net/encspi.c index 91c41b1..524f73f 100644 --- a/part14-ethernet/net/encspi.c +++ b/part14-ethernet/net/encspi.c @@ -4,14 +4,10 @@ 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) } -unsigned int ENC_SPI_SendBuf(unsigned char *master2slave, unsigned char *slave2master, unsigned short bufferSize) { - unsigned int read_count = 0; - +void ENC_SPI_SendBuf(unsigned char *master2slave, unsigned char *slave2master, unsigned short bufferSize) { spi_chip_select(0); - read_count = spi_send_recv(master2slave, slave2master, bufferSize); + spi_send_recv(master2slave, slave2master, bufferSize); spi_chip_select(1); // De-select the ENC - - return read_count; } void ENC_SPI_Send(unsigned char command) {