Serial Comms

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I need to transfer 10k of data from PC to XMega. I cannot send all of the data as one packet, as I have run out of ram.

The data is a structure, with sub structures. I am now going to send each sub structure as a data packet. Each packet will be a different size.

Looking for ideas how to do this. I was thinking of a start charachter followed by 16 bit packet size and 32bit checksum on the end. Then read the 2nd and 3rd byte to know where is the end of the packet. Is there a better way ?

Think generously about people who know less than you about code. Don't think they should understand some technical concept. They don't, and they're not stupid for not understanding.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is code I use to do something similar. I have a start and stop character plus packet size. When all three conditions are met, it turns on the GotRx flag. Works very well.

Ray.

if (USARTC0.STATUS & USART_RXCIF_bm) {
   static uint16_t pkt = 0;
   static uint16_t x = 0;
   static uint8_t Schar = False; // Start char
   uint8_t RxByte;
		  
   RxByte = USARTC0.DATA;;

   switch (RxByte) {
       case 0x24 : if (Schar == False && gotRx == False) {  // $ Start char
                      RxData[x++] = RxByte;
		      Schar = True;	  
		   } else {
		      if (Schar == True && gotRx == False) {
		         RxData[x++] = RxByte;  	
		      }
	           }
		   break;
	 
        case 0x7E : if (Schar == True && x == pkt) {  // ~ End char
		       RxData[x++] = RxByte;
		       gotRx = True;
		       x = 0;
		       pkt = 0;
		       Schar = False;
		    } else {   
		       if (Schar == True) {
		          RxData[x++] = RxByte;
		       }
		    }
		    break;

	  default : if (Schar == True && gotRx == False) {   
		       RxData[x++] = RxByte;
		       if (x == 3) { 
                          pkt = (RxData[x - 2] << 8); 
	                  pkt = pkt + (RxByte AND 0xFF);
			  pkt = pkt + 0x0004;
		       }
		     }
		     break;
	 	
    } // End Switch
} // End Uart
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

IIRC, someone has posted a simple "STX/ETX" implementation here at AVRfreaks a while back. An "infrastructure" supporting both type fields, variable length packages and checksums seems to come in handy for your purposes.

EDIT: Trawling this site, my memory and The InterWeb (in that order) I finally re-discovered that the STX/ETX implementation I've seen is in Pascal Stangs "Procyon AVRLib". About 200 lines of sparse C code (think, less than 100 lines if comment lines removed).

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ray that code is what I needed, and works with the Xmega.

Think generously about people who know less than you about code. Don't think they should understand some technical concept. They don't, and they're not stupid for not understanding.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

TheNoise,

Yes I only posted it as it would be helpful and work with the XMega. Too many people post replys, just to increace their ranking on the forum, rather then giving accurate useful replys.

Ray.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ray, whilst it may 'work', your protocol is none too robust. Did we not talk of protocols some time ago where the protocol SLIP was discussed? I'm sorry if you don't consider this helpful, but most of the time it is wasteful to repeat something that is covered in great detail on the interwebs. One post closer to 10K!!!

TheNoise - there's a number of file transfer programs conceived over the years. XModem is one of the simpler ones along with variants, Xmodem CRC, Ymodem,Zmodem etc. A 16 bit CRC will normally suffice as is done with Xmodem CRC. Most of the usual terminal programs implement these protocols. Just Google 'Xmodem' and you'll find a wealth of information.

[edit] Just read your specs again - Xmodem is a fixed size packet. As mentioned, SLIP might be of interest.