OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [CORTEX_STM32L152_IAR/] [system_and_ST_code/] [STM32L1xx_StdPeriph_Driver/] [src/] [stm32l1xx_usart.c] - Blame information for rev 582

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 582 jeremybenn
/**
2
  ******************************************************************************
3
  * @file    stm32l1xx_usart.c
4
  * @author  MCD Application Team
5
  * @version V1.0.0RC1
6
  * @date    07/02/2010
7
  * @brief   This file provides all the USART firmware functions.
8
  ******************************************************************************
9
  * @copy
10
  *
11
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
14
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
17
  *
18
  * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
19
  */
20
 
21
/* Includes ------------------------------------------------------------------*/
22
#include "stm32l1xx_usart.h"
23
#include "stm32l1xx_rcc.h"
24
 
25
/** @addtogroup STM32L1xx_StdPeriph_Driver
26
  * @{
27
  */
28
 
29
/** @defgroup USART
30
  * @brief USART driver modules
31
  * @{
32
  */
33
 
34
/** @defgroup USART_Private_TypesDefinitions
35
  * @{
36
  */
37
 
38
/**
39
  * @}
40
  */
41
 
42
/** @defgroup USART_Private_Defines
43
  * @{
44
  */
45
 
46
/*!< USART CR1 register clear Mask ((~(uint16_t)0xE9F3)) */
47
#define CR1_CLEAR_MASK            ((uint16_t)(USART_CR1_M | USART_CR1_PCE | \
48
                                              USART_CR1_PS | USART_CR1_TE | \
49
                                              USART_CR1_RE))
50
 
51
/*!< USART CR2 register clock bits clear Mask ((~(uint16_t)0xF0FF)) */
52
#define CR2_CLOCK_CLEAR_MASK      ((uint16_t)(USART_CR2_CLKEN | USART_CR2_CPOL | \
53
                                              USART_CR2_CPHA | USART_CR2_LBCL))
54
 
55
/*!< USART CR3 register clear Mask ((~(uint16_t)0xFCFF)) */
56
#define CR3_CLEAR_MASK            ((uint16_t)(USART_CR3_RTSE | USART_CR3_CTSE))
57
 
58
/*!< USART Interrupts mask */
59
#define IT_MASK                   ((uint16_t)0x001F)
60
 
61
/**
62
  * @}
63
  */
64
 
65
/** @defgroup USART_Private_Macros
66
  * @{
67
  */
68
 
69
/**
70
  * @}
71
  */
72
 
73
/** @defgroup USART_Private_Variables
74
  * @{
75
  */
76
 
77
/**
78
  * @}
79
  */
80
 
81
/** @defgroup USART_Private_FunctionPrototypes
82
  * @{
83
  */
84
 
85
/**
86
  * @}
87
  */
88
 
89
/** @defgroup USART_Private_Functions
90
  * @{
91
  */
92
 
93
/**
94
  * @brief  Deinitializes the USARTx peripheral registers to their default reset values.
95
  * @param  USARTx: Select the USART peripheral.
96
  *   This parameter can be one of the following values: USART1, USART2 or USART3.
97
  * @retval None
98
  */
99
void USART_DeInit(USART_TypeDef* USARTx)
100
{
101
  /* Check the parameters */
102
  assert_param(IS_USART_ALL_PERIPH(USARTx));
103
 
104
  if (USARTx == USART1)
105
  {
106
    RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
107
    RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
108
  }
109
  else if (USARTx == USART2)
110
  {
111
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
112
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
113
  }
114
  else
115
  {
116
    if (USARTx == USART3)
117
    {
118
      RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
119
      RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
120
    }
121
  }
122
}
123
 
124
/**
125
  * @brief  Initializes the USARTx peripheral according to the specified
126
  *   parameters in the USART_InitStruct .
127
  * @param  USARTx: Select the USART peripheral.
128
  *   This parameter can be one of the following values:
129
  *   USART1, USART2 or USART3.
130
  * @param  USART_InitStruct: pointer to a USART_InitTypeDef structure
131
  *   that contains the configuration information for the specified USART peripheral.
132
  * @retval None
133
  */
134
void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
135
{
136
  uint32_t tmpreg = 0x00, apbclock = 0x00;
137
  uint32_t integerdivider = 0x00;
138
  uint32_t fractionaldivider = 0x00;
139
  RCC_ClocksTypeDef RCC_ClocksStatus;
140
 
141
  /* Check the parameters */
142
  assert_param(IS_USART_ALL_PERIPH(USARTx));
143
  assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));
144
  assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
145
  assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
146
  assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));
147
  assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));
148
  assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));
149
 
