基于visual studio 2017 以及cubemx 搭建stm32的开发环境(2)

主要解决 vs2017中,printf无法打印数据的问题。

在keil环境下正常使用printf功能,但是以下的重定向代码在vs2017下使用不了:

#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/**
  * @brief  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 EVAL_COM1 and Loop until the end of transmission */
  HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
  return ch;
}

vs2017中将以下代码添加入,不需要添加上述在keil中的代码了:

int _write (int fd, char *ptr, int len)
{
 HAL_UART_Transmit(&huart1, (uint8_t*) ptr, len, 0xFFFF);
 return len;
}
int _read (int fd, char *ptr, int len)
{
 *ptr = 0x00; // Flush the character buffer
 HAL_UART_Receive(&huart1, (uint8_t*) ptr, 1, 0xFFFF);
 return 1;
}

--------------------------------------------------------------------------

开发板:正点原子的stm32f407ZGT6

调试器:JLINK

使用软件:cubemx,visual studio 2017

 个人源码如下:

链接:https://pan.baidu.com/s/1MDLE0TCcdZxTgvOvqYduVA

密码:vfg7

参考以下网址:

http://www.stm32cube.com/question/668

猜你喜欢

转载自www.cnblogs.com/sshbit/p/10375647.html