Handle link status

This commit is contained in:
Vasily Markov 2024-05-04 17:20:57 +03:00
parent 35f268d6dc
commit b2180a2f51
9 changed files with 82 additions and 53 deletions

View File

@ -5,11 +5,9 @@ void app() {
init_LWIP();
while(1) {
// process_LWIP();
uint32_t regvalue = 0;
HAL_ETH_ReadPHYRegister(getEthStruct(), 1, &regvalue);
delay(500);
printf("Test: %d\r\n", regvalue);
process_LWIP();
// GPIO_ToggleBits(GPIOB, GPIO_Pin_7);
delay(400);
};
}

View File

@ -2,11 +2,6 @@ set(LWIP_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lwip/src)
set(LWIP_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/lwip/src/include)
set(LWIP_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/lwip/src/core)
file(GLOB_RECURSE LWIP_SOURCES
${LWIP_SOURCE_DIR}/core/*.c
${LWIP_SOURCE_DIR}/netif/*.c
)
add_library(bsp STATIC
bsp.c
lwip/lwip.c

View File

@ -1,6 +1,3 @@
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_dma.h"
#include "misc.h"
#include "bsp.h"
#include "lwip.h"
@ -15,6 +12,12 @@ static volatile uint32_t cnt = 0;
void SysTick_Handler(void)
{
if (cnt == INTERVAL) {
// uint32_t regvalue = 0;
// HAL_ETH_ReadPHYRegister(getEthStruct(), 1, &regvalue);
// printf("PHY: %d\r\n", regvalue);
GPIO_ToggleBits(GPIOB, GPIO_Pin_14);
cnt = 0;
}
@ -52,7 +55,6 @@ void gpio_init() {
RCC_AHB1Periph_GPIOG,
ENABLE
);
GPIO_InitTypeDef gpio;
GPIO_StructInit(&gpio);
@ -60,7 +62,6 @@ void gpio_init() {
gpio.GPIO_Pin = led_pins;
GPIO_Init(GPIOB, &gpio);
GPIO_ResetBits(GPIOB, led_pins);
}
void usart_init() {
@ -106,6 +107,11 @@ void dma_init() {
void eth_init() {
RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_ETHMACEN, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_ETHMACRXEN, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_ETHMACTXEN, ENABLE);
GPIO_InitTypeDef gpio;
gpio.GPIO_Mode = GPIO_Mode_AF;
@ -134,25 +140,21 @@ void eth_init() {
GPIO_Init(GPIOB, &gpio);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_ETH); //RMII TXD1
RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_ETHMACEN, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_ETHMACRXEN, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_ETHMACTXEN, ENABLE);
NVIC_SetPriority(ETH_IRQn, 0);
NVIC_EnableIRQ(ETH_IRQn);
}
void board_init() {
uint32_t tick = SystemCoreClock/1000;
uint32_t tick = SystemCoreClock/1000 - 1;
SysTick_Config(tick);
NVIC_EnableIRQ(SysTick_IRQn);
__enable_irq();
// __enable_irq();
gpio_init();
usart_init();
// dma_init();
eth_init();
init_LWIP();
delay(50); //wait until periph init
printf("Controller is started...\r\n");
}

View File

@ -3,7 +3,9 @@
#include "stm32f4xx.h"
#include "stm32f4xx_usart.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_dma.h"
void board_init();
uint32_t getRegister();
uint32_t getSysTick();

View File

@ -290,7 +290,6 @@ static struct pbuf * low_level_input(struct netif *netif)
/* get received frame */
if (HAL_ETH_GetReceivedFrame(&heth) != HAL_OK)
return NULL;
/* Obtain the size of the packet and put it into the "len" variable. */
len = heth.RxFrameInfos.length;
buffer = (uint8_t *)heth.RxFrameInfos.buffer;
@ -354,6 +353,30 @@ static struct pbuf * low_level_input(struct netif *netif)
return p;
}
void check_link_status(struct netif* netif) {
uint32_t regvalue = 0;
if (HAL_ETH_ReadPHYRegister(&heth, PHY_BSR, &regvalue) == HAL_OK)
{
if((regvalue & PHY_LINKED_STATUS)== (uint16_t)RESET)
{
// Link status = disconnected
if (netif_is_link_up(netif))
{
netif_set_down(netif);
printf("unplugged\r\n");
netif_set_link_down(netif);
}
} else {
// Link status = connected
if (!netif_is_link_up(netif))
{
printf("plugged\r\n");
NVIC_SystemReset();
}
}
}
}
/**
* This function should be called when a packet is ready to be read
* from the interface. It uses the function low_level_input() that
@ -365,24 +388,25 @@ static struct pbuf * low_level_input(struct netif *netif)
*/
void ethernetif_input(struct netif *netif)
{
err_t err;
struct pbuf *p;
/* move received packet into a new pbuf */
p = low_level_input(netif);
// err_t err;
// struct pbuf *p;
// uint32_t regvalue = 0;
// /* move received packet into a new pbuf */
// p = low_level_input(netif);
netif_set_link_callback(netif, ethernetif_update_config);
check_link_status(netif);
// /* no packet could be read, silently ignore this */
// if (p == NULL) return;
/* no packet could be read, silently ignore this */
if (p == NULL) return;
/* 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;
}
// // /* 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;
// }
}
#if !LWIP_ARP
@ -486,9 +510,10 @@ void ethernetif_update_config(struct netif *netif)
{
__IO uint32_t tickstart = 0;
uint32_t regvalue = 0;
if(netif_is_link_up(netif))
{
GPIO_SetBits(GPIOB, GPIO_Pin_0);
GPIO_SetBits(GPIOB, GPIO_Pin_7);
/* Restart the auto-negotiation */
if(heth.Init.AutoNegotiation != ETH_AUTONEGOTIATION_DISABLE)
{
@ -558,7 +583,9 @@ void ethernetif_update_config(struct netif *netif)
else
{
/* Stop MAC interface */
HAL_ETH_Stop(&heth);
GPIO_ResetBits(GPIOB, GPIO_Pin_0);
GPIO_SetBits(GPIOB, GPIO_Pin_7);
// HAL_ETH_Stop(&heth);
}
ethernetif_notify_conn_changed(netif);

View File

@ -10,7 +10,7 @@ err_t ethernetif_init(struct netif *netif);
void ethernetif_input(struct netif *netif);
void ethernetif_update_config(struct netif *netif);
void ethernetif_notify_conn_changed(struct netif *netif);
void check_link_status(struct netif* netif);
u32_t sys_jiffies(void);
u32_t sys_now(void);
ETH_HandleTypeDef* getEthStruct();

View File

@ -65,6 +65,6 @@ void init_LWIP(void)
void process_LWIP()
{
ethernetif_input(&gnetif);
sys_check_timeouts();
// sys_check_timeouts();
}

View File

@ -76,7 +76,7 @@
/* LwIP Stack Parameters (modified compared to initialization value in opt.h) -*/
/* Parameters set in STM32CubeMX LwIP Configuration GUI -*/
/*----- Value in opt.h for LWIP_DHCP: 0 -----*/
#define LWIP_DHCP 1
#define LWIP_DHCP 0
/*----- Value in opt.h for NO_SYS: 0 -----*/
#define NO_SYS 1
/*----- Value in opt.h for SYS_LIGHTWEIGHT_PROT: 1 -----*/
@ -128,19 +128,19 @@
/*----- Value in opt.h for CHECKSUM_CHECK_ICMP6: 1 -----*/
#define CHECKSUM_CHECK_ICMP6 0
/*----- Default Value for ETHARP_DEBUG: LWIP_DBG_OFF ---*/
#define ETHARP_DEBUG LWIP_DBG_ON
#define ETHARP_DEBUG LWIP_DBG_OFF
/*----- Default Value for NETIF_DEBUG: LWIP_DBG_OFF ---*/
#define NETIF_DEBUG LWIP_DBG_ON
#define NETIF_DEBUG LWIP_DBG_OFF
/*----- Default Value for PBUF_DEBUG: LWIP_DBG_OFF ---*/
#define PBUF_DEBUG LWIP_DBG_OFF
/*----- Default Value for ICMP_DEBUG: LWIP_DBG_OFF ---*/
#define ICMP_DEBUG LWIP_DBG_ON
#define ICMP_DEBUG LWIP_DBG_OFF
/*----- Default Value for SYS_DEBUG: LWIP_DBG_OFF ---*/
#define SYS_DEBUG LWIP_DBG_ON
#define SYS_DEBUG LWIP_DBG_OFF
/*----- Default Value for UDP_DEBUG: LWIP_DBG_OFF ---*/
#define UDP_DEBUG LWIP_DBG_ON
#define UDP_DEBUG LWIP_DBG_OFF
/*----- Default Value for DHCP_DEBUG: LWIP_DBG_OFF ---*/
#define DHCP_DEBUG LWIP_DBG_ON
#define DHCP_DEBUG LWIP_DBG_OFF
/*-----------------------------------------------------------------------------*/
/* USER CODE BEGIN 1 */
#define LWIP_DEBUG 1
@ -148,6 +148,8 @@
#define LWIP_UDP 1
#define LWIP_TCP 0
#define LWIP_NETIF_LINK_CALLBACK 1
/* USER CODE END 1 */
#ifdef __cplusplus

View File

@ -1,7 +1,8 @@
project(stm32f4_lib C ASM)
set(DEVICE_FAMILY STM32F439xx)
get_filename_component(LINKER_SCRIPT src/stm32f439zi_flash.ld ABSOLUTE)
# get_filename_component(LINKER_SCRIPT src/stm32f439zi_flash.ld ABSOLUTE)
get_filename_component(LINKER_SCRIPT src/STM32F439ZITx_FLASH.ld ABSOLUTE)
add_library(stm32f4xx STATIC
src/STM32F4xx_StdPeriph_Driver/misc.c
@ -38,7 +39,8 @@ add_library(stm32f4xx STATIC
src/STM32F4xx_StdPeriph_Driver/stm32f4xx_usart.c
# src/STM32F4xx_StdPeriph_Driver/stm32f4xx_wwdg.c
src/system_stm32f4xx.c
src/startup_stm32f4xx.S
# src/startup_stm32f4xx.S
src/startup_stm32f439xx.S
)
target_include_directories(stm32f4xx PUBLIC
@ -50,6 +52,7 @@ target_include_directories(stm32f4xx PUBLIC
target_compile_definitions(stm32f4xx PUBLIC USE_STDPERIPH_DRIVER)
target_compile_definitions(stm32f4xx PUBLIC ${DEVICE_FAMILY})
target_compile_definitions(stm32f4xx PUBLIC HSE_VALUE=8000000)
target_compile_definitions(stm32f4xx PUBLIC SYSTEM_CORE_CLOCK=180000000)
target_compile_definitions(stm32f4xx PUBLIC USE_STM324x7I_EVAL)
target_compile_options(stm32f4xx PRIVATE -nostartfiles)