hi everyone,
i am trying to connect a serial eeprom (through spi) to my controller, and i have written some code that looks good in my mind, but doesn't seem to be working. Has anyone out there had experience with the microchip 25lc010aa eeprom? I will show my code that i have written. I am trying to use the readstring and storestring routines to write a block of 8 bytes to the memory. any help appreciated
#include "QSPI_functions.h"
#define EEPROM_CS 0xB
#define STORE_ADDR 0x10
//these are our instructions to send to the eeprom for various operations
#define EEPROM_READ 0x03
#define EEPROM_WRITE 0x02
#define EEPROM_WRDI 0x04
#define EEPROM_WREN 0x06
#define EEPROM_RDSR 0x05
#define EEPROM_WRSR 0x01
//this function saves a single value to the EEPROM sitting on the SPI Bus
void storeByte(uint8 addr,uint8 value)
{
uint16 cs[5] = {EEPROM_CS,0xFF,EEPROM_CS,EEPROM_CS,EEPROM_CS};
uint16 send[] = {EEPROM_WREN,0x00,EEPROM_WRITE,0x00,0x00};
uint16 receive[5];
send[3] = addr;
send[4] = value;
QSPIInit8Bit();
while(!eepromReady()); ///make sure there is not a write in progress!
QSPITransferCommandAlt(cs,5);
QSPITransferData(send,receive,5);
}
//this function sends a block of data to the eeprom. data length is limited
//by the qspi controller to be 14 bytes or less, but this should not be a
//problem (hopefully won't need this much space, as it is!)
void storeString(uint8 addr,uint8* values,uint8 length)
{
uint8 i;
uint16 cs[16] = {EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS};
uint16 send[16] = {EEPROM_WREN},receive[16];
QSPIInit8Bit();
while(!eepromReady()); //wait for the mem to be ready
QSPITransferCommandAlt(cs,1); //set write mode
QSPITransferData(send,receive,1);
send[0] = EEPROM_WRITE;
send[1] = addr;
for(i=0 ; i
send[i+2] = values[i]; //copy our data to qspi array
QSPITransferCommandAlt(cs,length+2);
QSPITransferData(send,receive,length+2); //send our write command and data!
}
void readByte(uint8 addr,uint8* value)
{
uint16 cs[3] = {EEPROM_CS,EEPROM_CS,EEPROM_CS};
uint16 send[3] = {EEPROM_READ,0x00,0x00};
uint16 receive[3];
send[1] = addr;
QSPIInit8Bit();
QSPITransferCommandAlt(cs,3);
QSPITransferData(send,receive,3); //send instruction/read value!
*value = receive[2]; //return the value
}
//reads the whole block of data (well, 14 bytes...), and returns it
void readString(uint8 addr, uint8* values)
{
uint8 i;
uint16 cs[16] = {EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS,EEPROM_CS};
uint16 send[16] = {EEPROM_READ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},receive[16];
send[2] = addr;
QSPIInit8Bit();
QSPITransferCommandAlt(cs,16);
QSPITransferData(send,receive,16);
for(i=0 ; i14 ; i++)
values[i] = receive[i+2];
}
uint8 eepromReady()
{
uint16 cs[2] = {EEPROM_CS,EEPROM_CS};
uint16 send[3] = {EEPROM_RDSR,0x00},receive[3];
QSPIInit8Bit();
QSPITransferCommandAlt(cs,2);
QSPITransferData(send,receive,2);
return !(receive[1] & 0x01);
}
---------------------------and in another file (qspifunctions.h/.c):-----------
void QSPIInit(void)
{
/* Setting the QSPI pins for QSPI purposes (by default they are GPIO's)*/
// MCF_GPIO_PQSPAR = 0x15;
MCF_GPIO_PQSPAR = 0x1555; ///get all QSPI Pins as QSPI Functions
MCF_QSPI_QMR = (MCF_QSPI_QMR_MSTR
| MCF_QSPI_QMR_BITS_16
| MCF_QSPI_QMR_CPHA
//| MCF_QSPI_QMR_BITS_8
| MCF_QSPI_QMR_BAUD(10));
//^^ settings:master,16 bit xfers,falling edge data clock,4MHz clock
MCF_QSPI_QDLYR = (MCF_QSPI_QDLYR_QCD(1) | MCF_QSPI_QDLYR_DTL(1));
MCF_QSPI_QIR = (MCF_QSPI_QIR_WCEFB | MCF_QSPI_QIR_ABRTB |
MCF_QSPI_QIR_ABRTL | MCF_QSPI_QIR_WCEF |
MCF_QSPI_QIR_ABRT | MCF_QSPI_QIR_SPIF);
MCF_QSPI_QWR |= MCF_QSPI_QWR_CSIV; //active low chip selects
return;
}
void QSPIInit8Bit(void)
{
MCF_GPIO_PQSPAR = 0x1555;
MCF_QSPI_QMR = (MCF_QSPI_QMR_MSTR
| MCF_QSPI_QMR_BITS(8)
| MCF_QSPI_QMR_CPHA
| MCF_QSPI_QMR_BAUD(10));
MCF_QSPI_QDLYR = (MCF_QSPI_QDLYR_QCD(1) | MCF_QSPI_QDLYR_DTL(1));
MCF_QSPI_QIR = (MCF_QSPI_QIR_WCEFB | MCF_QSPI_QIR_ABRTB |
MCF_QSPI_QIR_ABRTL | MCF_QSPI_QIR_WCEF |
MCF_QSPI_QIR_ABRT | MCF_QSPI_QIR_SPIF);
MCF_QSPI_QWR |= MCF_QSPI_QWR_CSIV; //active low chip selects
return;
}
/*
* In each command buffer put the control and chipselect
*/
void QSPITransferCommand(uint16 *chipselect, uint8 length )
{
unsigned char i;
MCF_QSPI_QAR = 0x20;
MCF_QSPI_QWR = MCF_QSPI_QWR_CSIV
| MCF_QSPI_QWR_ENDQP(length-1)
| MCF_QSPI_QWR_NEWQP(0x00);
for(i = 0 ; i length; i++)
{
QSPISetTransferCommand(chipselect[i],0);
}
return;
}
//this is the same as above, but keeps the CS line constant throughout xfer
void QSPITransferCommandAlt(uint16 *chipselect, uint8 length )
{
unsigned char i;
MCF_QSPI_QAR = 0x20;
MCF_QSPI_QWR = MCF_QSPI_QWR_CSIV
| MCF_QSPI_QWR_ENDQP(length-1)
| MCF_QSPI_QWR_NEWQP(0x00);
for(i = 0 ; i length; i++)
{
QSPISetTransferCommand(chipselect[i],1);
}
return;
}
/*
* fill the data buffer with the data we want to send and sended
*/
void QSPITransferData(uint16 *send, uint16 *receive, uint8 length)
{
unsigned char i;
uint16 temp;
MCF_QSPI_QAR = 0x00; //addr of 'data ram'
for(i = 0; i length; i++) //send all data values to data ram
MCF_QSPI_QDR = send[i];
MCF_QSPI_QDLYR |= MCF_QSPI_QDLYR_SPE; //enable transfer
while (!(MCF_QSPI_QIR & MCF_QSPI_QIR_SPIF))
; /* Empty Body */
MCF_QSPI_QAR = 0x10;
for (i = 0; i length; i++)
receive[i] = MCF_QSPI_QDR;
return;
}
/*
* build the control word with the chipselect
*/
void QSPISetTransferCommand (unsigned char u8CS, unsigned char u8Cont)
{
MCF_QSPI_QCR = (u8Cont 15)| (MCF_QSPI_QCR_BITSE | MCF_QSPI_QCR_DT |
MCF_QSPI_QCR_DSCK | MCF_QSPI_QCR_CS(u8CS));
return;
}
the code in qspifunctions should be working, because i have successfully used them to write to a d/a on the bus.
any thoughts?!
thanks in advance,
Trevor