Subversion Repositories LedShow

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 mjames 1
/**
2
  ******************************************************************************
3
  * @file    stm32f1xx_ll_usart.c
4
  * @author  MCD Application Team
5
  * @brief   USART LL module driver.
6
  ******************************************************************************
7
  * @attention
8
  *
9
  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
10
  *
11
  * Redistribution and use in source and binary forms, with or without modification,
12
  * are permitted provided that the following conditions are met:
13
  *   1. Redistributions of source code must retain the above copyright notice,
14
  *      this list of conditions and the following disclaimer.
15
  *   2. Redistributions in binary form must reproduce the above copyright notice,
16
  *      this list of conditions and the following disclaimer in the documentation
17
  *      and/or other materials provided with the distribution.
18
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
19
  *      may be used to endorse or promote products derived from this software
20
  *      without specific prior written permission.
21
  *
22
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
  *
33
  ******************************************************************************
34
  */
35
#if defined(USE_FULL_LL_DRIVER)
36
 
37
/* Includes ------------------------------------------------------------------*/
38
#include "stm32f1xx_ll_usart.h"
39
#include "stm32f1xx_ll_rcc.h"
40
#include "stm32f1xx_ll_bus.h"
41
#ifdef  USE_FULL_ASSERT
42
#include "stm32_assert.h"
43
#else
44
#define assert_param(expr) ((void)0U)
45
#endif
46
 
47
/** @addtogroup STM32F1xx_LL_Driver
48
  * @{
49
  */
50
 
51
#if defined (USART1) || defined (USART2) || defined (USART3) || defined (UART4) || defined (UART5)
52
 
53
/** @addtogroup USART_LL
54
  * @{
55
  */
56
 
57
/* Private types -------------------------------------------------------------*/
58
/* Private variables ---------------------------------------------------------*/
59
/* Private constants ---------------------------------------------------------*/
60
/** @addtogroup USART_LL_Private_Constants
61
  * @{
62
  */
63
 
64
/**
65
  * @}
66
  */
67
 
68
 
69
/* Private macros ------------------------------------------------------------*/
70
/** @addtogroup USART_LL_Private_Macros
71
  * @{
72
  */
73
 
74
/* __BAUDRATE__ The maximum Baud Rate is derived from the maximum clock available
75
 *              divided by the smallest oversampling used on the USART (i.e. 8)    */
76
#define IS_LL_USART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) <= 10000000U)
77
 
78
#define IS_LL_USART_DIRECTION(__VALUE__) (((__VALUE__) == LL_USART_DIRECTION_NONE) \
79
                                       || ((__VALUE__) == LL_USART_DIRECTION_RX) \
80
                                       || ((__VALUE__) == LL_USART_DIRECTION_TX) \
81
                                       || ((__VALUE__) == LL_USART_DIRECTION_TX_RX))
82
 
83
#define IS_LL_USART_PARITY(__VALUE__) (((__VALUE__) == LL_USART_PARITY_NONE) \
84
                                    || ((__VALUE__) == LL_USART_PARITY_EVEN) \
85
                                    || ((__VALUE__) == LL_USART_PARITY_ODD))
86
 
87
#define IS_LL_USART_DATAWIDTH(__VALUE__) (((__VALUE__) == LL_USART_DATAWIDTH_8B) \
88
                                       || ((__VALUE__) == LL_USART_DATAWIDTH_9B))
89
 
90
#define IS_LL_USART_OVERSAMPLING(__VALUE__) (((__VALUE__) == LL_USART_OVERSAMPLING_16) \
91
                                          || ((__VALUE__) == LL_USART_OVERSAMPLING_8))
92
 
93
#define IS_LL_USART_LASTBITCLKOUTPUT(__VALUE__) (((__VALUE__) == LL_USART_LASTCLKPULSE_NO_OUTPUT) \
94
                                              || ((__VALUE__) == LL_USART_LASTCLKPULSE_OUTPUT))
95
 
96
#define IS_LL_USART_CLOCKPHASE(__VALUE__) (((__VALUE__) == LL_USART_PHASE_1EDGE) \
97
                                        || ((__VALUE__) == LL_USART_PHASE_2EDGE))
98
 
