debug_printf.c 1.4 KB

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