Example of using block device class for creating a block device to access a SD/MMC card via SD/MMC interface on DISCO_F746NG
Dependencies: BD_SD_DISCO_F746NG
Example of using 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
main.cpp@2:d64219bde156, 2018-04-04 (annotated)
- Committer:
- roykrikke
- Date:
- Wed Apr 04 19:55:23 2018 +0000
- Revision:
- 2:d64219bde156
- Parent:
- 1:ffacc9d0b308
Code Style & Formatting
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
roykrikke | 1:ffacc9d0b308 | 1 | /* Example file of using SD/MMC Block device Library for MBED-OS |
roykrikke | 1:ffacc9d0b308 | 2 | * Copyright 2017 Roy Krikke |
roykrikke | 2:d64219bde156 | 3 | * |
roykrikke | 1:ffacc9d0b308 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
roykrikke | 1:ffacc9d0b308 | 5 | * you may not use this file except in compliance with the License. |
roykrikke | 1:ffacc9d0b308 | 6 | * You may obtain a copy of the License at |
roykrikke | 2:d64219bde156 | 7 | * |
roykrikke | 1:ffacc9d0b308 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
roykrikke | 2:d64219bde156 | 9 | * |
roykrikke | 1:ffacc9d0b308 | 10 | * Unless required by applicable law or agreed to in writing, software |
roykrikke | 1:ffacc9d0b308 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
roykrikke | 1:ffacc9d0b308 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
roykrikke | 1:ffacc9d0b308 | 13 | * See the License for the specific language governing permissions and |
roykrikke | 1:ffacc9d0b308 | 14 | * limitations under the License. |
roykrikke | 1:ffacc9d0b308 | 15 | * |
roykrikke | 1:ffacc9d0b308 | 16 | */ |
roykrikke | 1:ffacc9d0b308 | 17 | |
roykrikke | 0:4cdf28a602a8 | 18 | #include "mbed.h" |
roykrikke | 2:d64219bde156 | 19 | #include "SDBlockDeviceDISCOF746NG.h" |
roykrikke | 0:4cdf28a602a8 | 20 | |
roykrikke | 0:4cdf28a602a8 | 21 | DigitalOut led (LED1); |
roykrikke | 0:4cdf28a602a8 | 22 | |
roykrikke | 0:4cdf28a602a8 | 23 | // Instantiate the Block Device for sd card on DISCO-F746NG |
roykrikke | 2:d64219bde156 | 24 | SDBlockDeviceDISCOF746NG bd; |
roykrikke | 0:4cdf28a602a8 | 25 | uint8_t block[512] = "Hello World!\n"; |
roykrikke | 0:4cdf28a602a8 | 26 | |
roykrikke | 0:4cdf28a602a8 | 27 | int |
roykrikke | 2:d64219bde156 | 28 | main () |
roykrikke | 2:d64219bde156 | 29 | { |
roykrikke | 2:d64219bde156 | 30 | Serial pc (SERIAL_TX, SERIAL_RX); |
roykrikke | 2:d64219bde156 | 31 | pc.baud(115200); |
roykrikke | 2:d64219bde156 | 32 | printf("Start\n"); |
roykrikke | 2:d64219bde156 | 33 | |
roykrikke | 2:d64219bde156 | 34 | // Call the SDBlockDeviceDISCOF746NG instance initialisation method. |
roykrikke | 2:d64219bde156 | 35 | printf("sd card init...\n"); |
roykrikke | 2:d64219bde156 | 36 | if (0 != bd.init()) { |
roykrikke | 2:d64219bde156 | 37 | printf("Init failed \n"); |
roykrikke | 2:d64219bde156 | 38 | return -1; |
roykrikke | 2:d64219bde156 | 39 | } |
roykrikke | 0:4cdf28a602a8 | 40 | |
roykrikke | 2:d64219bde156 | 41 | printf("sd size: %llu\n", bd.size()); |
roykrikke | 2:d64219bde156 | 42 | printf("sd read size: %llu\n", bd.get_read_size()); |
roykrikke | 2:d64219bde156 | 43 | printf("sd program size: %llu\n", bd.get_program_size()); |
roykrikke | 2:d64219bde156 | 44 | printf("sd erase size: %llu\n\n", bd.get_erase_size()); |
roykrikke | 2:d64219bde156 | 45 | |
roykrikke | 2:d64219bde156 | 46 | printf("sd erase...\n"); |
roykrikke | 2:d64219bde156 | 47 | if (0 != bd.erase (0, bd.get_erase_size())) { |
roykrikke | 2:d64219bde156 | 48 | printf("Error Erasing block \n"); |
roykrikke | 2:d64219bde156 | 49 | } |
roykrikke | 0:4cdf28a602a8 | 50 | |
roykrikke | 2:d64219bde156 | 51 | // Write some the data block to the device |
roykrikke | 2:d64219bde156 | 52 | printf("sd write: %s\n", block); |
roykrikke | 2:d64219bde156 | 53 | if (0 == bd.program(block, 0, 512)) { |
roykrikke | 2:d64219bde156 | 54 | // read the data block from the device |
roykrikke | 2:d64219bde156 | 55 | printf("sd read: "); |
roykrikke | 2:d64219bde156 | 56 | if (0 == bd.read(block, 0, 512)) { |
roykrikke | 2:d64219bde156 | 57 | // print the contents of the block |
roykrikke | 2:d64219bde156 | 58 | printf("%s", block); |
roykrikke | 2:d64219bde156 | 59 | } |
roykrikke | 2:d64219bde156 | 60 | } |
roykrikke | 0:4cdf28a602a8 | 61 | |
roykrikke | 2:d64219bde156 | 62 | //////////////////////////////////////////////////////////////////////////////// |
roykrikke | 2:d64219bde156 | 63 | |
roykrikke | 2:d64219bde156 | 64 | for(int i; i < 10; i++) { |
roykrikke | 2:d64219bde156 | 65 | memset(block, i, sizeof(block)); |
roykrikke | 2:d64219bde156 | 66 | if(bd.program(block, (i*512), sizeof(block)) != 0) { |
roykrikke | 2:d64219bde156 | 67 | printf("Error at: %i\r\n",i); |
roykrikke | 2:d64219bde156 | 68 | } |
roykrikke | 0:4cdf28a602a8 | 69 | } |
roykrikke | 2:d64219bde156 | 70 | |
roykrikke | 2:d64219bde156 | 71 | if(bd.erase((1*512), sizeof(block)) != 0) { |
roykrikke | 2:d64219bde156 | 72 | printf("Error Erasing block\n"); |
roykrikke | 2:d64219bde156 | 73 | } |
roykrikke | 0:4cdf28a602a8 | 74 | |
roykrikke | 2:d64219bde156 | 75 | memset(block, 'x', sizeof(block)); |
roykrikke | 2:d64219bde156 | 76 | if(bd.program(block, (1*512), sizeof(block)) != 0) { |
roykrikke | 2:d64219bde156 | 77 | printf("Error\r\n"); |
roykrikke | 2:d64219bde156 | 78 | } |
roykrikke | 2:d64219bde156 | 79 | |
roykrikke | 2:d64219bde156 | 80 | //////////////////////////////////////////////////////////////////////////////// |
roykrikke | 0:4cdf28a602a8 | 81 | |
roykrikke | 2:d64219bde156 | 82 | // Call the BD_SD_DISCO_F746NG instance de-initialisation method. |
roykrikke | 2:d64219bde156 | 83 | printf("sd card deinit...\n"); |
roykrikke | 2:d64219bde156 | 84 | if (0 != bd.deinit()) { |
roykrikke | 2:d64219bde156 | 85 | printf ("Deinit failed \n"); |
roykrikke | 2:d64219bde156 | 86 | return -1; |
roykrikke | 2:d64219bde156 | 87 | } |
roykrikke | 2:d64219bde156 | 88 | |
roykrikke | 2:d64219bde156 | 89 | // Blink led with 2 Hz |
roykrikke | 2:d64219bde156 | 90 | while(true) { |
roykrikke | 2:d64219bde156 | 91 | led = !led; |
roykrikke | 2:d64219bde156 | 92 | wait (0.5); |
roykrikke | 2:d64219bde156 | 93 | } |
roykrikke | 0:4cdf28a602a8 | 94 | } |