From 3a5ea8ec9b6a54e23a7cf96520e89010f7682581 Mon Sep 17 00:00:00 2001 From: Vasily Markov Date: Fri, 3 May 2024 12:10:38 +0300 Subject: [PATCH] get link status --- app/app.c | 1 - bsp/bsp.c | 79 +++++++++++++++++++++++++------------------ bsp/lwip/ethernetif.c | 9 ++--- bsp/lwip/lwip.c | 4 +-- 4 files changed, 51 insertions(+), 42 deletions(-) diff --git a/app/app.c b/app/app.c index 184a9f3..97034e4 100644 --- a/app/app.c +++ b/app/app.c @@ -10,7 +10,6 @@ void app() { HAL_ETH_ReadPHYRegister(getEthStruct(), 1, ®value); delay(500); printf("Test: %d\r\n", regvalue); - // process_LWIP(); }; } diff --git a/bsp/bsp.c b/bsp/bsp.c index 94fce70..1bb2a81 100644 --- a/bsp/bsp.c +++ b/bsp/bsp.c @@ -43,7 +43,6 @@ void USART2_IRQHandler() void gpio_init() { const uint16_t led_pins = GPIO_Pin_0 | GPIO_Pin_7 | GPIO_Pin_14; - const uint16_t usart_pins = GPIO_Pin_5 | GPIO_Pin_6; RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA | @@ -56,15 +55,19 @@ void gpio_init() { GPIO_InitTypeDef gpio; -/***************LED_CONF********************/ GPIO_StructInit(&gpio); gpio.GPIO_Mode = GPIO_Mode_OUT; gpio.GPIO_Pin = led_pins; GPIO_Init(GPIOB, &gpio); GPIO_ResetBits(GPIOB, led_pins); -/*******************************************/ -/**************UART_CONF********************/ +} + +void usart_init() { + + GPIO_InitTypeDef gpio; + + const uint16_t usart_pins = GPIO_Pin_5 | GPIO_Pin_6; gpio.GPIO_Mode = GPIO_Mode_AF; gpio.GPIO_Pin = usart_pins; gpio.GPIO_Speed = GPIO_Speed_50MHz; @@ -74,34 +77,7 @@ void gpio_init() { GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2); GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2); -/*******************************************/ -/***************ETH_CONF********************/ - gpio.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_7; - GPIO_Init(GPIOA, &gpio); - GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_ETH); //RMII ref clock - GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_ETH); //RMII MDIO - GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_ETH); //RMII RX Data Valid - - gpio.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5; - GPIO_Init(GPIOC, &gpio); - GPIO_PinAFConfig(GPIOC, GPIO_PinSource1, GPIO_AF_ETH); //RMII MDC - GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_ETH); //RMII RXD0 - GPIO_PinAFConfig(GPIOC, GPIO_PinSource5, GPIO_AF_ETH); //RMII RXD1 - - gpio.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_13; - GPIO_Init(GPIOG, &gpio); - GPIO_PinAFConfig(GPIOG, GPIO_PinSource11, GPIO_AF_ETH); //RMII TX enable - GPIO_PinAFConfig(GPIOG, GPIO_PinSource13, GPIO_AF_ETH); //RMII TXD0 - - gpio.GPIO_Pin = GPIO_Pin_13; - GPIO_Init(GPIOB, &gpio); - GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_ETH); //RMII TXD1 -/*******************************************/ - -} - -void usart_init() { RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); USART_InitTypeDef usart; usart.USART_BaudRate = 115200; @@ -128,6 +104,44 @@ void dma_init() { DMA_Init(DMA2_Stream0, &dma); } + +void eth_init() { + GPIO_InitTypeDef gpio; + + gpio.GPIO_Mode = GPIO_Mode_AF; + gpio.GPIO_Speed = GPIO_Speed_100MHz; + gpio.GPIO_OType = GPIO_OType_PP; + gpio.GPIO_PuPd = GPIO_PuPd_NOPULL; + + gpio.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_7; + GPIO_Init(GPIOA, &gpio); + GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_ETH); //RMII ref clock + GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_ETH); //RMII MDIO + GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_ETH); //RMII RX Data Valid + + gpio.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5; + GPIO_Init(GPIOC, &gpio); + GPIO_PinAFConfig(GPIOC, GPIO_PinSource1, GPIO_AF_ETH); //RMII MDC + GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_ETH); //RMII RXD0 + GPIO_PinAFConfig(GPIOC, GPIO_PinSource5, GPIO_AF_ETH); //RMII RXD1 + + gpio.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_13; + GPIO_Init(GPIOG, &gpio); + GPIO_PinAFConfig(GPIOG, GPIO_PinSource11, GPIO_AF_ETH); //RMII TX enable + GPIO_PinAFConfig(GPIOG, GPIO_PinSource13, GPIO_AF_ETH); //RMII TXD0 + + gpio.GPIO_Pin = GPIO_Pin_13; + 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; SysTick_Config(tick); @@ -135,7 +149,8 @@ void board_init() { __enable_irq(); gpio_init(); usart_init(); - dma_init(); + // dma_init(); + eth_init(); init_LWIP(); delay(50); //wait until periph init printf("Controller is started...\r\n"); diff --git a/bsp/lwip/ethernetif.c b/bsp/lwip/ethernetif.c index b008a6e..8533440 100644 --- a/bsp/lwip/ethernetif.c +++ b/bsp/lwip/ethernetif.c @@ -59,7 +59,6 @@ #include #include "bsp.h" #include - /* Private define ------------------------------------------------------------*/ /* Network interface name */ @@ -95,6 +94,7 @@ ETH_HandleTypeDef heth; ETH_HandleTypeDef* getEthStruct() { return &heth; } + /******************************************************************************* LL Driver Interface ( LwIP stack --> ETH) *******************************************************************************/ @@ -111,7 +111,7 @@ static void low_level_init(struct netif *netif) HAL_StatusTypeDef hal_eth_init_status; - uint8_t MACAddr[6] ; + uint8_t MACAddr[6] ; heth.Instance = ETH; heth.Init.AutoNegotiation = ETH_AUTONEGOTIATION_ENABLE; heth.Init.PhyAddress = LAN8742A_PHY_ADDRESS; @@ -133,7 +133,7 @@ static void low_level_init(struct netif *netif) printf("LAN8742A interface is RMII\r\r\n"); hal_eth_init_status = HAL_ETH_Init(&heth); - + printf("Status: %d\r\n", hal_eth_init_status); if (hal_eth_init_status == HAL_OK) { /* Set netif link flag */ @@ -172,11 +172,8 @@ static void low_level_init(struct netif *netif) /* Enable MAC and DMA transmission and reception */ HAL_ETH_Start(&heth); - printf("Starting Ethernet IRQ/DMA..\r\r\n"); - - /* Read Register Configuration */ HAL_ETH_ReadPHYRegister(&heth, PHY_ISFR, ®value); regvalue |= (PHY_ISFR_INT4); diff --git a/bsp/lwip/lwip.c b/bsp/lwip/lwip.c index 726d4d2..57205ac 100644 --- a/bsp/lwip/lwip.c +++ b/bsp/lwip/lwip.c @@ -25,7 +25,7 @@ void init_LWIP(void) { IP_ADDRESS[0] = 192; IP_ADDRESS[1] = 168; - IP_ADDRESS[2] = 0; + IP_ADDRESS[2] = 1; IP_ADDRESS[3] = 66; NETMASK_ADDRESS[0] = 255; NETMASK_ADDRESS[1] = 255; @@ -64,9 +64,7 @@ void init_LWIP(void) void process_LWIP() { - ethernetif_input(&gnetif); sys_check_timeouts(); - }