diff --git a/.vscode/settings.json b/.vscode/settings.json index 89a936c..1a2508a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -40,6 +40,7 @@ "__config": "c", "string": "c", "atomic": "c", - "__bit_reference": "c" + "__bit_reference": "c", + "err.h": "c" } } \ No newline at end of file diff --git a/port/ethernetif.c b/port/ethernetif.c index 7e7fe6c..ef6f906 100644 --- a/port/ethernetif.c +++ b/port/ethernetif.c @@ -13,12 +13,6 @@ #define IFNAME0 'e' #define IFNAME1 'n' -#define ETH_RX_BUF_COUNT 4 -#define ETH_TX_BUF_COUNT 2 -/* buf size should be at least ETH_MAX_PACKET_SIZE */ -#define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE -#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE - typedef struct { volatile uint32_t head; // producer idx: next free slot to write to volatile uint32_t tail; // consumer idx: next slot to be txed @@ -216,12 +210,10 @@ static err_t low_level_output(struct netif* netif, struct pbuf* p) { } g_dma_tx_descs[current_idx].Status = len; - tx_queue_produce(ðernetif->tx_q); } tx_start_if_possible(); - return errval; } @@ -310,7 +302,8 @@ void ETH_IRQHandler(void) { ethernetif->rx_desc_head->Status &= ~ETH_DMARxDesc_OWN; // write packet len into status field for CPU ethernetif->rx_desc_head->Status |= - (ETH_DMARxDesc_FS | ETH_DMARxDesc_LS | (ETH10M->ERXLN << 16)); + (ETH_DMARxDesc_FS | ETH_DMARxDesc_LS | + (ETH10M->ERXLN << ETH_DMARxDesc_FrameLengthShift)); // advance descripotor ptr ethernetif->rx_desc_head = next_desc; // re-arm receiver with new emtpy buf diff --git a/port/ethernetif.h b/port/ethernetif.h index 00c0fee..75c2cee 100644 --- a/port/ethernetif.h +++ b/port/ethernetif.h @@ -4,25 +4,32 @@ #include "lwip/err.h" #include "lwip/netif.h" -void phy_write_reg(uint8_t reg_add, uint16_t reg_val); -uint16_t phy_read_reg(uint8_t reg_add); - +/* Unique device ID */ #define ROM_CFG_USERADR_ID 0x1FFFF7E8 -#define ETH_DMARxDesc_FrameLengthShift 16 - -#define ETH_MAX_PACKET_SIZE \ - 1536 /* ETH_HEADER + VLAN_TAG + MAX_ETH_PAYLOAD + ETH_CRC */ -#define ETH_HEADER \ - 14 /* 6 byte Dest addr, 6 byte Src addr, 2 byte length/type \ - */ +/* Ethernet Frame Size Definitions */ +#define ETH_HEADER \ + 14 /* 6 byte Dest addr, 6 byte Src addr, 2 byte length/type */ #define ETH_CRC 4 /* Ethernet CRC */ #define ETH_EXTRA 2 /* Extra bytes in some cases */ #define VLAN_TAG 4 /* optional 802.1q VLAN Tag */ + #define MIN_ETH_PAYLOAD 46 /* Minimum Ethernet payload size */ #define MAX_ETH_PAYLOAD 1500 /* Maximum Ethernet payload size */ + +#define ETH_MAX_PACKET_SIZE \ + 1536 /* ETH_HEADER + VLAN_TAG + MAX_ETH_PAYLOAD + ETH_CRC */ #define MIN_ETH_FRAME_SIZE (ETH_HEADER + MIN_ETH_PAYLOAD) /* 60 bytes */ +/* Buffer Configuration */ +#define ETH_RX_BUF_COUNT 4 +#define ETH_TX_BUF_COUNT 2 +#define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE +#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE + +/* DMA descriptor stuff */ +#define ETH_DMARxDesc_FrameLengthShift 16 + typedef struct { uint32_t volatile Status; /* Status */ uint32_t ControlBufferSize; /* Control and Buffer1, Buffer2 lengths */ @@ -31,29 +38,52 @@ typedef struct { } ETH_DMADESCTypeDef; /** - * @brief Initialize the ethernet interface and lwIP network stack. - * This function should be passed as the init function to netif_add(). + * Should be called at the beginning of the program to set up the + * network interface. It calls the function low_level_init() to do the + * actual setup of the hardware. * - * @param netif The lwIP network interface structure to be initialized. - * @return ERR_OK if the loopif is initialized, ERR_MEM if private data couldn't - * be allocated. + * This function should be passed as a parameter to netif_add(). + * + * @param netif the lwip network interface structure for this ethernetif + * @return ERR_OK if the loopif is initialized + * ERR_MEM if private data couldn't be allocated + * any other err_t on error */ err_t ethernetif_init(struct netif* netif); /** - * @brief This function should be called periodically from your main loop - * to check for incoming packets and pass them to lwIP. + * This function should be called when a packet is ready to be read + * from the interface. It uses the function low_level_input() that + * should handle the actual reception of bytes from the network + * interface. Then the type of the received packet is determined and + * the appropriate input function is called. * - * @param netif The lwIP network interface structure. + * @param netif the lwip network interface structure for this ethernetif */ void ethernetif_input(struct netif* netif); /** - * @brief This function should be called periodically from your main loop - * to check the link status and update lwIP accordingly. + * This function should be called periodically from the main loop + * to check the link status and update lwIP accordingly. * - * @param netif The lwIP network interface structure.. + * @param netif the lwip network interface structure for this ethernetif */ void ethernetif_link_poll(struct netif* netif); +/** + * Write a value to PHY register. + * + * @param reg_add PHY register address. + * @param reg_val Value to write. + */ +void phy_write_reg(uint8_t reg_add, uint16_t reg_val); + +/** + * Read a value from PHY register. + * + * @param reg_add PHY register address. + * @return Register value. + */ +uint16_t phy_read_reg(uint8_t reg_add); + #endif /* __ETHERNETIF_H */ \ No newline at end of file