2012-01-20 14:37:47 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @addtogroup PIOS PIOS Core hardware abstraction layer
|
|
|
|
* @{
|
|
|
|
* @addtogroup PIOS_FLASH Flash device handler
|
|
|
|
* @{
|
2011-01-24 08:52:14 +01:00
|
|
|
*
|
2012-01-20 14:37:47 +01:00
|
|
|
* @file pios_flash_w25x.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
2012-12-12 04:36:18 +01:00
|
|
|
* @author PhoenixPilot, http://github.com/PhoenixPilot, Copyright (C) 2012
|
2012-01-20 14:37:47 +01:00
|
|
|
* @brief Driver for talking to W25X flash chip (and most JEDEC chips)
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
2011-01-24 08:52:14 +01:00
|
|
|
*
|
2012-01-20 14:37:47 +01:00
|
|
|
*****************************************************************************/
|
2012-12-15 05:13:33 +01:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2012-01-20 14:37:47 +01:00
|
|
|
* (at your option) any later version.
|
2012-12-15 05:13:33 +01:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
2012-01-20 14:37:47 +01:00
|
|
|
* for more details.
|
2012-12-15 05:13:33 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2012-01-20 14:37:47 +01:00
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2011-01-24 08:52:14 +01:00
|
|
|
*/
|
|
|
|
#include "pios.h"
|
2012-12-12 04:36:18 +01:00
|
|
|
#include "pios_flash_jedec_priv.h"
|
2013-03-27 00:26:47 +01:00
|
|
|
#include "pios_flash_jedec_catalog.h"
|
2012-01-25 07:23:24 +01:00
|
|
|
|
|
|
|
#define JEDEC_WRITE_ENABLE 0x06
|
|
|
|
#define JEDEC_WRITE_DISABLE 0x04
|
|
|
|
#define JEDEC_READ_STATUS 0x05
|
|
|
|
#define JEDEC_WRITE_STATUS 0x01
|
|
|
|
#define JEDEC_READ_DATA 0x03
|
|
|
|
#define JEDEC_FAST_READ 0x0b
|
2012-01-26 17:04:49 +01:00
|
|
|
#define JEDEC_DEVICE_ID 0x9F
|
2012-01-25 07:23:24 +01:00
|
|
|
#define JEDEC_PAGE_WRITE 0x02
|
|
|
|
|
|
|
|
#define JEDEC_STATUS_BUSY 0x01
|
|
|
|
#define JEDEC_STATUS_WRITEPROTECT 0x02
|
|
|
|
#define JEDEC_STATUS_BP0 0x04
|
|
|
|
#define JEDEC_STATUS_BP1 0x08
|
|
|
|
#define JEDEC_STATUS_BP2 0x10
|
|
|
|
#define JEDEC_STATUS_TP 0x20
|
|
|
|
#define JEDEC_STATUS_SEC 0x40
|
|
|
|
#define JEDEC_STATUS_SRP0 0x80
|
2011-02-01 03:18:04 +01:00
|
|
|
|
2012-01-25 07:23:24 +01:00
|
|
|
enum pios_jedec_dev_magic {
|
|
|
|
PIOS_JEDEC_DEV_MAGIC = 0xcb55aa55,
|
2012-01-20 14:37:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
//! Device handle structure
|
2012-01-25 07:23:24 +01:00
|
|
|
struct jedec_flash_dev {
|
2012-01-20 14:37:47 +01:00
|
|
|
uint32_t spi_id;
|
|
|
|
uint32_t slave_num;
|
2012-01-21 16:57:12 +01:00
|
|
|
bool claimed;
|
2012-12-15 05:16:31 +01:00
|
|
|
|
|
|
|
uint8_t manufacturer;
|
|
|
|
uint8_t memorytype;
|
|
|
|
uint8_t capacity;
|
|
|
|
|
2012-01-25 07:23:24 +01:00
|
|
|
const struct pios_flash_jedec_cfg * cfg;
|
2012-01-26 18:50:05 +01:00
|
|
|
#if defined(FLASH_FREERTOS)
|
2012-01-26 18:35:43 +01:00
|
|
|
xSemaphoreHandle transaction_lock;
|
2012-01-26 18:50:05 +01:00
|
|
|
#endif
|
2012-01-25 07:23:24 +01:00
|
|
|
enum pios_jedec_dev_magic magic;
|
2012-01-20 14:37:47 +01:00
|
|
|
};
|
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
//! Private functions
|
2012-12-12 04:36:18 +01:00
|
|
|
static int32_t PIOS_Flash_Jedec_Validate(struct jedec_flash_dev * flash_dev);
|
2012-01-25 07:23:24 +01:00
|
|
|
static struct jedec_flash_dev * PIOS_Flash_Jedec_alloc(void);
|
2012-01-20 14:37:47 +01:00
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
static int32_t PIOS_Flash_Jedec_ReadID(struct jedec_flash_dev * flash_dev);
|
|
|
|
static int32_t PIOS_Flash_Jedec_ReadStatus(struct jedec_flash_dev * flash_dev);
|
|
|
|
static int32_t PIOS_Flash_Jedec_ClaimBus(struct jedec_flash_dev * flash_dev);
|
|
|
|
static int32_t PIOS_Flash_Jedec_ReleaseBus(struct jedec_flash_dev * flash_dev);
|
|
|
|
static int32_t PIOS_Flash_Jedec_WriteEnable(struct jedec_flash_dev * flash_dev);
|
|
|
|
static int32_t PIOS_Flash_Jedec_Busy(struct jedec_flash_dev * flash_dev);
|
2011-02-01 03:18:04 +01:00
|
|
|
|
2012-01-20 14:37:47 +01:00
|
|
|
/**
|
|
|
|
* @brief Allocate a new device
|
|
|
|
*/
|
2012-01-25 07:23:24 +01:00
|
|
|
static struct jedec_flash_dev * PIOS_Flash_Jedec_alloc(void)
|
2012-01-20 14:37:47 +01:00
|
|
|
{
|
2012-12-12 04:36:18 +01:00
|
|
|
struct jedec_flash_dev * flash_dev;
|
2012-12-15 05:13:33 +01:00
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
flash_dev = (struct jedec_flash_dev *)pvPortMalloc(sizeof(*flash_dev));
|
|
|
|
if (!flash_dev) return (NULL);
|
2012-12-15 05:13:33 +01:00
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
flash_dev->claimed = false;
|
|
|
|
flash_dev->magic = PIOS_JEDEC_DEV_MAGIC;
|
2012-01-26 18:50:05 +01:00
|
|
|
#if defined(FLASH_FREERTOS)
|
2012-12-12 04:36:18 +01:00
|
|
|
flash_dev->transaction_lock = xSemaphoreCreateMutex();
|
2012-01-26 18:50:05 +01:00
|
|
|
#endif
|
2012-12-12 04:36:18 +01:00
|
|
|
return(flash_dev);
|
2012-01-20 14:37:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Validate the handle to the spi device
|
|
|
|
*/
|
2012-12-12 04:36:18 +01:00
|
|
|
static int32_t PIOS_Flash_Jedec_Validate(struct jedec_flash_dev * flash_dev) {
|
|
|
|
if (flash_dev == NULL)
|
2012-01-20 14:37:47 +01:00
|
|
|
return -1;
|
2012-12-12 04:36:18 +01:00
|
|
|
if (flash_dev->magic != PIOS_JEDEC_DEV_MAGIC)
|
2012-01-20 14:37:47 +01:00
|
|
|
return -2;
|
2012-12-12 04:36:18 +01:00
|
|
|
if (flash_dev->spi_id == 0)
|
2012-01-20 14:37:47 +01:00
|
|
|
return -3;
|
|
|
|
return 0;
|
|
|
|
}
|
2011-02-12 23:19:50 +01:00
|
|
|
|
2011-07-16 01:06:18 +02:00
|
|
|
/**
|
2012-12-12 04:36:18 +01:00
|
|
|
* @brief Initialize the flash device and enable write access
|
2011-02-01 03:18:04 +01:00
|
|
|
*/
|
2013-03-27 00:26:47 +01:00
|
|
|
int32_t PIOS_Flash_Jedec_Init(uintptr_t * flash_id, uint32_t spi_id, uint32_t slave_num)
|
2011-01-24 08:52:14 +01:00
|
|
|
{
|
2012-12-12 04:36:18 +01:00
|
|
|
struct jedec_flash_dev * flash_dev = PIOS_Flash_Jedec_alloc();
|
|
|
|
if (flash_dev == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
flash_dev->spi_id = spi_id;
|
|
|
|
flash_dev->slave_num = slave_num;
|
2013-03-29 00:30:28 +01:00
|
|
|
flash_dev->cfg = NULL;
|
2012-12-12 04:36:18 +01:00
|
|
|
|
2012-12-15 05:16:31 +01:00
|
|
|
(void) PIOS_Flash_Jedec_ReadID(flash_dev);
|
2013-03-27 00:26:47 +01:00
|
|
|
uint32_t i = 0;
|
2013-03-29 00:30:28 +01:00
|
|
|
while ((i < pios_flash_jedec_catalog_size) && !flash_dev->cfg)
|
2013-03-27 00:26:47 +01:00
|
|
|
{
|
|
|
|
const struct pios_flash_jedec_cfg flash_jedec_entry = pios_flash_jedec_catalog[i];
|
|
|
|
|
|
|
|
if ((flash_dev->manufacturer == flash_jedec_entry.expect_manufacturer) &&
|
|
|
|
(flash_dev->memorytype == flash_jedec_entry.expect_memorytype) &&
|
|
|
|
(flash_dev->capacity == flash_jedec_entry.expect_capacity))
|
|
|
|
{
|
|
|
|
flash_dev->cfg = &pios_flash_jedec_catalog[i];
|
|
|
|
}
|
|
|
|
i++;
|
2012-12-15 05:16:31 +01:00
|
|
|
}
|
2012-01-20 14:37:47 +01:00
|
|
|
|
2013-03-29 00:30:28 +01:00
|
|
|
if(!flash_dev->cfg)
|
|
|
|
{
|
2013-03-27 00:26:47 +01:00
|
|
|
return -1;
|
2013-03-29 00:30:28 +01:00
|
|
|
}
|
2013-03-27 00:26:47 +01:00
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
/* Give back a handle to this flash device */
|
2012-12-15 05:13:33 +01:00
|
|
|
*flash_id = (uintptr_t) flash_dev;
|
2012-12-12 04:36:18 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Claim the SPI bus for flash use and assert CS pin
|
|
|
|
* @return 0 for sucess, -1 for failure to get semaphore
|
|
|
|
*/
|
|
|
|
static int32_t PIOS_Flash_Jedec_ClaimBus(struct jedec_flash_dev * flash_dev)
|
|
|
|
{
|
2012-12-15 05:13:33 +01:00
|
|
|
if (PIOS_SPI_ClaimBus(flash_dev->spi_id) < 0)
|
2012-01-21 16:57:12 +01:00
|
|
|
return -1;
|
2012-12-15 05:13:33 +01:00
|
|
|
|
2012-01-20 14:37:47 +01:00
|
|
|
PIOS_SPI_RC_PinSet(flash_dev->spi_id, flash_dev->slave_num, 0);
|
2012-01-21 16:57:12 +01:00
|
|
|
flash_dev->claimed = true;
|
2012-12-15 05:13:33 +01:00
|
|
|
|
2012-01-21 16:57:12 +01:00
|
|
|
return 0;
|
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
|
|
|
|
*/
|
2012-12-12 04:36:18 +01:00
|
|
|
static int32_t PIOS_Flash_Jedec_ReleaseBus(struct jedec_flash_dev * flash_dev)
|
2011-01-24 08:52:14 +01:00
|
|
|
{
|
2012-01-20 14:37:47 +01:00
|
|
|
PIOS_SPI_RC_PinSet(flash_dev->spi_id, flash_dev->slave_num, 1);
|
|
|
|
PIOS_SPI_ReleaseBus(flash_dev->spi_id);
|
2012-01-21 16:57:12 +01:00
|
|
|
flash_dev->claimed = false;
|
|
|
|
return 0;
|
2011-01-24 08:52:14 +01:00
|
|
|
}
|
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
/**
|
|
|
|
* @brief Returns if the flash chip is busy
|
2012-01-20 14:37:47 +01:00
|
|
|
* @returns -1 for failure, 0 for not busy, 1 for busy
|
2011-02-01 03:18:04 +01:00
|
|
|
*/
|
2012-12-12 04:36:18 +01:00
|
|
|
static int32_t PIOS_Flash_Jedec_Busy(struct jedec_flash_dev * flash_dev)
|
2011-07-16 01:06:18 +02:00
|
|
|
{
|
2012-12-12 04:36:18 +01:00
|
|
|
int32_t status = PIOS_Flash_Jedec_ReadStatus(flash_dev);
|
2012-01-20 14:37:47 +01:00
|
|
|
if (status < 0)
|
|
|
|
return -1;
|
2012-01-25 07:23:24 +01:00
|
|
|
return status & JEDEC_STATUS_BUSY;
|
2011-02-01 03:18:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Execute the write enable instruction and returns the status
|
|
|
|
* @returns 0 if successful, -1 if unable to claim bus
|
|
|
|
*/
|
2012-12-12 04:36:18 +01:00
|
|
|
static int32_t PIOS_Flash_Jedec_WriteEnable(struct jedec_flash_dev * flash_dev)
|
2011-01-24 08:52:14 +01:00
|
|
|
{
|
2012-12-12 04:36:18 +01:00
|
|
|
if (PIOS_Flash_Jedec_ClaimBus(flash_dev) != 0)
|
2012-01-20 14:37:47 +01:00
|
|
|
return -1;
|
|
|
|
|
2012-01-25 07:23:24 +01:00
|
|
|
uint8_t out[] = {JEDEC_WRITE_ENABLE};
|
2012-01-20 14:37:47 +01:00
|
|
|
PIOS_SPI_TransferBlock(flash_dev->spi_id,out,NULL,sizeof(out),NULL);
|
2012-12-12 04:36:18 +01:00
|
|
|
PIOS_Flash_Jedec_ReleaseBus(flash_dev);
|
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
return 0;
|
2011-01-24 08:52:14 +01:00
|
|
|
}
|
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
/**
|
2012-12-12 04:36:18 +01:00
|
|
|
* @brief Read the status register from flash chip and return it
|
2011-02-01 03:18:04 +01:00
|
|
|
*/
|
2012-12-12 04:36:18 +01:00
|
|
|
static int32_t PIOS_Flash_Jedec_ReadStatus(struct jedec_flash_dev * flash_dev)
|
2011-02-01 03:18:04 +01:00
|
|
|
{
|
2012-12-12 04:36:18 +01:00
|
|
|
if (PIOS_Flash_Jedec_ClaimBus(flash_dev) < 0)
|
2012-01-20 14:37:47 +01:00
|
|
|
return -1;
|
2012-01-25 07:23:24 +01:00
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
uint8_t out[2] = {JEDEC_READ_STATUS, 0};
|
|
|
|
uint8_t in[2] = {0,0};
|
|
|
|
if (PIOS_SPI_TransferBlock(flash_dev->spi_id,out,in,sizeof(out),NULL) < 0) {
|
|
|
|
PIOS_Flash_Jedec_ReleaseBus(flash_dev);
|
|
|
|
return -2;
|
|
|
|
}
|
2012-01-25 07:23:24 +01:00
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
PIOS_Flash_Jedec_ReleaseBus(flash_dev);
|
2011-02-12 23:19:50 +01:00
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
return in[1];
|
2011-02-01 03:18:04 +01:00
|
|
|
}
|
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
/**
|
|
|
|
* @brief Read the status register from flash chip and return it
|
|
|
|
*/
|
|
|
|
static int32_t PIOS_Flash_Jedec_ReadID(struct jedec_flash_dev * flash_dev)
|
|
|
|
{
|
|
|
|
if (PIOS_Flash_Jedec_ClaimBus(flash_dev) < 0)
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
uint8_t out[] = {JEDEC_DEVICE_ID, 0, 0, 0};
|
|
|
|
uint8_t in[4];
|
|
|
|
if (PIOS_SPI_TransferBlock(flash_dev->spi_id,out,in,sizeof(out),NULL) < 0) {
|
|
|
|
PIOS_Flash_Jedec_ReleaseBus(flash_dev);
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
PIOS_Flash_Jedec_ReleaseBus(flash_dev);
|
|
|
|
|
2012-12-15 05:16:31 +01:00
|
|
|
flash_dev->manufacturer = in[1];
|
|
|
|
flash_dev->memorytype = in[2];
|
|
|
|
flash_dev->capacity = in[3];
|
2012-12-12 04:36:18 +01:00
|
|
|
|
2012-12-15 05:16:31 +01:00
|
|
|
return flash_dev->manufacturer;
|
2012-12-12 04:36:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************
|
|
|
|
*
|
|
|
|
* Provide a PIOS flash driver API
|
|
|
|
*
|
|
|
|
*********************************/
|
|
|
|
#include "pios_flash.h"
|
|
|
|
|
|
|
|
#if FLASH_USE_FREERTOS_LOCKS
|
|
|
|
|
2012-01-26 18:35:43 +01:00
|
|
|
/**
|
|
|
|
* @brief Grab the semaphore to perform a transaction
|
|
|
|
* @return 0 for success, -1 for timeout
|
|
|
|
*/
|
2012-12-15 05:13:33 +01:00
|
|
|
static int32_t PIOS_Flash_Jedec_StartTransaction(uintptr_t flash_id)
|
2012-01-26 18:35:43 +01:00
|
|
|
{
|
2012-12-12 04:36:18 +01:00
|
|
|
struct jedec_flash_dev * flash_dev = (struct jedec_flash_dev *)flash_id;
|
|
|
|
|
2012-12-15 05:13:33 +01:00
|
|
|
if (PIOS_Flash_Jedec_Validate(flash_dev) != 0)
|
2012-01-26 18:35:43 +01:00
|
|
|
return -1;
|
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
#if defined(PIOS_INCLUDE_FREERTOS)
|
2012-12-15 05:13:33 +01:00
|
|
|
if (xSemaphoreTake(flash_dev->transaction_lock, portMAX_DELAY) != pdTRUE)
|
2012-12-12 04:36:18 +01:00
|
|
|
return -2;
|
2012-01-26 18:50:05 +01:00
|
|
|
#endif
|
2012-12-12 04:36:18 +01:00
|
|
|
|
2012-01-26 18:35:43 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Release the semaphore to perform a transaction
|
|
|
|
* @return 0 for success, -1 for timeout
|
|
|
|
*/
|
2012-12-15 05:13:33 +01:00
|
|
|
static int32_t PIOS_Flash_Jedec_EndTransaction(uintptr_t flash_id)
|
2012-01-26 18:35:43 +01:00
|
|
|
{
|
2012-12-12 04:36:18 +01:00
|
|
|
struct jedec_flash_dev * flash_dev = (struct jedec_flash_dev *)flash_id;
|
|
|
|
|
2012-12-15 05:13:33 +01:00
|
|
|
if (PIOS_Flash_Jedec_Validate(flash_dev) != 0)
|
2012-01-26 18:35:43 +01:00
|
|
|
return -1;
|
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
#if defined(PIOS_INCLUDE_FREERTOS)
|
2012-12-15 05:13:33 +01:00
|
|
|
if (xSemaphoreGive(flash_dev->transaction_lock) != pdTRUE)
|
2012-12-12 04:36:18 +01:00
|
|
|
return -2;
|
2012-01-26 18:50:05 +01:00
|
|
|
#endif
|
2012-12-12 04:36:18 +01:00
|
|
|
|
2012-01-26 18:35:43 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2011-02-01 03:18:04 +01:00
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
#else /* FLASH_USE_FREERTOS_LOCKS */
|
2012-01-20 14:37:47 +01:00
|
|
|
|
2012-12-15 05:13:33 +01:00
|
|
|
static int32_t PIOS_Flash_Jedec_StartTransaction(uintptr_t flash_id)
|
2012-12-12 04:36:18 +01:00
|
|
|
{
|
|
|
|
return 0;
|
2012-12-15 05:13:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t PIOS_Flash_Jedec_EndTransaction(uintptr_t flash_id)
|
2011-01-25 18:46:27 +01:00
|
|
|
{
|
2012-12-12 04:36:18 +01:00
|
|
|
return 0;
|
2012-12-15 05:13:33 +01:00
|
|
|
}
|
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
#endif /* FLASH_USE_FREERTOS_LOCKS */
|
2011-01-25 18:46:27 +01:00
|
|
|
|
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
|
|
|
*/
|
2012-12-15 05:13:33 +01:00
|
|
|
static int32_t PIOS_Flash_Jedec_EraseSector(uintptr_t flash_id, uint32_t addr)
|
2011-01-24 08:52:14 +01:00
|
|
|
{
|
2012-12-12 04:36:18 +01:00
|
|
|
struct jedec_flash_dev * flash_dev = (struct jedec_flash_dev *)flash_id;
|
|
|
|
|
2012-12-15 05:13:33 +01:00
|
|
|
if (PIOS_Flash_Jedec_Validate(flash_dev) != 0)
|
2012-01-20 14:37:47 +01:00
|
|
|
return -1;
|
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
uint8_t ret;
|
2012-01-25 07:23:24 +01:00
|
|
|
uint8_t out[] = {flash_dev->cfg->sector_erase, (addr >> 16) & 0xff, (addr >> 8) & 0xff , addr & 0xff};
|
2011-07-16 01:06:18 +02:00
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
if ((ret = PIOS_Flash_Jedec_WriteEnable(flash_dev)) != 0)
|
2011-02-01 03:18:04 +01:00
|
|
|
return ret;
|
2011-07-16 01:06:18 +02:00
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
if (PIOS_Flash_Jedec_ClaimBus(flash_dev) != 0)
|
2011-07-16 01:06:18 +02:00
|
|
|
return -1;
|
2012-12-15 05:13:33 +01:00
|
|
|
|
|
|
|
if (PIOS_SPI_TransferBlock(flash_dev->spi_id,out,NULL,sizeof(out),NULL) < 0) {
|
2012-12-12 04:36:18 +01:00
|
|
|
PIOS_Flash_Jedec_ReleaseBus(flash_dev);
|
2012-01-20 14:37:47 +01:00
|
|
|
return -2;
|
|
|
|
}
|
2012-12-15 05:13:33 +01:00
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
PIOS_Flash_Jedec_ReleaseBus(flash_dev);
|
2011-02-01 03:18:04 +01:00
|
|
|
|
2012-01-20 14:37:47 +01:00
|
|
|
// Keep polling when bus is busy too
|
2012-12-12 04:36:18 +01:00
|
|
|
while (PIOS_Flash_Jedec_Busy(flash_dev) != 0) {
|
2012-01-26 18:50:05 +01:00
|
|
|
#if defined(FLASH_FREERTOS)
|
2012-01-26 18:26:19 +01:00
|
|
|
vTaskDelay(1);
|
2012-01-26 18:50:05 +01:00
|
|
|
#endif
|
|
|
|
}
|
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
|
|
|
|
*/
|
2012-12-15 05:13:33 +01:00
|
|
|
static int32_t PIOS_Flash_Jedec_EraseChip(uintptr_t flash_id)
|
2011-02-01 03:18:08 +01:00
|
|
|
{
|
2012-12-12 04:36:18 +01:00
|
|
|
struct jedec_flash_dev * flash_dev = (struct jedec_flash_dev *)flash_id;
|
|
|
|
|
2012-12-15 05:13:33 +01:00
|
|
|
if (PIOS_Flash_Jedec_Validate(flash_dev) != 0)
|
2012-01-20 14:37:47 +01:00
|
|
|
return -1;
|
|
|
|
|
2011-02-01 03:18:08 +01:00
|
|
|
uint8_t ret;
|
2012-01-25 07:23:24 +01:00
|
|
|
uint8_t out[] = {flash_dev->cfg->chip_erase};
|
2011-07-16 01:06:18 +02:00
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
if ((ret = PIOS_Flash_Jedec_WriteEnable(flash_dev)) != 0)
|
2011-02-01 03:18:08 +01:00
|
|
|
return ret;
|
2011-07-16 01:06:18 +02:00
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
if (PIOS_Flash_Jedec_ClaimBus(flash_dev) != 0)
|
2011-02-01 03:18:08 +01:00
|
|
|
return -1;
|
2012-12-15 05:13:33 +01:00
|
|
|
|
|
|
|
if (PIOS_SPI_TransferBlock(flash_dev->spi_id,out,NULL,sizeof(out),NULL) < 0) {
|
2012-12-12 04:36:18 +01:00
|
|
|
PIOS_Flash_Jedec_ReleaseBus(flash_dev);
|
2012-01-20 14:37:47 +01:00
|
|
|
return -2;
|
|
|
|
}
|
2012-12-15 05:13:33 +01:00
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
PIOS_Flash_Jedec_ReleaseBus(flash_dev);
|
2011-07-16 01:06:18 +02:00
|
|
|
|
2012-01-20 14:37:47 +01:00
|
|
|
// Keep polling when bus is busy too
|
2012-06-11 18:48:05 +02:00
|
|
|
int i = 0;
|
2012-12-12 04:36:18 +01:00
|
|
|
while (PIOS_Flash_Jedec_Busy(flash_dev) != 0) {
|
2012-06-24 18:23:34 +02:00
|
|
|
#if defined(FLASH_FREERTOS)
|
2012-01-26 18:26:19 +01:00
|
|
|
vTaskDelay(1);
|
2012-07-28 18:22:24 +02:00
|
|
|
if ((i++) % 100 == 0)
|
|
|
|
#else
|
2012-06-24 18:23:34 +02:00
|
|
|
if ((i++) % 10000 == 0)
|
2012-07-28 18:22:24 +02:00
|
|
|
#endif
|
|
|
|
|
2012-06-11 18:48:05 +02:00
|
|
|
PIOS_LED_Toggle(PIOS_LED_HEARTBEAT);
|
2012-12-15 05:13:33 +01:00
|
|
|
|
2012-01-26 18:50:05 +01:00
|
|
|
}
|
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
|
|
|
|
*/
|
2012-12-15 05:13:33 +01:00
|
|
|
static int32_t PIOS_Flash_Jedec_WriteData(uintptr_t flash_id, uint32_t addr, uint8_t * data, uint16_t len)
|
2011-01-24 08:52:14 +01:00
|
|
|
{
|
2012-12-12 04:36:18 +01:00
|
|
|
struct jedec_flash_dev * flash_dev = (struct jedec_flash_dev *)flash_id;
|
|
|
|
|
2012-01-25 07:23:24 +01:00
|
|
|
if(PIOS_Flash_Jedec_Validate(flash_dev) != 0)
|
2012-01-20 14:37:47 +01:00
|
|
|
return -1;
|
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
uint8_t ret;
|
2012-01-25 07:23:24 +01:00
|
|
|
uint8_t out[4] = {JEDEC_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 */
|
2012-12-15 05:13:33 +01: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 */
|
2012-12-15 05:13:33 +01:00
|
|
|
if (((addr & 0xff) + len) > 0x100)
|
2011-02-01 03:18:04 +01:00
|
|
|
return -3;
|
2011-07-16 01:06:18 +02:00
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
if ((ret = PIOS_Flash_Jedec_WriteEnable(flash_dev)) != 0)
|
2011-02-01 03:18:04 +01:00
|
|
|
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 */
|
2012-12-12 04:36:18 +01:00
|
|
|
if (PIOS_Flash_Jedec_ClaimBus(flash_dev) != 0)
|
2011-02-01 03:18:04 +01:00
|
|
|
return -1;
|
2012-12-15 05:13:33 +01:00
|
|
|
|
|
|
|
if (PIOS_SPI_TransferBlock(flash_dev->spi_id,out,NULL,sizeof(out),NULL) < 0) {
|
2012-12-12 04:36:18 +01:00
|
|
|
PIOS_Flash_Jedec_ReleaseBus(flash_dev);
|
2012-01-20 14:37:47 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2011-07-16 01:06:18 +02:00
|
|
|
|
2011-02-01 03:18:04 +01:00
|
|
|
/* Clock out data to flash */
|
2012-12-15 05:13:33 +01:00
|
|
|
if (PIOS_SPI_TransferBlock(flash_dev->spi_id,data,NULL,len,NULL) < 0) {
|
2012-12-12 04:36:18 +01:00
|
|
|
PIOS_Flash_Jedec_ReleaseBus(flash_dev);
|
2012-01-20 14:37:47 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2011-02-01 03:18:04 +01:00
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
PIOS_Flash_Jedec_ReleaseBus(flash_dev);
|
2011-02-01 03:18:04 +01:00
|
|
|
|
2012-01-26 18:26:19 +01:00
|
|
|
// Keep polling when bus is busy too
|
2012-01-26 18:50:05 +01:00
|
|
|
#if defined(FLASH_FREERTOS)
|
2012-12-12 04:36:18 +01:00
|
|
|
while (PIOS_Flash_Jedec_Busy(flash_dev) != 0) {
|
2012-01-26 18:26:19 +01:00
|
|
|
vTaskDelay(1);
|
2012-01-26 19:39:35 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
|
|
|
// Query status this way to prevent accel chip locking us out
|
2012-12-12 04:36:18 +01:00
|
|
|
if (PIOS_Flash_Jedec_ClaimBus(flash_dev) < 0)
|
2012-01-26 19:39:35 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
PIOS_SPI_TransferByte(flash_dev->spi_id, JEDEC_READ_STATUS);
|
2012-12-15 05:13:33 +01:00
|
|
|
while (PIOS_SPI_TransferByte(flash_dev->spi_id, JEDEC_READ_STATUS) & JEDEC_STATUS_BUSY);
|
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
PIOS_Flash_Jedec_ReleaseBus(flash_dev);
|
2012-01-26 19:39:35 +01:00
|
|
|
|
2012-01-26 18:50:05 +01:00
|
|
|
#endif
|
2012-01-26 19:39:35 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Write multiple chunks of data in one transaction
|
|
|
|
* @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
|
|
|
|
*/
|
2012-12-15 05:13:33 +01:00
|
|
|
static int32_t PIOS_Flash_Jedec_WriteChunks(uintptr_t flash_id, uint32_t addr, struct pios_flash_chunk chunks[], uint32_t num)
|
2012-01-26 19:39:35 +01:00
|
|
|
{
|
2012-12-12 04:36:18 +01:00
|
|
|
struct jedec_flash_dev * flash_dev = (struct jedec_flash_dev *)flash_id;
|
|
|
|
|
2012-12-15 05:13:33 +01:00
|
|
|
if (PIOS_Flash_Jedec_Validate(flash_dev) != 0)
|
2012-01-26 19:39:35 +01:00
|
|
|
return -1;
|
2012-12-15 05:13:33 +01:00
|
|
|
|
2012-01-26 19:39:35 +01:00
|
|
|
uint8_t ret;
|
|
|
|
uint8_t out[4] = {JEDEC_PAGE_WRITE, (addr >> 16) & 0xff, (addr >> 8) & 0xff , addr & 0xff};
|
2012-12-15 05:13:33 +01:00
|
|
|
|
2012-01-26 19:39:35 +01:00
|
|
|
/* Can only write one page at a time */
|
|
|
|
uint32_t len = 0;
|
2012-12-15 05:13:33 +01:00
|
|
|
for (uint32_t i = 0; i < num; i++)
|
2012-12-12 04:36:18 +01:00
|
|
|
len += chunks[i].len;
|
2012-01-26 19:39:35 +01:00
|
|
|
|
2012-12-15 05:13:33 +01:00
|
|
|
if (len > 0x100)
|
2012-01-26 19:39:35 +01:00
|
|
|
return -2;
|
2012-12-15 05:13:33 +01:00
|
|
|
|
2012-01-26 19:39:35 +01:00
|
|
|
/* Ensure number of bytes fits after starting address before end of page */
|
2012-12-15 05:13:33 +01:00
|
|
|
if (((addr & 0xff) + len) > 0x100)
|
2012-01-26 19:39:35 +01:00
|
|
|
return -3;
|
2012-12-15 05:13:33 +01:00
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
if ((ret = PIOS_Flash_Jedec_WriteEnable(flash_dev)) != 0)
|
2012-01-26 19:39:35 +01:00
|
|
|
return ret;
|
2012-12-15 05:13:33 +01:00
|
|
|
|
2012-01-26 19:39:35 +01:00
|
|
|
/* Execute write page command and clock in address. Keep CS asserted */
|
2012-12-12 04:36:18 +01:00
|
|
|
if (PIOS_Flash_Jedec_ClaimBus(flash_dev) != 0)
|
2012-01-26 19:39:35 +01:00
|
|
|
return -1;
|
2012-12-15 05:13:33 +01:00
|
|
|
|
|
|
|
if (PIOS_SPI_TransferBlock(flash_dev->spi_id,out,NULL,sizeof(out),NULL) < 0) {
|
2012-12-12 04:36:18 +01:00
|
|
|
PIOS_Flash_Jedec_ReleaseBus(flash_dev);
|
2012-01-26 19:39:35 +01:00
|
|
|
return -1;
|
2012-01-26 18:50:05 +01:00
|
|
|
}
|
2012-12-15 05:13:33 +01:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < num; i++) {
|
2012-12-12 04:36:18 +01:00
|
|
|
struct pios_flash_chunk * chunk = &chunks[i];
|
2012-12-15 05:13:33 +01:00
|
|
|
|
2012-01-26 19:39:35 +01:00
|
|
|
/* Clock out data to flash */
|
2012-12-15 05:13:33 +01:00
|
|
|
if (PIOS_SPI_TransferBlock(flash_dev->spi_id,chunk->addr,NULL,chunk->len,NULL) < 0) {
|
2012-12-12 04:36:18 +01:00
|
|
|
PIOS_Flash_Jedec_ReleaseBus(flash_dev);
|
2012-01-26 19:39:35 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-12-12 04:36:18 +01:00
|
|
|
PIOS_Flash_Jedec_ReleaseBus(flash_dev);
|
2012-01-26 19:39:35 +01:00
|
|
|
|
|
|
|
// Skip checking for busy with this to get OS running again fast
|
|
|
|
|
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
|
|
|
|
*/
|
2012-12-15 05:13:33 +01:00
|
|
|
static int32_t PIOS_Flash_Jedec_ReadData(uintptr_t flash_id, uint32_t addr, uint8_t * data, uint16_t len)
|
2011-02-01 03:18:04 +01:00
|
|
|
{
|
2012-12-12 04:36:18 +01:00
|
|
|
struct jedec_flash_dev * flash_dev = (struct jedec_flash_dev *)flash_id;
|
|
|
|
|
2012-12-15 05:13:33 +01:00
|
|
|
if (PIOS_Flash_Jedec_Validate(flash_dev) != 0)
|
2012-01-20 14:37:47 +01:00
|
|
|
return -1;
|
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
if (PIOS_Flash_Jedec_ClaimBus(flash_dev) == -1)
|
2011-02-01 03:18:04 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Execute read command and clock in address. Keep CS asserted */
|
2012-01-25 07:23:24 +01:00
|
|
|
uint8_t out[] = {JEDEC_READ_DATA, (addr >> 16) & 0xff, (addr >> 8) & 0xff , addr & 0xff};
|
2012-12-15 05:13:33 +01:00
|
|
|
|
|
|
|
if (PIOS_SPI_TransferBlock(flash_dev->spi_id,out,NULL,sizeof(out),NULL) < 0) {
|
2012-12-12 04:36:18 +01:00
|
|
|
PIOS_Flash_Jedec_ReleaseBus(flash_dev);
|
2012-01-20 14:37:47 +01:00
|
|
|
return -2;
|
|
|
|
}
|
2011-02-01 03:18:04 +01:00
|
|
|
|
|
|
|
/* Copy the transfer data to the buffer */
|
2012-12-15 05:13:33 +01:00
|
|
|
if (PIOS_SPI_TransferBlock(flash_dev->spi_id,NULL,data,len,NULL) < 0) {
|
2012-12-12 04:36:18 +01:00
|
|
|
PIOS_Flash_Jedec_ReleaseBus(flash_dev);
|
2012-01-20 14:37:47 +01:00
|
|
|
return -3;
|
|
|
|
}
|
2011-02-01 03:18:04 +01:00
|
|
|
|
2012-12-12 04:36:18 +01:00
|
|
|
PIOS_Flash_Jedec_ReleaseBus(flash_dev);
|
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
|
|
|
}
|
2012-12-12 04:36:18 +01:00
|
|
|
|
|
|
|
/* Provide a flash driver to external drivers */
|
|
|
|
const struct pios_flash_driver pios_jedec_flash_driver = {
|
|
|
|
.start_transaction = PIOS_Flash_Jedec_StartTransaction,
|
|
|
|
.end_transaction = PIOS_Flash_Jedec_EndTransaction,
|
|
|
|
.erase_chip = PIOS_Flash_Jedec_EraseChip,
|
|
|
|
.erase_sector = PIOS_Flash_Jedec_EraseSector,
|
|
|
|
.write_chunks = PIOS_Flash_Jedec_WriteChunks,
|
|
|
|
.write_data = PIOS_Flash_Jedec_WriteData,
|
|
|
|
.read_data = PIOS_Flash_Jedec_ReadData,
|
|
|
|
};
|
|
|
|
|