Problems reading serial port

ChibiOS public support forum for topics related to the Atmel AVR family of micro-controllers.

Moderator: tfAteba

Post Reply
JetForMe
Posts: 99
Joined: Mon Jan 31, 2011 8:12 am

Problems reading serial port

Post by JetForMe »

I've been writing code to parse SiRF binary serial packets on an AVR ATmega644A, running at 8 MHz. The device has two serial ports, one 4800-8-N-1, the other 38400-8-N-1. With the 8 MHz crystal, the baud error is 0.2% for both.

I seem to be able to talk to both ports just fine. My parser works fine when I connect the GPS directly to my Mac via a FTDI USB serial port. If I put the GPS into text mode, and read the bytes from USART 0, and write them back out to USART 1, and look at the result on the desktop, everything looks fine.

But when I switch it to binary (which works directly connected to the desktop), and try to parse, only the first packet successfully parses. The second one fails the checksum, and after that it never seems to find the header bytes (0xA0 0xA2) again in the stream.

I read via a thread, blocking indefinitely on sdRead(), awaiting 64 bytes. I then loop over the returned buffer, and pass each byte to my parser, which is a byte-by-byte state machine.

Is it possible that my parsing loop over 64 bytes takes so much time that the internal ChibiOS buffer, 16 bytes long, overflows, and I'm dropping bytes? I haven't tried timing it yet (I will when I get home).

Hm. I just remembered, I did change the code to read a single byte at a time from sdRead(), and it still failed.

Again, reading the bytes, 64 at a time, and writing them to the other port, seems to work. I've only tried this in text mode. I will try in binary mode, and see if my desktop parser can parse it, but I wanted to pose the question.

It might be something else entirely, but I suspect it's due to my misuse of ChibiOS.
liamstask
Posts: 34
Joined: Wed Dec 08, 2010 5:42 pm

Re: Problems reading serial port

Post by liamstask »

Setting SERIAL_BUFFERS_SIZE to a larger value in halconf.h would be a quick way to test.

You might also be able to check for SD_OVERRUN_ERROR in the result from sdGetStatusChangeEventSource() to determine if the driver itself has detected an overrun. Agreed in advance :) - it would be nice to have some code examples handy for this kind of thing.
JetForMe
Posts: 99
Joined: Mon Jan 31, 2011 8:12 am

Re: Problems reading serial port

Post by JetForMe »

liamstask wrote:Setting SERIAL_BUFFERS_SIZE to a larger value in halconf.h would be a quick way to test.

You might also be able to check for SD_OVERRUN_ERROR in the result from sdGetStatusChangeEventSource() to determine if the driver itself has detected an overrun. Agreed in advance :) - it would be nice to have some code examples handy for this kind of thing.


I made the serial buffer size 64 bytes, and sure enough, it runs longer (decoding more packets) before it stops working. That pretty clearly indicates that something is wrong in the OS code, or that I can't read fast enough. But what's really puzzling is that after that, I almost never see the start of a packet again (0xA0 0xA2), and I would expect to see that much more frequently.

I can't find sdGetStatusChangeEventSource() anywhere in the ChibiOS code...
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Problems reading serial port

Post by Giovanni »

sdGetStatusChangeEventSource() is obsolete, now it is called chIOGetEventSource().

The circular queues code is not AVR specific so it is unlikely there is a problem there, it is more possible there is a problem in the AVR low level serial driver considering that the AVR platform is the one that received the least feedback since it was released.

When I'll back at home I will write a specific test case for the AVR serial driver (with events :-)) in order to verify the basic functionality, a software loop-back should be OK.

Giovanni
JetForMe
Posts: 99
Joined: Mon Jan 31, 2011 8:12 am

Re: Problems reading serial port

Post by JetForMe »

Giovanni wrote:sdGetStatusChangeEventSource() is obsolete, now it is called chIOGetEventSource().

