From 93b305123f6bfa9ce3b429e6d1408840264881fb Mon Sep 17 00:00:00 2001 From: Vasily Markov Date: Sun, 5 May 2024 19:31:36 +0300 Subject: [PATCH] Link status checking --- app/app.c | 1 - bsp/CMakeLists.txt | 1 + bsp/bsp.c | 69 ++++++++++++----------------------- bsp/bsp.h | 13 ++++++- bsp/lwip/ethernetif.c | 31 ++++++++-------- bsp/lwip/ethernetif.h | 1 + bsp/lwip/lwip.h | 1 - bsp/stm32f4xx_it.c | 35 ++++++++++++++++++ bsp/stm32f4xx_it.h | 7 ++++ toolchain-arm-none-eabi.cmake | 2 +- 10 files changed, 96 insertions(+), 65 deletions(-) create mode 100644 bsp/stm32f4xx_it.c create mode 100644 bsp/stm32f4xx_it.h diff --git a/app/app.c b/app/app.c index cf80452..bea9fb4 100644 --- a/app/app.c +++ b/app/app.c @@ -6,7 +6,6 @@ void app() { while(1) { process_LWIP(); - // GPIO_ToggleBits(GPIOB, GPIO_Pin_7); delay(400); }; } diff --git a/bsp/CMakeLists.txt b/bsp/CMakeLists.txt index 955c325..18545b6 100644 --- a/bsp/CMakeLists.txt +++ b/bsp/CMakeLists.txt @@ -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 diff --git a/bsp/bsp.c b/bsp/bsp.c index a5b66f9..b977fc8 100644 --- a/bsp/bsp.c +++ b/bsp/bsp.c @@ -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); + + tim7.TIM_Prescaler = 9000-1; + tim7.TIM_CounterMode = TIM_CounterMode_Up; + tim7.TIM_Period = 2000; + tim7.TIM_ClockDivision = TIM_CKD_DIV1; - TIM_TimeBaseInit(TIM2, &tim2); - - 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"); } diff --git a/bsp/bsp.h b/bsp/bsp.h index 17fabff..7e1d1fb 100644 --- a/bsp/bsp.h +++ b/bsp/bsp.h @@ -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 diff --git a/bsp/lwip/ethernetif.c b/bsp/lwip/ethernetif.c index 4c62ec1..6c064f5 100644 --- a/bsp/lwip/ethernetif.c +++ b/bsp/lwip/ethernetif.c @@ -57,7 +57,6 @@ #include "lwip/ethip6.h" #include "ethernetif.h" #include -#include "bsp.h" #include /* 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; } } } diff --git a/bsp/lwip/ethernetif.h b/bsp/lwip/ethernetif.h index 46d35c4..3f54f85 100644 --- a/bsp/lwip/ethernetif.h +++ b/bsp/lwip/ethernetif.h @@ -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); diff --git a/bsp/lwip/lwip.h b/bsp/lwip/lwip.h index b01cae1..d4c8021 100644 --- a/bsp/lwip/lwip.h +++ b/bsp/lwip/lwip.h @@ -2,7 +2,6 @@ #define LWIP_H void init_LWIP(); - void process_LWIP(); #endif /* LWIP_H */ diff --git a/bsp/stm32f4xx_it.c b/bsp/stm32f4xx_it.c new file mode 100644 index 0000000..f6ced44 --- /dev/null +++ b/bsp/stm32f4xx_it.c @@ -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; +} \ No newline at end of file diff --git a/bsp/stm32f4xx_it.h b/bsp/stm32f4xx_it.h new file mode 100644 index 0000000..6603015 --- /dev/null +++ b/bsp/stm32f4xx_it.h @@ -0,0 +1,7 @@ +#ifndef STM32F4XX_H +#define STM32F4XX_H +#include "bsp.h" + +uint32_t getSysTick(); + +#endif \ No newline at end of file diff --git a/toolchain-arm-none-eabi.cmake b/toolchain-arm-none-eabi.cmake index f1c925b..c16d2c0 100644 --- a/toolchain-arm-none-eabi.cmake +++ b/toolchain-arm-none-eabi.cmake @@ -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.")