Simple SD File System test using mbed-os, FATFileSystem + sd-driver

Dependencies:   sd-driver

Fork of mbed-os-example-fat-filesystem by mbed-os-examples

Committer:
loopsva
Date:
Wed Aug 02 21:47:39 2017 +0000
Revision:
9:4cbf1601a4a5
Parent:
0:ab69df6f1c47
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:ab69df6f1c47 1 # Getting started with Fat filesystem on mbed OS
mbed_official 0:ab69df6f1c47 2
mbed_official 0:ab69df6f1c47 3 This guide reviews the steps to get the FAT filesystem working on an mbed OS platform.
mbed_official 0:ab69df6f1c47 4
mbed_official 0:ab69df6f1c47 5 Please install [mbed CLI](https://github.com/ARMmbed/mbed-cli#installing-mbed-cli).
mbed_official 0:ab69df6f1c47 6
mbed_official 0:ab69df6f1c47 7 ## Hardware requirements
mbed_official 0:ab69df6f1c47 8
mbed_official 0:ab69df6f1c47 9 This exmaple uses a RAM backed FAT filesystem. The FAT filesystem requires at least 128 512-byte blocks for a total of 64KB of space. This space is backed by the HeapBlockDevice, which uses a target's heap for storage. Therefore, to support this example, a target must have at least 64KB of space usable by as heap. As there is more than just the heap in a device's ram this translates to the requirement that a target's RAM must be at least 96KB large.
mbed_official 0:ab69df6f1c47 10
mbed_official 0:ab69df6f1c47 11 ## Import the example application
mbed_official 0:ab69df6f1c47 12
mbed_official 0:ab69df6f1c47 13 From the command-line, import the example:
mbed_official 0:ab69df6f1c47 14
mbed_official 0:ab69df6f1c47 15 ```
mbed_official 0:ab69df6f1c47 16 mbed import mbed-os-example-fat-filesystem
mbed_official 0:ab69df6f1c47 17 cd mbed-os-example-fat-filesystem
mbed_official 0:ab69df6f1c47 18 ```
mbed_official 0:ab69df6f1c47 19
mbed_official 0:ab69df6f1c47 20 ### Now compile
mbed_official 0:ab69df6f1c47 21
mbed_official 0:ab69df6f1c47 22 Invoke `mbed compile`, and specify the name of your platform and your favorite toolchain (`GCC_ARM`, `ARM`, `IAR`). For example, for the ARM Compiler 5:
mbed_official 0:ab69df6f1c47 23
mbed_official 0:ab69df6f1c47 24 ```
mbed_official 0:ab69df6f1c47 25 mbed compile -m K64F -t ARM
mbed_official 0:ab69df6f1c47 26 ```
mbed_official 0:ab69df6f1c47 27
mbed_official 0:ab69df6f1c47 28 Your PC may take a few minutes to compile your code. At the end, you see the following result:
mbed_official 0:ab69df6f1c47 29
mbed_official 0:ab69df6f1c47 30 ```
mbed_official 0:ab69df6f1c47 31 [snip]
mbed_official 0:ab69df6f1c47 32 +--------------------------+-------+-------+-------+
mbed_official 0:ab69df6f1c47 33 | Module | .text | .data | .bss |
mbed_official 0:ab69df6f1c47 34 +--------------------------+-------+-------+-------+
mbed_official 0:ab69df6f1c47 35 | Fill | 164 | 0 | 2136 |
mbed_official 0:ab69df6f1c47 36 | Misc | 54505 | 2556 | 754 |
mbed_official 0:ab69df6f1c47 37 | drivers | 640 | 0 | 32 |
mbed_official 0:ab69df6f1c47 38 | features/filesystem | 15793 | 0 | 550 |
mbed_official 0:ab69df6f1c47 39 | features/storage | 42 | 0 | 184 |
mbed_official 0:ab69df6f1c47 40 | hal | 418 | 0 | 8 |
mbed_official 0:ab69df6f1c47 41 | platform | 2355 | 20 | 582 |
mbed_official 0:ab69df6f1c47 42 | rtos | 135 | 4 | 4 |
mbed_official 0:ab69df6f1c47 43 | rtos/rtx | 5861 | 20 | 6870 |
mbed_official 0:ab69df6f1c47 44 | targets/TARGET_Freescale | 8382 | 12 | 384 |
mbed_official 0:ab69df6f1c47 45 | Subtotals | 88295 | 2612 | 11504 |
mbed_official 0:ab69df6f1c47 46 +--------------------------+-------+-------+-------+
mbed_official 0:ab69df6f1c47 47 Allocated Heap: 24576 bytes
mbed_official 0:ab69df6f1c47 48 Allocated Stack: unknown
mbed_official 0:ab69df6f1c47 49 Total Static RAM memory (data + bss): 14116 bytes
mbed_official 0:ab69df6f1c47 50 Total RAM memory (data + bss + heap + stack): 38692 bytes
mbed_official 0:ab69df6f1c47 51 Total Flash memory (text + data + misc): 91947 bytes
mbed_official 0:ab69df6f1c47 52
mbed_official 0:ab69df6f1c47 53 Image: ./BUILD/K64F/gcc_arm/mbed-os-example-fat-filesystem.bin
mbed_official 0:ab69df6f1c47 54 ```
mbed_official 0:ab69df6f1c47 55
mbed_official 0:ab69df6f1c47 56 ### Program your board
mbed_official 0:ab69df6f1c47 57
mbed_official 0:ab69df6f1c47 58 1. Connect your mbed device to the computer over USB.
mbed_official 0:ab69df6f1c47 59 1. Copy the binary file to the mbed device.
mbed_official 0:ab69df6f1c47 60 1. Press the reset button to start the program.
mbed_official 0:ab69df6f1c47 61 1. Open the UART of the board in your favorite UART viewing program. For example, `screen /dev/ttyACM0`.
mbed_official 0:ab69df6f1c47 62
mbed_official 0:ab69df6f1c47 63 You see the following output:
mbed_official 0:ab69df6f1c47 64
mbed_official 0:ab69df6f1c47 65 ```
mbed_official 0:ab69df6f1c47 66 Welcome to the filesystem example.
mbed_official 0:ab69df6f1c47 67 Formatting a FAT, RAM-backed filesystem. done.
mbed_official 0:ab69df6f1c47 68 Mounting the filesystem on "/fs". done.
mbed_official 0:ab69df6f1c47 69 Opening a new file, numbers.txt. done.
mbed_official 0:ab69df6f1c47 70 Writing decimal numbers to a file (20/20) done.
mbed_official 0:ab69df6f1c47 71 Closing file. done.
mbed_official 0:ab69df6f1c47 72 Re-opening file read-only. done.
mbed_official 0:ab69df6f1c47 73 Dumping file to screen.
mbed_official 0:ab69df6f1c47 74 0
mbed_official 0:ab69df6f1c47 75 1
mbed_official 0:ab69df6f1c47 76 2
mbed_official 0:ab69df6f1c47 77 3
mbed_official 0:ab69df6f1c47 78 4
mbed_official 0:ab69df6f1c47 79 5
mbed_official 0:ab69df6f1c47 80 6
mbed_official 0:ab69df6f1c47 81 7
mbed_official 0:ab69df6f1c47 82 8
mbed_official 0:ab69df6f1c47 83 9
mbed_official 0:ab69df6f1c47 84 10
mbed_official 0:ab69df6f1c47 85 11
mbed_official 0:ab69df6f1c47 86 12
mbed_official 0:ab69df6f1c47 87 13
mbed_official 0:ab69df6f1c47 88 14
mbed_official 0:ab69df6f1c47 89 15
mbed_official 0:ab69df6f1c47 90 16
mbed_official 0:ab69df6f1c47 91 17
mbed_official 0:ab69df6f1c47 92 18
mbed_official 0:ab69df6f1c47 93 19
mbed_official 0:ab69df6f1c47 94 EOF.
mbed_official 0:ab69df6f1c47 95 Closing file. done.
mbed_official 0:ab69df6f1c47 96 Opening root directory. done.
mbed_official 0:ab69df6f1c47 97 Printing all filenames:
mbed_official 0:ab69df6f1c47 98 numbers.txt
mbed_official 0:ab69df6f1c47 99 Closeing root directory. done.
mbed_official 0:ab69df6f1c47 100 Filesystem Demo complete.
mbed_official 0:ab69df6f1c47 101
mbed_official 0:ab69df6f1c47 102 ```
mbed_official 0:ab69df6f1c47 103
mbed_official 0:ab69df6f1c47 104 ## Switch from RAM backed block device to an SD card
mbed_official 0:ab69df6f1c47 105
mbed_official 0:ab69df6f1c47 106 From the command-line, run the following command:
mbed_official 0:ab69df6f1c47 107
mbed_official 0:ab69df6f1c47 108 ```bash
mbed_official 0:ab69df6f1c47 109 mbed add sd-driver
mbed_official 0:ab69df6f1c47 110 ```
mbed_official 0:ab69df6f1c47 111
mbed_official 0:ab69df6f1c47 112 Then change the code on line 3 of `main.cpp` to import the SD card header:
mbed_official 0:ab69df6f1c47 113
mbed_official 0:ab69df6f1c47 114 ```C
mbed_official 0:ab69df6f1c47 115 #include "SDBlockDevice.h"
mbed_official 0:ab69df6f1c47 116 ```
mbed_official 0:ab69df6f1c47 117
mbed_official 0:ab69df6f1c47 118 Change the block device declaration on line 7 of `main.cpp` to use the SD card by replacing the `PinName`s with the pins connected to the SD card:
mbed_official 0:ab69df6f1c47 119
mbed_official 0:ab69df6f1c47 120 ```C
mbed_official 0:ab69df6f1c47 121 SDBlockDevice bd(PinName mosi, PinName miso, PinName sclk, PinName cs);
mbed_official 0:ab69df6f1c47 122 ```
mbed_official 0:ab69df6f1c47 123
mbed_official 0:ab69df6f1c47 124 ## Troubleshooting
mbed_official 0:ab69df6f1c47 125
mbed_official 0:ab69df6f1c47 126 1. Make sure `mbed-cli` is working correctly and its version is newer than `1.0.0`.
mbed_official 0:ab69df6f1c47 127
mbed_official 0:ab69df6f1c47 128 ```
mbed_official 0:ab69df6f1c47 129 mbed --version
mbed_official 0:ab69df6f1c47 130 ```
mbed_official 0:ab69df6f1c47 131
mbed_official 0:ab69df6f1c47 132 If not, update it:
mbed_official 0:ab69df6f1c47 133
mbed_official 0:ab69df6f1c47 134 ```
mbed_official 0:ab69df6f1c47 135 pip install mbed-cli --upgrade
mbed_official 0:ab69df6f1c47 136 ```