mirror of https://github.com/ARMmbed/mbed-os.git
atyle format
parent
fb64f92430
commit
7b0a8f23a2
|
@ -21,8 +21,8 @@ using namespace mbed;
|
|||
|
||||
|
||||
I2CEEBlockDevice::I2CEEBlockDevice(
|
||||
PinName sda, PinName scl, uint8_t addr,
|
||||
bd_size_t size, bd_size_t block, int freq)
|
||||
PinName sda, PinName scl, uint8_t addr,
|
||||
bd_size_t size, bd_size_t block, int freq)
|
||||
: _i2c_addr(addr), _size(size), _block(block)
|
||||
{
|
||||
_i2c = new (_i2c_buffer) I2C(sda, scl);
|
||||
|
@ -30,15 +30,15 @@ I2CEEBlockDevice::I2CEEBlockDevice(
|
|||
}
|
||||
|
||||
I2CEEBlockDevice::I2CEEBlockDevice(
|
||||
I2C * i2c_obj, uint8_t addr,
|
||||
bd_size_t size, bd_size_t block)
|
||||
I2C *i2c_obj, uint8_t addr,
|
||||
bd_size_t size, bd_size_t block)
|
||||
: _i2c_addr(addr), _size(size), _block(block)
|
||||
{
|
||||
_i2c = i2c_obj;
|
||||
}
|
||||
I2CEEBlockDevice::~I2CEEBlockDevice()
|
||||
{
|
||||
if (_i2c == (I2C*)_i2c_buffer) {
|
||||
if (_i2c == (I2C *)_i2c_buffer) {
|
||||
_i2c->~I2C();
|
||||
}
|
||||
}
|
||||
|
@ -59,14 +59,16 @@ int I2CEEBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
|
|||
MBED_ASSERT(is_valid_read(addr, size));
|
||||
|
||||
_i2c->start();
|
||||
|
||||
if (!_i2c->write(_i2c_addr | 0) ||
|
||||
!_i2c->write((char)(addr >> 8)) ||
|
||||
!_i2c->write((char)(addr & 0xff))) {
|
||||
!_i2c->write((char)(addr >> 8)) ||
|
||||
!_i2c->write((char)(addr & 0xff))) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
_i2c->stop();
|
||||
|
||||
if (_i2c->read(_i2c_addr, static_cast<char*>(buffer), size) < 0) {
|
||||
if (_i2c->read(_i2c_addr, static_cast<char *>(buffer), size) < 0) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
|
@ -84,25 +86,28 @@ int I2CEEBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size
|
|||
uint32_t chunk = (off + size < _block) ? size : (_block - off);
|
||||
|
||||
_i2c->start();
|
||||
|
||||
if (!_i2c->write(_i2c_addr | 0) ||
|
||||
!_i2c->write((char)(addr >> 8)) ||
|
||||
!_i2c->write((char)(addr & 0xff))) {
|
||||
!_i2c->write((char)(addr >> 8)) ||
|
||||
!_i2c->write((char)(addr & 0xff))) {
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < chunk; i++) {
|
||||
_i2c->write(static_cast<const char*>(buffer)[i]);
|
||||
_i2c->write(static_cast<const char *>(buffer)[i]);
|
||||
}
|
||||
|
||||
_i2c->stop();
|
||||
|
||||
int err = _sync();
|
||||
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
addr += chunk;
|
||||
size -= chunk;
|
||||
buffer = static_cast<const char*>(buffer) + chunk;
|
||||
buffer = static_cast<const char *>(buffer) + chunk;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -55,7 +55,8 @@
|
|||
* }
|
||||
* @endcode
|
||||
*/
|
||||
class I2CEEBlockDevice : public BlockDevice {
|
||||
class I2CEEBlockDevice : public BlockDevice
|
||||
{
|
||||
public:
|
||||
/** Constructor to create an I2CEEBlockDevice on I2C pins
|
||||
*
|
||||
|
@ -67,21 +68,21 @@ public:
|
|||
* @param freq The frequency of the I2C bus, defaults to 400K.
|
||||
*/
|
||||
I2CEEBlockDevice(
|
||||
PinName sda, PinName scl, uint8_t address,
|
||||
bd_size_t size, bd_size_t block=32,
|
||||
int bus_speed=400000);
|
||||
PinName sda, PinName scl, uint8_t address,
|
||||
bd_size_t size, bd_size_t block = 32,
|
||||
int bus_speed = 400000);
|
||||
|
||||
/** Constructor to create an I2CEEBlockDevice on I2C pins
|
||||
*
|
||||
* @param i2c The I2C instance pointer
|
||||
* @param addr The 8bit I2C address of the chip, common range 0xa0 - 0xae.
|
||||
* @param size The size of the device in bytes
|
||||
* @param block The page size of the device in bytes, defaults to 32bytes
|
||||
* @param freq The frequency of the I2C bus, defaults to 400K.
|
||||
*/
|
||||
/** Constructor to create an I2CEEBlockDevice on I2C pins
|
||||
*
|
||||
* @param i2c The I2C instance pointer
|
||||
* @param addr The 8bit I2C address of the chip, common range 0xa0 - 0xae.
|
||||
* @param size The size of the device in bytes
|
||||
* @param block The page size of the device in bytes, defaults to 32bytes
|
||||
* @param freq The frequency of the I2C bus, defaults to 400K.
|
||||
*/
|
||||
I2CEEBlockDevice(
|
||||
mbed::I2C * i2c_obj, uint8_t address,
|
||||
bd_size_t size, bd_size_t block=32);
|
||||
mbed::I2C *i2c_obj, uint8_t address,
|
||||
bd_size_t size, bd_size_t block = 32);
|
||||
|
||||
/** Destructor of I2CEEBlockDevice
|
||||
*/
|
||||
|
@ -163,7 +164,7 @@ public:
|
|||
virtual const char *get_type() const;
|
||||
|
||||
private:
|
||||
mbed::I2C * _i2c;
|
||||
mbed::I2C *_i2c;
|
||||
uint32_t _i2c_buffer[sizeof(mbed::I2C) / sizeof(uint32_t)];
|
||||
uint8_t _i2c_addr;
|
||||
uint32_t _size;
|
||||
|
|
|
@ -27,20 +27,23 @@ const struct {
|
|||
};
|
||||
|
||||
|
||||
void test_read_write() {
|
||||
void test_read_write()
|
||||
{
|
||||
I2CEEBlockDevice bd(TEST_PINS, TEST_ADDR,
|
||||
TEST_SIZE, TEST_BLOCK_SIZE, TEST_FREQ);
|
||||
TEST_SIZE, TEST_BLOCK_SIZE, TEST_FREQ);
|
||||
|
||||
int err = bd.init();
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
for (unsigned a = 0; a < sizeof(ATTRS)/sizeof(ATTRS[0]); a++) {
|
||||
for (unsigned a = 0; a < sizeof(ATTRS) / sizeof(ATTRS[0]); a++) {
|
||||
static const char *prefixes[] = {"", "k", "M", "G"};
|
||||
|
||||
for (int i = 3; i >= 0; i--) {
|
||||
bd_size_t size = (bd.*ATTRS[a].method)();
|
||||
if (size >= (1ULL << 10*i)) {
|
||||
|
||||
if (size >= (1ULL << 10 * i)) {
|
||||
printf("%s: %llu%sbytes (%llubytes)\n",
|
||||
ATTRS[a].name, size >> 10*i, prefixes[i], size);
|
||||
ATTRS[a].name, size >> 10 * i, prefixes[i], size);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -50,11 +53,11 @@ 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(float(bd.size()-1)) / log(float(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
|
||||
bd_addr_t block = (rand()*block_size) % bd.size();
|
||||
bd_addr_t block = (rand() * block_size) % bd.size();
|
||||
|
||||
// Use next random number as temporary seed to keep
|
||||
// the address progressing in the pseudorandom sequence
|
||||
|
@ -62,6 +65,7 @@ void test_read_write() {
|
|||
|
||||
// Fill with random sequence
|
||||
srand(seed);
|
||||
|
||||
for (bd_size_t i = 0; i < block_size; i++) {
|
||||
write_block[i] = 0xff & rand();
|
||||
}
|
||||
|
@ -76,47 +80,57 @@ void test_read_write() {
|
|||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
printf("write %0*llx:%llu ", addrwidth, block, block_size);
|
||||
|
||||
for (int i = 0; i < block_size && i < 16; i++) {
|
||||
printf("%02x", write_block[i]);
|
||||
}
|
||||
|
||||
if (block_size > 16) {
|
||||
printf("...\n");
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
err = bd.read(read_block, block, block_size);
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
printf("read %0*llx:%llu ", addrwidth, block, block_size);
|
||||
|
||||
for (int i = 0; i < block_size && i < 16; i++) {
|
||||
printf("%02x", read_block[i]);
|
||||
}
|
||||
|
||||
if (block_size > 16) {
|
||||
printf("...");
|
||||
}
|
||||
|
||||
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);
|
||||
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 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);
|
||||
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 < TEST_ERROR_MASK; i++) {
|
||||
printf("%02x", error_mask[i]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
// Check that the data was unmodified
|
||||
srand(seed);
|
||||
|
||||
for (bd_size_t i = 0; i < block_size; i++) {
|
||||
TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
|
||||
}
|
||||
|
@ -128,7 +142,8 @@ void test_read_write() {
|
|||
|
||||
|
||||
// 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");
|
||||
return verbose_test_setup_handler(number_of_cases);
|
||||
}
|
||||
|
@ -139,6 +154,7 @@ Case cases[] = {
|
|||
|
||||
Specification specification(test_setup, cases);
|
||||
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
return !Harness::run(specification);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue