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
13 static uint8_t p_us = 0;
14 static uint16_t p_ms = 0;
15
16 /*********************************************************************
17 * @fn Delay_Init
18 *
19 * @brief Initializes Delay Funcation.
20 *
21 * @return none
22 */
Delay_Init(void)23 void Delay_Init(void)
24 {
25 p_us = SystemCoreClock / 8000000;
26 p_ms = (uint16_t)p_us * 1000;
27 }
28
29 /*********************************************************************
30 * @fn Delay_Us
31 *
32 * @brief Microsecond Delay Time.
33 *
34 * @param n - Microsecond number.
35 *
36 * @return None
37 */
Delay_Us(uint32_t n)38 void Delay_Us(uint32_t n)
39 {
40 uint32_t i;
41
42 SysTick->CTLR = 0;
43 i = (uint32_t)n * p_us;
44
45 SysTick->CNTL0 = 0;
46 SysTick->CNTL1 = 0;
47 SysTick->CNTL2 = 0;
48 SysTick->CNTL3 = 0;
49 SysTick->CTLR = 1;
50
51 while((*(__IO uint32_t *)0xE000F004) <= i)
52 {
53 ;
54 }
55
56 }
57
58 /*********************************************************************
59 * @fn Delay_Ms
60 *
61 * @brief Millisecond Delay Time.
62 *
63 * @param n - Millisecond number.
64 *
65 * @return None
66 */
Delay_Ms(uint32_t n)67 void Delay_Ms(uint32_t n)
68 {
69 uint32_t i;
70
71 SysTick->CTLR = 0;
72 i = (uint32_t)n * p_ms;
73
74 SysTick->CNTL0 = 0;
75 SysTick->CNTL1 = 0;
76 SysTick->CNTL2 = 0;
77 SysTick->CNTL3 = 0;
78 SysTick->CTLR = 1;
79
80 while((*(__IO uint32_t *)0xE000F004) <= i) ;
81 }
82
83 /*********************************************************************
84 * @fn USART_Printf_Init
85 *
86 * @brief Initializes the USARTx peripheral.
87 *
88 * @param baudrate - USART communication baud rate.
89 *
90 * @return None
91 */
USART_Printf_Init(uint32_t baudrate)92 void USART_Printf_Init(uint32_t baudrate)
93 {
94 GPIO_InitTypeDef GPIO_InitStructure;
95 USART_InitTypeDef USART_InitStructure;
96
97 #if(DEBUG == DEBUG_UART1)
98 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
99
100 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
101 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
102 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
103 GPIO_Init(GPIOA, &GPIO_InitStructure);
104
105 #elif(DEBUG == DEBUG_UART2)
106 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
107 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
108
109 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
110 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
111 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
112 GPIO_Init(GPIOA, &GPIO_InitStructure);
113
114 #elif(DEBUG == DEBUG_UART3)
115 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
116 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
117
118 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
119 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
120 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
121 GPIO_Init(GPIOB, &GPIO_InitStructure);
122
123 #endif
124
125 USART_InitStructure.USART_BaudRate = baudrate;
126 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
127 USART_InitStructure.USART_StopBits = USART_StopBits_1;
128 USART_InitStructure.USART_Parity = USART_Parity_No;
129 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
130 USART_InitStructure.USART_Mode = USART_Mode_Tx;
131
132 #if(DEBUG == DEBUG_UART1)
133 USART_Init(USART1, &USART_InitStructure);
134 USART_Cmd(USART1, ENABLE);
135
136 #elif(DEBUG == DEBUG_UART2)
137 USART_Init(USART2, &USART_InitStructure);
138 USART_Cmd(USART2, ENABLE);
139
140 #elif(DEBUG == DEBUG_UART3)
141 USART_Init(USART3, &USART_InitStructure);
142 USART_Cmd(USART3, ENABLE);
143
144 #endif
145 }
146
147 /*********************************************************************
148 * @fn _write
149 *
150 * @brief Support Printf Function
151 *
152 * @param *buf - UART send Data.
153 * size - Data length.
154 *
155 * @return size - Data length
156 */
157 __attribute__((used))
_write(int fd,char * buf,int size)158 int _write(int fd, char *buf, int size)
159 {
160 int i;
161
162 for(i = 0; i < size; i++){
163 #if(DEBUG == DEBUG_UART1)
164 while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
165 USART_SendData(USART1, *buf++);
166 #elif(DEBUG == DEBUG_UART2)
167 while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
168 USART_SendData(USART2, *buf++);
169 #elif(DEBUG == DEBUG_UART3)
170 while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
171 USART_SendData(USART3, *buf++);
172 #endif
173 }
174
175 return size;
176 }
177
178 /*********************************************************************
179 * @fn _sbrk
180 *
181 * @brief Change the spatial position of data segment.
182 *
183 * @return size: Data length
184 */
_sbrk(ptrdiff_t incr)185 void *_sbrk(ptrdiff_t incr)
186 {
187 extern char _end[];
188 extern char _heap_end[];
189 static char *curbrk = _end;
190
191 if ((curbrk + incr < _end) || (curbrk + incr > _heap_end))
192 return NULL - 1;
193
194 curbrk += incr;
195 return curbrk - incr;
196 }
197
198
199
200