From a9f146ef4da9b9fcde9ec71f84a0741e64d8bd8a Mon Sep 17 00:00:00 2001 From: Vasily Markov Date: Mon, 10 Jun 2024 18:46:02 +0300 Subject: [PATCH] callbacks sending data --- app/CMakeLists.txt | 3 ++- app/app.c | 42 ++++++++++++++++++++++++++++++++++++++++++ app/app.h | 8 ++++++++ app/main.c | 39 ++------------------------------------- bsp/bsp.c | 16 ++++++++++++++++ bsp/bsp.h | 2 +- bsp/lwip/lwip.c | 2 -- 7 files changed, 71 insertions(+), 41 deletions(-) create mode 100644 app/app.c create mode 100644 app/app.h diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index a86e9e2..bc78a3a 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -5,7 +5,8 @@ find_package(Nanopb REQUIRED) nanopb_generate_cpp(TARGET proto simple.proto) -add_executable(${elf_file} +add_executable(${elf_file} + app.c main.c syscalls.c ) diff --git a/app/app.c b/app/app.c new file mode 100644 index 0000000..7a444c3 --- /dev/null +++ b/app/app.c @@ -0,0 +1,42 @@ +#include "app.h" +#include +#include +#include "simple.pb.h" + +#define SER_BUF_SIZE 128 +uint8_t ser_buffer[SER_BUF_SIZE]; + + +uint8_t serialize(uint8_t* buffer, size_t* len) { + size_t message_length = 0; + bool status = 0; + static uint32_t cnt = 0; + SimpleMessage message = SimpleMessage_init_zero; + + /* Create a stream that will write to our buffer. */ + pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); + + /* Fill in the lucky number */ + message.number = cnt++; + /* Now we are ready to encode the message! */ + status = pb_encode(&stream, SimpleMessage_fields, &message); + *len = stream.bytes_written; + + + /* Then just check for any errors.. */ + if (!status) + { + printf("Encoding failed: %s\n", PB_GET_ERROR(&stream)); + return 1; + } + + return 0; +} + + + +void sendData(sd_callback_t send_data_callback) { + size_t ser_len = 0; + serialize(ser_buffer, &ser_len); + send_data_callback(ser_buffer, ser_len); +} \ No newline at end of file diff --git a/app/app.h b/app/app.h new file mode 100644 index 0000000..e31abce --- /dev/null +++ b/app/app.h @@ -0,0 +1,8 @@ +#ifndef __APP_H +#define __APP_H +#include "bsp.h" + +typedef void (*sd_callback_t)(const uint8_t*, uint16_t); +void sendData(sd_callback_t); + +#endif \ No newline at end of file diff --git a/app/main.c b/app/main.c index 4c2a594..8b5858f 100644 --- a/app/main.c +++ b/app/main.c @@ -1,37 +1,7 @@ #include "../bsp/bsp.h" -#include -#include -#include "simple.pb.h" +#include "app.h" -void serialzie() { - /* This is the buffer where we will store our message. */ - uint8_t buffer[128]; - size_t message_length; - bool status; - - /* Encode our message */ - { - SimpleMessage message = SimpleMessage_init_zero; - - /* Create a stream that will write to our buffer. */ - pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); - - /* Fill in the lucky number */ - message.number = 42; - /* Now we are ready to encode the message! */ - status = pb_encode(&stream, SimpleMessage_fields, &message); - message_length = stream.bytes_written; - udp_send_data(buffer, message_length); - /* Then just check for any errors.. */ - if (!status) - { - printf("Encoding failed: %s\n", PB_GET_ERROR(&stream)); - return 1; - } - } - return 0; -} int main(void) { @@ -43,12 +13,7 @@ int main(void) while (1) { lwipProcess(); - const char *data = "Hello, world!"; - sprintf(numStr, "%d", cnt++); - char* strCat = strcat(data, numStr); - // udp_send_data(numStr, strlen(numStr)); - serialzie(); - // printf("Test\r\n"); + sendData(udp_send_data); GPIO_ToggleBits(GPIOB, GPIO_Pin_14); delay(100); } diff --git a/bsp/bsp.c b/bsp/bsp.c index bac31d1..633299a 100644 --- a/bsp/bsp.c +++ b/bsp/bsp.c @@ -3,12 +3,27 @@ #define kHz_1 1000 +static USART_TypeDef* dbg_uart = NULL; + static lwip_status_t lwip_status = {.link_status = LINK_DOWN}; lwip_status_t* getLwipStatus() { return &lwip_status; } +void register_dbg_uart(USART_TypeDef* uart) { + dbg_uart = uart; +} + +void send_uart(const uint8_t* data, uint8_t len) { + uint8_t i = 0; + while(i < len) { + while (USART_GetFlagStatus(dbg_uart, USART_FLAG_TXE) == RESET); + USART_SendData(dbg_uart, *data++); + i++; + } +} + void sysInit() { RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); @@ -134,6 +149,7 @@ void boardInit() { sysInit(); gpioInit(); uartInit(); + register_dbg_uart(USART2); ethInit(); tim_init(); lwipInit(); diff --git a/bsp/bsp.h b/bsp/bsp.h index bca0aa2..7f0a990 100644 --- a/bsp/bsp.h +++ b/bsp/bsp.h @@ -22,5 +22,5 @@ typedef struct { void boardInit(); lwip_status_t* getLwipStatus(); - +void send_uart(const uint8_t*, uint8_t); #endif \ No newline at end of file diff --git a/bsp/lwip/lwip.c b/bsp/lwip/lwip.c index 7be6f71..7a0f119 100644 --- a/bsp/lwip/lwip.c +++ b/bsp/lwip/lwip.c @@ -83,8 +83,6 @@ void udp_send_data(const char *data, u16_t len) } } - - void lwipInit(void) { IP_ADDRESS[0] = 192;