0

I couldn't find any sample that uses SPI API directly from the application. Instead, there are many sensors that use SPI as a bus.

Trying to write it from scratch, I've encountered a problem with getting the device binding to a spi-device node. I should also mention that I have working samples of other peripherals like GPIOs or ADC, so I believe it is a problem with SPI, not the project configuration.

Here is the excerpt of the devicetree:

&spi1 {
    pinctrl-0 = <&spi1_sck_pb3 &spi1_miso_pb4 &spi1_mosi_pb5>;
    cs-gpios = <&gpiob 9 GPIO_ACTIVE_LOW>;
    status = "okay";

    spi1_dev0: spi-device@0 {
        reg = <0>;
        spi-max-frequency = <1000000>;
        label = "spi_dev1";
        status = "okay";    
    };
};

I've set the SPI in the project config:

CONFIG_GPIO=y
CONFIG_SPI=y
CONFIG_SPI_STM32=y
CONFIG_SPI_STM32_USE_HW_SS=n

But unfortunately, the DT node of the spi1_dev0 deice cannot be resolved

const struct device *spi_dev = device_get_binding(DT_NODELABEL(spi1_dev0));
const struct spi_config *spi_cfg = SPI_CONFIG_DT(DT_NODELABEL(spi1_dev0), SPI_WORD_SET(8), 10);

[build] zephyr/include/generated/devicetree_unfixed.h:11068:34: error: 'DT_N_S_soc_S_spi_40013000_S_spi_device_0' undeclared (first use in this function); did you mean 'DT_N_S_soc_S_spi_40013000_S_spi_device_0_BUS'?

I also tried to use DT_BUS on top of DT_NODELABEL but without success.

Is it possible to use buses like SPI or I2C directly?

4

1 回答 1

0

以下对我有用:

...
#include <drivers/gpio.h>
#include <drivers/spi.h>

struct spi_cs_control spi_cs = {
    /* PA4 as CS pin */
    .gpio_dev = DEVICE_DT_GET(DT_NODELABEL(gpioa)),
    .gpio_pin = 4,
    .gpio_dt_flags = GPIO_ACTIVE_LOW,
    /* delay in microseconds to wait before starting the transmission and before releasing the CS line */
    .delay = 10,
};

#define SPI_CS (&spi_cs)

struct spi_config spi_cfg = {
    .frequency = 350000,
    .operation = SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | SPI_WORD_SET(8) | SPI_LINES_SINGLE | SPI_LOCK_ON,
    .cs = SPI_CS,
};

void spi_init()
{
    spi = device_get_binding("SPI_1");
    ....
}

SPI_1是设备树中 spi_1 节点的标签。检查构建文件夹中的 zephyr.dts 文件以找出您的 spi 节点标签。这是一个例子:

        spi1: arduino_spi: spi@40013000 {
            compatible = "st,stm32-spi-fifo", "st,stm32-spi";
            #address-cells = < 0x1 >;
            #size-cells = < 0x0 >;
            reg = < 0x40013000 0x400 >;
            clocks = < &rcc 0x3 0x1000 >;
            interrupts = < 0x23 0x5 >;
            status = "okay";
            label = "SPI_1";
            pinctrl-0 = < &spi1_sck_pa5 &spi1_miso_pa6 &spi1_mosi_pa7 >;
            cs-gpios = < &gpiod 0xe 0x11 >;

当然,您可以使用自己的 .overlay 文件覆盖这些设置。

于 2021-09-12T15:33:10.483 回答