99
#define IS_LL_USART_CLOCKPOLARITY(__VALUE__) (((__VALUE__) == LL_USART_POLARITY_LOW) \
100
                                           || ((__VALUE__) == LL_USART_POLARITY_HIGH))
101
 
102
#define IS_LL_USART_CLOCKOUTPUT(__VALUE__) (((__VALUE__) == LL_USART_CLOCK_DISABLE) \
103
                                         || ((__VALUE__) == LL_USART_CLOCK_ENABLE))
104
 
105
#define IS_LL_USART_STOPBITS(__VALUE__) (((__VALUE__) == LL_USART_STOPBITS_0_5) \
106
                                      || ((__VALUE__) == LL_USART_STOPBITS_1) \
107
                                      || ((__VALUE__) == LL_USART_STOPBITS_1_5) \
108
                                      || ((__VALUE__) == LL_USART_STOPBITS_2))
109
 
110
#define IS_LL_USART_HWCONTROL(__VALUE__) (((__VALUE__) == LL_USART_HWCONTROL_NONE) \
111
                                       || ((__VALUE__) == LL_USART_HWCONTROL_RTS) \
112
                                       || ((__VALUE__) == LL_USART_HWCONTROL_CTS) \
113
                                       || ((__VALUE__) == LL_USART_HWCONTROL_RTS_CTS))
114
 
115
/**
116
  * @}
117
  */
118
 
119
/* Private function prototypes -----------------------------------------------*/
120
 
121
/* Exported functions --------------------------------------------------------*/
122
/** @addtogroup USART_LL_Exported_Functions
123
  * @{
124
  */
125
 
126
/** @addtogroup USART_LL_EF_Init
127
  * @{
128
  */
129
 
130
/**
131
  * @brief  De-initialize USART registers (Registers restored to their default values).
132
  * @param  USARTx USART Instance
133
  * @retval An ErrorStatus enumeration value:
134
  *          - SUCCESS: USART registers are de-initialized
135
  *          - ERROR: USART registers are not de-initialized
136
  */
137
ErrorStatus LL_USART_DeInit(USART_TypeDef *USARTx)
138
{
139
  ErrorStatus status = SUCCESS;
140
 
141
  /* Check the parameters */
142
  assert_param(IS_UART_INSTANCE(USARTx));
143
 
144
  if (USARTx == USART1)
145
  {
146
    /* Force reset of USART clock */
147
    LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_USART1);
148
 
149
    /* Release reset of USART clock */
150
    LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_USART1);
151
  }
152
  else if (USARTx == USART2)
153
  {
154
    /* Force reset of USART clock */
155
    LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_USART2);
156
 
157
    /* Release reset of USART clock */
158
    LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_USART2);
159
  }
160
#if defined(USART3)
161
  else if (USARTx == USART3)
162
  {
163
    /* Force reset of USART clock */
164
    LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_USART3);
165
 
166
    /* Release reset of USART clock */
167
    LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_USART3);
168
  }
169
#endif /* USART3 */
170
#if defined(UART4)
171
  else if (USARTx == UART4)
172
  {
173
    /* Force reset of UART clock */
174
    LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART4);
175
 
176
    /* Release reset of UART clock */
177
    LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART4);
178
  }
179
#endif /* UART4 */
180
#if defined(UART5)
181
  else if (USARTx == UART5)
182
  {
183
    /* Force reset of UART clock */
184
    LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_UART5);
185
 
186
    /* Release reset of UART clock */
187
    LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_UART5);
188
  }
189
#endif /* UART5 */
190
  else
191
  {
192
    status = ERROR;
193
  }
194
 
195
  return (status);
196
}
197
 
198
/**
199
  * @brief  Initialize USART registers according to the specified
200
  *         parameters in USART_InitStruct.
201
  * @note   As some bits in USART configuration registers can only be written when the USART is disabled (USART_CR1_UE bit =0),
202
  *         USART IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
203
  * @note   Baud rate value stored in USART_InitStruct BaudRate field, should be valid (different from 0).
204
  * @param  USARTx USART Instance
205
  * @param  USART_InitStruct: pointer to a LL_USART_InitTypeDef structure
206
  *         that contains the configuration information for the specified USART peripheral.
207
  * @retval An ErrorStatus enumeration value:
208
  *          - SUCCESS: USART registers are initialized according to USART_InitStruct content
209
  *          - ERROR: Problem occurred during USART Registers initialization
210
  */
