2013-11-05

MCP 25LC256 32 KByte SPI EEPROM basic testing OK




// Semihositing output

*** MCP25LC256 Test - 2013nov05hkt1528 ***

 Now writing 0x55 ...
 Now testing byte read ...
 Readback databyte = 55 

 Now writing byte 0xaa ...
 Now testing byte read ...
 Readback databyte = aa 



// ***********************************************************************

// eeprom050.h 2013nov05hkt1526
// ***********************************************************************

#include "spi050.h"

// *** EEPROM commands and constants ***
#define WRITE_STATUS_REGISTER 0x01
#define READ_STATUS_REGISTER  0x05
#define DISABLE_WRITE_LATCH   0x04
#define ENABLE_WRITE_LATCH    0x06
#define WRITE_DATA_REGISTER   0x02
#define READ_DATA_REGISTER    0x03

#define WRITE_IN_PROGRESS_BIT_MASK 0xfe

#define DONT_CARE_INSTRUCTION 0x00
#define DONT_CARE_ADDRESS     0x00
#define DONT_CARE_DATA        0x00
#define DONT_CARE             0x00

#define WRITE_PROTECT_NONE          0x00
#define WRITE_PROTECT_UPPER_QUARTER 0x04 // 6000h-7FFFh
#define WRITE_PROTECT_UPPER_HALF    0x08 // 4000h-7FFFh
#define WRITE_PROTECT_ALL           0x0c // 0000h-7FFFh

// *** EEPROM Addresses ***

#define START_ADDRESS         0x0000
#define START_ADDRESS_UPPER     0x00
#define START_ADDRESS_LWER      0x00

#define TEST_ADDRESS          0x0300
#define TEST_ADDRESS_UPPER      0x03
#define TEST_ADDRESS_LOWER      0x00

#define TEST_DATA_BYTE_55    0x55
#define TEST_DATA_BYTE_AA    0xaa

void eepromWriteLatch(uint8_t eepromCommand, uint8_t spiChannelNumber)
{
    SSP_DATA_SETUP_Type xferConfig;
uint8_t TxBuf[1];
uint8_t RxBuf[1];

    TxBuf[0] = eepromCommand;
RxBuf[0] = DONT_CARE;

xferConfig.tx_data = TxBuf;
xferConfig.rx_data = RxBuf;
xferConfig.length = 1;

SpiWriteRead050(xferConfig, spiChannelNumber);

/* For debug only
uint8_t i;
for (i = 0; 1 < 100000; i++)
{
SpiWriteRead050(xferConfig, spiChannelNumber);
delayMilliSecond(1);
}
*/

}

uint8_t eepromReadStatusRegister(uint8_t spiChannelNumber)
{
    SSP_DATA_SETUP_Type xferConfig;
uint8_t TxBuf[2];
uint8_t RxBuf[2];

uint8_t statusRegisterValue;

xferConfig.tx_data = TxBuf;
xferConfig.rx_data = RxBuf;
xferConfig.length = 2;

    TxBuf[0] = READ_STATUS_REGISTER;
TxBuf[1] = DONT_CARE;
    RxBuf[0] = DONT_CARE;
RxBuf[1] = DONT_CARE;

SpiWriteRead050(xferConfig, spiChannelNumber);

/* *** For debugging only ***
uint8_t i;
for (i = 0; 1 < 100000; i++)
{
SpiWriteRead050(xferConfig, spiChannelNumber);
delayMilliSecond(1);
}
*/

statusRegisterValue = RxBuf[1];
return statusRegisterValue;
}