The circular queues code is not AVR specific so it is unlikely there is a problem there, it is more possible there is a problem in the AVR low level serial driver considering that the AVR platform is the one that received the least feedback since it was released.


Thanks, Giovanni.

I've been doing some further investigation. I have this (currently very messy code):

Code: Select all

while (TRUE)
{
   size_t qSize = chQSizeI(&(&SD1)->iqueue);
   size_t qSpace = chQSpaceI(&(&SD1)->iqueue);
   sLogger.log("Before read QSize: %lu, QSpace: %lu\n", qSize, qSpace);
   
   int numBytes = sdRead(&SD1, (uint8_t*) sBuf, 1);
   qSize = chQSizeI(&(&SD1)->iqueue);
   qSpace = chQSpaceI(&(&SD1)->iqueue);
   sLogger.log("After read QSize: %lu, QSpace: %lu\n", qSize, qSpace);
   
   long int start = chTimeNow();
   sLogger.log("Time after read: %ld\n", start);
   
   //   Pass byte to parser here.
   
   long int end = chTimeNow();
   sLogger.log("Time in parse: %ld\n", end - start);
}


And I'm getting output like this:

Code: Select all

       227: Before read QSize: 64, QSpace: 0
       306: After read QSize: 64, QSpace: 0
       307: Entry state: 1, byte: 0xa0 ('†')
       312: Exit  state: 2
       319: Time in loop: 12
       327: Before read QSize: 64, QSpace: 9
       338: After read QSize: 64, QSpace: 8
       350: Entry state: 2, byte: 0xa2 ('¢')
       361: Exit  state: 3
       368: Time in loop: 18
       376: Before read QSize: 64, QSpace: 8
       388: After read QSize: 64, QSpace: 7
       399: Entry state: 3, byte: 0000 ('')
       411: Exit  state: 4
       418: Time in loop: 19
       425: Before read QSize: 64, QSpace: 19
       437: After read QSize: 64, QSpace: 23
       449: Entry state: 4, byte: 0x02 ('')
       461: Exit  state: 5
       468: Time in loop: 19
       475: Before read QSize: 64, QSpace: 40
       487: After read QSize: 64, QSpace: 44
       499: Entry state: 5, byte: 0x12 ('')
       511: Exit  state: 5
       518: Time in loop: 19
       525: Before read QSize: 64, QSpace: 61
       537: After read QSize: 64, QSpace: 63
       549: Entry state: 5, byte: 0x01 ('')
       561: Exit  state: 6
       568: Time in loop: 19
       575: Before read QSize: 64, QSpace: 64
       587: After read QSize: 64, QSpace: 63
       599: Entry state: 6, byte: 0000 ('')
       610: Exit  state: 7
       618: Time in loop: 19
       625: Before read QSize: 64, QSpace: 64
       637: After read QSize: 64, QSpace: 63
       649: Entry state: 7, byte: 0x13 ('')
       660: mChecksum 0x0013, mSuppliedChecksum 0x0013
       675: Exit  state: 8
       682: Time in loop: 33
       689: Before read QSize: 64, QSpace: 64
       701: After read QSize: 64, QSpace: 63
       713: Entry state: 8, byte: 0xb0 ('∞')
       725: Exit  state: 9
       732: Time in loop: 19
       739: Before read QSize: 64, QSpace: 63
       751: After read QSize: 64, QSpace: 62
       763: Entry state: 9, byte: 0xb3 ('≥')
       775: Exit  state: 10
       782: Valid packet
       788: Got a SiRF packet. Length: 2, ID: 12
       801: Time in loop: 38
       809: Before read QSize: 64, QSpace: 62
       821: After read QSize: 64, QSpace: 61
       832: Entry state: 10, byte: 0xa0 ('†')
       844: Exit  state: 2
       851: Time in loop: 19
       859: Before read QSize: 64, QSpace: 61
       871: After read QSize: 64, QSpace: 60
       882: Entry state: 2, byte: 0xa2 ('¢')
       894: Exit  state: 3
       901: Time in loop: 19
       909: Before read QSize: 64, QSpace: 60
       921: After read QSize: 64, QSpace: 59
       932: Entry state: 3, byte: 0000 ('')
       944: Exit  state: 4
       951: Time in loop: 19
       959: Before read QSize: 64, QSpace: 59
       971: After read QSize: 64, QSpace: 58
       982: Entry state: 4, byte: 0x0e ('')
       994: Exit  state: 5
      1001: Time in loop: 19
      1009: Before read QSize: 64, QSpace: 58
      1021: After read QSize: 64, QSpace: 57
      1032: Entry state: 5, byte: 0xff ('ˇ')
      1044: Exit  state: 5
      1051: Time in loop: 19
      1058: Before read QSize: 64, QSpace: 57
      1070: After read QSize: 64, QSpace: 56
      1082: Entry state: 5, byte: 0x54 ('T')
      1094: Exit  state: 5
      1101: Time in loop: 19
      1108: Before read QSize: 64, QSpace: 56
      1120: After read QSize: 64, QSpace: 55
      1132: Entry state: 5, byte: 0x4f ('O')
      1144: Exit  state: 5
      1151: Time in loop: 19
      1158: Before read QSize: 64, QSpace: 55
      1170: After read QSize: 64, QSpace: 54
      1182: Entry state: 5, byte: 0x57 ('W')
      1194: Exit  state: 5
      1201: Time in loop: 19
      1208: Before read QSize: 64, QSpace: 54
      1220: After read QSize: 64, QSpace: 53
      1232: Entry state: 5, byte: 0x3a (':')
      1244: Exit  state: 5
      1251: Time in loop: 19
      1258: Before read QSize: 64, QSpace: 53
      1270: After read QSize: 64, QSpace: 52
      1282: Entry state: 5, byte: 0x20 (' ')
      1294: Exit  state: 5
      1301: Time in loop: 19
      1308: Before read QSize: 64, QSpace: 52
      1320: After read QSize: 64, QSpace: 51
      1332: Entry state: 5, byte: 0x32 ('2')
      1343: Exit  state: 5
      1350: Time in loop: 18
      1358: Before read QSize: 64, QSpace: 51
      1370: After read QSize: 64, QSpace: 50
      1382: Entry state: 5, byte: 0x30 ('0')
      1393: Exit  state: 5
      1400: Time in loop: 18
      1408: Before read QSize: 64, QSpace: 50
      1420: After read QSize: 64, QSpace: 49
      1432: Entry state: 5, byte: 0x38 ('8')
      1443: Exit  state: 5
      1450: Time in loop: 18
      1458: Before read QSize: 64, QSpace: 49
      1470: After read QSize: 64, QSpace: 48
      1481: Entry state: 5, byte: 0x37 ('7')
      1493: Exit  state: 5
      1500: Time in loop: 19
      1508: Before read QSize: 64, QSpace: 58
      1520: After read QSize: 64, QSpace: 62
      1531: Entry state: 5, byte: 0x34 ('4')
      1543: Exit  state: 5
      1550: Time in loop: 19
      1558: Before read QSize: 64, QSpace: 64
      1570: After read QSize: 64, QSpace: 63
      1581: Entry state: 5, byte: 0x34 ('4')
      1593: Exit  state: 5
      1600: Time in loop: 19
      1608: Before read QSize: 64, QSpace: 64
      1620: After read QSize: 64, QSpace: 63
      1631: Entry state: 5, byte: 0x20 (' ')
      1643: Exit  state: 5
      1650: Time in loop: 19
      1658: Before read QSize: 64, QSpace: 64
      1669: After read QSize: 64, QSpace: 63
      1681: Entry state: 5, byte: 0x20 (' ')
      1693: Exit  state: 6
      1700: Time in loop: 19
      1707: Before read QSize: 64, QSpace: 64
      1719: After read QSize: 64, QSpace: 63
      1731: Entry state: 6, byte: 0x03 ('')
      1743: Exit  state: 7
      1750: Time in loop: 19
      1757: Before read QSize: 64, QSpace: 64
      1769: After read QSize: 64, QSpace: 63
      1781: Entry state: 7, byte: 0xcc ('Ã')
      1793: mChecksum 0x03cc, mSuppliedChecksum 0x03cc
      1807: Exit  state: 8
      1814: Time in loop: 33
      1822: Before read QSize: 64, QSpace: 64
      1834: After read QSize: 64, QSpace: 63
      1845: Entry state: 8, byte: 0xb0 ('∞')
      1857: Exit  state: 9
      1864: Time in loop: 19
      1871: Before read QSize: 64, QSpace: 64
      1883: After read QSize: 64, QSpace: 63
      1895: Entry state: 9, byte: 0xb3 ('≥')
      1907: Exit  state: 10
      1914: Valid packet
      1921: Got a SiRF packet. Length: 14, ID: ff
      1934: Time in loop: 39
      1941: Before read QSize: 64, QSpace: 64
      1953: After read QSize: 64, QSpace: 63
      1965: Entry state: 10, byte: 0xa0 ('†')
      1977: Exit  state: 2
      1984: Time in loop: 19
      1991: Before read QSize: 64, QSpace: 64
      2003: After read QSize: 64, QSpace: 63
      2015: Entry state: 2, byte: 0xa2 ('¢')
      2027: Exit  state: 3
      2034: Time in loop: 19
      2041: Before read QSize: 64, QSpace: 64
      2053: After read QSize: 64, QSpace: 63
      2065: Entry state: 3, byte: 0000 ('')
      2077: Exit  state: 4
      2084: Time in loop: 19
      2091: Before read QSize: 64, QSpace: 64
      2103: After read QSize: 64, QSpace: 63
      2115: Entry state: 4, byte: 0x0a ('')
      2127: Exit  state: 5
      2134: Time in loop: 19
      2141: Before read QSize: 64, QSpace: 64
      2153: After read QSize: 64, QSpace: 63
      2165: Entry state: 5, byte: 0xff ('ˇ')
      2176: Exit  state: 5
      2183: Time in loop: 18
      2191: Before read QSize: 64, QSpace: 64
      2203: After read QSize: 64, QSpace: 63
      2215: Entry state: 5, byte: 0x57 ('W')
      2226: Exit  state: 5
      2233: Time in loop: 18
      2241: Before read QSize: 64, QSpace: 64
      2253: After read QSize: 64, QSpace: 63
      2265: Entry state: 5, byte: 0x4b ('K')
      2276: Exit  state: 5
      2283: Time in loop: 18
      2291: Before read QSize: 64, QSpace: 64
      2303: After read QSize: 64, QSpace: 63
      2315: Entry state: 5, byte: 0x3a (':')
      2326: Exit  state: 5
      2333: Time in loop: 18
      2341: Before read QSize: 64, QSpace: 64
      2353: After read QSize: 64, QSpace: 63
      2364: Entry state: 5, byte: 0x20 (' ')
      2376: Exit  state: 5
      2383: Time in loop: 19
      2391: Before read QSize: 64, QSpace: 64
      2403: After read QSize: 64, QSpace: 63
      2414: Entry state: 5, byte: 0x20 (' ')
      2426: Exit  state: 5
      2433: Time in loop: 19
      2441: Before read QSize: 64, QSpace: 64
      2453: After read QSize: 64, QSpace: 63
      2464: Entry state: 5, byte: 0x31 ('1')
      2476: Exit  state: 5
      2483: Time in loop: 19
      2491: Before read QSize: 64, QSpace: 64
      2503: After read QSize: 64, QSpace: 63
      2514: Entry state: 5, byte: 0x36 ('6')
      2526: Exit  state: 5
      2533: Time in loop: 19
      2540: Before read QSize: 64, QSpace: 64
      2552: After read QSize: 64, QSpace: 63
      2564: Entry state: 5, byte: 0x32 ('2')
      2576: Exit  state: 5
      2583: Time in loop: 19
      2590: Before read QSize: 64, QSpace: 64
      2602: After read QSize: 64, QSpace: 63
      2614: Entry state: 5, byte: 0x31 ('1')
      2626: Exit  state: 6
      2633: Time in loop: 19
      2640: Before read QSize: 64, QSpace: 64
      2652: After read QSize: 64, QSpace: 64
      2664: Entry state: 6, byte: 0x02 ('')
      2676: Exit  state: 7
      2683: Time in loop: 19
      2690: Before read QSize: 64, QSpace: 64
      2702: After read QSize: 64, QSpace: 63
      2714: Entry state: 7, byte: 0xe5 ('Â')
      2726: mChecksum 0x02e5, mSuppliedChecksum 0x02e5
      2740: Exit  state: 8
      2747: Time in loop: 33
      2754: Before read QSize: 64, QSpace: 64
      2766: After read QSize: 64, QSpace: 63
      2778: Entry state: 8, byte: 0xb0 ('∞')
      2790: Exit  state: 9
      2797: Time in loop: 19
      2804: Before read QSize: 64, QSpace: 64
      2816: After read QSize: 64, QSpace: 63
      2828: Entry state: 9, byte: 0xb3 ('≥')
      2840: Exit  state: 10
      2847: Valid packet
      2854: Got a SiRF packet. Length: 10, ID: ff
      2867: Time in loop: 38
      2874: Before read QSize: 64, QSpace: 64
      2886: After read QSize: 64, QSpace: 63
      2898: Entry state: 10, byte: 0xa0 ('†')
      2910: Exit  state: 2
      2917: Time in loop: 19
      2924: Before read QSize: 64, QSpace: 64
      2936: After read QSize: 64, QSpace: 63
      2948: Entry state: 2, byte: 0xa2 ('¢')
      2960: Exit  state: 3
      2967: Time in loop: 19
      2974: Before read QSize: 64, QSpace: 64
      2986: After read QSize: 64, QSpace: 63
      2998: Entry state: 3, byte: 0000 ('')
      3010: Exit  state: 4
      3017: Time in loop: 19
      3024: Before read QSize: 64, QSpace: 64
      3036: After read QSize: 64, QSpace: 63
      3048: Entry state: 4, byte: 0x20 (' ')
      3059: Exit  state: 5
      3066: Time in loop: 18
      3074: Before read QSize: 64, QSpace: 64
      3086: After read QSize: 64, QSpace: 63
      3098: Entry state: 5, byte: 0xff ('ˇ')
      3109: Exit  state: 5
      3116: Time in loop: 18
      3124: Before read QSize: 64, QSpace: 64
      3136: After read QSize: 64, QSpace: 63
      3148: Entry state: 5, byte: 0x50 ('P')
      3159: Exit  state: 5
      3166: Time in loop: 18
      3174: Before read QSize: 64, QSpace: 64
      3186: After read QSize: 64, QSpace: 63
      3197: Entry state: 5, byte: 0x4f ('O')
      3209: Exit  state: 5
      3216: Time in loop: 19
      3224: Before read QSize: 64, QSpace: 64
      3236: After read QSize: 64, QSpace: 63
      3247: Entry state: 5, byte: 0x53 ('S')
      3259: Exit  state: 5
      3266: Time in loop: 19
      3274: Before read QSize: 64, QSpace: 64
      3286: After read QSize: 64, QSpace: 63
      3297: Entry state: 5, byte: 0x3a (':')
      3309: Exit  state: 5
      3316: Time in loop: 19
      3324: Before read QSize: 64, QSpace: 64
      3336: After read QSize: 64, QSpace: 63
      3347: Entry state: 5, byte: 0x20 (' ')
      3359: Exit  state: 5
      3366: Time in loop: 19
      3374: Before read QSize: 64, QSpace: 64
      3385: After read QSize: 64, QSpace: 63
      3397: Entry state: 5, byte: 0x2d ('-')
      3409: Exit  state: 5
      3416: Time in loop: 19
      3423: Before read QSize: 64, QSpace: 64
      3435: After read QSize: 64, QSpace: 63
      3447: Entry state: 5, byte: 0x32 ('2')
      3459: Exit  state: 5
      3466: Time in loop: 19
      3473: Before read QSize: 64, QSpace: 64
      3485: After read QSize: 64, QSpace: 63
      3497: Entry state: 5, byte: 0x36 ('6')
      3509: Exit  state: 5
      3516: Time in loop: 19
      3523: Before read QSize: 64, QSpace: 64
      3535: After read QSize: 64, QSpace: 63
      3547: Entry state: 5, byte: 0x38 ('8')
      3559: Exit  state: 5
      3566: Time in loop: 19
      3573: Before read QSize: 64, QSpace: 64
      3585: After read QSize: 64, QSpace: 63
      3597: Entry state: 5, byte: 0x30 ('0')
      3609: Exit  state: 5
      3616: Time in loop: 19
      3623: Before read QSize: 64, QSpace: 64
      3635: After read QSize: 64, QSpace: 63
      3647: Entry state: 5, byte: 0x39 ('9')
      3658: Exit  state: 5
      3665: Time in loop: 18
      3673: Before read QSize: 64, QSpace: 64
      3685: After read QSize: 64, QSpace: 63
      3697: Entry state: 5, byte: 0x33 ('3')
      3708: Exit  state: 5
      3715: Time in loop: 18
      3723: Before read QSize: 64, QSpace: 64
      3735: After read QSize: 64, QSpace: 63
      3747: Entry state: 5, byte: 0x31 ('1')
      3758: Exit  state: 5
      3765: Time in loop: 18
      3773: Before read QSize: 64, QSpace: 64
      3785: After read QSize: 64, QSpace: 63
      3797: Entry state: 5, byte: 0x20 (' ')
      3808: Exit  state: 5
      3815: Time in loop: 18
      3823: Before read QSize: 64, QSpace: 64
      3835: After read QSize: 64, QSpace: 63
      3846: Entry state: 5, byte: 0x33 ('3')
      3858: Exit  state: 5
      3865: Time in loop: 19
      3873: Before read QSize: 64, QSpace: 64
      3885: After read QSize: 64, QSpace: 63
      3896: Entry state: 5, byte: 0x0e ('')
      3908: Exit  state: 5
      3915: Time in loop: 19
      3923: Before read QSize: 64, QSpace: 64
      3935: After read QSize: 64, QSpace: 63
      3946: Entry state: 5, byte: 0x09 ('\t')
      3958: Exit  state: 5
      3965: Time in loop: 19
      3973: Before read QSize: 64, QSpace: 64
      3985: After read QSize: 64, QSpace: 63
      3996: Entry state: 5, byte: 0xa0 ('†')
      4008: Exit  state: 5
      4015: Time in loop: 19
      4022: Before read QSize: 64, QSpace: 64
      4034: After read QSize: 64, QSpace: 63
      4046: Entry state: 5, byte: 0xa2 ('¢')
      4058: Exit  state: 5
      4065: Time in loop: 19
      4072: Before read QSize: 64, QSpace: 64
      4084: After read QSize: 64, QSpace: 63
      4096: Entry state: 5, byte: 0000 ('')
      4108: Exit  state: 5
      4115: Time in loop: 19
      4122: Before read QSize: 64, QSpace: 64
      4134: After read QSize: 64, QSpace: 63
      4146: Entry state: 5, byte: 0x5b ('[')
      4158: Exit  state: 5
      4165: Time in loop: 19
      4172: Before read QSize: 64, QSpace: 64
      4184: After read QSize: 64, QSpace: 63
      4196: Entry state: 5, byte: 0x29 (')')
      4208: Exit  state: 5
      4215: Time in loop: 19
      4222: Before read QSize: 64, QSpace: 64
      4234: After read QSize: 64, QSpace: 63
      4246: Entry state: 5, byte: 0000 ('')
      4258: Exit  state: 5
      4265: Time in loop: 19
      4272: Before read QSize: 64, QSpace: 64
      4284: After read QSize: 64, QSpace: 63
      4296: Entry state: 5, byte: 0x01 ('')
      4307: Exit  state: 5
      4314: Time in loop: 18
      4322: Before read QSize: 64, QSpace: 64
      4334: After read QSize: 64, QSpace: 63
      4346: Entry state: 5, byte: 0000 ('')
      4357: Exit  state: 5
      4364: Time in loop: 18
      4372: Before read QSize: 64, QSpace: 64
      4384: After read QSize: 64, QSpace: 63
      4396: Entry state: 5, byte: 0000 ('')
      4407: Exit  state: 5
      4414: Time in loop: 18
      4422: Before read QSize: 64, QSpace: 64
      4434: After read QSize: 64, QSpace: 63
      4446: Entry state: 5, byte: 0x06 ('')
      4457: Exit  state: 5
      4464: Time in loop: 19
      4472: Before read QSize: 64, QSpace: 64
      4484: After read QSize: 64, QSpace: 63
      4495: Entry state: 5, byte: 0x55 ('U')
      4507: Exit  state: 5
      4514: Time in loop: 19
      4522: Before read QSize: 64, QSpace: 64
      4534: After read QSize: 64, QSpace: 63
      4545: Entry state: 5, byte: 0x11 ('')
      4557: Exit  state: 5
      4564: Time in loop: 19
      4572: Before read QSize: 64, QSpace: 64
      4584: After read QSize: 64, QSpace: 63
      4595: Entry state: 5, byte: 0x54 ('T')
      4607: Exit  state: 5
      4614: Time in loop: 19
      4622: Before read QSize: 64, QSpace: 64
      4633: After read QSize: 64, QSpace: 63
      4645: Entry state: 5, byte: 0x71 ('q')
      4657: Exit  state: 6
      4664: Time in loop: 19
      4671: Before read QSize: 64, QSpace: 64
      4683: After read QSize: 64, QSpace: 63
      4695: Entry state: 6, byte: 0xbd ('Ω')
      4707: Exit  state: 7
      4714: Time in loop: 19
      4721: Before read QSize: 64, QSpace: 64
      4733: After read QSize: 64, QSpace: 63
      4745: Entry state: 7, byte: 0x07 ('')
      4757: mChecksum 0x0747, mSuppliedChecksum 0x3d07
      4771: Exit  state: 1
      4778: Time in loop: 33


