LPC800-MAX and UART problem

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LPC800-MAX and UART problem

1,492 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Cosik on Thu Feb 27 10:01:00 MST 2014
Hi,

I just try to run UART on my LPC800-MAX, but it's didn't working. I think that I don't configure or set all necessary registers.
Here are my code:

uint8_t key;
uint32_t clk, i;
uint32_t baudRate = 115200;
const uint32_t UARTCLKDIV = 1;

/* Enable IOCON clock */
LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 18);

/* Setup the clock and reset UART0 */
LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 14)|(1 << 15)|(1 << 16);
LPC_SYSCON->PRESETCTRL &= ~(1 << 3);
LPC_SYSCON->PRESETCTRL |= (1 << 3);
LPC_SYSCON->PRESETCTRL &= ~(1 << 2);
LPC_SYSCON->PRESETCTRL |= (1 << 2);
NVIC_DisableIRQ(UART0_IRQn);

/* Enable the clock to the Switch Matrix */
LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 7);

///* Pin Assign 8 bit Configuration */
///* U0_TXD */
///* U0_RXD */
//LPC_SWM->PINASSIGN0 = 0xffff0004UL;
//
///* Pin Assign 1 bit Configuration */
///* SWCLK */
///* SWDIO */
///* RESET */
//LPC_SWM->PINENABLE0 = 0xffffffb3UL;

    /* Pin Assign 8 bit Configuration */
    /* U1_TXD */
    LPC_SWM->PINASSIGN1 = 0xffff0fffUL;

    /* Pin Assign 1 bit Configuration */
    /* SWCLK */
    /* SWDIO */
    /* RESET */
    LPC_SWM->PINENABLE0 = 0xffffffb3UL;

LPC_SYSCON->UARTCLKDIV = UARTCLKDIV;

/* Configure UART0 */
clk = __MAIN_CLOCK / UARTCLKDIV;
LPC_USART1->CFG |= (1<<2);
LPC_USART1->BRG = clk / 16 / baudRate - 1;
LPC_SYSCON->UARTFRGDIV = 0xFF;
LPC_SYSCON->UARTFRGMULT = (((clk / 16) * (LPC_SYSCON->UARTFRGDIV + 1))
/ (baudRate * (LPC_USART1->BRG + 1)))
- (LPC_SYSCON->UARTFRGDIV + 1);

/* Clear the status bits */
LPC_USART1->STAT = (1<<5) | (1<<11);

/* Enable UART0 */
LPC_USART1->CFG |= (1<<0);

while (1)
{
for (key = 0x30; key <= 0x40; key++)
{
/* Wait until we're ready to send */
while (~LPC_USART1->STAT & (1<<2))
;
LPC_USART1->TXDATA = key;

while (~LPC_USART1->STAT & (1<<3))
;
}
}


Could any one write me what is wrong with my code?
Labels (1)
0 Kudos
Reply
10 Replies

1,173 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Cosik on Mon Mar 10 13:16:52 MST 2014
Hi,

My project base on this one https://github.com/sebseb7/lpc8xx a don't use any periph library, and have nothing more than i write.

Read/write UART are from LPC8xx user manual.
0 Kudos
Reply

1,173 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by starblue on Fri Mar 07 01:59:07 MST 2014
The loop condition by Dilberto is wrong, it will cause an endless loop.

If the bit to be tested is zero then the complement will have all bits set, so the condition is true.

If the bit is one then all other bits will be set and the condition is also true.

That said, I would write it like this (optimized for readability):
while ((LPC_USART1->STAT & (1<<2)) == 0)

I would also create constants for the flags.
0 Kudos
Reply

1,173 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by noahk on Thu Mar 06 16:52:29 MST 2014
Hi Cosik,

Can you post your full project and code? Make sure to indicate if you are using a software library and which one (to make sure that any headers are identical).

Thanks,
Noah
0 Kudos
Reply

1,173 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by noahk on Thu Mar 06 16:38:00 MST 2014

Quote: Dilberto
Hi, Cosik!

I haven't analyzed your code in deep, but seems to me that the last code snippet may have an unintended precedence. Try this :
( Note the added parentheses )

/* Wait until we're ready to send */
while (   ~(   LPC_USART1->STAT & (1<<2)   )   )
;
LPC_USART1->TXDATA = key;

while (   ~(   LPC_USART1->STAT & (1<<3)   )   )
;

Hope this helps.



I prefer the other way (although for the example you gave, it doesn't matter):
while(~STAT & (...)) allows for multiple flags:
while(~STAT & (1 << 2 | 1 << 3)); //wait for both. If either 1 << 2 is low or 1 << 3 is low in STAT, then the condition stays true and therefore waits. It only continues when both 1 << 2 and 1 << 3 are high in STAT.
while(~(STAT & (1 << 2 | 1 << 3))); //will continue if either flag is 1.

Noah
0 Kudos
Reply

1,173 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Cosik on Mon Mar 03 01:21:22 MST 2014

Quote: starblue
What do you mean by "is not working"?



I mean, when i connect board do FT232 converter I can see in console output from uC.
0 Kudos
Reply

1,173 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by starblue on Mon Mar 03 01:11:57 MST 2014
What do you mean by "is not working"?

In cases like these you should look into the assembly code and compare it for the two optimization settings.
0 Kudos
Reply

1,173 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Cosik on Sun Mar 02 13:32:20 MST 2014

Quote: TheFallGuy
See this FAQ
http://www.lpcware.com/content/faq/lpcxpresso/compiler-optimization




Yes, but there is information about "Optimized code fails to execute correctly". But I have inverted situation, optimized code is working good, and without optimization is not working.
0 Kudos
Reply

1,173 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Sun Mar 02 08:03:46 MST 2014
See this FAQ
http://www.lpcware.com/content/faq/lpcxpresso/compiler-optimization
0 Kudos
Reply

1,173 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Cosik on Sun Mar 02 07:29:55 MST 2014
Hi,

I just resolved my problem, but I think partly. Problem was with optimization.

When I used -O0 blinking LED was working, but program with USART not, but when I use -Os it's working without any changes in code.

Could anyone explain me why it happen?
0 Kudos
Reply

1,173 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Dilberto on Sun Mar 02 06:09:28 MST 2014
Hi, Cosik!

I haven't analyzed your code in deep, but seems to me that the last code snippet may have an unintended precedence. Try this :
( Note the added parentheses )

/* Wait until we're ready to send */
while (   ~(   LPC_USART1->STAT & (1<<2)   )   )
;
LPC_USART1->TXDATA = key;

while (   ~(   LPC_USART1->STAT & (1<<3)   )   )
;

Hope this helps.
0 Kudos
Reply