Refactoring

This commit is contained in:
Vasily Markov 2024-05-15 02:15:03 +03:00
parent 708ac5f80a
commit f34adc5973
9 changed files with 159 additions and 398 deletions

View File

@ -3,98 +3,6 @@
#include <pb_decode.h> #include <pb_decode.h>
#include "simple.pb.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() { void serialzie() {
/* This is the buffer where we will store our message. */ /* This is the buffer where we will store our message. */
uint8_t buffer[128]; uint8_t buffer[128];
@ -103,13 +11,6 @@ void serialzie() {
/* Encode our message */ /* 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; SimpleMessage message = SimpleMessage_init_zero;
/* Create a stream that will write to our buffer. */ /* Create a stream that will write to our buffer. */
@ -128,66 +29,23 @@ void serialzie() {
return 1; 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; return 0;
} }
int main(void) int main(void)
{ {
boardInit();
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);
udpServer_init(); udpServer_init();
// serialzie(); uint32_t cnt = 0;
char numStr[10];
while (1) 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!"; 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(); // serialzie();
GPIO_ToggleBits(GPIOB, GPIO_Pin_14); GPIO_ToggleBits(GPIOB, GPIO_Pin_14);
delay(500); delay(500);

108
bsp/bsp.c
View File

@ -1,7 +1,25 @@
#include "misc.h" #include "misc.h"
#include "bsp.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_AHB1PeriphClockCmd(
RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOA |
@ -22,9 +40,9 @@ void gpio_init()
GPIO_ResetBits(GPIOB, led_pins); GPIO_ResetBits(GPIOB, led_pins);
} }
void uartInit(void)
void usart_init()
{ {
GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitTypeDef GPIO_InitStruct = {0};
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
@ -53,20 +71,74 @@ void usart_init()
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); void tim_init() {
printf("RCC->CFGR :%d\r\n", RCC->CFGR); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);
printf("RCC->CR :%d\r\n", RCC->CR); TIM_TimeBaseInitTypeDef tim7;
printf("RCC->PLLCFGR :%d\r\n", RCC->PLLCFGR);
printf("RCC->APB1ENR :%d\r\n", RCC->APB1ENR); TIM_TimeBaseStructInit(&tim7);
MX_LWIP_Init();
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();
} }

View File

@ -10,6 +10,16 @@
#include "stm32f4xx_flash.h" #include "stm32f4xx_flash.h"
#include "lwip/lwip.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 #endif

View File

