stm32_serDes_protobuf_project/bsp/lwip/lwip.c
2024-05-13 15:21:22 +03:00

250 lines
6.3 KiB
C

#include "lwip.h"
#include "lwip/init.h"
#include "lwip/netif.h"
#include "lwip/opt.h"
#include "lwip/mem.h"
#include "lwip/memp.h"
#include "lwip/udp.h"
#include "lwip/timeouts.h"
#include "netif/etharp.h"
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;
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)
{
// UDP Control Block structure
err_t err;
/* 1. Create a new UDP control block */
upcb = udp_new();
ip_addr_t local_ip;
u16_t local_port;
local_port = 1234;
/* 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
/* 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
{
udp_remove(upcb);
}
}
void udp_send_data(const char *data, u16_t len)
{
struct pbuf *p;
err_t err;
p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
if (p != NULL)
{
memcpy(p->payload, data, len);
err = udp_sendto(upcb, p, &remote_ip, remote_port);
pbuf_free(p);
if (err != ERR_OK)
{
// handle error
}
}
}
/* USER CODE END 2 */
/**
* LwIP initialization function
*/
void MX_LWIP_Init(void)
{
IP_ADDRESS[0] = 192;
IP_ADDRESS[1] = 168;
IP_ADDRESS[2] = 1;
IP_ADDRESS[3] = 66;
NETMASK_ADDRESS[0] = 255;
NETMASK_ADDRESS[1] = 255;
NETMASK_ADDRESS[2] = 0;
NETMASK_ADDRESS[3] = 0;
GATEWAY_ADDRESS[0] = 0;
GATEWAY_ADDRESS[1] = 0;
GATEWAY_ADDRESS[2] = 0;
GATEWAY_ADDRESS[3] = 0;
/* 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, &ethernetif_init, &ethernet_input);
// netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &ethernetif_input);
/* Registers the default network interface */
netif_set_default(&gnetif);
if (netif_is_link_up(&gnetif))
{
/* When the netif is fully configured this function must be called */
netif_set_up(&gnetif);
}
else
{
/* 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
* Up to user to complete or change this function ...
* Up to user to call this function in main.c in while (1) of main(void)
*-----------------------------------------------------------------------
* Read a received packet from the Ethernet buffers
* Send it to the lwIP stack for handling
* 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)
{
/* 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****/