211
ErrorStatus LL_USART_Init(USART_TypeDef *USARTx, LL_USART_InitTypeDef *USART_InitStruct)
212
{
213
  ErrorStatus status = ERROR;
214
  uint32_t periphclk = LL_RCC_PERIPH_FREQUENCY_NO;
215
  LL_RCC_ClocksTypeDef rcc_clocks;
216
 
217
  /* Check the parameters */
218
  assert_param(IS_UART_INSTANCE(USARTx));
219
  assert_param(IS_LL_USART_BAUDRATE(USART_InitStruct->BaudRate));
220
  assert_param(IS_LL_USART_DATAWIDTH(USART_InitStruct->DataWidth));
221
  assert_param(IS_LL_USART_STOPBITS(USART_InitStruct->StopBits));
222
  assert_param(IS_LL_USART_PARITY(USART_InitStruct->Parity));
223
  assert_param(IS_LL_USART_DIRECTION(USART_InitStruct->TransferDirection));
224
  assert_param(IS_LL_USART_HWCONTROL(USART_InitStruct->HardwareFlowControl));
225
#if defined(USART_CR1_OVER8)
226
  assert_param(IS_LL_USART_OVERSAMPLING(USART_InitStruct->OverSampling));
227
#endif /* USART_OverSampling_Feature */
228
 
229
  /* USART needs to be in disabled state, in order to be able to configure some bits in
230
     CRx registers */
231
  if (LL_USART_IsEnabled(USARTx) == 0U)
232
  {
233
    /*---------------------------- USART CR1 Configuration -----------------------
234
     * Configure USARTx CR1 (USART Word Length, Parity, Mode and Oversampling bits) with parameters:
235
     * - DataWidth:          USART_CR1_M bits according to USART_InitStruct->DataWidth value
236
     * - Parity:             USART_CR1_PCE, USART_CR1_PS bits according to USART_InitStruct->Parity value
237
     * - TransferDirection:  USART_CR1_TE, USART_CR1_RE bits according to USART_InitStruct->TransferDirection value
238
     * - Oversampling:       USART_CR1_OVER8 bit according to USART_InitStruct->OverSampling value.
239
     */
240
#if defined(USART_CR1_OVER8)
241
    MODIFY_REG(USARTx->CR1,
242
               (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS |
243
                USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8),
244
               (USART_InitStruct->DataWidth | USART_InitStruct->Parity |
245
                USART_InitStruct->TransferDirection | USART_InitStruct->OverSampling));
246
#else
247
    MODIFY_REG(USARTx->CR1,
248
               (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS |
249
                USART_CR1_TE | USART_CR1_RE),
250
               (USART_InitStruct->DataWidth | USART_InitStruct->Parity |
251
                USART_InitStruct->TransferDirection));
252
#endif /* USART_OverSampling_Feature */
253
 
254
    /*---------------------------- USART CR2 Configuration -----------------------
255
     * Configure USARTx CR2 (Stop bits) with parameters:
256
     * - Stop Bits:          USART_CR2_STOP bits according to USART_InitStruct->StopBits value.
257
     * - CLKEN, CPOL, CPHA and LBCL bits are to be configured using LL_USART_ClockInit().
258
     */
259
    LL_USART_SetStopBitsLength(USARTx, USART_InitStruct->StopBits);
260
 
261
    /*---------------------------- USART CR3 Configuration -----------------------
262
     * Configure USARTx CR3 (Hardware Flow Control) with parameters:
263
     * - HardwareFlowControl: USART_CR3_RTSE, USART_CR3_CTSE bits according to USART_InitStruct->HardwareFlowControl value.
264
     */
265
    LL_USART_SetHWFlowCtrl(USARTx, USART_InitStruct->HardwareFlowControl);
266
 
267
    /*---------------------------- USART BRR Configuration -----------------------
268
     * Retrieve Clock frequency used for USART Peripheral
269
     */
270
    LL_RCC_GetSystemClocksFreq(&rcc_clocks);
271
    if (USARTx == USART1)
272
    {
273
      periphclk = rcc_clocks.PCLK2_Frequency;
274
    }
275
    else if (USARTx == USART2)
276
    {
277
      periphclk = rcc_clocks.PCLK1_Frequency;
278
    }
279
#if defined(USART3)
280
    else if (USARTx == USART3)
281
    {
282
      periphclk = rcc_clocks.PCLK1_Frequency;
283
    }
284
#endif /* USART3 */
285
#if defined(UART4)
286
    else if (USARTx == UART4)
287
    {
288
      periphclk = rcc_clocks.PCLK1_Frequency;
289
    }
290
#endif /* UART4 */
291
#if defined(UART5)
292
    else if (USARTx == UART5)
293
    {
294
      periphclk = rcc_clocks.PCLK1_Frequency;
295
    }
296
#endif /* UART5 */
297
    else
298
    {
299
      /* Nothing to do, as error code is already assigned to ERROR value */
300
    }
301
 
302
    /* Configure the USART Baud Rate :
303
       - valid baud rate value (different from 0) is required
304
       - Peripheral clock as returned by RCC service, should be valid (different from 0).
305
    */
306
    if ((periphclk != LL_RCC_PERIPH_FREQUENCY_NO)
307
        && (USART_InitStruct->BaudRate != 0U))
308
    {
309
      status = SUCCESS;
310
#if defined(USART_CR1_OVER8)
311
      LL_USART_SetBaudRate(USARTx,
312
                           periphclk,
313
                           USART_InitStruct->OverSampling,
314
                           USART_InitStruct->BaudRate);
315
#else
316
      LL_USART_SetBaudRate(USARTx,
317
                           periphclk,
318
                           USART_InitStruct->BaudRate);
319
#endif /* USART_OverSampling_Feature */
320
    }
321
  }
322
  /* Endif (=> USART not in Disabled state => return ERROR) */
323
 
324
  return (status);
325
}
326
 
