debug.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : debug.c
  3. * Author : WCH
  4. * Version : V1.0.0
  5. * Date : 2020/04/30
  6. * Description : This file contains all the functions prototypes for UART
  7. * Printf , Delay functions.
  8. *******************************************************************************/
  9. #include "debug.h"
  10. static uint8_t p_us=0;
  11. static uint16_t p_ms=0;
  12. /*******************************************************************************
  13. * Function Name : Delay_Init
  14. * Description : Initializes Delay Funcation.
  15. * Input : None
  16. * Return : None
  17. *******************************************************************************/
  18. void Delay_Init(void)
  19. {
  20. p_us=SystemCoreClock/8000000;
  21. p_ms=(uint16_t)p_us*1000;
  22. }
  23. /*******************************************************************************
  24. * Function Name : Delay_Us
  25. * Description : Microsecond Delay Time.
  26. * Input : n£ºMicrosecond number.
  27. * Return : None
  28. *******************************************************************************/
  29. void Delay_Us(uint32_t n)
  30. {
  31. uint32_t i;
  32. SysTick->CTLR = 0;
  33. i = (uint32_t)n*p_us;
  34. SysTick->CNTL0 = 0;
  35. SysTick->CNTL1 = 0;
  36. SysTick->CNTL2 = 0;
  37. SysTick->CNTL3 = 0;
  38. SysTick->CTLR = 1;
  39. while((*(__IO uint32_t*)0xE000F004) <= i);
  40. }
  41. /*******************************************************************************
  42. * Function Name : Delay_Ms
  43. * Description : Millisecond Delay Time.
  44. * Input : n£ºMillisecond number.
  45. * Return : None
  46. *******************************************************************************/
  47. void Delay_Ms (uint32_t n)
  48. {
  49. uint32_t i;
  50. SysTick->CTLR = 0;
  51. i = (uint32_t)n*p_ms;
  52. SysTick->CNTL0 = 0;
  53. SysTick->CNTL1 = 0;
  54. SysTick->CNTL2 = 0;
  55. SysTick->CNTL3 = 0;
  56. SysTick->CTLR = 1;
  57. while((*(__IO uint32_t*)0xE000F004) <= i);
  58. }
  59. /*******************************************************************************
  60. * Function Name : USART_Printf_Init
  61. * Description : Initializes the USARTx peripheral.
  62. * Input : baudrate: USART communication baud rate.
  63. * Return : None
  64. *******************************************************************************/
  65. void USART_Printf_Init(uint32_t baudrate)
  66. {
  67. GPIO_InitTypeDef GPIO_InitStructure;
  68. USART_InitTypeDef USART_InitStructure;
  69. #if (DEBUG == DEBUG_UART1)
  70. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
  71. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  72. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  73. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  74. GPIO_Init(GPIOA, &GPIO_InitStructure);
  75. #elif (DEBUG == DEBUG_UART2)
  76. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  77. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  78. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  79. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  80. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  81. GPIO_Init(GPIOA, &GPIO_InitStructure);
  82. #elif (DEBUG == DEBUG_UART3)
  83. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  84. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  85. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  86. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  87. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  88. GPIO_Init(GPIOB, &GPIO_InitStructure);
  89. #endif
  90. USART_InitStructure.USART_BaudRate = baudrate;
  91. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  92. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  93. USART_InitStructure.USART_Parity = USART_Parity_No;
  94. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  95. USART_InitStructure.USART_Mode = USART_Mode_Tx;
  96. #if (DEBUG == DEBUG_UART1)
  97. USART_Init(USART1, &USART_InitStructure);
  98. USART_Cmd(USART1, ENABLE);
  99. #elif (DEBUG == DEBUG_UART2)
  100. USART_Init(USART2, &USART_InitStructure);
  101. USART_Cmd(USART2, ENABLE);
  102. #elif (DEBUG == DEBUG_UART3)
  103. USART_Init(USART3, &USART_InitStructure);
  104. USART_Cmd(USART3, ENABLE);
  105. #endif
  106. }
  107. /*******************************************************************************
  108. * Function Name : _write
  109. * Description : Support Printf Function
  110. * Input : *buf: UART send Data.
  111. * size: Data length
  112. * Return : size: Data length
  113. *******************************************************************************/
  114. int _write(int fd, char *buf, int size)
  115. {
  116. int i;
  117. for(i=0; i<size; i++)
  118. {
  119. #if (DEBUG == DEBUG_UART1)
  120. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  121. USART_SendData(USART1, *buf++);
  122. #elif (DEBUG == DEBUG_UART2)
  123. while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
  124. USART_SendData(USART2, *buf++);
  125. #elif (DEBUG == DEBUG_UART3)
  126. while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
  127. USART_SendData(USART3, *buf++);
  128. #endif
  129. }
  130. return size;
  131. }