mirror of https://github.com/ARMmbed/mbed-os.git
Merge pull request #20 from ARMmbed/offir-spif-block-device
Extending SPIF BD SFDP supportpull/7774/head
commit
a821b69cd9
1146
SPIFBlockDevice.cpp
1146
SPIFBlockDevice.cpp
File diff suppressed because it is too large
Load Diff
|
@ -1,5 +1,5 @@
|
||||||
/* mbed Microcontroller Library
|
/* mbed Microcontroller Library
|
||||||
* Copyright (c) 2016 ARM Limited
|
* Copyright (c) 2018 ARM Limited
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -16,41 +16,58 @@
|
||||||
#ifndef MBED_SPIF_BLOCK_DEVICE_H
|
#ifndef MBED_SPIF_BLOCK_DEVICE_H
|
||||||
#define MBED_SPIF_BLOCK_DEVICE_H
|
#define MBED_SPIF_BLOCK_DEVICE_H
|
||||||
|
|
||||||
#include <mbed.h>
|
#include "SPI.h"
|
||||||
|
#include "DigitalOut.h"
|
||||||
#include "BlockDevice.h"
|
#include "BlockDevice.h"
|
||||||
|
|
||||||
|
namespace mbed {
|
||||||
/** BlockDevice for SPI based flash devices
|
|
||||||
* such as the MX25R or SST26F016B
|
/** Enum spif standard error codes
|
||||||
|
*
|
||||||
|
* @enum qpif_bd_error
|
||||||
|
*/
|
||||||
|
enum spif_bd_error {
|
||||||
|
SPIF_BD_ERROR_OK = 0, /*!< no error */
|
||||||
|
SPIF_BD_ERROR_DEVICE_ERROR = BD_ERROR_DEVICE_ERROR, /*!< device specific error -4001 */
|
||||||
|
SPIF_BD_ERROR_PARSING_FAILED = -4002, /* SFDP Parsing failed */
|
||||||
|
SPIF_BD_ERROR_READY_FAILED = -4003, /* Wait for Mem Ready failed */
|
||||||
|
SPIF_BD_ERROR_WREN_FAILED = -4004, /* Write Enable Failed */
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#define SPIF_MAX_REGIONS 10
|
||||||
|
#define MAX_NUM_OF_ERASE_TYPES 4
|
||||||
|
|
||||||
|
/** BlockDevice for SFDP based flash devices over SPI bus
|
||||||
*
|
*
|
||||||
* @code
|
* @code
|
||||||
* // Here's an example using the MX25R SPI flash device on the K82F
|
* // Here's an example using SPI flash device on K82F target
|
||||||
* #include "mbed.h"
|
* #include "mbed.h"
|
||||||
* #include "SPIFBlockDevice.h"
|
* #include "SPIFBlockDevice.h"
|
||||||
*
|
*
|
||||||
* // Create flash device on SPI bus with PTE5 as chip select
|
* // Create flash device on SPI bus with PTE5 as chip select
|
||||||
* SPIFBlockDevice spif(PTE2, PTE4, PTE1, PTE5);
|
* SPIFBlockDevice spif(PTE2, PTE4, PTE1, PTE5);
|
||||||
*
|
*
|
||||||
* int main() {
|
* int main() {
|
||||||
* printf("spif test\n");
|
* printf("spif test\n");
|
||||||
*
|
*
|
||||||
* // Initialize the SPI flash device and print the memory layout
|
* // Initialize the SPI flash device and print the memory layout
|
||||||
* spif.init();
|
* spif.init();
|
||||||
* printf("spif size: %llu\n", spif.size());
|
* printf("spif size: %llu\n", spif.size());
|
||||||
* printf("spif read size: %llu\n", spif.get_read_size());
|
* printf("spif read size: %llu\n", spif.get_read_size());
|
||||||
* printf("spif program size: %llu\n", spif.get_program_size());
|
* printf("spif program size: %llu\n", spif.get_program_size());
|
||||||
* printf("spif erase size: %llu\n", spif.get_erase_size());
|
* printf("spif erase size: %llu\n", spif.get_erase_size());
|
||||||
*
|
*
|
||||||
* // Write "Hello World!" to the first block
|
* // Write "Hello World!" to the first block
|
||||||
* char *buffer = (char*)malloc(spif.get_erase_size());
|
* char *buffer = (char*)malloc(spif.get_erase_size());
|
||||||
* sprintf(buffer, "Hello World!\n");
|
* sprintf(buffer, "Hello World!\n");
|
||||||
* spif.erase(0, spif.get_erase_size());
|
* spif.erase(0, spif.get_erase_size());
|
||||||
* spif.program(buffer, 0, spif.get_erase_size());
|
* spif.program(buffer, 0, spif.get_erase_size());
|
||||||
*
|
*
|
||||||
* // Read back what was stored
|
* // Read back what was stored
|
||||||
* spif.read(buffer, 0, spif.get_erase_size());
|
* spif.read(buffer, 0, spif.get_erase_size());
|
||||||
* printf("%s", buffer);
|
* printf("%s", buffer);
|
||||||
*
|
*
|
||||||
* // Deinitialize the device
|
* // Deinitialize the device
|
||||||
* spif.deinit();
|
* spif.deinit();
|
||||||
* }
|
* }
|
||||||
|
@ -66,26 +83,34 @@ public:
|
||||||
* @param csel SPI chip select pin
|
* @param csel SPI chip select pin
|
||||||
* @param freq Clock speed of the SPI bus (defaults to 40MHz)
|
* @param freq Clock speed of the SPI bus (defaults to 40MHz)
|
||||||
*/
|
*/
|
||||||
SPIFBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName csel, int freq=40000000);
|
SPIFBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName csel, int freq = 40000000);
|
||||||
|
|
||||||
/** Initialize a block device
|
/** Initialize a block device
|
||||||
*
|
*
|
||||||
* @return 0 on success or a negative error code on failure
|
* @return SPIF_BD_ERROR_OK(0) - success
|
||||||
|
* SPIF_BD_ERROR_DEVICE_ERROR - device driver transaction failed
|
||||||
|
* SPIF_BD_ERROR_READY_FAILED - Waiting for Memory ready failed or timedout
|
||||||
|
* SPIF_BD_ERROR_PARSING_FAILED - unexpected format or values in one of the SFDP tables
|
||||||
*/
|
*/
|
||||||
virtual int init();
|
virtual int init();
|
||||||
|
|
||||||
/** Deinitialize a block device
|
/** Deinitialize a block device
|
||||||
*
|
*
|
||||||
* @return 0 on success or a negative error code on failure
|
* @return SPIF_BD_ERROR_OK(0) - success
|
||||||
*/
|
*/
|
||||||
virtual int deinit();
|
virtual int deinit();
|
||||||
|
|
||||||
|
/** Desctruct SPIFBlockDevie
|
||||||
|
*/
|
||||||
|
~SPIFBlockDevice() {deinit();}
|
||||||
|
|
||||||
/** Read blocks from a block device
|
/** Read blocks from a block device
|
||||||
*
|
*
|
||||||
* @param buffer Buffer to write blocks to
|
* @param buffer Buffer to write blocks to
|
||||||
* @param addr Address of block to begin reading from
|
* @param addr Address of block to begin reading from
|
||||||
* @param size Size to read in bytes, must be a multiple of read block size
|
* @param size Size to read in bytes, must be a multiple of read block size
|
||||||
* @return 0 on success, negative error code on failure
|
* @return SPIF_BD_ERROR_OK(0) - success
|
||||||
|
* SPIF_BD_ERROR_DEVICE_ERROR - device driver transaction failed
|
||||||
*/
|
*/
|
||||||
virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
|
virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
|
||||||
|
|
||||||
|
@ -96,7 +121,10 @@ public:
|
||||||
* @param buffer Buffer of data to write to blocks
|
* @param buffer Buffer of data to write to blocks
|
||||||
* @param addr Address of block to begin writing to
|
* @param addr Address of block to begin writing to
|
||||||
* @param size Size to write in bytes, must be a multiple of program block size
|
* @param size Size to write in bytes, must be a multiple of program block size
|
||||||
* @return 0 on success, negative error code on failure
|
* @return SPIF_BD_ERROR_OK(0) - success
|
||||||
|
* SPIF_BD_ERROR_DEVICE_ERROR - device driver transaction failed
|
||||||
|
* SPIF_BD_ERROR_READY_FAILED - Waiting for Memory ready failed or timed out
|
||||||
|
* SPIF_BD_ERROR_WREN_FAILED - Write Enable failed
|
||||||
*/
|
*/
|
||||||
virtual int 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);
|
||||||
|
|
||||||
|
@ -106,7 +134,10 @@ public:
|
||||||
*
|
*
|
||||||
* @param addr Address of block to begin erasing
|
* @param addr Address of block to begin erasing
|
||||||
* @param size Size to erase in bytes, must be a multiple of erase block size
|
* @param size Size to erase in bytes, must be a multiple of erase block size
|
||||||
* @return 0 on success, negative error code on failure
|
* @return SPIF_BD_ERROR_OK(0) - success
|
||||||
|
* SPIF_BD_ERROR_DEVICE_ERROR - device driver transaction failed
|
||||||
|
* SPIF_BD_ERROR_READY_FAILED - Waiting for Memory ready failed or timed out
|
||||||
|
* SPIF_BD_ERROR_WREN_FAILED - Write Enable failed
|
||||||
*/
|
*/
|
||||||
virtual int erase(bd_addr_t addr, bd_size_t size);
|
virtual int erase(bd_addr_t addr, bd_size_t size);
|
||||||
|
|
||||||
|
@ -130,15 +161,15 @@ public:
|
||||||
*/
|
*/
|
||||||
virtual bd_size_t get_erase_size() const;
|
virtual bd_size_t get_erase_size() const;
|
||||||
|
|
||||||
/** Get the size of an erasable block given address
|
/** Get the size of minimal eraseable sector size of given address
|
||||||
*
|
*
|
||||||
* @param addr Address within the erasable block
|
* @param addr Any address within block queried for erase sector size (can be any address within flash size offset)
|
||||||
* @return Size of an erasable block in bytes
|
* @return Size of minimal erase sector size, in given address region, in bytes
|
||||||
* @note Must be a multiple of the program size
|
* @note Must be a multiple of the program size
|
||||||
*/
|
*/
|
||||||
virtual bd_size_t get_erase_size(bd_addr_t addr) const;
|
virtual bd_size_t get_erase_size(bd_addr_t addr);
|
||||||
|
|
||||||
/** Get the value of storage when erased
|
/** Get the value of storage byte after it was erased
|
||||||
*
|
*
|
||||||
* If get_erase_value returns a non-negative byte value, the underlying
|
* If get_erase_value returns a non-negative byte value, the underlying
|
||||||
* storage is set to that value when erased, and storage containing
|
* storage is set to that value when erased, and storage containing
|
||||||
|
@ -154,26 +185,115 @@ public:
|
||||||
* @return Size of the underlying device in bytes
|
* @return Size of the underlying device in bytes
|
||||||
*/
|
*/
|
||||||
virtual bd_size_t size() const;
|
virtual bd_size_t size() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
// Internal functions
|
||||||
|
|
||||||
|
/****************************************/
|
||||||
|
/* SFDP Detection and Parsing Functions */
|
||||||
|
/****************************************/
|
||||||
|
// Parse SFDP Headers and retrieve Basic Param and Sector Map Tables (if exist)
|
||||||
|
int _sfdp_parse_sfdp_headers(uint32_t& basic_table_addr, size_t& basic_table_size,
|
||||||
|
uint32_t& sector_map_table_addr, size_t& sector_map_table_size);
|
||||||
|
|
||||||
|
// Parse and Detect required Basic Parameters from Table
|
||||||
|
int _sfdp_parse_basic_param_table(uint32_t basic_table_addr, size_t basic_table_size);
|
||||||
|
|
||||||
|
// Parse and read information required by Regions Secotr Map
|
||||||
|
int _sfdp_parse_sector_map_table(uint32_t sector_map_table_addr, size_t sector_map_table_size);
|
||||||
|
|
||||||
|
// Detect fastest read Bus mode supported by device
|
||||||
|
int _sfdp_detect_best_bus_read_mode(uint8_t *basic_param_table_ptr, int basic_param_table_size, int& read_inst);
|
||||||
|
|
||||||
|
// Set Page size for program
|
||||||
|
unsigned int _sfdp_detect_page_size(uint8_t *basic_param_table_ptr, int basic_param_table_size);
|
||||||
|
|
||||||
|
// Detect all supported erase types
|
||||||
|
int _sfdp_detect_erase_types_inst_and_size(uint8_t *basic_param_table_ptr, int basic_param_table_size,
|
||||||
|
int& erase4k_inst,
|
||||||
|
int *erase_type_inst_arr, unsigned int *erase_type_size_arr);
|
||||||
|
|
||||||
|
/***********************/
|
||||||
|
/* Utilities Functions */
|
||||||
|
/***********************/
|
||||||
|
// Find the region to which the given offset belong to
|
||||||
|
int _utils_find_addr_region(bd_size_t offset);
|
||||||
|
|
||||||
|
// Iterate on all supported Erase Types of the Region to which the offset belong to.
|
||||||
|
// Iterates from highest type to lowest
|
||||||
|
int _utils_iterate_next_largest_erase_type(uint8_t& bitfield, int size, int offset, int boundry);
|
||||||
|
|
||||||
|
/********************************/
|
||||||
|
/* Calls to SPI Driver APIs */
|
||||||
|
/********************************/
|
||||||
|
// Send Program => Write command to Driver
|
||||||
|
spif_bd_error _spi_send_program_command(int prog_inst, const void *buffer, bd_addr_t addr, bd_size_t size);
|
||||||
|
|
||||||
|
// Send Read command to Driver
|
||||||
|
//spif_bd_error _spi_send_read_command(uint8_t read_inst, void *buffer, bd_addr_t addr, bd_size_t size);
|
||||||
|
spif_bd_error _spi_send_read_command(int read_inst, uint8_t *buffer, bd_addr_t addr, bd_size_t size);
|
||||||
|
|
||||||
|
// Send Erase Instruction using command_transfer command to Driver
|
||||||
|
spif_bd_error _spi_send_erase_command(int erase_inst, bd_addr_t addr, bd_size_t size);
|
||||||
|
|
||||||
|
// Send Generic command_transfer command to Driver
|
||||||
|
spif_bd_error _spi_send_general_command(int instruction, bd_addr_t addr, char *tx_buffer,
|
||||||
|
size_t tx_length, char *rx_buffer, size_t rx_length);
|
||||||
|
|
||||||
|
// Send set_frequency command to Driver
|
||||||
|
spif_bd_error _spi_set_frequency(int freq);
|
||||||
|
/********************************/
|
||||||
|
|
||||||
|
// Soft Reset Flash Memory
|
||||||
|
int _reset_flash_mem();
|
||||||
|
|
||||||
|
// Configure Write Enable in Status Register
|
||||||
|
int _set_write_enable();
|
||||||
|
|
||||||
|
// Wait on status register until write not-in-progress
|
||||||
|
bool _is_mem_ready();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Master side hardware
|
// Master side hardware
|
||||||
SPI _spi;
|
SPI _spi;
|
||||||
|
// Enable CS control (low/high) for SPI driver operatios
|
||||||
DigitalOut _cs;
|
DigitalOut _cs;
|
||||||
|
|
||||||
// Device configuration discovered through sfdp
|
// Mutex is used to protect Flash device for some SPI Driver commands that must be done sequentially with no other commands in between
|
||||||
bd_size_t _size;
|
// e.g. (1)Set Write Enable, (2)Program, (3)Wait Memory Ready
|
||||||
|
static SingletonPtr<PlatformMutex> _mutex;
|
||||||
|
|
||||||
bool _is_initialized;
|
// Command Instructions
|
||||||
|
int _read_instruction;
|
||||||
|
int _prog_instruction;
|
||||||
|
int _erase_instruction;
|
||||||
|
int _erase4k_inst; // Legacy 4K erase instruction (default 0x20h)
|
||||||
|
|
||||||
|
// Up To 4 Erase Types are supported by SFDP (each with its own command Instruction and Size)
|
||||||
|
int _erase_type_inst_arr[MAX_NUM_OF_ERASE_TYPES];
|
||||||
|
unsigned int _erase_type_size_arr[MAX_NUM_OF_ERASE_TYPES];
|
||||||
|
|
||||||
|
// Sector Regions Map
|
||||||
|
int _regions_count; //number of regions
|
||||||
|
int _region_size_bytes[SPIF_MAX_REGIONS]; //regions size in bytes
|
||||||
|
bd_size_t _region_high_boundary[SPIF_MAX_REGIONS]; //region high address offset boundary
|
||||||
|
//Each Region can support a bit combination of any of the 4 Erase Types
|
||||||
|
uint8_t _region_erase_types_bitfield[SPIF_MAX_REGIONS];
|
||||||
|
unsigned int _min_common_erase_size; // minimal common erase size for all regions (0 if none exists)
|
||||||
|
|
||||||
|
unsigned int _page_size_bytes; // Page size - 256 Bytes default
|
||||||
|
bd_size_t _device_size_bytes;
|
||||||
|
|
||||||
|
// Bus configuration
|
||||||
|
unsigned int _address_size; // number of bytes for address
|
||||||
|
unsigned int _read_dummy_and_mode_cycles; // Number of Dummy and Mode Bits required by Read Bus Mode
|
||||||
|
unsigned int _write_dummy_and_mode_cycles; // Number of Dummy and Mode Bits required by Write Bus Mode
|
||||||
|
unsigned int _dummy_and_mode_cycles; // Number of Dummy and Mode Bits required by Current Bus Mode
|
||||||
uint32_t _init_ref_count;
|
uint32_t _init_ref_count;
|
||||||
|
bool _is_initialized;
|
||||||
// Internal functions
|
|
||||||
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,
|
|
||||||
uint32_t addr, const uint8_t *args);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
} //namespace mbed
|
||||||
|
|
||||||
#endif /* MBED_SPIF_BLOCK_DEVICE_H */
|
#endif /* MBED_SPIF_BLOCK_DEVICE_H */
|
||||||
|
|
|
@ -1,23 +1,30 @@
|
||||||
#include "mbed.h"
|
/* mbed Microcontroller Library
|
||||||
|
* Copyright (c) 2018 ARM Limited
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
#include "greentea-client/test_env.h"
|
#include "greentea-client/test_env.h"
|
||||||
#include "unity.h"
|
#include "unity.h"
|
||||||
#include "utest.h"
|
#include "utest.h"
|
||||||
|
|
||||||
#include "SPIFBlockDevice.h"
|
#include "SPIFBlockDevice.h"
|
||||||
|
#include "mbed_trace.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
using namespace utest::v1;
|
using namespace utest::v1;
|
||||||
|
|
||||||
#if defined(TARGET_K82F)
|
|
||||||
#define TEST_PINS PTE2, PTE4, PTE1, PTE5
|
|
||||||
#define TEST_FREQ 40000000
|
|
||||||
#else
|
|
||||||
#define TEST_PINS D11, D12, D13, D10
|
|
||||||
#define TEST_FREQ 1000000
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define TEST_BLOCK_COUNT 10
|
#define TEST_BLOCK_COUNT 10
|
||||||
#define TEST_ERROR_MASK 16
|
#define TEST_ERROR_MASK 16
|
||||||
|
#define SPIF_TEST_NUM_OF_THREADS 5
|
||||||
|
|
||||||
const struct {
|
const struct {
|
||||||
const char *name;
|
const char *name;
|
||||||
|
@ -29,34 +36,133 @@ const struct {
|
||||||
{"total size", &BlockDevice::size},
|
{"total size", &BlockDevice::size},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static SingletonPtr<PlatformMutex> _mutex;
|
||||||
|
|
||||||
void test_read_write() {
|
// Mutex is protecting rand() per srand for buffer writing and verification.
|
||||||
SPIFBlockDevice bd(TEST_PINS, TEST_FREQ);
|
// Mutex is also protecting printouts for clear logs.
|
||||||
|
// Mutex is NOT protecting Block Device actions: erase/program/read - which is the purpose of the multithreaded test!
|
||||||
|
void basic_erase_program_read_test(SPIFBlockDevice& blockD, bd_size_t block_size, uint8_t *write_block,
|
||||||
|
uint8_t *read_block, unsigned addrwidth)
|
||||||
|
{
|
||||||
|
int err = 0;
|
||||||
|
_mutex->lock();
|
||||||
|
// Find a random block
|
||||||
|
bd_addr_t block = (rand() * block_size) % blockD.size();
|
||||||
|
|
||||||
int err = bd.init();
|
// Use next random number as temporary seed to keep
|
||||||
|
// the address progressing in the pseudorandom sequence
|
||||||
|
unsigned seed = rand();
|
||||||
|
|
||||||
|
// Fill with random sequence
|
||||||
|
srand(seed);
|
||||||
|
for (bd_size_t i_ind = 0; i_ind < block_size; i_ind++) {
|
||||||
|
write_block[i_ind] = 0xff & rand();
|
||||||
|
}
|
||||||
|
// Write, sync, and read the block
|
||||||
|
utest_printf("\ntest %0*llx:%llu...", addrwidth, block, block_size);
|
||||||
|
_mutex->unlock();
|
||||||
|
|
||||||
|
err = blockD.erase(block, block_size);
|
||||||
TEST_ASSERT_EQUAL(0, err);
|
TEST_ASSERT_EQUAL(0, err);
|
||||||
|
|
||||||
for (unsigned a = 0; a < sizeof(ATTRS)/sizeof(ATTRS[0]); a++) {
|
err = blockD.program(write_block, block, block_size);
|
||||||
|
TEST_ASSERT_EQUAL(0, err);
|
||||||
|
|
||||||
|
err = blockD.read(read_block, block, block_size);
|
||||||
|
TEST_ASSERT_EQUAL(0, err);
|
||||||
|
|
||||||
|
_mutex->lock();
|
||||||
|
// Check that the data was unmodified
|
||||||
|
srand(seed);
|
||||||
|
int val_rand;
|
||||||
|
for (bd_size_t i_ind = 0; i_ind < block_size; i_ind++) {
|
||||||
|
val_rand = rand();
|
||||||
|
if ( (0xff & val_rand) != read_block[i_ind] ) {
|
||||||
|
utest_printf("\n Assert Failed Buf Read - block:size: %llx:%llu \n", block, block_size);
|
||||||
|
utest_printf("\n pos: %llu, exp: %02x, act: %02x, wrt: %02x \n", i_ind, (0xff & val_rand), read_block[i_ind],
|
||||||
|
write_block[i_ind] );
|
||||||
|
}
|
||||||
|
TEST_ASSERT_EQUAL(0xff & val_rand, read_block[i_ind]);
|
||||||
|
}
|
||||||
|
_mutex->unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_spif_random_program_read_erase()
|
||||||
|
{
|
||||||
|
utest_printf("\nTest Random Program Read Erase Starts..\n");
|
||||||
|
|
||||||
|
SPIFBlockDevice blockD(MBED_CONF_SPIF_SPI_MOSI, MBED_CONF_SPIF_SPI_MISO, MBED_CONF_SPIF_SPI_CLK, MBED_CONF_SPIF_SPI_CS);
|
||||||
|
|
||||||
|
int err = blockD.init();
|
||||||
|
TEST_ASSERT_EQUAL(0, err);
|
||||||
|
|
||||||
|
for (unsigned atr = 0; atr < sizeof(ATTRS) / sizeof(ATTRS[0]); atr++) {
|
||||||
static const char *prefixes[] = {"", "k", "M", "G"};
|
static const char *prefixes[] = {"", "k", "M", "G"};
|
||||||
for (int i = 3; i >= 0; i--) {
|
for (int i_ind = 3; i_ind >= 0; i_ind--) {
|
||||||
bd_size_t size = (bd.*ATTRS[a].method)();
|
bd_size_t size = (blockD.*ATTRS[atr].method)();
|
||||||
if (size >= (1ULL << 10*i)) {
|
if (size >= (1ULL << 10 * i_ind)) {
|
||||||
printf("%s: %llu%sbytes (%llubytes)\n",
|
utest_printf("%s: %llu%sbytes (%llubytes)\n",
|
||||||
ATTRS[a].name, size >> 10*i, prefixes[i], size);
|
ATTRS[atr].name, size >> 10 * i_ind, prefixes[i_ind], size);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bd_size_t block_size = bd.get_erase_size();
|
bd_size_t block_size = blockD.get_erase_size();
|
||||||
uint8_t *write_block = new uint8_t[block_size];
|
unsigned addrwidth = ceil(log(float(blockD.size() - 1)) / log(float(16))) + 1;
|
||||||
uint8_t *read_block = new uint8_t[block_size];
|
|
||||||
uint8_t *error_mask = new uint8_t[TEST_ERROR_MASK];
|
uint8_t *write_block = new (std::nothrow) uint8_t[block_size];
|
||||||
unsigned addrwidth = ceil(log(float(bd.size()-1)) / log(float(16)))+1;
|
uint8_t *read_block = new (std::nothrow) uint8_t[block_size];
|
||||||
|
if (!write_block || !read_block) {
|
||||||
|
utest_printf("\n Not enough memory for test");
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
for (int b = 0; b < TEST_BLOCK_COUNT; b++) {
|
for (int b = 0; b < TEST_BLOCK_COUNT; b++) {
|
||||||
// Find a random block
|
basic_erase_program_read_test(blockD, block_size, write_block, read_block, addrwidth);
|
||||||
bd_addr_t block = (rand()*block_size) % bd.size();
|
}
|
||||||
|
|
||||||
|
err = blockD.deinit();
|
||||||
|
TEST_ASSERT_EQUAL(0, err);
|
||||||
|
|
||||||
|
end:
|
||||||
|
delete[] write_block;
|
||||||
|
delete[] read_block;
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_spif_unaligned_program()
|
||||||
|
{
|
||||||
|
utest_printf("\nTest Unaligned Program Starts..\n");
|
||||||
|
|
||||||
|
SPIFBlockDevice blockD(MBED_CONF_SPIF_SPI_MOSI, MBED_CONF_SPIF_SPI_MISO, MBED_CONF_SPIF_SPI_CLK, MBED_CONF_SPIF_SPI_CS);
|
||||||
|
|
||||||
|
int err = blockD.init();
|
||||||
|
TEST_ASSERT_EQUAL(0, err);
|
||||||
|
|
||||||
|
for (unsigned atr = 0; atr < sizeof(ATTRS) / sizeof(ATTRS[0]); atr++) {
|
||||||
|
static const char *prefixes[] = {"", "k", "M", "G"};
|
||||||
|
for (int i_ind = 3; i_ind >= 0; i_ind--) {
|
||||||
|
bd_size_t size = (blockD.*ATTRS[atr].method)();
|
||||||
|
if (size >= (1ULL << 10 * i_ind)) {
|
||||||
|
utest_printf("%s: %llu%sbytes (%llubytes)\n",
|
||||||
|
ATTRS[atr].name, size >> 10 * i_ind, prefixes[i_ind], size);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bd_size_t block_size = blockD.get_erase_size();
|
||||||
|
unsigned addrwidth = ceil(log(float(blockD.size() - 1)) / log(float(16))) + 1;
|
||||||
|
|
||||||
|
uint8_t *write_block = new (std::nothrow) uint8_t[block_size];
|
||||||
|
uint8_t *read_block = new (std::nothrow) uint8_t[block_size];
|
||||||
|
if (!write_block || !read_block ) {
|
||||||
|
utest_printf("\n Not enough memory for test");
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
bd_addr_t block = (rand() * block_size) % blockD.size() + 15;
|
||||||
|
|
||||||
// Use next random number as temporary seed to keep
|
// Use next random number as temporary seed to keep
|
||||||
// the address progressing in the pseudorandom sequence
|
// the address progressing in the pseudorandom sequence
|
||||||
|
@ -64,77 +170,121 @@ void test_read_write() {
|
||||||
|
|
||||||
// Fill with random sequence
|
// Fill with random sequence
|
||||||
srand(seed);
|
srand(seed);
|
||||||
for (bd_size_t i = 0; i < block_size; i++) {
|
for (bd_size_t i_ind = 0; i_ind < block_size; i_ind++) {
|
||||||
write_block[i] = 0xff & rand();
|
write_block[i_ind] = 0xff & rand();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write, sync, and read the block
|
// Write, sync, and read the block
|
||||||
printf("test %0*llx:%llu...\n", addrwidth, block, block_size);
|
utest_printf("\ntest %0*llx:%llu...", addrwidth, block, block_size);
|
||||||
|
|
||||||
err = bd.erase(block, block_size);
|
err = blockD.erase(block, block_size);
|
||||||
TEST_ASSERT_EQUAL(0, err);
|
TEST_ASSERT_EQUAL(0, err);
|
||||||
|
|
||||||
err = bd.program(write_block, block, block_size);
|
err = blockD.program(write_block, block, block_size);
|
||||||
TEST_ASSERT_EQUAL(0, err);
|
TEST_ASSERT_EQUAL(0, err);
|
||||||
|
|
||||||
printf("write %0*llx:%llu ", addrwidth, block, block_size);
|
err = blockD.read(read_block, block, block_size);
|
||||||
for (int i = 0; i < 16; i++) {
|
|
||||||
printf("%02x", write_block[i]);
|
|
||||||
}
|
|
||||||
printf("...\n");
|
|
||||||
|
|
||||||
err = bd.read(read_block, block, block_size);
|
|
||||||
TEST_ASSERT_EQUAL(0, err);
|
TEST_ASSERT_EQUAL(0, err);
|
||||||
|
|
||||||
printf("read %0*llx:%llu ", addrwidth, block, block_size);
|
|
||||||
for (int i = 0; i < 16; i++) {
|
|
||||||
printf("%02x", read_block[i]);
|
|
||||||
}
|
|
||||||
printf("...\n");
|
|
||||||
|
|
||||||
// Find error mask for debugging
|
|
||||||
memset(error_mask, 0, TEST_ERROR_MASK);
|
|
||||||
bd_size_t error_scale = block_size / (TEST_ERROR_MASK*8);
|
|
||||||
|
|
||||||
srand(seed);
|
|
||||||
for (bd_size_t i = 0; i < TEST_ERROR_MASK*8; i++) {
|
|
||||||
for (bd_size_t j = 0; j < error_scale; j++) {
|
|
||||||
if ((0xff & rand()) != read_block[i*error_scale + j]) {
|
|
||||||
error_mask[i/8] |= 1 << (i%8);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("error %0*llx:%llu ", addrwidth, block, block_size);
|
|
||||||
for (int i = 0; i < 16; i++) {
|
|
||||||
printf("%02x", error_mask[i]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
// Check that the data was unmodified
|
// Check that the data was unmodified
|
||||||
srand(seed);
|
srand(seed);
|
||||||
for (bd_size_t i = 0; i < block_size; i++) {
|
for (bd_size_t i_ind = 0; i_ind < block_size; i_ind++) {
|
||||||
TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
|
TEST_ASSERT_EQUAL(0xff & rand(), read_block[i_ind]);
|
||||||
|
}
|
||||||
|
|
||||||
|
err = blockD.deinit();
|
||||||
|
TEST_ASSERT_EQUAL(0, err);
|
||||||
|
}
|
||||||
|
end:
|
||||||
|
delete[] write_block;
|
||||||
|
delete[] read_block;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_spif_thread_job(void *vBlockD/*, int thread_num*/)
|
||||||
|
{
|
||||||
|
static int thread_num = 0;
|
||||||
|
thread_num++;
|
||||||
|
SPIFBlockDevice *blockD = (SPIFBlockDevice *)vBlockD;
|
||||||
|
utest_printf("\n Thread %d Started \n", thread_num);
|
||||||
|
|
||||||
|
bd_size_t block_size = blockD->get_erase_size();
|
||||||
|
unsigned addrwidth = ceil(log(float(blockD->size() - 1)) / log(float(16))) + 1;
|
||||||
|
|
||||||
|
uint8_t *write_block = new (std::nothrow) uint8_t[block_size];
|
||||||
|
uint8_t *read_block = new (std::nothrow) uint8_t[block_size];
|
||||||
|
if (!write_block || !read_block ) {
|
||||||
|
utest_printf("\n Not enough memory for test");
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int b = 0; b < TEST_BLOCK_COUNT; b++) {
|
||||||
|
basic_erase_program_read_test((*blockD), block_size, write_block, read_block, addrwidth);
|
||||||
|
}
|
||||||
|
|
||||||
|
end:
|
||||||
|
delete[] write_block;
|
||||||
|
delete[] read_block;
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_spif_multi_threads()
|
||||||
|
{
|
||||||
|
utest_printf("\nTest Multi Threaded Erase/Program/Read Starts..\n");
|
||||||
|
|
||||||
|
SPIFBlockDevice blockD(MBED_CONF_SPIF_SPI_MOSI, MBED_CONF_SPIF_SPI_MISO, MBED_CONF_SPIF_SPI_CLK, MBED_CONF_SPIF_SPI_CS);
|
||||||
|
|
||||||
|
int err = blockD.init();
|
||||||
|
TEST_ASSERT_EQUAL(0, err);
|
||||||
|
|
||||||
|
for (unsigned atr = 0; atr < sizeof(ATTRS) / sizeof(ATTRS[0]); atr++) {
|
||||||
|
static const char *prefixes[] = {"", "k", "M", "G"};
|
||||||
|
for (int i_ind = 3; i_ind >= 0; i_ind--) {
|
||||||
|
bd_size_t size = (blockD.*ATTRS[atr].method)();
|
||||||
|
if (size >= (1ULL << 10 * i_ind)) {
|
||||||
|
utest_printf("%s: %llu%sbytes (%llubytes)\n",
|
||||||
|
ATTRS[atr].name, size >> 10 * i_ind, prefixes[i_ind], size);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = bd.deinit();
|
rtos::Thread spif_bd_thread[SPIF_TEST_NUM_OF_THREADS];
|
||||||
|
|
||||||
|
osStatus threadStatus;
|
||||||
|
int i_ind;
|
||||||
|
|
||||||
|
for (i_ind = 0; i_ind < SPIF_TEST_NUM_OF_THREADS; i_ind++) {
|
||||||
|
threadStatus = spif_bd_thread[i_ind].start(test_spif_thread_job, (void *)&blockD);
|
||||||
|
if (threadStatus != 0) {
|
||||||
|
utest_printf("\n Thread %d Start Failed!", i_ind + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i_ind = 0; i_ind < SPIF_TEST_NUM_OF_THREADS; i_ind++) {
|
||||||
|
spif_bd_thread[i_ind].join();
|
||||||
|
}
|
||||||
|
|
||||||
|
err = blockD.deinit();
|
||||||
TEST_ASSERT_EQUAL(0, err);
|
TEST_ASSERT_EQUAL(0, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Test setup
|
// Test setup
|
||||||
utest::v1::status_t test_setup(const size_t number_of_cases) {
|
utest::v1::status_t test_setup(const size_t number_of_cases)
|
||||||
GREENTEA_SETUP(30, "default_auto");
|
{
|
||||||
|
GREENTEA_SETUP(60, "default_auto");
|
||||||
return verbose_test_setup_handler(number_of_cases);
|
return verbose_test_setup_handler(number_of_cases);
|
||||||
}
|
}
|
||||||
|
|
||||||
Case cases[] = {
|
Case cases[] = {
|
||||||
Case("Testing read write random blocks", test_read_write),
|
Case("Testing unaligned program blocks", test_spif_unaligned_program),
|
||||||
|
Case("Testing read write random blocks", test_spif_random_program_read_erase),
|
||||||
|
Case("Testing Multi Threads Erase Program Read", test_spif_multi_threads)
|
||||||
};
|
};
|
||||||
|
|
||||||
Specification specification(test_setup, cases);
|
Specification specification(test_setup, cases);
|
||||||
|
|
||||||
int main() {
|
int main()
|
||||||
|
{
|
||||||
|
mbed_trace_init();
|
||||||
|
utest_printf("MAIN STARTS\n");
|
||||||
return !Harness::run(specification);
|
return !Harness::run(specification);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"name": "spif-driver",
|
"name": "spif",
|
||||||
"config": {
|
"config": {
|
||||||
"SPI_MOSI": "NC",
|
"SPI_MOSI": "NC",
|
||||||
"SPI_MISO": "NC",
|
"SPI_MISO": "NC",
|
||||||
|
@ -9,10 +9,10 @@
|
||||||
},
|
},
|
||||||
"target_overrides": {
|
"target_overrides": {
|
||||||
"K82F": {
|
"K82F": {
|
||||||
"SPI_MOSI": "PTE2",
|
"SPI_MOSI": "PTE2",
|
||||||
"SPI_MISO": "PTE4",
|
"SPI_MISO": "PTE4",
|
||||||
"SPI_CLK": "PTE1",
|
"SPI_CLK": "PTE1",
|
||||||
"SPI_CS": "PTE5"
|
"SPI_CS": "PTE5"
|
||||||
},
|
},
|
||||||
"LPC54114": {
|
"LPC54114": {
|
||||||
"SPI_MOSI": "P0_20",
|
"SPI_MOSI": "P0_20",
|
||||||
|
|
Loading…
Reference in New Issue