void eepromByteWrite(uint8_t spiChannelNumber, uint8_t upperAddress, uint8_t lowerAddress, uint8_t dataByte)
{
    SSP_DATA_SETUP_Type xferConfig;
uint8_t TxBuf[4];
uint8_t RxBuf[4];
uint8_t statusRegisterValue;
uint8_t writeInProgressBit;

xferConfig.tx_data = TxBuf;
xferConfig.rx_data = RxBuf;
xferConfig.length = 4;

    TxBuf[0] = WRITE_DATA_REGISTER;
TxBuf[1] = upperAddress;
TxBuf[2] = lowerAddress;
TxBuf[3] = dataByte;

    RxBuf[0] = DONT_CARE;
RxBuf[1] = DONT_CARE;
RxBuf[2] = DONT_CARE;
RxBuf[3] = DONT_CARE;

SpiWriteRead050(xferConfig, spiChannelNumber);

// *** Wait for write to complete ***
// delayMilliSecond(5);

writeInProgressBit = 1;
while (writeInProgressBit == 1)
{
statusRegisterValue = eepromReadStatusRegister(spiChannelNumber);
    writeInProgressBit = statusRegisterValue & WRITE_IN_PROGRESS_BIT_MASK;
}

// *** For debugging only ***
/*
uint8_t i;
for (i = 0; 1 < 100000; i++)
{
SpiWriteRead050(xferConfig, spiChannelNumber);
delayMilliSecond(1);
}
*/
}

uint8_t eepromByteRead(uint8_t spiChannelNumber, uint8_t upperAddress, uint8_t lowerAddress)
{
    uint8_t readBackDataByte;
SSP_DATA_SETUP_Type xferConfig;
uint8_t TxBuf[4];
uint8_t RxBuf[4];

xferConfig.tx_data = TxBuf;
xferConfig.rx_data = RxBuf;
xferConfig.length = 4;

    TxBuf[0] = READ_DATA_REGISTER;
TxBuf[1] = upperAddress;
TxBuf[2] = lowerAddress;
TxBuf[3] = DONT_CARE;

    RxBuf[0] = DONT_CARE;
RxBuf[1] = DONT_CARE;
RxBuf[2] = DONT_CARE;
RxBuf[3] = DONT_CARE;

SpiWriteRead050(xferConfig, spiChannelNumber);

// *** For debugging only ***
/*
uint8_t i;
for (i = 0; 1 < 100000; i++)
{
SpiWriteRead050(xferConfig, spiChannelNumber);
delayMilliSecond(1);
}
*/
// delayMilliSecond(5);
    readBackDataByte = RxBuf[3];
    return readBackDataByte;
}

void testMcp25Lc256(uint8_t spiChannelNumber)
{
uint8_t statusRegisterValue;
uint8_t readBackDataByte;

printf("*** MCP25LC256 Test - 2013nov05hkt1528 ***\n\n");

setupSpi051(spiChannelNumber);

// eepromWriteLatch(ENABLE_WRITE_LATCH, spiChannelNumber);
// statusRegisterValue = eepromReadStatusRegister(spiChannelNumber);
// printf("\n Write Latch enabled Status Register value = %02x ", statusRegisterValue);

// eepromWriteLatch(DISABLE_WRITE_LATCH, spiChannelNumber);
// statusRegisterValue = eepromReadStatusRegister(spiChannelNumber);
// printf("\n Write Latch disabled Status Register value = %02x \n", statusRegisterValue);


printf("\n Now writing 0x55 ...\n");
eepromWriteLatch(ENABLE_WRITE_LATCH, spiChannelNumber);
eepromByteWrite(spiChannelNumber, TEST_ADDRESS_UPPER, TEST_ADDRESS_LOWER, TEST_DATA_BYTE_55);

printf("\n Now testing byte read ...\n");
readBackDataByte = eepromByteRead(spiChannelNumber, TEST_ADDRESS_UPPER, TEST_ADDRESS_LOWER);
printf("\n Readback databyte = %02x \n", readBackDataByte);

printf("\n Now writing byte 0xaa ...\n");
eepromWriteLatch(ENABLE_WRITE_LATCH, spiChannelNumber);
eepromByteWrite(spiChannelNumber, TEST_ADDRESS_UPPER, TEST_ADDRESS_LOWER, TEST_DATA_BYTE_AA);

printf("\n Now testing byte read ...\n");
readBackDataByte = eepromByteRead(spiChannelNumber, TEST_ADDRESS_UPPER, TEST_ADDRESS_LOWER);
printf("\n Readback databyte = %02x \n", readBackDataByte);
}

// ***********************************************************************
// End
// ***********************************************************************


 .END

No comments:

Post a Comment