I have my own board with the PIC32MX795F512L and I'm trying to connect it with an SPI EEPROM (M95128-DRE). I'm writing a byte (0x80) and reading it (after a 5ms delay) but always the reading result is 0xFF. Is there something am I doing wrong? The code:
while(true)
{
YB_posX = 0;
uint8_t ENA= 0b00000110; //ROM write-enable instruction
uint8_t INS1=0b00000010; //ROM write instruction
uint8_t add1=0x00; //address byte 1
uint8_t add2=0xee; //address byte 2
uint8_t INS2=0b00000011; //ROM read instruction
uint8_t INS3=0b00000101; //read status register
uint8_t DIS = 0b00000100; //write disable sequence
PLIB_SPI_BufferClear(SPI_ID_4);
LATEbits.LATE9= 0 ; // EEPROM Chip Select
PLIB_SPI_BufferWrite (SPI_ID_4,ENA); // Write Enable
LATEbits.LATE9= 1 ; // EEPROM Chip Select
LATEbits.LATE9= 0 ; // EEPROM Chip Select
PLIB_SPI_BufferWrite (SPI_ID_4,INS1);
PLIB_SPI_BufferWrite (SPI_ID_4,add1);
PLIB_SPI_BufferWrite (SPI_ID_4,add2);
PLIB_SPI_BufferWrite (SPI_ID_4,0x80); //the previous five instructions writed byte 0x50 to address(1+2)
delay_ms(1);
//<-----------------
LATEbits.LATE9=1;
delay_ms(5);
LATEbits.LATE9=0;
delay_ms(1);
PLIB_SPI_BufferWrite (SPI_ID_4,INS2); //reading instruction
PLIB_SPI_BufferWrite (SPI_ID_4,add1);
PLIB_SPI_BufferWrite (SPI_ID_4,add2);
PLIB_SPI_BufferWrite (SPI_ID_4,0x00); //this line is necessary since the ROM needs clock signal to clock out the data.
while(!PLIB_SPI_TransmitBufferIsEmpty(SPI_ID_4))
{}
YB_posX = PLIB_SPI_BufferRead(SPI_ID_4);
PLIB_SPI_ReceiverOverflowClear(SPI_ID_4);
delay_ms(1);
LATEbits.LATE9=1; //end of reading sequence
printNumI(YB_posX, 350, 235, 1, ' ');
delay_ms(1000);
}
Needless to mention that the SPI is initialised and opened at the start of main.
int main(void) {
/* Initialise all MPLAB Harmony modules, including application(s). */
SYS_Initialize(NULL);
DRV_SPI0_Initialize(); // SPI initialise
DRV_SPI_Open(SPI_ID_4,DRV_IO_INTENT_EXCLUSIVE );
I'm using MPLABX v.5.05, X32 v2.10 and Harmony Configurator, attached is the snapshot of the SPI settings. Any suggestion is welcomed.