327
/**
328
  * @brief Set each @ref LL_USART_InitTypeDef field to default value.
329
  * @param USART_InitStruct: pointer to a @ref LL_USART_InitTypeDef structure
330
  *                          whose fields will be set to default values.
331
  * @retval None
332
  */
333
 
334
void LL_USART_StructInit(LL_USART_InitTypeDef *USART_InitStruct)
335
{
336
  /* Set USART_InitStruct fields to default values */
337
  USART_InitStruct->BaudRate            = 9600U;
338
  USART_InitStruct->DataWidth           = LL_USART_DATAWIDTH_8B;
339
  USART_InitStruct->StopBits            = LL_USART_STOPBITS_1;
340
  USART_InitStruct->Parity              = LL_USART_PARITY_NONE ;
341
  USART_InitStruct->TransferDirection   = LL_USART_DIRECTION_TX_RX;
342
  USART_InitStruct->HardwareFlowControl = LL_USART_HWCONTROL_NONE;
343
#if defined(USART_CR1_OVER8)
344
  USART_InitStruct->OverSampling        = LL_USART_OVERSAMPLING_16;
345
#endif /* USART_OverSampling_Feature */
346
}
347
 
348
/**
349
  * @brief  Initialize USART Clock related settings according to the
350
  *         specified parameters in the USART_ClockInitStruct.
351
  * @note   As some bits in USART configuration registers can only be written when the USART is disabled (USART_CR1_UE bit =0),
352
  *         USART IP should be in disabled state prior calling this function. Otherwise, ERROR result will be returned.
353
  * @param  USARTx USART Instance
354
  * @param  USART_ClockInitStruct: pointer to a @ref LL_USART_ClockInitTypeDef structure
355
  *         that contains the Clock configuration information for the specified USART peripheral.
356
  * @retval An ErrorStatus enumeration value:
357
  *          - SUCCESS: USART registers related to Clock settings are initialized according to USART_ClockInitStruct content
358
  *          - ERROR: Problem occurred during USART Registers initialization
359
  */
