Haberler:

Foruma Resim Yükleme ve Boyut Sınırlaması ( ! )  https://bit.ly/2GMFb8H

Ana Menü

NRF52832 W25q32 Zephyr

Başlatan merve damar, 29 Şubat 2024, 13:40:33

merve damar

Merhabalar herkese kolaylıklar gelsinn ,
Nrf52832 ile W25q32 okuma yazma ve silme fonksiyonlarına ihtiyacım var. Spi iletişimini kurdum W25q32 idsini okuyabiliyorum ama kodlarda çıktı alırken sürekli ff değerini okuyorum. Hiç bir örnek kod bulamadım. Destek olabilir misiniz kod hakkında bir yorumunuz var mı  :du:
Kod: c
#include <zephyr/kernel.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/spi.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>

typedef int ret_code_t;


#define SPI_BUF_LEN 6
#define GPIO_CS 28

#define SPI_MODE_CPHA 0
const struct device *gpio0_dev;
const struct device *spi_dev;

struct spi_config spi_cfg;
struct spi_cs_control chip;




void SPI_Init(void) {
    spi_dev = DEVICE_DT_GET(DT_NODELABEL(spi1));
    if (!spi_dev) {
        printk("Failed to get SPI device\n");
        return;
    }

    gpio0_dev = DEVICE_DT_GET(DT_NODELABEL(gpio0));
    if (!gpio0_dev) {
        printk("Failed to get GPIO device\n");
        return;
    }

    spi_cfg.frequency = 10000000;
    spi_cfg.operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB |
                         SPI_MODE_CPHA;
    spi_cfg.slave = 0;

    chip.gpio.port = gpio0_dev;
    chip.gpio.pin = GPIO_CS;
    chip.gpio.dt_flags = GPIO_ACTIVE_LOW;
    chip.delay = 0;

    spi_cfg.cs = chip;

    printk("\r\nSPI device is initialized and ready.\n");
}


uint16_t spi_flash_read_mid_did(void) {
    int err;
    static uint8_t tx_buffer[4] = {
        0x90,
        0x00,
        0x00,
        0x00
    };
    static uint8_t rx_buffer[6];

    const struct spi_buf tx_buf = {
        .buf = tx_buffer,
        .len = sizeof(tx_buffer)
    };
    const struct spi_buf_set tx = {
        .buffers = &tx_buf,
        .count = 1
    };

    struct spi_buf rx_buf = {
        .buf = rx_buffer,
        .len = sizeof(rx_buffer),
    };
    const struct spi_buf_set rx = {
        .buffers = &rx_buf,
        .count = 1
    };

    gpio_pin_set(gpio0_dev, GPIO_CS, 0); 

    err = spi_transceive(spi_dev, &spi_cfg, &tx, &rx);
    if (err) {
        printk("SPI error: %d\n", err);
    }

    gpio_pin_set(gpio0_dev, GPIO_CS, 1); 

    uint16_t manufacturer_id = (rx_buffer[4] << 8) | rx_buffer[5];
    printk("Manufacturer ID: 0x%x\n", manufacturer_id);
    return manufacturer_id;
}


void SpiFlash_ChipErase(void) {
    int err;
    gpio_pin_set(gpio0_dev, GPIO_CS, 0);

    uint8_t wren_cmd = 0x06; 
    const struct spi_buf tx_buf_wren = {
        .buf = &wren_cmd,
        .len = sizeof(wren_cmd)
    };
    const struct spi_buf_set tx_wren = {
        .buffers = &tx_buf_wren,
        .count = 1
    };

    err = spi_write(spi_dev, &spi_cfg, &tx_wren);
    if (err) {
        printk("SPI error: %d\n", err);
        return;
    }

    while (spi_transceive(spi_dev, &spi_cfg, NULL, NULL)) {
        k_sleep(K_MSEC(1));
    }

    gpio_pin_set(gpio0_dev, GPIO_CS, 1);


    gpio_pin_set(gpio0_dev, GPIO_CS, 0);

    uint8_t chip_erase_cmd = 0xC7;
    const struct spi_buf tx_buf_chip_erase = {
        .buf = &chip_erase_cmd,
        .len = sizeof(chip_erase_cmd)
    };
    const struct spi_buf_set tx_chip_erase = {
        .buffers = &tx_buf_chip_erase,
        .count = 1
    };

    err = spi_write(spi_dev, &spi_cfg, &tx_chip_erase);
    if (err) {
        printk("SPI error: %d\n", err);
        return;
    }

   
    while (spi_transceive(spi_dev, &spi_cfg, NULL, NULL)) {
        k_sleep(K_MSEC(1));
    }

    gpio_pin_set(gpio0_dev, GPIO_CS, 1);
}


