provide default initialization of some local variables in serial_api.c:serial_format()

pull/396/head
Rohit Grover 2014-07-09 10:52:37 +01:00
parent aae8513739
commit 9413ed9163
1 changed files with 16 additions and 15 deletions

View File

@ -84,13 +84,13 @@ static struct serial_global_data_s uart_data[UART_NUM];
void serial_init(serial_t *obj, PinName tx, PinName rx) { void serial_init(serial_t *obj, PinName tx, PinName rx) {
int is_stdio_uart = 0; int is_stdio_uart = 0;
// determine the UART to use // determine the UART to use
UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
MBED_ASSERT((int)uart != NC); MBED_ASSERT((int)uart != NC);
obj->uart = (LPC_UART_TypeDef *)uart; obj->uart = (LPC_UART_TypeDef *)uart;
// enable power // enable power
switch (uart) { switch (uart) {
@ -110,19 +110,19 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
obj->uart->IER = 0 << 0 // Rx Data available irq enable obj->uart->IER = 0 << 0 // Rx Data available irq enable
| 0 << 1 // Tx Fifo empty irq enable | 0 << 1 // Tx Fifo empty irq enable
| 0 << 2; // Rx Line Status irq enable | 0 << 2; // Rx Line Status irq enable
// set default baud rate and format // set default baud rate and format
serial_baud (obj, 9600); serial_baud (obj, 9600);
serial_format(obj, 8, ParityNone, 1); serial_format(obj, 8, ParityNone, 1);
// pinout the chosen uart // pinout the chosen uart
pinmap_pinout(tx, PinMap_UART_TX); pinmap_pinout(tx, PinMap_UART_TX);
pinmap_pinout(rx, PinMap_UART_RX); pinmap_pinout(rx, PinMap_UART_RX);
// set rx/tx pins in PullUp mode // set rx/tx pins in PullUp mode
pin_mode(tx, PullUp); pin_mode(tx, PullUp);
pin_mode(rx, PullUp); pin_mode(rx, PullUp);
switch (uart) { switch (uart) {
case UART_0: obj->index = 0; break; case UART_0: obj->index = 0; break;
case UART_1: obj->index = 1; break; case UART_1: obj->index = 1; break;
@ -132,9 +132,9 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
uart_data[obj->index].sw_rts.pin = NC; uart_data[obj->index].sw_rts.pin = NC;
uart_data[obj->index].sw_cts.pin = NC; uart_data[obj->index].sw_cts.pin = NC;
serial_set_flow_control(obj, FlowControlNone, NC, NC); serial_set_flow_control(obj, FlowControlNone, NC, NC);
is_stdio_uart = (uart == STDIO_UART) ? (1) : (0); is_stdio_uart = (uart == STDIO_UART) ? (1) : (0);
if (is_stdio_uart) { if (is_stdio_uart) {
stdio_uart_inited = 1; stdio_uart_inited = 1;
memcpy(&stdio_uart, obj, sizeof(serial_t)); memcpy(&stdio_uart, obj, sizeof(serial_t));
@ -166,9 +166,9 @@ void serial_baud(serial_t *obj, int baudrate) {
case UART_3: LPC_SC->PCLKSEL1 &= ~(0x3 << 18); LPC_SC->PCLKSEL1 |= (0x1 << 18); break; case UART_3: LPC_SC->PCLKSEL1 &= ~(0x3 << 18); LPC_SC->PCLKSEL1 |= (0x1 << 18); break;
default: break; default: break;
} }
uint32_t PCLK = SystemCoreClock; uint32_t PCLK = SystemCoreClock;
// First we check to see if the basic divide with no DivAddVal/MulVal // First we check to see if the basic divide with no DivAddVal/MulVal
// ratio gives us an integer result. If it does, we set DivAddVal = 0, // ratio gives us an integer result. If it does, we set DivAddVal = 0,
// MulVal = 1. Otherwise, we search the valid ratio value range to find // MulVal = 1. Otherwise, we search the valid ratio value range to find
@ -229,16 +229,16 @@ void serial_baud(serial_t *obj, int baudrate) {
} }
} }
} }
// set LCR[DLAB] to enable writing to divider registers // set LCR[DLAB] to enable writing to divider registers
obj->uart->LCR |= (1 << 7); obj->uart->LCR |= (1 << 7);
// set divider values // set divider values
obj->uart->DLM = (DL >> 8) & 0xFF; obj->uart->DLM = (DL >> 8) & 0xFF;
obj->uart->DLL = (DL >> 0) & 0xFF; obj->uart->DLL = (DL >> 0) & 0xFF;
obj->uart->FDR = (uint32_t) DivAddVal << 0 obj->uart->FDR = (uint32_t) DivAddVal << 0
| (uint32_t) MulVal << 4; | (uint32_t) MulVal << 4;
// clear LCR[DLAB] // clear LCR[DLAB]
obj->uart->LCR &= ~(1 << 7); obj->uart->LCR &= ~(1 << 7);
} }
@ -260,9 +260,10 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
case ParityForced1: parity_enable = 1; parity_select = 2; break; case ParityForced1: parity_enable = 1; parity_select = 2; break;
case ParityForced0: parity_enable = 1; parity_select = 3; break; case ParityForced0: parity_enable = 1; parity_select = 3; break;
default: default:
parity_enable = 0, parity_select = 0;
break; break;
} }
obj->uart->LCR = data_bits << 0 obj->uart->LCR = data_bits << 0
| stop_bits << 2 | stop_bits << 2
| parity_enable << 3 | parity_enable << 3
@ -310,7 +311,7 @@ static void serial_irq_set_internal(serial_t *obj, SerialIrq irq, uint32_t enabl
case UART_2: irq_n=UART2_IRQn; vector = (uint32_t)&uart2_irq; break; case UART_2: irq_n=UART2_IRQn; vector = (uint32_t)&uart2_irq; break;
case UART_3: irq_n=UART3_IRQn; vector = (uint32_t)&uart3_irq; break; case UART_3: irq_n=UART3_IRQn; vector = (uint32_t)&uart3_irq; break;
} }
if (enable) { if (enable) {
obj->uart->IER |= 1 << irq; obj->uart->IER |= 1 << irq;
NVIC_SetVector(irq_n, vector); NVIC_SetVector(irq_n, vector);