debug_printf.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // 使用uart0来进行调试日子的打印
  2. #if 0
  3. #include "stm32f4xx.h"
  4. #include <stdio.h>
  5. #define DEBUG_PRINTF_UART USART1 /* USART1 */
  6. #ifdef __CC_ARM
  7. #pragma import(__use_no_semihosting)
  8. // 标准库需要的支持函数
  9. struct __FILE
  10. {
  11. int handle;
  12. };
  13. FILE __stdout;
  14. // 定义_sys_exit()以避免使用半主机模式
  15. void _sys_exit(int x) { x = x; }
  16. // 重定义fputc函数
  17. int fputc(int ch, FILE *f)
  18. {
  19. // 这是一个完整的正确的串口发送一个字节的写法,
  20. // 不会出现第一个或最后一个字节发送不出来的情况(putstring的时候经常出现最后一个字节发送不出来)
  21. while ((DEBUG_PRINTF_UART->SR & 0X80) == 0)
  22. ; // 等待发送寄存器为空
  23. DEBUG_PRINTF_UART->DR = (u8)ch; // 发送数据
  24. while ((DEBUG_PRINTF_UART->SR & 0X40) == 0)
  25. ; // 等待发送完成
  26. return ch;
  27. }
  28. #elif defined __GNUC__
  29. int _write(int file, char *ptr, int len)
  30. {
  31. int DataIdx;
  32. for (DataIdx = 0; DataIdx < len; DataIdx++)
  33. {
  34. while ((DEBUG_PRINTF_UART->SR & 0X80) == 0)
  35. ; // 等待发送寄存器为空
  36. DEBUG_PRINTF_UART->DR = (u8) * (ptr + DataIdx); // 发送数据
  37. while ((DEBUG_PRINTF_UART->SR & 0X40) == 0)
  38. ; // 等待发送完成
  39. }
  40. return len;
  41. }
  42. void _read(void)
  43. {
  44. }
  45. void _close(void)
  46. {
  47. }
  48. void _lseek(void)
  49. {
  50. }
  51. #endif
  52. #endif