SPI with EEPROM in MPC5744

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SPI with EEPROM in MPC5744

3,976 Views
ramanjineyuluth
Contributor II

Hi,

I am currently working on SPI with EEPROM but i am unable to write data to EEPROM.

Please see the below code and let me know any modifications is needed.

 

int8_t SpiEepWritePage(const EEP_FUNC_ADDR func_addr, const uint8_t * const content)

{

uint16_t phy_addr;

uint32_t i;

 

phy_addr = SpiEepConvert2PhyAddr(func_addr);//func_addr is structure with page number and offset

 

ASSERT_WR_ENABLE();

 

SpiEepSelectSlave(SIG_ASSERTED);//CS=0

SPIIfTransferByte(Eep_Spi_Chanl_No, EEP_INSTRUCT_WRITE);

SPIIfTransferByte(Eep_Spi_Chanl_No, ADDR_HIGH(phy_addr));

SPIIfTransferByte(Eep_Spi_Chanl_No, ADDR_LOW(phy_addr));

for (i = 0; i < EEP_PAGE_SIZE; i++)//128Bytes

{

SPIIfTransferByte(Eep_Spi_Chanl_No, (uint8_t)(*(uint8_t *)(content + i)));

}

SpiEepSelectSlave(SIG_NEGATED);//CS=1

 

/* Wait for completion of previous write cycle */

while (((EEP_STAT_REG)(SpiEepReadStatReg())).B.WIP);

 

ASSERT_WR_DISABLE();

 

return 1;

}

In the above code we are transferring data to DSPI2.

 

Can you please help on this.

 

Thanks in advance.

 

Thanks & Regards,

Ramanjineyulu.

Labels (1)
14 Replies

2,340 Views
PetrS
NXP TechSupport
NXP TechSupport

Hi,

 

The 5th line in SPIIfTransferByte() function looks strange…

 

Sorry, what kind of help do you expect if we have no idea what is inside your functions/macros.

 

You can refer to this post; https://community.nxp.com/thread/434481#comment-832325

 

All needed code is given there, except of SPI pin init. You can simply reuse it on your MCU.

 

Function Write_Byte(uint16_t cmd, uint16_t data) combines “cmd” and “data” into 32bit word and places it into PUSH register. Read device RM for the PUSHR register bit description.  

 

BR, Petr

2,340 Views
ramanjineyuluth
Contributor II

Hi Petr,

Thanks for your reply.

Please see the below code how am transferring the data.

uint8_t SPIIfTransferByte(const uint8_t channel_no, const uint8_t data)

{

        int data_ret;

        data_ret = DSPIMasterTransferByte(channel_no, data);

        return data_ret;

}

uint8_t DSPIMasterTransferByte(const uint8_t channel_no, const uint8_t ch)

{

SPI_Channel[channel_no]->PUSHR.PUSHR.R = (uint32_t)DSPI_PUSHR_TXDATA(ch);

/* Wait until TCF=0 in Status Register */

while(1!=(SPI_Channel[channel_no]->SR.B.TCF));

/* Clear TCF=1 in Status Register */

SPI_Channel[channel_no]->SR.B.TCF=1;

return (uint8_t)SPI_Channel[channel_no]->POPR.R;

}

In POP register always getting 0XFF data.

If you send any data it displays only 0XFF.

Please see the above functions and let me know your comments.

Please help me why POP register is displaying only 0XFF.

Thanks,

Ram.

0 Kudos

2,340 Views
ramanjineyuluth
Contributor II

Hi Petr,

The above code is while loop is continues true. It is not come out of the loop.

Please let me know below code is correct or not.

while (((EEP_STAT_REG)(SpiEepReadStatReg())).B.WIP);

uint8_t EEPROMReadByte(uint32_t Address, uint8_t *pReadData, uint8_t DataSize)

Please tell me which Address you are passing. Please tell me it is very help full for me.

Thanks & Regards,

Ramanjineyulu.

0 Kudos

2,340 Views
PetrS
NXP TechSupport
NXP TechSupport

Hi,

What is your EEPROM in fact? Do you have part number or datasheet? What are the commands sequence?

Have you select proper SPI mode and timing?

 

In your DSPIMasterTransferByte() what does the DSPI_PUSHR_TXDATA do? Is it putting just byte into PUSHR or whole 32bit (command+data)?

Try to check RFDF flag instead of TCF and finaly use full register access to clear flags.

 

