Skip to content

storage: Add block device API #3449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions features/TESTS/filesystem/basic/basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
*/

#include "mbed.h"
#include "SDFileSystem.h"
#include "FATFileSystem.h"
#include "SDBlockDevice.h"
#include "test_env.h"

#include "utest/utest.h"
Expand All @@ -69,22 +70,22 @@ using namespace utest::v1;
#if defined(DEVICE_SPI) && defined(FSFAT_SDCARD_INSTALLED)

#if defined(TARGET_KL25Z)
SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd");
SDFileSystem sd(PTD2, PTD3, PTD1, PTD0);

#elif defined(TARGET_KL46Z) || defined(TARGET_KL43Z)
SDFileSystem sd(PTD6, PTD7, PTD5, PTD4, "sd");
SDFileSystem sd(PTD6, PTD7, PTD5, PTD4);

#elif defined(TARGET_K64F) || defined(TARGET_K66F)
SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
SDFileSystem sd(PTE3, PTE1, PTE2, PTE4);

#elif defined(TARGET_K22F)
SDFileSystem sd(PTD6, PTD7, PTD5, PTD4, "sd");
SDFileSystem sd(PTD6, PTD7, PTD5, PTD4);

#elif defined(TARGET_K20D50M)
SDFileSystem sd(PTD2, PTD3, PTD1, PTC2, "sd");
SDFileSystem sd(PTD2, PTD3, PTD1, PTC2);

#elif defined(TARGET_nRF51822)
SDFileSystem sd(p12, p13, p15, p14, "sd");
SDFileSystem sd(p12, p13, p15, p14);

#elif defined(TARGET_NUCLEO_F030R8) || \
defined(TARGET_NUCLEO_F070RB) || \
Expand All @@ -100,31 +101,33 @@ SDFileSystem sd(p12, p13, p15, p14, "sd");
defined(TARGET_NUCLEO_L053R8) || \
defined(TARGET_NUCLEO_L073RZ) || \
defined(TARGET_NUCLEO_L152RE)
SDFileSystem sd(D11, D12, D13, D10, "sd");
SDFileSystem sd(D11, D12, D13, D10);

#elif defined(TARGET_DISCO_F051R8) || \
defined(TARGET_NUCLEO_L031K6)
SDFileSystem sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, "sd");
SDFileSystem sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS);

#elif defined(TARGET_LPC2368)
SDFileSystem sd(p11, p12, p13, p14, "sd");
SDFileSystem sd(p11, p12, p13, p14);

#elif defined(TARGET_LPC11U68)
SDFileSystem sd(D11, D12, D13, D10, "sd");
SDFileSystem sd(D11, D12, D13, D10);

#elif defined(TARGET_LPC1549)
SDFileSystem sd(D11, D12, D13, D10, "sd");
SDFileSystem sd(D11, D12, D13, D10);

#elif defined(TARGET_RZ_A1H)
SDFileSystem sd(P8_5, P8_6, P8_3, P8_4, "sd");
SDFileSystem sd(P8_5, P8_6, P8_3, P8_4);

#elif defined(TARGET_LPC11U37H_401)
SDFileSystem sd(SDMOSI, SDMISO, SDSCLK, SDSSEL, "sd");
SDFileSystem sd(SDMOSI, SDMISO, SDSCLK, SDSSEL);

#else
#error "[NOT SUPPORTED] Instantiate SDFileSystem sd(p11, p12, p13, p14, "sd") with the correct pin specification for target"
#error "[NOT SUPPORTED] Instantiate SDFileSystem sd(p11, p12, p13, p14) with the correct pin specification for target"
#endif

FATFileSystem fat("sd", &sd);


#define FSFAT_BASIC_TEST_00 fsfat_basic_test_00
#define FSFAT_BASIC_TEST_01 fsfat_basic_test_01
Expand Down
80 changes: 80 additions & 0 deletions features/TESTS/filesystem/fat_file_system/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"