360
ErrorStatus LL_USART_ClockInit(USART_TypeDef *USARTx, LL_USART_ClockInitTypeDef *USART_ClockInitStruct)
361
{
362
  ErrorStatus status = SUCCESS;
363
 
364
  /* Check USART Instance and Clock signal output parameters */
365
  assert_param(IS_UART_INSTANCE(USARTx));
366
  assert_param(IS_LL_USART_CLOCKOUTPUT(USART_ClockInitStruct->ClockOutput));
367
 
368
  /* USART needs to be in disabled state, in order to be able to configure some bits in
369
     CRx registers */
370
  if (LL_USART_IsEnabled(USARTx) == 0U)
371
  {
372
    /*---------------------------- USART CR2 Configuration -----------------------*/
373
    /* If Clock signal has to be output */
374
    if (USART_ClockInitStruct->ClockOutput == LL_USART_CLOCK_DISABLE)
375
    {
376
      /* Deactivate Clock signal delivery :
377
       * - Disable Clock Output:        USART_CR2_CLKEN cleared
378
       */
379
      LL_USART_DisableSCLKOutput(USARTx);
380
    }
381
    else
382
    {
383
      /* Ensure USART instance is USART capable */
384
      assert_param(IS_USART_INSTANCE(USARTx));
385
 
386
      /* Check clock related parameters */
387
      assert_param(IS_LL_USART_CLOCKPOLARITY(USART_ClockInitStruct->ClockPolarity));
388
      assert_param(IS_LL_USART_CLOCKPHASE(USART_ClockInitStruct->ClockPhase));
389
      assert_param(IS_LL_USART_LASTBITCLKOUTPUT(USART_ClockInitStruct->LastBitClockPulse));
390
 
391
      /*---------------------------- USART CR2 Configuration -----------------------
392
       * Configure USARTx CR2 (Clock signal related bits) with parameters:
393
       * - Enable Clock Output:         USART_CR2_CLKEN set
394
       * - Clock Polarity:              USART_CR2_CPOL bit according to USART_ClockInitStruct->ClockPolarity value
395
       * - Clock Phase:                 USART_CR2_CPHA bit according to USART_ClockInitStruct->ClockPhase value
396
       * - Last Bit Clock Pulse Output: USART_CR2_LBCL bit according to USART_ClockInitStruct->LastBitClockPulse value.
397
       */
398
      MODIFY_REG(USARTx->CR2,
399
                 USART_CR2_CLKEN | USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_LBCL,
400
                 USART_CR2_CLKEN | USART_ClockInitStruct->ClockPolarity |
401
                 USART_ClockInitStruct->ClockPhase | USART_ClockInitStruct->LastBitClockPulse);
402
    }
403
  }
404
  /* Else (USART not in Disabled state => return ERROR */
405
  else
406
  {
407
    status = ERROR;
408
  }
409
 
410
  return (status);
411
}
412
 
413
/**
414
  * @brief Set each field of a @ref LL_USART_ClockInitTypeDef type structure to default value.
415
  * @param USART_ClockInitStruct: pointer to a @ref LL_USART_ClockInitTypeDef structure
416
  *                               whose fields will be set to default values.
417
  * @retval None
418
  */
419
void LL_USART_ClockStructInit(LL_USART_ClockInitTypeDef *USART_ClockInitStruct)
420
{
421
  /* Set LL_USART_ClockInitStruct fields with default values */
422
  USART_ClockInitStruct->ClockOutput       = LL_USART_CLOCK_DISABLE;
423
  USART_ClockInitStruct->ClockPolarity     = LL_USART_POLARITY_LOW;            /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
424
  USART_ClockInitStruct->ClockPhase        = LL_USART_PHASE_1EDGE;             /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
425
  USART_ClockInitStruct->LastBitClockPulse = LL_USART_LASTCLKPULSE_NO_OUTPUT;  /* Not relevant when ClockOutput = LL_USART_CLOCK_DISABLE */
426
}
427
 
428
/**
429
  * @}
430
  */
431
 
432
/**
433
  * @}
434
  */
435
 
436
/**
437
  * @}
438
  */
439
 
440
#endif /* USART1 || USART2 || USART3 || UART4 || UART5 */
441
 
442
/**
443
  * @}
444
  */
445
 
446
#endif /* USE_FULL_LL_DRIVER */
447
 
448
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
449