STM32 HAL库 UART使用printf
// 添加这个函数
int fputc(int ch,FILE *f)
{
uint8_t temp[]={ch};
HAL_UART_Transmit(&UartHandle,temp,,);
}
MDK设置:勾选Use Micro LIB
测试板子:STM32F746NG-DISCOVERY
main.c文件
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include <stdio.h>
/** @addtogroup STM32F7xx_HAL_Examples
* @{
*/ /** @addtogroup UART_TwoBoards_ComDMA
* @{
*/ /* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define TRANSMITTER_BOARD /* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* UART handler declaration */
UART_HandleTypeDef UartHandle;
__IO ITStatus UartReady = RESET;
__IO uint32_t UserButtonStatus = ; /* set to 1 after User Button interrupt */ /* Buffer used for transmission */
uint8_t aTxBuffer[] = " ****UART_TwoBoards communication based on DMA**** ****UART_TwoBoards communication based on DMA**** ****UART_TwoBoards communication based on DMA**** "; /* Buffer used for reception */
uint8_t aRxBuffer[RXBUFFERSIZE]; /* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void Error_Handler(void);
static uint16_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
static void MPU_Config(void);
static void CPU_CACHE_Enable(void); /* Private functions ---------------------------------------------------------*/
UART_HandleTypeDef UartHandle;
uint8_t sendbuf[]="send ok "; // 添加这个函数
int fputc(int ch,FILE *f)
{
uint8_t temp[]={ch};
HAL_UART_Transmit(&UartHandle,temp,,);
} /**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
/* Configure the MPU attributes as Write Through */
MPU_Config(); /* Enable the CPU Cache */
CPU_CACHE_Enable();
/* STM32F7xx HAL library initialization:
- Configure the Flash ART accelerator
- Systick timer is configured by default as source of time base, but user
can eventually implement his proper time base source (a general purpose
timer for example or other time source), keeping in mind that Time base
duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
handled in milliseconds basis.
- Set NVIC Group Priority to 4
- Low Level Initialization
*/
HAL_Init(); /* Configure the system clock to 216 MHz */
SystemClock_Config(); /* Configure LED1 */
BSP_LED_Init(LED1); UartHandle.Instance = DISCOVERY_COM1; UartHandle.Init.BaudRate = ;
UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
UartHandle.Init.StopBits = UART_STOPBITS_1;
UartHandle.Init.Parity = UART_PARITY_NONE;
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UartHandle.Init.Mode = UART_MODE_TX_RX;
BSP_COM_DeInit(COM1,&UartHandle);
BSP_COM_Init(COM1,&UartHandle); // HAL_UART_Transmit(&UartHandle,sendbuf,sizeof(sendbuf),10);
/* Configure User push-button in Interrupt mode */
BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI); /* Wait for User push-button press before starting the Communication.
In the meantime, LED1 is blinking */
printf("hello"); while(UserButtonStatus == )
{
/* Toggle LED1*/
BSP_LED_Toggle(LED1);
HAL_Delay();
}
/* Turn on LED1 if test passes then enter infinite loop */
BSP_LED_On(LED1);
/* Infinite loop */
while ()
{
}
} /**
* @brief System Clock Configuration
* The system Clock is configured as follow :
* System Clock source = PLL (HSE)
* SYSCLK(Hz) = 216000000
* HCLK(Hz) = 216000000
* AHB Prescaler = 1
* APB1 Prescaler = 4
* APB2 Prescaler = 2
* HSE Frequency(Hz) = 25000000
* PLL_M = 25
* PLL_N = 432
* PLL_P = 2
* PLL_Q = 9
* VDD(V) = 3.3
* Main regulator output voltage = Scale1 mode
* Flash Latency(WS) = 7
* @param None
* @retval None
*/
void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
HAL_StatusTypeDef ret = HAL_OK; /* Enable HSE Oscillator and activate PLL with HSE as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = ;
RCC_OscInitStruct.PLL.PLLN = ;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = ; ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
if(ret != HAL_OK)
{
while() { ; }
} /* Activate the OverDrive to reach the 216 MHz Frequency */
ret = HAL_PWREx_EnableOverDrive();
if(ret != HAL_OK)
{
while() { ; }
} /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7);
if(ret != HAL_OK)
{
while() { ; }
}
} /**
* @brief Tx Transfer completed callback
* @param UartHandle: UART handle.
* @note This example shows a simple way to report end of DMA Tx transfer, and
* you can add your own implementation.
* @retval None
*/
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
{
/* Set transmission flag: trasfer complete*/
UartReady = SET; } /**
* @brief Rx Transfer completed callback
* @param UartHandle: UART handle
* @note This example shows a simple way to report end of DMA Rx transfer, and
* you can add your own implementation.
* @retval None
*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
/* Set transmission flag: trasfer complete*/
UartReady = SET; } /**
* @brief UART error callbacks
* @param UartHandle: UART handle
* @note This example shows a simple way to report transfer error, and you can
* add your own implementation.
* @retval None
*/
void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
{
Error_Handler();
} /**
* @brief EXTI line detection callbacks
* @param GPIO_Pin: Specifies the pins connected EXTI line
* @retval None
*/
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == KEY_BUTTON_PIN)
{
UserButtonStatus = ;
}
} /**
* @brief Compares two buffers.
* @param pBuffer1, pBuffer2: buffers to be compared.
* @param BufferLength: buffer's length
* @retval 0 : pBuffer1 identical to pBuffer2
* >0 : pBuffer1 differs from pBuffer2
*/
static uint16_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
{
while (BufferLength--)
{
if ((*pBuffer1) != *pBuffer2)
{
return BufferLength;
}
pBuffer1++;
pBuffer2++;
} return ;
} /**
* @brief This function is executed in case of error occurrence.
* @param None
* @retval None
*/
static void Error_Handler(void)
{
/* Turn LED1 on */
BSP_LED_On(LED1);
while()
{
/* Error if LED1 is slowly blinking (1 sec. period) */
BSP_LED_Toggle(LED1);
HAL_Delay();
}
} #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 can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* Infinite loop */
while ()
{
}
}
#endif /**
* @brief Configure the MPU attributes as Write Through for SRAM1/2.
* @note The Base Address is 0x20010000 since this memory interface is the AXI.
* The Region Size is 256KB, it is related to SRAM1 and SRAM2 memory size.
* @param None
* @retval None
*/
static void MPU_Config(void)
{
MPU_Region_InitTypeDef MPU_InitStruct; /* Disable the MPU */
HAL_MPU_Disable(); /* Configure the MPU attributes as WT for SRAM */
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
MPU_InitStruct.BaseAddress = 0x20010000;
MPU_InitStruct.Size = MPU_REGION_SIZE_256KB;
MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
MPU_InitStruct.IsCacheable = MPU_ACCESS_CACHEABLE;
MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
MPU_InitStruct.Number = MPU_REGION_NUMBER0;
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
MPU_InitStruct.SubRegionDisable = 0x00;
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE; HAL_MPU_ConfigRegion(&MPU_InitStruct); /* Enable the MPU */
HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
} /**
* @brief CPU L1-Cache enable.
* @param None
* @retval None
*/
static void CPU_CACHE_Enable(void)
{
/* Enable I-Cache */
SCB_EnableICache(); /* Enable D-Cache */
SCB_EnableDCache();
} /**
* @}
*/ /**
* @}
*/ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32 HAL库 UART使用printf的更多相关文章
- STM32 HAL库 UART 串口读写功能笔记
https://www.cnblogs.com/Mysterious/p/4804188.html STM32L0 HAL库 UART 串口读写功能 串口发送功能: uint8_t TxData[10 ...
- stm32 HAL库笔记(零)
最近在设计四旋翼飞行器,用stm32f407,有三种开发方式可以选择:一.寄存器开发.二:库函数开发.三:HAL库开发,考虑了一下,选择了HAL库,原因如下: 1. 寄存器开发相对较慢,寄存器很多,配 ...
- STM32 HAL库详解 及 手动移植
源: STM32 HAL库详解 及 手动移植
- 【书籍连载】《STM32 HAL 库开发实战指南—基于F7》-第一章
从今天起,每天开始连载一章<STM32 HAL 库开发实战指南—基于F7>.欢迎各位阅读.点评.学习. 第1章 如何使用本书 1.1 本书的参考资料 本书参考资料为:<STM32 ...
- stm32 hal库串口通信资料汇集
串口的发送接收函数:HAL_UART_Transmit();串口轮询模式发送,使用超时管理机制.HAL_UART_Receive();串口轮询模式发送,使用超时管理机制.HAL_UART_Transm ...
- 【有趣的全彩LED | 编程】用STM32 HAL库让WS2812B为你所动
一.效果展示 观看演示效果:https://www.bilibili.com/video/BV1dv411Y7x3 使用STM32 HAL库编程 PWM+DMA控制输出,CubeMX生成初始工程 实现 ...
- 【情人节选帽子】TCS34725颜色传感器和Python图形界面编程(STM32 HAL库)
截图 描述: l STM32 HAL库编程 l 使用模拟IIC通信,方便程序移植 l Python界面编写,蘑菇头的帽子是什么颜色 l STM32 HAL库串口通信 l Python界面使用 ...
- 【春节歌曲回味 | STM32小音乐盒 】PWM+定时器驱动无源蜂鸣器(STM32 HAL库)
l STM32通过PWM与定时器方式控制无源蜂鸣器鸣响 l STM32小音乐盒,歌曲进度条图形显示与百分比显示,歌曲切换 l 编程使用STM32 HAL库 l IIC OLED界面编程,动画实 ...
- STM32 HAL库利用DMA实现串口不定长度接收方法
参考:https://blog.csdn.net/u014470361/article/details/79206352 我这里使用的芯片是 F1 系列的,主要是利用 DMA 数据传输方式实现的,在配 ...
随机推荐
- goruntine
一.出让时间片 runtime.Gosched() 二.同步锁 Go语言包中的sync包提供了两种锁类型:sync.Mutex和sync.RWMutex.Mutex是最简单的一种锁类型,同时也比较暴力 ...
- close_wait状态的产生原因及解决(转)
最近测试环境server由于需要与大量的后台server交互,今天突然发现有大量的close_wait产生,于是仔细研究了一下: 如果我们的服务器程序处于CLOSE_WAIT状态的话,说明套接字是被动 ...
- 【VBA】VBA编写的,将一列中相同的内容的行提取出来单独生成文件
数据如上图所示,点击RUN后的运行结果如下: 得到该文件夹,文件夹内容如上图. 代码如下: Private Sub Command_OLIVER() Dim arr arr = Range(" ...
- 【VBA】自动填充序号
使用Excle自带的工具栏图标填充 填充效果图如下: 代码如下: Sub 自动填充序号() Dim A As CommandBar '代表容器应用程序中的一个命令栏 Dim B As CommandB ...
- centos针对postgres出现的postgresql Ident authentication failed for user
首先:将/var/lib/..../pg_hba.conf文件中的indent全修改为trust即可. #local all all ...
- Javascript属性constructor/prototype的底层原理
在Javascript语言中,constructor属性是专门为function而设计的,它存在于每个function的prototype属性中. 这个constructor保存了指向function ...
- VB.NET小结
在满头困惑与不断的摸索中.NET视频终究是看完了,感觉这是迄今为止的视频材料中最令人头疼的一个,漫天的繁体字和标准的台湾术语,真是让人欲罢不能.只是看着看着也就慢慢习惯了,大概可以理解老师在讲什么,可 ...
- Exploiting CVE-2015-2509 /MS15-100 : Windows Media Center could allow remote code execution
Exploiting CVE-2015-2509 /MS15-100 : Windows Media Center could allow remote code execution Trend Mi ...
- [译]GLUT教程 - 整合代码4
Lighthouse3d.com >> GLUT Tutorial >> Pop-up Menus >> The Code So Far IV 以下代码使用了位图字 ...
- IntelliJ IDEA 2014 付费版 免费版比较
http://www.jetbrains.com/idea/features/editions_comparison_matrix.html Freemarker, Velocity IDE Feat ...