#include "HeapBlockDevice.h"
#include "FATFileSystem.h"
#include <stdlib.h>

using namespace utest::v1;

// Test block device
#define BLOCK_SIZE 512
HeapBlockDevice bd(BLOCK_SIZE, 128);


void test_format() {
int err = FATFileSystem::format(&bd);
TEST_ASSERT_EQUAL(0, err);
}

template <ssize_t TEST_SIZE>
void test_read_write() {
FATFileSystem fs("fat");

int err = fs.mount(&bd);
TEST_ASSERT_EQUAL(0, err);

uint8_t *buffer = (uint8_t *)malloc(TEST_SIZE);
TEST_ASSERT(buffer);

// Fill with random sequence
srand(1);
for (int i = 0; i < TEST_SIZE; i++) {
buffer[i] = 0xff & rand();
}

// write and read file
FileHandle *file = fs.open("test_read_write.dat", O_WRONLY | O_CREAT);
TEST_ASSERT(file);
ssize_t size = file->write(buffer, TEST_SIZE);
TEST_ASSERT_EQUAL(TEST_SIZE, size);
err = file->close();
TEST_ASSERT_EQUAL(0, err);

file = fs.open("test_read_write.dat", O_RDONLY);
TEST_ASSERT(file);
size = file->read(buffer, TEST_SIZE);
TEST_ASSERT_EQUAL(TEST_SIZE, size);
err = file->close();
TEST_ASSERT_EQUAL(0, err);

// Check that the data was unmodified
srand(1);
for (int i = 0; i < TEST_SIZE; i++) {
TEST_ASSERT_EQUAL(0xff & rand(), buffer[i]);
}

err = fs.unmount();
TEST_ASSERT_EQUAL(0, err);
}


// Test setup
utest::v1::status_t test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(10, "default_auto");
return verbose_test_setup_handler(number_of_cases);
}

Case cases[] = {
Case("Testing formating", test_format),
Case("Testing read write < block", test_read_write<BLOCK_SIZE/2>),
Case("Testing read write > block", test_read_write<2*BLOCK_SIZE>),
};

Specification specification(test_setup, cases);

int main() {
return !Harness::run(specification);
}
33 changes: 18 additions & 15 deletions features/TESTS/filesystem/fopen/fopen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
*/

#include "mbed.h"
#include "SDFileSystem.h"
#include "FATFileSystem.h"
#include "SDBlockDevice.h"
#include "fsfat_debug.h"
#include "fsfat_test.h"
#include "utest/utest.h"
Expand Down Expand Up @@ -58,22 +59,22 @@ using namespace utest::v1;
static char fsfat_fopen_utest_msg_g[FSFAT_UTEST_MSG_BUF_SIZE];

#if defined(TARGET_KL25Z)
SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd");
SDBlockDevice sd(PTD2, PTD3, PTD1, PTD0);

#elif defined(TARGET_KL46Z) || defined(TARGET_KL43Z)
SDFileSystem sd(PTD6, PTD7, PTD5, PTD4, "sd");
SDBlockDevice sd(PTD6, PTD7, PTD5, PTD4);

#elif defined(TARGET_K64F) || defined(TARGET_K66F)
SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
SDBlockDevice sd(PTE3, PTE1, PTE2, PTE4);

#elif defined(TARGET_K22F)
SDFileSystem sd(PTD6, PTD7, PTD5, PTD4, "sd");
SDBlockDevice sd(PTD6, PTD7, PTD5, PTD4);

#elif defined(TARGET_K20D50M)
SDFileSystem sd(PTD2, PTD3, PTD1, PTC2, "sd");
SDBlockDevice sd(PTD2, PTD3, PTD1, PTC2);

#elif defined(TARGET_nRF51822)
SDFileSystem sd(p12, p13, p15, p14, "sd");
SDBlockDevice sd(p12, p13, p15, p14);