150
/*---------------------------- USART CR2 Configuration -----------------------*/
151
  tmpreg = USARTx->CR2;
152
  /* Clear STOP[13:12] bits */
153
  tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
154
 
155
  /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/
156
  /* Set STOP[13:12] bits according to USART_StopBits value */
157
  tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;
158
 
159
  /* Write to USART CR2 */
160
  USARTx->CR2 = (uint16_t)tmpreg;
161
 
162
/*---------------------------- USART CR1 Configuration -----------------------*/
163
  tmpreg = USARTx->CR1;
164
  /* Clear M, PCE, PS, TE and RE bits */
165
  tmpreg &= (uint32_t)~((uint32_t)CR1_CLEAR_MASK);
166
 
167
  /* Configure the USART Word Length, Parity and mode ----------------------- */
168
  /* Set the M bits according to USART_WordLength value */
169
  /* Set PCE and PS bits according to USART_Parity value */
170
  /* Set TE and RE bits according to USART_Mode value */
171
  tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
172
            USART_InitStruct->USART_Mode;
173
 
174
  /* Write to USART CR1 */
175
  USARTx->CR1 = (uint16_t)tmpreg;
176
 
177
/*---------------------------- USART CR3 Configuration -----------------------*/
178
  tmpreg = USARTx->CR3;
179
  /* Clear CTSE and RTSE bits */
180
  tmpreg &= (uint32_t)~((uint32_t)CR3_CLEAR_MASK);
181
 
182
  /* Configure the USART HFC -------------------------------------------------*/
183
  /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
184
  tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
185
 
186
  /* Write to USART CR3 */
187
  USARTx->CR3 = (uint16_t)tmpreg;
188
 
189
/*---------------------------- USART BRR Configuration -----------------------*/
190
  /* Configure the USART Baud Rate -------------------------------------------*/
191
  RCC_GetClocksFreq(&RCC_ClocksStatus);
192
  if (USARTx == USART1)
193
  {
194
    apbclock = RCC_ClocksStatus.PCLK2_Frequency;
195
  }
196
  else
197
  {
198
    apbclock = RCC_ClocksStatus.PCLK1_Frequency;
199
  }
200
 
201
  /* Determine the integer part */
202
  if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
203
  {
204
    /* Integer part computing in case Oversampling mode is 8 Samples */
205
    integerdivider = ((25 * apbclock) / (2 * (USART_InitStruct->USART_BaudRate)));
206
  }
207
  else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
208
  {
209
    /* Integer part computing in case Oversampling mode is 16 Samples */
210
    integerdivider = ((25 * apbclock) / (4 * (USART_InitStruct->USART_BaudRate)));
211
  }
212
  tmpreg = (integerdivider / 100) << 4;
213
 
214
  /* Determine the fractional part */
215
  fractionaldivider = integerdivider - (100 * (tmpreg >> 4));
216
 
217
  /* Implement the fractional part in the register */
218
  if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
219
  {
220
    tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
221
  }
222
  else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
223
  {
224
    tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
225
  }
226
 
227
  /* Write to USART BRR */
228
  USARTx->BRR = (uint16_t)tmpreg;
229
}
230
 
231
/**
232
  * @brief  Fills each USART_InitStruct member with its default value.
233
  * @param  USART_InitStruct: pointer to a USART_InitTypeDef structure
234
  *   which will be initialized.
235
  * @retval None
236
  */
237
void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
238
{
239
  /* USART_InitStruct members default value */
240
  USART_InitStruct->USART_BaudRate = 9600;
241
  USART_InitStruct->USART_WordLength = USART_WordLength_8b;
242
  USART_InitStruct->USART_StopBits = USART_StopBits_1;
243
  USART_InitStruct->USART_Parity = USART_Parity_No ;
244
  USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
245
  USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
246
}
247
 
248
/**
249
  * @brief  Initializes the USARTx peripheral Clock according to the
250
  *   specified parameters in the USART_ClockInitStruct .
251
  * @param  USARTx: where x can be 1, 2, 3 to select the USART peripheral.
252
  * @param  USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
253
  *   structure that contains the configuration information for the specified
254
  *   USART peripheral.
255
  * @retval None
256
  */
257
void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)
258
{
259
  uint32_t tmpreg = 0x00;
260
  /* Check the parameters */
261
  assert_param(IS_USART_ALL_PERIPH(USARTx));
262
  assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));
263
  assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));
264
  assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));
265
  assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));
266
 
267
/*---------------------------- USART CR2 Configuration -----------------------*/
268
  tmpreg = USARTx->CR2;
269
  /* Clear CLKEN, CPOL, CPHA and LBCL bits */
