/********************************************************************************* * * Embedded Studio (C) 2007 * * File: ES_FRAM.c * Desc: Ramtron FRAM FM25CL64 Driver Functions * CPU: Analog Device ADSP-21369 * **********************************************************************************/ #include "ES_FRAM.h" ////////////////////////////////////////////////////////////////////////////// // // private functions // ////////////////////////////////////////////////////////////////////////////// int WriteWordToSPI(int data); int ReadWordFromSPI(void); int ReadFramStatusRegister(void); bool WriteFramStatusRegister(int status); bool WaitForSPIF(void); void AssertSpiCS(void); void DeassertSpiCS(void); void EnableFramWrite(void); /******************************************************************************************* * Function: FRAM_Open * * Desc: This function sets up hardware SPI interface and configures control registers * * Params: None * * Returns: true if opening the port successfully, false otherwise * * Notes: None * *******************************************************************************************/ bool FRAM_Open(void) { int temp; ST_SPICTL spictl; //set up hardware interface DSP_SRU_SPI(); //SPI DMA is not used SPIDMAC.ALL = 0; //set SPI baud rate SPIBAUD.BIT.BAUD_DIVISOR = FRAM_BAUD_RATE_DIVISOR; //configure flag0 as chip select SPIFLG.BIT.DS0EN = 1; //configure SPI control register spictl.ALL = 0; spictl.BIT.TIMOD = 1; //mode 1 spictl.BIT.GM = 1; //new data overwrites previous one spictl.BIT.ISSEN = 0; //disable SPIDS to free that pin for chip select spictl.BIT.SPIEN = 1; //SPI enable spictl.BIT.SPIMS = 1; //master spictl.BIT.OPD = 0; //open drain mode disabled as we use 1 (master) - 1 (slave) connection spictl.BIT.SENDZ = 1; //send zeros spictl.BIT.WL = 0; //word width 8 spictl.BIT.MSBF = 1; //MSB first spictl.BIT.CLKPL = 1; //clock polarity active low spictl.BIT.CPHASE = 1; //control chip select manually spictl.BIT.ILPBK = 0; //internal loop back disabled SPICTL.ALL = spictl.ALL; //write status register to disable write protection WriteFramStatusRegister(FARM_INIT_STATUS); //read status register temp = ReadFramStatusRegister(); if (temp == FARM_INIT_STATUS) { return true; } else { return false; } } /******************************************************************************************* * Function: FRAM_Write * * Desc: This function writes a buffer to FRAM device * * Params: * int startAddr - address in FRAM to start the writes at * int length - number of elements to write, in this case bytes * int *pData - pointer to data buffer * * Returns: bool - true if writing is successful, false otherwise * * Notes: None * *******************************************************************************************/ bool FRAM_Write( int startAddr, int length, int *pData ) { int i; int absAddr; absAddr = FRAM_START_ADDR + startAddr; //write enable command EnableFramWrite(); //assert CS AssertSPI_CS(); //write command WriteWordToSPI(FRAM_WRITE); //high byte address WriteWordToSPI(absAddr >> 8); //low byte address WriteWordToSPI(absAddr); // write data to FRAM sequentially for (i = 0; i < length; i++) { WriteWordToSPI(*pData++); } //de-assert CS DeassertSPI_CS(); return true; } /******************************************************************************************* * Function: FRAM_Read * * Desc: This function reads data from FRAM device to a buffer * * Params: * int startAddr - address in FRAM to start the reading at * int length - number of bytes to read * int *pData - pointer to data buffer * * Returns: bool - true if reading is successful, false otherwise * * Notes: None * *******************************************************************************************/ bool FRAM_Read( int startAddr, int length, int *pData ) { int i; int absAddr; absAddr = FRAM_START_ADDR + startAddr; //assert CS AssertSPI_CS(); //read command WriteWordToSPI(FRAM_READ); //high byte address WriteWordToSPI(absAddr >> 8); //low byte address WriteWordToSPI(absAddr); // read the buffer up to NVM_BUFFER_SIZE items for (i = 0; i < length; i++) { //dummy write to generate 8 clks to clock data into SPIRX *pData++ = WriteWordToSPI(0); } //de-assert CS DeassertSPI_CS(); return true; } /******************************************************************************************* * Function: FRAM_Close * * Desc: This function closes FRAM device driver. * * Params: None * * Returns: bool - true if successful, false otherwise * * Notes: None * *******************************************************************************************/ bool FRAM_Close(void) { SPICTL.BIT.SPIEN = 0; return true; } /******************************************************************************************* * Function: ReadFramStatusRegister * * Desc: This function reads the 8-bit status register from the FRAM * * Params: None * * Returns: Status of the register * * Notes: None * *******************************************************************************************/ int ReadFramStatusRegister(void) { int status; //assert CS AssertSPI_CS(); //read status register WriteWordToSPI(FRAM_RDSR); //write status register with dummy word status = WriteWordToSPI(0); //de-assert CS DeassertSPI_CS(); return status; } /******************************************************************************************* * Function: WriteFramStatusRegister * * Desc: This function writes a word to the 8-bit status register of the FRAM * * Params: int status - status value * * Returns: bool - true if successful, false otherwise * * Notes: None * *******************************************************************************************/ bool WriteFramStatusRegister(int status) { //write enable command EnableFramWrite(); //assert CS AssertSPI_CS(); //write status register WriteWordToSPI(FRAM_WRSR); //write status register WriteWordToSPI(status); //de-assert CS DeassertSPI_CS(); return true; } /******************************************************************************************* * Function: EnableFramWrite * * Desc: This function enables FRAM write * * Params: None * * Returns: None * * Notes: None * *******************************************************************************************/ void EnableFramWrite(void) { //assert CS AssertSPI_CS(); //write enable WriteWordToSPI(FRAM_WREN); //de-assert CS DeassertSPI_CS(); } /******************************************************************************************* * Function: WriteWordToSPI * * Desc: This function writes one word to the SPI Tx register, waits for SPIF status bit * and reads the SPIRX content * * Params: data - the word to be written * * Returns: int - a byte from SPIRX * * Notes: SPIRX is full after any transmission is complete * *******************************************************************************************/ int WriteWordToSPI(int data) { //make sure SPITX is empty if (SPISTAT.BIT.TXS) { SPICTL.BIT.TXFLSH = 1; } TXSPI = data; //wait for transmission complete WaitForSPIF(); //must read SPIRX because transmission and reading take place simultaneously return ReadWordFromSPI(); } /******************************************************************************************* * Function: ReadWordFromSPI * * Desc: This function reads one word from the SPI Rx register * * Params: None * * Returns: int - contend of SPIRX * * Notes: None * *******************************************************************************************/ int ReadWordFromSPI(void) { while (!SPISTAT.BIT.RXS); return RXSPI; } /******************************************************************************************* * Function: WaitForSPIF * * Desc: This function polls the SPIF (SPI single word transfer complete) bit * of SPISTAT until the transfer is complete. * * Params: None * * Returns: bool - true if single word transfer is completed successfully, false otherwise * * Notes: status updates can be delayed up to 10 cycles * *******************************************************************************************/ bool WaitForSPIF(void) { int timeout = 10000; while( !SPISTAT.BIT.SPIF ) { if( timeout-- < 0 ) { return false; } } return true; } /******************************************************************************************* * Function: AssertSpiCS * * Desc: This function asserts the chip select signal through FLAG0 * * Params: None * * Returns: None * * Notes: this function is used when chip select is controlled manually * when SPICTL.BIT.CPHASE = 1 * it does not have any effect when chip select is controlled automatically * when SPICTL.BIT.CPHASE = 0 * *******************************************************************************************/ void AssertSpiCS(void) { //assert low level on flag 0 SPIFLG.BIT.SPIFLG0 = 0; } /******************************************************************************************* * Function: DeassertSpiCS * * Desc: This function de-asserts the chip select signal through FLAG0 * * Params: None * * Returns: None * * Notes: this function is used when chip select is controlled manually * when SPICTL.BIT.CPHASE = 1 * it does not have any effect when chip select is controlled automatically * when SPICTL.BIT.CPHASE = 0 * *******************************************************************************************/ void DeassertSpiCS(void) { //set high level on flag 0 SPIFLG.BIT.SPIFLG0 = 1; }