I'm reading one byte at a time. You can see the input queue fill up, go down, then fill up again and stay full. A few more packets are successfully parsed, then the checksum fails at the end. After that, it never recovers.

Looks like my parsing code is just too slow (which worries me, because it's very simple). I'll go back to reading more bytes at a time, see what happens.
JetForMe
Posts: 99
Joined: Mon Jan 31, 2011 8:12 am

Re: Problems reading serial port

Post by JetForMe »

Well! I'm an idiot. I found one major problem. The buffer I was reading into, I accidentally defined as an array of uint8_t*, not uint8_t. So, I was skipping along by four bytes.

I still think I have an overflow problem, but this solves the big mystery of why I was never seeing the header bytes when reading more than one byte at a time from the stream.

Stupid little-endian machine; I would've caught this much sooner had it been big-endian (because not even reading a single byte would've worked).
JetForMe
Posts: 99
Joined: Mon Jan 31, 2011 8:12 am

Re: Problems reading serial port

Post by JetForMe »

Okay, after fixing that bug, and removing the painful logging code, my parser is working great. I can probably restore the internal queues to the default size, but I wanted to let everyone know that ChibiOS is doing the right thing, and it just took me forever to locate a stupid typo.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Problems reading serial port

Post by Giovanni »

It always happens this way to me too :-)
Post Reply