1 /******************** (C) COPYRIGHT 2003 STMicroelectronics ********************
\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
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
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
28 *******************************************************************************/
\r
29 void UART_Init(UART_TypeDef *UARTx)
\r
33 (void)UARTx->RxBUFR;
\r
34 UARTx->RxRSTR = 0xFFFF;
\r
35 UARTx->TxRSTR = 0xFFFF;
\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
45 *******************************************************************************/
\r
46 void UART_BaudRateConfig(UART_TypeDef *UARTx, u32 BaudRate)
\r
48 UARTx->BR = (u16)(RCCU_FrequencyValue(RCCU_FCLK)/(16*BaudRate));
\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
62 *******************************************************************************/
\r
63 void UART_Config(UART_TypeDef *UARTx, u32 BaudRate, UARTParity_TypeDef Parity,
\r
64 UARTStopBits_TypeDef StopBits, UARTMode_TypeDef Mode)
\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
72 /*******************************************************************************
\r
73 * Function Name : UART_ItConfig
\r
74 * Description : This function enables or disables the interrupts of the
\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
81 *******************************************************************************/
\r
82 void UART_ItConfig(UART_TypeDef *UARTx, u16 UART_Flag, FunctionalState NewState)
\r
84 if (NewState==ENABLE) UARTx->IER|=UART_Flag; else UARTx->IER&=~UART_Flag;
\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
95 *******************************************************************************/
\r
96 void UART_FifoConfig(UART_TypeDef *UARTx, FunctionalState NewState)
\r
98 if (NewState==ENABLE) UARTx->CR|=0x0400; else UARTx->CR&=~0x0400;
\r
101 /*******************************************************************************
\r
102 * Function Name : UART_FifoReset
\r
103 * Description : This function resets the Rx and the Tx FIFOs of the
\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
109 *******************************************************************************/
\r
110 void UART_FifoReset(UART_TypeDef *UARTx, UARTFIFO_TypeDef FIFO)
\r
112 if (FIFO==UART_RxFIFO) UARTx->RxRSTR=0xFFFF; else UARTx->TxRSTR=0xFFFF;
\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
123 *******************************************************************************/
\r
124 void UART_LoopBackConfig(UART_TypeDef *UARTx, FunctionalState NewState)
\r
126 if (NewState==ENABLE) UARTx->CR|=0x0040; else UARTx->CR&=~0x0040;
\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
136 *******************************************************************************/
\r
137 void UART_RxConfig(UART_TypeDef *UARTx, FunctionalState NewState)
\r
139 if (NewState==ENABLE) UARTx->CR|=0x0100; else UARTx->CR&=~0x0100;
\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
149 *******************************************************************************/
\r
150 void UART_OnOffConfig(UART_TypeDef *UARTx, FunctionalState NewState)
\r
152 if (NewState==ENABLE) UARTx->CR|=0x0080; else UARTx->CR&=~0x0080;
\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
162 *******************************************************************************/
\r
163 void UART_ByteSend(UART_TypeDef *UARTx, u8 *Data)
\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
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
179 *******************************************************************************/
\r
180 void UART_9BitByteSend(UART_TypeDef *UARTx, u16 *Data)
\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
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
197 *******************************************************************************/
\r
198 void UART_DataSend(UART_TypeDef *UARTx, u8 *Data, u8 DataLength)
\r
200 while(DataLength--)
\r
202 UART_ByteSend(UARTx,Data);
\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
215 *******************************************************************************/
\r
216 void UART_9BitDataSend(UART_TypeDef *UARTx, u16 *Data, u8 DataLength)
\r
218 while(DataLength--)
\r
220 UART_9BitByteSend(UARTx,Data);
\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
232 *******************************************************************************/
\r
233 void UART_StringSend(UART_TypeDef *UARTx, u8 *String)
\r
236 while(*Data != '\0')
\r
237 UART_ByteSend(UARTx, Data++);
\r
239 UART_ByteSend(UARTx, Data);
\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
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
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
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
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
291 while(DataLength--)
\r
292 wStatus=UART_ByteReceive(UARTx,Data++,TimeOut);
\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
309 while(DataLength--)
\r
310 wStatus=UART_9BitByteReceive(UARTx,Data++,TimeOut);
\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
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
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
342 *******************************************************************************/
\r
343 void sendchar( char *ch )
\r
346 #define UARTx UART0
\r
347 #endif /* Use_UART0 */
\r
350 #define UARTx UART1
\r
351 #endif /* Use_UART1 */
\r
354 #define UARTx UART2
\r
355 #endif /* Use_UART2 */
\r
358 #define UARTx UART3
\r
359 #endif /* Use_UART3 */
\r
361 UART_ByteSend(UARTx,(u8 *)ch);
\r
363 #endif /* USE_SERIAL_PORT */
\r
365 /******************* (C) COPYRIGHT 2003 STMicroelectronics *****END OF FILE****/
\r