@ -57,22 +57,12 @@
#include "lwip/ethip6.h" #include "lwip/ethip6.h"
#include "ethernetif.h" #include "ethernetif.h"
#include <string.h> #include <string.h>
/* Within 'USER CODE' section, code will be kept by default at each generation */
/* USER CODE BEGIN 0 */
#include <stdio.h> #include <stdio.h>
/* USER CODE END 0 */
/* Private define ------------------------------------------------------------*/
/* Network interface name */ /* Network interface name */
#define IFNAME0 's' #define IFNAME0 's'
#define IFNAME1 't' #define IFNAME1 't'
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* Private variables ---------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/
#if defined ( __ICCARM__ ) /*!< IAR Compiler */ #if defined ( __ICCARM__ ) /*!< IAR Compiler */
#pragma data_alignment=4 #pragma data_alignment=4
@ -94,10 +84,6 @@ __ALIGN_BEGIN uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE] __ALIGN_END; /* Ethe
#endif #endif
__ALIGN_BEGIN uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE] __ALIGN_END; /* Ethernet Transmit Buffer */ __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 */ /* Global Ethernet handle */
ETH_HandleTypeDef heth; ETH_HandleTypeDef heth;
@ -105,21 +91,6 @@ ETH_HandleTypeDef* getEthStruct() {
return &heth; 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) LL Driver Interface ( LwIP stack --> ETH)
*******************************************************************************/ *******************************************************************************/
@ -135,8 +106,6 @@ static void low_level_init(struct netif *netif)
uint32_t regvalue = 0; uint32_t regvalue = 0;
HAL_StatusTypeDef hal_eth_init_status; HAL_StatusTypeDef hal_eth_init_status;
/* Init ETH */
uint8_t MACAddr[6] ; uint8_t MACAddr[6] ;
heth.Instance = ETH; heth.Instance = ETH;
heth.Init.AutoNegotiation = ETH_AUTONEGOTIATION_ENABLE; 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.ChecksumMode = ETH_CHECKSUM_BY_HARDWARE;
heth.Init.MediaInterface = ETH_MEDIA_INTERFACE_RMII; heth.Init.MediaInterface = ETH_MEDIA_INTERFACE_RMII;
/* USER CODE BEGIN MACADDRESS */
printf("LAN8742A PHYAD: 0x0\r\r\n"); printf("LAN8742A PHYAD: 0x0\r\r\n");
printf("Setting MACaddr: %02x:%02x:%02x:%02x:%02x:%02x\r\r\n", printf("Setting MACaddr: %02x:%02x:%02x:%02x:%02x:%02x\r\r\n",
MACAddr[0], MACAddr[1], MACAddr[2], MACAddr[0], MACAddr[1], MACAddr[2],
MACAddr[3], MACAddr[4], MACAddr[5]); MACAddr[3], MACAddr[4], MACAddr[5]);
printf("LAN8742A interface is RMII\r\r\n"); printf("LAN8742A interface is RMII\r\r\n");
/* USER CODE END MACADDRESS */
hal_eth_init_status = HAL_ETH_Init(&heth); 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 */ /* Enable MAC and DMA transmission and reception */
HAL_ETH_Start(&heth); HAL_ETH_Start(&heth);
/* USER CODE BEGIN PHY_PRE_CONFIG */
printf("Starting Ethernet IRQ/DMA..\r\r\n"); printf("Starting Ethernet IRQ/DMA..\r\r\n");
/* USER CODE END PHY_PRE_CONFIG */
/* Read Register Configuration */ /* Read Register Configuration */
HAL_ETH_ReadPHYRegister(&heth, PHY_ISFR, &regvalue); HAL_ETH_ReadPHYRegister(&heth, PHY_ISFR, &regvalue);
@ -214,14 +178,9 @@ static void low_level_init(struct netif *netif)
/* Read Register Configuration */ /* Read Register Configuration */
HAL_ETH_ReadPHYRegister(&heth, PHY_ISFR , &regvalue); HAL_ETH_ReadPHYRegister(&heth, PHY_ISFR , &regvalue);
/* USER CODE BEGIN PHY_POST_CONFIG */
/* USER CODE END PHY_POST_CONFIG */
#endif /* LWIP_ARP || LWIP_ETHERNET */ #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 byteslefttocopy = 0;
uint32_t i=0; uint32_t i=0;
/* get received frame */ /* get received frame */
if (HAL_ETH_GetReceivedFrame(&heth) != HAL_OK) if (HAL_ETH_GetReceivedFrame(&heth) != HAL_OK)
return NULL; return NULL;
@ -404,29 +362,31 @@ static struct pbuf * low_level_input(struct netif *netif)
return p; return p;
} }
void check_link_status(struct netif *netif) { void check_link_status(struct netif* netif) {
uint32_t regvalue = 0; uint32_t regvalue = 0;
if (HAL_ETH_ReadPHYRegister(&heth, PHY_BSR, &regvalue) == HAL_OK) if (HAL_ETH_ReadPHYRegister(&heth, PHY_BSR, &regvalue) == HAL_OK)
{ {
if((regvalue & PHY_LINKED_STATUS)== (uint16_t)RESET) if((regvalue & PHY_LINKED_STATUS)== (uint16_t)RESET)
{ {
// Link status = disconnected
if (netif_is_link_up(netif)) if (netif_is_link_up(netif))
{ {
// netif_set_down(netif); netif_set_down(netif);
printf("unplugged\r\n"); getLwipStatus()->link_status = LINK_DOWN;
// netif_set_link_down(netif); netif_set_link_down(netif);
} }
} else { }
else {
// Link status = connected // Link status = connected
if (!netif_is_link_up(netif)) if (!netif_is_link_up(netif))
{ {
printf("plugged\r\n"); getLwipStatus()->link_status = LINK_UP;
// NVIC_SystemReset(); NVIC_SystemReset();
} }
getLwipStatus()->link_status = LINK_UP;
} }
} }
} }
/** /**
* This function should be called when a packet is ready to be read * This function should be called when a packet is ready to be read
* from the interface. It uses the function low_level_input() that * from the interface. It uses the function low_level_input() that
@ -440,7 +400,7 @@ void ethernetif_input(struct netif *netif)
{ {
err_t err; err_t err;
struct pbuf *p; struct pbuf *p;
// HAL_GPIO_TogglePin(GPIOB, 7);
/* move received packet into a new pbuf */ /* move received packet into a new pbuf */
p = low_level_input(netif); p = low_level_input(netif);
check_link_status(netif); check_link_status(netif);
@ -470,10 +430,6 @@ static err_t low_level_output_arp_off(struct netif *netif, struct pbuf *q, const
err_t errval; err_t errval;
errval = ERR_OK; errval = ERR_OK;
/* USER CODE BEGIN 5 */
/* USER CODE END 5 */
return errval; return errval;
} }
@ -530,8 +486,6 @@ err_t ethernetif_init(struct netif *netif)
return ERR_OK; return ERR_OK;
} }
/* USER CODE BEGIN 6 */
/** /**
* @brief Returns the current time in milliseconds * @brief Returns the current time in milliseconds
* when LWIP_TIMERS == 1 and NO_SYS == 1 * when LWIP_TIMERS == 1 and NO_SYS == 1
@ -554,12 +508,6 @@ u32_t sys_now(void)
return getSysTick(); return getSysTick();
} }
/* USER CODE END 6 */
/* USER CODE BEGIN 7 */
/* USER CODE END 7 */
#if LWIP_NETIF_LINK_CALLBACK #if LWIP_NETIF_LINK_CALLBACK
/** /**
* @brief Link callback function, this function is called on change of link status * @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); ethernetif_notify_conn_changed(netif);
} }
/* USER CODE BEGIN 8 */
/** /**
* @brief This function notify user about link status changement. * @brief This function notify user about link status changement.
* @param netif: the network interface * @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 */ #endif /* LWIP_NETIF_LINK_CALLBACK */
/* USER CODE BEGIN 9 */
/* USER CODE END 9 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View File

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

View File

@ -10,18 +10,10 @@
void Error_Handler(void); void Error_Handler(void);
/* DHCP Variables initialization ---------------------------------------------*/
uint32_t DHCPfineTimer = 0;
uint32_t DHCPcoarseTimer = 0;
/* USER CODE BEGIN 1 */
struct udp_pcb *upcb; struct udp_pcb *upcb;
ip_addr_t remote_ip; ip_addr_t remote_ip;
u16_t remote_port; u16_t remote_port;
/* USER CODE END 1 */
/* Variables Initialization */
struct netif gnetif; struct netif gnetif;
ip4_addr_t ipaddr; ip4_addr_t ipaddr;
ip4_addr_t netmask; ip4_addr_t netmask;
@ -29,7 +21,6 @@ ip4_addr_t gw;
uint8_t IP_ADDRESS[4]; uint8_t IP_ADDRESS[4];
uint8_t NETMASK_ADDRESS[4]; uint8_t NETMASK_ADDRESS[4];
uint8_t GATEWAY_ADDRESS[4]; uint8_t GATEWAY_ADDRESS[4];
/* USER CODE BEGIN 2 */
void udpServer_init(void) void udpServer_init(void)
{ {
@ -44,15 +35,14 @@ void udpServer_init(void)
/* 2. Bind the upcb to the local port */ /* 2. Bind the upcb to the local port */
IP_ADDR4(&local_ip, 192, 168, 1, 66); 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 */ /* 3. Set a receive callback for the upcb */
if(err == ERR_OK) if(err == ERR_OK)
{ {
IP4_ADDR(&remote_ip, 192, 168, 1, 111); IP4_ADDR(&remote_ip, 192, 168, 1, 111);
remote_port = 5678; remote_port = 5678;
// udp_recv(udp_pcb, udp_receive_callback, NULL);
} }
else else
{ {
@ -73,16 +63,12 @@ void udp_send_data(const char *data, u16_t len)
pbuf_free(p); pbuf_free(p);
if (err != ERR_OK) if (err != ERR_OK)
{ {
// handle error
} }
} }
} }
/* USER CODE END 2 */
/** void lwipInit(void)
* LwIP initialization function
*/
void MX_LWIP_Init(void)
{ {
IP_ADDRESS[0] = 192; IP_ADDRESS[0] = 192;
IP_ADDRESS[1] = 168; IP_ADDRESS[1] = 168;
@ -100,22 +86,12 @@ void MX_LWIP_Init(void)
/* Initilialize the LwIP stack without RTOS */ /* Initilialize the LwIP stack without RTOS */
lwip_init(); 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(&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(&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]); 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 */ /* add the network interface (IPv4/IPv6) without RTOS */
netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &ethernet_input); netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &ethernet_input);
// netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &ethernetif_input);
/* Registers the default network interface */ /* Registers the default network interface */
netif_set_default(&gnetif); netif_set_default(&gnetif);
@ -130,22 +106,8 @@ void MX_LWIP_Init(void)
/* When the netif link is down this function must be called */ /* When the netif link is down this function must be called */
netif_set_down(&gnetif); 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 * 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 timeouts if LWIP_TIMERS is set and without RTOS
* Handle the llink status if LWIP_NETIF_LINK_CALLBACK 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); ethernetif_input(&gnetif);
/* USER CODE BEGIN 4_2 */
/* USER CODE END 4_2 */
/* Handle timeouts */
sys_check_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****/

