LPC845breakout Nonblocking getChar() over USB

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

LPC845breakout Nonblocking getChar() over USB

773 Views
KoComputes
Contributor II

Hi,

As a beginner on LPC, I use a LPC845breakout board in Keil MDK with MCU Xpresso Config Tools, no (RT)OS. I need to do both output AND diagnostic commands (input) over the serial port, which goes over USB to the PC (VCOM). The output is based on fsl_debug_console.c and works fine with PRINTF(). 

I need to be able to respond to single character commands received over the VCOM port as well. 

When I use DbgConsole_Getchar(), it waits for input and then continues. 

This does not work well because it blocks execution of the other tasks, that should always run in my application. The MCU seems to spend most of the time mostly on this line in fsl_usart.c :

 

while ((base->STAT & USART_STAT_RXRDY_MASK) == 0U)

 

What is the proper way to check if there is input available for processing?

Any help is appreciated!

Labels (2)
0 Kudos
5 Replies

719 Views
KoComputes
Contributor II

Thank you for the suggestion. I think you're missing the point that this is code for an LPC845, for which a call like uart_statusRegister() is not available in the SDK. 

Anyway, I have found an (ugly) workaround. Before calling getChar() I will check availability of a received byte. No interrupts required. It assumes that DbgConsole works with USART0. For other boards than LPC845BREAKOUT this may need to be changed.

 

/*!
* @brief DbgConsole_Available()
* Returns 1 if characters are waiting to be read, 0 if there are none.
*/
int DbgConsole_Available(void) {

    uint32_t * pointer = (uint32_t *) USART0_BASE; // dirty: assume USART0 is used
    uint32_t serialStatus = pointer[2];            // offset 2 = Status register

    return ((serialStatus & USART_STAT_RXRDY_MASK) == 0U) ? 0 : 1;
}

 

 In the main program loop I check for keys to respond to as follows:

 

if (DbgConsole_Available() == 1) {
    int c = DbgConsole_Getchar();

    // process received key
    (....)
}

 

 

0 Kudos

732 Views
KoComputes
Contributor II

Hope you can find time to look at my reply. This is a commercial application and further support would really be appreciated.

 

0 Kudos

724 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

From my experience, there are two methods to read data from peripherals, one is polling mode, another is interrupt mode.

The polling mode is like this:

while(uart_statusRegister(UART0)&FLAG_MASK) {}

 char c=UART0_DATA;

 

Obviously, polling mode is blocking mode, which is not desired.

 

Interrupt mode:

bool flag;

void USART0_ISR()
{

flag=true;

 

}

main()

{

......

while(1)

{

if(flag)

{

1)read data from usart input data reg to char c

2)check if the chard c, if it  is "return" char, execute command of string; otherwise, append the char c to a string.

clear uart status flag bit

}

}

}

 

Hope it can help you

BR

XiangJun Rong

0 Kudos

753 Views
KoComputes
Contributor II

Hi XiangJun Rong,

Thanks. I downloaded the code and had a look. That code refers to the getc() function. I can't find the implementation of it, maybe it is covered by the file system component. Remember I am only using a Cortex M0+ with no file system, no OS. I see no way to use this in my own application.

Do you have any other suggestions besides branching my own version of fsl_usart.c ?

Marco

0 Kudos

758 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

I think you want to develop the shell like tools, so I suggest that you refer to the shell code in SDK package, but the shell code is only available for LPC54xxx, pls download the SDK package for LPC54628 from the link:

https://www.nxp.com/design/software/development-software/mcuxpresso-software-and-tools-/mcuxpresso-s...

 

check the directory:

xiangjun_rong_0-1652759415966.png

Hope it can help you

BR

XiangJun Rong

 

 

0 Kudos