2011-01-24 08:52:14 +01:00
|
|
|
/*
|
|
|
|
* pios_flash_w25x.c
|
|
|
|
* OpenPilotOSX
|
|
|
|
*
|
|
|
|
* Created by James Cotton on 1/23/11.
|
|
|
|
* Copyright 2011 OpenPilot. All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pios.h"
|
|
|
|
#include "pios_flash_w25x.h"
|
2011-01-25 18:46:27 +01:00
|
|
|
#include "pios_adxl345.h"
|
2011-01-24 08:52:14 +01:00
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
#define W25X_WRITE_ENABLE 0x06
|
|
|
|
#define W25X_WRITE_DISABLE 0x04
|
|
|
|
#define W25X_READ_STATUS 0x05
|
|
|
|
#define W25X_WRITE_STATUS 0x01
|
|
|
|
#define W25X_READ_DATA 0x03
|
|
|
|
#define W25X_FAST_READ 0x0b
|
|
|
|
#define W25X_DEVICE_ID 0x90
|
2011-11-26 21:55:16 +01:00
|
|
|
#define W25X_SECTOR_ERASE 0xD8
|
2011-02-01 03:18:04 +01:00
|
|
|
#define W25X_PAGE_WRITE 0x02
|
2011-11-26 21:55:16 +01:00
|
|
|
#define W25X_CHIP_ERASE 0xC7
|
2011-02-01 03:18:04 +01:00
|
|
|
|
|
|
|
#define W25X_STATUS_BUSY 0x01
|
|
|
|
#define W25X_STATUS_WRITEPROTECT 0x02
|
|
|
|
#define W25X_STATUS_BP0 0x04
|
|
|
|
#define W25X_STATUS_BP1 0x08
|
|
|
|
#define W25X_STATUS_BP2 0x10
|
|
|
|
#define W25X_STATUS_TP 0x20
|
|
|
|
#define W25X_STATUS_SEC 0x40
|
|
|
|
#define W25X_STATUS_SRP0 0x80
|
|
|
|
|
|
|
|
static uint8_t device_type;
|
|
|
|
|
|
|
|
//! Private functions
|
|
|
|
static int8_t PIOS_Flash_W25X_ClaimBus();
|
|
|
|
static void PIOS_Flash_W25X_ReleaseBus();
|
|
|
|
static uint8_t PIOS_Flash_W25X_WriteEnable();
|
|
|
|
static uint8_t PIOS_Flash_W25X_Busy() ;
|
|
|
|
|
2011-02-12 23:19:50 +01:00
|
|
|
static uint32_t PIOS_SPI_FLASH;
|
|
|
|
|
2011-07-16 01:06:18 +02:00
|
|
|
/**
|
2011-02-01 03:18:04 +01:00
|
|
|
* @brief Claim the SPI bus for flash use and assert CS pin
|
|
|
|
* @return 0 for sucess, -1 for failure to get semaphore
|
|
|
|
*/
|
2011-07-16 01:06:18 +02:00
|
|
|
static int8_t PIOS_Flash_W25X_ClaimBus()
|
2011-01-24 08:52:14 +01:00
|
|
|
{
|
2011-02-01 03:18:04 +01:00
|
|
|
int8_t ret = PIOS_SPI_ClaimBus(PIOS_SPI_FLASH);
|
2011-01-24 08:52:14 +01:00
|
|
|
PIOS_FLASH_ENABLE;
|
2011-02-01 03:18:04 +01:00
|
|
|
return (ret == 0) ? 0 : -1;
|
2011-01-24 08:52:14 +01:00
|
|
|
}
|
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
/**
|
|
|
|
* @brief Release the SPI bus sempahore and ensure flash chip not using bus
|
|
|
|
*/
|
2011-07-16 01:06:18 +02:00
|
|
|
static void PIOS_Flash_W25X_ReleaseBus()
|
2011-01-24 08:52:14 +01:00
|
|
|
{
|
|
|
|
PIOS_FLASH_DISABLE;
|
2011-01-25 18:46:27 +01:00
|
|
|
PIOS_SPI_ReleaseBus(PIOS_SPI_FLASH);
|
2011-01-24 08:52:14 +01:00
|
|
|
}
|
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
/**
|
|
|
|
* @brief Returns if the flash chip is busy
|
|
|
|
*/
|
2011-07-16 01:06:18 +02:00
|
|
|
static uint8_t PIOS_Flash_W25X_Busy()
|
|
|
|
{
|
2011-02-01 03:18:04 +01:00
|
|
|
return PIOS_Flash_W25X_ReadStatus() & W25X_STATUS_BUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Execute the write enable instruction and returns the status
|
|
|
|
* @returns 0 if successful, -1 if unable to claim bus
|
|
|
|
*/
|
2011-07-16 01:06:18 +02:00
|
|
|
static uint8_t PIOS_Flash_W25X_WriteEnable()
|
2011-01-24 08:52:14 +01:00
|
|
|
{
|
2011-02-01 03:18:04 +01:00
|
|
|
uint8_t out[] = {W25X_WRITE_ENABLE};
|
|
|
|
if(PIOS_Flash_W25X_ClaimBus() != 0)
|
|
|
|
return -1;
|
2011-07-16 01:06:18 +02:00
|
|
|
PIOS_SPI_TransferBlock(PIOS_SPI_FLASH,out,NULL,sizeof(out),NULL);
|
2011-02-01 03:18:04 +01:00
|
|
|
PIOS_Flash_W25X_ReleaseBus();
|
|
|
|
return 0;
|
2011-01-24 08:52:14 +01:00
|
|
|
}
|
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
/**
|
|
|
|
* @brief Initialize the flash device and enable write access
|
|
|
|
*/
|
2011-02-12 23:19:50 +01:00
|
|
|
int8_t PIOS_Flash_W25X_Init(uint32_t spi_id)
|
2011-02-01 03:18:04 +01:00
|
|
|
{
|
2011-02-12 23:19:50 +01:00
|
|
|
PIOS_SPI_FLASH = spi_id;
|
|
|
|
|
2011-11-19 07:13:02 +01:00
|
|
|
#if defined(PIOS_FLASH_CS_PIN)
|
2011-02-01 03:18:04 +01:00
|
|
|
PIOS_GPIO_Enable(PIOS_FLASH_CS_PIN);
|
2011-11-19 07:13:02 +01:00
|
|
|
#endif
|
2011-02-01 03:18:04 +01:00
|
|
|
device_type = PIOS_Flash_W25X_ReadID();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-24 08:52:14 +01:00
|
|
|
/**
|
2011-07-16 01:06:18 +02:00
|
|
|
* @brief Read the status register from flash chip and return it
|
2011-01-24 08:52:14 +01:00
|
|
|
*/
|
2011-07-16 01:06:18 +02:00
|
|
|
uint8_t PIOS_Flash_W25X_ReadStatus()
|
2011-01-24 08:52:14 +01:00
|
|
|
{
|
|
|
|
uint8_t out[2] = {W25X_READ_STATUS, 0};
|
|
|
|
uint8_t in[2] = {0,0};
|
2011-02-01 03:18:04 +01:00
|
|
|
PIOS_Flash_W25X_ClaimBus();
|
2011-07-16 01:06:18 +02:00
|
|
|
PIOS_SPI_TransferBlock(PIOS_SPI_FLASH,out,in,sizeof(out),NULL);
|
2011-02-01 03:18:04 +01:00
|
|
|
PIOS_Flash_W25X_ReleaseBus();
|
2011-01-24 08:52:14 +01:00
|
|
|
return in[1];
|
|
|
|
}
|
|
|
|
|
2011-01-25 18:46:27 +01:00
|
|
|
/**
|
2011-07-16 01:06:18 +02:00
|
|
|
* @brief Read the status register from flash chip and return it
|
2011-01-25 18:46:27 +01:00
|
|
|
*/
|
2011-07-16 01:06:18 +02:00
|
|
|
uint8_t PIOS_Flash_W25X_ReadID()
|
2011-01-25 18:46:27 +01:00
|
|
|
{
|
|
|
|
uint8_t out[] = {W25X_DEVICE_ID, 0, 0, 0, 0, 0};
|
|
|
|
uint8_t in[6];
|
2011-02-01 03:18:04 +01:00
|
|
|
PIOS_Flash_W25X_ClaimBus();
|
2011-07-16 01:06:18 +02:00
|
|
|
PIOS_SPI_TransferBlock(PIOS_SPI_FLASH,out,in,sizeof(out),NULL);
|
2011-02-01 03:18:04 +01:00
|
|
|
PIOS_Flash_W25X_ReleaseBus();
|
2011-01-25 18:46:27 +01:00
|
|
|
return in[5];
|
|
|
|
}
|
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
/**
|
|
|
|
* @brief Erase a sector on the flash chip
|
|
|
|
* @param[in] add Address of flash to erase
|
|
|
|
* @returns 0 if successful
|
|
|
|
* @retval -1 if unable to claim bus
|
2011-07-16 01:06:18 +02:00
|
|
|
* @retval
|
2011-02-01 03:18:04 +01:00
|
|
|
*/
|
|
|
|
int8_t PIOS_Flash_W25X_EraseSector(uint32_t addr)
|
2011-01-24 08:52:14 +01:00
|
|
|
{
|
2011-02-01 03:18:04 +01:00
|
|
|
uint8_t ret;
|
2011-02-01 03:18:12 +01:00
|
|
|
uint8_t out[] = {W25X_SECTOR_ERASE, (addr >> 16) & 0xff, (addr >> 8) & 0xff , addr & 0xff};
|
2011-07-16 01:06:18 +02:00
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
if((ret = PIOS_Flash_W25X_WriteEnable()) != 0)
|
|
|
|
return ret;
|
2011-07-16 01:06:18 +02:00
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
if(PIOS_Flash_W25X_ClaimBus() != 0)
|
2011-07-16 01:06:18 +02:00
|
|
|
return -1;
|
2011-02-01 03:18:04 +01:00
|
|
|
PIOS_SPI_TransferBlock(PIOS_SPI_FLASH,out,NULL,sizeof(out),NULL);
|
|
|
|
PIOS_Flash_W25X_ReleaseBus();
|
|
|
|
|
|
|
|
while(PIOS_Flash_W25X_Busy()) {
|
|
|
|
}
|
2011-07-16 01:06:18 +02:00
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
return 0;
|
2011-01-24 08:52:14 +01:00
|
|
|
}
|
|
|
|
|
2011-02-01 03:18:08 +01:00
|
|
|
/**
|
|
|
|
* @brief Execute the whole chip
|
|
|
|
* @returns 0 if successful, -1 if unable to claim bus
|
|
|
|
*/
|
2011-02-01 03:18:12 +01:00
|
|
|
int8_t PIOS_Flash_W25X_EraseChip()
|
2011-02-01 03:18:08 +01:00
|
|
|
{
|
|
|
|
uint8_t ret;
|
|
|
|
uint8_t out[] = {W25X_CHIP_ERASE};
|
2011-07-16 01:06:18 +02:00
|
|
|
|
2011-02-01 03:18:08 +01:00
|
|
|
if((ret = PIOS_Flash_W25X_WriteEnable()) != 0)
|
|
|
|
return ret;
|
2011-07-16 01:06:18 +02:00
|
|
|
|
2011-02-01 03:18:08 +01:00
|
|
|
if(PIOS_Flash_W25X_ClaimBus() != 0)
|
|
|
|
return -1;
|
2011-07-16 01:06:18 +02:00
|
|
|
PIOS_SPI_TransferBlock(PIOS_SPI_FLASH,out,NULL,sizeof(out),NULL);
|
2011-02-01 03:18:08 +01:00
|
|
|
PIOS_Flash_W25X_ReleaseBus();
|
2011-07-16 01:06:18 +02:00
|
|
|
|
2011-02-01 03:18:08 +01:00
|
|
|
while(PIOS_Flash_W25X_Busy()) {
|
|
|
|
}
|
2011-07-16 01:06:18 +02:00
|
|
|
|
2011-02-01 03:18:08 +01:00
|
|
|
return 0;
|
2011-02-01 03:18:12 +01:00
|
|
|
}
|
2011-02-01 03:18:08 +01:00
|
|
|
|
|
|
|
|
2011-07-16 01:06:18 +02:00
|
|
|
/**
|
2011-02-01 03:18:04 +01:00
|
|
|
* @brief Write one page of data (up to 256 bytes) aligned to a page start
|
|
|
|
* @param[in] addr Address in flash to write to
|
|
|
|
* @param[in] data Pointer to data to write to flash
|
|
|
|
* @param[in] len Length of data to write (max 256 bytes)
|
|
|
|
* @return Zero if success or error code
|
|
|
|
* @retval -1 Unable to claim SPI bus
|
|
|
|
* @retval -2 Size exceeds 256 bytes
|
|
|
|
* @retval -3 Length to write would wrap around page boundary
|
|
|
|
*/
|
|
|
|
int8_t PIOS_Flash_W25X_WriteData(uint32_t addr, uint8_t * data, uint16_t len)
|
2011-01-24 08:52:14 +01:00
|
|
|
{
|
2011-02-01 03:18:04 +01:00
|
|
|
uint8_t ret;
|
2011-02-01 03:18:08 +01:00
|
|
|
uint8_t out[4] = {W25X_PAGE_WRITE, (addr >> 16) & 0xff, (addr >> 8) & 0xff , addr & 0xff};
|
2011-07-16 01:06:18 +02:00
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
/* Can only write one page at a time */
|
2011-07-16 01:06:18 +02:00
|
|
|
if(len > 0x100)
|
2011-02-01 03:18:04 +01:00
|
|
|
return -2;
|
2011-07-16 01:06:18 +02:00
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
/* Ensure number of bytes fits after starting address before end of page */
|
2011-07-16 01:06:18 +02:00
|
|
|
if(((addr & 0xff) + len) > 0x100)
|
2011-02-01 03:18:04 +01:00
|
|
|
return -3;
|
2011-07-16 01:06:18 +02:00
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
if((ret = PIOS_Flash_W25X_WriteEnable()) != 0)
|
|
|
|
return ret;
|
2011-07-16 01:06:18 +02:00
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
/* Execute write page command and clock in address. Keep CS asserted */
|
|
|
|
if(PIOS_Flash_W25X_ClaimBus() != 0)
|
|
|
|
return -1;
|
|
|
|
PIOS_SPI_TransferBlock(PIOS_SPI_FLASH,out,NULL,sizeof(out),NULL);
|
2011-07-16 01:06:18 +02:00
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
/* Clock out data to flash */
|
|
|
|
PIOS_SPI_TransferBlock(PIOS_SPI_FLASH,data,NULL,len,NULL);
|
|
|
|
|
|
|
|
PIOS_Flash_W25X_ReleaseBus();
|
|
|
|
|
|
|
|
while(PIOS_Flash_W25X_Busy()) {
|
|
|
|
}
|
2011-07-16 01:06:18 +02:00
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-16 01:06:18 +02:00
|
|
|
/**
|
2011-05-14 21:28:11 +02:00
|
|
|
* @brief Read data from a location in flash memory
|
|
|
|
* @param[in] addr Address in flash to write to
|
|
|
|
* @param[in] data Pointer to data to write from flash
|
|
|
|
* @param[in] len Length of data to write (max 256 bytes)
|
|
|
|
* @return Zero if success or error code
|
|
|
|
* @retval -1 Unable to claim SPI bus
|
|
|
|
*/
|
2011-02-01 03:18:04 +01:00
|
|
|
int8_t PIOS_Flash_W25X_ReadData(uint32_t addr, uint8_t * data, uint16_t len)
|
|
|
|
{
|
|
|
|
if(PIOS_Flash_W25X_ClaimBus() == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Execute read command and clock in address. Keep CS asserted */
|
2011-02-01 03:18:08 +01:00
|
|
|
uint8_t out[] = {W25X_READ_DATA, (addr >> 16) & 0xff, (addr >> 8) & 0xff , addr & 0xff};
|
2011-02-01 03:18:04 +01:00
|
|
|
PIOS_SPI_TransferBlock(PIOS_SPI_FLASH,out,NULL,sizeof(out),NULL);
|
|
|
|
|
|
|
|
/* Copy the transfer data to the buffer */
|
|
|
|
PIOS_SPI_TransferBlock(PIOS_SPI_FLASH,NULL,data,len,NULL);
|
|
|
|
|
|
|
|
PIOS_Flash_W25X_ReleaseBus();
|
2011-07-16 01:06:18 +02:00
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
return 0;
|
2011-02-12 23:19:50 +01:00
|
|
|
}
|