I am trying to write to external flash memory using NRF52840. The flash uses spi protcol. My code is getting stuck in spi_xfer function. Error code is not in scope for the particular function so cant determine that. Attached hereis the code that i am running. I have already changed interrupt priority to 7 so that it dose not clash with SAADC. But that dosent seem to help. The code is getting stuck at line 254 (image) of file nrf_spi.h

#include "nrf_drv_spi.h"
#include "app_util_platform.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "boards.h"
#include "app_error.h"
#include <string.h>
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "FlashExt.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#define SPI_INSTANCE 1 /**< SPI instance index. */
static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE); /**< SPI instance. */
static volatile bool spi_xfer_done; /**< Flag used to indicate that SPI instance completed the transfer. */
#define TEST_STRING "Nordic"
static uint8_t m_tx_buf[]= TEST_STRING;
static uint8_t m_rx_buf[sizeof(TEST_STRING) + 1]; /**< RX buffer. */
static const uint8_t m_length = 1; /**< Transfer length. */
extern uint32_t System_Clock_ms;
bool Flash_Busy_Flag=false,Write_Enabled_Flag=false;
uint8_t tx_buf_flash[2]={0},rx_buf_flash[2]={0},para1;
uint64_t prev_ms;
void spi_event_handler(nrf_drv_spi_evt_t const * p_event,
void * p_context)
{
spi_xfer_done = true;
NRF_LOG_INFO("Transfer completed.");
if (m_rx_buf[0] != 0)
{
NRF_LOG_INFO(" Received:");
NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
}
}
void SPI_Setup(void)
{
nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
spi_config.ss_pin = SPI_SS_PIN;
spi_config.miso_pin = SPI_MISO_PIN;
spi_config.mosi_pin = SPI_MOSI_PIN;
spi_config.sck_pin = SPI_SCK_PIN;
APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
}
uint8_t SPI_Transfer(uint8_t Data)
{
m_tx_buf[0]= Data;
memset(m_rx_buf, 0, m_length);
spi_xfer_done = false;
APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
return *m_rx_buf;
}
uint8_t SPI_Transfer_Read(void)
{
memset(m_tx_buf, 0, m_length);
memset(m_rx_buf, 0, m_length);
spi_xfer_done = false;
APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
return *m_rx_buf;
}
uint8_t Flash_Stat(void)
{ Pull_CS_Low(); //check status register
SPI_Transfer(RDSR);
Pull_CS_High();
uint8_t s=SPI_Transfer_Read();
// printBits(s);
return s;
}
// use between each communication to make sure S25FLxx is ready to go.
void Flash_Waitforit()
{
uint8_t s=Flash_Stat();
while ((s&0x01)==0x01) //check if WIP bit is 1
{
if ((System_Clock_ms-prev_ms)>1000)
{
prev_ms=System_Clock_ms;
Flash_Busy_Flag=true;
}
Flash_Busy_Flag=false;
s=Flash_Stat();
}
}
void Flash_Write_Enable()
{
SPI_Transfer(WREN);
Flash_Waitforit();
Write_Enabled_Flag=true;
}
void Pull_CS_High(void)
{
nrf_gpio_pin_write(SPI_SS_PIN,1);
}
void Pull_CS_Low(void)
{
nrf_gpio_pin_write(SPI_SS_PIN,0);
}
void Flash_Read(unsigned long loc, uint8_t* array, unsigned long length)
{
SPI_Transfer(FLASH_READ); //control byte follow by location bytes
SPI_Transfer(loc>>16); // convert the location integer to 3 bytes
SPI_Transfer(loc>>8);
SPI_Transfer(loc & 0xff);
for (int i=0; i<length+1;i++){
array[i]=SPI_Transfer_Read(); //send the data
para1=array[0];
}
}
void Flash_Erase_4k(unsigned long loc)
{
Flash_Waitforit();
Flash_Write_Enable();
SPI_Transfer(0x20);
SPI_Transfer(loc>>16);
SPI_Transfer(loc>>8);
SPI_Transfer(loc & 0xFF);
Flash_Waitforit();
}
void Flash_Erase_64k(unsigned long loc)
{
Flash_Waitforit();
Flash_Write_Enable();
SPI_Transfer(0x20);
SPI_Transfer(loc>>16);
SPI_Transfer(loc>>8);
SPI_Transfer(loc & 0xFF);
Flash_Waitforit();
}
void Flash_Erase_All(void)
{
Flash_Waitforit();
Flash_Write_Enable();
SPI_Transfer(CE);
Flash_Waitforit();
}
void Flash_Write_Reg(uint8_t w)
{
SPI_Transfer(WRSR);
SPI_Transfer(w);
}
void Flash_Write(unsigned long loc, uint8_t* array, unsigned long length)
{
if (length>255)
{
unsigned long reps=length>>8;
unsigned long length1;
unsigned long array_count;
unsigned long first_length;
unsigned remainer0=length-(256*reps);
unsigned long locb=loc;
for (int i=0; i<(reps+2);i++)
{
if (i==0)
{
length1=256-(locb & 0xff);
first_length=length1;
if (length1==0){i++;}
array_count=0;
}
if (i>0 && i<(reps+1))
{
locb= first_length+loc+(256*(i-1));;
array_count=first_length+(256*(i-1));
length1=255;
}
if (i==(reps+1))
{
locb+=(256);
array_count+=256;
length1=remainer0;
if (remainer0==0){break;}
}
Flash_Write_Enable();
Flash_Waitforit();
SPI_Transfer(PP);
SPI_Transfer(locb>>16);
SPI_Transfer(locb>>8);
SPI_Transfer(locb & 0xff);
for (unsigned long i=array_count; i<(length1+array_count+1) ; i++)
{
SPI_Transfer(array[i]);
}
Flash_Waitforit();
}
}
if (length<=255)
{
if (((loc & 0xff)!=0) | ((loc & 0xff)<length))
{
uint8_t remainder = loc & 0xff;
uint8_t length1 =256-remainder;
uint8_t length2 = length-length1;
unsigned long page1_loc = loc;
unsigned long page2_loc = loc+length1;
Flash_Write_Enable();
Flash_Waitforit();
SPI_Transfer(PP);
SPI_Transfer(page1_loc>>16);
SPI_Transfer(page1_loc>>8);
SPI_Transfer(page1_loc & 0xff);
for (int i=0; i<length1;i++)
{
SPI_Transfer(array[i]);
}
Flash_Waitforit();
Flash_Write_Enable();
Flash_Waitforit();
SPI_Transfer(PP);
SPI_Transfer(page2_loc>>16);
SPI_Transfer(page2_loc>>8);
SPI_Transfer(page2_loc & 0xff);
for (int i=length1; i<length+1;i++)
{
SPI_Transfer(array[i]);
}
Flash_Waitforit();
}
else
{
Flash_Write_Enable(); // Must be done before writing can commence. Erase clears it.
Flash_Waitforit();
SPI_Transfer(PP);
SPI_Transfer(loc>>16);
SPI_Transfer(loc>>8);
SPI_Transfer(loc & 0xff);
for (int i=0; i<length+1;i++){
SPI_Transfer(array[i]);
}
Flash_Waitforit();
}
}
}
void Flash_Store(uint8_t Parameter, uint32_t location)
{
Flash_Waitforit();
Flash_Erase_4k(location);
Flash_Waitforit();
tx_buf_flash[0] = (Parameter)/256;
tx_buf_flash[1] =(int)((Parameter)%256);
Flash_Write(location,tx_buf_flash,2);
}