270
  tmpreg &= (uint32_t)~((uint32_t)CR2_CLOCK_CLEAR_MASK);
271
  /* Configure the USART Clock, CPOL, CPHA and LastBit ------------*/
272
  /* Set CLKEN bit according to USART_Clock value */
273
  /* Set CPOL bit according to USART_CPOL value */
274
  /* Set CPHA bit according to USART_CPHA value */
275
  /* Set LBCL bit according to USART_LastBit value */
276
  tmpreg |= (uint32_t)USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL |
277
                 USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit;
278
  /* Write to USART CR2 */
279
  USARTx->CR2 = (uint16_t)tmpreg;
280
}
281
 
282
/**
283
  * @brief  Fills each USART_ClockInitStruct member with its default value.
284
  * @param  USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
285
  *   structure which will be initialized.
286
  * @retval None
287
  */
288
void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)
289
{
290
  /* USART_ClockInitStruct members default value */
291
  USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;
292
  USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
293
  USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
294
  USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
295
}
296
 
297
/**
298
  * @brief  Enables or disables the specified USART peripheral.
299
  * @param  USARTx: Select the USART peripheral.
300
  *   This parameter can be one of the following values:
301
  *   USART1, USART2 or USART3.
302
  * @param  NewState: new state of the USARTx peripheral.
303
  *   This parameter can be: ENABLE or DISABLE.
304
  * @retval None
305
  */
306
void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
307
{
308
  /* Check the parameters */
309
  assert_param(IS_USART_ALL_PERIPH(USARTx));
310
  assert_param(IS_FUNCTIONAL_STATE(NewState));
311
 
312
  if (NewState != DISABLE)
313
  {
314
    /* Enable the selected USART by setting the UE bit in the CR1 register */
315
    USARTx->CR1 |= USART_CR1_UE;
316
  }
317
  else
318
  {
319
    /* Disable the selected USART by clearing the UE bit in the CR1 register */
320
    USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_UE);
321
  }
322
}
323
 
324
/**
325
  * @brief  Enables or disables the specified USART interrupts.
326
  * @param  USARTx: Select the USART peripheral.
327
  *   This parameter can be one of the following values:
328
  *   USART1, USART2 or USART3.
329
  * @param  USART_IT: specifies the USART interrupt sources to be enabled or disabled.
330
  *   This parameter can be one of the following values:
331
  *     @arg USART_IT_CTS:  CTS change interrupt (not available for UART4 and UART5)
332
  *     @arg USART_IT_LBD:  LIN Break detection interrupt
333
  *     @arg USART_IT_TXE:  Tansmit Data Register empty interrupt
334
  *     @arg USART_IT_TC:   Transmission complete interrupt
335
  *     @arg USART_IT_RXNE: Receive Data register not empty interrupt
336
  *     @arg USART_IT_IDLE: Idle line detection interrupt
337
  *     @arg USART_IT_PE:   Parity Error interrupt
338
  *     @arg USART_IT_ERR:  Error interrupt(Frame error, noise error, overrun error)
339
  * @param  NewState: new state of the specified USARTx interrupts.
340
  *   This parameter can be: ENABLE or DISABLE.
341
  * @retval None
342
  */
343
void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState)
344
{
345
  uint32_t usartreg = 0x00, itpos = 0x00, itmask = 0x00;
346
  uint32_t usartxbase = 0x00;
347
  /* Check the parameters */
348
  assert_param(IS_USART_ALL_PERIPH(USARTx));
349
  assert_param(IS_USART_CONFIG_IT(USART_IT));
350
  assert_param(IS_FUNCTIONAL_STATE(NewState));
351
 
352
  usartxbase = (uint32_t)USARTx;
353
 
354
  /* Get the USART register index */
355
  usartreg = (((uint8_t)USART_IT) >> 0x05);
356
 
357
  /* Get the interrupt position */
358
  itpos = USART_IT & IT_MASK;
359
  itmask = (((uint32_t)0x01) << itpos);
360
 
361
  if (usartreg == 0x01) /* The IT is in CR1 register */
362
  {
363
    usartxbase += 0x0C;
364
  }
365
  else if (usartreg == 0x02) /* The IT is in CR2 register */
366
  {
367
    usartxbase += 0x10;
368
  }
369
  else /* The IT is in CR3 register */
370
  {
371
    usartxbase += 0x14;
372
  }
373
  if (NewState != DISABLE)
374
  {
375
    *(__IO uint32_t*)usartxbase  |= itmask;
376
  }
377
  else
378
  {
379
    *(__IO uint32_t*)usartxbase &= ~itmask;
380
  }
381
}
382
 
