User manual

LPCXpresso Experiment Kit - User’s Guide
Page 85
Copyright 2013 © Embedded Artists AB
/* RDSR status bit definition */
#define RDSR_RDY 0x01
#define RDSR_WEN 0x02
#define SSEL_GPIO_8_PORT PORT2
#define SSEL_GPIO_8_PIN 0
#define SSEL_HIGH 1
#define SSEL_LOW 0
/*****************************************************************************
** Function name: spiE2PROMread
** Descriptions: This function will read bytes from the SPI E2PROM
** parameters: address in memory region, buffer pointer and block length
** Returned value: None
**
*****************************************************************************/
void spiE2PROMread( uint16_t address, uint8_t *pBuf, uint32_t length )
{
uint8_t buf[3];
//pull SSEL/CS low
GPIOSetValue(SSEL_GPIO_8_PORT, SSEL_GPIO_8_PIN, SSEL_LOW);
//output read command and address
buf[0] = INST_READ;
buf[1] = (address >> 8) & 0xff;
buf[2] = address & 0xff;
SSP0Send(&buf[0], 3);
//read bytes from E2PROM
SSP0Receive(pBuf, length);
//pull SSEL/CS high
GPIOSetValue(SSEL_GPIO_8_PORT, SSEL_GPIO_8_PIN, SSEL_HIGH);
}
/*****************************************************************************
** Function name: spiE2PROMwrite
** Descriptions: This function will write bytes to the SPI E2PROM
** parameters: address in memory region, buffer pointer and block length
** Returned value: None
**
*****************************************************************************/
void spiE2PROMwrite( uint16_t address, uint8_t *pBuf, uint32_t length )
{
uint8_t buf[3];
//Insert code here to break up large write operation into several
//page write operations...
//Do not forget to add a 5ms delay after each page write operation!
//pull SSEL/CS low
GPIOSetValue(SSEL_GPIO_8_PORT, SSEL_GPIO_8_PIN, SSEL_LOW);
//output write command and address
buf[0] = INST_WRITE;
buf[1] = (address >> 8) & 0xff;
buf[2] = address & 0xff;
SSP0Send(&buf[0], 3);
//send bytes to write E2PROM
SSP0Send(pBuf, length);
//pull SSEL/CS high
GPIOSetValue(SSEL_GPIO_8_PORT, SSEL_GPIO_8_PIN, SSEL_HIGH);
}
Add functionality in the write operation to check so that no page boundaries are crossed. Even better,
add functionality to break up a large write block to smaller, correctly addressed page writes. Do not
forget to add functionality in the write function to set the write enable latch before every write operation.