SPI not working as expected

Hi,

    I have a simple SPI circuit to read data from a flash memory W25Q64 to be exact and I have difficult time reading the device address of the chip through SPI. 

    The situation is SPI works but just that the data received is wrong. 

     I tried a bunch of solutions like setting the Frequency to 1Mhz, 4MHz and 8 MHz and still the situation is the same. ( external spi flash memory read  as per this solution)

There are no pullup resistors connected as of now to the circuit.  

     The more weird part is if I remove the Vcc to the memory chip, I get the correct data that I expect to receive. 

     Please help me with the same. 

The code is as follows:

#include "app_error.h"
#include "boards.h"
#include "nrf_drv_spi.h"
#include "nrf_delay.h"

#define LEDBUTTON_LED BSP_BOARD_LED_2 

#define WB_JEDEC_ID           0x9F
#define ID_READ 4 // 4

#define SPI_CS 34
#define SPI_MISO 35
#define SPI_MOSI 36
#define SPI_SCK 37

#define APP_CFG_CHAR_LEN 20

static uint8_t m_char_value[APP_CFG_CHAR_LEN];
static const nrf_drv_spi_t m_spi = NRF_DRV_SPI_INSTANCE(0);

void spi_init() 
{
	ret_code_t err_code;
	nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
	// spi_config.ss_pin = SPI_CS;
    spi_config.ss_pin = NRF_DRV_SPI_PIN_NOT_USED;
	spi_config.miso_pin = SPI_MISO;
	spi_config.mosi_pin = SPI_MOSI;
	spi_config.sck_pin = SPI_SCK;
	    spi_config.frequency = NRF_DRV_SPI_FREQ_4M;
	    spi_config.bit_order = NRF_SPI_BIT_ORDER_MSB_FIRST;
	    spi_config.mode = NRF_SPI_MODE_0;
	err_code = nrf_drv_spi_init(&m_spi, &spi_config, NULL, NULL);
	APP_ERROR_CHECK(err_code);

    nrf_gpio_cfg_output(SPI_CS);
    nrf_gpio_pin_set(SPI_CS);
}

void read_id() 
{
	uint8_t reg_addr[4]={WB_JEDEC_ID, 0x00, 0x00, 0x00};
	uint8_t read_temp[ID_READ];
    nrf_gpio_pin_clear(SPI_CS);
	nrf_drv_spi_transfer(&m_spi, reg_addr, sizeof(reg_addr), read_temp, ID_READ);
    // nrf_drv_spi_transfer(&m_spi, reg_addr, 1, read_temp, ID_READ);
    nrf_gpio_pin_set(SPI_CS);
	for (int i = 1; i < ID_READ; i++) 
		m_char_value[i] = read_temp[i];
	m_char_value[ID_READ] = 0x44;
}

int main(void)
{
	int cnt = 0;
	bsp_board_init(BSP_INIT_LEDS);
	spi_init();

	// Main loop.
	for (;;)
	{
		read_id();
		if (cnt%2)
			bsp_board_led_on(LEDBUTTON_LED);
		else
			bsp_board_led_off(LEDBUTTON_LED);
		nrf_delay_ms(25);
		cnt ++;
	}
}

Related