SPI custom clock frequency value calculation

I have a slightly off the wall requirement where I need to send a serial data stream at 5kbps, it will be used to drive an external OOK 433MHz transmitter. The data format and bit timing are rigid as it will be used with some existing legacy receivers. The Softdevice will be running, the system will be advertising and accepting GATT connections at all times.

Because the softdevice is running and will interrupt any code I need DMA, SPI looks like the best option as far as I can see. But is it possible to set the frequency as low as 5k bps? I have read other articles here and know it is out of spec, but that doesn't matter here as it's not actually interfacing to another SPI device. If so, what is the calculation for custom bitrates?

UART won't work because it adds start/stop bits.

(If you can suggest a better way to serially transmit 56 bits of data by DMA I'm happy to listen!)

  • Some of the peripherals use common hardware implementations, so for example a peripheral can be used as either SPI or I2C depending on the enable bits; that can create difficulties if switching between them but maybe the baud rate generator is similar to the Uart baud rate generator; I don't remember trying this on the SPIM but maybe worth a look at my (unofficial) code:

       // Baud rate calculation on nRF51 and nRF52832
       // ===========================================
       //
       // Calculate BAUDRATE value settings for the black-magic 20-bit baud rate generator
    
       uint32_t CalculatedRegisterValue;
       uint64_t SystemClock = 16000000ULL;    // Typically 16MHz
       uint64_t MagicScaler = 32; // Preserves bits on divisions, shift 32 bits
    
       // Baudrate generator for the UART is the top 20 bits of a 32-bit field; add in rounding 0.5 ls bit
       CalculatedRegisterValue = (uint32_t)(((((uint64_t)RequiredBaudRate << MagicScaler) + (SystemClock>>1)) / SystemClock) + 0x800) & 0xFFFFF000;

  • Sadly 5kbps wasn't achievable, although it will go low enough the frequency setting doesn't have enough resolution - it jumps from 4k to 6k

        spi_config.frequency =   0x0017fffeUL; // 250uS 4kbps
        spi_config.frequency =   0x0017ffffUL; // 165uS 6kbps

    Still it may be a moot point after all: looking at hardware options for the 433MHz RF side many devices now incorporate a TX FIFO so I may be able to just use SPI as-is.

Related