View File

@ -5,7 +5,7 @@
void udpServer_init(void); void udpServer_init(void);
void udp_send_data(const char *data, u16_t len); void udp_send_data(const char *data, u16_t len);
void MX_LWIP_Init(void); void lwipInit(void);
void MX_LWIP_Process(void); void lwipProcess(void);
#endif /*__ mx_lwip_H */ #endif

View File

@ -1,8 +1,6 @@
#include "stm32f4xx_it.h" #include "stm32f4xx_it.h"
#include "lwip/ethernetif.h" #include "lwip/ethernetif.h"
// extern ETH_HandleTypeDef heth;
static volatile uint32_t sys_tick = 0; static volatile uint32_t sys_tick = 0;
void SysTick_Handler(void) void SysTick_Handler(void)
@ -25,6 +23,18 @@ void delay(uint32_t delay) {
while((getSysTick() - tickStart) < wait) {} 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) void USART2_IRQHandler(void)
{ {

View File

@ -1,21 +1,10 @@
#ifndef __STM32F4xx_IT_H #ifndef __STM32F4xx_IT_H
#define __STM32F4xx_IT_H #define __STM32F4xx_IT_H
#include "stdint.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(); uint32_t getSysTick();
void delay(uint32_t); void delay(uint32_t);
#endif
#endif /* __STM32F4xx_IT_H */