199767f8919635c4928607450d9e0abb932109ceToomas Soome/******************************************************************************
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Instantiation of SPI flash control routines supporting AT45DB161B
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Revision information:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 17JAN2005 kb_admin initial creation
199767f8919635c4928607450d9e0abb932109ceToomas Soome * adapted from external sources
199767f8919635c4928607450d9e0abb932109ceToomas Soome * tested for basic operation only!!!
199767f8919635c4928607450d9e0abb932109ceToomas Soome * BEGIN_KBDD_BLOCK
199767f8919635c4928607450d9e0abb932109ceToomas Soome * No warranty, expressed or implied, is included with this software. It is
199767f8919635c4928607450d9e0abb932109ceToomas Soome * provided "AS IS" and no warranty of any kind including statutory or aspects
199767f8919635c4928607450d9e0abb932109ceToomas Soome * relating to merchantability or fitness for any purpose is provided. All
199767f8919635c4928607450d9e0abb932109ceToomas Soome * intellectual property rights of others is maintained with the respective
199767f8919635c4928607450d9e0abb932109ceToomas Soome * owners. This software is not copyrighted and is intended for reference
199767f8919635c4928607450d9e0abb932109ceToomas Soome *****************************************************************************/
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*********************** PRIVATE FUNCTIONS/DATA ******************************/
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char tx_commandBuffer[8], rx_commandBuffer[8];
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_START
199767f8919635c4928607450d9e0abb932109ceToomas Soome * void SendCommand(spiCommand_t *pCommand)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Private function sends 8-bit value to the device and returns the 8-bit
199767f8919635c4928607450d9e0abb932109ceToomas Soome * value in response.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_END
199767f8919635c4928607450d9e0abb932109ceToomas Soome pSPI->SPI_PTCR = AT91C_PDC_TXTDIS | AT91C_PDC_RXTDIS;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pSPI->SPI_PTCR = AT91C_PDC_TXTEN | AT91C_PDC_RXTEN;
199767f8919635c4928607450d9e0abb932109ceToomas Soome // wait for completion
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_START
199767f8919635c4928607450d9e0abb932109ceToomas Soome * char GetFlashStatus(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Private function to return device status.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_END
199767f8919635c4928607450d9e0abb932109ceToomas Soome p_memset((char *)&spi_command, 0, sizeof(spi_command));
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_START
199767f8919635c4928607450d9e0abb932109ceToomas Soome * void WaitForDeviceReady(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Private function to poll until the device is ready for next operation.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_END
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*************************** GLOBAL FUNCTIONS ********************************/
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_START
199767f8919635c4928607450d9e0abb932109ceToomas Soome * void SPI_ReadFlash(unsigned flash_addr, unsigned dest_addr, unsigned size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Global function to read the SPI flash device using the continuous read
199767f8919635c4928607450d9e0abb932109ceToomas Soome * array command.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_END
199767f8919635c4928607450d9e0abb932109ceToomas SoomeSPI_ReadFlash(unsigned flash_addr, char *dest_addr, unsigned size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome // determine page address
199767f8919635c4928607450d9e0abb932109ceToomas Soome // determine byte address
199767f8919635c4928607450d9e0abb932109ceToomas Soome tx_commandBuffer[1] = ((pageAddress >> 6) & 0xFF);
199767f8919635c4928607450d9e0abb932109ceToomas Soome tx_commandBuffer[2] = ((pageAddress << 2) & 0xFC) |
199767f8919635c4928607450d9e0abb932109ceToomas Soome tx_commandBuffer[0] = CONTINUOUS_ARRAY_READ_HF;
199767f8919635c4928607450d9e0abb932109ceToomas Soome tx_commandBuffer[1] = ((pageAddress >> 5) & 0xFF);
199767f8919635c4928607450d9e0abb932109ceToomas Soome tx_commandBuffer[2] = ((pageAddress << 3) & 0xF8) |
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_START
199767f8919635c4928607450d9e0abb932109ceToomas Soome * void SPI_WriteFlash(unsigned flash_addr, unsigned src_addr, unsigned size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Global function to program the SPI flash device. Notice the warning
199767f8919635c4928607450d9e0abb932109ceToomas Soome * provided in lower-level functions regarding corruption of data in non-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * page aligned write operations.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_END
199767f8919635c4928607450d9e0abb932109ceToomas SoomeSPI_WriteFlash(unsigned flash_addr, char *src_addr, unsigned size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome // determine page address
199767f8919635c4928607450d9e0abb932109ceToomas Soome // determine byte address
199767f8919635c4928607450d9e0abb932109ceToomas Soome tx_commandBuffer[1] = ((pageAddress >> 6) & 0xFF);
199767f8919635c4928607450d9e0abb932109ceToomas Soome tx_commandBuffer[2] = ((pageAddress << 2) & 0xFC) |
199767f8919635c4928607450d9e0abb932109ceToomas Soome tx_commandBuffer[1] = ((pageAddress >> 5) & 0xFF);
199767f8919635c4928607450d9e0abb932109ceToomas Soome tx_commandBuffer[2] = ((pageAddress << 3) & 0xF8) |
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_START
199767f8919635c4928607450d9e0abb932109ceToomas Soome * void SPI_InitFlash(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Global function to initialize the SPI flash device/accessor functions.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_END
199767f8919635c4928607450d9e0abb932109ceToomas Soome // enable CS0, CLK, MOSI, MISO
199767f8919635c4928607450d9e0abb932109ceToomas Soome pPio->PIO_ASR = AT91C_PIO_PA3 | AT91C_PIO_PA1 | AT91C_PIO_PA0 |
199767f8919635c4928607450d9e0abb932109ceToomas Soome pPio->PIO_PDR = AT91C_PIO_PA3 | AT91C_PIO_PA1 | AT91C_PIO_PA0 |
199767f8919635c4928607450d9e0abb932109ceToomas Soome // enable clocks to SPI
199767f8919635c4928607450d9e0abb932109ceToomas Soome // reset the SPI
199767f8919635c4928607450d9e0abb932109ceToomas Soome pSPI->SPI_MR = (0xf << 24) | AT91C_SPI_MSTR | AT91C_SPI_MODFDIS |
199767f8919635c4928607450d9e0abb932109ceToomas Soome pSPI->SPI_CSR[0] = AT91C_SPI_CPOL | (4 << 16) | (2 << 8);