fsfw-example-common/stm32h7/networking/ethernetif.c

629 lines
21 KiB
C
Raw Normal View History

2021-07-12 21:21:03 +02:00
/**
2022-05-05 20:55:28 +02:00
******************************************************************************
* @file LwIP/LwIP_UDP_Echo_Client/Src/ethernetif.c
* @author MCD Application Team
* @brief This file implements Ethernet network interface drivers for lwIP
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2017 STMicroelectronics International N.V.
* All rights reserved.</center></h2>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted, provided that the following conditions are met:
*
* 1. Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of other
* contributors to this software may be used to endorse or promote products
* derived from this software without specific written permission.
* 4. This software, including modifications and/or derivative works of this
* software, must execute solely and exclusively on microcontroller or
* microprocessor devices manufactured by or for STMicroelectronics.
* 5. Redistribution and use of this software other than as permitted under
* this license is void and will automatically terminate your rights under
* this license.
*
* THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
* RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
* SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
2021-07-12 21:21:03 +02:00
/* Includes ------------------------------------------------------------------*/
#include "ethernetif.h"
2022-05-05 20:55:28 +02:00
#include <stdbool.h>
2022-08-08 12:32:06 +02:00
#include <string.h>
2022-05-05 20:55:28 +02:00
#include "fsfw/FSFW.h"
2022-08-08 12:32:06 +02:00
#include "lan8742.h"
#include "lwip/netif.h"
#include "lwip/opt.h"
#include "lwip/timeouts.h"
#include "netif/etharp.h"
#include "stm32h7xx_hal.h"
2021-07-12 21:21:03 +02:00
2021-07-14 10:24:24 +02:00
#ifdef FSFW_OSAL_RTEMS
2021-07-12 21:21:03 +02:00
#include <rtems.h>
#endif
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Network interface name */
#define IFNAME0 's'
#define IFNAME1 't'
2022-08-08 12:32:06 +02:00
#define ETH_DMA_TRANSMIT_TIMEOUT (20U)
2022-08-08 12:32:06 +02:00
#define ETH_RX_BUFFER_SIZE 1536U
#define ETH_RX_BUFFER_CNT 12U
#define ETH_TX_BUFFER_MAX ((ETH_TX_DESC_CNT)*2U)
2021-07-12 21:21:03 +02:00
#define DMA_DESCRIPTOR_ALIGNMENT 0x20
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
2022-05-05 20:55:28 +02:00
/*
2021-07-12 21:21:03 +02:00
@Note: This interface is implemented to operate in zero-copy mode only:
2022-05-22 15:30:08 +02:00
- Rx buffers are allocated statically and passed directly to the LwIP
stack they will return back to DMA after been processed by the stack.
2022-05-05 20:55:28 +02:00
- Tx Buffers will be allocated from LwIP stack memory heap,
2021-07-12 21:21:03 +02:00
then passed to ETH HAL driver.
2022-05-05 20:55:28 +02:00
@Notes:
1.a. ETH DMA Rx descriptors must be contiguous, the default count is 4,
2021-07-12 21:21:03 +02:00
to customize it please redefine ETH_RX_DESC_CNT in stm32xxxx_hal_conf.h
2022-05-05 20:55:28 +02:00
1.b. ETH DMA Tx descriptors must be contiguous, the default count is 4,
2021-07-12 21:21:03 +02:00
to customize it please redefine ETH_TX_DESC_CNT in stm32xxxx_hal_conf.h
2.a. Rx Buffers number must be between ETH_RX_DESC_CNT and 2*ETH_RX_DESC_CNT
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)
*/
2022-08-08 12:32:06 +02:00
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;
2021-07-12 21:21:03 +02:00
2022-05-05 20:55:28 +02:00
#if defined(__ICCARM__) /*!< IAR Compiler */
2021-07-12 21:21:03 +02:00
2022-08-08 12:32:06 +02:00
#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 */
2021-07-12 21:21:03 +02:00
2022-05-05 20:55:28 +02:00
#elif defined(__CC_ARM) /* MDK ARM Compiler */
2021-07-12 21:21:03 +02:00
2022-08-08 12:32:06 +02:00
__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 */
2021-07-12 21:21:03 +02:00
2022-05-05 20:55:28 +02:00
#elif defined(__GNUC__) /* GNU Compiler */
2021-07-12 21:21:03 +02:00
2021-07-14 10:24:24 +02:00
#ifdef FSFW_OSAL_RTEMS
2021-07-12 21:21:03 +02:00
/* Put into special RTEMS section and align correctly */
2022-08-08 12:32:06 +02:00
ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT]
__attribute__((section(".bsp_nocache"),
__aligned__(DMA_DESCRIPTOR_ALIGNMENT))); /* Ethernet Rx DMA Descriptors */
2021-07-12 21:21:03 +02:00
/* Put into special RTEMS section and align correctly */
2022-08-08 12:32:06 +02:00
ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT]
__attribute__((section(".bsp_nocache"),
__aligned__(DMA_DESCRIPTOR_ALIGNMENT))); /* Ethernet Tx DMA Descriptors */
2022-05-22 15:30:08 +02:00
/* Ethernet Receive Buffers. Just place somewhere is BSS instead of explicitely
* placing it */
2021-07-12 21:21:03 +02:00
uint8_t Rx_Buff[ETH_RX_DESC_CNT][ETH_RX_BUFFER_SIZE];
2021-07-14 10:24:24 +02:00
#elif defined FSFW_OSAL_FREERTOS
2022-08-08 12:32:06 +02:00
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 */
2021-07-12 21:21:03 +02:00
#endif /* defined ( __GNUC__ ) */
/* Memory Pool Declaration */
LWIP_MEMPOOL_DECLARE(RX_POOL, ETH_RX_BUFFER_CNT, sizeof(RxBuff_t), "Zero-copy RX PBUF pool");
2022-08-08 12:32:06 +02:00
#if defined(__ICCARM__) /*!< IAR Compiler */
#pragma location = 0x30000400
extern u8_t memp_memory_RX_POOL_base[];
2022-08-08 12:32:06 +02:00
#elif defined(__CC_ARM) /* MDK ARM Compiler */
__attribute__((section(".Rx_PoolSection"))) extern u8_t memp_memory_RX_POOL_base[];
2022-08-08 12:32:06 +02:00
#elif defined(__GNUC__) /* GNU Compiler */
__attribute__((section(".Rx_PoolSection"))) extern u8_t memp_memory_RX_POOL_base[];
#endif
2021-07-12 21:21:03 +02:00
/* Global boolean to track ethernet connection */
bool ethernet_cable_connected;
/* Variable Definitions */
static uint8_t RxAllocStatus;
2021-07-12 21:21:03 +02:00
/* Global Ethernet handle*/
2021-07-12 21:21:03 +02:00
ETH_HandleTypeDef EthHandle;
2022-05-05 20:55:28 +02:00
ETH_TxPacketConfig TxConfig;
2021-07-12 21:21:03 +02:00
/* Private function prototypes -----------------------------------------------*/
u32_t sys_now(void);
extern void Error_Handler(void);
2021-07-12 21:21:03 +02:00
int32_t ETH_PHY_IO_Init(void);
2022-08-08 12:32:06 +02:00
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);
2021-07-12 21:21:03 +02:00
int32_t ETH_PHY_IO_GetTick(void);
lan8742_Object_t LAN8742;
2022-08-08 12:32:06 +02:00
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};
2021-07-12 21:21:03 +02:00
/* Private functions ---------------------------------------------------------*/
void pbuf_free_custom(struct pbuf *p);
2021-07-12 21:21:03 +02:00
/*******************************************************************************
2022-05-05 20:55:28 +02:00
LL Driver Interface ( LwIP stack --> ETH)
2021-07-12 21:21:03 +02:00
*******************************************************************************/
/**
2022-05-05 20:55:28 +02:00
* @brief In this function, the hardware should be initialized.
* Called from ethernetif_init().
*
* @param netif the already initialized lwip network interface structure
* for this ethernetif
*/
static void low_level_init(struct netif *netif) {
2022-08-08 12:32:06 +02:00
uint8_t macaddress[6] = {ETH_MAC_ADDR0, ETH_MAC_ADDR1, ETH_MAC_ADDR2,
ETH_MAC_ADDR3, ETH_MAC_ADDR4, ETH_MAC_ADDR5};
2022-05-05 20:55:28 +02:00
EthHandle.Instance = ETH;
2021-07-12 21:21:03 +02:00
EthHandle.Init.MACAddr = macaddress;
EthHandle.Init.MediaInterface = HAL_ETH_RMII_MODE;
EthHandle.Init.RxDesc = DMARxDscrTab;
EthHandle.Init.TxDesc = DMATxDscrTab;
EthHandle.Init.RxBuffLen = ETH_RX_BUFFER_SIZE;
2022-05-05 20:55:28 +02:00
2021-07-12 21:21:03 +02:00
/* configure ethernet peripheral (GPIOs, clocks, MAC, DMA) */
HAL_ETH_Init(&EthHandle);
/* set MAC hardware address length */
netif->hwaddr_len = ETH_HWADDR_LEN;
2022-05-05 20:55:28 +02:00
2021-07-12 21:21:03 +02:00
/* set MAC hardware address */
2022-08-08 12:32:06 +02:00
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;
2022-05-05 20:55:28 +02:00
2021-07-12 21:21:03 +02:00
/* maximum transfer unit */
netif->mtu = ETH_MAX_PAYLOAD;
2022-05-05 20:55:28 +02:00
2021-07-12 21:21:03 +02:00
/* device capabilities */
/* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
2022-05-05 20:55:28 +02:00
/* Initialize the RX POOL */
LWIP_MEMPOOL_INIT(RX_POOL);
2022-05-05 20:55:28 +02:00
2021-07-12 21:21:03 +02:00
/* Set Tx packet config common parameters */
2022-08-08 12:32:06 +02:00
memset(&TxConfig, 0, sizeof(ETH_TxPacketConfig));
TxConfig.Attributes = ETH_TX_PACKETS_FEATURES_CSUM | ETH_TX_PACKETS_FEATURES_CRCPAD;
2021-07-12 21:21:03 +02:00
TxConfig.ChecksumCtrl = ETH_CHECKSUM_IPHDR_PAYLOAD_INSERT_PHDR_CALC;
TxConfig.CRCPadCtrl = ETH_CRC_PAD_INSERT;
/* Set PHY IO functions */
LAN8742_RegisterBusIO(&LAN8742, &LAN8742_IOCtx);
/* Initialize the LAN8742 ETH PHY */
LAN8742_Init(&LAN8742);
ethernet_link_check_state(netif);
}
/**
2022-05-22 15:30:08 +02:00
* @brief This function should do the actual transmission of the packet. The
* packet is contained in the pbuf that is passed to the function. This pbuf
2022-05-05 20:55:28 +02:00
* might be chained.
*
* @param netif the lwip network interface structure for this ethernetif
2022-05-22 15:30:08 +02:00
* @param p the MAC packet to send (e.g. IP packet including MAC addresses and
* type)
2022-05-05 20:55:28 +02:00
* @return ERR_OK if the packet could be sent
* an err_t value if the packet couldn't be sent
*
* @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
* strange results. You might consider waiting for space in the DMA queue
* to become availale since the stack doesn't retry to send a packet
* 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 = 0U;
struct pbuf *q = NULL;
2021-07-12 21:21:03 +02:00
err_t errval = ERR_OK;
ETH_BufferTypeDef Txbuffer[ETH_TX_DESC_CNT] = {0};
2022-08-08 12:32:06 +02:00
memset(Txbuffer, 0, ETH_TX_DESC_CNT * sizeof(ETH_BufferTypeDef));
2022-05-05 20:55:28 +02:00
2022-08-08 12:32:06 +02:00
for (q = p; q != NULL; q = q->next) {
if (i >= ETH_TX_DESC_CNT) return ERR_IF;
2022-05-05 20:55:28 +02:00
2021-07-12 21:21:03 +02:00
Txbuffer[i].buffer = q->payload;
Txbuffer[i].len = q->len;
2022-05-05 20:55:28 +02:00
2022-08-08 12:32:06 +02:00
if (i > 0) {
Txbuffer[i - 1].next = &Txbuffer[i];
2021-07-12 21:21:03 +02:00
}
2022-05-05 20:55:28 +02:00
2022-08-08 12:32:06 +02:00
if (q->next == NULL) {
2021-07-12 21:21:03 +02:00
Txbuffer[i].next = NULL;
}
i++;
}
TxConfig.Length = p->tot_len;
2021-07-12 21:21:03 +02:00
TxConfig.TxBuffer = Txbuffer;
TxConfig.pData = p;
2021-07-12 21:21:03 +02:00
HAL_StatusTypeDef ret = HAL_ETH_Transmit(&EthHandle, &TxConfig, ETH_DMA_TRANSMIT_TIMEOUT);
2022-05-05 20:55:28 +02:00
if (ret != HAL_OK) {
2022-08-08 12:32:06 +02:00
printf("low_level_output: Could not transmit ethernet packet, code %d!\n\r", ret);
2021-07-12 21:21:03 +02:00
}
return errval;
}
/**
2022-05-05 20:55:28 +02:00
* @brief Should allocate a pbuf and transfer the bytes of the incoming
* packet from the interface into the pbuf.
*
* @param netif the lwip network interface structure for this ethernetif
* @return a pbuf filled with the received packet (including MAC header)
* NULL on memory error
*/
static struct pbuf *low_level_input(struct netif *netif) {
2021-07-12 21:21:03 +02:00
struct pbuf *p = NULL;
2022-08-08 12:32:06 +02:00
if (RxAllocStatus == RX_ALLOC_OK) {
HAL_ETH_ReadData(&EthHandle, (void **)&p);
2021-07-12 21:21:03 +02:00
}
return p;
2021-07-12 21:21:03 +02:00
}
/**
2022-05-22 15:30:08 +02:00
* @brief This function is the ethernetif_input task, it is processed 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.
2022-05-05 20:55:28 +02:00
*
* @param netif the lwip network interface structure for this ethernetif
*/
void ethernetif_input(struct netif *netif) {
struct pbuf *p = NULL;
2021-07-12 21:21:03 +02:00
2022-08-08 12:32:06 +02:00
do {
p = low_level_input(netif);
if (p != NULL) {
if (netif->input(p, netif) != ERR_OK) {
pbuf_free(p);
}
}
2021-07-12 21:21:03 +02:00
2022-08-08 12:32:06 +02:00
} while (p != NULL);
2021-07-12 21:21:03 +02:00
}
/**
2022-05-05 20:55:28 +02:00
* @brief 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.
*
* 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) {
2021-07-12 21:21:03 +02:00
LWIP_ASSERT("netif != NULL", (netif != NULL));
#if LWIP_NETIF_HOSTNAME
/* Initialize interface hostname */
netif->hostname = "lwip";
#endif /* LWIP_NETIF_HOSTNAME */
netif->name[0] = IFNAME0;
netif->name[1] = IFNAME1;
2021-07-12 21:21:03 +02:00
/* 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
* is available...) */
netif->output = etharp_output;
netif->linkoutput = low_level_output;
/* initialize the hardware */
low_level_init(netif);
return ERR_OK;
}
/**
2022-05-05 20:55:28 +02:00
* @brief Custom Rx pbuf free callback
* @param pbuf: pbuf to be freed
* @retval None
*/
void pbuf_free_custom(struct pbuf *p) {
2022-08-08 12:32:06 +02:00
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. */
2022-08-08 12:32:06 +02:00
if (RxAllocStatus == RX_ALLOC_ERROR) {
RxAllocStatus = RX_ALLOC_OK;
2021-07-12 21:21:03 +02:00
}
}
/**
2022-05-05 20:55:28 +02:00
* @brief Returns the current time in milliseconds
* when LWIP_TIMERS == 1 and NO_SYS == 1
* @param None
* @retval Current Time value
*/
u32_t sys_now(void) { return HAL_GetTick(); }
2021-07-12 21:21:03 +02:00
/*******************************************************************************
Ethernet MSP Routines
*******************************************************************************/
/**
2022-05-05 20:55:28 +02:00
* @brief Initializes the ETH MSP.
* @param heth: ETH handle
* @retval None
*/
void HAL_ETH_MspInit(ETH_HandleTypeDef *heth) {
2021-07-12 21:21:03 +02:00
GPIO_InitTypeDef GPIO_InitStructure;
2022-05-05 20:55:28 +02:00
2021-07-12 21:21:03 +02:00
/* Ethernett MSP init: RMII Mode */
2022-05-05 20:55:28 +02:00
2021-07-12 21:21:03 +02:00
/* Enable GPIOs clocks */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();
2022-05-22 15:30:08 +02:00
/* Ethernet pins configuration
* ************************************************/
2021-07-12 21:21:03 +02:00
/*
RMII_REF_CLK ----------------------> PA1
RMII_MDIO -------------------------> PA2
RMII_MDC --------------------------> PC1
RMII_MII_CRS_DV -------------------> PA7
RMII_MII_RXD0 ---------------------> PC4
RMII_MII_RXD1 ---------------------> PC5
RMII_MII_RXER ---------------------> PG2
RMII_MII_TX_EN --------------------> PG11
RMII_MII_TXD0 ---------------------> PG13
RMII_MII_TXD1 ---------------------> PB13
*/
/* Configure PA1, PA2 and PA7 */
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
2022-05-05 20:55:28 +02:00
GPIO_InitStructure.Pull = GPIO_NOPULL;
2021-07-12 21:21:03 +02:00
GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
2022-05-05 20:55:28 +02:00
2021-07-12 21:21:03 +02:00
/* Configure PB13 */
GPIO_InitStructure.Pin = GPIO_PIN_13;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
2022-05-05 20:55:28 +02:00
2021-07-12 21:21:03 +02:00
/* Configure PC1, PC4 and PC5 */
GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5;
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Configure PG2, PG11, PG13 and PG14 */
2022-05-05 20:55:28 +02:00
GPIO_InitStructure.Pin = GPIO_PIN_2 | GPIO_PIN_11 | GPIO_PIN_13;
HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
2021-07-12 21:21:03 +02:00
#if NO_SYS == 0
/* Enable the Ethernet global Interrupt */
HAL_NVIC_SetPriority(ETH_IRQn, 0x7, 0);
HAL_NVIC_EnableIRQ(ETH_IRQn);
#endif
2022-05-05 20:55:28 +02:00
2021-07-12 21:21:03 +02:00
/* Enable Ethernet clocks */
__HAL_RCC_ETH1MAC_CLK_ENABLE();
__HAL_RCC_ETH1TX_CLK_ENABLE();
__HAL_RCC_ETH1RX_CLK_ENABLE();
}
/*******************************************************************************
PHI IO Functions
*******************************************************************************/
/**
2022-05-05 20:55:28 +02:00
* @brief Initializes the MDIO interface GPIO and clocks.
* @param None
* @retval 0 if OK, -1 if ERROR
*/
int32_t ETH_PHY_IO_Init(void) {
2021-07-12 21:21:03 +02:00
/* We assume that MDIO GPIO configuration is already done
2022-05-05 20:55:28 +02:00
in the ETH_MspInit() else it should be done here
2021-07-12 21:21:03 +02:00
*/
2022-05-05 20:55:28 +02:00
2021-07-12 21:21:03 +02:00
/* Configure the MDIO Clock */
HAL_ETH_SetMDIOClockRange(&EthHandle);
2022-05-05 20:55:28 +02:00
2021-07-12 21:21:03 +02:00
return 0;
}
/**
2022-05-05 20:55:28 +02:00
* @brief De-Initializes the MDIO interface .
* @param None
* @retval 0 if OK, -1 if ERROR
*/
int32_t ETH_PHY_IO_DeInit(void) { return 0; }
2021-07-12 21:21:03 +02:00
/**
2022-05-05 20:55:28 +02:00
* @brief Read a PHY register through the MDIO interface.
* @param DevAddr: PHY port address
* @param RegAddr: PHY register address
* @param pRegVal: pointer to hold the register value
* @retval 0 if OK -1 if Error
*/
2022-08-08 12:32:06 +02:00
int32_t ETH_PHY_IO_ReadReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t *pRegVal) {
if (HAL_ETH_ReadPHYRegister(&EthHandle, DevAddr, RegAddr, pRegVal) != HAL_OK) {
2021-07-12 21:21:03 +02:00
return -1;
}
2022-05-05 20:55:28 +02:00
2021-07-12 21:21:03 +02:00
return 0;
}
/**
2022-05-05 20:55:28 +02:00
* @brief Write a value to a PHY register through the MDIO interface.
* @param DevAddr: PHY port address
* @param RegAddr: PHY register address
* @param RegVal: Value to be written
* @retval 0 if OK -1 if Error
*/
2022-08-08 12:32:06 +02:00
int32_t ETH_PHY_IO_WriteReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t RegVal) {
if (HAL_ETH_WritePHYRegister(&EthHandle, DevAddr, RegAddr, RegVal) != HAL_OK) {
2021-07-12 21:21:03 +02:00
return -1;
}
2022-05-05 20:55:28 +02:00
2021-07-12 21:21:03 +02:00
return 0;
}
/**
2022-05-05 20:55:28 +02:00
* @brief Get the time in millisecons used for internal PHY driver process.
* @retval Time value
*/
int32_t ETH_PHY_IO_GetTick(void) { return HAL_GetTick(); }
2021-07-12 21:21:03 +02:00
/**
2022-05-05 20:55:28 +02:00
* @brief
* @retval None
*/
void ethernet_link_check_state(struct netif *netif) {
ETH_MACConfigTypeDef MACConf = {0};
int32_t PHYLinkState = 0U;
uint32_t linkchanged = 0U, speed = 0U, duplex = 0U;
2022-05-05 20:55:28 +02:00
2021-07-12 21:21:03 +02:00
PHYLinkState = LAN8742_GetLinkState(&LAN8742);
2022-08-08 12:32:06 +02:00
if (netif_is_link_up(netif) && (PHYLinkState <= LAN8742_STATUS_LINK_DOWN)) {
HAL_ETH_Stop_IT(&EthHandle);
2021-07-12 21:21:03 +02:00
netif_set_down(netif);
netif_set_link_down(netif);
2022-08-08 12:32:06 +02:00
} 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;
linkchanged = 1;
break;
case LAN8742_STATUS_100MBITS_HALFDUPLEX:
duplex = ETH_HALFDUPLEX_MODE;
speed = ETH_SPEED_100M;
linkchanged = 1;
break;
case LAN8742_STATUS_10MBITS_FULLDUPLEX:
duplex = ETH_FULLDUPLEX_MODE;
speed = ETH_SPEED_10M;
linkchanged = 1;
break;
case LAN8742_STATUS_10MBITS_HALFDUPLEX:
duplex = ETH_HALFDUPLEX_MODE;
speed = ETH_SPEED_10M;
linkchanged = 1;
break;
default:
break;
2021-07-12 21:21:03 +02:00
}
2022-08-08 12:32:06 +02:00
if (linkchanged) {
2021-07-12 21:21:03 +02:00
/* Get MAC Config MAC */
HAL_ETH_GetMACConfig(&EthHandle, &MACConf);
MACConf.DuplexMode = duplex;
MACConf.Speed = speed;
HAL_ETH_SetMACConfig(&EthHandle, &MACConf);
HAL_ETH_Start_IT(&EthHandle);
2021-07-12 21:21:03 +02:00
netif_set_up(netif);
netif_set_link_up(netif);
}
}
}
2022-08-08 12:32:06 +02:00
void HAL_ETH_RxAllocateCallback(uint8_t **buff) {
struct pbuf_custom *p = LWIP_MEMPOOL_ALLOC(RX_POOL);
2022-08-08 12:32:06 +02:00
if (p) {
/* Get the buff from the struct pbuf address. */
*buff = (uint8_t *)p + offsetof(RxBuff_t, buff);
p->custom_free_function = pbuf_free_custom;
/* Initialize the struct pbuf.
2022-08-08 12:32:06 +02:00
* This must be performed whenever a buffer's allocated because it may be
* changed by lwIP or the app, e.g., pbuf_free decrements ref. */
pbuf_alloced_custom(PBUF_RAW, 0, PBUF_REF, p, *buff, ETH_RX_BUFFER_SIZE);
2022-08-08 12:32:06 +02:00
} else {
RxAllocStatus = RX_ALLOC_ERROR;
*buff = NULL;
}
}
2022-08-08 12:32:06 +02:00
void HAL_ETH_RxLinkCallback(void **pStart, void **pEnd, uint8_t *buff, uint16_t Length) {
struct pbuf **ppStart = (struct pbuf **)pStart;
struct pbuf **ppEnd = (struct pbuf **)pEnd;
struct pbuf *p = NULL;
/* Get the struct pbuf from the buff address. */
p = (struct pbuf *)(buff - offsetof(RxBuff_t, buff));
p->next = NULL;
p->tot_len = 0;
p->len = Length;
/* Chain the buffer. */
2022-08-08 12:32:06 +02:00
if (!*ppStart) {
/* The first buffer of the packet. */
*ppStart = p;
2022-08-08 12:32:06 +02:00
} else {
/* Chain the buffer to the end of the packet. */
(*ppEnd)->next = p;
}
2022-08-08 12:32:06 +02:00
*ppEnd = p;
2022-08-08 12:32:06 +02:00
/* Update the total length of all the buffers of the chain. Each pbuf in the chain should have its
* tot_len set to its own length, plus the length of all the following pbufs in the chain. */
for (p = *ppStart; p != NULL; p = p->next) {
p->tot_len += Length;
}
/* Invalidate data cache because Rx DMA's writing to physical memory makes it stale. */
SCB_InvalidateDCache_by_Addr((uint32_t *)buff, Length);
}
2022-08-08 12:32:06 +02:00
void HAL_ETH_TxFreeCallback(uint32_t *buff) { pbuf_free((struct pbuf *)buff); }
2022-05-05 20:55:28 +02:00
ETH_HandleTypeDef *getEthernetHandle() { return &EthHandle; }
2021-07-12 21:21:03 +02:00
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/