Block device class for creating a block device to access a SD/MMC card via SD/MMC interface on DISCO_F746NG
Dependencies: BSP_DISCO_F746NG
Dependents: DISCO-F746NG_BLOCK_DEVICE_SDCARD DISCO-F746NG_BLOCK_DEVICE_WITH_FAT_FILESYSTEM_ON_SDCARD SDReaderTest
Block device class for creating a block device to access a SD/MMC card via SD/MMC interface on DISCO_F746NG development board. This block device API provides an interface for access to block-based storage. You can use a block device to back a full file system or write to it directly. More about Block Devices
Simple example how to use the block device
/* Example file of using SD/MMC Block device Library for MBED-OS * Copyright 2017 Roy Krikke * * 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 "mbed.h" #include "SDBlockDeviceDISCOF746NG.h" DigitalOut led (LED1); // Instantiate the Block Device for sd card on DISCO-F746NG SDBlockDeviceDISCOF746NG bd; uint8_t block[512] = "Hello World!\n"; int main () { Serial pc (SERIAL_TX, SERIAL_RX); pc.baud(115200); printf("Start\n"); // Call the SDBlockDeviceDISCOF746NG instance initialisation method. printf("sd card init...\n"); if (0 != bd.init()) { printf("Init failed \n"); return -1; } printf("sd size: %llu\n", bd.size()); printf("sd read size: %llu\n", bd.get_read_size()); printf("sd program size: %llu\n", bd.get_program_size()); printf("sd erase size: %llu\n\n", bd.get_erase_size()); printf("sd erase...\n"); if (0 != bd.erase (0, bd.get_erase_size())) { printf("Error Erasing block \n"); } // Write some the data block to the device printf("sd write: %s\n", block); if (0 == bd.program(block, 0, 512)) { // read the data block from the device printf("sd read: "); if (0 == bd.read(block, 0, 512)) { // print the contents of the block printf("%s", block); } } //////////////////////////////////////////////////////////////////////////////// for(int i; i < 10; i++) { memset(block, i, sizeof(block)); if(bd.program(block, (i*512), sizeof(block)) != 0) { printf("Error at: %i\r\n",i); } } if(bd.erase((1*512), sizeof(block)) != 0) { printf("Error Erasing block\n"); } memset(block, 'x', sizeof(block)); if(bd.program(block, (1*512), sizeof(block)) != 0) { printf("Error\r\n"); } //////////////////////////////////////////////////////////////////////////////// // Call the BD_SD_DISCO_F746NG instance de-initialisation method. printf("sd card deinit...\n"); if (0 != bd.deinit()) { printf ("Deinit failed \n"); return -1; } // Blink led with 2 Hz while(true) { led = !led; wait (0.5); } }
Simple example how to use the block device to create a file on a FAT file system
/* Example file of using SD/MMC Block device Library for MBED-OS * Copyright 2017 Roy Krikke * * 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 "mbed.h" #include "FATFileSystem.h" #include "SDBlockDeviceDISCOF746NG.h" #include <stdio.h> #include <errno.h> DigitalOut led (LED1); // Instantiate the Block Device for sd card on DISCO-F746NG SDBlockDeviceDISCOF746NG bd; FATFileSystem fs ("fs"); void return_error (int ret_val) { if(ret_val) printf ("Failure. %d\r\n", ret_val); else printf ("done.\r\n"); } void errno_error (void* ret_val) { if(ret_val == NULL) printf (" Failure. %d \r\n", errno); else printf (" done.\r\n"); } int main () { Serial pc (SERIAL_TX, SERIAL_RX); pc.baud(115200); printf("Start\n"); int error = 0; printf("Welcome to the filesystem example.\r\n" "Formatting a FAT, RAM-backed filesystem. "); error = FATFileSystem::format(&bd); return_error(error); printf("Mounting the filesystem on \"/fs\". "); error = fs.mount(&bd); return_error(error); printf("Opening a new file, numbers.txt."); FILE* fd = fopen("/fs/numbers.txt", "w"); errno_error(fd); for (int i = 0; i < 20; i++) { printf("Writing decimal numbers to a file (%d/20)\r", i); fprintf(fd, "%d\r\n", i); } printf("Writing decimal numbers to a file (20/20) done.\r\n"); printf("Closing file."); fclose(fd); printf(" done.\r\n"); printf("Re-opening file read-only."); fd = fopen("/fs/numbers.txt", "r"); errno_error(fd); printf("Dumping file to screen.\r\n"); char buff[16] = { 0 }; while(!feof (fd)) { int size = fread(&buff[0], 1, 15, fd); fwrite(&buff[0], 1, size, stdout); } printf("EOF.\r\n"); printf("Closing file."); fclose(fd); printf(" done.\r\n"); printf("Opening root directory."); DIR* dir = opendir("/fs/"); errno_error(fd); struct dirent* de; printf("Printing all filenames:\r\n"); while((de = readdir (dir)) != NULL) { printf(" %s\r\n", &(de->d_name)[0]); } printf("Closeing root directory. "); error = closedir(dir); return_error(error); printf("Filesystem Demo complete.\r\n"); // Blink led with 2 Hz while(true) { led = !led; wait (0.5); } }
Diff: BD_SD_DISCO_F746NG.h
- Revision:
- 1:d1d99cfe13df
- Parent:
- 0:131c3e1d831e
diff -r 131c3e1d831e -r d1d99cfe13df BD_SD_DISCO_F746NG.h --- a/BD_SD_DISCO_F746NG.h Sat Mar 24 16:27:20 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,207 +0,0 @@ -/* SD/MMC Block device Library for MBED-OS - * Copyright 2017 Roy Krikke - * - * 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. - * - */ - -#ifndef BD_SD_DISCO_F746NG_H -#define BD_SD_DISCO_F746NG_H - -#include "mbed.h" -#include "BlockDevice.h" -#include "platform/PlatformMutex.h" -#include "stm32746g_discovery_sd.h" - -#define BD_SD_DISCO_F746NG_MAJOR_VERSION 1 -#define BD_SD_DISCO_F746NG_MINOR_VERSION 0 -#define BD_SD_DISCO_F746NG_PATCH_VERSION 0 - -/** - * BD_SD_DISCO_F746NG class. - * Block device class for creating a block device to access a SD/MMC card via SD/MMC interface on DISCO_F746NG - * - * Example: - * @code - * #include "mbed.h" - * #include "BD_SD_DISCO_F746NG.h" - * - * DigitalOut led (LED1); - * - * // Instantiate the Block Device for sd card on DISCO-F746NG - * BD_SD_DISCO_F746NG bd; - * uint8_t block[512] = "Hello World!\n"; - * - * int - * main () { - * Serial pc (SERIAL_TX, SERIAL_RX); - * pc.baud (115200); - * printf ("Start\n"); - * - * // Call the BD_SD_DISCO_F746NG instance initialisation method. - * printf ("sd card init...\n"); - * if (0 != bd.init ()) { - * printf ("Init failed \n"); - * return -1; - * } - * - * printf ("sd size: %llu\n", bd.size ()); - * printf ("sd read size: %llu\n", bd.get_read_size ()); - * printf ("sd program size: %llu\n", bd.get_program_size ()); - * printf ("sd erase size: %llu\n\n", bd.get_erase_size ()); - * - * printf ("sd erase...\n"); - * if (0 != bd.erase (0, bd.get_erase_size ())) { - * printf ("Error Erasing block \n"); - * } - * - * // Write some the data block to the device - * printf ("sd write: %s\n", block); - * if (0 == bd.program (block, 0, 512)) { - * // read the data block from the device - * printf ("sd read: "); - * if (0 == bd.read (block, 0, 512)) { - * // print the contents of the block - * printf ("%s", block); - * } - * } - * - * // Call the BD_SD_DISCO_F746NG instance de-initialisation method. - * printf ("sd card deinit...\n"); - * if (0 != bd.deinit ()) { - * printf ("Deinit failed \n"); - * return -1; - * } - * - * // Blink led with 2 Hz - * while (true) { - * led = !led; - * wait (0.5); - * } - * } - * @endcode - * - */ -class BD_SD_DISCO_F746NG : public BlockDevice { - public: - - /** Lifetime of the memory block device - * - * Only a block size of 512 bytes is supported - * - */ - BD_SD_DISCO_F746NG (); - virtual - ~BD_SD_DISCO_F746NG (); - - /** Initialize a block device - * - * @return 0 on success or a negative error code on failure - */ - virtual int - init (); - - /** Deinitialize a block device - * - * @return 0 on success or a negative error code on failure - */ - virtual int - deinit (); - - /** Read blocks from a block device - * - * @param buffer Buffer to read blocks into - * @param addr Address of block to begin reading from - * @param size Size to read in bytes, must be a multiple of read block size - * @return 0 on success, negative error code on failure - */ - virtual int - read (void *buffer, bd_addr_t addr, bd_size_t size); - - /** Program blocks to a block device - * - * The blocks must have been erased prior to being programmed - * - * @param buffer Buffer of data to write to blocks - * @param addr Address of block to begin writing to - * @param size Size to write in bytes, must be a multiple of program block size - * @return 0 on success, negative error code on failure - */ - virtual int - program (const void *buffer, bd_addr_t addr, bd_size_t size); - - /** Erase blocks on a block device - * - * The state of an erased block is undefined until it has been programmed - * - * @param addr Address of block to begin erasing - * @param size Size to erase in bytes, must be a multiple of erase block size - * @return 0 on success, negative error code on failure - */ - virtual int - erase (bd_addr_t addr, bd_size_t size); - - /** Get the size of a readable block - * - * @return Size of a readable block in bytes - */ - virtual bd_size_t - get_read_size () const; - - /** Get the size of a programable block - * - * @return Size of a programable block in bytes - */ - virtual bd_size_t - get_program_size () const; - - /** Get the size of a eraseable block - * - * @return Size of a eraseable block in bytes - */ - virtual bd_size_t - get_erase_size () const; - - /** Get the total size of the underlying device - * - * @return Size of the underlying device in bytes - */ - virtual bd_size_t - size () const; - - private: - uint8_t _card_type; - bd_size_t _read_size; - bd_size_t _program_size; - bd_size_t _erase_size; - bd_size_t _block_size; - bd_size_t _capacity_in_blocks; - BSP_SD_CardInfo _current_card_info; - uint8_t _sd_state; - uint32_t _timeout; - PlatformMutex _mutex;bool _is_initialized; - - virtual void - lock () { - _mutex.lock (); - } - - virtual void - unlock () { - _mutex.unlock (); - } - -}; - -#endif /* BD_SD_DISCO_F746NG_H */ -