383
/**
384
  * @brief  Enables or disables the USART’s DMA interface.
385
  * @param  USARTx: Select the USART peripheral.
386
  *   This parameter can be one of the following values:
387
  *   USART1, USART2 or USART3.
388
  * @param  USART_DMAReq: specifies the DMA request.
389
  *   This parameter can be any combination of the following values:
390
  *     @arg USART_DMAReq_Tx: USART DMA transmit request
391
  *     @arg USART_DMAReq_Rx: USART DMA receive request
392
  * @param  NewState: new state of the DMA Request sources.
393
  *   This parameter can be: ENABLE or DISABLE.
394
  * @note The DMA mode is not available for UART5.
395
  * @retval None
396
  */
397
void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState)
398
{
399
  /* Check the parameters */
400
  assert_param(IS_USART_ALL_PERIPH(USARTx));
401
  assert_param(IS_USART_DMAREQ(USART_DMAReq));
402
  assert_param(IS_FUNCTIONAL_STATE(NewState));
403
 
404
  if (NewState != DISABLE)
405
  {
406
    /* Enable the DMA transfer for selected requests by setting the DMAT and/or
407
       DMAR bits in the USART CR3 register */
408
    USARTx->CR3 |= USART_DMAReq;
409
  }
410
  else
411
  {
412
    /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
413
       DMAR bits in the USART CR3 register */
414
    USARTx->CR3 &= (uint16_t)~USART_DMAReq;
415
  }
416
}
417
 
418
/**
419
  * @brief  Sets the address of the USART node.
420
  * @param  USARTx: Select the USART peripheral.
421
  *   This parameter can be one of the following values:
422
  *   USART1, USART2 or USART3.
423
  * @param  USART_Address: Indicates the address of the USART node.
424
  * @retval None
425
  */
426
void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)
427
{
428
  /* Check the parameters */
429
  assert_param(IS_USART_ALL_PERIPH(USARTx));
430
  assert_param(IS_USART_ADDRESS(USART_Address));
431
 
432
  /* Clear the USART address */
433
  USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_ADD);
434
  /* Set the USART address node */
435
  USARTx->CR2 |= USART_Address;
436
}
437
 
438
/**
439
  * @brief  Selects the USART WakeUp method.
440
  * @param  USARTx: Select the USART peripheral.
441
  *   This parameter can be one of the following values:
442
  *   USART1, USART2 or USART3.
443
  * @param  USART_WakeUp: specifies the USART wakeup method.
444
  *   This parameter can be one of the following values:
445
  *     @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection
446
  *     @arg USART_WakeUp_AddressMark: WakeUp by an address mark
447
  * @retval None
448
  */
449
void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp)
450
{
451
  /* Check the parameters */
452
  assert_param(IS_USART_ALL_PERIPH(USARTx));
453
  assert_param(IS_USART_WAKEUP(USART_WakeUp));
454
 
455
  USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_WAKE);
456
  USARTx->CR1 |= USART_WakeUp;
457
}
458
 
459
/**
460
  * @brief  Determines if the USART is in mute mode or not.
461
  * @param  USARTx: Select the USART peripheral.
462
  *   This parameter can be one of the following values:
463
  *   USART1, USART2 or USART3.
464
  * @param  NewState: new state of the USART mute mode.
465
  *   This parameter can be: ENABLE or DISABLE.
466
  * @retval None
467
  */
468
void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)
469
{
470
  /* Check the parameters */
471
  assert_param(IS_USART_ALL_PERIPH(USARTx));
472
  assert_param(IS_FUNCTIONAL_STATE(NewState));
473
 
474
  if (NewState != DISABLE)
475
  {
476
    /* Enable the USART mute mode  by setting the RWU bit in the CR1 register */
477
    USARTx->CR1 |= USART_CR1_RWU;
478
  }
479
  else
480
  {
481
    /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */
482
    USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_RWU);
483
  }
484
}
485
 
486
/**
487
  * @brief  Sets the USART LIN Break detection length.
488
  * @param  USARTx: Select the USART peripheral.
489
  *   This parameter can be one of the following values:
490
  *   USART1, USART2 or USART3.
491
  * @param  USART_LINBreakDetectLength: specifies the LIN break detection length.
492
  *   This parameter can be one of the following values:
493
  *     @arg USART_LINBreakDetectLength_10b: 10-bit break detection
494
  *     @arg USART_LINBreakDetectLength_11b: 11-bit break detection
495
  * @retval None
496
  */
497
void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength)
498
{
499
  /* Check the parameters */
500
  assert_param(IS_USART_ALL_PERIPH(USARTx));
501
  assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));
502
 
503
  USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_LBDL);
504
  USARTx->CR2 |= USART_LINBreakDetectLength;
505
}
506
 
