2015-02-25 12:11:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the copyright holder nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* \addtogroup sensortag-cc26xx-ext-flash
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* \file
|
2016-07-17 00:56:35 +00:00
|
|
|
* Sensortag/LaunchPad External Flash Driver
|
2015-02-25 12:11:09 +00:00
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "ext-flash.h"
|
2017-11-20 17:02:37 +00:00
|
|
|
#include "spi-hal.h"
|
|
|
|
#include "gpio-hal.h"
|
|
|
|
#include "sys/log.h"
|
2016-05-14 19:22:11 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2015-02-25 12:11:09 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-11-20 17:02:37 +00:00
|
|
|
#ifndef EXT_FLASH_SPI_CONTROLLER
|
|
|
|
|
|
|
|
#define EXT_FLASH_SPI_CONTROLLER 0xFF /* No controller */
|
|
|
|
|
|
|
|
#define EXT_FLASH_SPI_PIN_SCK GPIO_HAL_PIN_UNKNOWN
|
|
|
|
#define EXT_FLASH_SPI_PIN_MOSI GPIO_HAL_PIN_UNKNOWN
|
|
|
|
#define EXT_FLASH_SPI_PIN_MISO GPIO_HAL_PIN_UNKNOWN
|
|
|
|
#define EXT_FLASH_SPI_PIN_CS GPIO_HAL_PIN_UNKNOWN
|
|
|
|
|
|
|
|
#define EXT_FLASH_DEVICE_ID 0xFF
|
|
|
|
#define EXT_FLASH_MID 0xFF
|
|
|
|
|
|
|
|
#define EXT_FLASH_PROGRAM_PAGE_SIZE 256
|
|
|
|
#define EXT_FLASH_ERASE_SECTOR_SIZE 4096
|
|
|
|
|
|
|
|
#endif /* EXT_FLASH_SPI_CONTROLLER */
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Log configuration */
|
|
|
|
#define LOG_MODULE "ext-flash"
|
|
|
|
#define LOG_LEVEL LOG_LEVEL_NONE
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-02-25 12:11:09 +00:00
|
|
|
/* Instruction codes */
|
|
|
|
|
|
|
|
#define BLS_CODE_PROGRAM 0x02 /**< Page Program */
|
|
|
|
#define BLS_CODE_READ 0x03 /**< Read Data */
|
|
|
|
#define BLS_CODE_READ_STATUS 0x05 /**< Read Status Register */
|
|
|
|
#define BLS_CODE_WRITE_ENABLE 0x06 /**< Write Enable */
|
|
|
|
#define BLS_CODE_SECTOR_ERASE 0x20 /**< Sector Erase */
|
|
|
|
#define BLS_CODE_MDID 0x90 /**< Manufacturer Device ID */
|
|
|
|
|
2015-08-16 16:24:05 +00:00
|
|
|
#define BLS_CODE_PD 0xB9 /**< Power down */
|
|
|
|
#define BLS_CODE_RPD 0xAB /**< Release Power-Down */
|
2015-02-25 12:11:09 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Erase instructions */
|
|
|
|
|
|
|
|
#define BLS_CODE_ERASE_4K 0x20 /**< Sector Erase */
|
|
|
|
#define BLS_CODE_ERASE_32K 0x52
|
|
|
|
#define BLS_CODE_ERASE_64K 0xD8
|
|
|
|
#define BLS_CODE_ERASE_ALL 0xC7 /**< Mass Erase */
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Bitmasks of the status register */
|
|
|
|
|
|
|
|
#define BLS_STATUS_SRWD_BM 0x80
|
|
|
|
#define BLS_STATUS_BP_BM 0x0C
|
|
|
|
#define BLS_STATUS_WEL_BM 0x02
|
|
|
|
#define BLS_STATUS_WIP_BM 0x01
|
|
|
|
|
|
|
|
#define BLS_STATUS_BIT_BUSY 0x01 /**< Busy bit of the status register */
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-11-20 17:02:37 +00:00
|
|
|
#define VERIFY_PART_LOCKED -2
|
2015-08-16 16:24:05 +00:00
|
|
|
#define VERIFY_PART_ERROR -1
|
|
|
|
#define VERIFY_PART_POWERED_DOWN 0
|
|
|
|
#define VERIFY_PART_OK 1
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-11-20 17:02:37 +00:00
|
|
|
static spi_device_t flash_spi_configuration_default = {
|
|
|
|
.spi_controller = EXT_FLASH_SPI_CONTROLLER,
|
|
|
|
.pin_spi_sck = EXT_FLASH_SPI_PIN_SCK,
|
|
|
|
.pin_spi_miso = EXT_FLASH_SPI_PIN_MISO,
|
|
|
|
.pin_spi_mosi = EXT_FLASH_SPI_PIN_MOSI,
|
|
|
|
.pin_spi_cs = EXT_FLASH_SPI_PIN_CS,
|
|
|
|
.spi_bit_rate = 4000000,
|
|
|
|
.spi_pha = 0,
|
|
|
|
.spi_pol = 0
|
|
|
|
};
|
|
|
|
|
|
|
|
static spi_device_t *flash_spi_configuration;
|
2015-02-25 12:11:09 +00:00
|
|
|
/**
|
|
|
|
* Clear external flash CSN line
|
|
|
|
*/
|
2017-11-20 17:02:37 +00:00
|
|
|
static bool
|
2015-12-27 13:48:26 +00:00
|
|
|
select_on_bus(void)
|
2015-02-25 12:11:09 +00:00
|
|
|
{
|
2017-11-20 17:02:37 +00:00
|
|
|
if(spi_select(flash_spi_configuration) == SPI_DEV_STATUS_OK) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-02-25 12:11:09 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* Set external flash CSN line
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
deselect(void)
|
|
|
|
{
|
2017-11-20 17:02:37 +00:00
|
|
|
spi_deselect(flash_spi_configuration);
|
2015-02-25 12:11:09 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* \brief Wait till previous erase/program operation completes.
|
2015-08-16 16:24:05 +00:00
|
|
|
* \return True when successful.
|
2015-02-25 12:11:09 +00:00
|
|
|
*/
|
2015-08-16 16:24:05 +00:00
|
|
|
static bool
|
2015-02-25 12:11:09 +00:00
|
|
|
wait_ready(void)
|
|
|
|
{
|
2015-08-16 16:24:05 +00:00
|
|
|
bool ret;
|
2015-02-25 12:11:09 +00:00
|
|
|
const uint8_t wbuf[1] = { BLS_CODE_READ_STATUS };
|
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
if(select_on_bus() == false) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-25 12:11:09 +00:00
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
ret = spi_write(flash_spi_configuration, wbuf, sizeof(wbuf));
|
2015-02-25 12:11:09 +00:00
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
if(ret != SPI_DEV_STATUS_OK) {
|
2015-02-25 12:11:09 +00:00
|
|
|
deselect();
|
2015-08-16 16:24:05 +00:00
|
|
|
return false;
|
2015-02-25 12:11:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
uint8_t buf;
|
|
|
|
/* Note that this temporary implementation is not
|
|
|
|
* energy efficient.
|
|
|
|
* Thread could have yielded while waiting for flash
|
|
|
|
* erase/program to complete.
|
|
|
|
*/
|
2017-11-20 17:02:37 +00:00
|
|
|
ret = spi_read(flash_spi_configuration, &buf, sizeof(buf));
|
2015-02-25 12:11:09 +00:00
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
if(ret != SPI_DEV_STATUS_OK) {
|
2015-02-25 12:11:09 +00:00
|
|
|
/* Error */
|
|
|
|
deselect();
|
2015-08-16 16:24:05 +00:00
|
|
|
return false;
|
2015-02-25 12:11:09 +00:00
|
|
|
}
|
2017-11-20 17:02:37 +00:00
|
|
|
|
2015-02-25 12:11:09 +00:00
|
|
|
if(!(buf & BLS_STATUS_BIT_BUSY)) {
|
|
|
|
/* Now ready */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deselect();
|
2015-08-16 16:24:05 +00:00
|
|
|
return true;
|
2015-02-25 12:11:09 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-05-01 15:23:15 +00:00
|
|
|
/**
|
|
|
|
* \brief Verify the flash part.
|
2015-08-16 16:24:05 +00:00
|
|
|
* \retval VERIFY_PART_OK The part was identified successfully
|
|
|
|
* \retval VERIFY_PART_ERROR There was an error communicating with the part
|
|
|
|
* \retval VERIFY_PART_POWERED_DOWN Communication was successful, but the part
|
|
|
|
* was powered down
|
2015-05-01 15:23:15 +00:00
|
|
|
*/
|
2015-08-16 16:24:05 +00:00
|
|
|
static uint8_t
|
2015-05-01 15:23:15 +00:00
|
|
|
verify_part(void)
|
|
|
|
{
|
|
|
|
const uint8_t wbuf[] = { BLS_CODE_MDID, 0xFF, 0xFF, 0x00 };
|
2016-01-24 16:52:56 +00:00
|
|
|
uint8_t rbuf[2] = { 0, 0 };
|
2015-08-16 16:24:05 +00:00
|
|
|
bool ret;
|
2015-05-01 15:23:15 +00:00
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
if(select_on_bus() == false) {
|
|
|
|
return VERIFY_PART_LOCKED;
|
|
|
|
}
|
2015-05-01 15:23:15 +00:00
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
if(spi_write(flash_spi_configuration, wbuf, sizeof(wbuf)) != SPI_DEV_STATUS_OK) {
|
2015-05-01 15:23:15 +00:00
|
|
|
deselect();
|
2015-08-16 16:24:05 +00:00
|
|
|
return VERIFY_PART_ERROR;
|
2015-05-01 15:23:15 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
ret = spi_read(flash_spi_configuration, rbuf, sizeof(rbuf));
|
2015-05-01 15:23:15 +00:00
|
|
|
deselect();
|
2017-11-20 17:02:37 +00:00
|
|
|
if(ret != SPI_DEV_STATUS_OK) {
|
2015-08-16 16:24:05 +00:00
|
|
|
return VERIFY_PART_ERROR;
|
2015-05-01 15:23:15 +00:00
|
|
|
}
|
2015-08-16 16:24:05 +00:00
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
LOG_DBG("Verify: %02x %02x\n", rbuf[0], rbuf[1]);
|
|
|
|
|
|
|
|
if(rbuf[0] != EXT_FLASH_MID || rbuf[1] != EXT_FLASH_DEVICE_ID) {
|
2015-08-16 16:24:05 +00:00
|
|
|
return VERIFY_PART_POWERED_DOWN;
|
|
|
|
}
|
|
|
|
return VERIFY_PART_OK;
|
2015-05-01 15:23:15 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-02-25 12:11:09 +00:00
|
|
|
/**
|
|
|
|
* \brief Put the device in power save mode. No access to data; only
|
|
|
|
* the status register is accessible.
|
|
|
|
*/
|
2017-11-20 17:02:37 +00:00
|
|
|
static bool
|
2015-02-25 12:11:09 +00:00
|
|
|
power_down(void)
|
|
|
|
{
|
|
|
|
uint8_t cmd;
|
2015-05-01 15:23:15 +00:00
|
|
|
uint8_t i;
|
2015-02-25 12:11:09 +00:00
|
|
|
|
2015-08-16 16:24:05 +00:00
|
|
|
/* First, wait for the device to be ready */
|
|
|
|
if(wait_ready() == false) {
|
|
|
|
/* Entering here will leave the device in standby instead of powerdown */
|
2017-11-20 17:02:37 +00:00
|
|
|
return false;
|
2015-08-16 16:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd = BLS_CODE_PD;
|
2017-11-20 17:02:37 +00:00
|
|
|
if(select_on_bus() == false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(spi_write_byte(flash_spi_configuration, cmd) != SPI_DEV_STATUS_OK) {
|
|
|
|
deselect();
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-25 12:11:09 +00:00
|
|
|
deselect();
|
|
|
|
|
2015-05-01 15:23:15 +00:00
|
|
|
i = 0;
|
|
|
|
while(i < 10) {
|
2015-08-16 16:24:05 +00:00
|
|
|
if(verify_part() == VERIFY_PART_POWERED_DOWN) {
|
|
|
|
/* Device is powered down */
|
2017-11-20 17:02:37 +00:00
|
|
|
return true;
|
2015-05-01 15:23:15 +00:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Should not be required */
|
|
|
|
deselect();
|
2017-11-20 17:02:37 +00:00
|
|
|
return false;
|
2015-02-25 12:11:09 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* \brief Take device out of power save mode and prepare it for normal operation
|
|
|
|
* \return True if the command was written successfully
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
power_standby(void)
|
|
|
|
{
|
|
|
|
uint8_t cmd;
|
|
|
|
bool success;
|
|
|
|
|
2015-08-16 16:24:05 +00:00
|
|
|
cmd = BLS_CODE_RPD;
|
2017-11-20 17:02:37 +00:00
|
|
|
if(select_on_bus() == false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
success = (spi_write(flash_spi_configuration, &cmd, sizeof(cmd)) == SPI_DEV_STATUS_OK);
|
2015-02-25 12:11:09 +00:00
|
|
|
|
|
|
|
if(success) {
|
2015-08-16 16:24:05 +00:00
|
|
|
success = wait_ready() == true ? true : false;
|
2015-02-25 12:11:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
deselect();
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* \brief Enable write.
|
2015-08-16 16:24:05 +00:00
|
|
|
* \return True when successful.
|
2015-02-25 12:11:09 +00:00
|
|
|
*/
|
2015-08-16 16:24:05 +00:00
|
|
|
static bool
|
2015-02-25 12:11:09 +00:00
|
|
|
write_enable(void)
|
|
|
|
{
|
2015-08-16 16:24:05 +00:00
|
|
|
bool ret;
|
2015-02-25 12:11:09 +00:00
|
|
|
const uint8_t wbuf[] = { BLS_CODE_WRITE_ENABLE };
|
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
if(select_on_bus() == false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = (spi_write(flash_spi_configuration, wbuf, sizeof(wbuf)) == SPI_DEV_STATUS_OK);
|
2015-02-25 12:11:09 +00:00
|
|
|
deselect();
|
|
|
|
|
2015-08-16 16:24:05 +00:00
|
|
|
if(ret == false) {
|
|
|
|
return false;
|
2015-02-25 12:11:09 +00:00
|
|
|
}
|
2015-08-16 16:24:05 +00:00
|
|
|
return true;
|
2015-02-25 12:11:09 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
bool
|
|
|
|
ext_flash_open()
|
|
|
|
{
|
2017-11-20 17:02:37 +00:00
|
|
|
/* Check if platform has ext-flash */
|
|
|
|
if(flash_spi_configuration->pin_spi_sck == 255) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-25 12:11:09 +00:00
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
if(spi_acquire(flash_spi_configuration) != SPI_DEV_STATUS_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-25 12:11:09 +00:00
|
|
|
/* Default output to clear chip select */
|
|
|
|
deselect();
|
|
|
|
|
|
|
|
/* Put the part is standby mode */
|
|
|
|
power_standby();
|
|
|
|
|
2015-08-16 16:24:05 +00:00
|
|
|
return verify_part() == VERIFY_PART_OK ? true : false;
|
2015-02-25 12:11:09 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-11-20 17:02:37 +00:00
|
|
|
bool
|
2015-02-25 12:11:09 +00:00
|
|
|
ext_flash_close()
|
|
|
|
{
|
|
|
|
/* Put the part in low power mode */
|
2017-11-20 17:02:37 +00:00
|
|
|
if(power_down() == false) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-25 12:11:09 +00:00
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
if(spi_release(flash_spi_configuration) != SPI_DEV_STATUS_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-02-25 12:11:09 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
bool
|
2017-11-20 17:02:37 +00:00
|
|
|
ext_flash_read(uint32_t offset, uint32_t length, uint8_t *buf)
|
2015-02-25 12:11:09 +00:00
|
|
|
{
|
|
|
|
uint8_t wbuf[4];
|
2017-11-20 17:02:37 +00:00
|
|
|
bool ret;
|
2015-02-25 12:11:09 +00:00
|
|
|
|
|
|
|
/* Wait till previous erase/program operation completes */
|
2017-11-20 17:02:37 +00:00
|
|
|
if(wait_ready() == false) {
|
2015-02-25 12:11:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SPI is driven with very low frequency (1MHz < 33MHz fR spec)
|
|
|
|
* in this implementation, hence it is not necessary to use fast read.
|
|
|
|
*/
|
|
|
|
wbuf[0] = BLS_CODE_READ;
|
|
|
|
wbuf[1] = (offset >> 16) & 0xff;
|
|
|
|
wbuf[2] = (offset >> 8) & 0xff;
|
|
|
|
wbuf[3] = offset & 0xff;
|
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
if(select_on_bus() == false) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-25 12:11:09 +00:00
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
if(spi_write(flash_spi_configuration, wbuf, sizeof(wbuf)) != SPI_DEV_STATUS_OK) {
|
2015-02-25 12:11:09 +00:00
|
|
|
/* failure */
|
|
|
|
deselect();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
ret = (spi_read(flash_spi_configuration, buf, length) == SPI_DEV_STATUS_OK);
|
2015-02-25 12:11:09 +00:00
|
|
|
|
|
|
|
deselect();
|
|
|
|
|
2015-08-16 16:24:05 +00:00
|
|
|
return ret;
|
2015-02-25 12:11:09 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
bool
|
2017-11-20 17:02:37 +00:00
|
|
|
ext_flash_write(uint32_t offset, uint32_t length, const uint8_t *buf)
|
2015-02-25 12:11:09 +00:00
|
|
|
{
|
|
|
|
uint8_t wbuf[4];
|
2017-11-20 17:02:37 +00:00
|
|
|
uint32_t ilen; /* interim length per instruction */
|
2015-02-25 12:11:09 +00:00
|
|
|
|
|
|
|
while(length > 0) {
|
|
|
|
/* Wait till previous erase/program operation completes */
|
2017-11-20 17:02:37 +00:00
|
|
|
if(wait_ready() == false) {
|
2015-02-25 12:11:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
if(write_enable() == false) {
|
2015-02-25 12:11:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
ilen = EXT_FLASH_PROGRAM_PAGE_SIZE - (offset % EXT_FLASH_PROGRAM_PAGE_SIZE);
|
2015-02-25 12:11:09 +00:00
|
|
|
if(length < ilen) {
|
|
|
|
ilen = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
wbuf[0] = BLS_CODE_PROGRAM;
|
|
|
|
wbuf[1] = (offset >> 16) & 0xff;
|
|
|
|
wbuf[2] = (offset >> 8) & 0xff;
|
|
|
|
wbuf[3] = offset & 0xff;
|
|
|
|
|
|
|
|
offset += ilen;
|
|
|
|
length -= ilen;
|
|
|
|
|
|
|
|
/* Upto 100ns CS hold time (which is not clear
|
|
|
|
* whether it's application only inbetween reads)
|
|
|
|
* is not imposed here since above instructions
|
|
|
|
* should be enough to delay
|
|
|
|
* as much. */
|
2017-11-20 17:02:37 +00:00
|
|
|
if(select_on_bus() == false) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-25 12:11:09 +00:00
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
if(spi_write(flash_spi_configuration, wbuf, sizeof(wbuf)) != SPI_DEV_STATUS_OK) {
|
2015-02-25 12:11:09 +00:00
|
|
|
/* failure */
|
|
|
|
deselect();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
if(spi_write(flash_spi_configuration, buf, ilen) != SPI_DEV_STATUS_OK) {
|
2015-02-25 12:11:09 +00:00
|
|
|
/* failure */
|
|
|
|
deselect();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
buf += ilen;
|
|
|
|
deselect();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
bool
|
2017-11-20 17:02:37 +00:00
|
|
|
ext_flash_erase(uint32_t offset, uint32_t length)
|
2015-02-25 12:11:09 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Note that Block erase might be more efficient when the floor map
|
|
|
|
* is well planned for OTA, but to simplify this implementation,
|
|
|
|
* sector erase is used blindly.
|
|
|
|
*/
|
|
|
|
uint8_t wbuf[4];
|
2017-11-20 17:02:37 +00:00
|
|
|
uint32_t i, numsectors;
|
|
|
|
uint32_t endoffset = offset + length - 1;
|
2015-02-25 12:11:09 +00:00
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
offset = (offset / EXT_FLASH_ERASE_SECTOR_SIZE) * EXT_FLASH_ERASE_SECTOR_SIZE;
|
|
|
|
numsectors = (endoffset - offset + EXT_FLASH_ERASE_SECTOR_SIZE - 1) / EXT_FLASH_ERASE_SECTOR_SIZE;
|
2015-02-25 12:11:09 +00:00
|
|
|
|
|
|
|
wbuf[0] = BLS_CODE_SECTOR_ERASE;
|
|
|
|
|
|
|
|
for(i = 0; i < numsectors; i++) {
|
|
|
|
/* Wait till previous erase/program operation completes */
|
2017-11-20 17:02:37 +00:00
|
|
|
if(wait_ready() == false) {
|
2015-02-25 12:11:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
if(write_enable() == false) {
|
2015-02-25 12:11:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
wbuf[1] = (offset >> 16) & 0xff;
|
|
|
|
wbuf[2] = (offset >> 8) & 0xff;
|
|
|
|
wbuf[3] = offset & 0xff;
|
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
if(select_on_bus() == false) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-25 12:11:09 +00:00
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
if(spi_write(flash_spi_configuration, wbuf, sizeof(wbuf)) != SPI_DEV_STATUS_OK) {
|
2015-02-25 12:11:09 +00:00
|
|
|
/* failure */
|
|
|
|
deselect();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
deselect();
|
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
offset += EXT_FLASH_ERASE_SECTOR_SIZE;
|
2015-02-25 12:11:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
bool
|
|
|
|
ext_flash_test(void)
|
|
|
|
{
|
2017-11-20 17:02:37 +00:00
|
|
|
flash_spi_configuration = &flash_spi_configuration_default;
|
|
|
|
|
|
|
|
if(ext_flash_open() == false) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-25 12:11:09 +00:00
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
if(ext_flash_close() == false) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-25 12:11:09 +00:00
|
|
|
|
2017-11-20 17:02:37 +00:00
|
|
|
LOG_INFO("Flash test successful\n");
|
|
|
|
|
|
|
|
return true;
|
2015-02-25 12:11:09 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-11-20 17:02:37 +00:00
|
|
|
bool
|
|
|
|
ext_flash_init(spi_device_t *conf)
|
2015-05-01 15:23:15 +00:00
|
|
|
{
|
2017-11-20 17:02:37 +00:00
|
|
|
if(conf == NULL) {
|
|
|
|
flash_spi_configuration = &flash_spi_configuration_default;
|
|
|
|
} else {
|
|
|
|
flash_spi_configuration = conf;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ext_flash_test();
|
2015-05-01 15:23:15 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-02-25 12:11:09 +00:00
|
|
|
/** @} */
|