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