507
/**
508
  * @brief  Enables or disables the USART’s LIN mode.
509
  * @param  USARTx: Select the USART peripheral.
510
  *   This parameter can be one of the following values:
511
  *   USART1, USART2 or USART3.
512
  * @param  NewState: new state of the USART LIN mode.
513
  *   This parameter can be: ENABLE or DISABLE.
514
  * @retval None
515
  */
516
void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)
517
{
518
  /* Check the parameters */
519
  assert_param(IS_USART_ALL_PERIPH(USARTx));
520
  assert_param(IS_FUNCTIONAL_STATE(NewState));
521
 
522
  if (NewState != DISABLE)
523
  {
524
    /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
525
    USARTx->CR2 |= USART_CR2_LINEN;
526
  }
527
  else
528
  {
529
    /* Disable the LIN mode by clearing the LINEN bit in the CR2 register */
530
    USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_LINEN);
531
  }
532
}
533
 
534
/**
535
  * @brief  Transmits single data through the USARTx peripheral.
536
  * @param  USARTx: Select the USART peripheral.
537
  *   This parameter can be one of the following values:
538
  *   USART1, USART2 or USART3.
539
  * @param  Data: the data to transmit.
540
  * @retval None
541
  */
542
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
543
{
544
  /* Check the parameters */
545
  assert_param(IS_USART_ALL_PERIPH(USARTx));
546
  assert_param(IS_USART_DATA(Data));
547
 
548
  /* Transmit Data */
549
  USARTx->DR = (Data & (uint16_t)0x01FF);
550
}
551
 
552
/**
553
  * @brief  Returns the most recent received data by the USARTx peripheral.
554
  * @param  USARTx: Select the USART peripheral.
555
  *   This parameter can be one of the following values:
556
  *   USART1, USART2 or USART3.
557
  * @retval The received data.
558
  */
559
uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
560
{
561
  /* Check the parameters */
562
  assert_param(IS_USART_ALL_PERIPH(USARTx));
563
 
564
  /* Receive Data */
565
  return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);
566
}
567
 
568
/**
569
  * @brief  Transmits break characters.
570
  * @param  USARTx: Select the USART peripheral.
571
  *   This parameter can be one of the following values:
572
  *   USART1, USART2 or USART3.
573
  * @retval None
574
  */
575
void USART_SendBreak(USART_TypeDef* USARTx)
576
{
577
  /* Check the parameters */
578
  assert_param(IS_USART_ALL_PERIPH(USARTx));
579
 
580
  /* Send break characters */
581
  USARTx->CR1 |= USART_CR1_SBK;
582
}
583
 
584
/**
585
  * @brief  Sets the specified USART guard time.
586
  * @param  USARTx: Select the USART peripheral.
587
  *   This parameter can be one of the following values:
588
  *   USART1, USART2 or USART3.
589
  * @param  USART_GuardTime: specifies the guard time.
590
  * @note The guard time bits are not available for UART4 and UART5.
591
  * @retval None
592
  */
593
void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)
594
{
595
  /* Check the parameters */
596
  assert_param(IS_USART_ALL_PERIPH(USARTx));
597
 
598
  /* Clear the USART Guard time */
599
  USARTx->GTPR &= USART_GTPR_PSC;
600
  /* Set the USART guard time */
601
  USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);
602
}
603
 
604
/**
605
  * @brief  Sets the system clock prescaler.
606
  * @param  USARTx: Select the USART peripheral.
607
  *   This parameter can be one of the following values:
608
  *   USART1, USART2 or USART3.
609
  * @param  USART_Prescaler: specifies the prescaler clock.
610
  * @note   The function is used for IrDA mode with UART4 and UART5.
611
  * @retval None
612
  */
613
void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)
614
{
615
  /* Check the parameters */
616
  assert_param(IS_USART_ALL_PERIPH(USARTx));
617
 
618
  /* Clear the USART prescaler */
619
  USARTx->GTPR &= USART_GTPR_GT;
620
  /* Set the USART prescaler */
621
  USARTx->GTPR |= USART_Prescaler;
622
}
623
 
624
/**
625
  * @brief  Enables or disables the USART’s Smart Card mode.
626
  * @param  USARTx: Select the USART peripheral.
627
  *   This parameter can be one of the following values:
628
  *   USART1, USART2 or USART3.
629
  * @param  NewState: new state of the Smart Card mode.
630
  *   This parameter can be: ENABLE or DISABLE.
631
  * @note The Smart Card mode is not available for UART4 and UART5.
632
  * @retval None
633
  */
