Skip to content

Commit 0e6d7ac

Browse files
committed
Merge pull request #1009 from toyowata/master
LPC824 - Add UART channel duplication check
2 parents bcc032c + 2bc9c42 commit 0e6d7ac

File tree

1 file changed

+30
-1
lines changed
  • libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC82X

1 file changed

+30
-1
lines changed

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,39 @@ static uart_irq_handler irq_handler;
9090
int stdio_uart_inited = 0;
9191
serial_t stdio_uart;
9292

93+
static int check_duplication(serial_t *obj, PinName tx, PinName rx)
94+
{
95+
if (uart_used == 0)
96+
return 0;
97+
98+
const SWM_Map *swm;
99+
uint32_t assigned_tx, assigned_rx;
100+
int ch;
101+
for (ch=0; ch<UART_NUM; ch++) {
102+
// read assigned TX in the UART channel of switch matrix
103+
swm = &SWM_UART_TX[ch];
104+
assigned_tx = LPC_SWM->PINASSIGN[swm->n] & (0xFF << swm->offset);
105+
assigned_tx = assigned_tx >> swm->offset;
106+
// read assigned RX in the UART channel of switch matrix
107+
swm = &SWM_UART_RX[ch];
108+
assigned_rx = LPC_SWM->PINASSIGN[swm->n] & (0xFF << swm->offset);
109+
assigned_rx = assigned_rx >> swm->offset;
110+
if ((assigned_tx == (uint32_t)(tx >> PIN_SHIFT)) && (assigned_rx == (uint32_t)(rx >> PIN_SHIFT))) {
111+
obj->index = ch;
112+
obj->uart = (LPC_USART0_Type *)(LPC_USART0_BASE + (0x4000 * ch));
113+
return 1;
114+
}
115+
}
116+
return 0;
117+
}
118+
93119
void serial_init(serial_t *obj, PinName tx, PinName rx)
94120
{
95121
int is_stdio_uart = 0;
96122

123+
if (check_duplication(obj, tx, rx) == 1)
124+
return;
125+
97126
int uart_n = get_available_uart();
98127
if (uart_n == -1) {
99128
error("No available UART");
@@ -192,7 +221,7 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
192221
stop_bits -= 1;
193222
data_bits -= 7;
194223

195-
int paritysel;
224+
int paritysel = 0;
196225
switch (parity) {
197226
case ParityNone: paritysel = 0; break;
198227
case ParityEven: paritysel = 2; break;

0 commit comments

Comments
 (0)