hackrf/firmware/common/w25q80bv.c
Jared Boone 4ac2be5304 W25Q80BV: Extract hardware-specific code into separate layer.
Conflicts:
	firmware/hackrf_usb/Makefile
2014-11-10 16:45:23 -08:00

203 lines
4.8 KiB
C

/*
* Copyright 2013 Michael Ossmann
* Copyright 2013 Benjamin Vernoux
* Copyright 2014 Jared Boone, ShareBrained Technology
*
* This file is part of HackRF.
*
* 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 2, or (at your option)
* any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
/*
* This is a rudimentary driver for the W25Q80BV SPI Flash IC using the
* LPC43xx's SSP0 peripheral (not quad SPIFI). The only goal here is to allow
* programming the flash.
*/
#include <stdint.h>
#include "w25q80bv.h"
#include "w25q80bv_drv.h"
/*
* Set up pins for GPIO and SPI control, configure SSP0 peripheral for SPI.
* SSP0_SSEL is controlled by GPIO in order to handle various transfer lengths.
*/
void w25q80bv_setup(void)
{
uint8_t device_id;
w25q80bv_spi_init();
device_id = 0;
while(device_id != W25Q80BV_DEVICE_ID_RES)
{
device_id = w25q80bv_get_device_id();
}
}
uint8_t w25q80bv_get_status(void)
{
uint8_t value;
w25q80bv_spi_select();
w25q80bv_spi_transfer(W25Q80BV_READ_STATUS1);
value = w25q80bv_spi_transfer(0xFF);
w25q80bv_spi_unselect();
return value;
}
/* Release power down / Device ID */
uint8_t w25q80bv_get_device_id(void)
{
uint8_t value;
w25q80bv_spi_select();
w25q80bv_spi_transfer(W25Q80BV_DEVICE_ID);
/* Read 3 dummy bytes */
value = w25q80bv_spi_transfer(0xFF);
value = w25q80bv_spi_transfer(0xFF);
value = w25q80bv_spi_transfer(0xFF);
/* Read Device ID shall return 0x13 for W25Q80BV */
value = w25q80bv_spi_transfer(0xFF);
w25q80bv_spi_unselect();
return value;
}
void w25q80bv_get_unique_id(w25q80bv_unique_id_t* unique_id)
{
int i;
uint8_t value;
w25q80bv_spi_select();
w25q80bv_spi_transfer(W25Q80BV_UNIQUE_ID);
/* Read 4 dummy bytes */
for(i=0; i<4; i++)
value = w25q80bv_spi_transfer(0xFF);
/* Read Unique ID 64bits (8*8) */
for(i=0; i<8; i++)
{
value = w25q80bv_spi_transfer(0xFF);
unique_id->id_8b[i] = value;
}
w25q80bv_spi_unselect();
}
void w25q80bv_wait_while_busy(void)
{
while (w25q80bv_get_status() & W25Q80BV_STATUS_BUSY);
}
void w25q80bv_write_enable(void)
{
w25q80bv_wait_while_busy();
w25q80bv_spi_select();
w25q80bv_spi_transfer(W25Q80BV_WRITE_ENABLE);
w25q80bv_spi_unselect();
}
void w25q80bv_chip_erase(void)
{
uint8_t device_id;
device_id = 0;
while(device_id != W25Q80BV_DEVICE_ID_RES)
{
device_id = w25q80bv_get_device_id();
}
w25q80bv_write_enable();
w25q80bv_wait_while_busy();
w25q80bv_spi_select();
w25q80bv_spi_transfer(W25Q80BV_CHIP_ERASE);
w25q80bv_spi_unselect();
}
/* write up a 256 byte page or partial page */
void w25q80bv_page_program(const uint32_t addr, const uint16_t len, const uint8_t* data)
{
int i;
/* do nothing if asked to write beyond a page boundary */
if (((addr & 0xFF) + len) > W25Q80BV_PAGE_LEN)
return;
/* do nothing if we would overflow the flash */
if (addr > (W25Q80BV_NUM_BYTES - len))
return;
w25q80bv_write_enable();
w25q80bv_wait_while_busy();
w25q80bv_spi_select();
w25q80bv_spi_transfer(W25Q80BV_PAGE_PROGRAM);
w25q80bv_spi_transfer((addr & 0xFF0000) >> 16);
w25q80bv_spi_transfer((addr & 0xFF00) >> 8);
w25q80bv_spi_transfer(addr & 0xFF);
for (i = 0; i < len; i++)
w25q80bv_spi_transfer(data[i]);
w25q80bv_spi_unselect();
}
/* write an arbitrary number of bytes */
void w25q80bv_program(uint32_t addr, uint32_t len, const uint8_t* data)
{
uint16_t first_block_len;
uint8_t device_id;
device_id = 0;
while(device_id != W25Q80BV_DEVICE_ID_RES)
{
device_id = w25q80bv_get_device_id();
}
/* do nothing if we would overflow the flash */
if ((len > W25Q80BV_NUM_BYTES) || (addr > W25Q80BV_NUM_BYTES)
|| ((addr + len) > W25Q80BV_NUM_BYTES))
return;
/* handle start not at page boundary */
first_block_len = W25Q80BV_PAGE_LEN - (addr % W25Q80BV_PAGE_LEN);
if (len < first_block_len)
first_block_len = len;
if (first_block_len) {
w25q80bv_page_program(addr, first_block_len, data);
addr += first_block_len;
data += first_block_len;
len -= first_block_len;
}
/* one page at a time on boundaries */
while (len >= W25Q80BV_PAGE_LEN) {
w25q80bv_page_program(addr, W25Q80BV_PAGE_LEN, data);
addr += W25Q80BV_PAGE_LEN;
data += W25Q80BV_PAGE_LEN;
len -= W25Q80BV_PAGE_LEN;
}
/* handle end not at page boundary */
if (len) {
w25q80bv_page_program(addr, len, data);
}
}