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 extern u16 UART_FlagStatus(UART_TypeDef *UARTx);
\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
30 *******************************************************************************/
\r
31 void UART_Init(UART_TypeDef *UARTx)
\r
35 (void)UARTx->RxBUFR;
\r
36 UARTx->RxRSTR = 0xFFFF;
\r
37 UARTx->TxRSTR = 0xFFFF;
\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
47 *******************************************************************************/
\r
48 void UART_BaudRateConfig(UART_TypeDef *UARTx, u32 BaudRate)
\r
50 UARTx->BR = (u16)(RCCU_FrequencyValue(RCCU_FCLK)/(16*BaudRate));
\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
64 *******************************************************************************/
\r
65 void UART_Config(UART_TypeDef *UARTx, u32 BaudRate, UARTParity_TypeDef Parity,
\r
66 UARTStopBits_TypeDef StopBits, UARTMode_TypeDef Mode)
\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
74 /*******************************************************************************
\r
75 * Function Name : UART_ItConfig
\r
76 * Description : This function enables or disables the interrupts of the
\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
83 *******************************************************************************/
\r
84 void UART_ItConfig(UART_TypeDef *UARTx, u16 UART_Flag, FunctionalState NewState)
\r
86 if (NewState==ENABLE) UARTx->IER|=UART_Flag; else UARTx->IER&=~UART_Flag;
\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
97 *******************************************************************************/
\r
98 void UART_FifoConfig(UART_TypeDef *UARTx, FunctionalState NewState)
\r
100 if (NewState==ENABLE) UARTx->CR|=0x0400; else UARTx->CR&=~0x0400;
\r
103 /*******************************************************************************
\r
104 * Function Name : UART_FifoReset
\r
105 * Description : This function resets the Rx and the Tx FIFOs of the
\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
111 *******************************************************************************/
\r
112 void UART_FifoReset(UART_TypeDef *UARTx, UARTFIFO_TypeDef FIFO)
\r
114 if (FIFO==UART_RxFIFO) UARTx->RxRSTR=0xFFFF; else UARTx->TxRSTR=0xFFFF;
\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
125 *******************************************************************************/
\r
126 void UART_LoopBackConfig(UART_TypeDef *UARTx, FunctionalState NewState)
\r
128 if (NewState==ENABLE) UARTx->CR|=0x0040; else UARTx->CR&=~0x0040;
\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
138 *******************************************************************************/
\r
139 void UART_RxConfig(UART_TypeDef *UARTx, FunctionalState NewState)
\r
141 if (NewState==ENABLE) UARTx->CR|=0x0100; else UARTx->CR&=~0x0100;
\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
151 *******************************************************************************/
\r
152 void UART_OnOffConfig(UART_TypeDef *UARTx, FunctionalState NewState)
\r
154 if (NewState==ENABLE) UARTx->CR|=0x0080; else UARTx->CR&=~0x0080;
\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
164 *******************************************************************************/
\r
165 void UART_ByteSend(UART_TypeDef *UARTx, u8 *Data)
\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
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
181 *******************************************************************************/
\r
182 void UART_9BitByteSend(UART_TypeDef *UARTx, u16 *Data)
\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
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
199 *******************************************************************************/
\r
200 void UART_DataSend(UART_TypeDef *UARTx, u8 *Data, u8 DataLength)
\r
202 while(DataLength--)
\r
204 UART_ByteSend(UARTx,Data);
\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
217 *******************************************************************************/
\r
218 void UART_9BitDataSend(UART_TypeDef *UARTx, u16 *Data, u8 DataLength)
\r
220 while(DataLength--)
\r
222 UART_9BitByteSend(UARTx,Data);
\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
234 *******************************************************************************/
\r
235 void UART_StringSend(UART_TypeDef *UARTx, u8 *String)
\r
238 while(*Data != '\0')
\r
239 UART_ByteSend(UARTx, Data++);
\r
241 UART_ByteSend(UARTx, Data);
\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
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
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
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
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
293 while(DataLength--)
\r
294 wStatus=UART_ByteReceive(UARTx,Data++,TimeOut);
\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
311 while(DataLength--)
\r
312 wStatus=UART_9BitByteReceive(UARTx,Data++,TimeOut);
\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
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
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
344 *******************************************************************************/
\r
345 void sendchar( char *ch )
\r
348 #define UARTx UART0
\r
349 #endif /* Use_UART0 */
\r
352 #define UARTx UART1
\r
353 #endif /* Use_UART1 */
\r
356 #define UARTx UART2
\r
357 #endif /* Use_UART2 */
\r
360 #define UARTx UART3
\r
361 #endif /* Use_UART3 */
\r
363 UART_ByteSend(UARTx,(u8 *)ch);
\r
365 #endif /* USE_SERIAL_PORT */
\r
367 /******************* (C) COPYRIGHT 2003 STMicroelectronics *****END OF FILE****/
\r