当前位置:网站首页>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 .


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 :

 Insert picture description here


Four 、 To be continued ...

原网站

版权声明
本文为[Summer foam and light rain]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211158514706.html