debug.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. *********************************************************************************
  9. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  10. * Attention: This software (modified or not) and binary are used for
  11. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  12. *******************************************************************************/
  13. #include "debug.h"
  14. static uint8_t p_us = 0;
  15. static uint16_t p_ms = 0;
  16. #define DEBUG_DATA0_ADDRESS ((volatile uint32_t*)0xE0000380)
  17. #define DEBUG_DATA1_ADDRESS ((volatile uint32_t*)0xE0000384)
  18. /*********************************************************************
  19. * @fn Delay_Init
  20. *
  21. * @brief Initializes Delay Funcation.
  22. *
  23. * @return none
  24. */
  25. void Delay_Init(void)
  26. {
  27. p_us = SystemCoreClock / 8000000;
  28. p_ms = (uint16_t)p_us * 1000;
  29. }
  30. /*********************************************************************
  31. * @fn Delay_Us
  32. *
  33. * @brief Microsecond Delay Time.
  34. *
  35. * @param n - Microsecond number.
  36. *
  37. * @return None
  38. */
  39. void Delay_Us(uint32_t n)
  40. {
  41. uint32_t i;
  42. SysTick->SR &= ~(1 << 0);
  43. i = (uint32_t)n * p_us;
  44. SysTick->CMP = i;
  45. SysTick->CTLR |= (1 << 4);
  46. SysTick->CTLR |= (1 << 5) | (1 << 0);
  47. while((SysTick->SR & (1 << 0)) != (1 << 0))
  48. ;
  49. SysTick->CTLR &= ~(1 << 0);
  50. }
  51. /*********************************************************************
  52. * @fn Delay_Ms
  53. *
  54. * @brief Millisecond Delay Time.
  55. *
  56. * @param n - Millisecond number.
  57. *
  58. * @return None
  59. */
  60. void Delay_Ms(uint32_t n)
  61. {
  62. uint32_t i;
  63. SysTick->SR &= ~(1 << 0);
  64. i = (uint32_t)n * p_ms;
  65. SysTick->CMP = i;
  66. SysTick->CTLR |= (1 << 4);
  67. SysTick->CTLR |= (1 << 5) | (1 << 0);
  68. while((SysTick->SR & (1 << 0)) != (1 << 0))
  69. ;
  70. SysTick->CTLR &= ~(1 << 0);
  71. }
  72. /*********************************************************************
  73. * @fn USART_Printf_Init
  74. *
  75. * @brief Initializes the USARTx peripheral.
  76. *
  77. * @param baudrate - USART communication baud rate.
  78. *
  79. * @return None
  80. */
  81. void USART_Printf_Init(uint32_t baudrate)
  82. {
  83. GPIO_InitTypeDef GPIO_InitStructure;
  84. USART_InitTypeDef USART_InitStructure;
  85. #if(DEBUG == DEBUG_UART1)
  86. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
  87. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  88. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  89. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  90. GPIO_Init(GPIOA, &GPIO_InitStructure);
  91. #elif(DEBUG == DEBUG_UART2)
  92. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  93. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  94. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  95. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  96. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  97. GPIO_Init(GPIOA, &GPIO_InitStructure);
  98. #elif(DEBUG == DEBUG_UART3)
  99. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  100. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  101. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  102. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  103. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  104. GPIO_Init(GPIOB, &GPIO_InitStructure);
  105. #elif(DEBUG == DEBUG_UART7)
  106. RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART7, ENABLE);
  107. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  108. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  109. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  110. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  111. GPIO_Init(GPIOC, &GPIO_InitStructure);
  112. #endif
  113. USART_InitStructure.USART_BaudRate = baudrate;
  114. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  115. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  116. USART_InitStructure.USART_Parity = USART_Parity_No;
  117. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  118. USART_InitStructure.USART_Mode = USART_Mode_Tx;
  119. #if(DEBUG == DEBUG_UART1)
  120. USART_Init(USART1, &USART_InitStructure);
  121. USART_Cmd(USART1, ENABLE);
  122. #elif(DEBUG == DEBUG_UART2)
  123. USART_Init(USART2, &USART_InitStructure);
  124. USART_Cmd(USART2, ENABLE);
  125. #elif(DEBUG == DEBUG_UART3)
  126. USART_Init(USART3, &USART_InitStructure);
  127. USART_Cmd(USART3, ENABLE);
  128. #elif(DEBUG == DEBUG_UART7)
  129. USART_Init(UART7, &USART_InitStructure);
  130. USART_Cmd(UART7, ENABLE);
  131. #endif
  132. }
  133. /*********************************************************************
  134. * @fn SDI_Printf_Enable
  135. *
  136. * @brief Initializes the SDI printf Function.
  137. *
  138. * @param None
  139. *
  140. * @return None
  141. */
  142. void SDI_Printf_Enable(void)
  143. {
  144. *(DEBUG_DATA0_ADDRESS) = 0;
  145. Delay_Init();
  146. Delay_Ms(1);
  147. }
  148. /*********************************************************************
  149. * @fn _write
  150. *
  151. * @brief Support Printf Function
  152. *
  153. * @param *buf - UART send Data.
  154. * size - Data length
  155. *
  156. * @return size: Data length
  157. */
  158. __attribute__((used)) int _write(int fd, char *buf, int size)
  159. {
  160. int i = 0;
  161. #if (SDI_PRINT == SDI_PR_OPEN)
  162. int writeSize = size;
  163. do
  164. {
  165. /**
  166. * data0 data1 8 bytes
  167. * data0 The lowest byte storage length, the maximum is 7
  168. *
  169. */
  170. while( (*(DEBUG_DATA0_ADDRESS) != 0u))
  171. {
  172. }
  173. if(writeSize>7)
  174. {
  175. *(DEBUG_DATA1_ADDRESS) = (*(buf+i+3)) | (*(buf+i+4)<<8) | (*(buf+i+5)<<16) | (*(buf+i+6)<<24);
  176. *(DEBUG_DATA0_ADDRESS) = (7u) | (*(buf+i)<<8) | (*(buf+i+1)<<16) | (*(buf+i+2)<<24);
  177. i += 7;
  178. writeSize -= 7;
  179. }
  180. else
  181. {
  182. *(DEBUG_DATA1_ADDRESS) = (*(buf+i+3)) | (*(buf+i+4)<<8) | (*(buf+i+5)<<16) | (*(buf+i+6)<<24);
  183. *(DEBUG_DATA0_ADDRESS) = (writeSize) | (*(buf+i)<<8) | (*(buf+i+1)<<16) | (*(buf+i+2)<<24);
  184. writeSize = 0;
  185. }
  186. } while (writeSize);
  187. #else
  188. for(i = 0; i < size; i++)
  189. {
  190. #if(DEBUG == DEBUG_UART1)
  191. while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  192. USART_SendData(USART1, *buf++);
  193. #elif(DEBUG == DEBUG_UART2)
  194. while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
  195. USART_SendData(USART2, *buf++);
  196. #elif(DEBUG == DEBUG_UART3)
  197. while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
  198. USART_SendData(USART3, *buf++);
  199. #elif(DEBUG == DEBUG_UART7)
  200. while(USART_GetFlagStatus(UART7, USART_FLAG_TC) == RESET);
  201. USART_SendData(UART7, *buf++);
  202. #endif
  203. }
  204. #endif
  205. return size;
  206. }
  207. /*********************************************************************
  208. * @fn _sbrk
  209. *
  210. * @brief Change the spatial position of data segment.
  211. *
  212. * @return size: Data length
  213. */
  214. __attribute__((used)) void *_sbrk(ptrdiff_t incr)
  215. {
  216. extern char _end[];
  217. extern char _heap_end[];
  218. static char *curbrk = _end;
  219. if ((curbrk + incr < _end) || (curbrk + incr > _heap_end))
  220. return NULL - 1;
  221. curbrk += incr;
  222. return curbrk - incr;
  223. }