single channel logic analyser feasible?

Hi, Since I am planning on using a SAMD based arduino (MKR) and the timer stuff is really complex. I though I would ask you if what I want to do is even possible before I begin.

I have a serial input that I want to measure the idle time between characters. It is the output from a pair of bathroom scales at 115220 baud.

Since this is similar in function to a logic analyser I thought I would title it as such and maybe over time i could expand it to do that job.

Firstly, currently I'm not really interested in the actual data characters just the idle time between them.

So this my proposed method:

set up two SAMD 32bit timers as incrementing counters to count at 48MHz wait for a trigger condition (say input has gone high) set one timer to count when my serial input is high. set the other timer to count when my serial input is low. This means that only one timer is counting at any one time.

Regularly the current idle counter is read, stored in a memory based log file and then the counter is reset to zero. when the log file is full it is printed on the serial monitor.

I think this method should allow long capture times at high resolution.

But is it possible? I have lost count of the number of times that I have discovered something I want to do is impossible. It usually happens after I realize the implications of something in a data sheet after hours of experimentation.

This time I thought I would ask first!

While solution code would be nice. I really only need an opinion and perhaps some tips.

Bob

I’d read the Serial signal, and wait for the next character becoming available. Then the idle time is the measured time minus character transmission time.

Hi DrDiettrich,

Thank you for your reply.

Your suggested way would work but I'm thinking that the serial available is interrupt driven and might be quite variable.

I have already tried an alternative way (using an oscilloscope) sometimes it shows 349 microseconds and others 357 microseconds. It is difficult to trigger on the gap between packets with my scope and yet still have good resolution.

Bob

Your suggested way would work but I'm thinking that the serial available is interrupt driven and might be quite variable.

No. Available just checks the receive buffer to see if the head pointer is the same as the tail pointer.

Hi Delta_G

What I meant is the characters going into the buffer are from a serial interrupt and so the available test is going to be just as variable.

My solution, if it works, can give me around 21ns resolution. Overkill for this application but good, as a general purpose logic analyser.

Bob

I don't know SAMD timers but similar thing is surely possible with Arduino Uno so I think it should be doable. I see only one problem: if last bit(s) sent are 1 you cannot say when transmission ended and idle period started. So you will not measure time "form last bit to start of next byte" but "from last zero to start of next packet".

You may have to cope with 2 events: the start of a character transmission which can be recognized from the start bit level change, and the end of a transmission from the UART interrupt. The time between characters can be determined only when the following character is being sent, so that it IMO does not make a difference whether you measure the gap in between or from end to end of a character transmission. Eventually a timeout should be used to detect the end of a package transmission, if there is no next package transmitted immediately.

Hooking into the UART receive interrupt will require a library hack, that's all.

Thanks to all,

I just discovered that while the SAMD can have up to 5 x 16 bit timers (TC) and that you can pair them to get 32 bits. The G version of the chip as used on the Arduinos has only 3 x 16 bit ones and so my desire for 2 x 32bit timers is not possible. 16 bits would only give me a bit over 1 second of measurement.

There are some other 24 bit timers (TCC) but I need to read up on those to see if they will work for me.

By the way, does anyone know which timers are already dedicated to the arduino and should not be changed?

thanks

Bob

bobdring: 16 bits would only give me a bit over 1 second of measurement.

This is no problem. You can "extend in software" the timer length: simply count overflows somehow. It needs some thought to prevent a glitch when the overflow happens close to the event you are interested in but it is not so hard.