Skip to content

Commit 4b72158

Browse files
author
Cruz Monrreal
authored
Merge pull request #6559 from davidsaada/david_flash_sim_bd
Implement FlashSimBlockDevice - flash simulated block device over RAM
2 parents 73eebae + c3e3999 commit 4b72158

File tree

3 files changed

+431
-0
lines changed

3 files changed

+431
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include "mbed.h"
17+
#include "greentea-client/test_env.h"
18+
#include "unity.h"
19+
#include "utest.h"
20+
21+
#include "FlashSimBlockDevice.h"
22+
#include "HeapBlockDevice.h"
23+
#include <stdlib.h>
24+
25+
using namespace utest::v1;
26+
27+
static const bd_size_t read_size = 1;
28+
static const bd_size_t prog_size = 8;
29+
static const bd_size_t erase_size = 512;
30+
static const bd_size_t num_blocks = 4;
31+
static const bd_size_t test_buf_size = 64;
32+
static const uint8_t blank = 0xFF;
33+
34+
// Simple test for all APIs
35+
void functionality_test()
36+
{
37+
HeapBlockDevice heap_bd(num_blocks * erase_size, read_size, prog_size, erase_size);
38+
FlashSimBlockDevice bd(&heap_bd, blank);
39+
40+
int err = bd.init();
41+
TEST_ASSERT_EQUAL(0, err);
42+
43+
uint8_t read_buf[test_buf_size], write_buf[test_buf_size];
44+
45+
TEST_ASSERT_EQUAL(num_blocks * erase_size, bd.size());
46+
TEST_ASSERT_EQUAL(read_size, bd.get_read_size());
47+
TEST_ASSERT_EQUAL(prog_size, bd.get_program_size());
48+
TEST_ASSERT_EQUAL(erase_size, bd.get_erase_size());
49+
TEST_ASSERT_EQUAL(blank, bd.get_erase_value());
50+
51+
srand(1);
52+
for (bd_size_t i = 0; i < test_buf_size; i++) {
53+
write_buf[i] = 0xff & rand();
54+
}
55+
56+
// Make sure we can't program if not erased (even after init)
57+
err = bd.program(write_buf, 0, test_buf_size);
58+
TEST_ASSERT_EQUAL(BD_ERROR_NOT_ERASED, err);
59+
60+
err = bd.erase(0, erase_size * 2);
61+
TEST_ASSERT_EQUAL(0, err);
62+
63+
err = bd.program(write_buf, 0, test_buf_size);
64+
TEST_ASSERT_EQUAL(0, err);
65+
66+
// Allow programming same data
67+
err = bd.program(write_buf, 0, test_buf_size);
68+
TEST_ASSERT_EQUAL(0, err);
69+
70+
srand(2);
71+
for (bd_size_t i = 0; i < test_buf_size; i++) {
72+
write_buf[i] = 0xff & rand();
73+
}
74+
75+
err = bd.program(write_buf, 0, test_buf_size);
76+
TEST_ASSERT_EQUAL(BD_ERROR_NOT_ERASED, err);
77+
78+
err = bd.program(write_buf, 2 * erase_size - test_buf_size, test_buf_size);
79+
TEST_ASSERT_EQUAL(0, err);
80+
81+
memset(write_buf, blank, test_buf_size / 2);
82+
err = bd.read(read_buf, test_buf_size * 2, test_buf_size / 2);
83+
TEST_ASSERT_EQUAL(0, err);
84+
TEST_ASSERT_EQUAL_UINT8_ARRAY(write_buf, read_buf, test_buf_size / 2);
85+
86+
srand(1);
87+
for (bd_size_t i = 0; i < test_buf_size; i++) {
88+
write_buf[i] = 0xff & rand();
89+
}
90+
91+
err = bd.read(read_buf, 0, test_buf_size);
92+
TEST_ASSERT_EQUAL(0, err);
93+
TEST_ASSERT_EQUAL_UINT8_ARRAY(write_buf, read_buf, test_buf_size);
94+
95+
srand(2);
96+
for (bd_size_t i = 0; i < test_buf_size; i++) {
97+
write_buf[i] = 0xff & rand();
98+
}
99+
100+
err = bd.read(read_buf, 2 * erase_size - test_buf_size, test_buf_size);
101+
TEST_ASSERT_EQUAL(0, err);
102+
TEST_ASSERT_EQUAL_UINT8_ARRAY(write_buf, read_buf, test_buf_size);
103+
104+
err = bd.deinit();
105+
TEST_ASSERT_EQUAL(0, err);
106+
107+
err = bd.init();
108+
TEST_ASSERT_EQUAL(0, err);
109+
110+
// Make sure data lives across inits
111+
err = bd.read(read_buf, 2 * erase_size - test_buf_size, test_buf_size);
112+
TEST_ASSERT_EQUAL(0, err);
113+
TEST_ASSERT_EQUAL_UINT8_ARRAY(write_buf, read_buf, test_buf_size);
114+
115+
err = bd.erase(0, erase_size);
116+
TEST_ASSERT_EQUAL(0, err);
117+
118+
// Make sure erase returns the erase value
119+
memset(write_buf, blank, test_buf_size);
120+
err = bd.read(read_buf, 0, test_buf_size);
121+
TEST_ASSERT_EQUAL(0, err);
122+
TEST_ASSERT_EQUAL_UINT8_ARRAY(write_buf, read_buf, test_buf_size);
123+
}
124+
125+
126+
// Test setup
127+
utest::v1::status_t test_setup(const size_t number_of_cases)
128+
{
129+
GREENTEA_SETUP(30, "default_auto");
130+
return verbose_test_setup_handler(number_of_cases);
131+
}
132+
133+
Case cases[] = {
134+
Case("FlashSimBlockDevice functionality test", functionality_test),
135+
};
136+
137+
Specification specification(test_setup, cases);
138+
139+
int main()
140+
{
141+
return !Harness::run(specification);
142+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "FlashSimBlockDevice.h"
18+
#include "mbed_assert.h"
19+
#include <algorithm>
20+
#include <stdlib.h>
21+
#include <string.h>
22+
23+
static const bd_size_t min_blank_buf_size = 32;
24+
25+
static inline uint32_t align_up(bd_size_t val, bd_size_t size)
26+
{
27+
return (((val - 1) / size) + 1) * size;
28+
}
29+
30+
FlashSimBlockDevice::FlashSimBlockDevice(BlockDevice *bd, uint8_t erase_value) :
31+
_erase_value(erase_value), _blank_buf_size(0),
32+
_blank_buf(0), _bd(bd)
33+
{
34+
}
35+
36+
FlashSimBlockDevice::~FlashSimBlockDevice()
37+
{
38+
deinit();
39+
delete[] _blank_buf;
40+
}
41+
42+
int FlashSimBlockDevice::init()
43+
{
44+
int ret = _bd->init();
45+
if (ret) {
46+
return ret;
47+
}
48+
_blank_buf_size = align_up(min_blank_buf_size, _bd->get_program_size());
49+
if (!_blank_buf) {
50+
_blank_buf = new uint8_t[_blank_buf_size];
51+
MBED_ASSERT(_blank_buf);
52+
}
53+
return BD_ERROR_OK;
54+
}
55+
56+
int FlashSimBlockDevice::deinit()
57+
{
58+
return _bd->deinit();
59+
}
60+
61+
int FlashSimBlockDevice::sync()
62+
{
63+
return _bd->sync();
64+
}
65+
66+
bd_size_t FlashSimBlockDevice::get_read_size() const
67+
{
68+
return _bd->get_read_size();
69+
}
70+
71+
bd_size_t FlashSimBlockDevice::get_program_size() const
72+
{
73+
return _bd->get_program_size();
74+
}
75+
76+
bd_size_t FlashSimBlockDevice::get_erase_size() const
77+
{
78+
return _bd->get_erase_size();
79+
}
80+
81+
bd_size_t FlashSimBlockDevice::get_erase_size(bd_addr_t addr) const
82+
{
83+
return _bd->get_erase_size(addr);
84+
}
85+
86+
bd_size_t FlashSimBlockDevice::size() const
87+
{
88+
return _bd->size();
89+
}
90+
91+
int FlashSimBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
92+
{
93+
return _bd->read(b, addr, size);
94+
}
95+
96+
int FlashSimBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
97+
{
98+
MBED_ASSERT(is_valid_program(addr, size));
99+
bd_addr_t curr_addr = addr;
100+
bd_size_t curr_size = size;
101+
102+
const uint8_t *buf = (const uint8_t *) b;
103+
while (curr_size) {
104+
bd_size_t read_size = std::min(_blank_buf_size, curr_size);
105+
int ret = _bd->read(_blank_buf, curr_addr, read_size);
106+
if (ret) {
107+
return ret;
108+
}
109+
for (bd_size_t i = 0; i < read_size; i++) {
110+
// Allow either programming on blanks or programming the same value
111+
// (as real flash devices do)
112+
if ((_blank_buf[i] != _erase_value) && (_blank_buf[i] != *buf)) {
113+
return BD_ERROR_NOT_ERASED;
114+
}
115+
buf++;
116+
}
117+
curr_addr += read_size;
118+
curr_size -= read_size;
119+
}
120+
121+
return _bd->program(b, addr, size);
122+
}
123+
124+
int FlashSimBlockDevice::erase(bd_addr_t addr, bd_size_t size)
125+
{
126+
MBED_ASSERT(is_valid_erase(addr, size));
127+
128+
bd_addr_t curr_addr = addr;
129+
bd_size_t curr_size = size;
130+
131+
memset(_blank_buf, _erase_value, (unsigned int) _blank_buf_size);
132+
133+
while (curr_size) {
134+
bd_size_t prog_size = std::min(_blank_buf_size, curr_size);
135+
int ret = _bd->program(_blank_buf, curr_addr, prog_size);
136+
if (ret) {
137+
return ret;
138+
}
139+
curr_addr += prog_size;
140+
curr_size -= prog_size;
141+
}
142+
143+
return BD_ERROR_OK;
144+
}
145+
146+
int FlashSimBlockDevice::get_erase_value() const
147+
{
148+
return _erase_value;
149+
}

0 commit comments

Comments
 (0)