Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: USB MIDI clock jitter with ILI9341 display calls -- use two Teensys?

  1. #1

    USB MIDI clock jitter with ILI9341 display calls -- use two Teensys?


    I'm using the Teensy 4.1 to build a MIDI program change sequencer/orchestrator. I wanted a device that acts as the main clock source for various MIDI instruments, and I wanted a device that can sequence program change messages. Most existing sequencers do way more than that, so I thought this would be a good project for a Teensy.

    All it really does is send MIDI clock and program change messages over USB MIDI to a max of 4 instruments connected to the Teensy via the USB Host port.

    I'm also using a ILI9341 driven display module for the main UI display, specifically this one for now for prototyping:

    The rest of the build is just some switches/buttons.

    Here's a link to my unfinished code.

    Here's a photo of the prototype so far. All the wiring is underneath the perfboard, but I don't think the wiring is the problem.

    Here's a video of the MIDI clock working as expected, with no jitter, with no calls to update the display during playback.

    Problem 1:

    I have some code in XRSequencer.cpp, which gets executed during the main loop if the sequencer is running:

    void Sequencer::doStep()
        for (auto &track : this->instrumentTracks) {
            if (!track->deviceActive || !track->trackActive) {
            bool isProgChanging = track->doStep(this->usb);
            if (this->hardStart || isProgChanging) {
                // EXPERIMENTAL, highlight program index indicator on step
    //            this->display->drawMainScreen(
    //                this->currInstrument,
    //                this->instrumentTracks,
    //                true,
    //                this->tempo,
    //                this->currCursoredProgramIdx,
    //                this->cursoringPrograms,
    //                true
    //            );
    when the commented out part is uncommented, that portion of the code makes calls to update the display while the internal sequencer is running / sending MIDI messages. In this example, the calls to update the display occur on every completed bar (isProgChanging), since this code updates the display to indicate which program is currently playing.

    I notice some jitter in the clock. The instruments begin to sound disjointed, like every display update is introducing some delay time to the main sequencer update loop.

    See this video for an example of this problem.

    Problem 2:

    There's another area of the code that I modified to try and allow setting the tempo while sequencer is running, but which causes the clock to get all messed up, more extreme than the previous example. I think the reason it's so messed up is because every time I push the up/down buttons to set the tempo, the display is being updated, causing the clock timing to get messed up somehow.

    This is that portion of the code in XRSequencer.cpp, but is currently limited to being executed if the sequencer is not running, so as to not cause this issue:
    if (upBtnPressed) {
        this->tempo = min((int)this->tempo, 240);
    } else if (downBtnPressed) {
        this->tempo = max((int)this->tempo, 60);

    See this video for an example of this problem.


    This could be a code issue, but my thought is, what I just offloaded the display processing to a separate Teensy, so that the MIDI processing isn't hindered by the display processing? I'd love to not have to do that, but I'm not sure what else in the code could be causing the MIDI jitter like this.

    Thanks in advance!

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    @KurtE has developed a DMA based update for the ILI9341 - it might be this with "Asynchronous Update support (Frame buffer)" ::

    That takes a memory buffer for the screen - but the DMA update may run without delay or other issues

  3. #3
    Thanks -- I'm not familiar with DMA, but on reading about it, looks like it may work for what I need. Just not really sure how to exactly implement it. The examples in KurtE's ILI9341 repository seem a bit complex, it'll take me a bit to wrap my head around it I think.

  4. #4
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Perhaps run an example to see it work.
    Then following an example to swap the library and instantiation, the change would come with updates to the buffer - then kicking off the DMA driven update - that will return while the screen is updated.

  5. #5
    I think Problem #2 is actually not display-related. It has to do with changing the step length while the sequencer is running, not sure why it freaks out though. I may just not allow the tempo to be adjusted while the sequencer is running.

  6. #6
    I eked out a little bit more flexibility by using a coroutine library. I didn't want to try and implement a fully fledged RTOS or multi-threading just yet, but now I'm at least able to push relatively infrequent updates to the display while the sequencer is running without the internal clock stuttering. The DMA approach didn't seem to reduce the load / blocking at all, and it introduced nasty looking tearing in the screen on draw calls.

    I'd love to swap the current MIDI/internal clock code out with this eventually:

    But that library uses some core AVR stuff for the timing that the Teensy doesn't have, and I'm not well versed in the lower level timing stuff enough to try and port it over to the Teensy.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts