Updated to match API changes in mbed OS

pull/7774/head
Christopher Haster 2017-03-07 17:27:46 -06:00
parent 18005bcc56
commit 6e3dcd9103
3 changed files with 35 additions and 51 deletions

View File

@ -55,7 +55,7 @@ SPIFBlockDevice::SPIFBlockDevice(
_spi.frequency(freq);
}
bd_error_t SPIFBlockDevice::init()
int SPIFBlockDevice::init()
{
// Check for vendor specific hacks, these should move into more general
// handling when possible. RDID is not used to verify a device is attached.
@ -72,9 +72,9 @@ bd_error_t SPIFBlockDevice::init()
}
// Check that device is doing ok
bd_error_t err = _sync();
int err = _sync();
if (err) {
return BD_ERROR_NO_DEVICE;
return BD_ERROR_DEVICE_ERROR;
}
// Check JEDEC serial flash discoverable parameters for device
@ -128,7 +128,7 @@ bd_error_t SPIFBlockDevice::init()
return 0;
}
bd_error_t SPIFBlockDevice::deinit()
int SPIFBlockDevice::deinit()
{
// Latch write disable just to keep noise
// from changing the device
@ -209,7 +209,7 @@ void SPIFBlockDevice::_cmdwrite(
}
}
bd_error_t SPIFBlockDevice::_sync()
int SPIFBlockDevice::_sync()
{
for (int i = 0; i < SPIF_TIMEOUT; i++) {
// Read status register until write not-in-progress
@ -227,7 +227,7 @@ bd_error_t SPIFBlockDevice::_sync()
return BD_ERROR_DEVICE_ERROR;
}
bd_error_t SPIFBlockDevice::_wren()
int SPIFBlockDevice::_wren()
{
_cmdwrite(SPIF_WREN, 0, 0, 0x0, NULL);
@ -247,26 +247,22 @@ bd_error_t SPIFBlockDevice::_wren()
return BD_ERROR_DEVICE_ERROR;
}
bd_error_t SPIFBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
int SPIFBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
{
// Check the address and size fit onto the chip.
if (!is_valid_read(addr, size)) {
return BD_ERROR_PARAMETER;
}
MBED_ASSERT(is_valid_read(addr, size));
_cmdread(SPIF_READ, 3, size, addr, static_cast<uint8_t *>(buffer));
return 0;
}
bd_error_t SPIFBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size)
int SPIFBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size)
{
// Check the address and size fit onto the chip.
if (!is_valid_program(addr, size)) {
return BD_ERROR_PARAMETER;
}
MBED_ASSERT(is_valid_program(addr, size));
while (size > 0) {
bd_error_t err = _wren();
int err = _wren();
if (err) {
return err;
}
@ -291,15 +287,13 @@ bd_error_t SPIFBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_
return 0;
}
bd_error_t SPIFBlockDevice::erase(bd_addr_t addr, bd_size_t size)
int SPIFBlockDevice::erase(bd_addr_t addr, bd_size_t size)
{
// Check the address and size fit onto the chip.
if (!is_valid_erase(addr, size)) {
return BD_ERROR_PARAMETER;
}
MBED_ASSERT(is_valid_erase(addr, size));
while (size > 0) {
bd_error_t err = _wren();
int err = _wren();
if (err) {
return err;
}
@ -320,22 +314,22 @@ bd_error_t SPIFBlockDevice::erase(bd_addr_t addr, bd_size_t size)
return 0;
}
bd_size_t SPIFBlockDevice::get_read_size()
bd_size_t SPIFBlockDevice::get_read_size() const
{
return SPIF_READ_SIZE;
}
bd_size_t SPIFBlockDevice::get_program_size()
bd_size_t SPIFBlockDevice::get_program_size() const
{
return SPIF_PROG_SIZE;
}
bd_size_t SPIFBlockDevice::get_erase_size()
bd_size_t SPIFBlockDevice::get_erase_size() const
{
return SPIF_SE_SIZE;
}
bd_size_t SPIFBlockDevice::size()
bd_size_t SPIFBlockDevice::size() const
{
return _size;
}

View File

@ -16,9 +16,6 @@
#ifndef MBED_SPIF_BLOCK_DEVICE_H
#define MBED_SPIF_BLOCK_DEVICE_H
/* If the target has no SPI support then SPIF is not supported */
#ifdef DEVICE_SPI
#include <mbed.h>
#include "BlockDevice.h"
@ -67,13 +64,13 @@ public:
*
* @return 0 on success or a negative error code on failure
*/
virtual bd_error_t init();
virtual int init();
/** Deinitialize a block device
*
* @return 0 on success or a negative error code on failure
*/
virtual bd_error_t deinit();
virtual int deinit();
/** Read blocks from a block device
*
@ -82,7 +79,7 @@ public:
* @param size Size to read in bytes, must be a multiple of read block size
* @return 0 on success, negative error code on failure
*/
virtual bd_error_t read(void *buffer, bd_addr_t addr, bd_size_t size);
virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
/** Program blocks to a block device
*
@ -93,7 +90,7 @@ public:
* @param size Size to write in bytes, must be a multiple of program block size
* @return 0 on success, negative error code on failure
*/
virtual bd_error_t program(const void *buffer, bd_addr_t addr, bd_size_t size);
virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
/** Erase blocks on a block device
*
@ -103,33 +100,33 @@ public:
* @param size Size to erase in bytes, must be a multiple of erase block size
* @return 0 on success, negative error code on failure
*/
virtual bd_error_t erase(bd_addr_t addr, bd_size_t size);
virtual int erase(bd_addr_t addr, bd_size_t size);
/** Get the size of a readable block
*
* @return Size of a readable block in bytes
*/
virtual bd_size_t get_read_size();
virtual bd_size_t get_read_size() const;
/** Get the size of a programable block
*
* @return Size of a programable block in bytes
* @note Must be a multiple of the read size
*/
virtual bd_size_t get_program_size();
virtual bd_size_t get_program_size() const;
/** Get the size of a eraseable block
*
* @return Size of a eraseable block in bytes
* @note Must be a multiple of the program size
*/
virtual bd_size_t get_erase_size();
virtual bd_size_t get_erase_size() const;
/** Get the total size of the underlying device
*
* @return Size of the underlying device in bytes
*/
virtual bd_size_t size();
virtual bd_size_t size() const;
private:
// Master side hardware
@ -140,8 +137,8 @@ private:
bd_size_t _size;
// Internal functions
bd_error_t _wren();
bd_error_t _sync();
int _wren();
int _sync();
void _cmdread(uint8_t op, uint32_t addrc, uint32_t retc,
uint32_t addr, uint8_t *rets);
void _cmdwrite(uint8_t op, uint32_t addrc, uint32_t argc,
@ -149,6 +146,4 @@ private:
};
#endif /* DEVICE_SPI */
#endif /* MBED_SPIF_BLOCK_DEVICE_H */

View File

@ -8,14 +8,6 @@
using namespace utest::v1;
#ifndef SPIF_INSTALLED
#define SPIF_INSTALLED defined(TARGET_K82F)
#endif
#if !SPIF_INSTALLED
#error [NOT_SUPPORTED] SPIF Required
#endif
#if defined(TARGET_K82F)
#define TEST_PINS PTE2, PTE4, PTE1, PTE5
#define TEST_FREQ 40000000
@ -29,7 +21,7 @@ using namespace utest::v1;
const struct {
const char *name;
bd_size_t (BlockDevice::*method)();
bd_size_t (BlockDevice::*method)() const;
} ATTRS[] = {
{"read size", &BlockDevice::get_read_size},
{"program size", &BlockDevice::get_program_size},
@ -60,7 +52,7 @@ void test_read_write() {
uint8_t *write_block = new uint8_t[block_size];
uint8_t *read_block = new uint8_t[block_size];
uint8_t *error_mask = new uint8_t[TEST_ERROR_MASK];
unsigned addrwidth = ceil(log(bd.size()-1) / log(16))+1;
unsigned addrwidth = ceil(log(float(bd.size()-1)) / log(float(16)))+1;
for (int b = 0; b < TEST_BLOCK_COUNT; b++) {
// Find a random block
@ -79,7 +71,10 @@ void test_read_write() {
// Write, sync, and read the block
printf("test %0*llx:%llu...\n", addrwidth, block, block_size);
err = bd.write(write_block, block, block_size);
err = bd.erase(block, block_size);
TEST_ASSERT_EQUAL(0, err);
err = bd.program(write_block, block, block_size);
TEST_ASSERT_EQUAL(0, err);
printf("write %0*llx:%llu ", addrwidth, block, block_size);