Refactoring
This commit is contained in:
parent
708ac5f80a
commit
f34adc5973
160
app/main.c
160
app/main.c
@ -3,98 +3,6 @@
|
||||
#include <pb_decode.h>
|
||||
#include "simple.pb.h"
|
||||
|
||||
void MX_GPIO_Init(void)
|
||||
{
|
||||
RCC_AHB1PeriphClockCmd(
|
||||
RCC_AHB1Periph_GPIOA |
|
||||
RCC_AHB1Periph_GPIOB |
|
||||
RCC_AHB1Periph_GPIOC |
|
||||
RCC_AHB1Periph_GPIOD |
|
||||
RCC_AHB1Periph_GPIOG,
|
||||
ENABLE
|
||||
);
|
||||
|
||||
const uint16_t led_pins = GPIO_Pin_0 | GPIO_Pin_7 | GPIO_Pin_14;
|
||||
GPIO_InitTypeDef gpio;
|
||||
|
||||
GPIO_StructInit(&gpio);
|
||||
gpio.GPIO_Mode = GPIO_Mode_OUT;
|
||||
gpio.GPIO_Pin = led_pins;
|
||||
GPIO_Init(GPIOB, &gpio);
|
||||
GPIO_ResetBits(GPIOB, led_pins);
|
||||
}
|
||||
|
||||
void MX_USART2_UART_Init(void)
|
||||
{
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
||||
|
||||
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;
|
||||
gpio.GPIO_OType = GPIO_OType_PP;
|
||||
gpio.GPIO_PuPd = GPIO_PuPd_UP;
|
||||
GPIO_Init(GPIOD, &gpio);
|
||||
|
||||
GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2);
|
||||
GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2);
|
||||
|
||||
// NVIC_SetPriority(USART2_IRQn, 0);
|
||||
// NVIC_EnableIRQ(USART2_IRQn);
|
||||
|
||||
USART_InitTypeDef usart;
|
||||
usart.USART_BaudRate = 115200;
|
||||
usart.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
||||
|
||||
USART_Init(USART2, &usart);
|
||||
USART_Cmd(USART2, ENABLE);
|
||||
}
|
||||
|
||||
void SystemClock_Config();
|
||||
|
||||
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;
|
||||
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
|
||||
|
||||
NVIC_SetPriority(ETH_IRQn, 0);
|
||||
NVIC_EnableIRQ(ETH_IRQn);
|
||||
}
|
||||
|
||||
void serialzie() {
|
||||
/* This is the buffer where we will store our message. */
|
||||
uint8_t buffer[128];
|
||||
@ -103,13 +11,6 @@ void serialzie() {
|
||||
|
||||
/* Encode our message */
|
||||
{
|
||||
/* Allocate space on the stack to store the message data.
|
||||
*
|
||||
* Nanopb generates simple struct definitions for all the messages.
|
||||
* - check out the contents of simple.pb.h!
|
||||
* It is a good idea to always initialize your structures
|
||||
* so that you do not have garbage data from RAM in there.
|
||||
*/
|
||||
SimpleMessage message = SimpleMessage_init_zero;
|
||||
|
||||
/* Create a stream that will write to our buffer. */
|
||||
@ -127,67 +28,24 @@ void serialzie() {
|
||||
printf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Now we could transmit the message over network, store it in a file or
|
||||
* wrap it to a pigeon's leg.
|
||||
*/
|
||||
|
||||
/* But because we are lazy, we will just decode it immediately. */
|
||||
|
||||
// {
|
||||
// /* Allocate space for the decoded message. */
|
||||
// SimpleMessage message = SimpleMessage_init_zero;
|
||||
|
||||
// /* Create a stream that reads from the buffer. */
|
||||
// pb_istream_t stream = pb_istream_from_buffer(buffer, message_length);
|
||||
|
||||
// /* Now we are ready to decode the message. */
|
||||
// status = pb_decode(&stream, SimpleMessage_fields, &message);
|
||||
|
||||
// /* Check for errors... */
|
||||
// if (!status)
|
||||
// {
|
||||
// printf("Decoding failed: %s\n", PB_GET_ERROR(&stream));
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
// /* Print the data contained in the message. */
|
||||
// // printf("Project: %s\n", message.project);
|
||||
// // printf("Number: %d\n", message.number);
|
||||
// }
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
|
||||
SystemClock_Config();
|
||||
uint32_t tick = SystemCoreClock/1000 - 1;
|
||||
SysTick_Config(tick);
|
||||
MX_GPIO_Init();
|
||||
MX_USART2_UART_Init();
|
||||
eth_init();
|
||||
MX_LWIP_Init();
|
||||
|
||||
printf("Starting main loop..\r\n");
|
||||
printf("SysClock: %d\r\n:", SystemCoreClock);
|
||||
|
||||
printf("FLASH->ACR: %d\r\n", FLASH->ACR);
|
||||
printf("RCC->CFGR :%d\r\n", RCC->CFGR);
|
||||
printf("RCC->CR :%d\r\n", RCC->CR);
|
||||
printf("RCC->PLLCFGR :%d\r\n", RCC->PLLCFGR);
|
||||
printf("RCC->APB1ENR :%d\r\n", RCC->APB1ENR);
|
||||
boardInit();
|
||||
udpServer_init();
|
||||
// serialzie();
|
||||
uint32_t cnt = 0;
|
||||
char numStr[10];
|
||||
|
||||
while (1)
|
||||
{
|
||||
MX_LWIP_Process(); // poll for ethernet rx and timer operations.
|
||||
lwipProcess(); // poll for ethernet rx and timer operations.
|
||||
const char *data = "Hello, world!";
|
||||
udp_send_data(data, strlen(data));
|
||||
sprintf(numStr, "%d", cnt++);
|
||||
char* strCat = strcat(data, numStr);
|
||||
udp_send_data(numStr, strlen(numStr));
|
||||
// serialzie();
|
||||
GPIO_ToggleBits(GPIOB, GPIO_Pin_14);
|
||||
delay(500);
|
||||
|
110
bsp/bsp.c
110
bsp/bsp.c
@ -1,7 +1,25 @@
|
||||
#include "misc.h"
|
||||
#include "bsp.h"
|
||||
|
||||
void gpio_init()
|
||||
#define kHz_1 1000
|
||||
|
||||
static lwip_status_t lwip_status = {.link_status = LINK_DOWN};
|
||||
|
||||
lwip_status_t* getLwipStatus() {
|
||||
return &lwip_status;
|
||||
}
|
||||
|
||||
void sysInit() {
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
|
||||
FLASH_PrefetchBufferCmd(ENABLE);
|
||||
SystemCoreClock = 180000000;
|
||||
uint32_t tick = SystemCoreClock/kHz_1 - 1;
|
||||
SysTick_Config(tick);
|
||||
NVIC_EnableIRQ(SysTick_IRQn);
|
||||
}
|
||||
|
||||
void gpioInit()
|
||||
{
|
||||
RCC_AHB1PeriphClockCmd(
|
||||
RCC_AHB1Periph_GPIOA |
|
||||
@ -22,9 +40,9 @@ void gpio_init()
|
||||
GPIO_ResetBits(GPIOB, led_pins);
|
||||
}
|
||||
|
||||
|
||||
void usart_init()
|
||||
void uartInit(void)
|
||||
{
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
||||
@ -50,23 +68,77 @@ void usart_init()
|
||||
usart.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
||||
|
||||
USART_Init(USART2, &usart);
|
||||
USART_Cmd(USART2, ENABLE);
|
||||
USART_Cmd(USART2, ENABLE);
|
||||
}
|
||||
|
||||
void board_init() {
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
|
||||
SystemInit();
|
||||
SystemCoreClock = 180000000;
|
||||
gpio_init();
|
||||
usart_init();
|
||||
printf("Starting main loop..\r\n");
|
||||
printf("SysClock: %d\r\n:", SystemCoreClock);
|
||||
|
||||
printf("FLASH->ACR: %d\r\n", FLASH->ACR);
|
||||
printf("RCC->CFGR :%d\r\n", RCC->CFGR);
|
||||
printf("RCC->CR :%d\r\n", RCC->CR);
|
||||
printf("RCC->PLLCFGR :%d\r\n", RCC->PLLCFGR);
|
||||
printf("RCC->APB1ENR :%d\r\n", RCC->APB1ENR);
|
||||
MX_LWIP_Init();
|
||||
void tim_init() {
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);
|
||||
TIM_TimeBaseInitTypeDef tim7;
|
||||
|
||||
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(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 ethInit() {
|
||||
|
||||
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;
|
||||
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
|
||||
|
||||
NVIC_SetPriority(ETH_IRQn, 0);
|
||||
NVIC_EnableIRQ(ETH_IRQn);
|
||||
}
|
||||
|
||||
void boardInit() {
|
||||
sysInit();
|
||||
gpioInit();
|
||||
uartInit();
|
||||
ethInit();
|
||||
tim_init();
|
||||
printf("Starting main loop..\r\n");
|
||||
lwipInit();
|
||||
}
|
12
bsp/bsp.h
12
bsp/bsp.h
@ -10,6 +10,16 @@
|
||||
#include "stm32f4xx_flash.h"
|
||||
#include "lwip/lwip.h"
|
||||
|
||||
void board_init();
|
||||
typedef enum {
|
||||
LINK_DOWN,
|
||||
LINK_UP
|
||||
} status_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t link_status;
|
||||
} lwip_status_t;
|
||||
|
||||
void boardInit();
|
||||
lwip_status_t* getLwipStatus();
|
||||
|
||||
#endif
|
@ -57,22 +57,12 @@
|
||||
#include "lwip/ethip6.h"
|
||||
#include "ethernetif.h"
|
||||
#include <string.h>
|
||||
|
||||
/* Within 'USER CODE' section, code will be kept by default at each generation */
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdio.h>
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
|
||||
/* Network interface name */
|
||||
#define IFNAME0 's'
|
||||
#define IFNAME1 't'
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
#if defined ( __ICCARM__ ) /*!< IAR Compiler */
|
||||
#pragma data_alignment=4
|
||||
@ -94,10 +84,6 @@ __ALIGN_BEGIN uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE] __ALIGN_END; /* Ethe
|
||||
#endif
|
||||
__ALIGN_BEGIN uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE] __ALIGN_END; /* Ethernet Transmit Buffer */
|
||||
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Global Ethernet handle */
|
||||
ETH_HandleTypeDef heth;
|
||||
|
||||
@ -105,21 +91,6 @@ ETH_HandleTypeDef* getEthStruct() {
|
||||
return &heth;
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
|
||||
/* USER CODE END 3 */
|
||||
|
||||
/* Private functions ---------------------------------------------------------*/
|
||||
|
||||
// void HAL_ETH_MspInit(ETH_HandleTypeDef* ethHandle)
|
||||
// {
|
||||
|
||||
// }
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/*******************************************************************************
|
||||
LL Driver Interface ( LwIP stack --> ETH)
|
||||
*******************************************************************************/
|
||||
@ -135,8 +106,6 @@ static void low_level_init(struct netif *netif)
|
||||
uint32_t regvalue = 0;
|
||||
HAL_StatusTypeDef hal_eth_init_status;
|
||||
|
||||
/* Init ETH */
|
||||
|
||||
uint8_t MACAddr[6] ;
|
||||
heth.Instance = ETH;
|
||||
heth.Init.AutoNegotiation = ETH_AUTONEGOTIATION_ENABLE;
|
||||
@ -152,13 +121,11 @@ static void low_level_init(struct netif *netif)
|
||||
heth.Init.ChecksumMode = ETH_CHECKSUM_BY_HARDWARE;
|
||||
heth.Init.MediaInterface = ETH_MEDIA_INTERFACE_RMII;
|
||||
|
||||
/* USER CODE BEGIN MACADDRESS */
|
||||
printf("LAN8742A PHYAD: 0x0\r\r\n");
|
||||
printf("Setting MACaddr: %02x:%02x:%02x:%02x:%02x:%02x\r\r\n",
|
||||
MACAddr[0], MACAddr[1], MACAddr[2],
|
||||
MACAddr[3], MACAddr[4], MACAddr[5]);
|
||||
printf("LAN8742A interface is RMII\r\r\n");
|
||||
/* USER CODE END MACADDRESS */
|
||||
|
||||
hal_eth_init_status = HAL_ETH_Init(&heth);
|
||||
|
||||
@ -200,10 +167,7 @@ static void low_level_init(struct netif *netif)
|
||||
/* Enable MAC and DMA transmission and reception */
|
||||
HAL_ETH_Start(&heth);
|
||||
|
||||
/* USER CODE BEGIN PHY_PRE_CONFIG */
|
||||
printf("Starting Ethernet IRQ/DMA..\r\r\n");
|
||||
/* USER CODE END PHY_PRE_CONFIG */
|
||||
|
||||
printf("Starting Ethernet IRQ/DMA..\r\r\n");
|
||||
|
||||
/* Read Register Configuration */
|
||||
HAL_ETH_ReadPHYRegister(&heth, PHY_ISFR, ®value);
|
||||
@ -214,14 +178,9 @@ static void low_level_init(struct netif *netif)
|
||||
|
||||
/* Read Register Configuration */
|
||||
HAL_ETH_ReadPHYRegister(&heth, PHY_ISFR , ®value);
|
||||
/* USER CODE BEGIN PHY_POST_CONFIG */
|
||||
/* USER CODE END PHY_POST_CONFIG */
|
||||
|
||||
#endif /* LWIP_ARP || LWIP_ETHERNET */
|
||||
|
||||
/* USER CODE BEGIN LOW_LEVEL_INIT */
|
||||
|
||||
/* USER CODE END LOW_LEVEL_INIT */
|
||||
}
|
||||
|
||||
/**
|
||||
@ -336,7 +295,6 @@ static struct pbuf * low_level_input(struct netif *netif)
|
||||
uint32_t byteslefttocopy = 0;
|
||||
uint32_t i=0;
|
||||
|
||||
|
||||
/* get received frame */
|
||||
if (HAL_ETH_GetReceivedFrame(&heth) != HAL_OK)
|
||||
return NULL;
|
||||
@ -404,29 +362,31 @@ static struct pbuf * low_level_input(struct netif *netif)
|
||||
return p;
|
||||
}
|
||||
|
||||
void check_link_status(struct netif *netif) {
|
||||
void check_link_status(struct netif* netif) {
|
||||
uint32_t regvalue = 0;
|
||||
if (HAL_ETH_ReadPHYRegister(&heth, PHY_BSR, ®value) == HAL_OK)
|
||||
{
|
||||
if((regvalue & PHY_LINKED_STATUS)== (uint16_t)RESET)
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
if((regvalue & PHY_LINKED_STATUS)== (uint16_t)RESET)
|
||||
{
|
||||
// Link status = disconnected
|
||||
if (netif_is_link_up(netif))
|
||||
{
|
||||
netif_set_down(netif);
|
||||
getLwipStatus()->link_status = LINK_DOWN;
|
||||
netif_set_link_down(netif);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Link status = connected
|
||||
if (!netif_is_link_up(netif))
|
||||
{
|
||||
getLwipStatus()->link_status = LINK_UP;
|
||||
NVIC_SystemReset();
|
||||
}
|
||||
getLwipStatus()->link_status = LINK_UP;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function should be called when a packet is ready to be read
|
||||
* from the interface. It uses the function low_level_input() that
|
||||
@ -440,7 +400,7 @@ void ethernetif_input(struct netif *netif)
|
||||
{
|
||||
err_t err;
|
||||
struct pbuf *p;
|
||||
// HAL_GPIO_TogglePin(GPIOB, 7);
|
||||
|
||||
/* move received packet into a new pbuf */
|
||||
p = low_level_input(netif);
|
||||
check_link_status(netif);
|
||||
@ -469,11 +429,7 @@ static err_t low_level_output_arp_off(struct netif *netif, struct pbuf *q, const
|
||||
{
|
||||
err_t errval;
|
||||
errval = ERR_OK;
|
||||
|
||||
/* USER CODE BEGIN 5 */
|
||||
|
||||
/* USER CODE END 5 */
|
||||
|
||||
|
||||
return errval;
|
||||
|
||||
}
|
||||
@ -530,8 +486,6 @@ err_t ethernetif_init(struct netif *netif)
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 6 */
|
||||
|
||||
/**
|
||||
* @brief Returns the current time in milliseconds
|
||||
* when LWIP_TIMERS == 1 and NO_SYS == 1
|
||||
@ -554,12 +508,6 @@ u32_t sys_now(void)
|
||||
return getSysTick();
|
||||
}
|
||||
|
||||
/* USER CODE END 6 */
|
||||
|
||||
/* USER CODE BEGIN 7 */
|
||||
|
||||
/* USER CODE END 7 */
|
||||
|
||||
#if LWIP_NETIF_LINK_CALLBACK
|
||||
/**
|
||||
* @brief Link callback function, this function is called on change of link status
|
||||
@ -649,7 +597,6 @@ void ethernetif_update_config(struct netif *netif)
|
||||
ethernetif_notify_conn_changed(netif);
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 8 */
|
||||
/**
|
||||
* @brief This function notify user about link status changement.
|
||||
* @param netif: the network interface
|
||||
@ -662,11 +609,8 @@ __weak void ethernetif_notify_conn_changed(struct netif *netif)
|
||||
*/
|
||||
|
||||
}
|
||||
/* USER CODE END 8 */
|
||||
|
||||
#endif /* LWIP_NETIF_LINK_CALLBACK */
|
||||
|
||||
/* USER CODE BEGIN 9 */
|
||||
|
||||
/* USER CODE END 9 */
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
|
||||
|
@ -5,7 +5,7 @@
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/netif.h"
|
||||
#include "stm32f4xx_hal_eth.h"
|
||||
#include "bsp.h"
|
||||
#include "../bsp/bsp.h"
|
||||
|
||||
err_t ethernetif_init(struct netif *netif);
|
||||
|
||||
|
134
bsp/lwip/lwip.c
134
bsp/lwip/lwip.c
@ -10,18 +10,10 @@
|
||||
|
||||
void Error_Handler(void);
|
||||
|
||||
/* DHCP Variables initialization ---------------------------------------------*/
|
||||
uint32_t DHCPfineTimer = 0;
|
||||
uint32_t DHCPcoarseTimer = 0;
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
struct udp_pcb *upcb;
|
||||
ip_addr_t remote_ip;
|
||||
u16_t remote_port;
|
||||
|
||||
/* USER CODE END 1 */
|
||||
|
||||
/* Variables Initialization */
|
||||
struct netif gnetif;
|
||||
ip4_addr_t ipaddr;
|
||||
ip4_addr_t netmask;
|
||||
@ -29,7 +21,6 @@ ip4_addr_t gw;
|
||||
uint8_t IP_ADDRESS[4];
|
||||
uint8_t NETMASK_ADDRESS[4];
|
||||
uint8_t GATEWAY_ADDRESS[4];
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
void udpServer_init(void)
|
||||
{
|
||||
@ -44,15 +35,14 @@ void udpServer_init(void)
|
||||
/* 2. Bind the upcb to the local port */
|
||||
IP_ADDR4(&local_ip, 192, 168, 1, 66);
|
||||
|
||||
err = udp_bind(upcb, &local_ip, local_port); // 7 is the server UDP port
|
||||
|
||||
err = udp_bind(upcb, &local_ip, local_port);
|
||||
|
||||
/* 3. Set a receive callback for the upcb */
|
||||
if(err == ERR_OK)
|
||||
{
|
||||
IP4_ADDR(&remote_ip, 192, 168, 1, 111);
|
||||
remote_port = 5678;
|
||||
// udp_recv(udp_pcb, udp_receive_callback, NULL);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -73,16 +63,12 @@ void udp_send_data(const char *data, u16_t len)
|
||||
pbuf_free(p);
|
||||
if (err != ERR_OK)
|
||||
{
|
||||
// handle error
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/**
|
||||
* LwIP initialization function
|
||||
*/
|
||||
void MX_LWIP_Init(void)
|
||||
void lwipInit(void)
|
||||
{
|
||||
IP_ADDRESS[0] = 192;
|
||||
IP_ADDRESS[1] = 168;
|
||||
@ -100,22 +86,12 @@ void MX_LWIP_Init(void)
|
||||
/* Initilialize the LwIP stack without RTOS */
|
||||
lwip_init();
|
||||
|
||||
/* IP addresses initialization with DHCP (IPv4) */
|
||||
// ipaddr.addr = 0;
|
||||
// netmask.addr = 0;
|
||||
// gw.addr = 0;
|
||||
|
||||
IP4_ADDR(&ipaddr, IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2], IP_ADDRESS[3]);
|
||||
IP4_ADDR(&netmask, NETMASK_ADDRESS[0], NETMASK_ADDRESS[1] , NETMASK_ADDRESS[2], NETMASK_ADDRESS[3]);
|
||||
IP4_ADDR(&gw, GATEWAY_ADDRESS[0], GATEWAY_ADDRESS[1], GATEWAY_ADDRESS[2], GATEWAY_ADDRESS[3]);
|
||||
|
||||
// netif_set_ipaddr(&gnetif, &ipaddr);
|
||||
// netif_set_netmask(&gnetif, &netmask);
|
||||
// netif_set_gw(&gnetif, &gw);
|
||||
|
||||
|
||||
/* add the network interface (IPv4/IPv6) without RTOS */
|
||||
netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);
|
||||
// netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernetif_input);
|
||||
|
||||
/* Registers the default network interface */
|
||||
netif_set_default(&gnetif);
|
||||
@ -130,22 +106,8 @@ void MX_LWIP_Init(void)
|
||||
/* When the netif link is down this function must be called */
|
||||
netif_set_down(&gnetif);
|
||||
}
|
||||
|
||||
/* Start DHCP negotiation for a network interface (IPv4) */
|
||||
// dhcp_start(&gnetif);
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
|
||||
/* USER CODE END 3 */
|
||||
}
|
||||
|
||||
#ifdef USE_OBSOLETE_USER_CODE_SECTION_4
|
||||
/* Kept to help code migration. (See new 4_1, 4_2... sections) */
|
||||
/* Avoid to use this user section which will become obsolete. */
|
||||
/* USER CODE BEGIN 4 */
|
||||
/* USER CODE END 4 */
|
||||
#endif
|
||||
|
||||
/**
|
||||
* ----------------------------------------------------------------------
|
||||
* Function given to help user to continue LwIP Initialization
|
||||
@ -157,93 +119,9 @@ void MX_LWIP_Init(void)
|
||||
* Handle timeouts if LWIP_TIMERS is set and without RTOS
|
||||
* Handle the llink status if LWIP_NETIF_LINK_CALLBACK is set and without RTOS
|
||||
*/
|
||||
void MX_LWIP_Process(void)
|
||||
void lwipProcess(void)
|
||||
{
|
||||
/* USER CODE BEGIN 4_1 */
|
||||
/* USER CODE END 4_1 */
|
||||
ethernetif_input(&gnetif);
|
||||
|
||||
/* USER CODE BEGIN 4_2 */
|
||||
/* USER CODE END 4_2 */
|
||||
/* Handle timeouts */
|
||||
sys_check_timeouts();
|
||||
|
||||
/* USER CODE BEGIN 4_3 */
|
||||
|
||||
/* USER CODE END 4_3 */
|
||||
}
|
||||
|
||||
#if defined ( __CC_ARM ) /* MDK ARM Compiler */
|
||||
/**
|
||||
* Opens a serial device for communication.
|
||||
*
|
||||
* @param devnum device number
|
||||
* @return handle to serial device if successful, NULL otherwise
|
||||
*/
|
||||
sio_fd_t sio_open(u8_t devnum)
|
||||
{
|
||||
sio_fd_t sd;
|
||||
|
||||
/* USER CODE BEGIN 7 */
|
||||
sd = 0; // dummy code
|
||||
/* USER CODE END 7 */
|
||||
|
||||
return sd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a single character to the serial device.
|
||||
*
|
||||
* @param c character to send
|
||||
* @param fd serial device handle
|
||||
*
|
||||
* @note This function will block until the character can be sent.
|
||||
*/
|
||||
void sio_send(u8_t c, sio_fd_t fd)
|
||||
{
|
||||
/* USER CODE BEGIN 8 */
|
||||
/* USER CODE END 8 */
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads from the serial device.
|
||||
*
|
||||
* @param fd serial device handle
|
||||
* @param data pointer to data buffer for receiving
|
||||
* @param len maximum length (in bytes) of data to receive
|
||||
* @return number of bytes actually received - may be 0 if aborted by sio_read_abort
|
||||
*
|
||||
* @note This function will block until data can be received. The blocking
|
||||
* can be cancelled by calling sio_read_abort().
|
||||
*/
|
||||
u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len)
|
||||
{
|
||||
u32_t recved_bytes;
|
||||
|
||||
/* USER CODE BEGIN 9 */
|
||||
recved_bytes = 0; // dummy code
|
||||
/* USER CODE END 9 */
|
||||
return recved_bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to read from the serial device. Same as sio_read but returns
|
||||
* immediately if no data is available and never blocks.
|
||||
*
|
||||
* @param fd serial device handle
|
||||
* @param data pointer to data buffer for receiving
|
||||
* @param len maximum length (in bytes) of data to receive
|
||||
* @return number of bytes actually received
|
||||
*/
|
||||
u32_t sio_tryread(sio_fd_t fd, u8_t *data, u32_t len)
|
||||
{
|
||||
u32_t recved_bytes;
|
||||
|
||||
/* USER CODE BEGIN 10 */
|
||||
recved_bytes = 0; // dummy code
|
||||
/* USER CODE END 10 */
|
||||
return recved_bytes;
|
||||
}
|
||||
#endif /* MDK ARM Compiler */
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
void udpServer_init(void);
|
||||
void udp_send_data(const char *data, u16_t len);
|
||||
void MX_LWIP_Init(void);
|
||||
void MX_LWIP_Process(void);
|
||||
void lwipInit(void);
|
||||
void lwipProcess(void);
|
||||
|
||||
#endif /*__ mx_lwip_H */
|
||||
#endif
|
||||
|
@ -1,8 +1,6 @@
|
||||
#include "stm32f4xx_it.h"
|
||||
#include "lwip/ethernetif.h"
|
||||
|
||||
// extern ETH_HandleTypeDef heth;
|
||||
|
||||
static volatile uint32_t sys_tick = 0;
|
||||
|
||||
void SysTick_Handler(void)
|
||||
@ -25,6 +23,18 @@ void delay(uint32_t delay) {
|
||||
while((getSysTick() - tickStart) < wait) {}
|
||||
}
|
||||
|
||||
void TIM7_IRQHandler()
|
||||
{
|
||||
TIM_ClearITPendingBit(TIM7, TIM_IT_Update);
|
||||
|
||||
if(getLwipStatus()->link_status == LINK_UP) {
|
||||
GPIO_ToggleBits(GPIOB, GPIO_Pin_7);
|
||||
}
|
||||
else {
|
||||
GPIO_ResetBits(GPIOB, GPIO_Pin_7);
|
||||
}
|
||||
}
|
||||
|
||||
void USART2_IRQHandler(void)
|
||||
{
|
||||
|
||||
|
@ -1,21 +1,10 @@
|
||||
#ifndef __STM32F4xx_IT_H
|
||||
#define __STM32F4xx_IT_H
|
||||
#include "stdint.h"
|
||||
#include "bsp.h"
|
||||
|
||||
void NMI_Handler(void);
|
||||
void HardFault_Handler(void);
|
||||
void MemManage_Handler(void);
|
||||
void BusFault_Handler(void);
|
||||
void UsageFault_Handler(void);
|
||||
void SVC_Handler(void);
|
||||
void DebugMon_Handler(void);
|
||||
void PendSV_Handler(void);
|
||||
void SysTick_Handler(void);
|
||||
void USART2_IRQHandler(void);
|
||||
void ETH_IRQHandler(void);
|
||||
uint32_t getSysTick();
|
||||
void delay(uint32_t);
|
||||
|
||||
|
||||
#endif /* __STM32F4xx_IT_H */
|
||||
#endif
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user