Link status checking

This commit is contained in:
Vasily Markov 2024-05-05 19:31:36 +03:00
parent a2e13390e9
commit 93b305123f
10 changed files with 96 additions and 65 deletions

View File

@ -6,7 +6,6 @@ void app() {
while(1) {
process_LWIP();
// GPIO_ToggleBits(GPIOB, GPIO_Pin_7);
delay(400);
};
}

View File

@ -4,6 +4,7 @@ set(LWIP_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/lwip/src/core)
add_library(bsp STATIC
bsp.c
stm32f4xx_it.c
lwip/lwip.c
lwip/ethernetif.c
lwip/stm32f4_eth_periph/src/stm32f4xx_hal_eth.c

View File

@ -1,26 +1,12 @@
#include "misc.h"
#include "bsp.h"
#include "lwip.h"
#define MAX_DELAY 0xFFFFFFFU
#define INTERVAL 500
static uint32_t EthStatus = 0;
static volatile uint32_t sysTick = 0;
static volatile uint32_t cnt = 0;
static lwip_status_t lwip_status = {.link_status = LINK_DOWN};
void SysTick_Handler(void)
{
if (cnt == INTERVAL) {
GPIO_ToggleBits(GPIOB, GPIO_Pin_0);
cnt = 0;
}
sysTick++;
cnt++;
}
uint32_t getSysTick() {
return sysTick;
lwip_status_t* get_lwip_status() {
return &lwip_status;
}
void delay(uint32_t delay) {
@ -32,21 +18,6 @@ void delay(uint32_t delay) {
while((getSysTick() - tickStart) < wait) {}
}
void TIM2_IRQHandler()
{
GPIO_ToggleBits(GPIOB, GPIO_Pin_7);
printf("Tim\r\n");
printf("Tim\r\n");
printf("Tim\r\n");
printf("Tim\r\n");
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
void USART2_IRQHandler()
{
}
void gpio_init() {
const uint16_t led_pins = GPIO_Pin_0 | GPIO_Pin_7 | GPIO_Pin_14;
@ -92,19 +63,26 @@ void usart_init() {
}
void tim_init() {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseInitTypeDef tim2;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);
TIM_TimeBaseInitTypeDef tim7;
tim2.TIM_Prescaler = 9000-1;
tim2.TIM_CounterMode = TIM_CounterMode_Up;
tim2.TIM_Period = 5000;
tim2.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructInit(&tim7);
TIM_TimeBaseInit(TIM2, &tim2);
tim7.TIM_Prescaler = 9000-1;
tim7.TIM_CounterMode = TIM_CounterMode_Up;
tim7.TIM_Period = 2000;
tim7.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
NVIC_SetPriority(TIM2_IRQn, 0);
NVIC_EnableIRQ(TIM2_IRQn);
TIM_TimeBaseInit(TIM7, &tim7);
TIM_ClearITPendingBit(TIM7, TIM_IT_Update);
TIM_UpdateRequestConfig(TIM7, TIM_UpdateSource_Regular);
TIM_ITConfig(TIM7, TIM_IT_Update, ENABLE);
NVIC_SetPriority(TIM7_IRQn, 0);
NVIC_EnableIRQ(TIM7_IRQn);
TIM_Cmd(TIM7, ENABLE);
}
void dma_init() {
@ -166,16 +144,15 @@ void eth_init() {
void board_init() {
uint32_t tick = SystemCoreClock/1000 - 1;
__enable_irq();
SysTick_Config(tick);
NVIC_EnableIRQ(SysTick_IRQn);
__enable_irq();
gpio_init();
usart_init();
tim_init();
// dma_init();
eth_init();
init_LWIP();
TIM_Cmd(TIM2, ENABLE);
tim_init();
printf("Controller is started...\r\n");
}

View File

@ -7,9 +7,20 @@
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_dma.h"
#include "stm32f4xx_tim.h"
#include "lwip/lwip.h"
typedef enum {
LINK_DOWN,
LINK_UP
} status_t;
typedef struct {
uint8_t link_status;
} lwip_status_t;
void board_init();
uint32_t getRegister();
uint32_t getSysTick();
void delay(uint32_t);
lwip_status_t* get_lwip_status();
#endif

View File

@ -57,7 +57,6 @@
#include "lwip/ethip6.h"
#include "ethernetif.h"
#include <string.h>
#include "bsp.h"
#include <stdio.h>
/* Private define ------------------------------------------------------------*/
@ -359,20 +358,22 @@ void check_link_status(struct netif* netif) {
{
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();
}
// Link status = disconnected
if (netif_is_link_up(netif))
{
netif_set_down(netif);
get_lwip_status()->link_status = LINK_DOWN;
netif_set_link_down(netif);
}
}
else {
// Link status = connected
if (!netif_is_link_up(netif))
{
get_lwip_status()->link_status = LINK_UP;
NVIC_SystemReset();
}
get_lwip_status()->link_status = LINK_UP;
}
}
}

View File

@ -4,6 +4,7 @@
#include "lwip/err.h"
#include "lwip/netif.h"
#include "stm32f4xx_hal_eth.h"
#include "bsp.h"
err_t ethernetif_init(struct netif *netif);

View File

@ -2,7 +2,6 @@
#define LWIP_H
void init_LWIP();
void process_LWIP();
#endif /* LWIP_H */

35
bsp/stm32f4xx_it.c Normal file
View File

@ -0,0 +1,35 @@
#include "stm32f4xx_it.h"
static volatile uint32_t sysTick = 0;
void TIM7_IRQHandler()
{
TIM_ClearITPendingBit(TIM7, TIM_IT_Update);
if(get_lwip_status()->link_status == LINK_UP) {
GPIO_ToggleBits(GPIOB, GPIO_Pin_7);
}
else {
GPIO_ResetBits(GPIOB, GPIO_Pin_7);
}
}
void USART2_IRQHandler()
{
}
void SysTick_Handler(void)
{
static uint32_t cnt = 0;
if (cnt == 500) {
GPIO_ToggleBits(GPIOB, GPIO_Pin_0);
cnt = 0;
}
sysTick++;
cnt++;
}
uint32_t getSysTick() {
return sysTick;
}

7
bsp/stm32f4xx_it.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef STM32F4XX_H
#define STM32F4XX_H
#include "bsp.h"
uint32_t getSysTick();
#endif

View File

@ -16,7 +16,7 @@ set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(shared_options "-Wall -Wextra -Os -mlittle-endian -mthumb -mcpu=cortex-m4 -mthumb-interwork -mfloat-abi=hard -mfpu=fpv4-sp-d16")
set(shared_options "-Wall -Wextra -O2 -mlittle-endian -mthumb -mcpu=cortex-m4 -mthumb-interwork -mfloat-abi=hard -mfpu=fpv4-sp-d16")
set(CMAKE_C_FLAGS_INIT "${shared_options}" CACHE INTERNAL "Initial options for C compiler.")
set(CMAKE_CXX_FLAGS_INIT "${shared_options}" CACHE INTERNAL "Initial options for C++ compiler.")
set(CMAKE_EXE_LINKER_FLAGS_INIT "-Wl,--gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map" CACHE INTERNAL "Initial options for executable linker.")