Author Topic: How do you write and read from the PIC32 UART Tx and Rx buffers?  (Read 3100 times)

0 Members and 1 Guest are viewing this topic.

Offline mapexmbirchTopic starter

  • Contributor
  • Posts: 20
Hi

The PIC32 has two 8 level buffers, one for Rx the other for Tx.   How do I read say line 6 of the Rx buffer and how do I write to line 6 of the Tx buffer?
I can't see it in the datasheet any where.

There are the UxTXREG and UxRXREG registers, do the buffers get shifted along into UxTXREG and UxRXREG?

Thanks
 

Offline bktemp

  • Super Contributor
  • ***
  • Posts: 1616
  • Country: de
Re: How do you write and read from the PIC32 UART Tx and Rx buffers?
« Reply #1 on: March 12, 2017, 01:22:36 pm »
You can't write to individual locations, because it is a FIFO buffer. Just write to the TX register or read from RX register and the hardware handles the buffering/shifting.
 

Offline mapexmbirchTopic starter

  • Contributor
  • Posts: 20
Re: How do you write and read from the PIC32 UART Tx and Rx buffers?
« Reply #2 on: March 12, 2017, 02:10:14 pm »
So, if I want to read the 6th line of the Rx buffer I just have to read the UxRXREG 6 times?
 

Offline BrianHG

  • Super Contributor
  • ***
  • Posts: 7733
  • Country: ca
Re: How do you write and read from the PIC32 UART Tx and Rx buffers?
« Reply #3 on: March 12, 2017, 02:52:57 pm »
Yes.  Whenever there are any characters in the buffer to be read, just copy that main location, 1 by 1, checking the empty flag, into your own 6 character long FIFO buffer.  Now you have access to any character you want, it will only eat 6 bytes of ram.  You can also make your own read buffer any number of characters you like.


Basically, (ok, I oversimplified thing, you can use a loop and make a 20 character buffer history for the rxcom)
Code: [Select]
while ( uxreg not empty ) {
charbuf[1] = charbuf[2];
charbuf[2] = charbuf[3];
charbuf[3] = charbuf[4];
charbuf[4] = charbuf[5];
charbuf[5] = charbuf[6];
charbur[6] = uxrxreg;
}

Now, if you want character 6, read charbuf#6, if you want character #1, use charbuf#1.
This is excellent if you have say a 4 character command with a CR/LF at the end, just monitor for the CR/LF and use the contents of the earlier charbuf#s buffer since they will still contain your command contents.

Oh, and if this is an interrupt service of your own making, make charbuf is a global array so that their contents are not lost since your probably getting single characters much slower than the execution speed of your code and you don't want to sit there waiting for all your characters to come in.

Now when I say waiting for CR/LF, you will be checking charbuf#6 and #5.  You obviously cant be always reading the rxreg.  Aslo, after the test, if you want to only execute a command one, once executed, just clear out all the 'charbuf#s = 0;'.  Now you cleared the valid CR/LF terminator and...  I'll let you figure out the rest.
« Last Edit: March 12, 2017, 03:25:22 pm by BrianHG »
 

Offline mapexmbirchTopic starter

  • Contributor
  • Posts: 20
Re: How do you write and read from the PIC32 UART Tx and Rx buffers?
« Reply #4 on: March 12, 2017, 03:05:42 pm »
Thanks all
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf