convert usart.c to cpp class
This commit is contained in:
4
.editorconfig
Normal file
4
.editorconfig
Normal file
@@ -0,0 +1,4 @@
|
||||
charset = utf-8
|
||||
[*.{c,h,cpp,hpp}]
|
||||
indent_style = space
|
||||
indent_size = 4
|
18
Core/Inc/pindef.hpp
Normal file
18
Core/Inc/pindef.hpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "stm32f401xe.h"
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
constexpr const uint16_t B1_Pin = GPIO_PIN_13;
|
||||
constexpr const uint16_t USART_TX_Pin = GPIO_PIN_2;
|
||||
constexpr const uint16_t USART_RX_Pin = GPIO_PIN_3;
|
||||
constexpr const uint16_t LD2_Pin = GPIO_PIN_5;
|
||||
constexpr const uint16_t TMS_Pin = GPIO_PIN_13;
|
||||
constexpr const uint16_t TCK_Pin = GPIO_PIN_14;
|
||||
constexpr const uint16_t SWO_Pin = GPIO_PIN_3;
|
||||
|
||||
inline GPIO_TypeDef* B1_GPIO_Port = GPIOC;
|
||||
inline GPIO_TypeDef* USART_TX_GPIO_Port = GPIOA;
|
||||
inline GPIO_TypeDef* USART_RX_GPIO_Port = GPIOA;
|
||||
inline GPIO_TypeDef* LD2_GPIO_Port = GPIOA;
|
||||
inline GPIO_TypeDef* TMS_GPIO_Port = GPIOA;
|
||||
inline GPIO_TypeDef* TCK_GPIO_Port = GPIOA;
|
||||
inline GPIO_TypeDef* SWO_GPIO_Port = GPIOB;
|
@@ -40,13 +40,8 @@ extern UART_HandleTypeDef huart2;
|
||||
|
||||
void MX_USART2_UART_Init(void);
|
||||
|
||||
/* USER CODE BEGIN Prototypes */
|
||||
void HAL_UART_Tx(void);
|
||||
/* USER CODE END Prototypes */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USART_H__ */
|
||||
|
||||
|
@@ -20,6 +20,7 @@ class Usart {
|
||||
~Usart() = default;
|
||||
|
||||
bool init();
|
||||
void deinit();
|
||||
void print(const std::string_view str);
|
||||
void println(const std::string_view str);
|
||||
|
||||
|
@@ -67,47 +67,27 @@ void SystemClock_Config(void);
|
||||
*/
|
||||
int main(void) {
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
|
||||
/* MCU Configuration--------------------------------------------------------*/
|
||||
|
||||
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
|
||||
/* USER CODE BEGIN SysInit */
|
||||
|
||||
/* USER CODE END SysInit */
|
||||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
|
||||
driver::usart::Usart usart2(USART2, 115200, UART_WORDLENGTH_8B, UART_STOPBITS_1,
|
||||
UART_PARITY_NONE, UART_MODE_TX_RX, UART_HWCONTROL_NONE,
|
||||
UART_OVERSAMPLING_16);
|
||||
|
||||
usart2.init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
usart2.println("\r\nWeight cell init.");
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1) {
|
||||
usart2.println("This is a pretty long string, exceeding TX bufsize. Hello, world!11!");
|
||||
HAL_Delay(500);
|
||||
/* USER CODE END WHILE */
|
||||
usart2.println("\r\n Hello, world! cell init.");
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
HAL_Delay(500);
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,8 +119,7 @@ void SystemClock_Config(void) {
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Initializes the CPU, AHB and APB buses clocks
|
||||
*/
|
||||
/** Initializes the CPU, AHB and APB buses clocks */
|
||||
RCC_ClkInitStruct.ClockType =
|
||||
RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
|
||||
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
||||
|
121
Core/Src/usart.c
121
Core/Src/usart.c
@@ -1,121 +0,0 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file usart.c
|
||||
* @brief This file provides code for the configuration
|
||||
* of the USART instances.
|
||||
******************************************************************************
|
||||
* @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 "usart.h"
|
||||
#include "stm32f4xx_hal_uart.h"
|
||||
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
UART_HandleTypeDef huart2;
|
||||
|
||||
/* USART2 init function */
|
||||
|
||||
void MX_USART2_UART_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN USART2_Init 0 */
|
||||
|
||||
/* USER CODE END USART2_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN USART2_Init 1 */
|
||||
|
||||
/* USER CODE END USART2_Init 1 */
|
||||
huart2.Instance = USART2;
|
||||
huart2.Init.BaudRate = 115200;
|
||||
huart2.Init.WordLength = UART_WORDLENGTH_8B;
|
||||
huart2.Init.StopBits = UART_STOPBITS_1;
|
||||
huart2.Init.Parity = UART_PARITY_NONE;
|
||||
huart2.Init.Mode = UART_MODE_TX_RX;
|
||||
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
||||
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
|
||||
if (HAL_UART_Init(&huart2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN USART2_Init 2 */
|
||||
|
||||
/* USER CODE END USART2_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
void HAL_UART_Tx(void) {
|
||||
const uint8_t testdata[20] = "Hello, World!\r\n";
|
||||
|
||||
HAL_UART_Transmit(&huart2, testdata, sizeof(testdata), 500);
|
||||
}
|
||||
|
||||
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
|
||||
{
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
if(uartHandle->Instance==USART2)
|
||||
{
|
||||
/* USER CODE BEGIN USART2_MspInit 0 */
|
||||
|
||||
/* USER CODE END USART2_MspInit 0 */
|
||||
/* USART2 clock enable */
|
||||
__HAL_RCC_USART2_CLK_ENABLE();
|
||||
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
/**USART2 GPIO Configuration
|
||||
PA2 ------> USART2_TX
|
||||
PA3 ------> USART2_RX
|
||||
*/
|
||||
GPIO_InitStruct.Pin = USART_TX_Pin|USART_RX_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN USART2_MspInit 1 */
|
||||
|
||||
/* USER CODE END USART2_MspInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
|
||||
{
|
||||
|
||||
if(uartHandle->Instance==USART2)
|
||||
{
|
||||
/* USER CODE BEGIN USART2_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END USART2_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_USART2_CLK_DISABLE();
|
||||
|
||||
/**USART2 GPIO Configuration
|
||||
PA2 ------> USART2_TX
|
||||
PA3 ------> USART2_RX
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOA, USART_TX_Pin|USART_RX_Pin);
|
||||
|
||||
/* USER CODE BEGIN USART2_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END USART2_MspDeInit 1 */
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <string_view>
|
||||
|
||||
#include "pindef.hpp"
|
||||
#include "stm32f4xx_hal_uart.h"
|
||||
|
||||
namespace driver::usart {
|
||||
@@ -20,9 +21,35 @@ Usart::Usart(USART_TypeDef* usart, uint32_t baudRate, uint32_t wordLength, uint3
|
||||
.OverSampling = overSampling}} {}
|
||||
|
||||
bool Usart::init() {
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
if (mHandle.Instance == USART2) {
|
||||
// USART2 clock enable
|
||||
__HAL_RCC_USART2_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
// USART2 GPIO Configuration
|
||||
// PA2 -> USART2_TX
|
||||
// PA3 -> USART2_RX
|
||||
|
||||
GPIO_InitStruct.Pin = USART_TX_Pin | USART_RX_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
}
|
||||
|
||||
return HAL_UART_Init(&mHandle) != HAL_OK;
|
||||
}
|
||||
|
||||
void Usart::deinit() {
|
||||
if (mHandle.Instance == USART2) {
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_USART2_CLK_DISABLE();
|
||||
|
||||
HAL_GPIO_DeInit(GPIOA, USART_TX_Pin | USART_RX_Pin);
|
||||
}
|
||||
}
|
||||
|
||||
void Usart::print(const std::string_view str) {
|
||||
tx(str);
|
||||
};
|
||||
|
@@ -22,7 +22,6 @@ target_include_directories(stm32cubemx INTERFACE
|
||||
|
||||
target_sources(stm32cubemx INTERFACE
|
||||
../../Core/Src/gpio.c
|
||||
../../Core/Src/usart.c
|
||||
../../Core/Src/stm32f4xx_it.c
|
||||
../../Core/Src/stm32f4xx_hal_msp.c
|
||||
../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c
|
||||
|
Reference in New Issue
Block a user