flash erase

Hello forum ,

               spi_flash_1.zip

here i m using nrf52840 where it is customized board ....i tried to print jedec number for windbond w25q64 here this code prints the jedec number and also i tried to erase,write and read a one byte 0x55 but it is failed to erase ,write and read....pls help me to store the byte in flash....below i will upload screenshot for the above 

Parents
  • sry now i changed main.c as 

    #include <zephyr/kernel.h>
    #include <zephyr/sys/printk.h>
    #include <zephyr/device.h>
    #include <zephyr/drivers/gpio.h>
    #include <zephyr/drivers/flash.h>
    #include <nrfx_qspi.h>
    #include <hal/nrf_qspi.h>
    #include <string.h>
     
    // JEDEC ID command
    #define CMD_READ_JEDEC_ID 0x9F
     
    // LED aliases
    #define LED1_NODE DT_NODELABEL(led1)
    #define LED0_NODE DT_NODELABEL(led0)
    #define LED2_NODE DT_NODELABEL(led2)
    #define LEDB_NODE DT_NODELABEL(ledbacklight)
     
    static const struct gpio_dt_spec led1 = GPIO_DT_SPEC_GET(LED1_NODE, gpios);
    static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
    static const struct gpio_dt_spec led2 = GPIO_DT_SPEC_GET(LED2_NODE, gpios);
    static const struct gpio_dt_spec led  = GPIO_DT_SPEC_GET(LEDB_NODE, gpios);
     
    // Flash alias
    #define FLASH_NODE DT_NODELABEL(w25q64)
     
    // Test config
    #define TEST_OFFSET 0x00010000       // must be 4KB aligned
    #define TEST_SECTOR_SIZE 4096
    #define TEST_DATA_BYTE 0x55
     
    void read_qspi_jedec_id(void)
    {
        nrfx_err_t err_code;
        uint8_t read_buf[3] = {0};
     
        nrf_qspi_cinstr_conf_t cinstr_cfg = {
            .opcode    = CMD_READ_JEDEC_ID,
            .length    = NRF_QSPI_CINSTR_LEN_4B, // 1 command + 3 data
            .io2_level = true,
            .io3_level = true,
            .wipwait   = true,
            .wren      = false
        };
     
        err_code = nrfx_qspi_cinstr_xfer(&cinstr_cfg, NULL, read_buf);
        if (err_code != NRFX_SUCCESS) {
            printk("Failed to read JEDEC ID: %d\n", err_code);
            return;
        }
     
        printk("JEDEC ID: %02X %02X %02X\n", read_buf[0], read_buf[1], read_buf[2]);
    }
     
    void flash_test_1byte(const struct device *flash_dev)
    {
        uint8_t tx_byte = TEST_DATA_BYTE;
        uint8_t rx_byte = 0x00;
        int rc;

        printk("Erasing 4KB sector at 0x%08x...\n", TEST_OFFSET);
        rc = flash_erase(flash_dev, TEST_OFFSET, TEST_SECTOR_SIZE);
        if (rc) {
            printk("Erase failed: %d\n", rc);
            return;
        }

        printk("Writing 0x%02X to flash...\n", tx_byte);
        rc = flash_write(flash_dev, TEST_OFFSET, &tx_byte, sizeof(tx_byte));
        if (rc) {
            printk("Write failed: %d\n", rc);
            return;
        }

        rc = flash_read(flash_dev, TEST_OFFSET, &rx_byte, sizeof(rx_byte));
        if (rc) {
            printk("Read failed: %d\n", rc);
            return;
        }

        printk("Read back from 0x%08x = 0x%02X\n", TEST_OFFSET, rx_byte);

        if (rx_byte == tx_byte) {
            printk("QSPI: White check mark Byte matched!\n");
        } else {
            printk("QSPI: X Byte mismatch! Expected 0x%02X\n", tx_byte);
        }
    }

    void main(void)
    {
        printk("QSPI JEDEC ID Read & Flash Byte Test\n");
     
        const struct device *flash_dev = DEVICE_DT_GET(FLASH_NODE);
        if (!device_is_ready(flash_dev)) {
            printk("Flash device not ready!\n");
            return;
        }
     
        if (!gpio_is_ready_dt(&led) || !gpio_is_ready_dt(&led1) ||
            !gpio_is_ready_dt(&led0) || !gpio_is_ready_dt(&led2)) {
            printk("One or more LEDs not ready\n");
            return;
        }
     
        gpio_pin_configure_dt(&led,  GPIO_OUTPUT_ACTIVE);
        gpio_pin_configure_dt(&led1, GPIO_OUTPUT_ACTIVE);
        gpio_pin_configure_dt(&led0, GPIO_OUTPUT_ACTIVE);
        gpio_pin_configure_dt(&led2, GPIO_OUTPUT_ACTIVE);
     
        gpio_pin_set_dt(&led, 1);
        gpio_pin_set_dt(&led1, 1);
        gpio_pin_set_dt(&led0, 1);
        gpio_pin_set_dt(&led2, 0);
     
        k_sleep(K_MSEC(10));  // Wait for flash ready
     
        read_qspi_jedec_id();
     
        flash_test_1byte(flash_dev);
    } where i got in terminal as 
