Fix udp receive

This commit is contained in:
Vasily Markov 2024-06-06 14:12:37 +03:00
parent f34adc5973
commit 9586aed728
96 changed files with 97 additions and 77 deletions

View File

@ -7,7 +7,7 @@ project(stm32)
add_subdirectory(app) #application add_subdirectory(app) #application
add_subdirectory(bsp) #board support package, lwip add_subdirectory(bsp) #board support package, lwip
add_subdirectory(lib) #stm32f4xx library add_subdirectory(libs/stm32f4xx) #stm32f4xx library

View File

@ -1,9 +1,18 @@
set(elf_file stm32.elf) set(elf_file stm32.elf)
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/nanopb/extra) # include(FetchContent)
find_package(Nanopb REQUIRED)
nanopb_generate_cpp(TARGET proto simple.proto) # FetchContent_Declare(nanopb
# GIT_REPOSITORY https://github.com/nanopb/nanopb
# GIT_TAG master
# )
# FetchContent_MakeAvailable(nanopb)
# set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/nanopb/extra)
# find_package(Nanopb REQUIRED)
# nanopb_generate_cpp(TARGET proto simple.proto)
add_executable(${elf_file} add_executable(${elf_file}
main.c main.c
@ -12,15 +21,13 @@ add_executable(${elf_file}
target_link_libraries(${elf_file} PUBLIC target_link_libraries(${elf_file} PUBLIC
bsp bsp
proto # proto
) )
target_include_directories(${elf_file} PRIVATE target_include_directories(${elf_file} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
) )
set(EXECUTABLE ${PROJECT_NAME}.elf) set(EXECUTABLE ${PROJECT_NAME}.elf)
add_custom_command(TARGET ${EXECUTABLE} add_custom_command(TARGET ${EXECUTABLE}

View File

@ -1,36 +1,37 @@
#include "../bsp/bsp.h" #include "../bsp/bsp.h"
#include <pb_encode.h> // #include <pb_encode.h>
#include <pb_decode.h> // #include <pb_decode.h>
#include "simple.pb.h" // #include "simple.pb.h"
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];
size_t message_length; // size_t message_length;
bool status; // bool status;
/* Encode our message */ // /* Encode our message */
{ // {
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. */
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); // pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
/* Fill in the lucky number */ // /* Fill in the lucky number */
message.number = 42; // message.number = 42;
/* Now we are ready to encode the message! */ // /* Now we are ready to encode the message! */
status = pb_encode(&stream, SimpleMessage_fields, &message); // status = pb_encode(&stream, SimpleMessage_fields, &message);
message_length = stream.bytes_written; // message_length = stream.bytes_written;
udp_send_data(buffer, message_length); // printf('%d\r\n', buffer);
/* Then just check for any errors.. */ // // udp_send_data(buffer, message_length);
if (!status) // /* Then just check for any errors.. */
{ // if (!status)
printf("Encoding failed: %s\n", PB_GET_ERROR(&stream)); // {
return 1; // printf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
} // return 1;
} // }
return 0; // }
} // return 0;
// }
int main(void) int main(void)
{ {
@ -41,14 +42,14 @@ int main(void)
while (1) while (1)
{ {
lwipProcess(); // poll for ethernet rx and timer operations. lwipProcess();
const char *data = "Hello, world!"; const char *data = "Hello, world!";
sprintf(numStr, "%d", cnt++); sprintf(numStr, "%d", cnt++);
char* strCat = strcat(data, numStr); char* strCat = strcat(data, numStr);
udp_send_data(numStr, strlen(numStr)); // udp_send_data(numStr, strlen(numStr));
// serialzie(); // serialzie();
GPIO_ToggleBits(GPIOB, GPIO_Pin_14); GPIO_ToggleBits(GPIOB, GPIO_Pin_14);
delay(500); delay(100);
} }
} }

View File

@ -1,6 +1,3 @@
// A very simple protocol definition, consisting of only
// one message.
syntax = "proto2"; syntax = "proto2";
message SimpleMessage { message SimpleMessage {

View File

@ -60,9 +60,6 @@ void uartInit(void)
GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2); GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2); GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2);
// NVIC_SetPriority(USART2_IRQn, 0);
// NVIC_EnableIRQ(USART2_IRQn);
USART_InitTypeDef usart; USART_InitTypeDef usart;
usart.USART_BaudRate = 115200; usart.USART_BaudRate = 115200;
usart.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; usart.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
@ -139,6 +136,6 @@ void boardInit() {
uartInit(); uartInit();
ethInit(); ethInit();
tim_init(); tim_init();
printf("Starting main loop..\r\n");
lwipInit(); lwipInit();
printf("Starting app..\r\n");
} }

View File

