当前位置:网站首页>Summary of UART problems in stm32cubemx
Summary of UART problems in stm32cubemx
2022-06-21 12:04:00 【Summer foam and light rain】
Write it at the front :
The purpose of this article is to summarize backup 、 For future reference , Because it's a personal summary , If there is any wrong , Welcome to correct ; in addition , Most of the content comes from the Internet 、 Books 、 And all kinds of manuals , In case of infringement, please inform , Immediately delete the post and apologize .
Catalog
One 、gcc I / O redirection in the environment
stay gcc In the environment ,printf Redirection is the same as before IDE Redirection on is a bit different .
In the past Keil、IAR etc. IDE above , They are redirected in the following way :
int fputc(int ch, FILE *f)
int fgetc(FILE *f)
But in gcc In the environment , The following methods are used :
int _write(int file, char *ptr, int len)
int _read(int file, char *ptr, int len)
Now it's clear gcc I / O redirection in the environment , Then you can write the corresponding function . In a general way , We will refer to the official demo To modify and write , Fortunately, the official has provided a sample project , In the firmware library ...\STM32Cube_FW_F1_V1.8.0\Projects\STM32F103RB-Nucleo\Examples\UART\UART_Printf You can find it in the directory ; however , What we really need is syscalls.c file ,syscalls System call means , You can find it inside _write Functions and _read How to implement the function :
__attribute__((weak)) int _read(int file, char *ptr, int len)
{
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
*ptr++ = __io_getchar();
}
return len;
}
__attribute__((weak)) int _write(int file, char *ptr, int len)
{
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
__io_putchar(*ptr++);
}
return len;
}
So we put the whole syscalls.c Just migrate the files ;
stay _write Functions and _read In the function , We can see that it has interfaces , namely :__io_putchar and __io_getchar , So what we really want to achieve is __io_putchar and __io_getchar ; Of course , You can also directly re implement _write Functions and _read function .
Then, in order to use different compilation environments , Often we use macros to make choices , And in the gcc Environment __GNUC__ Is defined by default , Therefore, the following implementation methods are obtained :
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#define GETCHAR_PROTOTYPE int __io_getchar(void)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#define GETCHAR_PROTOTYPE int fgetc(FILE *f)
#endif /* __GNUC__ */
/** * The functionality : Redirect c Library function printf To DEBUG_USARTx * Input parameters : nothing * return return value : nothing * say bright : nothing */
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
/** * The functionality : Redirect c Library function getchar,scanf To DEBUG_USARTx * Input parameters : nothing * return return value : nothing * say bright : nothing */
GETCHAR_PROTOTYPE
{
uint8_t ch = 0;
HAL_UART_Receive(&huart1, &ch, 1, 0xFFFF);
return ch;
}
then , What's worth noting here is :
syscalls.c After the file is migrated to your own project , Remember in Makefile Add the operation of compiling it in .
Reference resources :
https://electronics.stackexchange.com/questions/206113/how-do-i-use-the-printf-function-on-stm32/279945#279945
https://www.openstm32.org/forumthread1055
Two 、gcc Output stream refresh in environment
stay gcc In the environment , Use printf() Output , If... Is not included in the output data \n A newline , that , In case of \n Before or after buffer overflow , No data will be output on the screen ; besides , There is another way to refresh the output data , That is, after sending the data , To run a fflush(stdout) Force a refresh of the output stream , So the data can be sent out .
3、 ... and 、HAL Some functions on the library understand
1、 On interrupt handling ,STM32cubeMX After initializing the generated code , Depend on HAL_UART_Transmit_IT To turn on send or HAL_UART_Receive_IT To open the receive, complete the rest of the interrupt configuration and open ?.. and , The third parameter value Size Determines the parameters that trigger the entry interrupt ;eg:HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, 8); That means that only the received data is greater than or equal to 8 When you start to receive interrupts , And greater than 8 Will enter many times , As the case may be .
2、 In the use of non - interrupt processing functions HAL_UART_Transmit and HAL_UART_Receive When processing receiving and sending data , Fourth parameter Timeout Timeout mechanism , It uses STM32cubeMX Configured Timebase Source The clock ( It's usually systick) To determine the timeout :

Four 、 To be continued ...
边栏推荐
- Shell process control - 35. Multi branch case conditional statements
- MySQL-DML
- [comprehensive pen test] sword finger offer II 114 Alien dictionary
- MySQL 5.6.49 企业版设置密码复杂度策略
- Factory mode implementation
- 1108. IP address invalidation
- harmonyos培训一
- Architect training plan - infinite thinking - variables
- 适配器电源自动测试设备|充电器ATE测试系统NSAT-8000介绍
- 知识点:PCB电路板的几种特殊布线方法
猜你喜欢

STM32开发之 VS Code + GDB下载调试

2022 safety officer-b certificate retraining question bank and simulated examination

清除交换机配置、配置镜像端口以及Wireshark抓包(以Huawei S5720为例)

重磅,MapStruct 1.5 发布,这次终于支持Map转为Bean了!

图文并茂--微信小程序,获取用户地理位置信息,并调用腾讯地图API来获取用户具体位置

2022危险化学品经营单位安全管理人员特种作业证考试题库及在线模拟考试

2022 safety officer-c certificate title and answer

蜜雪冰城(已黑化)

XML entity injection vulnerability

Ansible operating instructions for configuring SSH authentication free for the first time
随机推荐
动手学数据分析 数据重构
2022 safety officer-b certificate retraining question bank and simulated examination
DDoS attack and defense: from principle to practice
第八章 Web项目测试
1108. IP 地址无效化
STL basic container test
Interesting research on mouse pointer interaction
Broken knowledge
Centos7 upgrade MySQL 5.6.40 to enterprise 5.6.49
20N10-ASEMI中低压MOS管20N10
Illustrated with pictures and texts -- wechat applet to obtain the user's geographic location information and call Tencent map API to obtain the user's specific location
HMS core machine learning service ID card identification function to achieve efficient information entry
STM32cubeMX之 uart问题汇总
Is the Huatai Securities account given by qiniu school true? Is it safe to open an account
5 best practices for perfect security code auditing
harmonyos培訓一
Devsecops: ten things that should be done well
STM32开发之 VS Code + GDB下载调试
工厂模式实现
电源老化测试系统定制|充电桩自动化测试系统NSAT-8000概述