I'm building a simple circuit with LPC1114 to make a "hello world" application using UART. Trying to write all the code from scratch, to have a better understanding on what's happening.
My problem is that UART only works only after flashing (i.e. when lpc21isp asks MCU to start running code). The same code doesn't work if MCU is reset.
Here's the code I'm using:
void platform_uart_setup(uint32_t baud_rate) // 9600
{
// Make sure UART IRQ is disabled
NVIC_DisableIRQ(UART_IRQn);
// Setup pin 1_6 as RXD
LPC_IOCON->PIO1_6 &= ~0x07;
LPC_IOCON->PIO1_6 |= 0x01;
// Setup pin 1_7 as TXD
LPC_IOCON->PIO1_7 &= ~0x07;
LPC_IOCON->PIO1_7 |= 0x01;
// Enable & configure UART clock
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12);
LPC_SYSCON->UARTCLKDIV = 0x1;
// Setup format: 8N1, enable access to divisor latches
LPC_UART->LCR = 0x83;
// Setup baud rate, which is based on system clock
uint32_t Fdiv = platform_clock // take cpu clock (12000000 in this case)
/ LPC_SYSCON->SYSAHBCLKDIV // divide by ABH clock
/ LPC_SYSCON->UARTCLKDIV // divide further by UART clock
/ 16 // divisor latch is 16x the desired baud rate
/ baud_rate;
LPC_UART->DLM = Fdiv / 256;
LPC_UART->DLL = Fdiv % 256;
LPC_UART->FDR = 0x00 | (1 << 4) | 0;
// Enable and reset FIFOs
LPC_UART->FCR = 0x07;
// Disable access to divisor latches
LPC_UART->LCR = 0x03;
// Read to reset LSR
volatile uint32_t unused = LPC_UART->LSR;
// Make sure there's no data
while(( LPC_UART->LSR & (0x20|0x40)) != (0x20|0x40) )
;
while( LPC_UART->LSR & 0x01 ) {
unused = LPC_UART->RBR;
}
// Enable UART IRQ
NVIC_EnableIRQ(UART_IRQn);
// We don't care about interrupts for now
LPC_UART->IER = 0;
}
void platform_uart_putc(const char c)
{
while( !(LPC_UART->LSR & 0x20) )
;
LPC_UART->THR = c;
}
On after the reset there are absolutely no signal changes on TXD pin.