[sdiy] MIDI I/O c code..

Tom Wiltshire tom at electricdruid.net
Fri Mar 20 15:59:52 CET 2020


> On 20 Mar 2020, at 14:48, René Schmitz <synth at schmitzbits.de> wrote:
> 
> On 20.03.2020 15:11, Jean-Pierre Desrochers wrote:
>> Hi Mikko,
>> Here is my USART Rx  interrupt routine :
>> Maybe you’ll spot any errors or things I forgot.. ??
> 
> Without looking at the interrupt setup code I'm not sure, but it seems you check the USART in a Timer ISR is that the intention? (by how you named things.)
> if so, it could be that sometimes you miss a byte because there is an overrun between two timer cycles. have you tried seeing if the overrun flag is actually set? (maybe toggle a pin so you can check with a scope.)
> 
> Best,
> René

+1agree with René - what’s going on with Timer1?

You’ve got a UART Rx interrupt, so use that instead. When the byte comes in, stick it in the buffer like you’re doing. Maybe use the Timer interupt to process MIDI messages every couple of milliseconds or so (It takes a millisecond for a full note-on message to arrive anyway, so there’s not *much* point going faster). If you use the Rx interrupt, you also don’t need to check RCIF and RCIE on entry, which saves you time.

I’d also use modulus arithmetic to limit the length of the buffer (especially since your buffer is 32 bytes not something awkward like 57). That saves an “if” statement:

 rcvbuf[rcvoffset & 31] = x;         // move byte to rcv buffer, limited to index 0-31                                                                                                                       
 rcvoffset++;                          // offset next byte


Personally, I’d probably drop some of the safety checking the various error flags in the interrupt. You could check those in the processing routine instead. They’ll just be slowing things down and possibly making the events they check for more likely to occur!

Tom

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://synth-diy.org/pipermail/synth-diy/attachments/20200320/b6078edf/attachment.htm>


More information about the Synth-diy mailing list