how to connnect external flash(w25qxx) to nrf52833dk board and validate that it the spi communication has been enabled

hi i'm new to developing nrf applications i have practised the sampled examples from https://academy.nordicsemi.com/courses/nrf-connect-sdk-fundamentals/ but i'm unable to connect using the spi interface. I am currently using nrf connect sdk v2.2.0. It would be really helpful if you can help me establish spi communication with my external flash. 

  • A good place to start is the zephyr\samples\drivers\spi_flash example. It's designed for a different board but the application looks in your device tree for spi_flash0. I'd create an overlay for spi_flash0 for your flash with your SPI configurations and go from there. 

  • tysm that would be really helpfull to understand. Can you also guide me as to what would be the pin out configuration to connect w25qxx to nrf52833dk or if not what to refer for pin out configuration. i am attaching a photo of my flash memory pin out. Also can you guide me as to what changes has to be made or not. 

     

  • Hello,

    Take a look at this old thread from my colleague which explains more on creating the overlay file and configuration you need to include.

    Kind Regards,

    Abhijith

  • i've looked at this thread it's building for nrf52822 but not for nrf52833. As mentioned in the thread i have enabled CONFIG_SPI_NOR=y. I'll attach the files and can you please help me out with this. 


  • main.c 

    /*
     * Copyright (c) 2016 Intel Corporation.
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <zephyr/kernel.h>
    #include <zephyr/drivers/flash.h>
    #include <zephyr/device.h>
    #include <zephyr/devicetree.h>
    #include <stdio.h>
    #include <string.h>
    
    #if defined(CONFIG_BOARD_ADAFRUIT_FEATHER_STM32F405)
    #define SPI_FLASH_TEST_REGION_OFFSET 0xf000
    #elif defined(CONFIG_BOARD_ARTY_A7_ARM_DESIGNSTART_M1) || \
    	defined(CONFIG_BOARD_ARTY_A7_ARM_DESIGNSTART_M3)
    /* The FPGA bitstream is stored in the lower 536 sectors of the flash. */
    #define SPI_FLASH_TEST_REGION_OFFSET \
    	DT_REG_SIZE(DT_NODE_BY_FIXED_PARTITION_LABEL(fpga_bitstream))
    #elif defined(CONFIG_BOARD_NPCX9M6F_EVB) || \
    	defined(CONFIG_BOARD_NPCX7M6FB_EVB)
    #define SPI_FLASH_TEST_REGION_OFFSET 0x7F000
    #else
    #define SPI_FLASH_TEST_REGION_OFFSET 0xff000
    #endif
    #define SPI_FLASH_SECTOR_SIZE        4096
    
    void main(void)
    {
    	const uint8_t expected[] = { 0x55, 0xaa, 0x66, 0x99 };
    	const size_t len = sizeof(expected);
    	uint8_t buf[sizeof(expected)];
    	const struct device *flash_dev;
    	int rc;
    
    	flash_dev = DEVICE_DT_GET(DT_ALIAS(spi_flash0));
    
    	if (!device_is_ready(flash_dev)) {
    		printk("%s: device not ready.\n", flash_dev->name);
    		return;
    	}
    
    	printf("\n%s SPI flash testing\n", flash_dev->name);
    	printf("==========================\n");
    
    
    	/* Write protection needs to be disabled before each write or
    	 * erase, since the flash component turns on write protection
    	 * automatically after completion of write and erase
    	 * operations.
    	 */
    	printf("\nTest 1: Flash erase\n");
    
    	/* Full flash erase if SPI_FLASH_TEST_REGION_OFFSET = 0 and
    	 * SPI_FLASH_SECTOR_SIZE = flash size
    	 */
    	rc = flash_erase(flash_dev, SPI_FLASH_TEST_REGION_OFFSET,
    			 SPI_FLASH_SECTOR_SIZE);
    	if (rc != 0) {
    		printf("Flash erase failed! %d\n", rc);
    	} else {
    		printf("Flash erase succeeded!\n");
    	}
    
    	printf("\nTest 2: Flash write\n");
    
    	printf("Attempting to write %zu bytes\n", len);
    	rc = flash_write(flash_dev, SPI_FLASH_TEST_REGION_OFFSET, expected, len);
    	if (rc != 0) {
    		printf("Flash write failed! %d\n", rc);
    		return;
    	}
    
    	memset(buf, 0, len);
    	rc = flash_read(flash_dev, SPI_FLASH_TEST_REGION_OFFSET, buf, len);
    	if (rc != 0) {
    		printf("Flash read failed! %d\n", rc);
    		return;
    	}
    
    	if (memcmp(expected, buf, len) == 0) {
    		printf("Data read matches data written. Good!!\n");
    	} else {
    		const uint8_t *wp = expected;
    		const uint8_t *rp = buf;
    		const uint8_t *rpe = rp + len;
    
    		printf("Data read does not match data written!!\n");
    		while (rp < rpe) {
    			printf("%08x wrote %02x read %02x %s\n",
    			       (uint32_t)(SPI_FLASH_TEST_REGION_OFFSET + (rp - buf)),
    			       *wp, *rp, (*rp == *wp) ? "match" : "MISMATCH");
    			++rp;
    			++wp;
    		}
    	}
    }
    

Related