Skip to content

Commit e61b2f1

Browse files
committed
make sure we remove any function from a physical pin so that we can assign a new function to it.
1 parent 850bb11 commit e61b2f1

File tree

1 file changed

+31
-41
lines changed
  • libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC15XX

1 file changed

+31
-41
lines changed

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

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ static const SWM_Map SWM_UART_RX[] = {
4242
static const SWM_Map SWM_UART_RTS[] = {
4343
{0, 16},
4444
{1, 24},
45-
{3, 0},
45+
{3, 0}, // not available
4646
};
4747

4848
static const SWM_Map SWM_UART_CTS[] = {
4949
{0, 24},
5050
{2, 0},
51-
{3, 8}
51+
{3, 8} // not available
5252
};
5353

5454
// bit flags for used UARTs
@@ -82,9 +82,29 @@ static uart_irq_handler irq_handler;
8282
int stdio_uart_inited = 0;
8383
serial_t stdio_uart;
8484

85+
static void switch_pin(const SWM_Map *swm, PinName pn)
86+
{
87+
uint32_t regVal;
88+
if (pn != NC)
89+
{
90+
// check if we have any function mapped to this pin already and remove it
91+
for (int n = 0; n < sizeof(LPC_SWM->PINASSIGN)/sizeof(*LPC_SWM->PINASSIGN); n ++) {
92+
regVal = LPC_SWM->PINASSIGN[n];
93+
for (int j = 0; j <= 24; j += 8) {
94+
if (((regVal >> j) & 0xFF) == pn)
95+
regVal |= (0xFF << j);
96+
}
97+
LPC_SWM->PINASSIGN[n] = regVal;
98+
}
99+
}
100+
// now map it
101+
regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
102+
LPC_SWM->PINASSIGN[swm->n] = regVal | (pn << swm->offset);
103+
}
104+
85105
void serial_init(serial_t *obj, PinName tx, PinName rx) {
86106
int is_stdio_uart = 0;
87-
107+
88108
int uart_n = get_available_uart();
89109
if (uart_n == -1) {
90110
error("No available UART");
@@ -93,16 +113,8 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
93113
obj->uart = (LPC_USART0_Type *)(LPC_USART0_BASE + (0x4000 * uart_n));
94114
uart_used |= (1 << uart_n);
95115

96-
const SWM_Map *swm;
97-
uint32_t regVal;
98-
99-
swm = &SWM_UART_TX[uart_n];
100-
regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
101-
LPC_SWM->PINASSIGN[swm->n] = regVal | (tx << swm->offset);
102-
103-
swm = &SWM_UART_RX[uart_n];
104-
regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
105-
LPC_SWM->PINASSIGN[swm->n] = regVal | (rx << swm->offset);
116+
switch_pin(&SWM_UART_TX[uart_n], tx);
117+
switch_pin(&SWM_UART_RX[uart_n], rx);
106118

107119
/* uart clock divided by 6 */
108120
LPC_SYSCON->UARTCLKDIV =6;
@@ -296,33 +308,11 @@ void serial_break_clear(serial_t *obj) {
296308
}
297309

298310
void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow) {
299-
const SWM_Map *swm_rts, *swm_cts;
300-
uint32_t regVal_rts, regVal_cts;
301-
302-
swm_rts = &SWM_UART_RTS[obj->index];
303-
swm_cts = &SWM_UART_CTS[obj->index];
304-
regVal_rts = LPC_SWM->PINASSIGN[swm_rts->n] & ~(0xFF << swm_rts->offset);
305-
regVal_cts = LPC_SWM->PINASSIGN[swm_cts->n] & ~(0xFF << swm_cts->offset);
306-
307-
if (FlowControlNone == type) {
308-
LPC_SWM->PINASSIGN[swm_rts->n] = regVal_rts | (0xFF << swm_rts->offset);
309-
LPC_SWM->PINASSIGN[swm_cts->n] = regVal_cts | (0xFF << swm_cts->offset);
310-
obj->uart->CFG &= ~CTSEN;
311-
return;
312-
}
313-
if ((FlowControlRTS == type || FlowControlRTSCTS == type) && (rxflow != NC)) {
314-
LPC_SWM->PINASSIGN[swm_rts->n] = regVal_rts | (rxflow << swm_rts->offset);
315-
if (FlowControlRTS == type) {
316-
LPC_SWM->PINASSIGN[swm_cts->n] = regVal_cts | (0xFF << swm_cts->offset);
317-
obj->uart->CFG &= ~CTSEN;
318-
}
319-
}
320-
if ((FlowControlCTS == type || FlowControlRTSCTS == type) && (txflow != NC)) {
321-
LPC_SWM->PINASSIGN[swm_cts->n] = regVal_cts | (txflow << swm_cts->offset);
322-
obj->uart->CFG |= CTSEN;
323-
if (FlowControlCTS == type) {
324-
LPC_SWM->PINASSIGN[swm_rts->n] = regVal_rts | (0xFF << swm_rts->offset);
325-
}
326-
}
311+
if ((FlowControlNone == type || FlowControlRTS == type)) txflow = NC;
312+
if ((FlowControlNone == type || FlowControlCTS == type)) rxflow = NC;
313+
switch_pin(&SWM_UART_RTS[obj->index], rxflow);
314+
switch_pin(&SWM_UART_CTS[obj->index], txflow);
315+
if (txflow == NC) obj->uart->CFG &= ~CTSEN;
316+
else obj->uart->CFG |= CTSEN;
327317
}
328318

0 commit comments

Comments
 (0)