634
void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)
635
{
636
  /* Check the parameters */
637
  assert_param(IS_USART_ALL_PERIPH(USARTx));
638
  assert_param(IS_FUNCTIONAL_STATE(NewState));
639
  if (NewState != DISABLE)
640
  {
641
    /* Enable the SC mode by setting the SCEN bit in the CR3 register */
642
    USARTx->CR3 |= USART_CR3_SCEN;
643
  }
644
  else
645
  {
646
    /* Disable the SC mode by clearing the SCEN bit in the CR3 register */
647
    USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_SCEN);
648
  }
649
}
650
 
651
/**
652
  * @brief  Enables or disables NACK transmission.
653
  * @param  USARTx: Select the USART peripheral.
654
  *   This parameter can be one of the following values:
655
  *   USART1, USART2 or USART3.
656
  * @param  NewState: new state of the NACK transmission.
657
  *   This parameter can be: ENABLE or DISABLE.
658
  * @note The Smart Card mode is not available for UART4 and UART5.
659
  * @retval None
660
  */
661
void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)
662
{
663
  /* Check the parameters */
664
  assert_param(IS_USART_ALL_PERIPH(USARTx));
665
  assert_param(IS_FUNCTIONAL_STATE(NewState));
666
  if (NewState != DISABLE)
667
  {
668
    /* Enable the NACK transmission by setting the NACK bit in the CR3 register */
669
    USARTx->CR3 |= USART_CR3_NACK;
670
  }
671
  else
672
  {
673
    /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */
674
    USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_NACK);
675
  }
676
}
677
 
678
/**
679
  * @brief  Enables or disables the USART’s Half Duplex communication.
680
  * @param  USARTx: Select the USART peripheral.
681
  *   This parameter can be one of the following values:
682
  *   USART1, USART2 or USART3.
683
  * @param  NewState: new state of the USART Communication.
684
  *   This parameter can be: ENABLE or DISABLE.
685
  * @retval None
686
  */
687
void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
688
{
689
  /* Check the parameters */
690
  assert_param(IS_USART_ALL_PERIPH(USARTx));
691
  assert_param(IS_FUNCTIONAL_STATE(NewState));
692
 
693
  if (NewState != DISABLE)
694
  {
695
    /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
696
    USARTx->CR3 |= USART_CR3_HDSEL;
697
  }
698
  else
699
  {
700
    /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */
701
    USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_HDSEL);
702
  }
703
}
704
 
705
/**
706
  * @brief  Enables or disables the USART's 8x oversampling mode.
707
  * @param USARTx: Select the USART or the UART peripheral.
708
  *   This parameter can be one of the following values:
709
  *     USART1, USART2, USART3.
710
  * @param NewState: new state of the USART 8x oversampling mode.
711
  *   This parameter can be: ENABLE or DISABLE.
712
  *
713
  * @note
714
  *   This function has to be called before calling USART_Init()
715
  *   function in order to have correct baudrate Divider value.
716
  * @retval : None
717
  */
718
void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
719
{
720
  /* Check the parameters */
721
  assert_param(IS_USART_ALL_PERIPH(USARTx));
722
  assert_param(IS_FUNCTIONAL_STATE(NewState));
723
 
724
  if (NewState != DISABLE)
725
  {
726
    /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */
727
    USARTx->CR1 |= USART_CR1_OVER8;
728
  }
729
  else
730
  {
731
    /* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */
732
    USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_OVER8);
733
  }
734
}
735
 
736
/**
737
  * @brief  Enables or disables the USART's one bit sampling methode.
738
  * @param USARTx: Select the USART or the UART peripheral.
739
  *   This parameter can be one of the following values:
740
  *   USART1, USART2, USART3.
741
  * @param NewState: new state of the USART one bit sampling methode.
742
  *   This parameter can be: ENABLE or DISABLE.
743
  * @retval : None
744
  */
745
void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)
746
{
747
  /* Check the parameters */
748
  assert_param(IS_USART_ALL_PERIPH(USARTx));
749
  assert_param(IS_FUNCTIONAL_STATE(NewState));
750
 
751
  if (NewState != DISABLE)
752
  {
753
    /* Enable the one bit method by setting the ONEBITE bit in the CR3 register */
754
    USARTx->CR3 |= USART_CR3_ONEBIT;
755
  }
756
  else
757
  {
758
    /* Disable tthe one bit method by clearing the ONEBITE bit in the CR3 register */
759
    USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_ONEBIT);
760
  }
761
}
762
 
763
/**
764
  * @brief  Configures the USART’s IrDA interface.
765
  * @param  USARTx: Select the USART peripheral.
766
  *   This parameter can be one of the following values:
767
  *   USART1, USART2 or USART3.
768
  * @param  USART_IrDAMode: specifies the IrDA mode.
769
  *   This parameter can be one of the following values:
770
  *     @arg USART_IrDAMode_LowPower
771
  *     @arg USART_IrDAMode_Normal
772
  * @retval None
773
  */