Reply
  • sry now i changed main.c as 

    #include <zephyr/kernel.h>
    #include <zephyr/sys/printk.h>
    #include <zephyr/device.h>
    #include <zephyr/drivers/gpio.h>
    #include <zephyr/drivers/flash.h>
    #include <nrfx_qspi.h>
    #include <hal/nrf_qspi.h>
    #include <string.h>
     
    // JEDEC ID command
    #define CMD_READ_JEDEC_ID 0x9F
     
    // LED aliases
    #define LED1_NODE DT_NODELABEL(led1)
    #define LED0_NODE DT_NODELABEL(led0)
    #define LED2_NODE DT_NODELABEL(led2)
    #define LEDB_NODE DT_NODELABEL(ledbacklight)
     
    static const struct gpio_dt_spec led1 = GPIO_DT_SPEC_GET(LED1_NODE, gpios);
    static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
    static const struct gpio_dt_spec led2 = GPIO_DT_SPEC_GET(LED2_NODE, gpios);
    static const struct gpio_dt_spec led  = GPIO_DT_SPEC_GET(LEDB_NODE, gpios);
     
    // Flash alias
    #define FLASH_NODE DT_NODELABEL(w25q64)
     
    // Test config
    #define TEST_OFFSET 0x00010000       // must be 4KB aligned
    #define TEST_SECTOR_SIZE 4096
    #define TEST_DATA_BYTE 0x55
     
    void read_qspi_jedec_id(void)
    {
        nrfx_err_t err_code;
        uint8_t read_buf[3] = {0};
     
        nrf_qspi_cinstr_conf_t cinstr_cfg = {
            .opcode    = CMD_READ_JEDEC_ID,
            .length    = NRF_QSPI_CINSTR_LEN_4B, // 1 command + 3 data
            .io2_level = true,
            .io3_level = true,
            .wipwait   = true,
            .wren      = false
        };
     
        err_code = nrfx_qspi_cinstr_xfer(&cinstr_cfg, NULL, read_buf);
        if (err_code != NRFX_SUCCESS) {
            printk("Failed to read JEDEC ID: %d\n", err_code);
            return;
        }
     
        printk("JEDEC ID: %02X %02X %02X\n", read_buf[0], read_buf[1], read_buf[2]);
    }
     
    void flash_test_1byte(const struct device *flash_dev)
    {
        uint8_t tx_byte = TEST_DATA_BYTE;
        uint8_t rx_byte = 0x00;
        int rc;

        printk("Erasing 4KB sector at 0x%08x...\n", TEST_OFFSET);
        rc = flash_erase(flash_dev, TEST_OFFSET, TEST_SECTOR_SIZE);
        if (rc) {
            printk("Erase failed: %d\n", rc);
            return;
        }

        printk("Writing 0x%02X to flash...\n", tx_byte);
        rc = flash_write(flash_dev, TEST_OFFSET, &tx_byte, sizeof(tx_byte));
        if (rc) {
            printk("Write failed: %d\n", rc);
            return;
        }

        rc = flash_read(flash_dev, TEST_OFFSET, &rx_byte, sizeof(rx_byte));
        if (rc) {
            printk("Read failed: %d\n", rc);
            return;
        }

        printk("Read back from 0x%08x = 0x%02X\n", TEST_OFFSET, rx_byte);

        if (rx_byte == tx_byte) {
            printk("QSPI: White check mark Byte matched!\n");
        } else {
            printk("QSPI: X Byte mismatch! Expected 0x%02X\n", tx_byte);
        }
    }

    void main(void)
    {
        printk("QSPI JEDEC ID Read & Flash Byte Test\n");
     
        const struct device *flash_dev = DEVICE_DT_GET(FLASH_NODE);
        if (!device_is_ready(flash_dev)) {
            printk("Flash device not ready!\n");
            return;
        }
     
        if (!gpio_is_ready_dt(&led) || !gpio_is_ready_dt(&led1) ||
            !gpio_is_ready_dt(&led0) || !gpio_is_ready_dt(&led2)) {
            printk("One or more LEDs not ready\n");
            return;
        }
     
        gpio_pin_configure_dt(&led,  GPIO_OUTPUT_ACTIVE);
        gpio_pin_configure_dt(&led1, GPIO_OUTPUT_ACTIVE);
        gpio_pin_configure_dt(&led0, GPIO_OUTPUT_ACTIVE);
        gpio_pin_configure_dt(&led2, GPIO_OUTPUT_ACTIVE);
     
        gpio_pin_set_dt(&led, 1);
        gpio_pin_set_dt(&led1, 1);
        gpio_pin_set_dt(&led0, 1);
        gpio_pin_set_dt(&led2, 0);
     
        k_sleep(K_MSEC(10));  // Wait for flash ready
     
        read_qspi_jedec_id();
     
        flash_test_1byte(flash_dev);
    } where i got in terminal as 
Children
Related