Disconnect the EEPROM and try to send single byte with MISO connected to ground and then to VCC. Read POPR, if it is same still you have wrong reading from POPR or MISO pin incorrectly configured.

 

Function EEPROMReadByte(uint32_t Address, uint8_t *pReadData, uint8_t DataSize) should read “DataSize” number of bytes from user defined Address. Look at EEPROM datasheet for read command for more info.

BR, Petr

0 Kudos

2,340 Views
ramanjineyuluth
Contributor II

Hi Petr,

Thanks a lot for your support.

I am using M95512 EEPROM chip.

Please see the below commands am using.

#define EEP_INSTRUCT_WREN (0x06)

#define EEP_INSTRUCT_WRDI (0x04)

#define EEP_INSTRUCT_RDSR (0x05)

#define EEP_INSTRUCT_WRSR (0x01)

#define EEP_INSTRUCT_READ (0x03)

#define EEP_INSTRUCT_WRITE (0x02)

#define EEP_INSTRUCT_RDID (0x83)

#define EEP_INSTRUCT_WRID (0x82)

#define EEP_INSTRUCT_RDLS (0x06)

#define EEP_INSTRUCT_LID (0x06)

command.png

#define DSPI_PUSHR_TXDATA(x) (((x)&0xFFFF)<<0) //16bit data only not 32bit

DSPI2 initialization & DSPI master configuration function.

static void DSPIChannel2Init(uint8_t dev_mode)

{

if (DSPI_MASTER == dev_mode)

{

//SIULDigitalIO(3, SIUL_OUTPUT); // DSPI2_SC0

SIULDigitalIO(3, 0x32840002uL);

SIULDigitalOutput(0, 0x32840002uL); //DSPI2_SCK

}

else

{

SIULDigitalIO(3, SIUL_INPUT); // DSPI2_SC0

SIULDigitalIO(0, SIUL_INPUT); //DSPI2_SCK

}

SIULDigitalOutput(1, 0x32840002uL); //DSPI2_SOUT

SIULDigitalInput(2, 47, 0x308C0000uL, 0x00000002uL); //DSPI2_SIN

MC_ME.PCTL209.B.RUN_CFG=0; /* DSPI_2 Peripheral configuration select for run modes */

MC_ME.PS6.B.S_DSPI_2=1; /* DSPI2 is enable in Peripheral Status Register 3 */

}

static void DSPICofigMasterDefault(uint8_t channel_no) //Here channel number is 2(SPI2)

{

/* Module Configuration Register SPIx configured as

* @ Master MSTR=1

* @ The inactive state of PCS0 is high PCSIS=1

* @ Stopped mode HALT=1

*/

SPI_Channel[channel_no]->MCR.R =0x80010001;

/* @ TX FIFO is disabled DIS_TXF=1

* @ RX FIFO is disabled DIS_RXF=1

*

* **Note: This bit can be written only when the MDIS bit is cleared

*/

SPI_Channel[channel_no]->MCR.R |= (0x3 << 0x0C);

/* Configure transfer attributes in CTAR0 (156Kbps)

* @ DBR = 0

* @ FMSZ = 0x7

* @ CPOL = 0

* @ CPHA = 0

* @ PCSSCK = 0

* @ PASC = 0

* @ PDT = 2

* @ PBR = 2

* @ CSSCK=7

* @ ASC =7

* @ DT =2

* @ BR = 7

*/

SPI_Channel[channel_no]->MODE.CTAR[0].R = 0x38021007;

}

Please let me know any modifications is required in above functions.

Please help me on this.

Thanks,

Ramanjineyulu.

0 Kudos

2,340 Views
ramanjineyuluth
Contributor II

Hi Petr,

Please see the below code also.

while (((EEP_STAT_REG)(SpiEepReadStatReg())).B.WIP);

uint8_t SpiEepReadStatReg(void)

{

uint8_t status;

SpiEepSelectSlave(SIG_ASSERTED);//CS0 pad is LOW

SPIIfTransferByte(Eep_Spi_Chanl_No, EEP_INSTRUCT_RDSR);//EEP_INSTRUCT_RDSR = 0x05

status = SPIIfTransferByte(Eep_Spi_Chanl_No, 0x0);//Eep_Spi_Chanl_No is 2(SPI2)

SpiEepSelectSlave(SIG_NEGATED);//CS0 pad is high

return status;

}

In the above function always getting 0xff in status variable.

Please let me know any modification is needed in the above function.

Thanks & Regards,

Ramanjineyulu.

0 Kudos

2,340 Views
PetrS
NXP TechSupport
NXP TechSupport

