LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
usart_004.c
Go to the documentation of this file.
1 /*
2  * @brief UART/USART registers and control functions
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2012
6  * All rights reserved.
7  *
8  * @par
9  * Software that is described herein is for illustrative purposes only
10  * which provides customers with programming information regarding the
11  * LPC products. This software is supplied "AS IS" without any warranties of
12  * any kind, and NXP Semiconductors and its licensor disclaim any and
13  * all warranties, express or implied, including all implied warranties of
14  * merchantability, fitness for a particular purpose and non-infringement of
15  * intellectual property rights. NXP Semiconductors assumes no responsibility
16  * or liability for the use of the software, conveys no license or rights under any
17  * patent, copyright, mask work right, or any other intellectual property rights in
18  * or to any products. NXP Semiconductors reserves the right to make changes
19  * in the software without notification. NXP Semiconductors also makes no
20  * representation or warranty that such application will be suitable for the
21  * specified use without further testing or modification.
22  *
23  * @par
24  * Permission to use, copy, modify, and distribute this software and its
25  * documentation is hereby granted, under NXP Semiconductors' and its
26  * licensor's relevant copyrights in the software, without fee, provided that it
27  * is used in conjunction with NXP Semiconductors microcontrollers. This
28  * copyright, permission, and disclaimer notice must appear in all copies of
29  * this code.
30  */
31 
32 #include "usart_004.h"
33 
34 /*****************************************************************************
35  * Private types/enumerations/variables
36  ****************************************************************************/
37 
38 /* Baud-rate pre-scaler multiplier value */
39 #define UART_FDR_MULVAL(n) (((n) << 4) & 0xF0)
40 
41 /* Acceptable UART baudrate error */
42 #define UART_ACCEPTED_BAUDRATE_ERROR (3)
43 
44 /*****************************************************************************
45  * Public types/enumerations/variables
46  ****************************************************************************/
47 
48 /*****************************************************************************
49  * Private functions
50  ****************************************************************************/
51 
52 /*****************************************************************************
53  * Public functions
54  ****************************************************************************/
55 
56 /* Basic UART initialization */
58 {
59  /* Enable FIFOs by default, reset them */
61 
62  /* Default 8N1, with DLAB disabled */
64 
65  /* Disable fractional divider */
66  pUART->FDR = 0x10;
67 }
68 
69 // FIXME - this function doesn't work correctly and is too big
70 /* Determines and sets best dividers to get a target baud rate */
72 {
73  uint32_t actualRate = 0, d, m, bestd, bestm, tmp;
74  uint64_t best_divisor, divisor;
75  uint32_t current_error, best_error;
76  uint32_t recalcbaud;
77 
78  /* In the Uart IP block, baud rate is calculated using FDR and DLL-DLM registers
79  * The formula is :
80  * BaudRate= uClk * (mulFracDiv/(mulFracDiv+dividerAddFracDiv) / (16 * (DLL)
81  * It involves floating point calculations. That's the reason the formulae are adjusted with
82  * Multiply and divide method.*/
83  /* The value of mulFracDiv and dividerAddFracDiv should comply to the following expressions:
84  * 0 < mulFracDiv <= 15, 0 <= dividerAddFracDiv <= 15 */
85  best_error = 0xFFFFFFFF;/* Worst case */
86  bestd = 0;
87  bestm = 0;
88  best_divisor = 0;
89  for (m = 1; m <= 15; m++) {
90  for (d = 0; d < m; d++) {
91  divisor = ((uint64_t) uClk << 28) * m / (baudrate * (m + d));
92  current_error = divisor & 0xFFFFFFFF;
93 
94  tmp = divisor >> 32;
95 
96  /* Adjust error */
97  if (current_error > ((uint32_t) 1 << 31)) {
98  current_error = -current_error;
99  tmp++;
100  }
101 
102  if (( tmp < 1) || ( tmp > 65536)) {
103  /* Out of range */
104  continue;
105  }
106 
107  if ( current_error < best_error) {
108  best_error = current_error;
109  best_divisor = tmp;
110  bestd = d;
111  bestm = m;
112  if (best_error == 0) {
113  break;
114  }
115  }
116  } /* for (d) */
117 
118  if (best_error == 0) {
119  break;
120  }
121  } /* for (m) */
122 
123  if (best_divisor == 0) {
124  /* can not find best match */
125  return 0;
126  }
127 
128  recalcbaud = (uClk >> 4) * bestm / (best_divisor * (bestm + bestd));
129 
130  /* reuse best_error to evaluate baud error */
131  if (baudrate > recalcbaud) {
132  best_error = baudrate - recalcbaud;
133  }
134  else {
135  best_error = recalcbaud - baudrate;
136  }
137 
138  best_error = (best_error * 100) / baudrate;
139 
140  if (best_error < UART_ACCEPTED_BAUDRATE_ERROR) {
142  // FIXME - this is not how DLL and DLM work - they do not add
143  IP_UART_SetDivisorLatches(pUART, best_divisor, best_divisor);
145 
146  /* Set best fractional divider */
147  pUART->FDR = (UART_FDR_MULVAL(bestm) | bestd);
148 
149  /* Return actual baud rate */
150  actualRate = recalcbaud;
151  }
152 
153  return actualRate;
154 }