From 708ac5f80a0aedfd6c9e516f8c2da94fae7e03a7 Mon Sep 17 00:00:00 2001 From: Vasily Markov Date: Mon, 13 May 2024 20:44:16 +0300 Subject: [PATCH] nanopb --- app/CMakeLists.txt | 8 ++++ app/main.c | 95 ++++++++++++++++++++++++++++++++++------------ app/simple.proto | 10 +++++ bsp/CMakeLists.txt | 1 + 4 files changed, 89 insertions(+), 25 deletions(-) create mode 100644 app/simple.proto diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 0f4e615..f6e788a 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -1,5 +1,10 @@ set(elf_file stm32.elf) +set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/nanopb/extra) +find_package(Nanopb REQUIRED) + +nanopb_generate_cpp(TARGET proto simple.proto) + add_executable(${elf_file} main.c syscalls.c @@ -7,12 +12,15 @@ add_executable(${elf_file} target_link_libraries(${elf_file} PUBLIC bsp + proto ) target_include_directories(${elf_file} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ) + + set(EXECUTABLE ${PROJECT_NAME}.elf) add_custom_command(TARGET ${EXECUTABLE} diff --git a/app/main.c b/app/main.c index 3fdc12e..e862641 100644 --- a/app/main.c +++ b/app/main.c @@ -1,4 +1,7 @@ #include "../bsp/bsp.h" +#include +#include +#include "simple.pb.h" void MX_GPIO_Init(void) { @@ -92,6 +95,71 @@ void eth_init() { NVIC_EnableIRQ(ETH_IRQn); } +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 */ + { + /* 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; + + /* 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; + } + } + + /* 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; +} + int main(void) { @@ -114,12 +182,13 @@ int main(void) printf("RCC->PLLCFGR :%d\r\n", RCC->PLLCFGR); printf("RCC->APB1ENR :%d\r\n", RCC->APB1ENR); udpServer_init(); - + // serialzie(); while (1) { MX_LWIP_Process(); // poll for ethernet rx and timer operations. const char *data = "Hello, world!"; udp_send_data(data, strlen(data)); + // serialzie(); GPIO_ToggleBits(GPIOB, GPIO_Pin_14); delay(500); } @@ -128,30 +197,6 @@ int main(void) void SystemClock_Config() { - // SystemInit(); SystemCoreClock = 180000000; } -void Error_Handler(void) -{ - printf("Error Handler Triggered..\r\n"); -} - -#ifdef USE_FULL_ASSERT -/** - * @brief Reports the name of the source file and the source line number - * where the assert_param error has occurred. - * @param file: pointer to the source file name - * @param line: assert_param error line source number - * @retval None - */ -void assert_failed(uint8_t *file, uint32_t line) -{ - /* USER CODE BEGIN 6 */ - /* User can add his own implementation to report the file name and line number, - tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ - /* USER CODE END 6 */ -} -#endif /* USE_FULL_ASSERT */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/app/simple.proto b/app/simple.proto new file mode 100644 index 0000000..da1456b --- /dev/null +++ b/app/simple.proto @@ -0,0 +1,10 @@ +// A very simple protocol definition, consisting of only +// one message. + +syntax = "proto2"; + +message SimpleMessage { + optional string project = 1 [default = "controls"]; + required int32 number = 2; +} + diff --git a/bsp/CMakeLists.txt b/bsp/CMakeLists.txt index 8700bd5..14cac90 100644 --- a/bsp/CMakeLists.txt +++ b/bsp/CMakeLists.txt @@ -11,6 +11,7 @@ add_library(bsp STATIC lwip/stm32f4xx_hal_eth.c ${LWIP_SOURCE_DIR}/api/err.c ${LWIP_SOURCE_DIR}/core/def.c + ${LWIP_SOURCE_DIR}/core/inet_chksum.c ${LWIP_SOURCE_DIR}/core/init.c ${LWIP_SOURCE_DIR}/core/ip.c ${LWIP_SOURCE_DIR}/core/mem.c