Hi,
I have a SPI Peripheral connected with nrf52832 EVK to check working of SPI interface.
I am writing a 10 bytes of data to a specific address and read back from specific address. (loopback)
But this is not working fine. I am receiving some junk values.
Kindly help me to solve the above problem.
Hardware: nrf52832 EVK with SPI IC (W25Q80DVSNIG)
Software used : nRF5_SDK_16.0.0_98a08e2\examples\ble_peripheral\ble_app_uart\pca10040\s132\ses
SPI Configuration as shown in below:
PIN numbers:
#define SPI_SS_PIN 31
#define SPI_MISO_PIN 30
#define SPI_MOSI_PIN 29
#define SPI_SCK_PIN 26
Instance is 2(SPI2)
sdk_config.h is attachedsdk_config.h
Code: attached
Output: Junk values ( Attached output with waveform)
#define SPI_INSTANCE 2/**< 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. */
uint8_t Write_Data[20] = {'\0'};
uint8_t Read_Data[20] = {'\0'};
uint32_t SPI_Write_Address = 0;
uint8_t SPI_Write_Length = 0;
uint8_t SPI_Read_Length = 0;
uint32_t Spi_Flash_write_address=0;
uint8_t Tx_Buffer[50];
volatile uint8_t spi_write_data[15]= {'\0'};
volatile uint8_t spi_read_data[15]= {'\0'};
uint8_t read_spiflash_buffer[50]={0};
uint32_t Flash_Device_ID = 0;
volatile uint8_t Flag_Off = 0;
uint8_t Length_Ble_Received;
uint8_t SPI_Rx_Buffer[1024] = {0};
int main()
{
spi_init();
Spi_flash_powerUp();
nrf_delay_ms(10);
SPI_Flash_Erase_Sector(0xF000);
nrf_delay_ms(1000);
Write_Data[0]=0x80;
Write_Data[1]=0xFF;
Write_Data[2]=0xF0;
Write_Data[3]=0x80;
Write_Data[4]=0x02;
Write_Data[5]=0xff;
Write_Data[6]=0x00;
Write_Data[7]=0xff;
Write_Data[8]=0xff;
Write_Data[9]=0x00;
for(i=0;i<10;i++)
{
SPI_Flash_Write_Data((0xF000+i),&Write_Data[i],1);
nrf_delay_ms(100);
}
while(1)
{
SPI_Flash_Read_Data((0xF000),&spi_read_data,12);
nrf_delay_ms(2000);
}
}
void Spi_flash_powerUp()
{
uint8_t Tx_Buffer[4] = {0};
uint8_t RxBuffer[4]; // Receive buffer
Tx_Buffer[0]=0xAB; // Command 0xAB = "Release from power down"
nrf_drv_spi_transfer(&spi,Tx_Buffer, 1, SPI_Rx_Buffer, 0);
}
uint8_t readStatusRegister1()
{
uint8_t Tx_Buffer[4] = {0};
Tx_Buffer[0] = 0x05;
memset(SPI_Rx_Buffer, 0, sizeof(SPI_Rx_Buffer));
nrf_drv_spi_transfer(&spi,Tx_Buffer, 2, SPI_Rx_Buffer, 1);
return SPI_Rx_Buffer[1];
}
void SPI_Flash_Erase_Sector(uint32_t Address)
{
uint8_t Tx_Buffer[4] = {0};
char Retry_Cntr = 0;
SPI_Flash_Write_Enable();
Tx_Buffer[0] = 0x20;
Tx_Buffer[1] = (Address >> 16) & 0xff;
Tx_Buffer[2] = (Address >> 8) & 0xff;
Tx_Buffer[3] = Address & 0xff;
nrf_drv_spi_transfer(&spi,Tx_Buffer, 4, SPI_Rx_Buffer, 3);
nrf_delay_ms(10);
Retry_Cntr = 10;
while(Retry_Cntr)
{
Retry_Cntr = readStatusRegister1(); //&& (Retry_Cntr < 10))
Retry_Cntr = Retry_Cntr & 0x01;
}
nrf_delay_ms(5);
}
void SPI_Flash_Write_Enable()
{
uint8_t Tx_Buffer[1] = {0x06};
memset(SPI_Rx_Buffer, 0, sizeof(SPI_Rx_Buffer));
nrf_drv_spi_transfer(&spi,Tx_Buffer, 1, SPI_Rx_Buffer, 0);
nrf_delay_ms(10);
}
void SPI_Flash_Write_Data(uint32_t Address, uint8_t *ByteArray, uint16_t Length)
{
//uint8_t Tx_Buffer[Length+4];
uint8_t Retry_Cntr = 1;
uint16_t temp = 0;
SPI_Flash_Write_Enable();
Tx_Buffer[0] = 0x02;
Tx_Buffer[1] = (Address >> 16) & 0xff;
Tx_Buffer[2] = (Address >> 8) & 0xff;
Tx_Buffer[3] = Address & 0xff;
for(temp = 0; temp < Length; temp++)
{
Tx_Buffer[4+temp] = ByteArray[temp];
}
nrf_drv_spi_transfer(&spi,Tx_Buffer, (Length+4), SPI_Rx_Buffer, (Length+3));
nrf_delay_ms(10);
Retry_Cntr = 1;
while(Retry_Cntr)
{
Retry_Cntr = readStatusRegister1(); //&& (Retry_Cntr < 10))
Retry_Cntr = Retry_Cntr & 0x01;
}
nrf_delay_ms(5);
}
void SPI_Flash_Read_Data(uint32_t Address, uint8_t *ByteArray, uint16_t Length)
{
uint8_t Tx_Buffer[Length+4];
char Retry_Cntr = 0;
uint16_t temp = 0;
//SPI_Flash_Write_Enable();
Tx_Buffer[0] = 0x03;
Tx_Buffer[1] = (Address >> 16) & 0xff;
Tx_Buffer[2] = (Address >> 8) & 0xff;
Tx_Buffer[3] = Address & 0xff;
nrf_drv_spi_transfer(&spi,Tx_Buffer, (Length+4), ByteArray, (Length+3));
//while(readStatusRegister1() & 1);
}
void spi_init (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,NULL,NULL));// spi_event_handler, NULL));
}
#define NRF_DRV_SPI_DEFAULT_CONFIG \
{ \
.sck_pin = NRF_DRV_SPI_PIN_NOT_USED, \
.mosi_pin = NRF_DRV_SPI_PIN_NOT_USED, \
.miso_pin = NRF_DRV_SPI_PIN_NOT_USED, \
.ss_pin = NRF_DRV_SPI_PIN_NOT_USED, \
.irq_priority = SPI_DEFAULT_CONFIG_IRQ_PRIORITY, \
.orc = 0xFF, \
.frequency = NRF_SPI_FREQ_125K, \
.mode = NRF_DRV_SPI_MODE_0, \
.bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST, \
}
Regards,
Meghana