void SpiFlash_Write(uint32_t StartAddress, uint8_t *u8DataBuffer) {
    uint32_t i = 0;
    int err;


    gpio_pin_set(gpio0_dev, GPIO_CS, 0);


    uint8_t wren_cmd = 0x06; 
    const struct spi_buf tx_buf_wren = {
        .buf = &wren_cmd,
        .len = sizeof(wren_cmd)
    };
    const struct spi_buf_set tx_wren = {
        .buffers = &tx_buf_wren,
        .count = 1
    };

    err = spi_write(spi_dev, &spi_cfg, &tx_wren);
    if (err) {
        printk("SPI error during Write Enable command: %d\n", err);
        return;
    }


      while (spi_transceive(spi_dev, &spi_cfg, NULL, NULL)) {
            k_sleep(K_MSEC(1));
        }

  
    gpio_pin_set(gpio0_dev, GPIO_CS, 1);


    gpio_pin_set(gpio0_dev, GPIO_CS, 0);

   
    uint8_t page_program_cmd = 0x02; 
    const struct spi_buf tx_buf_page_program_cmd = {
        .buf = &page_program_cmd,
        .len = sizeof(page_program_cmd)
    };
    const struct spi_buf_set tx_page_program_cmd = {
        .buffers = &tx_buf_page_program_cmd,
        .count = 1
    };

    err = spi_write(spi_dev, &spi_cfg, &tx_page_program_cmd);
    if (err) {
        printk("SPI error during Page Program command: %d\n", err);
        return;
    }

  
    uint8_t addr_buffer[3] = {
        (StartAddress >> 16) & 0xFF,
        (StartAddress >> 8) & 0xFF,
        StartAddress & 0xFF
    };
    const struct spi_buf tx_buf_addr = {
        .buf = addr_buffer,
        .len = sizeof(addr_buffer)
    };
    const struct spi_buf_set tx_addr = {
        .buffers = &tx_buf_addr,
        .count = 1
    };

    err = spi_write(spi_dev, &spi_cfg, &tx_addr);
    if (err) {
        printk("SPI error during address transmission: %d\n", err);
        return;
    }

 
    while (i < 6) {
        uint8_t data = u8DataBuffer[i++];
        const struct spi_buf tx_buf_data = {
            .buf = &data,
            .len = sizeof(data)
        };
        const struct spi_buf_set tx_data = {
            .buffers = &tx_buf_data,
            .count = 1
        };

        err = spi_write(spi_dev, &spi_cfg, &tx_data);
        if (err) {
            printk("SPI error during data transmission: %d\n", err);
            return;
        }
    }


      while (spi_transceive(spi_dev, &spi_cfg, NULL, NULL)) {
            k_sleep(K_MSEC(1));
        }

    gpio_pin_set(gpio0_dev, GPIO_CS, 1);

k_sleep(K_MSEC(1));

}

void SpiFlash_Read(uint32_t StartAddress, uint8_t *u8DataBuffer)
{
    uint32_t i;
    int err;
k_sleep(K_MSEC(1));
    gpio_pin_set(gpio0_dev, GPIO_CS, 0);

    uint8_t read_cmd = 0x03; 
    const struct spi_buf tx_buf_read_cmd = {
        .buf = &read_cmd,
        .len = sizeof(read_cmd)
    };
    const struct spi_buf_set tx_read_cmd = {
        .buffers = &tx_buf_read_cmd,
        .count = 1
    };

    err = spi_write(spi_dev, &spi_cfg, &tx_read_cmd);
    if (err) {
        printk("SPI error during Read command: %d\n", err);
        return;
    }

 
    uint8_t addr_buffer[3] = {
        (StartAddress >> 16) & 0xFF,
        (StartAddress >> 8) & 0xFF,
        StartAddress & 0xFF
    };
    const struct spi_buf tx_buf_addr = {
        .buf = addr_buffer,
        .len = sizeof(addr_buffer)
    };
    const struct spi_buf_set tx_addr = {
        .buffers = &tx_buf_addr,
        .count = 1
    };

    err = spi_write(spi_dev, &spi_cfg, &tx_addr);
    if (err) {
        printk("SPI error during address transmission: %d\n", err);
        return;
    }


    while (spi_transceive(spi_dev, &spi_cfg, NULL, NULL)) {
        k_sleep(K_MSEC(1));
    }

 
    for(i = 0; i < 5; i++)
    {
        uint8_t dummy_data = 0x00;
        const struct spi_buf tx_buf_dummy_data = {
            .buf = &dummy_data,
            .len = sizeof(dummy_data)
        };
        const struct spi_buf_set tx_dummy_data = {
            .buffers = &tx_buf_dummy_data,
            .count = 1
        };

        err = spi_transceive(spi_dev, &spi_cfg, &tx_dummy_data, &tx_dummy_data);
        if (err) {
            printk("SPI error during data transmission: %d\n", err);
            return;
        }

   
        while (spi_transceive(spi_dev, &spi_cfg, NULL, NULL)) {
            k_sleep(K_MSEC(1));
        }

        u8DataBuffer[i] = dummy_data;
    }

}

void main(void) {
    SPI_Init();
    uint32_t manufacturer_id;
    manufacturer_id = spi_flash_read_mid_did();
   SpiFlash_ChipErase();
    uint8_t data_buffer[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE};
   

    uint32_t start_address = 0x2000;

   
    SpiFlash_Write(start_address, data_buffer);

 
    uint8_t read_data_buffer[5]; 
    k_sleep(K_MSEC(1));
    SpiFlash_Read(start_address, read_data_buffer);

    printk("Read data:");
    for (int i = 0; i < 5; i++) {
        printk(" %02X", read_data_buffer[i]);
    }
    printk("\n");
}


Manufacturer ID: 0xef15
Read data: FF FF FF FF FF

buda kod çıktısı


parametre

#1
,..

X-Fi

prj.conf dosyasına aşağıdaki tanımları eklediniz mi? tüm projeyi görmeden hatayı bulmak zor.

CONFIG_SPI=n
CONFIG_NORDIC_QSPI_NOR=y

Ayrıca zephyr reposunda işinize yarayabilecek bir örnek mevcut.

https://docs.zephyrproject.org/latest/samples/drivers/spi_flash/README.html


http://www.coskunergan.dev/    (Yürümekle varılmaz, lakin varanlar yürüyenlerdir.)