Page 1 of 1

spiStop() breaks SPI reception on STM32F042

Posted: Fri Apr 17, 2020 7:16 pm
by elagil
Hi,

I am using an STM32F042 and I want to use the I2C and SPI drivers together. Due to the DMA stream overlap, I need to use the start/stop functionality in both drivers. Still, there seems to be some conflict and I get broken data reception and transmission on SPI. I cannot exactly tell what is wrong (no scope at the moment) but I receive wrong data, and my peripherals behave incorrectly.

If I disable my I2C thread, including all data exchange, the problems reduce to SPI reception errors. When I remove the spiStop() command, everything is fine. Is something wrong with spiStop()?

Here is my spiHelper that exchanges data with the peripherals. It is inspired by the demo applications. I am also using a semaphore that prevents I2C and SPI from acting at the same time.

Thanks in advance!

Code: Select all

void spiExchangeHelper(SPIDriver *spi, const SPIConfig *conf, uint32_t length, uint8_t *txbuf, uint8_t *rxbuf)
{
    chBSemWait(&dma_lock);

    spiStart(spi, conf);
    /* Bus acquisition and SPI reprogramming.*/
    spiAcquireBus(spi);

    /* Slave selection and data transmission.*/
    spiSelect(spi);

    if (rxbuf != NULL && txbuf != NULL) // exchange data
        spiExchange(spi, length, txbuf, rxbuf);

    else if (txbuf != NULL && rxbuf == NULL) // only send
        spiSend(spi, length, txbuf);

    else if (rxbuf != NULL && txbuf == NULL) // only receive
        spiReceive(spi, length, rxbuf);

    spiUnselect(spi);

    /* Releasing the bus.*/
    spiReleaseBus(spi);
    //spiStop(spi);

    chBSemSignal(&dma_lock);
}

Re: spiStop() breaks SPI reception on STM32F042

Posted: Fri Apr 17, 2020 8:24 pm
by Giovanni
Try enabling assertions, do you get some error?

Giovanni

Re: spiStop() breaks SPI reception on STM32F042

Posted: Fri Apr 17, 2020 8:51 pm
by elagil
No, I do have them enabled already.

Currently, I have two slaves on the SPI bus: A display and a temperature sensor, with a chip select each. The data that I receive from the temperature sensor is byte-swapped (it is 16 bit long), when I use the spiStop() command.

Re: spiStop() breaks SPI reception on STM32F042

Posted: Fri Apr 17, 2020 8:59 pm
by Giovanni
Try putting a small delay before spiStop and/or i2cStop then see if it changes anything, it could be an hint about the problem.

Giovanni

Re: spiStop() breaks SPI reception on STM32F042

Posted: Fri Apr 17, 2020 9:23 pm
by elagil
Ok, I tried a delay of up to 100ms (chThdSleepMilliseconds), but the bytes are still flipped. Should the delay be blocking or is sleep sufficient? I will compare the dma registers of the two implementations tomorrow, if there is some issue to be found in those.

Re: spiStop() breaks SPI reception on STM32F042

Posted: Sat Apr 18, 2020 7:06 am
by Giovanni
You should check if the DMA channels are disabled when xxxStop() is executed. I have no other ideas about the "swap" problem, probably we are not looking at the right thing.

Giovanni