774
void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode)
775
{
776
  /* Check the parameters */
777
  assert_param(IS_USART_ALL_PERIPH(USARTx));
778
  assert_param(IS_USART_IRDA_MODE(USART_IrDAMode));
779
 
780
  USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_IRLP);
781
  USARTx->CR3 |= USART_IrDAMode;
782
}
783
 
784
/**
785
  * @brief  Enables or disables the USART’s IrDA interface.
786
  * @param  USARTx: Select the USART peripheral.
787
  *   This parameter can be one of the following values:
788
  *   USART1, USART2 or USART3.
789
  * @param  NewState: new state of the IrDA mode.
790
  *   This parameter can be: ENABLE or DISABLE.
791
  * @retval None
792
  */
793
void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)
794
{
795
  /* Check the parameters */
796
  assert_param(IS_USART_ALL_PERIPH(USARTx));
797
  assert_param(IS_FUNCTIONAL_STATE(NewState));
798
 
799
  if (NewState != DISABLE)
800
  {
801
    /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
802
    USARTx->CR3 |= USART_CR3_IREN;
803
  }
804
  else
805
  {
806
    /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
807
    USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_IREN);
808
  }
809
}
810
 
811
/**
812
  * @brief  Checks whether the specified USART flag is set or not.
813
  * @param  USARTx: Select the USART peripheral.
814
  *   This parameter can be one of the following values:
815
  *   USART1, USART2 or USART3.
816
  * @param  USART_FLAG: specifies the flag to check.
817
  *   This parameter can be one of the following values:
818
  *     @arg USART_FLAG_CTS:  CTS Change flag
819
  *     @arg USART_FLAG_LBD:  LIN Break detection flag
820
  *     @arg USART_FLAG_TXE:  Transmit data register empty flag
821
  *     @arg USART_FLAG_TC:   Transmission Complete flag
822
  *     @arg USART_FLAG_RXNE: Receive data register not empty flag
823
  *     @arg USART_FLAG_IDLE: Idle Line detection flag
824
  *     @arg USART_FLAG_ORE:  OverRun Error flag
825
  *     @arg USART_FLAG_NE:   Noise Error flag
826
  *     @arg USART_FLAG_FE:   Framing Error flag
827
  *     @arg USART_FLAG_PE:   Parity Error flag
828
  * @retval The new state of USART_FLAG (SET or RESET).
829
  */
830
FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)
831
{
832
  FlagStatus bitstatus = RESET;
833
  /* Check the parameters */
834
  assert_param(IS_USART_ALL_PERIPH(USARTx));
835
  assert_param(IS_USART_FLAG(USART_FLAG));
836
 
837
  if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET)
838
  {
839
    bitstatus = SET;
840
  }
841
  else
842
  {
843
    bitstatus = RESET;
844
  }
845
  return bitstatus;
846
}
847
 
848
/**
849
  * @brief  Clears the USARTx's pending flags.
850
  * @param  USARTx: Select the USART peripheral.
851
  *   This parameter can be one of the following values:
852
  *   USART1, USART2 or USART3.
853
  * @param  USART_FLAG: specifies the flag to clear.
854
  *   This parameter can be any combination of the following values:
855
  *     @arg USART_FLAG_CTS:  CTS Change flag.
856
  *     @arg USART_FLAG_LBD:  LIN Break detection flag.
857
  *     @arg USART_FLAG_TC:   Transmission Complete flag.
858
  *     @arg USART_FLAG_RXNE: Receive data register not empty flag.
859
  *
860
  * @note
861
  *   - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
862
  *     error) and IDLE (Idle line detected) flags are cleared by software
863
  *     sequence: a read operation to USART_SR register (USART_GetFlagStatus())
864
  *     followed by a read operation to USART_DR register (USART_ReceiveData()).
865
  *   - RXNE flag can be also cleared by a read to the USART_DR register
866
  *     (USART_ReceiveData()).
867
  *   - TC flag can be also cleared by software sequence: a read operation to
868
  *     USART_SR register (USART_GetFlagStatus()) followed by a write operation
869
  *     to USART_DR register (USART_SendData()).
870
  *   - TXE flag is cleared only by a write to the USART_DR register
871
  *     (USART_SendData()).
872
  * @retval None
873
  */
874
void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG)
875
{
876
  /* Check the parameters */
877
  assert_param(IS_USART_ALL_PERIPH(USARTx));
878
  assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));
879
 
880
  USARTx->SR = (uint16_t)~USART_FLAG;
