本帖最后由 sujingliang 于 2024-9-7 21:05 编辑
目的
实现板载调试串口通讯,实现字符串打印输出“Hello 21ic, Hello STM32H7S78-DK!"
一、硬件准备
STLINK虚拟串口连接到PD0、PD1,对应UART4
STM32CubeMX中对UART4配置
二、TouchGFX配置
增加一个Screen3,添加几个部件
增加一个Ineraction
trigger:Button clicked
Choose clicked Soucre:buttonUartSend
Action:Call new virtual fuciton
Funciton Name: uartSend
实现按下buttonUartSend按键,触发uartSend函数。
三、Uart4初始化及printf重定向
STM32CubeMX配置UART4后,生成如下代码:
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] UART4 Initialization Function
* @param None
* @retval None
*/
static void MX_UART4_Init(void)
{
/* USER CODE BEGIN UART4_Init 0 */
/* USER CODE END UART4_Init 0 */
/* USER CODE BEGIN UART4_Init 1 */
/* USER CODE END UART4_Init 1 */
huart4.Instance = UART4;
huart4.Init.BaudRate = 115200;
huart4.Init.WordLength = UART_WORDLENGTH_8B;
huart4.Init.StopBits = UART_STOPBITS_1;
huart4.Init.Parity = UART_PARITY_NONE;
huart4.Init.Mode = UART_MODE_TX_RX;
huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart4.Init.OverSampling = UART_OVERSAMPLING_16;
huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart4.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart4) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart4, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart4, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart4) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN UART4_Init 2 */
/* USER CODE END UART4_Init 2 */
}
添加printf重定向:
#if defined(__ICCARM__)
__ATTRIBUTES size_t __write(int, const unsigned char *, size_t);
#endif /* __ICCARM__ */
#if defined(__ICCARM__)
/* New definition from EWARM V9, compatible with EWARM8 */
int iar_fputc(int ch);
#define PUTCHAR_PROTOTYPE int iar_fputc(int ch)
#elif defined ( __CC_ARM ) || defined(__ARMCC_VERSION)
/* ARM Compiler 5/6*/
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#elif defined(__GNUC__)
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#endif /* __ICCARM__ */
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART3 and Loop until the end of transmission */
HAL_UART_Transmit(&huart4, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
int fgetc(FILE *f)
{
int ch;
/* 等待串口输入数据 */
while (__HAL_UART_GET_FLAG(&huart4, UART_FLAG_RXNE) == RESET);
HAL_UART_Receive(&huart4, (uint8_t *)&ch, 1, 0xFFFF);
return (ch);
}
#if defined(__ICCARM__)
size_t __write(int file, unsigned char const *ptr, size_t len)
{
size_t idx;
unsigned char const *pdata = ptr;
for (idx = 0; idx < len; idx++)
{
iar_fputc((int)*pdata);
pdata++;
}
return len;
}
#endif /* __ICCARM__ */
如果用gcc,需要确保makefile中包括syscalls.c。当然还要有
$(Drivers_path)/STM32H7RSxx_HAL_Driver/Src/stm32h7rsxx_hal_uart.c
$(Drivers_path)/STM32H7RSxx_HAL_Driver/Src/stm32h7rsxx_hal_uart_ex.c
四、MVP架构实现通过TouchGFX驱动串口发送信息
1、Model.cpp中增加
void Model::uart4SendTest()
{
printf("Hello 21ic, Hello STM32H7S78-DK!\r\n");
}
2、Screen3View中增加
void Screen3View::uartSend()
{
presenter->uart4SendTest();
}
3、Screen3Presenter中增加
void Screen3Presenter::uart4SendTest()
{
model->uart4SendTest();
}
五、效果
按下“UartSend”,串口会打印出信息。
|