I have recently started a project with using EEPROM chip (25LC040P) and MSP430F6638.
The thing is that I can't understand the read/write sequence for this chip. I have tried to write code using some examples and the official spec ( http://ww1.microchip.com/downloads/en/DeviceDoc/21204d.pdf ), but I don't know what's wrong with it:
// -----------------------------------------------
void spi_eeprom_select(void)
{
P8OUT &= ~BIT_CS;
__delay_cycles(100);
}
// -----------------------------------------------
void spi_eeprom_release(void)
{
P8OUT |= BIT_CS;
__delay_cycles(100);
}
// -----------------------------------------------
unsigned char spi_eeprom_exchg(unsigned char cDataOut)
{
unsigned char cDataIn = 0;
while (!(UCB1IFG & UCTXIFG)); // wait for USCI_B1 TX buffer ready
UCB1TXBUF = cDataOut; // transmit the first character
while (!(UCB1IFG & UCRXIFG)); // wait for USCI_B1 RX buffer ready
cDataIn = UCB1RXBUF; // receive the first character
return cDataIn;
}
// -----------------------------------------------
void spi_eeprom_WREN(void)
{
spi_eeprom_select();
spi_eeprom_exchg( CMD_WREN );
spi_eeprom_release();
}
// -----------------------------------------------
void spi_eeprom_WRDI(void)
{
spi_eeprom_select();
spi_eeprom_exchg( CMD_WRDI );
spi_eeprom_release();
}
// -----------------------------------------------
unsigned char spi_eeprom_RDSR(void)
{
unsigned int iResult;
spi_eeprom_select();
spi_eeprom_exchg( CMD_RDSR );
iResult = spi_eeprom_exchg( 0 );
spi_eeprom_release();
return iResult;
}
// -----------------------------------------------
void spi_eeprom_WRSR(unsigned char cStatus)
{
spi_eeprom_select();
spi_eeprom_exchg( CMD_WRSR );
spi_eeprom_exchg( cStatus );
spi_eeprom_release();
}
// -----------------------------------------------
unsigned char spi_eeprom_bwrite(unsigned int iAddr, unsigned char cDataOut)
{
spi_eeprom_WREN();
// perform write operation
spi_eeprom_select();
spi_eeprom_exchg( CMD_WRITE );
spi_eeprom_exchg( iAddr & 0x00FF );
spi_eeprom_exchg( cDataOut );
spi_eeprom_release();
return spi_eeprom_RDSR();
}
// -----------------------------------------------
unsigned char spi_eeprom_bread(unsigned int iAddr)
{
unsigned int iResult;
spi_eeprom_select();
spi_eeprom_exchg( CMD_READ );
spi_eeprom_exchg( iAddr & 0x00FF );
iResult = spi_eeprom_exchg( 0 );
spi_eeprom_release();
return iResult;
}
// -----------------------------------------------
Could anyone review the command sequence for that chip?
And, by the way, somebody provide a working example how to work with 25LC040P on MSP430F6638 with SYS/BIOS?