881
}
882
 
883
/**
884
  * @brief  Checks whether the specified USART interrupt has occurred or not.
885
  * @param  USARTx: Select the USART peripheral.
886
  *   This parameter can be one of the following values:
887
  *   USART1, USART2 or USART3.
888
  * @param  USART_IT: specifies the USART interrupt source to check.
889
  *   This parameter can be one of the following values:
890
  *     @arg USART_IT_CTS:  CTS change interrupt
891
  *     @arg USART_IT_LBD:  LIN Break detection interrupt
892
  *     @arg USART_IT_TXE:  Tansmit Data Register empty interrupt
893
  *     @arg USART_IT_TC:   Transmission complete interrupt
894
  *     @arg USART_IT_RXNE: Receive Data register not empty interrupt
895
  *     @arg USART_IT_IDLE: Idle line detection interrupt
896
  *     @arg USART_IT_ORE:  OverRun Error interrupt
897
  *     @arg USART_IT_NE:   Noise Error interrupt
898
  *     @arg USART_IT_FE:   Framing Error interrupt
899
  *     @arg USART_IT_PE:   Parity Error interrupt
900
  * @retval The new state of USART_IT (SET or RESET).
901
  */
902
ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)
903
{
904
  uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00;
905
  ITStatus bitstatus = RESET;
906
  /* Check the parameters */
907
  assert_param(IS_USART_ALL_PERIPH(USARTx));
908
  assert_param(IS_USART_GET_IT(USART_IT));
909
 
910
  /* Get the USART register index */
911
  usartreg = (((uint8_t)USART_IT) >> 0x05);
912
  /* Get the interrupt position */
913
  itmask = USART_IT & IT_MASK;
914
  itmask = (uint32_t)0x01 << itmask;
915
 
916
  if (usartreg == 0x01) /* The IT  is in CR1 register */
917
  {
918
    itmask &= USARTx->CR1;
919
  }
920
  else if (usartreg == 0x02) /* The IT  is in CR2 register */
921
  {
922
    itmask &= USARTx->CR2;
923
  }
924
  else /* The IT  is in CR3 register */
925
  {
926
    itmask &= USARTx->CR3;
927
  }
928
 
929
  bitpos = USART_IT >> 0x08;
930
  bitpos = (uint32_t)0x01 << bitpos;
931
  bitpos &= USARTx->SR;
932
  if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))
933
  {
934
    bitstatus = SET;
935
  }
936
  else
937
  {
938
    bitstatus = RESET;
939
  }
940
 
941
  return bitstatus;
942
}
943
 
944
/**
945
  * @brief  Clears the USARTx’s interrupt pending bits.
946
  * @param  USARTx: Select the USART peripheral.
947
  *   This parameter can be one of the following values:
948
  *   USART1, USART2 or USART3.
949
  * @param  USART_IT: specifies the interrupt pending bit to clear.
950
  *   This parameter can be one of the following values:
951
  *     @arg USART_IT_CTS:  CTS change interrupt
952
  *     @arg USART_IT_LBD:  LIN Break detection interrupt
953
  *     @arg USART_IT_TC:   Transmission complete interrupt.
954
  *     @arg USART_IT_RXNE: Receive Data register not empty interrupt.
955
  *
956
  * @note
957
  *   - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
958
  *     error) and IDLE (Idle line detected) pending bits are cleared by
959
  *     software sequence: a read operation to USART_SR register
960
  *     (USART_GetITStatus()) followed by a read operation to USART_DR register
961
  *     (USART_ReceiveData()).
962
  *   - RXNE pending bit can be also cleared by a read to the USART_DR register
963
  *     (USART_ReceiveData()).
964
  *   - TC pending bit can be also cleared by software sequence: a read
965
  *     operation to USART_SR register (USART_GetITStatus()) followed by a write
966
  *     operation to USART_DR register (USART_SendData()).
967
  *   - TXE pending bit is cleared only by a write to the USART_DR register
968
  *     (USART_SendData()).
969
  * @retval None
970
  */
971
void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT)
972
{
973
  uint16_t bitpos = 0x00, itmask = 0x00;
974
  /* Check the parameters */
975
  assert_param(IS_USART_ALL_PERIPH(USARTx));
976
  assert_param(IS_USART_CLEAR_IT(USART_IT));
977
 
978
  bitpos = USART_IT >> 0x08;
979
  itmask = ((uint16_t)0x01 << (uint16_t)bitpos);
980
  USARTx->SR = (uint16_t)~itmask;
981
}
982
 
983
/**
984
  * @}
985
  */
986
 
987
/**
988
  * @}
989
  */
990
 
991
/**
992
  * @}
993
  */
994
 
995
/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.