debug.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. * 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->CTLR = 0;
  39. i = (uint32_t)n * p_us;
  40. SysTick->CNTL0 = 0;
  41. SysTick->CNTL1 = 0;
  42. SysTick->CNTL2 = 0;
  43. SysTick->CNTL3 = 0;
  44. SysTick->CTLR = 1;
  45. while((*(__IO uint32_t *)0xE000F004) <= i)
  46. {
  47. ;
  48. }
  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->CTLR = 0;
  63. i = (uint32_t)n * p_ms;
  64. SysTick->CNTL0 = 0;
  65. SysTick->CNTL1 = 0;
  66. SysTick->CNTL2 = 0;
  67. SysTick->CNTL3 = 0;
  68. SysTick->CTLR = 1;
  69. while((*(__IO uint32_t *)0xE000F004) <= i) ;
  70. }
  71. /*********************************************************************
  72. * @fn USART_Printf_Init
  73. *
  74. * @brief Initializes the USARTx peripheral.
  75. *
  76. * @param baudrate - USART communication baud rate.
  77. *
  78. * @return None
  79. */
  80. void USART_Printf_Init(uint32_t baudrate)
  81. {
  82. GPIO_InitTypeDef GPIO_InitStructure;
  83. USART_InitTypeDef USART_InitStructure;
  84. #if(DEBUG == DEBUG_UART1)
  85. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
  86. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  87. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  88. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  89. GPIO_Init(GPIOA, &GPIO_InitStructure);
  90. #elif(DEBUG == DEBUG_UART2)
  91. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  92. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  93. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  94. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  95. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  96. GPIO_Init(GPIOA, &GPIO_InitStructure);
  97. #elif(DEBUG == DEBUG_UART3)
  98. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  99. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  100. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  101. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  102. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  103. GPIO_Init(GPIOB, &GPIO_InitStructure);
  104. #endif
  105. USART_InitStructure.USART_BaudRate = baudrate;
  106. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  107. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  108. USART_InitStructure.USART_Parity = USART_Parity_No;
  109. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  110. USART_InitStructure.USART_Mode = USART_Mode_Tx;
  111. #if(DEBUG == DEBUG_UART1)
  112. USART_Init(USART1, &USART_InitStructure);
  113. USART_Cmd(USART1, ENABLE);
  114. #elif(DEBUG == DEBUG_UART2)
  115. USART_Init(USART2, &USART_InitStructure);
  116. USART_Cmd(USART2, ENABLE);
  117. #elif(DEBUG == DEBUG_UART3)
  118. USART_Init(USART3, &USART_InitStructure);
  119. USART_Cmd(USART3, ENABLE);
  120. #endif
  121. }
  122. /*********************************************************************
  123. * @fn _write
  124. *
  125. * @brief Support Printf Function
  126. *
  127. * @param *buf - UART send Data.
  128. * size - Data length.
  129. *
  130. * @return size - Data length
  131. */
  132. __attribute__((used))
  133. int _write(int fd, char *buf, int size)
  134. {
  135. int i;
  136. for(i = 0; i < size; i++){
  137. #if(DEBUG == DEBUG_UART1)
  138. while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  139. USART_SendData(USART1, *buf++);
  140. #elif(DEBUG == DEBUG_UART2)
  141. while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
  142. USART_SendData(USART2, *buf++);
  143. #elif(DEBUG == DEBUG_UART3)
  144. while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
  145. USART_SendData(USART3, *buf++);
  146. #endif
  147. }
  148. return size;
  149. }
  150. /*********************************************************************
  151. * @fn _sbrk
  152. *
  153. * @brief Change the spatial position of data segment.
  154. *
  155. * @return size: Data length
  156. */
  157. void *_sbrk(ptrdiff_t incr)
  158. {
  159. extern char _end[];
  160. extern char _heap_end[];
  161. static char *curbrk = _end;
  162. if ((curbrk + incr < _end) || (curbrk + incr > _heap_end))
  163. return NULL - 1;
  164. curbrk += incr;
  165. return curbrk - incr;
  166. }