Skip to content

Commit 320b891

Browse files
committed
Merge mainline updates
1 parent 6f69452 commit 320b891

File tree

1 file changed

+22
-27
lines changed
  • libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC43XX

1 file changed

+22
-27
lines changed

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC43XX/serial_api.c

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
* Ported to NXP LPC43XX by Micromint USA <[email protected]>
1717
*/
1818
// math.h required for floating point operations for baud rate calculation
19+
#include "mbed_assert.h"
1920
#include <math.h>
2021
#include <string.h>
2122
#include <stdlib.h>
2223

2324
#include "serial_api.h"
2425
#include "cmsis.h"
2526
#include "pinmap.h"
26-
#include "mbed_error.h"
2727
#include "gpio_api.h"
2828

2929
/******************************************************************************
@@ -111,14 +111,12 @@ static struct serial_global_data_s uart_data[UART_NUM];
111111

112112
void serial_init(serial_t *obj, PinName tx, PinName rx) {
113113
int is_stdio_uart = 0;
114-
114+
115115
// determine the UART to use
116116
UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
117117
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
118118
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);
122120

123121
obj->uart = (LPC_USART_T *)uart;
124122

@@ -132,23 +130,23 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
132130
obj->uart->IER = 0 << 0 // Rx Data available irq enable
133131
| 0 << 1 // Tx Fifo empty irq enable
134132
| 0 << 2; // Rx Line Status irq enable
135-
133+
136134
// set default baud rate and format
137135
serial_baud (obj, 9600);
138136
serial_format(obj, 8, ParityNone, 1);
139-
137+
140138
// pinout the chosen uart
141139
pinmap_pinout(tx, PinMap_UART_TX);
142140
pinmap_pinout(rx, PinMap_UART_RX);
143-
141+
144142
// set rx/tx pins in PullUp mode
145143
if (tx != NC) {
146144
pin_mode(tx, PullUp);
147145
}
148146
if (rx != NC) {
149147
pin_mode(rx, PullUp);
150148
}
151-
149+
152150
switch (uart) {
153151
case UART_0: obj->index = 0; break;
154152
case UART_1: obj->index = 1; break;
@@ -159,7 +157,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
159157
uart_data[obj->index].sw_cts.pin = NC;
160158
serial_set_flow_control(obj, FlowControlNone, NC, NC);
161159

162-
is_stdio_uart = (uart == STDIO_UART) ? (1) : (0);
160+
is_stdio_uart = (uart == STDIO_UART) ? (1) : (0);
163161

164162
if (is_stdio_uart) {
165163
stdio_uart_inited = 1;
@@ -175,7 +173,7 @@ void serial_free(serial_t *obj) {
175173
// set the baud rate, taking in to account the current SystemFrequency
176174
void serial_baud(serial_t *obj, int baudrate) {
177175
uint32_t PCLK = SystemCoreClock;
178-
176+
179177
// First we check to see if the basic divide with no DivAddVal/MulVal
180178
// ratio gives us an integer result. If it does, we set DivAddVal = 0,
181179
// MulVal = 1. Otherwise, we search the valid ratio value range to find
@@ -236,31 +234,27 @@ void serial_baud(serial_t *obj, int baudrate) {
236234
}
237235
}
238236
}
239-
237+
240238
// set LCR[DLAB] to enable writing to divider registers
241239
obj->uart->LCR |= (1 << 7);
242-
240+
243241
// set divider values
244242
obj->uart->DLM = (DL >> 8) & 0xFF;
245243
obj->uart->DLL = (DL >> 0) & 0xFF;
246244
obj->uart->FDR = (uint32_t) DivAddVal << 0
247245
| (uint32_t) MulVal << 4;
248-
246+
249247
// clear LCR[DLAB]
250248
obj->uart->LCR &= ~(1 << 7);
251249
}
252250

253251
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+
258257
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-
}
264258
data_bits -= 5;
265259

266260
int parity_enable, parity_select;
@@ -271,10 +265,10 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
271265
case ParityForced1: parity_enable = 1; parity_select = 2; break;
272266
case ParityForced0: parity_enable = 1; parity_select = 3; break;
273267
default:
274-
error("Invalid serial parity setting");
275-
return;
268+
parity_enable = 0, parity_select = 0;
269+
break;
276270
}
277-
271+
278272
obj->uart->LCR = data_bits << 0
279273
| stop_bits << 2
280274
| parity_enable << 3
@@ -322,7 +316,7 @@ static void serial_irq_set_internal(serial_t *obj, SerialIrq irq, uint32_t enabl
322316
case UART_2: irq_n=USART2_IRQn; vector = (uint32_t)&uart2_irq; break;
323317
case UART_3: irq_n=USART3_IRQn; vector = (uint32_t)&uart3_irq; break;
324318
}
325-
319+
326320
if (enable) {
327321
obj->uart->IER |= 1 << irq;
328322
NVIC_SetVector(irq_n, vector);
@@ -409,3 +403,4 @@ void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, Pi
409403
#if (DEVICE_SERIAL_FC)
410404
#endif
411405
}
406+

0 commit comments

Comments
 (0)