Iterating on the Ethernet driver implementation

This commit is contained in:
Adam Greenwood-Byrne 2021-10-30 02:04:31 +01:00
parent 66f80328bc
commit bca2e179f9
6 changed files with 33 additions and 29 deletions

View file

@ -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);

View file

@ -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);
}

View file

@ -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) {

View file

@ -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;
@ -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);
}

View file

@ -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 */

View file

@ -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) {