serdes working

This commit is contained in:
Vasily Markov 2024-06-11 19:59:25 +03:00
parent ca64e3480d
commit b302c1d306
10 changed files with 77 additions and 23 deletions

View File

@ -3,7 +3,10 @@ set(elf_file stm32.elf)
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/nanopb/extra) set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/nanopb/extra)
find_package(Nanopb REQUIRED) find_package(Nanopb REQUIRED)
nanopb_generate_cpp(TARGET proto simple.proto) nanopb_generate_cpp(TARGET proto
simple.proto
quaternion.proto
)
add_executable(${elf_file} add_executable(${elf_file}
app.c app.c

View File

@ -2,9 +2,29 @@
#include <pb_encode.h> #include <pb_encode.h>
#include <pb_decode.h> #include <pb_decode.h>
#include "simple.pb.h" #include "simple.pb.h"
#include "quaternion.pb.h"
#define SER_BUF_SIZE 128 #define SER_BUF_SIZE 128
uint8_t ser_buffer[SER_BUF_SIZE]; uint8_t serdes_buffer[SER_BUF_SIZE];
uint8_t deserialize(uint8_t* buffer, size_t len) {
bool status = 0;
SimpleMessage message = SimpleMessage_init_zero;
Quaternion quat = Quaternion_init_zero;
pb_istream_t stream = pb_istream_from_buffer(buffer, len);
status = pb_decode(&stream, Quaternion_fields, &quat);
if (!status)
{
printf("Decoding failed: %s\r\n", PB_GET_ERROR(&stream));
return 1;
}
printf("Quaternion: w: %.2f, x: %.2f, y: %.2f, z: %.2f\r\n", quat.w, quat.x, quat.y, quat.z);
}
uint8_t serialize(uint8_t* buffer, size_t* len) { uint8_t serialize(uint8_t* buffer, size_t* len) {
size_t message_length = 0; size_t message_length = 0;
@ -28,10 +48,12 @@ uint8_t serialize(uint8_t* buffer, size_t* len) {
return 0; return 0;
} }
void sendData(sd_callback_t send_data_callback) { void commandHandler() {
if (get_dbg_uart() == NULL)
return; }
size_t ser_len = 0;
serialize(ser_buffer, &ser_len); void sendData(sd_callback_t send_data_callback) {
send_data_callback(ser_buffer, ser_len); size_t ser_len = 0;
serialize(serdes_buffer, &ser_len);
send_data_callback(serdes_buffer, ser_len);
} }

View File

@ -3,6 +3,7 @@
#include "bsp.h" #include "bsp.h"
typedef void (*sd_callback_t)(const uint8_t*, uint16_t); typedef void (*sd_callback_t)(const uint8_t*, uint16_t);
void sendData(sd_callback_t);
void sendData(sd_callback_t);
uint8_t deserialize(uint8_t* buffer, size_t len);
#endif #endif

View File

@ -11,9 +11,12 @@ int main(void)
while (1) while (1)
{ {
lwipProcess(); lwipProcess();
sendData(udp_send_data);
GPIO_ToggleBits(GPIOB, GPIO_Pin_14); GPIO_ToggleBits(GPIOB, GPIO_Pin_14);
delay(100); if (getLwipStatus()->udp_packet_rdy == PACKET_RDY) {
getLwipStatus()->udp_packet_rdy = PACKET_NOT_RDY;
udp_receive_buffer_t* udp_buffer = getUdpReceiveBuffer();
deserialize(udp_buffer->buf, udp_buffer->len);
}
} }
} }

8
app/quaternion.proto Normal file
View File

@ -0,0 +1,8 @@
syntax = "proto3";
message Quaternion {
float w = 1;
float x = 2;
float y = 3;
float z = 4;
}

View File

@ -5,7 +5,7 @@
static USART_TypeDef* dbg_uart = NULL; static USART_TypeDef* dbg_uart = NULL;
static lwip_status_t lwip_status = {.link_status = LINK_DOWN}; static lwip_status_t lwip_status = {.link_status = LINK_DOWN, .udp_packet_rdy = PACKET_NOT_RDY};
USART_TypeDef* get_dbg_uart() { USART_TypeDef* get_dbg_uart() {
return &dbg_uart; return &dbg_uart;
@ -19,7 +19,9 @@ void register_dbg_uart(USART_TypeDef* uart) {
dbg_uart = uart; dbg_uart = uart;
} }
void send_uart(const uint8_t* data, uint8_t len) { void uart_send_data(const uint8_t* data, uint8_t len) {
if (get_dbg_uart() == NULL)
return;
uint8_t i = 0; uint8_t i = 0;
while(i < len) { while(i < len) {
while (USART_GetFlagStatus(dbg_uart, USART_FLAG_TXE) == RESET); while (USART_GetFlagStatus(dbg_uart, USART_FLAG_TXE) == RESET);

View File

@ -13,15 +13,18 @@
typedef enum { typedef enum {
LINK_DOWN, LINK_DOWN,
LINK_UP LINK_UP,
PACKET_RDY,
PACKET_NOT_RDY
} status_t; } status_t;
typedef struct { typedef struct {
uint8_t link_status; uint8_t link_status;
uint8_t udp_packet_rdy;
} lwip_status_t; } lwip_status_t;
void boardInit(); void boardInit();
lwip_status_t* getLwipStatus(); lwip_status_t* getLwipStatus();
USART_TypeDef* get_dbg_uart(); USART_TypeDef* get_dbg_uart();
void send_uart(const uint8_t*, uint8_t); void uart_send_data(const uint8_t*, uint8_t);
#endif #endif

View File

@ -8,7 +8,6 @@
#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);
@ -24,17 +23,22 @@ 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]; static udp_receive_buffer_t udp_receive_buffer = {.len = 0};
udp_receive_buffer_t* getUdpReceiveBuffer() {
return &udp_receive_buffer;
}
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);
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); strncpy(udp_receive_buffer.buf,p->payload,p->len);
udp_receive_buffer[p->len]=0; udp_receive_buffer.len = p->len;
// printf("Test\r\n"); udp_receive_buffer.buf[p->len]=0;
printf("%s\r\n", udp_receive_buffer); // printf("udp packet\r\n");
// printf("%s\r\n", udp_receive_buffer.buf);
getLwipStatus()->udp_packet_rdy = PACKET_RDY;
pbuf_free(p); pbuf_free(p);
GPIO_ToggleBits(GPIOD, GPIO_Pin_0);
} }
void udpServer_init(void) void udpServer_init(void)
@ -52,7 +56,7 @@ void udpServer_init(void)
if (err == ERR_OK) { if (err == ERR_OK) {
// printf("UDP bind\r\n"); // printf("UDP bind\r\n");
} }
IP4_ADDR(&remote_ip, 192, 168, 1, 111); IP4_ADDR(&remote_ip, 192, 168, 1, 110);
// err= udp_connect(upcb, &remote_ip, remote_port); // err= udp_connect(upcb, &remote_ip, remote_port);
if (err == ERR_OK) { if (err == ERR_OK) {
// printf("UDP connect\r\n"); // printf("UDP connect\r\n");

View File

@ -3,9 +3,16 @@
#include "ethernetif.h" #include "ethernetif.h"
#define UDP_RX_BUFFER_SIZE 128
typedef struct {
size_t len;
char buf[UDP_RX_BUFFER_SIZE];
} udp_receive_buffer_t;
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 lwipInit(void); void lwipInit(void);
void lwipProcess(void); void lwipProcess(void);
udp_receive_buffer_t* getUdpReceiveBuffer();
#endif #endif

View File

@ -1,5 +1,6 @@
#include "stm32f4xx_it.h" #include "stm32f4xx_it.h"
#include "lwip/ethernetif.h" #include "lwip/ethernetif.h"
#include "../app/app.h"
static volatile uint32_t sys_tick = 0; static volatile uint32_t sys_tick = 0;
@ -26,13 +27,13 @@ void delay(uint32_t delay) {
void TIM7_IRQHandler() void TIM7_IRQHandler()
{ {
TIM_ClearITPendingBit(TIM7, TIM_IT_Update); TIM_ClearITPendingBit(TIM7, TIM_IT_Update);
// GPIO_ToggleBits(GPIOB, GPIO_Pin_7);
if(getLwipStatus()->link_status == LINK_UP) { if(getLwipStatus()->link_status == LINK_UP) {
GPIO_ToggleBits(GPIOB, GPIO_Pin_7); GPIO_ToggleBits(GPIOB, GPIO_Pin_7);
} }
else { else {
GPIO_ResetBits(GPIOB, GPIO_Pin_7); GPIO_ResetBits(GPIOB, GPIO_Pin_7);
} }
// sendData(udp_send_data);
} }
void USART2_IRQHandler(void) void USART2_IRQHandler(void)