try to use attached code ...

BR, Petr

0 Kudos

2,340 Views
swathireddy
Contributor I

Hi Petr,

Thanks for your support.

Ramanji & me tested your code.

It's not working for us.

We are unable to read data from EEPROM.

"rxData" buffer contains 0x00 not actual data.

Did you tested the code? Is it working for you?

If it is Hardware issue then how to findout the issue.

How can we test the hardware?

We just added some lines of code. We are just toggling LED when received data matches with transmitting data.

Please see the below code

int main (void)

{

uint8_t status;

uint8_t i,j;

uint32_t matches = 0uL;

uint32_t timeout;

SysClk_Init();

InitINTC();

SUIL2_Init();

SPI_Init();

EEPROMWriteEnable();

EEPROMWriteBytes(0,&txData[0],6);

status=EEPROMReadRDSR();

EEPROMReadBytes(0,&rxData[0],6);

for (i = 0; i < 6; i++)

{

if (txData[i] == rxData[i])

{

matches++;

if (matches >= 6)

{

for (j = 0; j < 5; j++)

{

SetPadLow(92);

/* Signal 3 for WEL set */

timeout = 10000uL;

while ((--timeout) != 0uL);

SetPadHigh(92);

}

}

rxData[i] = 0;

}

}

while(1)

{

}

}

2,340 Views
PetrS
NXP TechSupport
NXP TechSupport

I have just tested if the transmitted bytes/commands are correct. I do not have SPI EEPROM memory.

So use the functions properly. After writing data into memory you should wait until memory is ready again. So you should read RDSR until WIP is cleared, otherwise other command (read) is not accepted.

 

Can you share scope trace off all 4 SPI signals?  

BR, Petr

0 Kudos

2,340 Views
ramanjineyuluth
Contributor II

Hi Petr,

SPI with EEPROM code is working fine now.

The issue is resolved by connecting the HOLD pin to VCC with 10K, and the code is working fine.

Thanks a lot for your support.

Thanks & Regards,

Ramanjineyulu. 

0 Kudos

2,340 Views
ramanjineyuluth
Contributor II

Hi Petr,

I have a doubt about PUSHR register.

In source code we are always transferring data with command(0x8001) means CONT bit is set. Instead of setting CONT bit am setting EOQ bit, at that time it won't come out of below while loop function.

while (((EEP_STAT_REG)(SpiEepReadStatReg())).B.WIP);

Is there specific code to use EOQ bit instead of CONT bit.

Each and every byte data transfer I have to use EOQ bit instead of CONT bit.

Could you please help on this.

Thanks & Regards,

Ramanjineyulu.

0 Kudos

2,340 Views
xushengyu
Contributor II

Can you send your resource code to me? I am using the M95512 now. Thank you in advance.

0 Kudos

2,340 Views
ramanjineyuluth
Contributor II

Hi Petr,

I have tested Hardware side, The CS0 bit is always high.

Please see the below code to initialize the SPI2.

void SUIL2_Init(void)
{

    SIUL2.MSCR[3].R = 0x32870002; // A[3] pin as SPI_0 CS0 output
    SIUL2.MSCR[0].R = 0x32840002; // A[0] pin as SPI_0 SCK output
    SIUL2.MSCR[1].R = 0x32840002; // A[1] pin as SPI_0 SOUT output

    SIUL2.MSCR[2].B.IBE = 1;    // A[2]: Enable pad for input - SPI_0 SIN
    SIUL2.IMCR[47].B.SSS = 2;   // SPI_0 SIN: connected to pad C[7]
}   

while(1)
    {
     SetPadLow(92);
     SetPadLow(3);

     timeout = 100000000uL;
     while ((--timeout) != 0uL);

     SetPadHigh(92);
     SetPadHigh(3);

     timeout = 100000000uL;
     while ((--timeout) != 0uL);
    }

Please look at the Hardware schematic and let me know hardware is perfect or not.

Hardware.jpg

Thanks & Regards,

Ramanjineyulu.

0 Kudos

2,340 Views
PetrS
NXP TechSupport
NXP TechSupport

If the SetPadLow(3) just writes to SIU.GPDO[3] register then nothing can change on CS0 as you configure it to be driven by SPI module by SIUL2.MSCR[3].R = 0x32870002;

 

Be sure both /W and /HOLD pins of memory are high during write/read operation. I see /HOLD is floating, which could cause issue, if there is no internal pull up within U23.

BR, Petr

0 Kudos