c++ – Connecting stm32 to RPi via SPI

Question:

I am making an SPI-CAN adapter based on stm32f103c8t6 for RPi 3 and ran into a number of problems and questions.

1) RPi program. To work with SPI, I use the bcm2835-spi library. The whole program hangs when trying to send a message:

#include <QCoreApplication>
#include </home/pi/CAN_Process/src/bcm2835.h>

#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <string.h>
#include <stdio.h>

int main(void)
{
    //SPI INIT
    if (!bcm2835_init())
    {
        printf("bcm2835_init failed. Are you running as root??\n");
        return 1;
    }
    bcm2835_spi_begin();
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                   // The default
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_64); // 64 прескаллер
    bcm2835_spi_chipSelect(BCM2835_SPI_CS0);                      // The default
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);      // the default

    uint8_t spi_write[11];
    uint8_t spi_read[11];

    //infinite
    while(1){
        for(uint8_t i=0;i<11;i++)spi_write=i;//Просто заполнение массива    
        bcm2835-spi-transfern(spi_write,11);//Строчка на которой всё зависает
    }
    bcm2835_spi_end();
    bcm2835_close();
    return 1;
}

If someone has a ready-made SPI recipe for the WiringPI library, then I'm happy to switch to it.

2) Configuring SPI. Since the frequency of the RPi 3 processor is 400 MHz and for the SPI clock, you can select prescallers only multiples of two, and the frequency of the Stm32 8 MHz quartz and the SPI frequency cannot be selected either, you cannot select the same frequencies. I chose the SPI frequency on RPi 6.250MHz. And on stm32 the maximum (18 Mbit / s), hoping that it will still be synchronized on the CLK bus. Is it right or not?

3) Program on stm32. I work through CubeMX and HAL library. When the SPI is initialized, the leg for selecting the Slave is not initialized. I understand that our stm32 works as a slave, but how can we correctly implement the CE bus from the stm32 side? I have only one slave for this SPI bus, does this mean that the choice of slave from the stm32 side can not be processed in any way? Thank you very much.

Answer:

  • I think the hang can occur due to the fact that the function is called very quickly and endlessly, clogging up the queue. Try making a call by pressing a button, or calling every second once.

  • That's right, you can and not the maximum.

Scroll to Top
AllEscort