stm32h7xx-hal icon indicating copy to clipboard operation
stm32h7xx-hal copied to clipboard

How to perform SPI write before starting DMA transfer?

Open nullobject opened this issue 1 year ago • 3 comments

Is it possible to do a normal SPI write before starting a DMA transfer?

I am trying to use DMA to write the frame buffer to an OLED display (SSD1322), but I need to write a couple of commands to the display before sending the pixel data.

For example, building on the SPI DMA example it would be nice to be able to do something like this:

transfer.start(|spi| {
    // Write a command before beginning the transfer
    dc.set_low();
    cs.set_low();
    spi.write(&[0x11u8, 0x22, 0x33]).unwrap(); // <-- can't call write here because SPI is disabled
    cs.set_high();

    // Write frame buffer using DMA transfer
    dc.set_high();
    cs.set_low();
    spi.enable_dma_tx();
    spi.inner_mut().cr1.modify(|_, w| w.spe().enabled());
    spi.inner_mut().cr1.modify(|_, w| w.cstart().started());
});

Unfortunately the types make this impossible, as the SPI reference passed to the closure is disabled.

Any suggestions?

It would be nice to just use a similar API to the I2C DMA example:

transfer.start(|i2c| {
    // This closure runs right after enabling the stream

    // Issue the first part of an I2C transaction to read data from a
    // touchscreen

    // Write register index
    i2c.write(0xBA >> 1, &[0x41, 0xE4]).unwrap();

    // Start a read of 10 bytes
    i2c.master_read(0xBA >> 1, 10, i2c::Stop::Automatic);
});

nullobject avatar Nov 29 '23 05:11 nullobject