9 Replies Latest reply on Apr 9, 2014 3:37 PM by user_199038981

    UART Buffer

    user_199038981

      Hi,

         

      I'm using the PSoC 3 UART function and seem to be having problems getting the data in a simple application.

         

      If I am using a terminal window to type characters, everything works fine but if I programmatically send a string, it appears that the FIFO wraps around and the LF winds up in the first character in the buffer.

         

      What I tried to do is read the status to see when a character comes in (UART_1_RX_STS_FIFO_NOTEMPTY), then go into a loop almost identical to the example in the datasheet.that takes all the characters until the CR-LF. .

         

      I have put a UART_1_GetRxBufferSize() to see if more than one character has arrived but I never see more than one at a time.

         

      Any ideas to prevent me from days of hacking and guessing?

         

      Regards,

         

      Nick

        • 1. Re: UART Buffer
          user_1377889

          When you provide us a bit more with settings and selected properties it would be rather helpful to clear your issue. Can you create a "Workspace Bundle" and upload it here (NOT USING chrome, won't work)?

             

          Both of your principles usually do work, so the cause must be at a different place.

             

           

             

          Bob

          • 2. Re: UART Buffer
            user_199038981

            Bob,

               

            Yes, I'll upload it but I have subsequently done an experiment where I sent only 4 bytes (default Rx buffer size) to the device and didn't see any problems. It got all 4 bytes 10 times out of 10. Do you have to configure your receive buffer size to account for worst case?

               

            Nick

            • 3. Re: UART Buffer
              user_1377889

              The UART FIFO is exactly 4 bytes deep, that might be the reason why that test worked. When you set the Rx buffersize larger than 4 an internal interrupt is generated, so that you will not be able to catch a non-empty FIFO-state or an empty Rx register, only checking the buffersize will work.

                 

              At this point (you are not the first having problems here) I usually suggest to write an interrupt-driven circular buffer to handle incoming characters.

                 

               

                 

              Bob

              • 4. Re: UART Buffer
                user_199038981

                Bob,

                   

                Thanks, that makes sense. I'm a little confused about the internal interrupt. I don't have to write a special interupt handler or do I?

                   

                Can I simply set the buffer size to something like 48, then do a UART_GetRxBufferSize() call to determine when to retrieve the data as I pass through the main loop?

                   

                Nick

                • 5. Re: UART Buffer
                  user_1377889

                  Exactly.

                     

                  while( UART_GetRxBufferSize() )

                     

                  {

                     

                     RetrieveOneCharacter(); // Use the appropiate UART API here

                     

                  }

                     

                   

                     

                  Boib

                  • 6. Re: UART Buffer
                    user_199038981

                    Bob,

                       

                    Thanks a million. That works a lot better. The key was using the GetRxBufferSize as opposed to check RxStatus. I still occasionally miss a character but very rarely and can probably tune that out. My application is a small RS485 network. It seems like the 4 byte buffer is good for a human typing commands from a terminal but I need to be able to do both terminal and automated serial commands and this works well for both. I had a similar issue with the USBUART and it seems like the GetRxBufferSize is a close cousin of the USBUART_GetAll() command.

                       

                    To simplify matters, I get everything with the GetRxBufferSize in the while loop like you suggested, then call UART_ClearRxBuffer. There might be a better way using pointers or a better time to do that and it may be the source of my occasionally miss.

                       

                    Thanks again,

                       

                    Nick

                    • 7. Re: UART Buffer
                      user_1377889

                      The "ClearBuffer()" might be the cause for missing characters sometimes, it throws away any information still stored in the buffer. Retrieving a character from the buffer removes it from the queue, so there is no need (except for a software resetting) to use ClearBuffer().

                         

                      When time is not pressing: Again I would suggest you to write your own circular buffer. Not because you need to, but only to understand one of the basic pronciples to solve the problem of storing incoming data and retrieve them later.

                         

                       

                         

                      Bob

                      • 8. Re: UART Buffer
                        user_199038981

                        Yes, I'll try that immediately while I actually have the time.

                           

                        One other question. Which function do you prefer to retrieve the data? I have used the UART_GetChar and UART_ReadRxData and they both seem to work about the same with the GetRxBufferSize() loop approach.

                           

                        Nick

                        • 9. Re: UART Buffer
                          user_1377889

                          UART_GetChar() includes a status info and cannot be used for binary data.

                             

                          UART_ReadRxData() assumes that the state is already checked (as it is the case in your example) and as opposed to GetChar() you may receive a binary 0x00. This is the way I prefer.

                             

                           

                             

                          Bob