@ -8,6 +8,7 @@
#include "stm32f4xx_dma.h" #include "stm32f4xx_dma.h"
#include "stm32f4xx_tim.h" #include "stm32f4xx_tim.h"
#include "stm32f4xx_flash.h" #include "stm32f4xx_flash.h"
#include "stm32f4xx_hal_eth.h"
#include "lwip/lwip.h" #include "lwip/lwip.h"
typedef enum { typedef enum {

View File

@ -4,11 +4,9 @@
#include "lwip/err.h" #include "lwip/err.h"
#include "lwip/netif.h" #include "lwip/netif.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);
void ethernetif_input(struct netif *netif); void ethernetif_input(struct netif *netif);
void ethernetif_update_config(struct netif *netif); void ethernetif_update_config(struct netif *netif);
void ethernetif_notify_conn_changed(struct netif *netif); void ethernetif_notify_conn_changed(struct netif *netif);

View File

@ -8,11 +8,13 @@
#include "lwip/timeouts.h" #include "lwip/timeouts.h"
#include "netif/etharp.h" #include "netif/etharp.h"
#define UDP_RX_BUFFER_SIZE 128
void Error_Handler(void); void Error_Handler(void);
struct udp_pcb *upcb; struct udp_pcb *upcb;
ip_addr_t remote_ip; ip_addr_t remote_ip;
u16_t remote_port; const uint16_t remote_port = 5678;
struct netif gnetif; struct netif gnetif;
ip4_addr_t ipaddr; ip4_addr_t ipaddr;
@ -22,27 +24,40 @@ 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];
static char udp_receive_buffer[UDP_RX_BUFFER_SIZE];
void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port);
void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) {
strncpy(udp_receive_buffer,p->payload,p->len);
udp_receive_buffer[p->len]=0;
// printf("Test\r\n");
printf("%s\r\n", udp_receive_buffer);
pbuf_free(p);
GPIO_ToggleBits(GPIOD, GPIO_Pin_0);
}
void udpServer_init(void) void udpServer_init(void)
{ {
// UDP Control Block structure ip_addr_t local_ip;
uint16_t local_port = 1234;
uint16_t remote_port = 5678;
err_t err; err_t err;
/* 1. Create a new UDP control block */
upcb = udp_new(); upcb = udp_new();
ip_addr_t local_ip;
u16_t local_port; if(upcb != NULL) {
local_port = 1234;
/* 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); err = udp_bind(upcb, &local_ip, local_port);
if (err == ERR_OK) {
/* 3. Set a receive callback for the upcb */ // printf("UDP bind\r\n");
if(err == ERR_OK) }
{ // IP4_ADDR(&remote_ip, 192, 168, 1, 111);
IP4_ADDR(&remote_ip, 192, 168, 1, 111); // err= udp_connect(upcb, &remote_ip, remote_port);
remote_port = 5678; if (err == ERR_OK) {
// printf("UDP connect\r\n");
udp_recv(upcb, udp_receive_callback, NULL);
}
} }
else else
{ {
@ -68,6 +83,8 @@ void udp_send_data(const char *data, u16_t len)
} }
} }
void lwipInit(void) void lwipInit(void)
{ {
IP_ADDRESS[0] = 192; IP_ADDRESS[0] = 192;

View File

@ -147,8 +147,11 @@
#define LWIP_DEBUG 1 #define LWIP_DEBUG 1
#define LWIP_TCP 0 #define LWIP_TCP 0
#define LWIP_UDP 1
#define MEMP_NUM_UDP_PCB 6
#define LWIP_NETIF_LINK_CALLBACK 1 #define LWIP_NETIF_LINK_CALLBACK 1
#define PBUF_POOL_SIZE 16
#define PBUF_POOL_BUFSIZE 1200
/* USER CODE END 1 */ /* USER CODE END 1 */
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -396,7 +396,8 @@ udp_input(struct pbuf *p, struct netif *inp)
pbuf_free(p); pbuf_free(p);
goto end; goto end;
} }
} else { }
else {
LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: not for us.\n")); LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: not for us.\n"));
#if LWIP_ICMP || LWIP_ICMP6 #if LWIP_ICMP || LWIP_ICMP6

1
nanopb

@ -1 +0,0 @@
Subproject commit 7f88274070afa5edfaf608f4d8e32f3d3c1de139

View File

@ -20,6 +20,5 @@ set(shared_options "-Wall -Wextra -O2 -mlittle-endian -mthumb -mcpu=cortex-m4 -m
set(CMAKE_C_FLAGS_INIT "${shared_options}" CACHE INTERNAL "Initial options for C compiler.") 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_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.") 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.")
# set(CMAKE_EXE_LINKER_FLAGS_INIT "${CMAKE_EXE_LINKER_FLAGS_INIT}, -T ${CMAKE_SOURCE_DIR}/lib/src/stm32f439zi_flash.ld")