when I change from compatible = "nordic,nrf-spi"; to compatible = "nordic,nrf-spim";
the SPI transceive failed
my overlay file
&spi1 {
compatible = "nordic,nrf-spim";
status = "okay";
pinctrl-0 = <&spi1_default>;
pinctrl-1 = <&spi1_sleep>;
pinctrl-names = "default","sleep";
cs-gpios = <&gpio0 17 GPIO_ACTIVE_LOW>; // 选择适当的CS引脚
// def-char = <0xFF>; // 设定默认字符
w25m02g: w25m02g@0 {
compatible = "winbond,w25m02g";
reg = <0>;
spi-max-frequency = <8000000>;
size = <2147483648>;
wp-gpios = <&gpio0 14 GPIO_ACTIVE_LOW>;
hold-gpios = <&gpio0 16 GPIO_ACTIVE_LOW>;
};
};
&pinctrl {
spi1_default: spi1_default {
group1 {
psels = <NRF_PSEL(SPIM_SCK, 0, 26)>, // 根据需要调整引脚配置
<NRF_PSEL(SPIM_MOSI, 0, 24)>,
<NRF_PSEL(SPIM_MISO, 0, 20)>;
};
};
spi1_sleep: spi1_sleep {
group1 {
psels = <NRF_PSEL(SPIM_SCK, 0, 26)>,
<NRF_PSEL(SPIM_MOSI, 0, 24)>,
<NRF_PSEL(SPIM_MISO, 0, 20)>;
low-power-enable;
};
};
};
my yaml file
# Copyright (c) 2020 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0
description: Winbond W25M02G SPI flash
compatible: "winbond,w25m02g"
#include: [spi-device.yaml, pinctrl-device.yaml,sensor-device.yaml]
include: [spi-device.yaml, sensor-device.yaml]
properties:
size:
type: int
default: 2147483648
description: Flash capacity in bits.
wp-gpios:
type: phandle-array
required: false
description: /WP pin
hold-gpios:
type: phandle-array
required: false
description: /HOLD pin (edited)
main code
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
// #include <zephyr/drivers/sensor.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/drivers/spi.h>
#include <zephyr/drivers/gpio.h>
#include <stdio.h>
#include <string.h>
// int main(void)
// {
// printf("Hello World! %s\n", CONFIG_BOARD);
// return 0;
// }
LOG_MODULE_REGISTER(w25m02g_example);
// SPI configuration
#define SPIOP SPI_WORD_SET(8) | SPI_TRANSFER_MSB
struct spi_dt_spec spispec = SPI_DT_SPEC_GET(DT_NODELABEL(w25m02g), SPIOP, 0);
static const struct gpio_dt_spec wp_gpio = GPIO_DT_SPEC_GET(DT_NODELABEL(w25m02g), wp_gpios);
static const struct gpio_dt_spec hold_gpio = GPIO_DT_SPEC_GET(DT_NODELABEL(w25m02g), hold_gpios);
static int w25m02g_read_jedec_id(uint8_t *id[4])
{
// uint8_t tx_data[] = {0x9F, 0x00, 0x00, 0x00}; // JEDEC ID command
uint8_t tx_data[] = {0x9F}; // JEDEC ID command
uint8_t len = 4;
int err;
struct spi_buf tx_spi_buf = {.buf = (void *)&tx_data, .len = sizeof(tx_data)};
printf("%04x\n",sizeof(tx_data));
struct spi_buf_set tx_spi_buf_set = {.buffers = &tx_spi_buf, .count = 1};
struct spi_buf rx_spi_bufs = {.buf = id, .len = len};
struct spi_buf_set rx_spi_buf_set = {.buffers = &rx_spi_bufs, .count = 1};
err = spi_transceive_dt(&spispec, &tx_spi_buf_set, &rx_spi_buf_set);
for (int i = 0; i < 1; i++) {
printk("TX transmit: %02x\n", tx_data[i]);
}
for (int i = 0; i < 4; i++) {
printk("RX recv: %04x\n", id[i]);
}
if (err < 0) {
LOG_ERR("spi_transceive_dt() failed, err: %d", err);
return err;
}
return 0;
// return status;
}
void main(void) {
printf("w25m02g Test Begin\n");
if (!spi_is_ready_dt(&spispec)) {
// if (!device_is_ready(&spispec)) {
LOG_ERR("SPI device not ready");
return 0;
}
while(1){
k_sleep(K_MSEC(3000));
uint8_t jedec_id[4];
// uint8_t values[3];
uint8_t size = 4;
printk("tessting master 03\n");
w25m02g_read_jedec_id(jedec_id[4]);
printk("JEDEC ID: %04x %04x %04x %04x \n", jedec_id[0], jedec_id[1], jedec_id[2], jedec_id[3]);
}
}