Skip to content

Commit 7fa50c3

Browse files
gekysimonqhughes
authored andcommitted
bd: Adopted the block storage api in the FATFileSystem
1 parent 99da911 commit 7fa50c3

File tree

4 files changed

+259
-212
lines changed

4 files changed

+259
-212
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 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 "HeapBlockDevice.h"
22+
#include "FATFileSystem.h"
23+
#include <stdlib.h>
24+
25+
using namespace utest::v1;
26+
27+
// Test block device
28+
#define BLOCK_SIZE 512
29+
HeapBlockDevice bd(128*BLOCK_SIZE, BLOCK_SIZE);
30+
31+
32+
void test_format() {
33+
int err = FATFileSystem::format(&bd);
34+
TEST_ASSERT_EQUAL(0, err);
35+
}
36+
37+
// Simple test for reading/writing files
38+
template <ssize_t TEST_SIZE>
39+
void test_read_write() {
40+
FATFileSystem fs("fat");
41+
42+
int err = fs.mount(&bd);
43+
TEST_ASSERT_EQUAL(0, err);
44+
45+
uint8_t *buffer = (uint8_t *)malloc(TEST_SIZE);
46+
TEST_ASSERT(buffer);
47+
48+
// Fill with random sequence
49+
srand(1);
50+
for (int i = 0; i < TEST_SIZE; i++) {
51+
buffer[i] = 0xff & rand();
52+
}
53+
54+
// write and read file
55+
FileHandle *file = fs.open("test_read_write.dat", O_WRONLY | O_CREAT);
56+
TEST_ASSERT(file);
57+
ssize_t size = file->write(buffer, TEST_SIZE);
58+
TEST_ASSERT_EQUAL(TEST_SIZE, size);
59+
err = file->close();
60+
TEST_ASSERT_EQUAL(0, err);
61+
62+
file = fs.open("test_read_write.dat", O_RDONLY);
63+
TEST_ASSERT(file);
64+
size = file->read(buffer, TEST_SIZE);
65+
TEST_ASSERT_EQUAL(TEST_SIZE, size);
66+
err = file->close();
67+
TEST_ASSERT_EQUAL(0, err);
68+
69+
// Check that the data was unmodified
70+
srand(1);
71+
for (int i = 0; i < TEST_SIZE; i++) {
72+
TEST_ASSERT_EQUAL(0xff & rand(), buffer[i]);
73+
}
74+
75+
err = fs.unmount();
76+
TEST_ASSERT_EQUAL(0, err);
77+
}
78+
79+
80+
// Test setup
81+
utest::v1::status_t test_setup(const size_t number_of_cases) {
82+
GREENTEA_SETUP(10, "default_auto");
83+
return verbose_test_setup_handler(number_of_cases);
84+
}
85+
86+
Case cases[] = {
87+
Case("Testing formating", test_format),
88+
Case("Testing read write < block", test_read_write<BLOCK_SIZE/2>),
89+
Case("Testing read write > block", test_read_write<2*BLOCK_SIZE>),
90+
};
91+
92+
Specification specification(test_setup, cases);
93+
94+
int main() {
95+
return !Harness::run(specification);
96+
}

features/filesystem/fat/ChaN/diskio.cpp

Lines changed: 0 additions & 117 deletions
This file was deleted.

0 commit comments

Comments
 (0)