I want the program won't run until I type in a character into UART.

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
DaCh_4286001
Level 3
Level 3
First like received First like given

So I want to use UART to write code that program won't run until I type in a character in UART.

my code looks like this

for(;;)

{

           while((UART_ReadRxStatus()& UART_RX_STS_FIFO_NOTEMPTY) ==0 )

        {

            data.command = UART_GetChar();      //put input character as command

        } 

  

           while((UART_ReadRxStatus()& UART_RX_STS_FIFO_NOTEMPTY) ==0 )

        {

            data.size= UART_GetChar();      //put input character into size

        } 

}

and it is not working correctly after I type in first character. It never stuck in while loop after UART received first character

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

As I noticed that I've been writing same code again and again,

recently I wrote a UART utility for myself.

I hope that It may be useful for you, too.

int get_line(void) ; /* get one line till EOL or the length is up to STR_BUF_LEN */

int get_string(void) ; / * get a string with a delimiter of space, tab, newline */

they fill strings to

  char str[STR_BUF_LEN]

schematic

001-schematic.JPG

UART Config

002-UART-Config.JPG

main.c as a sample usage

======================

#include "project.h"

#include "stdio.h"

#include "tty_utils.h"

void init_hardware(void)

{

    tty_init() ;

    CyGlobalIntEnable; /* Enable global interrupts. */

}

int main(void)

{

    init_hardware() ;

    splash("tty utils test") ;

    prompt() ;

    for(;;) {

//        if (get_string() > 0) { // got a string

        if (get_line()) { // got a line

            print("Got: ") ;

            print(str) ;

            print("\n") ;

            prompt() ;

        }

    }

}

======================

If you want to stop your program until a string is received.

Please modify main.c as below

======================

#include "project.h"

#include "stdio.h"

#include "tty_utils.h"

void init_hardware(void)

{

    tty_init() ;

    CyGlobalIntEnable; /* Enable global interrupts. */

}

int main(void)

{

    init_hardware() ;

    splash("tty utils test") ;

    prompt() ;

    for(;;) {

       while(get_string() <= 0) {

                ; // waiting for a string to be received

         }

         // if program gets here, str has been received

         print("Got: ") ;

         print(str) ;

         print("\n") ;

         prompt() ;

    }

}

======================

Note:

After posting the first response with a sample for PSoC 4 (CY8CKIT-044),

I amended and added another sample for 5LP (CY8CKIT-059).

moto

View solution in original post

0 Likes
2 Replies
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

As I noticed that I've been writing same code again and again,

recently I wrote a UART utility for myself.

I hope that It may be useful for you, too.

int get_line(void) ; /* get one line till EOL or the length is up to STR_BUF_LEN */

int get_string(void) ; / * get a string with a delimiter of space, tab, newline */

they fill strings to

  char str[STR_BUF_LEN]

schematic

001-schematic.JPG

UART Config

002-UART-Config.JPG

main.c as a sample usage

======================

#include "project.h"

#include "stdio.h"

#include "tty_utils.h"

void init_hardware(void)

{

    tty_init() ;

    CyGlobalIntEnable; /* Enable global interrupts. */

}

int main(void)

{

    init_hardware() ;

    splash("tty utils test") ;

    prompt() ;

    for(;;) {

//        if (get_string() > 0) { // got a string

        if (get_line()) { // got a line

            print("Got: ") ;

            print(str) ;

            print("\n") ;

            prompt() ;

        }

    }

}

======================

If you want to stop your program until a string is received.

Please modify main.c as below

======================

#include "project.h"

#include "stdio.h"

#include "tty_utils.h"

void init_hardware(void)

{

    tty_init() ;

    CyGlobalIntEnable; /* Enable global interrupts. */

}

int main(void)

{

    init_hardware() ;

    splash("tty utils test") ;

    prompt() ;

    for(;;) {

       while(get_string() <= 0) {

                ; // waiting for a string to be received

         }

         // if program gets here, str has been received

         print("Got: ") ;

         print(str) ;

         print("\n") ;

         prompt() ;

    }

}

======================

Note:

After posting the first response with a sample for PSoC 4 (CY8CKIT-044),

I amended and added another sample for 5LP (CY8CKIT-059).

moto

0 Likes