debug.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : debug.c
  3. * Author : WCH
  4. * Version : V1.0.0
  5. * Date : 2021/06/06
  6. * Description : This file contains all the functions prototypes for UART
  7. * Printf , Delay functions.
  8. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  9. * SPDX-License-Identifier: Apache-2.0
  10. *******************************************************************************/
  11. #include "debug.h"
  12. static uint8_t p_us = 0;
  13. static uint16_t p_ms = 0;
  14. /*********************************************************************
  15. * @fn Delay_Init
  16. *
  17. * @brief Initializes Delay Funcation.
  18. *
  19. * @return none
  20. */
  21. void Delay_Init(void)
  22. {
  23. p_us = SystemCoreClock / 8000000;
  24. p_ms = (uint16_t)p_us * 1000;
  25. }
  26. /*********************************************************************
  27. * @fn Delay_Us
  28. *
  29. * @brief Microsecond Delay Time.
  30. *
  31. * @param n - Microsecond number.
  32. *
  33. * @return None
  34. */
  35. void Delay_Us(uint32_t n)
  36. {
  37. uint32_t i;
  38. SysTick->SR &= ~(1 << 0);
  39. i = (uint32_t)n * p_us;
  40. SysTick->CMP = i;
  41. SysTick->CTLR |= (1 << 4) | (1 << 5) | (1 << 0);
  42. while((SysTick->SR & (1 << 0)) != (1 << 0))
  43. ;
  44. SysTick->CTLR &= ~(1 << 0);
  45. }
  46. /*********************************************************************
  47. * @fn Delay_Ms
  48. *
  49. * @brief Millisecond Delay Time.
  50. *
  51. * @param n - Millisecond number.
  52. *
  53. * @return None
  54. */
  55. void Delay_Ms(uint32_t n)
  56. {
  57. uint32_t i;
  58. SysTick->SR &= ~(1 << 0);
  59. i = (uint32_t)n * p_ms;
  60. SysTick->CMP = i;
  61. SysTick->CTLR |= (1 << 4) | (1 << 5) | (1 << 0);
  62. while((SysTick->SR & (1 << 0)) != (1 << 0))
  63. ;
  64. SysTick->CTLR &= ~(1 << 0);
  65. }
  66. /*********************************************************************
  67. * @fn USART_Printf_Init
  68. *
  69. * @brief Initializes the USARTx peripheral.
  70. *
  71. * @param baudrate - USART communication baud rate.
  72. *
  73. * @return None
  74. */
  75. void USART_Printf_Init(uint32_t baudrate)
  76. {
  77. GPIO_InitTypeDef GPIO_InitStructure;
  78. USART_InitTypeDef USART_InitStructure;
  79. #if(DEBUG == DEBUG_UART1)
  80. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
  81. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  82. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  83. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  84. GPIO_Init(GPIOA, &GPIO_InitStructure);
  85. #elif(DEBUG == DEBUG_UART2)
  86. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  87. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  88. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  89. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  90. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  91. GPIO_Init(GPIOA, &GPIO_InitStructure);
  92. #elif(DEBUG == DEBUG_UART3)
  93. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  94. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  95. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  96. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  97. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  98. GPIO_Init(GPIOB, &GPIO_InitStructure);
  99. #endif
  100. USART_InitStructure.USART_BaudRate = baudrate;
  101. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  102. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  103. USART_InitStructure.USART_Parity = USART_Parity_No;
  104. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  105. USART_InitStructure.USART_Mode = USART_Mode_Tx;
  106. #if(DEBUG == DEBUG_UART1)
  107. USART_Init(USART1, &USART_InitStructure);
  108. USART_Cmd(USART1, ENABLE);
  109. #elif(DEBUG == DEBUG_UART2)
  110. USART_Init(USART2, &USART_InitStructure);
  111. USART_Cmd(USART2, ENABLE);
  112. #elif(DEBUG == DEBUG_UART3)
  113. USART_Init(USART3, &USART_InitStructure);
  114. USART_Cmd(USART3, ENABLE);
  115. #endif
  116. }
  117. /*********************************************************************
  118. * @fn _write
  119. *
  120. * @brief Support Printf Function
  121. *
  122. * @param *buf - UART send Data.
  123. * size - Data length
  124. *
  125. * @return size: Data length
  126. */
  127. __attribute__((used)) int _write(int fd, char *buf, int size)
  128. {
  129. int i;
  130. for(i = 0; i < size; i++)
  131. {
  132. #if(DEBUG == DEBUG_UART1)
  133. while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  134. USART_SendData(USART1, *buf++);
  135. #elif(DEBUG == DEBUG_UART2)
  136. while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
  137. USART_SendData(USART2, *buf++);
  138. #elif(DEBUG == DEBUG_UART3)
  139. while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
  140. USART_SendData(USART3, *buf++);
  141. #endif
  142. }
  143. return size;
  144. }
  145. /*********************************************************************
  146. * @fn _sbrk
  147. *
  148. * @brief Change the spatial position of data segment.
  149. *
  150. * @return size: Data length
  151. */
  152. void *_sbrk(ptrdiff_t incr)
  153. {
  154. extern char _end[];
  155. extern char _heap_end[];
  156. static char *curbrk = _end;
  157. if ((curbrk + incr < _end) || (curbrk + incr > _heap_end))
  158. return NULL - 1;
  159. curbrk += incr;
  160. return curbrk - incr;
  161. }