Skip to content

Commit 7b0a8f2

Browse files
committed
atyle format
1 parent fb64f92 commit 7b0a8f2

File tree

3 files changed

+77
-55
lines changed

3 files changed

+77
-55
lines changed

components/storage/blockdevice/COMPONENT_I2CEE/I2CEEBlockDevice.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,27 @@
1818
using namespace mbed;
1919

2020
#define I2CEE_TIMEOUT 10000
21-
21+
2222

2323
I2CEEBlockDevice::I2CEEBlockDevice(
24-
PinName sda, PinName scl, uint8_t addr,
25-
bd_size_t size, bd_size_t block, int freq)
24+
PinName sda, PinName scl, uint8_t addr,
25+
bd_size_t size, bd_size_t block, int freq)
2626
: _i2c_addr(addr), _size(size), _block(block)
2727
{
2828
_i2c = new (_i2c_buffer) I2C(sda, scl);
2929
_i2c->frequency(freq);
3030
}
3131

3232
I2CEEBlockDevice::I2CEEBlockDevice(
33-
I2C * i2c_obj, uint8_t addr,
34-
bd_size_t size, bd_size_t block)
33+
I2C *i2c_obj, uint8_t addr,
34+
bd_size_t size, bd_size_t block)
3535
: _i2c_addr(addr), _size(size), _block(block)
3636
{
3737
_i2c = i2c_obj;
3838
}
3939
I2CEEBlockDevice::~I2CEEBlockDevice()
4040
{
41-
if (_i2c == (I2C*)_i2c_buffer) {
41+
if (_i2c == (I2C *)_i2c_buffer) {
4242
_i2c->~I2C();
4343
}
4444
}
@@ -59,50 +59,55 @@ int I2CEEBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
5959
MBED_ASSERT(is_valid_read(addr, size));
6060

6161
_i2c->start();
62+
6263
if (!_i2c->write(_i2c_addr | 0) ||
63-
!_i2c->write((char)(addr >> 8)) ||
64-
!_i2c->write((char)(addr & 0xff))) {
64+
!_i2c->write((char)(addr >> 8)) ||
65+
!_i2c->write((char)(addr & 0xff))) {
6566
return BD_ERROR_DEVICE_ERROR;
6667
}
68+
6769
_i2c->stop();
6870

69-
if (_i2c->read(_i2c_addr, static_cast<char*>(buffer), size) < 0) {
71+
if (_i2c->read(_i2c_addr, static_cast<char *>(buffer), size) < 0) {
7072
return BD_ERROR_DEVICE_ERROR;
7173
}
7274

7375
return 0;
7476
}
75-
77+
7678
int I2CEEBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size)
7779
{
7880
// Check the addr and size fit onto the chip.
7981
MBED_ASSERT(is_valid_program(addr, size));
80-
82+
8183
// While we have some more data to write.
8284
while (size > 0) {
8385
uint32_t off = addr % _block;
8486
uint32_t chunk = (off + size < _block) ? size : (_block - off);
8587

8688
_i2c->start();
89+
8790
if (!_i2c->write(_i2c_addr | 0) ||
88-
!_i2c->write((char)(addr >> 8)) ||
89-
!_i2c->write((char)(addr & 0xff))) {
91+
!_i2c->write((char)(addr >> 8)) ||
92+
!_i2c->write((char)(addr & 0xff))) {
9093
return BD_ERROR_DEVICE_ERROR;
9194
}
9295

9396
for (unsigned i = 0; i < chunk; i++) {
94-
_i2c->write(static_cast<const char*>(buffer)[i]);
97+
_i2c->write(static_cast<const char *>(buffer)[i]);
9598
}
99+
96100
_i2c->stop();
97101

98102
int err = _sync();
103+
99104
if (err) {
100105
return err;
101106
}
102107

103108
addr += chunk;
104109
size -= chunk;
105-
buffer = static_cast<const char*>(buffer) + chunk;
110+
buffer = static_cast<const char *>(buffer) + chunk;
106111
}
107112

108113
return 0;
@@ -129,7 +134,7 @@ int I2CEEBlockDevice::_sync()
129134

130135
return BD_ERROR_DEVICE_ERROR;
131136
}
132-
137+
133138
bd_size_t I2CEEBlockDevice::get_read_size() const
134139
{
135140
return 1;

components/storage/blockdevice/COMPONENT_I2CEE/I2CEEBlockDevice.h

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,48 @@
1515
*/
1616
#ifndef MBED_I2CEEPROM_BLOCK_DEVICE_H
1717
#define MBED_I2CEEPROM_BLOCK_DEVICE_H
18-
18+
1919
#include "BlockDevice.h"
2020
#include "I2C.h"
21-
21+
2222
/** BlockDevice for I2C based flash device such as
2323
* Microchip's 24LC or ATMEL's AT24C ranges
2424
*
2525
* @code
2626
* // Here's an example using a 24LC256 on a GR PEACH
2727
* #include "mbed.h"
2828
* #include "I2CEEBlockDevice.h"
29-
*
29+
*
3030
* // Create EEPROM device on I2C bus with 32kbytes of memory
3131
* I2CEEBlockDevice i2cee(D14, D15, 0xa0, 32*1024);
32-
*
32+
*
3333
* int main() {
3434
* printf("i2cee test\n");
35-
*
35+
*
3636
* // Initialize the device and print the memory layout
3737
* i2cee.init();
3838
* printf("i2cee size: %llu\n", i2cee.size());
3939
* printf("i2cee read size: %llu\n", i2cee.get_read_size());
4040
* printf("i2cee program size: %llu\n", i2cee.get_program_size());
4141
* printf("i2cee erase size: %llu\n", i2cee.get_erase_size());
42-
*
42+
*
4343
* // Write "Hello World!" to the first block
4444
* char *buffer = (char*)malloc(i2cee.get_erase_size());
4545
* sprintf(buffer, "Hello World!\n");
4646
* i2cee.erase(0, i2cee.get_erase_size());
4747
* i2cee.program(buffer, 0, i2cee.get_erase_size());
48-
*
48+
*
4949
* // Read back what was stored
5050
* i2cee.read(buffer, 0, i2cee.get_erase_size());
5151
* printf("%s", buffer);
52-
*
52+
*
5353
* // Deinitialize the device
5454
* i2cee.deinit();
5555
* }
5656
* @endcode
5757
*/
58-
class I2CEEBlockDevice : public BlockDevice {
58+
class I2CEEBlockDevice : public BlockDevice
59+
{
5960
public:
6061
/** Constructor to create an I2CEEBlockDevice on I2C pins
6162
*
@@ -67,21 +68,21 @@ class I2CEEBlockDevice : public BlockDevice {
6768
* @param freq The frequency of the I2C bus, defaults to 400K.
6869
*/
6970
I2CEEBlockDevice(
70-
PinName sda, PinName scl, uint8_t address,
71-
bd_size_t size, bd_size_t block=32,
72-
int bus_speed=400000);
71+
PinName sda, PinName scl, uint8_t address,
72+
bd_size_t size, bd_size_t block = 32,
73+
int bus_speed = 400000);
7374

74-
/** Constructor to create an I2CEEBlockDevice on I2C pins
75-
*
76-
* @param i2c The I2C instance pointer
77-
* @param addr The 8bit I2C address of the chip, common range 0xa0 - 0xae.
78-
* @param size The size of the device in bytes
79-
* @param block The page size of the device in bytes, defaults to 32bytes
80-
* @param freq The frequency of the I2C bus, defaults to 400K.
81-
*/
75+
/** Constructor to create an I2CEEBlockDevice on I2C pins
76+
*
77+
* @param i2c The I2C instance pointer
78+
* @param addr The 8bit I2C address of the chip, common range 0xa0 - 0xae.
79+
* @param size The size of the device in bytes
80+
* @param block The page size of the device in bytes, defaults to 32bytes
81+
* @param freq The frequency of the I2C bus, defaults to 400K.
82+
*/
8283
I2CEEBlockDevice(
83-
mbed::I2C * i2c_obj, uint8_t address,
84-
bd_size_t size, bd_size_t block=32);
84+
mbed::I2C *i2c_obj, uint8_t address,
85+
bd_size_t size, bd_size_t block = 32);
8586

8687
/** Destructor of I2CEEBlockDevice
8788
*/
@@ -161,16 +162,16 @@ class I2CEEBlockDevice : public BlockDevice {
161162
* @return A string representation of the BlockDevice class type.
162163
*/
163164
virtual const char *get_type() const;
164-
165+
165166
private:
166-
mbed::I2C * _i2c;
167+
mbed::I2C *_i2c;
167168
uint32_t _i2c_buffer[sizeof(mbed::I2C) / sizeof(uint32_t)];
168169
uint8_t _i2c_addr;
169170
uint32_t _size;
170171
uint32_t _block;
171172

172173
int _sync();
173174
};
174-
175+
175176

176177
#endif /* MBED_SD_BLOCK_DEVICE_H */

components/storage/blockdevice/COMPONENT_I2CEE/TESTS/block_device/i2cee/main.cpp

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,23 @@ const struct {
2727
};
2828

2929

30-
void test_read_write() {
30+
void test_read_write()
31+
{
3132
I2CEEBlockDevice bd(TEST_PINS, TEST_ADDR,
32-
TEST_SIZE, TEST_BLOCK_SIZE, TEST_FREQ);
33+
TEST_SIZE, TEST_BLOCK_SIZE, TEST_FREQ);
3334

3435
int err = bd.init();
3536
TEST_ASSERT_EQUAL(0, err);
3637

37-
for (unsigned a = 0; a < sizeof(ATTRS)/sizeof(ATTRS[0]); a++) {
38+
for (unsigned a = 0; a < sizeof(ATTRS) / sizeof(ATTRS[0]); a++) {
3839
static const char *prefixes[] = {"", "k", "M", "G"};
40+
3941
for (int i = 3; i >= 0; i--) {
4042
bd_size_t size = (bd.*ATTRS[a].method)();
41-
if (size >= (1ULL << 10*i)) {
43+
44+
if (size >= (1ULL << 10 * i)) {
4245
printf("%s: %llu%sbytes (%llubytes)\n",
43-
ATTRS[a].name, size >> 10*i, prefixes[i], size);
46+
ATTRS[a].name, size >> 10 * i, prefixes[i], size);
4447
break;
4548
}
4649
}
@@ -50,18 +53,19 @@ void test_read_write() {
5053
uint8_t *write_block = new uint8_t[block_size];
5154
uint8_t *read_block = new uint8_t[block_size];
5255
uint8_t *error_mask = new uint8_t[TEST_ERROR_MASK];
53-
unsigned addrwidth = ceil(log(float(bd.size()-1)) / log(float(16)))+1;
56+
unsigned addrwidth = ceil(log(float(bd.size() - 1)) / log(float(16))) + 1;
5457

5558
for (int b = 0; b < TEST_BLOCK_COUNT; b++) {
5659
// Find a random block
57-
bd_addr_t block = (rand()*block_size) % bd.size();
60+
bd_addr_t block = (rand() * block_size) % bd.size();
5861

5962
// Use next random number as temporary seed to keep
6063
// the address progressing in the pseudorandom sequence
6164
unsigned seed = rand();
6265

6366
// Fill with random sequence
6467
srand(seed);
68+
6569
for (bd_size_t i = 0; i < block_size; i++) {
6670
write_block[i] = 0xff & rand();
6771
}
@@ -76,59 +80,70 @@ void test_read_write() {
7680
TEST_ASSERT_EQUAL(0, err);
7781

7882
printf("write %0*llx:%llu ", addrwidth, block, block_size);
83+
7984
for (int i = 0; i < block_size && i < 16; i++) {
8085
printf("%02x", write_block[i]);
8186
}
87+
8288
if (block_size > 16) {
8389
printf("...\n");
8490
}
91+
8592
printf("\n");
8693

8794
err = bd.read(read_block, block, block_size);
8895
TEST_ASSERT_EQUAL(0, err);
8996

9097
printf("read %0*llx:%llu ", addrwidth, block, block_size);
98+
9199
for (int i = 0; i < block_size && i < 16; i++) {
92100
printf("%02x", read_block[i]);
93101
}
102+
94103
if (block_size > 16) {
95104
printf("...");
96105
}
106+
97107
printf("\n");
98108

99109
// Find error mask for debugging
100110
memset(error_mask, 0, TEST_ERROR_MASK);
101-
bd_size_t error_scale = block_size / (TEST_ERROR_MASK*8);
111+
bd_size_t error_scale = block_size / (TEST_ERROR_MASK * 8);
102112

103113
srand(seed);
104-
for (bd_size_t i = 0; i < TEST_ERROR_MASK*8; i++) {
114+
115+
for (bd_size_t i = 0; i < TEST_ERROR_MASK * 8; i++) {
105116
for (bd_size_t j = 0; j < error_scale; j++) {
106-
if ((0xff & rand()) != read_block[i*error_scale + j]) {
107-
error_mask[i/8] |= 1 << (i%8);
117+
if ((0xff & rand()) != read_block[i * error_scale + j]) {
118+
error_mask[i / 8] |= 1 << (i % 8);
108119
}
109120
}
110121
}
111122

112123
printf("error %0*llx:%llu ", addrwidth, block, block_size);
124+
113125
for (int i = 0; i < TEST_ERROR_MASK; i++) {
114126
printf("%02x", error_mask[i]);
115127
}
128+
116129
printf("\n");
117130

118131
// Check that the data was unmodified
119132
srand(seed);
133+
120134
for (bd_size_t i = 0; i < block_size; i++) {
121135
TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
122136
}
123137
}
124-
138+
125139
err = bd.deinit();
126140
TEST_ASSERT_EQUAL(0, err);
127141
}
128142

129143

130144
// Test setup
131-
utest::v1::status_t test_setup(const size_t number_of_cases) {
145+
utest::v1::status_t test_setup(const size_t number_of_cases)
146+
{
132147
GREENTEA_SETUP(30, "default_auto");
133148
return verbose_test_setup_handler(number_of_cases);
134149
}
@@ -139,6 +154,7 @@ Case cases[] = {
139154

140155
Specification specification(test_setup, cases);
141156

142-
int main() {
157+
int main()
158+
{
143159
return !Harness::run(specification);
144160
}

0 commit comments

Comments
 (0)