7 Replies Latest reply on Jul 26, 2018 10:39 PM by yekt

    Having Errors Reading UART TX straight to RX

    scott_2272156

      I am designing a DC data buss system i have modeled in my circuit simulator.  I am trying to set up a test to validate its data transfer of this system.  Currently I have a Pioneer kit testing its functions.  I have tied the TX pin P0.5 to the RX pin P0.4. I wrote a little bit of code to send data out the UART to my PC and validate it was revived then post it back to PC Screen.  I also toggle an LED on and off each time the data is correct.  In my code below it always works one or two times thorough the loop but anything over two time it errors.  Can anyone tell me why it is not correct every time through the loops sense I have tied the TX and RX pins together.  Also is there a proper way to detect when data has completed sending and receiving. In my code i just put delays but i believe this is not correct way to do this.

      Thanks

       

       

        • 1. Re: Having Errors Reading UART TX straight to RX
          yekt

          Hello Scott,

           

          Please go through the code attached below.

          uart.png

          I have checked this code and it works fine. Use "UART_SpiUartGetRxBufferSize()" to see if you have something in the RX buffer and then read the data. I also see that there is some logical issue in your code. You are putting the data thrice in TX buffer but reading it only once. If you have any doubts, please fell free to add a reply.

           

          Thanks,

          Yeshwanth

          • 2. Re: Having Errors Reading UART TX straight to RX
            scott_2272156

            Yeshwanth,

             

            Thank you for helping me out with my UART problem.  that UART_SpiUartGet RXBuffersize() was the key.  I added this to my code and it ran fine. Question:  Are there any examples of how to send two bytes at time using the UART?  In the end I really want to send an address byte and the then an mode/action byte.  If there are any examples how to do this properly i would sure appreciate if i could be directed to it.  I am very new to this type of thing.... thanks for taking the time to help me.

             

            Scott

            • 3. Re: Having Errors Reading UART TX straight to RX
              scott_2272156

                      Why does the routine work-every time if change my variable Unit_ID each time through he loop. Yet when I just want to post the recived data back to PC CRT again and again it does not work?   I am trying to produce some test routine/code that send the same data each time so i can verify the data is sent correctly every time for a test circuit i want to validate.

               

              I am looking basically to send the letter "A" for example and then post to CRT.  Then to check if it was received properly and add 1 to it so I get the letter "B" and then post that letter to PC. So in my code i am looking for the following output ABABABABAB.  What i see for output is ABAABABA.

               

                        int i = 0;

                      for(i=0; i<5;i++)

                   {

                     

                      UART_UartPutChar(Unit_ID);          // Send Data to PC Screen

                      while(!UART_SpiUartGetRxBufferSize());

                      recd = UART_UartGetChar();          // Recive Data Sent

                     

                      if (recd == Unit_ID)                // check if ID sent is correct

                      {

                      Red_LED_Write (~Red_LED_Read());    //Toggle Kit42 LED On/Off

                      UART_UartPutChar(recd+1);

                      CyDelay(500);

                      //Unit_ID++;

                    

                      }

               

              Now if i do the following code and change my variable Unit_ID each time through the loop it works.

               

                      int i = 0;

                      for(i=0; i<5;i++)

                   {

                     

                      UART_UartPutChar(Unit_ID);          // Send Data to PC Screen

                      while(!UART_SpiUartGetRxBufferSize());

                      recd = UART_UartGetChar();          // Recive Data Sent

                     

                      if (recd == Unit_ID)                // check if ID sent is correct

                      {

                      Red_LED_Write (~Red_LED_Read());    //Toggle Kit42 LED On/Off

                      //UART_UartPutChar(recd+1);

                      CyDelay(500);

                      Unit_ID++;

                    

                      }

               

              hope i can be helped to understand were i am going wrong?

              Thanks

              Scott

              • 4. Re: Having Errors Reading UART TX straight to RX
                yekt

                Hello Scott,

                 

                In the first code snippet, you send "A" first and then receive it immediately. So the output is "A". Then in the "if" block you send "B", but it is not received anywhere. So "B" stays in RX buffer. In the next loop "A" is again sent, so RX buffer will have "BA". One more character is received to give output "AB". Since recd "B" is not same as Unit_ID "A" the "if" loop is skipped. In the next loop again "A" is sent, so RX buffer will have "AA". One more character is received to give output "ABA". Then in the "if" block again "B" is sent, so RX buffer will have "AB". This continues again and again to give you the output  "ABAABA..."

                 

                In the second code. You send the character once and receive it once. This is not true in first code where you send twice and receiving only once. This is exactly what i pointed out as logical error in the previous reply.

                 

                Thanks,

                Yeshwanth

                • 5. Re: Having Errors Reading UART TX straight to RX
                  yekt

                  For references go through these materials:

                   

                  Video Tutorials: http://www.cypress.com/video-library/PSoC/psoc-101-lesson-12-uart/387666

                  CE: http://www.cypress.com/documentation/code-examples/ce95366-psoc-4-scb-uart

                   

                  If you have any doubts regarding the previous explanation please feel free to ask.

                   

                  Thanks,

                  Yeshwanth

                  • 6. Re: Having Errors Reading UART TX straight to RX
                    scott_2272156

                    Thanks Yeshwanth ...your explanation  really helped me understand what i was doing. Below i changed my code to reflect what you tough me and it works.  I added a read to clear the the RX buffer so it would balance.  I know this is for sure probably not the right way to do this but for me its helps to understand whats going on.

                     

                    Thank you for taking the time to help me! 

                     

                    Scott

                     

                            int i = 0;

                            for(i=0; i<5;i++)

                         {

                           

                            UART_UartPutChar(Unit_ID);          // Send Data to PC Screen

                            while(!UART_SpiUartGetRxBufferSize());

                            recd = UART_UartGetChar();          // Recive Data Sent

                           

                            if (recd == Unit_ID)                // check if ID sent is correct

                            {

                            Red_LED_Write (~Red_LED_Read());    //Toggle Kit42 LED On/Off

                            UART_UartPutChar(recd+1);

                            while(!UART_SpiUartGetRxBufferSize());

                            UART_UartGetChar();

                            CyDelay(500);

                            //Unit_ID++;

                    • 7. Re: Having Errors Reading UART TX straight to RX
                      yekt

                      My pleasure, Scott. If you get more problems please don't hesitate to ask again.

                       

                      Regards,

                      Yeshwanth