Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | ingob | 1 | /******************** (C) COPYRIGHT 2006 STMicroelectronics ******************** |
2 | * File Name : 91x_uart.c |
||
3 | * Author : MCD Application Team |
||
4 | * Date First Issued : 05/18/2006 : Version 1.0 |
||
5 | * Description : This file provides all the UART software functions. |
||
6 | ******************************************************************************** |
||
7 | * History: |
||
8 | * 05/22/2007 : Version 1.2 |
||
9 | * 05/24/2006 : Version 1.1 |
||
10 | * 05/18/2006 : Version 1.0 |
||
11 | ******************************************************************************** |
||
12 | * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS |
||
13 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. |
||
14 | * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, |
||
15 | * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE |
||
16 | * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING |
||
17 | * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. |
||
18 | *******************************************************************************/ |
||
19 | |||
20 | /* Includes ------------------------------------------------------------------*/ |
||
21 | #include "91x_uart.h" |
||
22 | #include "91x_scu.h" |
||
23 | |||
24 | /* Private typedef -----------------------------------------------------------*/ |
||
25 | /* Private define ------------------------------------------------------------*/ |
||
26 | /* UART IrDA Mask */ |
||
27 | #define UART_IrDA_Disable_Mask 0xFFFD /* IrDA Disable Mask */ |
||
28 | #define UART_IrDA_Enable_Mask 0x0002 /* IrDA Enable Mask */ |
||
29 | #define IrDA_LowPower_Enable_Mask 0x0004 /*IrDA lower power mode enable*/ |
||
30 | #define IrDA_LowPower_Disable_Mask 0xFFFB /*IrDA lower power mode enable*/ |
||
31 | |||
32 | /* UART Mask */ |
||
33 | #define UART_Enable_Mask 0x0001 /* UART Enable Mask */ |
||
34 | #define UART_Disable_Mask 0xFFFE /* UART Disable Mask */ |
||
35 | |||
36 | /* UART LoopBack */ |
||
37 | #define UART_LoopBack_Disable_Mask 0xFF7F /* LoopBack Disable Mask */ |
||
38 | #define UART_LoopBack_Enable_Mask 0x0080 /* LoopBack Enable Mask */ |
||
39 | |||
40 | #define UART_WordLength_Mask 0xFF9F /* UART Word Length Mask */ |
||
41 | #define UART_Parity_Mask 0xFF79 /* UART Parity Mask */ |
||
42 | #define UART_HardwareFlowControl_Mask 0x3FFF /* UART Hardware Flow Control Mask */ |
||
43 | #define UART_TxRxFIFOLevel_Mask 0xFFC0 /* UART Tx Rx FIFO Level Mask */ |
||
44 | #define UART_BreakChar_Mask 0x0001 /* UART Break Character send Mask*/ |
||
45 | #define UART_FLAG_Mask 0x1F /* UART Flag Mask */ |
||
46 | #define UART_Mode_Mask 0xFCFF /* UART Mode Mask */ |
||
47 | #define UART_RTS_LowLevel_Mask 0x0800 /* RTS signal is low */ |
||
48 | #define UART_RTS_HighLevel_Mask 0xF7FF /* RTS signal is High */ |
||
49 | #define UART_DTR_LowLevel_Mask 0x0400 /* DTR signal is low */ |
||
50 | #define UART_DTR_HighLevel_Mask 0xFBFF /* DTR signal is High */ |
||
51 | #define UART_ClearFlag_Mask 0xAA /* Clear Flag Mask */ |
||
52 | |||
53 | /* Private macro -------------------------------------------------------------*/ |
||
54 | /* Private variables ---------------------------------------------------------*/ |
||
55 | /* Private function prototypes -----------------------------------------------*/ |
||
56 | /* Private functions ---------------------------------------------------------*/ |
||
57 | |||
58 | /******************************************************************************* |
||
59 | * Function Name : UART_DeInit |
||
60 | * Description : Deinitializes the UARTx peripheral registers |
||
61 | * to their default reset values. |
||
62 | * Input : UARTx: where x can be 0,1 or 2 to select the UART peripheral. |
||
63 | * Output : None |
||
64 | * Return : None |
||
65 | *******************************************************************************/ |
||
66 | void UART_DeInit(UART_TypeDef* UARTx) |
||
67 | { |
||
68 | /* Reset the UARTx registers values */ |
||
69 | if(UARTx == UART0) |
||
70 | { |
||
71 | SCU_APBPeriphReset(__UART0,ENABLE); |
||
72 | SCU_APBPeriphReset(__UART0,DISABLE); |
||
73 | } |
||
74 | else if(UARTx == UART1) |
||
75 | { |
||
76 | SCU_APBPeriphReset(__UART1,ENABLE); |
||
77 | SCU_APBPeriphReset(__UART1,DISABLE); |
||
78 | } |
||
79 | else if(UARTx == UART2) |
||
80 | { |
||
81 | SCU_APBPeriphReset(__UART2,ENABLE); |
||
82 | SCU_APBPeriphReset(__UART2,DISABLE); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | /******************************************************************************* |
||
87 | * Function Name : UART_Init |
||
88 | * Description : Initializes the UARTx peripheral according to the specified |
||
89 | * parameters in the UART_InitStruct . |
||
90 | * Input : - UARTx: where x can be 0,1or 2 to select the UART peripheral. |
||
91 | * - UART_InitStruct: pointer to a UART_InitTypeDef structure |
||
92 | * that contains the configuration information for the |
||
93 | * specified UART peripheral. |
||
94 | * Output : None |
||
95 | * Return : None |
||
96 | *******************************************************************************/ |
||
97 | void UART_Init(UART_TypeDef* UARTx, UART_InitTypeDef* UART_InitStruct) |
||
98 | { |
||
99 | |||
100 | u64 UART_MainClock = 0; |
||
101 | u32 IntegerDivider = 0; |
||
102 | u32 FractionalDivider = 0; |
||
103 | |||
104 | /* Clear the LCR[6:5] bits */ |
||
105 | UARTx->LCR &= UART_WordLength_Mask; |
||
106 | /* Set the LCR[6:5] bits according to UART_WordLength value */ |
||
107 | UARTx->LCR |= UART_InitStruct->UART_WordLength; |
||
108 | |||
109 | /* Choose Stop Bits */ |
||
110 | if(UART_InitStruct->UART_StopBits == UART_StopBits_2) |
||
111 | { |
||
112 | /* 2 Stop Bit */ |
||
113 | UARTx->LCR |= UART_StopBits_2; |
||
114 | } |
||
115 | else |
||
116 | { |
||
117 | /* One Stop Bits */ |
||
118 | UARTx->LCR &= UART_StopBits_1; |
||
119 | } |
||
120 | |||
121 | /* Configure the Parity */ |
||
122 | /* Clear the LCR[7]and LCR[2:1] bits */ |
||
123 | UARTx->LCR &= UART_Parity_Mask; |
||
124 | /* Set the LCR[7]and LCR[2:1] bits according to UART_Parity value */ |
||
125 | UARTx->LCR |= UART_InitStruct->UART_Parity; |
||
126 | |||
127 | /* Configure the BaudRate */ |
||
128 | UART_MainClock = (SCU_GetMCLKFreqValue())*1000; |
||
129 | if((SCU->CLKCNTR & 0x200) != 0x200) |
||
130 | { |
||
131 | UART_MainClock = UART_MainClock/2; |
||
132 | } |
||
133 | /* Determine the integer part */ |
||
134 | IntegerDivider = ((100) * (UART_MainClock) / (16 * (UART_InitStruct->UART_BaudRate))); |
||
135 | UARTx->IBRD = IntegerDivider / 100; |
||
136 | |||
137 | /* Determine the fractional part */ |
||
138 | FractionalDivider = IntegerDivider - (100 * (UARTx->IBRD)); |
||
139 | UARTx->FBRD = ((((FractionalDivider * 64) + 50) / 100)); |
||
140 | |||
141 | /* Choose the Hardware Flow Control */ |
||
142 | /* Clear the CR[15:14] bits */ |
||
143 | UARTx->CR &= UART_HardwareFlowControl_Mask; |
||
144 | /* Set the CR[15:14] bits according to UART_HardwareFlowControl value */ |
||
145 | UARTx->CR |= UART_InitStruct->UART_HardwareFlowControl; |
||
146 | |||
147 | /* Configure the UART mode */ |
||
148 | /* Clear the CR[9:8] bits */ |
||
149 | UARTx->CR &= UART_Mode_Mask; |
||
150 | /* Set the CR[9:8] bits according to UART_Mode value */ |
||
151 | UARTx->CR |= UART_InitStruct->UART_Mode; |
||
152 | |||
153 | /* Enable or disable the FIFOs */ |
||
154 | /* Set the FIFOs Levels */ |
||
155 | if(UART_InitStruct->UART_FIFO == UART_FIFO_Enable) |
||
156 | { |
||
157 | /* Enable the FIFOs */ |
||
158 | UARTx->LCR |= UART_FIFO_Enable; |
||
159 | |||
160 | /* Clear TXIFLSEL and RXIFLSEL bits */ |
||
161 | UARTx->IFLS &= UART_TxRxFIFOLevel_Mask; |
||
162 | |||
163 | /* Set RXIFLSEL bits according to UART_RxFIFOLevel value */ |
||
164 | UARTx->IFLS |= (UART_InitStruct->UART_RxFIFOLevel << 3); |
||
165 | |||
166 | /* Set TXIFLSEL bits according to UART_TxFIFOLevel value */ |
||
167 | UARTx->IFLS |= UART_InitStruct->UART_TxFIFOLevel; |
||
168 | } |
||
169 | else |
||
170 | { |
||
171 | /* Disable the FIFOs */ |
||
172 | UARTx->LCR &= UART_FIFO_Disable; |
||
173 | } |
||
174 | } |
||
175 | |||
176 | /******************************************************************************* |
||
177 | * Function Name : UART_StructInit |
||
178 | * Description : Fills each UART_InitStruct member with its reset value. |
||
179 | * Input : UART_InitStruct: pointer to a UART_InitTypeDef structure which |
||
180 | * will be initialized. |
||
181 | * Output : None |
||
182 | * Return : None |
||
183 | *******************************************************************************/ |
||
184 | void UART_StructInit(UART_InitTypeDef* UART_InitStruct) |
||
185 | { |
||
186 | /* Reset the UART_InitStruct members */ |
||
187 | UART_InitStruct->UART_WordLength = UART_WordLength_8D; |
||
188 | UART_InitStruct->UART_StopBits = UART_StopBits_1; |
||
189 | UART_InitStruct->UART_Parity = UART_Parity_Odd ; |
||
190 | UART_InitStruct->UART_BaudRate = 9600; |
||
191 | UART_InitStruct->UART_HardwareFlowControl = UART_HardwareFlowControl_None; |
||
192 | UART_InitStruct->UART_Mode = UART_Mode_Tx_Rx; |
||
193 | UART_InitStruct->UART_FIFO = UART_FIFO_Enable; |
||
194 | UART_InitStruct->UART_TxFIFOLevel = UART_FIFOLevel_1_2; |
||
195 | UART_InitStruct->UART_RxFIFOLevel = UART_FIFOLevel_1_2; |
||
196 | } |
||
197 | |||
198 | /******************************************************************************* |
||
199 | * Function Name : UART_Cmd |
||
200 | * Description : Enables or disables the specified UART peripheral. |
||
201 | * Input : - UARTx: where x can be 0,1 or 2 to select the UART peripheral |
||
202 | * - NewState: new state of the UARTx peripheral. |
||
203 | * This parameter can be: ENABLE or DISABLE. |
||
204 | * Output : None |
||
205 | * Return : None |
||
206 | *******************************************************************************/ |
||
207 | void UART_Cmd(UART_TypeDef* UARTx, FunctionalState NewState) |
||
208 | { |
||
209 | if (NewState == ENABLE) |
||
210 | { |
||
211 | /* Enable the selected UART by setting the UARTEN bit in the CR register */ |
||
212 | UARTx->CR |= UART_Enable_Mask; |
||
213 | } |
||
214 | else |
||
215 | { |
||
216 | /* Disable the selected UART by clearing the UARTEN bit in the CR register */ |
||
217 | UARTx->CR &= UART_Disable_Mask; |
||
218 | } |
||
219 | } |
||
220 | |||
221 | /******************************************************************************* |
||
222 | * Function Name : UART_ITConfig |
||
223 | * Description : Enables or disables the specified UART interrupts. |
||
224 | * Input : - UARTx: where x can be 0,1 or 2 to select the UART peripheral |
||
225 | * - UART_IT: specifies the UART interrupts sources to be |
||
226 | * enabled or disabled. This parameter can be any combination |
||
227 | * of the following values: |
||
228 | * - UART_IT_OverrunError: Overrun Error interrupt |
||
229 | * - UART_IT_BreakError: Break Error interrupt |
||
230 | * - UART_IT_ParityError: Parity Error interrupt |
||
231 | * - UART_IT_FrameError: Frame Error interrupt |
||
232 | * - UART_IT_ReceiveTimeOut: Receive Time Out interrupt |
||
233 | * - UART_IT_Transmit: Transmit interrupt |
||
234 | * - UART_IT_Receive: Receive interrupt |
||
235 | * - UART_IT_DSR: DSR interrupt |
||
236 | * - UART_IT_DCD: DCD interrupt |
||
237 | * - UART_IT_CTS: CTS interrupt |
||
238 | * - UART_IT_RI: RI interrupt |
||
239 | * - NewState: new state of the UARTx peripheral. |
||
240 | * This parameter can be: ENABLE or DISABLE. |
||
241 | * Output : None |
||
242 | * Return : None |
||
243 | *******************************************************************************/ |
||
244 | void UART_ITConfig(UART_TypeDef* UARTx, u16 UART_IT, FunctionalState NewState) |
||
245 | { |
||
246 | if(NewState == ENABLE) |
||
247 | { |
||
248 | /* Enables the selected interrupts */ |
||
249 | UARTx->IMSC |= UART_IT; |
||
250 | } |
||
251 | else |
||
252 | { |
||
253 | /* Disables the selected interrupts */ |
||
254 | UARTx->IMSC &= ~UART_IT; |
||
255 | } |
||
256 | } |
||
257 | |||
258 | /******************************************************************************* |
||
259 | * Function Name : UART_DMAConfig |
||
260 | * Description : Configures the UARTx’s DMA interface. |
||
261 | * Input : - UARTx: where x can be 1 or 2 to select the UART peripheral |
||
262 | * - UART_DMAOnError: specifies the DMA on error request. |
||
263 | * This parameter can be: |
||
264 | * - UART_DMAOnError_Enable: DMA receive request enabled |
||
265 | * when the UART error interrupt is asserted. |
||
266 | * - UART_DMAOnError_Disable: DMA receive request disabled |
||
267 | * when the UART error interrupt is asserted. |
||
268 | * Output : None |
||
269 | * Return : None |
||
270 | *******************************************************************************/ |
||
271 | void UART_DMAConfig(UART_TypeDef* UARTx, u16 UART_DMAOnError) |
||
272 | { |
||
273 | if(UART_DMAOnError == UART_DMAOnError_Enable) |
||
274 | { |
||
275 | UARTx->DMACR &= UART_DMAOnError_Enable; |
||
276 | } |
||
277 | else |
||
278 | { |
||
279 | UARTx->DMACR |= UART_DMAOnError_Disable; |
||
280 | } |
||
281 | } |
||
282 | |||
283 | /******************************************************************************* |
||
284 | * Function Name : UART_DMACmd |
||
285 | * Description : Enables or disables the UARTx’s DMA interface. |
||
286 | * Input : - UARTx: where x can be 1 or 2 to select the UART peripheral |
||
287 | * - UART_DMAReq: enables or disables the request of DMA from UART. |
||
288 | * This parameter can be: |
||
289 | * - UART_DMAReq_Tx: Transmit DMA Enable |
||
290 | * - UART_DMAReq_Rx: Receive DMA Enable |
||
291 | * - NewState: new state of the UARTx peripheral. |
||
292 | * This parameter can be: ENABLE or DISABLE. |
||
293 | * Output : None |
||
294 | * Return : None |
||
295 | *******************************************************************************/ |
||
296 | void UART_DMACmd(UART_TypeDef* UARTx, u8 UART_DMAReq, FunctionalState NewState) |
||
297 | { |
||
298 | if(UART_DMAReq == UART_DMAReq_Tx) |
||
299 | { |
||
300 | if(NewState == ENABLE) |
||
301 | { |
||
302 | UARTx->DMACR |= UART_DMAReq_Tx; |
||
303 | } |
||
304 | else |
||
305 | { |
||
306 | UARTx->DMACR &= ~UART_DMAReq_Tx; |
||
307 | } |
||
308 | } |
||
309 | |||
310 | if(UART_DMAReq == UART_DMAReq_Rx) |
||
311 | { |
||
312 | if(NewState == ENABLE) |
||
313 | { |
||
314 | UARTx->DMACR |= UART_DMAReq_Rx; |
||
315 | } |
||
316 | else |
||
317 | { |
||
318 | UARTx->DMACR &= ~UART_DMAReq_Rx; |
||
319 | } |
||
320 | } |
||
321 | } |
||
322 | |||
323 | /******************************************************************************* |
||
324 | * Function Name : UART_LoopBackConfig |
||
325 | * Description : Enables or disables the LoopBack mode. |
||
326 | * Input : - UARTx: where x can be 0,1 or 2 to select the UART peripheral |
||
327 | * - NewState: new state of the UARTx peripheral. |
||
328 | * This parameter can be: ENABLE or DISABLE. |
||
329 | * Output : None |
||
330 | * Return : None |
||
331 | *******************************************************************************/ |
||
332 | void UART_LoopBackConfig(UART_TypeDef* UARTx, FunctionalState NewState) |
||
333 | { |
||
334 | if (NewState == ENABLE) |
||
335 | { |
||
336 | /* Enable the LoopBack mode of the specified UART */ |
||
337 | UARTx->CR |= UART_LoopBack_Enable_Mask; |
||
338 | } |
||
339 | else |
||
340 | { |
||
341 | /* Disable the LoopBack mode of the specified UART */ |
||
342 | UARTx->CR &= UART_LoopBack_Disable_Mask; |
||
343 | } |
||
344 | } |
||
345 | |||
346 | /******************************************************************************* |
||
347 | * Function Name : UART_GetFlagStatus |
||
348 | * Description : Checks whether the specified UART flag is set or not. |
||
349 | * Input : - UARTx: where x can be 0,1 or 2 to select the UART peripheral |
||
350 | * - UART_FLAG: specifies the flag to check. |
||
351 | * This parameter can be one of the following values: |
||
352 | * - UART_FLAG_OverrunError: Overrun error flag |
||
353 | * - UART_FLAG_Break: break error flag |
||
354 | * - UART_FLAG_ParityError: parity error flag |
||
355 | * - UART_FLAG_FrameError: frame error flag |
||
356 | * - UART_FLAG_RI: RI flag |
||
357 | * - UART_FLAG_TxFIFOEmpty: Transmit FIFO Empty flag |
||
358 | * - UART_FLAG_RxFIFOFull: Receive FIFO Full flag |
||
359 | * - UART_FLAG_TxFIFOFull: Transmit FIFO Full flag |
||
360 | * - UART_FLAG_RxFIFOEmpty: Receive FIFO Empty flag |
||
361 | * - UART_FLAG_Busy: UART Busy flag |
||
362 | * - UART_FLAG_CTS: CTS flag |
||
363 | * - UART_FLAG_DCD: DCD flag |
||
364 | * - UART_FLAG_DSR: DSR flag |
||
365 | * - UART_RawIT_OverrunError: Overrun Error interrupt flag |
||
366 | * - UART_RawIT_BreakError: Break Error interrupt flag |
||
367 | * - UART_RawIT_ParityError: Parity Error interrupt flag |
||
368 | * - UART_RawIT_FrameError: Frame Error interrupt flag |
||
369 | * - UART_RawIT_ReceiveTimeOut: ReceiveTimeOut interrupt flag |
||
370 | * - UART_RawIT_Transmit: Transmit interrupt flag |
||
371 | * - UART_RawIT_Receive: Receive interrupt flag |
||
372 | * - UART_RawIT_DSR: DSR interrupt flag |
||
373 | * - UART_RawIT_DCD: DCD interrupt flag |
||
374 | * - UART_RawIT_CTS: CTS interrupt flag |
||
375 | * - UART_RawIT_RI: RI interrupt flag |
||
376 | * Output : None |
||
377 | * Return : The new state of UART_FLAG (SET or RESET). |
||
378 | *******************************************************************************/ |
||
379 | FlagStatus UART_GetFlagStatus(UART_TypeDef* UARTx, u16 UART_FLAG) |
||
380 | { |
||
381 | |||
382 | u32 UARTReg = 0, FlagPos = 0; |
||
383 | u32 StatusReg = 0; |
||
384 | |||
385 | /* Get the UART register index */ |
||
386 | UARTReg = UART_FLAG >> 5; |
||
387 | |||
388 | /* Get the flag position */ |
||
389 | FlagPos = UART_FLAG & UART_FLAG_Mask; |
||
390 | |||
391 | if(UARTReg == 1) /* The flag to check is in RSR register */ |
||
392 | { |
||
393 | StatusReg = UARTx->RSECR; |
||
394 | } |
||
395 | else if (UARTReg == 2) /* The flag to check is in FR register */ |
||
396 | { |
||
397 | StatusReg = UARTx->FR; |
||
398 | } |
||
399 | else if(UARTReg == 3) /* The flag to check is in RIS register */ |
||
400 | { |
||
401 | StatusReg = UARTx->RIS; |
||
402 | } |
||
403 | |||
404 | if((StatusReg & (1 << FlagPos))!= RESET) |
||
405 | { |
||
406 | return SET; |
||
407 | } |
||
408 | else |
||
409 | { |
||
410 | return RESET; |
||
411 | } |
||
412 | } |
||
413 | |||
414 | /******************************************************************************* |
||
415 | * Function Name : UART_ClearFlag |
||
416 | * Description : Clears the UARTx’s flags(Frame, Parity, Break, Overrun error). |
||
417 | * Input : - UARTx: where x can be 0,1or 2 to select the UART peripheral. |
||
418 | * Output : None |
||
419 | * Return : None |
||
420 | *******************************************************************************/ |
||
421 | void UART_ClearFlag(UART_TypeDef* UARTx) |
||
422 | { |
||
423 | /* Clear the flag */ |
||
424 | UARTx->RSECR = UART_ClearFlag_Mask; |
||
425 | } |
||
426 | |||
427 | /******************************************************************************* |
||
428 | * Function Name : UART_GetITStatus |
||
429 | * Description : Checks whether the specified UART interrupt has occured or not. |
||
430 | * Input : - UARTx: where x can be 0,1or 2 to select the UART peripheral. |
||
431 | * - UART_IT: specifies the interrupt pending bit to be checked. |
||
432 | * This parameter can be one of the following values: |
||
433 | * - UART_IT_OverrunError: Overrun Error interrupt |
||
434 | * - UART_IT_BreakError: Break Error interrupt |
||
435 | * - UART_IT_ParityError: Parity Error interrupt |
||
436 | * - UART_IT_FrameError: Frame Error interrupt |
||
437 | * - UART_IT_ReceiveTimeOut: Receive Time Out interrupt |
||
438 | * - UART_IT_Transmit: Transmit interrupt |
||
439 | * - UART_IT_Receive: Receive interrupt |
||
440 | * - UART_IT_DSR: DSR interrupt |
||
441 | * - UART_IT_DCD: DCD interrupt |
||
442 | * - UART_IT_CTS: CTS interrupt |
||
443 | * - UART_IT_RI: RI interrupt |
||
444 | * Output : None |
||
445 | * Return : The new state of UART_IT (SET or RESET). |
||
446 | *******************************************************************************/ |
||
447 | ITStatus UART_GetITStatus(UART_TypeDef* UARTx, u16 UART_IT) |
||
448 | { |
||
449 | if((UARTx->MIS & UART_IT) != RESET) |
||
450 | { |
||
451 | return SET; |
||
452 | } |
||
453 | else |
||
454 | { |
||
455 | return RESET; |
||
456 | } |
||
457 | } |
||
458 | |||
459 | /******************************************************************************* |
||
460 | * Function Name : UART_ClearITPendingBit |
||
461 | * Description : Clears the UARTx’s interrupt pending bits. |
||
462 | * Input : - UARTx: where x can be 0,1or 2 to select the UART peripheral. |
||
463 | * - UART_IT: specifies the interrupt pending bit to clear. |
||
464 | * More than one interrupt can be cleared using the “|” operator. |
||
465 | * This parameter can be: |
||
466 | * - UART_IT_OverrunError: Overrun Error interrupt |
||
467 | * - UART_IT_BreakError: Break Error interrupt |
||
468 | * - UART_IT_ParityError: Parity Error interrupt |
||
469 | * - UART_IT_FrameError: Frame Error interrupt |
||
470 | * - UART_IT_ReceiveTimeOut: Receive Time Out interrupt |
||
471 | * - UART_IT_Transmit: Transmit interrupt |
||
472 | * - UART_IT_Receive: Receive interrupt |
||
473 | * - UART_IT_DSR: DSR interrupt |
||
474 | * - UART_IT_DCD: DCD interrupt |
||
475 | * - UART_IT_CTS: CTS interrupt |
||
476 | * - UART_IT_RI: RI interrupt |
||
477 | * Output : None |
||
478 | * Return : None |
||
479 | *******************************************************************************/ |
||
480 | void UART_ClearITPendingBit(UART_TypeDef* UARTx, u16 UART_IT) |
||
481 | { |
||
482 | /* Clear the specified interrupt */ |
||
483 | UARTx->ICR = UART_IT; |
||
484 | } |
||
485 | |||
486 | /******************************************************************************* |
||
487 | * Function Name : UART_IrDALowPowerConfig |
||
488 | * Description : Sets the IrDA low power mode |
||
489 | * Input : - IrDAx: where x can be 0,1 or 2 to select the UART/IrDA peripheral. |
||
490 | * - NewState: new state of the UARTIrDA peripheral. |
||
491 | * This parameter can be: ENABLE or DISABLE. |
||
492 | * Output : None |
||
493 | * Return : None |
||
494 | *******************************************************************************/ |
||
495 | void UART_IrDALowPowerConfig(u8 IrDAx, FunctionalState NewState) |
||
496 | { |
||
497 | UART_TypeDef* UARTx; |
||
498 | |||
499 | switch(IrDAx) |
||
500 | { |
||
501 | case IrDA0: UARTx = UART0; |
||
502 | break; |
||
503 | case IrDA1: UARTx = UART1; |
||
504 | break; |
||
505 | case IrDA2: UARTx = UART2; |
||
506 | break; |
||
507 | } |
||
508 | |||
509 | if (NewState == ENABLE) |
||
510 | { |
||
511 | UARTx->CR |= IrDA_LowPower_Enable_Mask; |
||
512 | } |
||
513 | else |
||
514 | { |
||
515 | UARTx->CR &= IrDA_LowPower_Disable_Mask; |
||
516 | } |
||
517 | } |
||
518 | |||
519 | /******************************************************************************* |
||
520 | * Function Name : UART_IrDASetCounter |
||
521 | * Description : Sets the IrDA counter divisor value. |
||
522 | * Input : - UARTx: where x can be 0,1 or 2 to select the UART/IrDA peripheral. |
||
523 | * - IrDA_Counter: IrDA counter divisor new value n low power mode(Hz). |
||
524 | * Output : None |
||
525 | * Return : None |
||
526 | *******************************************************************************/ |
||
527 | void UART_IrDASetCounter(u8 IrDAx, u32 IrDA_Counter) |
||
528 | { |
||
529 | UART_TypeDef* UARTx; |
||
530 | u32 APBClock; |
||
531 | switch(IrDAx) |
||
532 | { |
||
533 | case IrDA0: UARTx = UART0; |
||
534 | break; |
||
535 | case IrDA1: UARTx = UART1; |
||
536 | break; |
||
537 | case IrDA2: UARTx = UART2; |
||
538 | break; |
||
539 | } |
||
540 | /* Get the APB frequency */ |
||
541 | APBClock = (SCU_GetPCLKFreqValue())*1000; |
||
542 | /* Determine the Counter Divisor part */ |
||
543 | UARTx->ILPR = (((APBClock*10) / ( IrDA_Counter)) + 5 )/10; |
||
544 | } |
||
545 | |||
546 | /******************************************************************************* |
||
547 | * Function Name : UART_IrDACmd |
||
548 | * Description : Enables or disables the UARTx’s IrDA interface. |
||
549 | * Input : - IrDAx: where x can be 0,1 or 2 to select the UART/IrDA peripheral |
||
550 | * - NewState: new state of the UARTx peripheral. |
||
551 | * This parameter can be: ENABLE or DISABLE. |
||
552 | * Output : None |
||
553 | * Return : None |
||
554 | *******************************************************************************/ |
||
555 | void UART_IrDACmd(u8 IrDAx, FunctionalState NewState) |
||
556 | { |
||
557 | UART_TypeDef* UARTx; |
||
558 | |||
559 | switch(IrDAx) |
||
560 | { |
||
561 | case IrDA0: UARTx = UART0; |
||
562 | break; |
||
563 | case IrDA1: UARTx = UART1; |
||
564 | break; |
||
565 | case IrDA2: UARTx = UART2; |
||
566 | break; |
||
567 | } |
||
568 | if(NewState == ENABLE) |
||
569 | { |
||
570 | /* Enable the IrDA mode of the specified UART */ |
||
571 | UARTx->CR |= UART_IrDA_Enable_Mask; |
||
572 | } |
||
573 | else |
||
574 | { |
||
575 | /* Disable the IrDA mode of the specified UART */ |
||
576 | UARTx->CR &= UART_IrDA_Disable_Mask; |
||
577 | } |
||
578 | } |
||
579 | |||
580 | /******************************************************************************* |
||
581 | * Function Name : UART_SendData |
||
582 | * Description : Transmits signle Byte of data through the UARTx peripheral. |
||
583 | * Input : - UARTx: where x can be 0,1 or 2 to select the UART peripheral. |
||
584 | * - Data: the byte to transmit |
||
585 | * Output : None |
||
586 | * Return : None |
||
587 | *******************************************************************************/ |
||
588 | void UART_SendData(UART_TypeDef* UARTx, u8 Data) |
||
589 | { |
||
590 | /* Transmit one byte */ |
||
591 | UARTx->DR = Data; |
||
592 | } |
||
593 | |||
594 | /******************************************************************************* |
||
595 | * Function Name : UART_ReceiveData |
||
596 | * Description : Returns the most recent received Byte by the UARTx peripheral. |
||
597 | * Input : UARTx: where x can be 0,1 or 2 to select the UART peripheral. |
||
598 | * Output : None |
||
599 | * Return : The received data |
||
600 | *******************************************************************************/ |
||
601 | u8 UART_ReceiveData(UART_TypeDef* UARTx) |
||
602 | { |
||
603 | /* Receive one byte */ |
||
604 | return ((u8)UARTx->DR); |
||
605 | } |
||
606 | |||
607 | /******************************************************************************* |
||
608 | * Function Name : UART_SendBreak |
||
609 | * Description : Transmits break characters. |
||
610 | * Input : UARTx: where x can be 0,1 or 2 to select the UART peripheral. |
||
611 | * Output : None |
||
612 | * Return : None |
||
613 | *******************************************************************************/ |
||
614 | void UART_SendBreak(UART_TypeDef* UARTx) |
||
615 | { |
||
616 | /* Send break characters */ |
||
617 | UARTx->LCR |= UART_BreakChar_Mask; |
||
618 | } |
||
619 | |||
620 | /******************************************************************************* |
||
621 | * Function Name : UART_RTSConfig |
||
622 | * Description : Sets or Resets the RTS signal |
||
623 | * Input : - LevelState: new state of the RTS signal for UART0 only. |
||
624 | * This parameter can be: LowLevel or HighLevel |
||
625 | * Output : None |
||
626 | * Return : None |
||
627 | *******************************************************************************/ |
||
628 | void UART_RTSConfig(UART_LevelTypeDef LevelState) |
||
629 | { |
||
630 | if(LevelState == LowLevel) |
||
631 | { |
||
632 | UART0->CR |= UART_RTS_LowLevel_Mask; |
||
633 | } |
||
634 | else |
||
635 | { |
||
636 | UART0->CR &= UART_RTS_HighLevel_Mask; |
||
637 | } |
||
638 | } |
||
639 | |||
640 | /******************************************************************************* |
||
641 | * Function Name : UART_DTRConfig |
||
642 | * Description : Sets or Resets the DTR signal for UART0 only |
||
643 | * Input : - LevelState: new state of the DTR signal. |
||
644 | * This parameter can be: LowLevel or HighLevel |
||
645 | * Output : None |
||
646 | * Return : None |
||
647 | *******************************************************************************/ |
||
648 | void UART_DTRConfig(UART_LevelTypeDef LevelState) |
||
649 | { |
||
650 | if(LevelState == LowLevel) |
||
651 | { |
||
652 | UART0->CR |= UART_DTR_LowLevel_Mask; |
||
653 | } |
||
654 | else |
||
655 | { |
||
656 | UART0->CR &= UART_DTR_HighLevel_Mask; |
||
657 | } |
||
658 | } |
||
659 | /******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/ |