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

PSoC™ Creator & Designer Forum Discussions

nibec_3605396
Level 2
Level 2
First like given

I apologize for being a bit of a newb. C is not my strongest language and it's way of doing arrays is a bit foreign to me. I was hoping somebody could help me with figuring out how to implement a UART RX receiving function for fetching packets being sent from another dev board to my PSoC 5LP board. They gave examples of TX and RX functions. I understood TX but having trouble figuring out the RX. They gave:

int recv_packet(char *p, int len){

    char c;

    int received = 0;

    /* sit in a loop reading bytes until we put together

    * a whole packet.

    * Make sure not to copy them into the packet if we

    * run out of room.

    */

    while(1) {

        /* get a character to process

        */

        c = UART_GetChar();

        /* handle bytestuffing if necessary

        */

        switch(c) {

            /* if it's an END character then we're done with

            * the packet

            */

            case END:

                /* a minor optimization: if there is no

                * data in the packet, ignore it. This is

                * meant to avoid bothering IP with all

                * the empty packets generated by the

                * duplicate END characters which are in

                * turn sent to try to detect line noise.

                */

                if(received)

                    return received;

                else

                    break;

            /* if it's the same code as an ESC character, wait

            * and get another character and then figure out

            * what to store in the packet based on that.

            */

            case ESC:

                c = UART_GetChar();

                /* if "c" is not one of these two, then we

                * have a protocol violation. The best bet

                * seems to be to leave the byte alone and

                * just stuff it into the packet

                */

                switch(c) {

                    case ESC_END:

                        c = END;

                        break;

                    case ESC_ESC:

                        c = ESC;

                        break;

                }

            /* here we fall into the default handler and let

            * it store the character for us

            */

            default:

                if(received < len)

                p[received++] = c;

        }

    }

}

So first question: The comments they gave are helpful but I'm having trouble with the while loop. Is the return in the first case (for END) going to end the while loop? I've never seen a while loop use a (1) instead of some variable.

Second question: How do I use this function effectively? With fiddling with the TX function I've understood that arrays need to have a declared length in C but how would I essentialy make the array dynamically resized to be the size of the packet received?

0 Likes
1 Solution
Yeshwanth_KT
Employee
Employee
50 replies posted 25 replies posted 10 likes received

Hello Nik,

1) In C programming anything other than '0' is true. Therefore while(1) will be an infinite loop, since '1' represents true ( it's a general convetion to use '1', anything true can be used to create an infinite loop).

2)Meaning of Case END: When the END character is received and there is some data received previously, return from the function. If the END charcter is received and that is the first character continue getting the data.

3) C supports dynamic memory allocation by using functions like "malloc", "calloc" and "realloc". For a simple example refer this website: C library function - realloc()

Regards,

Yeshwanth

View solution in original post

0 Likes
3 Replies
Yeshwanth_KT
Employee
Employee
50 replies posted 25 replies posted 10 likes received

Hello Nik,

1) In C programming anything other than '0' is true. Therefore while(1) will be an infinite loop, since '1' represents true ( it's a general convetion to use '1', anything true can be used to create an infinite loop).

2)Meaning of Case END: When the END character is received and there is some data received previously, return from the function. If the END charcter is received and that is the first character continue getting the data.

3) C supports dynamic memory allocation by using functions like "malloc", "calloc" and "realloc". For a simple example refer this website: C library function - realloc()

Regards,

Yeshwanth

0 Likes
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

nik_berry,

You didn't specify your processor type ( PSoC 4 / PSoC 5). For PSoC5 example see this thread

UART string reception garbage value

For PSoC4

Hello, I am new to PSOC4, I need to take data from UART and transmit to the GPIOs

/odissey1

Sorry for the long delay... The bosses had me hop on a different project for a while. I did put in that I'm using the PSOC 5LP but I will look further into your recommended thread. Thank you!

0 Likes