16
16
* Ported to NXP LPC43XX by Micromint USA <[email protected] >
17
17
*/
18
18
// math.h required for floating point operations for baud rate calculation
19
+ #include "mbed_assert.h"
19
20
#include <math.h>
20
21
#include <string.h>
21
22
#include <stdlib.h>
22
23
23
24
#include "serial_api.h"
24
25
#include "cmsis.h"
25
26
#include "pinmap.h"
26
- #include "mbed_error.h"
27
27
#include "gpio_api.h"
28
28
29
29
/******************************************************************************
@@ -111,14 +111,12 @@ static struct serial_global_data_s uart_data[UART_NUM];
111
111
112
112
void serial_init (serial_t * obj , PinName tx , PinName rx ) {
113
113
int is_stdio_uart = 0 ;
114
-
114
+
115
115
// determine the UART to use
116
116
UARTName uart_tx = (UARTName )pinmap_peripheral (tx , PinMap_UART_TX );
117
117
UARTName uart_rx = (UARTName )pinmap_peripheral (rx , PinMap_UART_RX );
118
118
UARTName uart = (UARTName )pinmap_merge (uart_tx , uart_rx );
119
- if ((int )uart == NC ) {
120
- error ("Serial pinout mapping failed" );
121
- }
119
+ MBED_ASSERT ((int )uart != NC );
122
120
123
121
obj -> uart = (LPC_USART_T * )uart ;
124
122
@@ -132,23 +130,23 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
132
130
obj -> uart -> IER = 0 << 0 // Rx Data available irq enable
133
131
| 0 << 1 // Tx Fifo empty irq enable
134
132
| 0 << 2 ; // Rx Line Status irq enable
135
-
133
+
136
134
// set default baud rate and format
137
135
serial_baud (obj , 9600 );
138
136
serial_format (obj , 8 , ParityNone , 1 );
139
-
137
+
140
138
// pinout the chosen uart
141
139
pinmap_pinout (tx , PinMap_UART_TX );
142
140
pinmap_pinout (rx , PinMap_UART_RX );
143
-
141
+
144
142
// set rx/tx pins in PullUp mode
145
143
if (tx != NC ) {
146
144
pin_mode (tx , PullUp );
147
145
}
148
146
if (rx != NC ) {
149
147
pin_mode (rx , PullUp );
150
148
}
151
-
149
+
152
150
switch (uart ) {
153
151
case UART_0 : obj -> index = 0 ; break ;
154
152
case UART_1 : obj -> index = 1 ; break ;
@@ -159,7 +157,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
159
157
uart_data [obj -> index ].sw_cts .pin = NC ;
160
158
serial_set_flow_control (obj , FlowControlNone , NC , NC );
161
159
162
- is_stdio_uart = (uart == STDIO_UART ) ? (1 ) : (0 );
160
+ is_stdio_uart = (uart == STDIO_UART ) ? (1 ) : (0 );
163
161
164
162
if (is_stdio_uart ) {
165
163
stdio_uart_inited = 1 ;
@@ -175,7 +173,7 @@ void serial_free(serial_t *obj) {
175
173
// set the baud rate, taking in to account the current SystemFrequency
176
174
void serial_baud (serial_t * obj , int baudrate ) {
177
175
uint32_t PCLK = SystemCoreClock ;
178
-
176
+
179
177
// First we check to see if the basic divide with no DivAddVal/MulVal
180
178
// ratio gives us an integer result. If it does, we set DivAddVal = 0,
181
179
// MulVal = 1. Otherwise, we search the valid ratio value range to find
@@ -236,31 +234,27 @@ void serial_baud(serial_t *obj, int baudrate) {
236
234
}
237
235
}
238
236
}
239
-
237
+
240
238
// set LCR[DLAB] to enable writing to divider registers
241
239
obj -> uart -> LCR |= (1 << 7 );
242
-
240
+
243
241
// set divider values
244
242
obj -> uart -> DLM = (DL >> 8 ) & 0xFF ;
245
243
obj -> uart -> DLL = (DL >> 0 ) & 0xFF ;
246
244
obj -> uart -> FDR = (uint32_t ) DivAddVal << 0
247
245
| (uint32_t ) MulVal << 4 ;
248
-
246
+
249
247
// clear LCR[DLAB]
250
248
obj -> uart -> LCR &= ~(1 << 7 );
251
249
}
252
250
253
251
void serial_format (serial_t * obj , int data_bits , SerialParity parity , int stop_bits ) {
254
- // 0: 1 stop bits, 1: 2 stop bits
255
- if (stop_bits != 1 && stop_bits != 2 ) {
256
- error ("Invalid stop bits specified" );
257
- }
252
+ MBED_ASSERT ((stop_bits == 1 ) || (stop_bits == 2 )); // 0: 1 stop bits, 1: 2 stop bits
253
+ MBED_ASSERT ((data_bits > 4 ) && (data_bits < 9 )); // 0: 5 data bits ... 3: 8 data bits
254
+ MBED_ASSERT ((parity == ParityNone ) || (parity == ParityOdd ) || (parity == ParityEven ) ||
255
+ (parity == ParityForced1 ) || (parity == ParityForced0 ));
256
+
258
257
stop_bits -= 1 ;
259
-
260
- // 0: 5 data bits ... 3: 8 data bits
261
- if (data_bits < 5 || data_bits > 8 ) {
262
- error ("Invalid number of bits (%d) in serial format, should be 5..8" , data_bits );
263
- }
264
258
data_bits -= 5 ;
265
259
266
260
int parity_enable , parity_select ;
@@ -271,10 +265,10 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
271
265
case ParityForced1 : parity_enable = 1 ; parity_select = 2 ; break ;
272
266
case ParityForced0 : parity_enable = 1 ; parity_select = 3 ; break ;
273
267
default :
274
- error ( "Invalid serial parity setting" ) ;
275
- return ;
268
+ parity_enable = 0 , parity_select = 0 ;
269
+ break ;
276
270
}
277
-
271
+
278
272
obj -> uart -> LCR = data_bits << 0
279
273
| stop_bits << 2
280
274
| parity_enable << 3
@@ -322,7 +316,7 @@ static void serial_irq_set_internal(serial_t *obj, SerialIrq irq, uint32_t enabl
322
316
case UART_2 : irq_n = USART2_IRQn ; vector = (uint32_t )& uart2_irq ; break ;
323
317
case UART_3 : irq_n = USART3_IRQn ; vector = (uint32_t )& uart3_irq ; break ;
324
318
}
325
-
319
+
326
320
if (enable ) {
327
321
obj -> uart -> IER |= 1 << irq ;
328
322
NVIC_SetVector (irq_n , vector );
@@ -409,3 +403,4 @@ void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, Pi
409
403
#if (DEVICE_SERIAL_FC )
410
404
#endif
411
405
}
406
+
0 commit comments