]> git.sur5r.net Git - freertos/blob - Demo/ARM7_STR71x_IAR/Library/uart.c
First version under SVN is V4.0.1
[freertos] / Demo / ARM7_STR71x_IAR / Library / uart.c
1 /******************** (C) COPYRIGHT 2003 STMicroelectronics ********************\r
2 * File Name          : uart.c\r
3 * Author             : MCD Application Team\r
4 * Date First Issued  : 06/08/2003\r
5 * Description        : This file provides all the UART software functions\r
6 ********************************************************************************\r
7 * History:\r
8 *  30/11/2004 : V2.0\r
9 *  14/07/2004 : V1.3\r
10 *  01/01/2004 : V1.2\r
11 *******************************************************************************\r
12  THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH\r
13  CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.\r
14  AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT\r
15  OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT\r
16  OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION\r
17  CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
18 *******************************************************************************/\r
19 \r
20 #include "uart.h"\r
21 \r
22 /*******************************************************************************\r
23 * Function Name  : UART_Init\r
24 * Description    : This function initializes the selected UART.\r
25 * Input 1        : UARTx (x can be 0,1, 2 or 3) the desired UART\r
26 * Output         : None\r
27 * Return         : None\r
28 *******************************************************************************/\r
29 void UART_Init(UART_TypeDef *UARTx)\r
30 {\r
31   UARTx->IER = 0x00;\r
32   UARTx->CR = 0x00;\r
33   (void)UARTx->RxBUFR;\r
34   UARTx->RxRSTR = 0xFFFF;\r
35   UARTx->TxRSTR = 0xFFFF;\r
36 }\r
37 \r
38 /*******************************************************************************\r
39 * Function Name  : UART_BaudRateConfig\r
40 * Description    : This function configures the baud rate of the selected UART.\r
41 * Input 1        : UARTx (x can be 0,1, 2 or 3) the desired UART\r
42 * Input 2        : The baudrate value\r
43 * Output         : None\r
44 * Return         : None\r
45 *******************************************************************************/\r
46 void UART_BaudRateConfig(UART_TypeDef *UARTx, u32 BaudRate)\r
47 {\r
48   UARTx->BR = (u16)(RCCU_FrequencyValue(RCCU_FCLK)/(16*BaudRate));\r
49 }\r
50 \r
51 /*******************************************************************************\r
52 * Function Name  : UART_Config\r
53 * Description    : This function configures the baudrate, the mode, the data\r
54 *                  parity and the number of stop bits of the selected UART.\r
55 * Input 1        : UARTx (x can be 0,1, 2 or 3) the desired UART\r
56 * Input 2        : The baudrate value\r
57 * Input 3        : The parity type\r
58 * Input 4        : The number of stop bits\r
59 * Input 5        : The UART mode\r
60 * Output         : None\r
61 * Return         : None\r
62 *******************************************************************************/\r
63 void UART_Config(UART_TypeDef *UARTx, u32 BaudRate, UARTParity_TypeDef Parity,\r
64                  UARTStopBits_TypeDef StopBits, UARTMode_TypeDef Mode)\r
65 {\r
66   UART_ModeConfig(UARTx, Mode);\r
67   UART_BaudRateConfig(UARTx, BaudRate);\r
68   UART_ParityConfig(UARTx, Parity);\r
69   UART_StopBitsConfig(UARTx, StopBits);\r
70 }\r
71 \r
72 /*******************************************************************************\r
73 * Function Name  : UART_ItConfig\r
74 * Description    : This function enables or disables the interrupts of the\r
75 *                  selected UART.\r
76 * Input 1        : UARTx (x can be 0,1, 2 or 3) the desired UART\r
77 * Input 2        : The new interrupt flag\r
78 * Input 3        : ENABLE or DISABLE\r
79 * Output         : None\r
80 * Return         : None\r
81 *******************************************************************************/\r
82 void UART_ItConfig(UART_TypeDef *UARTx, u16 UART_Flag, FunctionalState NewState)\r
83 {\r
84   if (NewState==ENABLE) UARTx->IER|=UART_Flag; else UARTx->IER&=~UART_Flag;\r
85 }\r
86 \r
87 /*******************************************************************************\r
88 * Function Name  : UART_FifoConfig\r
89 * Description    : This function enables or disables the Rx and Tx FIFOs of\r
90 *                  the selected UART.\r
91 * Input 1        : UARTx (x can be 0,1, 2 or 3) the desired UART\r
92 * Input 2        : ENABLE or DISABLE\r
93 * Output         : None\r
94 * Return         : None\r
95 *******************************************************************************/\r
96 void UART_FifoConfig(UART_TypeDef *UARTx, FunctionalState NewState)\r
97 {\r
98   if (NewState==ENABLE) UARTx->CR|=0x0400; else UARTx->CR&=~0x0400;\r
99 }\r
100 \r
101 /*******************************************************************************\r
102 * Function Name  : UART_FifoReset\r
103 * Description    : This function resets the Rx and the Tx FIFOs of the\r
104 *                  selected UART.\r
105 * Input 1        : UARTx (x can be 0,1, 2 or 3) the desired UART\r
106 * Input 2        : UART_RxFIFO or UART_TxFIFO\r
107 * Output         : None\r
108 * Return         : None\r
109 *******************************************************************************/\r
110 void UART_FifoReset(UART_TypeDef *UARTx, UARTFIFO_TypeDef FIFO)\r
111 {\r
112   if (FIFO==UART_RxFIFO) UARTx->RxRSTR=0xFFFF; else UARTx->TxRSTR=0xFFFF;\r
113 }\r
114 \r
115 /*******************************************************************************\r
116 * Function Name  : UART_LoopBackConfig\r
117 * Description    : This function enables or disables the loop back mode of\r
118 *                  the selected UART.\r
119 * Input 1        : UARTx (x can be 0,1, 2 or 3) the desired UART\r
120 * Input 2        : ENABLE or DISABLE\r
121 * Output         : None\r
122 * Return         : None\r
123 *******************************************************************************/\r
124 void UART_LoopBackConfig(UART_TypeDef *UARTx, FunctionalState NewState)\r
125 {\r
126   if (NewState==ENABLE) UARTx->CR|=0x0040; else UARTx->CR&=~0x0040;\r
127 }\r
128 \r
129 /*******************************************************************************\r
130 * Function Name  : UART_RxConfig\r
131 * Description    : This function enables or disables the UART data reception.\r
132 * Input 1        : UARTx (x can be 0,1, 2 or 3) the desired UART\r
133 * Input 2        : ENABLE or DISABLE\r
134 * Output         : None\r
135 * Return         : None\r
136 *******************************************************************************/\r
137 void UART_RxConfig(UART_TypeDef *UARTx, FunctionalState NewState)\r
138 {\r
139   if (NewState==ENABLE) UARTx->CR|=0x0100; else UARTx->CR&=~0x0100;\r
140 }\r
141 \r
142 /*******************************************************************************\r
143 * Function Name  : UART_OnOffConfig\r
144 * Description    : This function sets On/Off the selected UART.\r
145 * Input 1        : UARTx (x can be 0,1, 2 or 3) the desired UART\r
146 * Input 2        : ENABLE or DISABLE\r
147 * Output         : None\r
148 * Return         : None\r
149 *******************************************************************************/\r
150 void UART_OnOffConfig(UART_TypeDef *UARTx, FunctionalState NewState)\r
151 {\r
152   if (NewState==ENABLE) UARTx->CR|=0x0080; else UARTx->CR&=~0x0080;\r
153 }\r
154 \r
155 /*******************************************************************************\r
156 * Function Name  : UART_ByteSend\r
157 * Description    : This function sends a data byte to the selected UART.\r
158 * Input 1        : UARTx (x can be 0,1, 2 or 3) the desired UART\r
159 * Input 2        : A pointer to the data byte to send\r
160 * Output         : None\r
161 * Return         : None\r
162 *******************************************************************************/\r
163 void UART_ByteSend(UART_TypeDef *UARTx, u8 *Data)\r
164 {\r
165   if (UARTx->CR & (0x0001<<UART_FIFOEnableBit))// if FIFO ENABLED\r
166     while((UARTx->SR & UART_TxFull)); // while the UART_TxFIFO contain 16 characters.\r
167   else                  // if FIFO DISABLED\r
168     while (!(UARTx->SR & UART_TxEmpty)); // while the transmit shift register not empty\r
169   UARTx->TxBUFR = *Data;\r
170 }\r
171 \r
172 /*******************************************************************************\r
173 * Function Name  : UART_9BitByteSend\r
174 * Description    : This function sends a 9 bits data byte to the selected UART.\r
175 * Input 1        : UARTx (x can be 0,1, 2 or 3) the desired UART\r
176 * Input 2        : A pointer to the data to send\r
177 * Output         : None\r
178 * Return         : None\r
179 *******************************************************************************/\r
180 void UART_9BitByteSend(UART_TypeDef *UARTx, u16 *Data)\r
181 {\r
182   if(UARTx->CR & (0x0001<<UART_FIFOEnableBit))// if FIFO ENABLED\r
183     while((UARTx->SR & UART_TxFull)); // while the UART_TxFIFO contain 16 characters.\r
184   else                  // if FIFO DISABLED\r
185     while (!(UARTx->SR & UART_TxEmpty)); // while the transmit shift register not empty\r
186   UARTx->TxBUFR = *Data;\r
187 }\r
188 \r
189 /*******************************************************************************\r
190 * Function Name  : UART_DataSend\r
191 * Description    : This function sends several data bytes to the selected UART.\r
192 * Input 1        : UARTx (x can be 0,1, 2 or 3) the desired UART\r
193 * Input 2        : A pointer to the data to send\r
194 * Input 3        : The data length in bytes\r
195 * Output         : None\r
196 * Return         : None\r
197 *******************************************************************************/\r
198 void UART_DataSend(UART_TypeDef *UARTx, u8 *Data, u8 DataLength)\r
199 {\r
200   while(DataLength--)\r
201   {\r
202     UART_ByteSend(UARTx,Data);\r
203     Data++;\r
204   }\r
205 }\r
206 \r
207 /*******************************************************************************\r
208 * Function Name  : UART_9BitDataSend\r
209 * Description    : This function sends several 9 bits data bytes to the selected UART.\r
210 * Input 1        : UARTx (x can be 0,1, 2 or 3) the desired UART\r
211 * Input 2        : A pointer to the data to send\r
212 * Input 3        : The data length\r
213 * Output         : None\r
214 * Return         : None\r
215 *******************************************************************************/\r
216 void UART_9BitDataSend(UART_TypeDef *UARTx, u16 *Data, u8 DataLength)\r
217 {\r
218   while(DataLength--)\r
219   {\r
220     UART_9BitByteSend(UARTx,Data);\r
221     Data++;\r
222   }\r
223 }\r
224 \r
225 /*******************************************************************************\r
226 * Function Name  : UART_StringSend\r
227 * Description    : This function sends a string to the selected UART.\r
228 * Input 1        : UARTx (x can be 0,1, 2 or 3) the desired UART\r
229 * Input 2        : A pointer to the string to send\r
230 * Output         : None\r
231 * Return         : None\r
232 *******************************************************************************/\r
233 void UART_StringSend(UART_TypeDef *UARTx, u8 *String)\r
234 {\r
235   u8 *Data=String;\r
236   while(*Data != '\0')\r
237     UART_ByteSend(UARTx, Data++);\r
238   *Data='\0';\r
239   UART_ByteSend(UARTx, Data);\r
240 }\r
241 \r
242 /*******************************************************************************\r
243 * Function Name  : UART_ByteReceive\r
244 * Description    : This function gets a data byte from the selected UART.\r
245 * Input 1        : UARTx (x can be 0,1, 2 or 3) the desired UART\r
246 * Input 2        : A pointer to the buffer where the data will be stored\r
247 * Input 3        : The time-out period\r
248 * Output         : The received data\r
249 * Return         : The UARTx.SR register contents\r
250 *******************************************************************************/\r
251 u16 UART_ByteReceive(UART_TypeDef *UARTx, u8 *Data, u8 TimeOut)\r
252 {\r
253    u16 wStatus;\r
254    UARTx->TOR=TimeOut;// reload the Timeout counter\r
255    while (!((wStatus=UARTx->SR) & (UART_TimeOutIdle|UART_RxHalfFull|UART_RxBufFull)));// while the UART_RxFIFO is empty and no Timeoutidle\r
256    *Data = (u8)UARTx->RxBUFR; // then read the Receive Buffer Register\r
257    return wStatus;\r
258 }\r
259 \r
260 /*******************************************************************************\r
261 * Function Name  : UART_9BitByteReceive\r
262 * Description    : This function gets a 9 bits data byte from the selected UART.\r
263 * Input 1        : UARTx (x can be 0,1, 2 or 3) the desired UART\r
264 * Input 2        : A pointer to the buffer where the data will be stored\r
265 * Input 3        : The time-out period value\r
266 * Output         : The received data\r
267 * Return         : The UARTx.SR register contents\r
268 *******************************************************************************/\r
269 u16 UART_9BitByteReceive(UART_TypeDef *UARTx, u16 *Data, u8 TimeOut)\r
270 {\r
271   u16 wStatus;\r
272   UARTx->TOR=TimeOut;// reload the Timeout counter\r
273   while (!((wStatus=UARTx->SR) & (UART_TimeOutIdle|UART_RxHalfFull|UART_RxBufFull)));// while the UART_RxFIFO is empty and no Timeoutidle\r
274   *Data = (u16)UARTx->RxBUFR; // then read the RxBUFR\r
275   return wStatus;\r
276 }\r
277 \r
278 /*******************************************************************************\r
279 * Function Name  : UART_DataReceive\r
280 * Description    : This function gets 8 bits data bytes from the selected UART.\r
281 * Input 1        : UARTx (x can be 0,1, 2 or 3) the desired UART\r
282 * Input 2        : A pointer to the buffer where the data will be stored\r
283 * Input 3        : The data length\r
284 * Input 4        : The time-out period value\r
285 * Output         : The received data\r
286 * Return         : The UARTx.SR register contents\r
287 *******************************************************************************/\r
288 u16 UART_DataReceive(UART_TypeDef *UARTx, u8 *Data, u8 DataLength, u8 TimeOut)\r
289 {\r
290   u16 wStatus;\r
291   while(DataLength--)\r
292     wStatus=UART_ByteReceive(UARTx,Data++,TimeOut);\r
293   return wStatus;\r
294 }\r
295 \r
296 /*******************************************************************************\r
297 * Function Name  : UART_9BitDataReceive\r
298 * Description    : This function gets 9 bits data bytes from the selected UART.\r
299 * Input 1        : UARTx (x can be 0,1, 2 or 3) the desired UART\r
300 * Input 2        : A pointer to the buffer where the data will be stored\r
301 * Input 3        : The data length\r
302 * Input 4        : The time-out value\r
303 * Output         : The received data\r
304 * Return         : The UARTx.SR register contents\r
305 *******************************************************************************/\r
306 u16 UART_9BitDataReceive(UART_TypeDef *UARTx, u16 *Data, u8 DataLength, u8 TimeOut)\r
307 {\r
308   u16 wStatus;\r
309   while(DataLength--)\r
310     wStatus=UART_9BitByteReceive(UARTx,Data++,TimeOut);\r
311   return wStatus;\r
312 }\r
313 \r
314 /*******************************************************************************\r
315 * Function Name  : UART_StringReceive\r
316 * Description    : This function gets 8 bits data bytes from the selected UART.\r
317 * Input 1        : UARTx (x can be 0,1, 2 or 3) the desired UART\r
318 * Input 2        : A pointer to the buffer where the string will be stored\r
319 * Output         : The received string\r
320 * Return         : The UARTx.SR register contents\r
321 *******************************************************************************/\r
322 u16 UART_StringReceive(UART_TypeDef *UARTx, u8 *Data)\r
323 {\r
324   u8 *pSTRING=Data;\r
325   u16 wStatus;\r
326   do\r
327   {\r
328     while (!((wStatus=UARTx->SR) & (UART_RxHalfFull|UART_RxBufFull)));// while the UART_RxFIFO is empty and no Timeoutidle\r
329     *(pSTRING++) = (u8)UARTx->RxBUFR; // then read the RxBUFR\r
330   } while((*(pSTRING - 1)!=0x0D)&(*(pSTRING - 1)!='\0'));\r
331   *(pSTRING - 1)='\0';\r
332   return wStatus;\r
333 }\r
334 \r
335 #ifdef USE_SERIAL_PORT\r
336 /*******************************************************************************\r
337 * Function Name  : sendchar\r
338 * Description    : This function sends a character to the selected UART.\r
339 * Input 1        : A pointer to the character to send.\r
340 * Output         : None\r
341 * Return         : None\r
342 *******************************************************************************/\r
343 void sendchar( char *ch )\r
344 {\r
345    #ifdef USE_UART0\r
346      #define  UARTx  UART0\r
347    #endif /* Use_UART0 */\r
348 \r
349    #ifdef USE_UART1\r
350      #define  UARTx  UART1\r
351    #endif /* Use_UART1 */\r
352 \r
353    #ifdef USE_UART2\r
354      #define  UARTx  UART2\r
355    #endif /* Use_UART2 */\r
356 \r
357    #ifdef USE_UART3\r
358      #define  UARTx  UART3\r
359    #endif /* Use_UART3 */\r
360 \r
361    UART_ByteSend(UARTx,(u8 *)ch);\r
362 }\r
363 #endif /* USE_SERIAL_PORT */\r
364 \r
365 /******************* (C) COPYRIGHT 2003 STMicroelectronics *****END OF FILE****/\r