How to implement a 2MHz 20bit protocol

I am currently implementing a device which accepts 20bit of data at 2MHz.
How can i implement this?

Idealy i want a buffer accepting word´s(16bit blocks) which supplies the data to the device.

The device requires a CLOCK at 2MHz, a SYNC held high during data transfer and low for the last bit which is parity. The data goes on a third pin. The first three bits are command(and can be static 0,0,1), the following 16 bits are data and the last bit is parity (even).

I have a oscilloscope to check my progress...

I have tried the following

PWM:
I set up analogWriteResolution(16);
analogWriteFrequency(pwm_pin_clock, 200e4);
analogWriteFrequency(pwm_pin_data, 200e4);
Synckronize them
FTM0_SYNC = 1;
FTM1_SYNC = 1;
FTM2_SYNC = 1;
and finally
attachInterrupt(digitalPinToInterrupt(pwm_pin_clock), data, CHANGE);
PWM result:
The interrupt executes data() once every 2-300 clock cykles so i guess this approach is to slow.

SPI:
I hacked SPI.cpp / SPI.h to only send 4 bits instead of 8 so that i can have cycles of 5x4 = 20bits but my knowledge is limited and i have not managed so far

IntervalTimer:
I used the IntervalTimer with hardcoded values and this was my best try so far the scope shows a clear cloch, a sync is low on every 20th bit and the data is showing nicely but...... I cant get it to go faster than 1MHz.

OC:
I have done some overclocking but it does not help in any of the scenarios i have tried so far.

Now i need assistance! Is there another approach? have i missed something or is this a hard nut to crack?

Help anyone?

Processor?

Cortex M4 i believe (Teensy 3.6)

In which case I won't be any help.

I hope your project goes well.

For anyone looking in the future:

I implemented this setting frequency on a analog ping to 2MHz and had an interrupt run a port manipulation command setting Sync, data1 and data2 pins at the same time.

For more info see:
https://www.reddit.com/r/OpenGalvo/

The code will eventually be published on GitHub -> Search for OpenGalvo

Implementation is for XY2-100 Galvo control....

Edit 2021: Usable versions have been now posted on github :slight_smile: