bugfixes & use new lwip mempool for FreeRTOS lwip
This commit is contained in:
@ -44,16 +44,16 @@
|
||||
*/
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "stm32h7xx_hal.h"
|
||||
#include "lwip/opt.h"
|
||||
#include "lwip/timeouts.h"
|
||||
#include "lwip/netif.h"
|
||||
#include "netif/etharp.h"
|
||||
#include "ethernetif.h"
|
||||
|
||||
#include <lan8742.h>
|
||||
#include <lwip/netif.h>
|
||||
#include <lwip/opt.h>
|
||||
#include <lwip/timeouts.h>
|
||||
#include <netif/etharp.h>
|
||||
#include <stm32h7xx_hal.h>
|
||||
#include "lan8742.h"
|
||||
#include <string.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "fsfw/FSFW.h"
|
||||
|
||||
#ifdef FSFW_OSAL_RTEMS
|
||||
@ -66,6 +66,12 @@
|
||||
#define IFNAME0 's'
|
||||
#define IFNAME1 't'
|
||||
|
||||
#define ETH_DMA_TRANSMIT_TIMEOUT (20U)
|
||||
|
||||
#define ETH_RX_BUFFER_SIZE 1536U
|
||||
#define ETH_RX_BUFFER_CNT 12U
|
||||
#define ETH_TX_BUFFER_MAX ((ETH_TX_DESC_CNT) * 2U)
|
||||
|
||||
#define DMA_DESCRIPTOR_ALIGNMENT 0x20
|
||||
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
@ -87,27 +93,30 @@ stack they will return back to DMA after been processed by the stack.
|
||||
2.b. Rx Buffers must have the same size: ETH_RX_BUFFER_SIZE, this value must
|
||||
passed to ETH DMA in the init field (EthHandle.Init.RxBuffLen)
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
RX_ALLOC_OK = 0x00,
|
||||
RX_ALLOC_ERROR = 0x01
|
||||
} RxAllocStatusTypeDef;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
struct pbuf_custom pbuf_custom;
|
||||
uint8_t buff[(ETH_RX_BUFFER_SIZE + 31) & ~31] __ALIGNED(32);
|
||||
} RxBuff_t;
|
||||
|
||||
#if defined(__ICCARM__) /*!< IAR Compiler */
|
||||
|
||||
#pragma location = 0x30040000
|
||||
ETH_DMADescTypeDef
|
||||
DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */
|
||||
#pragma location = 0x30040060
|
||||
ETH_DMADescTypeDef
|
||||
DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */
|
||||
#pragma location = 0x30040200
|
||||
uint8_t Rx_Buff[ETH_RX_DESC_CNT]
|
||||
[ETH_RX_BUFFER_SIZE]; /* Ethernet Receive Buffers */
|
||||
#pragma location=0x30000000
|
||||
ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */
|
||||
#pragma location=0x30000200
|
||||
ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */
|
||||
|
||||
|
||||
#elif defined(__CC_ARM) /* MDK ARM Compiler */
|
||||
|
||||
__attribute__((section(".RxDecripSection"))) ETH_DMADescTypeDef
|
||||
DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */
|
||||
__attribute__((section(".TxDecripSection"))) ETH_DMADescTypeDef
|
||||
DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */
|
||||
__attribute__((section(".RxArraySection"))) uint8_t
|
||||
Rx_Buff[ETH_RX_DESC_CNT][ETH_RX_BUFFER_SIZE]; /* Ethernet Receive Buffer */
|
||||
__attribute__((section(".RxDecripSection"))) ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */
|
||||
__attribute__((section(".TxDecripSection"))) ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */
|
||||
|
||||
#elif defined(__GNUC__) /* GNU Compiler */
|
||||
|
||||
@ -124,44 +133,57 @@ ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT] __attribute__((
|
||||
* placing it */
|
||||
uint8_t Rx_Buff[ETH_RX_DESC_CNT][ETH_RX_BUFFER_SIZE];
|
||||
#elif defined FSFW_OSAL_FREERTOS
|
||||
/* Placement and alignment specified in linker script here */
|
||||
ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT] __attribute__((
|
||||
section(".RxDecripSection"))); /* Ethernet Rx DMA Descriptors */
|
||||
ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT] __attribute__((
|
||||
section(".TxDecripSection"))); /* Ethernet Tx DMA Descriptors */
|
||||
uint8_t Rx_Buff[ETH_RX_DESC_CNT][ETH_RX_BUFFER_SIZE]
|
||||
__attribute__((section(".RxArraySection"))); /* Ethernet Receive Buffers */
|
||||
#endif /* FSFW_FREERTOS */
|
||||
|
||||
ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT] __attribute__((section(".RxDecripSection"))); /* Ethernet Rx DMA Descriptors */
|
||||
ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT] __attribute__((section(".TxDecripSection"))); /* Ethernet Tx DMA Descriptors */
|
||||
|
||||
#endif /* FSFW_OSAL_RTEMS */
|
||||
|
||||
#endif /* defined ( __GNUC__ ) */
|
||||
|
||||
/* Memory Pool Declaration */
|
||||
LWIP_MEMPOOL_DECLARE(RX_POOL, ETH_RX_BUFFER_CNT, sizeof(RxBuff_t), "Zero-copy RX PBUF pool");
|
||||
|
||||
#if defined ( __ICCARM__ ) /*!< IAR Compiler */
|
||||
#pragma location = 0x30000400
|
||||
extern u8_t memp_memory_RX_POOL_base[];
|
||||
|
||||
#elif defined ( __CC_ARM ) /* MDK ARM Compiler */
|
||||
__attribute__((section(".Rx_PoolSection"))) extern u8_t memp_memory_RX_POOL_base[];
|
||||
|
||||
#elif defined ( __GNUC__ ) /* GNU Compiler */
|
||||
__attribute__((section(".Rx_PoolSection"))) extern u8_t memp_memory_RX_POOL_base[];
|
||||
|
||||
#endif
|
||||
|
||||
/* Global boolean to track ethernet connection */
|
||||
bool ethernet_cable_connected;
|
||||
|
||||
struct pbuf_custom rx_pbuf[ETH_RX_DESC_CNT];
|
||||
uint32_t current_pbuf_idx = 0;
|
||||
/* Variable Definitions */
|
||||
static uint8_t RxAllocStatus;
|
||||
|
||||
/* Global Ethernet handle*/
|
||||
ETH_HandleTypeDef EthHandle;
|
||||
ETH_TxPacketConfig TxConfig;
|
||||
|
||||
lan8742_Object_t LAN8742;
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
u32_t sys_now(void);
|
||||
void pbuf_free_custom(struct pbuf *p);
|
||||
|
||||
extern void Error_Handler(void);
|
||||
int32_t ETH_PHY_IO_Init(void);
|
||||
int32_t ETH_PHY_IO_DeInit(void);
|
||||
int32_t ETH_PHY_IO_ReadReg(uint32_t DevAddr, uint32_t RegAddr,
|
||||
uint32_t *pRegVal);
|
||||
int32_t ETH_PHY_IO_WriteReg(uint32_t DevAddr, uint32_t RegAddr,
|
||||
uint32_t RegVal);
|
||||
int32_t ETH_PHY_IO_DeInit (void);
|
||||
int32_t ETH_PHY_IO_ReadReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t *pRegVal);
|
||||
int32_t ETH_PHY_IO_WriteReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t RegVal);
|
||||
int32_t ETH_PHY_IO_GetTick(void);
|
||||
|
||||
lan8742_IOCtx_t LAN8742_IOCtx = {ETH_PHY_IO_Init, ETH_PHY_IO_DeInit,
|
||||
ETH_PHY_IO_WriteReg, ETH_PHY_IO_ReadReg,
|
||||
lan8742_Object_t LAN8742;
|
||||
lan8742_IOCtx_t LAN8742_IOCtx = {ETH_PHY_IO_Init,
|
||||
ETH_PHY_IO_DeInit,
|
||||
ETH_PHY_IO_WriteReg,
|
||||
ETH_PHY_IO_ReadReg,
|
||||
ETH_PHY_IO_GetTick};
|
||||
|
||||
/* Private functions ---------------------------------------------------------*/
|
||||
void pbuf_free_custom(struct pbuf *p);
|
||||
/*******************************************************************************
|
||||
LL Driver Interface ( LwIP stack --> ETH)
|
||||
*******************************************************************************/
|
||||
@ -173,9 +195,7 @@ lan8742_IOCtx_t LAN8742_IOCtx = {ETH_PHY_IO_Init, ETH_PHY_IO_DeInit,
|
||||
* for this ethernetif
|
||||
*/
|
||||
static void low_level_init(struct netif *netif) {
|
||||
uint32_t idx = 0;
|
||||
uint8_t macaddress[6] = {ETH_MAC_ADDR0, ETH_MAC_ADDR1, ETH_MAC_ADDR2,
|
||||
ETH_MAC_ADDR3, ETH_MAC_ADDR4, ETH_MAC_ADDR5};
|
||||
uint8_t macaddress[6]= {ETH_MAC_ADDR0, ETH_MAC_ADDR1, ETH_MAC_ADDR2, ETH_MAC_ADDR3, ETH_MAC_ADDR4, ETH_MAC_ADDR5};
|
||||
|
||||
EthHandle.Instance = ETH;
|
||||
EthHandle.Init.MACAddr = macaddress;
|
||||
@ -188,15 +208,15 @@ static void low_level_init(struct netif *netif) {
|
||||
HAL_ETH_Init(&EthHandle);
|
||||
|
||||
/* set MAC hardware address length */
|
||||
netif->hwaddr_len = ETHARP_HWADDR_LEN;
|
||||
netif->hwaddr_len = ETH_HWADDR_LEN;
|
||||
|
||||
/* set MAC hardware address */
|
||||
netif->hwaddr[0] = 0x02;
|
||||
netif->hwaddr[1] = 0x00;
|
||||
netif->hwaddr[2] = 0x00;
|
||||
netif->hwaddr[3] = 0x00;
|
||||
netif->hwaddr[4] = 0x00;
|
||||
netif->hwaddr[5] = 0x00;
|
||||
netif->hwaddr[0] = ETH_MAC_ADDR0;
|
||||
netif->hwaddr[1] = ETH_MAC_ADDR1;
|
||||
netif->hwaddr[2] = ETH_MAC_ADDR2;
|
||||
netif->hwaddr[3] = ETH_MAC_ADDR3;
|
||||
netif->hwaddr[4] = ETH_MAC_ADDR4;
|
||||
netif->hwaddr[5] = ETH_MAC_ADDR5;
|
||||
|
||||
/* maximum transfer unit */
|
||||
netif->mtu = ETH_MAX_PAYLOAD;
|
||||
@ -205,17 +225,12 @@ static void low_level_init(struct netif *netif) {
|
||||
/* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
|
||||
netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
|
||||
|
||||
for (idx = 0; idx < ETH_RX_DESC_CNT; idx++) {
|
||||
HAL_ETH_DescAssignMemory(&EthHandle, idx, Rx_Buff[idx], NULL);
|
||||
|
||||
/* Set Custom pbuf free function */
|
||||
rx_pbuf[idx].custom_free_function = pbuf_free_custom;
|
||||
}
|
||||
/* Initialize the RX POOL */
|
||||
LWIP_MEMPOOL_INIT(RX_POOL);
|
||||
|
||||
/* Set Tx packet config common parameters */
|
||||
memset(&TxConfig, 0, sizeof(ETH_TxPacketConfig));
|
||||
TxConfig.Attributes =
|
||||
ETH_TX_PACKETS_FEATURES_CSUM | ETH_TX_PACKETS_FEATURES_CRCPAD;
|
||||
memset(&TxConfig, 0 , sizeof(ETH_TxPacketConfig));
|
||||
TxConfig.Attributes = ETH_TX_PACKETS_FEATURES_CSUM | ETH_TX_PACKETS_FEATURES_CRCPAD;
|
||||
TxConfig.ChecksumCtrl = ETH_CHECKSUM_IPHDR_PAYLOAD_INSERT_PHDR_CALC;
|
||||
TxConfig.CRCPadCtrl = ETH_CRC_PAD_INSERT;
|
||||
|
||||
@ -245,40 +260,43 @@ static void low_level_init(struct netif *netif) {
|
||||
* dropped because of memory failure (except for the TCP timers).
|
||||
*/
|
||||
static err_t low_level_output(struct netif *netif, struct pbuf *p) {
|
||||
uint32_t i = 0, framelen = 0;
|
||||
struct pbuf *q;
|
||||
uint32_t i = 0U;
|
||||
struct pbuf *q = NULL;
|
||||
err_t errval = ERR_OK;
|
||||
ETH_BufferTypeDef Txbuffer[ETH_TX_DESC_CNT];
|
||||
ETH_BufferTypeDef Txbuffer[ETH_TX_DESC_CNT] = {0};
|
||||
|
||||
for (q = p; q != NULL; q = q->next) {
|
||||
if (i >= ETH_TX_DESC_CNT)
|
||||
memset(Txbuffer, 0 , ETH_TX_DESC_CNT*sizeof(ETH_BufferTypeDef));
|
||||
|
||||
for(q = p; q != NULL; q = q->next)
|
||||
{
|
||||
if(i >= ETH_TX_DESC_CNT)
|
||||
return ERR_IF;
|
||||
|
||||
Txbuffer[i].buffer = q->payload;
|
||||
Txbuffer[i].len = q->len;
|
||||
framelen += q->len;
|
||||
|
||||
if (i > 0) {
|
||||
Txbuffer[i - 1].next = &Txbuffer[i];
|
||||
if(i>0)
|
||||
{
|
||||
Txbuffer[i-1].next = &Txbuffer[i];
|
||||
}
|
||||
|
||||
if (q->next == NULL) {
|
||||
if(q->next == NULL)
|
||||
{
|
||||
Txbuffer[i].next = NULL;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
TxConfig.Length = framelen;
|
||||
TxConfig.Length = p->tot_len;
|
||||
TxConfig.TxBuffer = Txbuffer;
|
||||
TxConfig.pData = p;
|
||||
|
||||
HAL_StatusTypeDef ret = HAL_ETH_Transmit(&EthHandle, &TxConfig, 20);
|
||||
|
||||
HAL_StatusTypeDef ret = HAL_ETH_Transmit(&EthHandle, &TxConfig, ETH_DMA_TRANSMIT_TIMEOUT);
|
||||
if (ret != HAL_OK) {
|
||||
printf("low_level_output: Could not transmit ethernet packet, code %d!\n\r",
|
||||
ret);
|
||||
}
|
||||
|
||||
return errval;
|
||||
}
|
||||
|
||||
@ -292,30 +310,13 @@ static err_t low_level_output(struct netif *netif, struct pbuf *p) {
|
||||
*/
|
||||
static struct pbuf *low_level_input(struct netif *netif) {
|
||||
struct pbuf *p = NULL;
|
||||
ETH_BufferTypeDef RxBuff;
|
||||
uint32_t framelength = 0;
|
||||
|
||||
if (HAL_ETH_IsRxDataAvailable(&EthHandle)) {
|
||||
HAL_ETH_GetRxDataBuffer(&EthHandle, &RxBuff);
|
||||
HAL_ETH_GetRxDataLength(&EthHandle, &framelength);
|
||||
|
||||
/* Invalidate data cache for ETH Rx Buffers */
|
||||
SCB_InvalidateDCache_by_Addr((uint32_t *)Rx_Buff,
|
||||
(ETH_RX_DESC_CNT * ETH_RX_BUFFER_SIZE));
|
||||
|
||||
p = pbuf_alloced_custom(PBUF_RAW, framelength, PBUF_POOL,
|
||||
&rx_pbuf[current_pbuf_idx], RxBuff.buffer,
|
||||
ETH_RX_BUFFER_SIZE);
|
||||
if (current_pbuf_idx < (ETH_RX_DESC_CNT - 1)) {
|
||||
current_pbuf_idx++;
|
||||
} else {
|
||||
current_pbuf_idx = 0;
|
||||
}
|
||||
|
||||
return p;
|
||||
} else {
|
||||
return NULL;
|
||||
if(RxAllocStatus == RX_ALLOC_OK)
|
||||
{
|
||||
HAL_ETH_ReadData(&EthHandle, (void **)&p);
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -328,26 +329,21 @@ static struct pbuf *low_level_input(struct netif *netif) {
|
||||
* @param netif the lwip network interface structure for this ethernetif
|
||||
*/
|
||||
void ethernetif_input(struct netif *netif) {
|
||||
err_t err;
|
||||
struct pbuf *p;
|
||||
struct pbuf *p = NULL;
|
||||
|
||||
/* move received packet into a new pbuf */
|
||||
p = low_level_input(netif);
|
||||
do
|
||||
{
|
||||
p = low_level_input( netif );
|
||||
if (p != NULL)
|
||||
{
|
||||
if (netif->input( p, netif) != ERR_OK )
|
||||
{
|
||||
pbuf_free(p);
|
||||
}
|
||||
}
|
||||
|
||||
/* no packet could be read, silently ignore this */
|
||||
if (p == NULL)
|
||||
return;
|
||||
} while(p!=NULL);
|
||||
|
||||
/* entry point to the LwIP stack */
|
||||
err = netif->input(p, netif);
|
||||
|
||||
if (err != ERR_OK) {
|
||||
LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
|
||||
pbuf_free(p);
|
||||
p = NULL;
|
||||
}
|
||||
|
||||
HAL_ETH_BuildRxDescriptors(&EthHandle);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -372,6 +368,7 @@ err_t ethernetif_init(struct netif *netif) {
|
||||
|
||||
netif->name[0] = IFNAME0;
|
||||
netif->name[1] = IFNAME1;
|
||||
|
||||
/* We directly use etharp_output() here to save a function call.
|
||||
* You can instead declare your own function an call etharp_output()
|
||||
* from it if you have to do some checks before sending (e.g. if link
|
||||
@ -391,12 +388,13 @@ err_t ethernetif_init(struct netif *netif) {
|
||||
* @retval None
|
||||
*/
|
||||
void pbuf_free_custom(struct pbuf *p) {
|
||||
if (p != NULL) {
|
||||
p->flags = 0;
|
||||
p->next = NULL;
|
||||
p->len = p->tot_len = 0;
|
||||
p->ref = 0;
|
||||
p->payload = NULL;
|
||||
struct pbuf_custom* custom_pbuf = (struct pbuf_custom*)p;
|
||||
LWIP_MEMPOOL_FREE(RX_POOL, custom_pbuf);
|
||||
/* If the Rx Buffer Pool was exhausted, signal the ethernetif_input task to
|
||||
* call HAL_ETH_GetRxDataBuffer to rebuild the Rx descriptors. */
|
||||
if (RxAllocStatus == RX_ALLOC_ERROR)
|
||||
{
|
||||
RxAllocStatus = RX_ALLOC_OK;
|
||||
}
|
||||
}
|
||||
|
||||
@ -545,19 +543,22 @@ int32_t ETH_PHY_IO_GetTick(void) { return HAL_GetTick(); }
|
||||
* @retval None
|
||||
*/
|
||||
void ethernet_link_check_state(struct netif *netif) {
|
||||
ETH_MACConfigTypeDef MACConf;
|
||||
uint32_t PHYLinkState;
|
||||
uint32_t linkchanged = 0, speed = 0, duplex = 0;
|
||||
ETH_MACConfigTypeDef MACConf = {0};
|
||||
int32_t PHYLinkState = 0U;
|
||||
uint32_t linkchanged = 0U, speed = 0U, duplex = 0U;
|
||||
|
||||
PHYLinkState = LAN8742_GetLinkState(&LAN8742);
|
||||
|
||||
if (netif_is_link_up(netif) && (PHYLinkState <= LAN8742_STATUS_LINK_DOWN)) {
|
||||
HAL_ETH_Stop(&EthHandle);
|
||||
if(netif_is_link_up(netif) && (PHYLinkState <= LAN8742_STATUS_LINK_DOWN))
|
||||
{
|
||||
HAL_ETH_Stop_IT(&EthHandle);
|
||||
netif_set_down(netif);
|
||||
netif_set_link_down(netif);
|
||||
} else if (!netif_is_link_up(netif) &&
|
||||
(PHYLinkState > LAN8742_STATUS_LINK_DOWN)) {
|
||||
switch (PHYLinkState) {
|
||||
}
|
||||
else if(!netif_is_link_up(netif) && (PHYLinkState > LAN8742_STATUS_LINK_DOWN))
|
||||
{
|
||||
switch (PHYLinkState)
|
||||
{
|
||||
case LAN8742_STATUS_100MBITS_FULLDUPLEX:
|
||||
duplex = ETH_FULLDUPLEX_MODE;
|
||||
speed = ETH_SPEED_100M;
|
||||
@ -582,13 +583,14 @@ void ethernet_link_check_state(struct netif *netif) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (linkchanged) {
|
||||
if(linkchanged)
|
||||
{
|
||||
/* Get MAC Config MAC */
|
||||
HAL_ETH_GetMACConfig(&EthHandle, &MACConf);
|
||||
MACConf.DuplexMode = duplex;
|
||||
MACConf.Speed = speed;
|
||||
HAL_ETH_SetMACConfig(&EthHandle, &MACConf);
|
||||
HAL_ETH_Start(&EthHandle);
|
||||
HAL_ETH_Start_IT(&EthHandle);
|
||||
netif_set_up(netif);
|
||||
netif_set_link_up(netif);
|
||||
}
|
||||
|
Reference in New Issue
Block a user