USART tx length fix, some experiments with cpp string manip

This commit is contained in:
2024-12-16 12:29:13 +01:00
parent 7b7e783d68
commit 17099b0047
4 changed files with 39 additions and 96 deletions

View File

@@ -22,6 +22,9 @@ endif()
# Set the project name # Set the project name
set(CMAKE_PROJECT_NAME weight_cell) set(CMAKE_PROJECT_NAME weight_cell)
# Color output
set(CMAKE_CXX_FLAGSS "-fdiagnostics-color=always")
# Include toolchain file # Include toolchain file
include("cmake/gcc-arm-none-eabi.cmake") include("cmake/gcc-arm-none-eabi.cmake")

View File

@@ -1,70 +1,13 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h" #include "main.h"
#include <cstdlib>
#include "gpio.h" #include "gpio.h"
#include "stm32f4xx_hal.h" #include "stm32f4xx_hal.h"
#include "usart.hpp" #include "usart.hpp"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void); void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void) { int main(void) {
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
@@ -81,19 +24,19 @@ int main(void) {
UART_OVERSAMPLING_16); UART_OVERSAMPLING_16);
usart2.init(); usart2.init();
usart2.println("");
usart2.println("\r\nWeight cell init."); usart2.println("\r\nWeight cell init.");
while (1) { int i{0};
usart2.println("\r\n Hello, world! cell init.");
HAL_Delay(500); while (1) {
std::string buf{"Iteration #"};
usart2.println(buf + std::to_string(i++));
HAL_Delay(1000);
} }
} }
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void) { void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
@@ -132,10 +75,6 @@ void SystemClock_Config(void) {
} }
} }
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/** /**
* @brief This function is executed in case of error occurrence. * @brief This function is executed in case of error occurrence.
* @retval None * @retval None

View File

@@ -27,7 +27,7 @@
/** /**
* Pointer to the current high watermark of the heap usage * Pointer to the current high watermark of the heap usage
*/ */
static uint8_t *__sbrk_heap_end = NULL; static uint8_t* __sbrk_heap_end = NULL;
/** /**
* @brief _sbrk() allocates memory to the newlib heap and is used by malloc * @brief _sbrk() allocates memory to the newlib heap and is used by malloc
@@ -50,30 +50,27 @@ static uint8_t *__sbrk_heap_end = NULL;
* @param incr Memory size * @param incr Memory size
* @return Pointer to allocated memory * @return Pointer to allocated memory
*/ */
void *_sbrk(ptrdiff_t incr) void* _sbrk(ptrdiff_t incr) {
{ extern uint8_t _end; /* Symbol defined in the linker script */
extern uint8_t _end; /* Symbol defined in the linker script */ extern uint8_t _estack; /* Symbol defined in the linker script */
extern uint8_t _estack; /* Symbol defined in the linker script */ extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */
extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */ const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size;
const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size; const uint8_t* max_heap = (uint8_t*)stack_limit;
const uint8_t *max_heap = (uint8_t *)stack_limit; uint8_t* prev_heap_end;
uint8_t *prev_heap_end;
/* Initialize heap end at first call */ /* Initialize heap end at first call */
if (NULL == __sbrk_heap_end) if (NULL == __sbrk_heap_end) {
{ __sbrk_heap_end = &_end;
__sbrk_heap_end = &_end; }
}
/* Protect heap from growing into the reserved MSP stack */ /* Protect heap from growing into the reserved MSP stack */
if (__sbrk_heap_end + incr > max_heap) if (__sbrk_heap_end + incr > max_heap) {
{ errno = ENOMEM;
errno = ENOMEM; return (void*)-1;
return (void *)-1; }
}
prev_heap_end = __sbrk_heap_end; prev_heap_end = __sbrk_heap_end;
__sbrk_heap_end += incr; __sbrk_heap_end += incr;
return (void *)prev_heap_end; return (void*)prev_heap_end;
} }

View File

@@ -21,7 +21,7 @@ Usart::Usart(USART_TypeDef* usart, uint32_t baudRate, uint32_t wordLength, uint3
.OverSampling = overSampling}} {} .OverSampling = overSampling}} {}
bool Usart::init() { bool Usart::init() {
GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitTypeDef GPIO_InitStruct;
if (mHandle.Instance == USART2) { if (mHandle.Instance == USART2) {
// USART2 clock enable // USART2 clock enable
__HAL_RCC_USART2_CLK_ENABLE(); __HAL_RCC_USART2_CLK_ENABLE();
@@ -61,8 +61,12 @@ void Usart::println(const std::string_view str) {
void Usart::tx(const std::string_view range) { void Usart::tx(const std::string_view range) {
for (uint32_t pt{0}; pt <= range.size(); pt += TX_BUFSIZE) { for (uint32_t pt{0}; pt <= range.size(); pt += TX_BUFSIZE) {
HAL_UART_Transmit(&mHandle, reinterpret_cast<const uint8_t*>(range.begin() + pt), uint8_t txLen{TX_BUFSIZE};
TX_BUFSIZE, TX_TIMEOUT_MS); if (range.length() < TX_BUFSIZE) {
txLen = range.length();
}
HAL_UART_Transmit(&mHandle, reinterpret_cast<const uint8_t*>(range.begin() + pt), txLen,
TX_TIMEOUT_MS);
} }
}; };