debug.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. SysTick->CTLR &= ~(1 << 0);
  49. }
  50. /*********************************************************************
  51. * @fn Delay_Ms
  52. *
  53. * @brief Millisecond Delay Time.
  54. *
  55. * @param n - Millisecond number.
  56. *
  57. * @return None
  58. */
  59. void Delay_Ms(uint32_t n)
  60. {
  61. uint32_t i;
  62. SysTick->SR &= ~(1 << 0);
  63. i = (uint32_t)n * p_ms;
  64. SysTick->CMP = i;
  65. SysTick->CTLR |= (1 << 4);
  66. SysTick->CTLR |= (1 << 5) | (1 << 0);
  67. while((SysTick->SR & (1 << 0)) != (1 << 0));
  68. SysTick->CTLR &= ~(1 << 0);
  69. }
  70. /*********************************************************************
  71. * @fn USART_Printf_Init
  72. *
  73. * @brief Initializes the USARTx peripheral.
  74. *
  75. * @param baudrate - USART communication baud rate.
  76. *
  77. * @return None
  78. */
  79. void USART_Printf_Init(uint32_t baudrate)
  80. {
  81. GPIO_InitTypeDef GPIO_InitStructure;
  82. USART_InitTypeDef USART_InitStructure;
  83. #if(DEBUG == DEBUG_UART1)
  84. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
  85. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  86. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  87. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  88. GPIO_Init(GPIOA, &GPIO_InitStructure);
  89. #elif(DEBUG == DEBUG_UART2)
  90. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  91. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  92. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  93. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  94. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  95. GPIO_Init(GPIOA, &GPIO_InitStructure);
  96. #elif(DEBUG == DEBUG_UART3)
  97. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  98. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  99. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  100. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  101. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  102. GPIO_Init(GPIOB, &GPIO_InitStructure);
  103. #endif
  104. USART_InitStructure.USART_BaudRate = baudrate;
  105. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  106. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  107. USART_InitStructure.USART_Parity = USART_Parity_No;
  108. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  109. USART_InitStructure.USART_Mode = USART_Mode_Tx;
  110. #if(DEBUG == DEBUG_UART1)
  111. USART_Init(USART1, &USART_InitStructure);
  112. USART_Cmd(USART1, ENABLE);
  113. #elif(DEBUG == DEBUG_UART2)
  114. USART_Init(USART2, &USART_InitStructure);
  115. USART_Cmd(USART2, ENABLE);
  116. #elif(DEBUG == DEBUG_UART3)
  117. USART_Init(USART3, &USART_InitStructure);
  118. USART_Cmd(USART3, ENABLE);
  119. #endif
  120. }
  121. /*********************************************************************
  122. * @fn SDI_Printf_Enable
  123. *
  124. * @brief Initializes the SDI printf Function.
  125. *
  126. * @param None
  127. *
  128. * @return None
  129. */
  130. void SDI_Printf_Enable(void)
  131. {
  132. *(DEBUG_DATA0_ADDRESS) = 0;
  133. Delay_Init();
  134. Delay_Ms(1);
  135. }
  136. /*********************************************************************
  137. * @fn _write
  138. *
  139. * @brief Support Printf Function
  140. *
  141. * @param *buf - UART send Data.
  142. * size - Data length
  143. *
  144. * @return size: Data length
  145. */
  146. __attribute__((used))
  147. int _write(int fd, char *buf, int size)
  148. {
  149. int i = 0;
  150. #if (SDI_PRINT == SDI_PR_OPEN)
  151. int writeSize = size;
  152. do
  153. {
  154. /**
  155. * data0 data1 8 byte
  156. * data0 The storage length of the lowest byte, with a maximum of 7 bytes.
  157. */
  158. while( (*(DEBUG_DATA0_ADDRESS) != 0u))
  159. {
  160. }
  161. if(writeSize>7)
  162. {
  163. *(DEBUG_DATA1_ADDRESS) = (*(buf+i+3)) | (*(buf+i+4)<<8) | (*(buf+i+5)<<16) | (*(buf+i+6)<<24);
  164. *(DEBUG_DATA0_ADDRESS) = (7u) | (*(buf+i)<<8) | (*(buf+i+1)<<16) | (*(buf+i+2)<<24);
  165. i += 7;
  166. writeSize -= 7;
  167. }
  168. else
  169. {
  170. *(DEBUG_DATA1_ADDRESS) = (*(buf+i+3)) | (*(buf+i+4)<<8) | (*(buf+i+5)<<16) | (*(buf+i+6)<<24);
  171. *(DEBUG_DATA0_ADDRESS) = (writeSize) | (*(buf+i)<<8) | (*(buf+i+1)<<16) | (*(buf+i+2)<<24);
  172. writeSize = 0;
  173. }
  174. } while (writeSize);
  175. #else
  176. for(i = 0; i < size; i++){
  177. #if(DEBUG == DEBUG_UART1)
  178. while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  179. USART_SendData(USART1, *buf++);
  180. #elif(DEBUG == DEBUG_UART2)
  181. while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
  182. USART_SendData(USART2, *buf++);
  183. #elif(DEBUG == DEBUG_UART3)
  184. while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
  185. USART_SendData(USART3, *buf++);
  186. #endif
  187. }
  188. #endif
  189. return size;
  190. }
  191. /*********************************************************************
  192. * @fn _sbrk
  193. *
  194. * @brief Change the spatial position of data segment.
  195. *
  196. * @return size: Data length
  197. */
  198. __attribute__((used))
  199. void *_sbrk(ptrdiff_t incr)
  200. {
  201. extern char _end[];
  202. extern char _heap_end[];
  203. static char *curbrk = _end;
  204. if ((curbrk + incr < _end) || (curbrk + incr > _heap_end))
  205. return NULL - 1;
  206. curbrk += incr;
  207. return curbrk - incr;
  208. }