开发环境:
Ubuntu16.04 + SW4STM32 + STM32CubeMX + STM32F103C8T6
说明:scanf()函数有个bug,那就是当我们想要获取一个int时,如果输入了非数字,就会导致scanf()永远获取不到用户的输入,除非重启mcu。
STM32CubeMX配置:
main.c中添加:
/* With GCC, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#define GETCHAR_PROTOTYPE int __io_getchar(FILE *f)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#define GETCHAR_PROTOTYPE int fgetc(FILE *f)
#endif /* __GNUC__ */
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART2 and Loop until the end of transmission */
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
GETCHAR_PROTOTYPE
{
uint8_t ch = 0;
HAL_UART_Receive(&huart1,(uint8_t *)&ch, 1, 0xFFFF);
if (ch == '\r')
{
__io_putchar('\r');
ch = '\n';
}
return __io_putchar(ch);
// return ch;
}
复制SW4STM32安装目录中的syscalls.c到自己的project,并按如下修改"__read()"函数。
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
- *ptr++ = __io_getchar();
+ *ptr = __io_getchar();
+ if (*ptr++ == '\n') break;
}
-return len;
+ return ++DataIdx;
}