#elif defined(TARGET_NUCLEO_F030R8) || \
defined(TARGET_NUCLEO_F070RB) || \
Expand All @@ -89,32 +90,34 @@ SDFileSystem sd(p12, p13, p15, p14, "sd");
defined(TARGET_NUCLEO_L053R8) || \
defined(TARGET_NUCLEO_L073RZ) || \
defined(TARGET_NUCLEO_L152RE)
SDFileSystem sd(D11, D12, D13, D10, "sd");
SDBlockDevice sd(D11, D12, D13, D10);


#elif defined(TARGET_DISCO_F051R8) || \
defined(TARGET_NUCLEO_L031K6)
SDFileSystem sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, "sd");
SDBlockDevice sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS);

#elif defined(TARGET_LPC2368)
SDFileSystem sd(p11, p12, p13, p14, "sd");
SDBlockDevice sd(p11, p12, p13, p14);

#elif defined(TARGET_LPC11U68)
SDFileSystem sd(D11, D12, D13, D10, "sd");
SDBlockDevice sd(D11, D12, D13, D10);

#elif defined(TARGET_LPC1549)
SDFileSystem sd(D11, D12, D13, D10, "sd");
SDBlockDevice sd(D11, D12, D13, D10);

#elif defined(TARGET_RZ_A1H)
SDFileSystem sd(P8_5, P8_6, P8_3, P8_4, "sd");
SDBlockDevice sd(P8_5, P8_6, P8_3, P8_4);

#elif defined(TARGET_LPC11U37H_401)
SDFileSystem sd(SDMOSI, SDMISO, SDSCLK, SDSSEL, "sd");
SDBlockDevice sd(SDMOSI, SDMISO, SDSCLK, SDSSEL);

#else
#error "[NOT SUPPORTED] Instantiate SDFileSystem sd(p11, p12, p13, p14, "sd") with the correct pin specification for target"
#error "[NOT SUPPORTED] Instantiate SDBlockDevice sd(p11, p12, p13, p14) with the correct pin specification for target"
#endif

FATFileSystem fat("sd", &sd);


#define FSFAT_FOPEN_TEST_01 fsfat_fopen_test_01
#define FSFAT_FOPEN_TEST_02 fsfat_fopen_test_02
Expand Down
63 changes: 63 additions & 0 deletions features/TESTS/filesystem/heap_block_device/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"

#include "HeapBlockDevice.h"
#include <stdlib.h>

using namespace utest::v1;

#define BLOCK_SIZE 512
uint8_t write_block[BLOCK_SIZE];
uint8_t read_block[BLOCK_SIZE];


void test_read_write() {
HeapBlockDevice bd(BLOCK_SIZE, 16);

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

// Fill with random sequence
srand(1);
for (int i = 0; i < BLOCK_SIZE; i++) {
write_block[i] = 0xff & rand();
}

// Write, sync, and read the block
err = bd.write(write_block, 0, 1);
TEST_ASSERT_EQUAL(1, err);

err = bd.sync();
TEST_ASSERT_EQUAL(0, err);

err = bd.read(read_block, 0, 1);
TEST_ASSERT_EQUAL(1, err);

// Check that the data was unmodified
srand(1);
for (int i = 0; i < BLOCK_SIZE; i++) {
TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
}

err = bd.deinit();
TEST_ASSERT_EQUAL(0, err);
}


// Test setup
utest::v1::status_t test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(10, "default_auto");
return verbose_test_setup_handler(number_of_cases);
}

Case cases[] = {
Case("Testing read write of a block", test_read_write),
};

Specification specification(test_setup, cases);

int main() {
return !Harness::run(specification);
}
28 changes: 28 additions & 0 deletions features/filesystem/bd/BlockDevice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* mbed Microcontroller Library
* Copyright (c) 2016 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 "BlockDevice.h"


bd_error_t BlockDevice::sync()
{
return BD_ERROR_OK;
}

bd_error_t BlockDevice::status()
{
return BD_ERROR_OK;
}
Loading