spiStop() breaks SPI reception on STM32F042

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
elagil
Posts: 94
Joined: Tue Sep 19, 2017 7:38 am
Has thanked: 8 times
Been thanked: 7 times

spiStop() breaks SPI reception on STM32F042

Post 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);
}
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: spiStop() breaks SPI reception on STM32F042

Post by Giovanni »

Try enabling assertions, do you get some error?

Giovanni
elagil
Posts: 94
Joined: Tue Sep 19, 2017 7:38 am
Has thanked: 8 times
Been thanked: 7 times

Re: spiStop() breaks SPI reception on STM32F042

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: spiStop() breaks SPI reception on STM32F042

Post 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
elagil
Posts: 94
Joined: Tue Sep 19, 2017 7:38 am
Has thanked: 8 times
Been thanked: 7 times

Re: spiStop() breaks SPI reception on STM32F042

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: spiStop() breaks SPI reception on STM32F042

Post 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
Post Reply