A library with drivers for different peripherals on the LPC4088 QuickStart Board or related add-on boards.

Dependencies:   FATFileSystem

Dependents:   LPC4088test LPC4088test_ledonly LPC4088test_deleteall LPC4088_RAMtest ... more

Committer:
embeddedartists
Date:
Wed Jun 10 08:34:09 2015 +0000
Revision:
20:e1e36493f347
Parent:
12:15597e45eea0
Fixed compiler error in MCIFileSystem regarding us_ticker_api.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 12:15597e45eea0 1 /*
embeddedartists 12:15597e45eea0 2 * Copyright 2013 Embedded Artists AB
embeddedartists 12:15597e45eea0 3 *
embeddedartists 12:15597e45eea0 4 * Licensed under the Apache License, Version 2.0 (the "License");
embeddedartists 12:15597e45eea0 5 * you may not use this file except in compliance with the License.
embeddedartists 12:15597e45eea0 6 * You may obtain a copy of the License at
embeddedartists 12:15597e45eea0 7 *
embeddedartists 12:15597e45eea0 8 * http://www.apache.org/licenses/LICENSE-2.0
embeddedartists 12:15597e45eea0 9 *
embeddedartists 12:15597e45eea0 10 * Unless required by applicable law or agreed to in writing, software
embeddedartists 12:15597e45eea0 11 * distributed under the License is distributed on an "AS IS" BASIS,
embeddedartists 12:15597e45eea0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
embeddedartists 12:15597e45eea0 13 * See the License for the specific language governing permissions and
embeddedartists 12:15597e45eea0 14 * limitations under the License.
embeddedartists 12:15597e45eea0 15 */
embeddedartists 12:15597e45eea0 16
embeddedartists 0:0fdadbc3d852 17 #ifndef QSPIFILESYSTEM_H
embeddedartists 0:0fdadbc3d852 18 #define QSPIFILESYSTEM_H
embeddedartists 0:0fdadbc3d852 19
embeddedartists 0:0fdadbc3d852 20 #include "mbed.h"
embeddedartists 0:0fdadbc3d852 21 #include "FileSystemLike.h"
embeddedartists 0:0fdadbc3d852 22
embeddedartists 0:0fdadbc3d852 23 /** Access the filesystem on an QSPI flash using SPIFI
embeddedartists 0:0fdadbc3d852 24 *
embeddedartists 2:1c6134c80dc5 25 * One way to utilize the 8MByte of QSPI FLASH on the LPC4088 QuickStart Board
embeddedartists 2:1c6134c80dc5 26 * is to place a file system on it. The QSPIFileSystem is using the
embeddedartists 2:1c6134c80dc5 27 * FileSystemLike interface making using it is as simple as:
embeddedartists 2:1c6134c80dc5 28 *
embeddedartists 0:0fdadbc3d852 29 * @code
embeddedartists 0:0fdadbc3d852 30 * #include "mbed.h"
embeddedartists 0:0fdadbc3d852 31 * #include "QSPIFileSystem.h"
embeddedartists 0:0fdadbc3d852 32 *
embeddedartists 10:f2409dc07e49 33 * QSPIFileSystem qspifs("qspi");
embeddedartists 0:0fdadbc3d852 34 *
embeddedartists 0:0fdadbc3d852 35 * int main() {
embeddedartists 10:f2409dc07e49 36 * if (!qspifs.isformatted()) {
embeddedartists 0:0fdadbc3d852 37 * qspifs.format();
embeddedartists 0:0fdadbc3d852 38 * }
embeddedartists 0:0fdadbc3d852 39 *
embeddedartists 0:0fdadbc3d852 40 * FILE *fp = fopen("/qspi/myfile.txt", "w");
embeddedartists 0:0fdadbc3d852 41 * if (fp != NULL) {
embeddedartists 10:f2409dc07e49 42 * fwrite("Hello World!", 12, 1, fp);
embeddedartists 0:0fdadbc3d852 43 * fclose(fp);
embeddedartists 0:0fdadbc3d852 44 * }
embeddedartists 0:0fdadbc3d852 45 * }
embeddedartists 0:0fdadbc3d852 46 * @endcode
embeddedartists 2:1c6134c80dc5 47 *
embeddedartists 2:1c6134c80dc5 48 * The file system can be formatted to a specific size in increments of 1MByte
embeddedartists 2:1c6134c80dc5 49 * and will allways be placed at the top of the address range. For the 8 MByte
embeddedartists 2:1c6134c80dc5 50 * memory on the LPC4088 QuickStart Board this means:
embeddedartists 2:1c6134c80dc5 51 *
embeddedartists 2:1c6134c80dc5 52 * <pre>
embeddedartists 2:1c6134c80dc5 53 * 0x28000000 0x28800000
embeddedartists 2:1c6134c80dc5 54 * |------|------|------|------|------|------|------|------|
embeddedartists 2:1c6134c80dc5 55 * qspifs.format(1) | available for program | FS |
embeddedartists 2:1c6134c80dc5 56 * |------|------|------|------|------|------|------|------|
embeddedartists 2:1c6134c80dc5 57 * </pre><pre>
embeddedartists 2:1c6134c80dc5 58 * |------|------|------|------|------|------|------|------|
embeddedartists 2:1c6134c80dc5 59 * qspifs.format(2) | available for program | FileSystem |
embeddedartists 2:1c6134c80dc5 60 * |------|------|------|------|------|------|------|------|
embeddedartists 2:1c6134c80dc5 61 * </pre><pre>
embeddedartists 2:1c6134c80dc5 62 * |------|------|------|------|------|------|------|------|
embeddedartists 2:1c6134c80dc5 63 * qspifs.format(7) | PROG | FileSystem |
embeddedartists 2:1c6134c80dc5 64 * |------|------|------|------|------|------|------|------|
embeddedartists 2:1c6134c80dc5 65 * </pre><pre>
embeddedartists 2:1c6134c80dc5 66 * |------|------|------|------|------|------|------|------|
embeddedartists 2:1c6134c80dc5 67 * qspifs.format(8) | FileSystem |
embeddedartists 2:1c6134c80dc5 68 * |------|------|------|------|------|------|------|------|
embeddedartists 2:1c6134c80dc5 69 * </pre>
embeddedartists 2:1c6134c80dc5 70 * The file system must be placed at the top of the memory because the linker scripts
embeddedartists 2:1c6134c80dc5 71 * places your program at the bottom of the memory (if needed).
embeddedartists 2:1c6134c80dc5 72 *
embeddedartists 2:1c6134c80dc5 73 * The file system is using one or more blocks (each the size of one erase block) per
embeddedartists 2:1c6134c80dc5 74 * file. Each file is stored in a sequence of blocks. If a file is written to so that
embeddedartists 2:1c6134c80dc5 75 * it's size will occupy more than one block and the block after is already used by
embeddedartists 2:1c6134c80dc5 76 * a different file the entire file will be moved, like this:
embeddedartists 2:1c6134c80dc5 77 *
embeddedartists 2:1c6134c80dc5 78 * <pre>
embeddedartists 2:1c6134c80dc5 79 * |-------|-------|-------|-------|-------|-------|-------|
embeddedartists 2:1c6134c80dc5 80 * Before |///////| FILE1 | FILE2 | FILE3 |///////|///////|///////|
embeddedartists 2:1c6134c80dc5 81 * |-------|-------|-------|-------|-------|-------|-------|
embeddedartists 2:1c6134c80dc5 82 * </pre><pre>
embeddedartists 2:1c6134c80dc5 83 * After writing |-------|-------|-------|-------|-------|-------|-------|
embeddedartists 2:1c6134c80dc5 84 * to FILE2 |///////| FILE1 |///////| FILE3 | FILE2 |///////|
embeddedartists 2:1c6134c80dc5 85 * |-------|-------|-------|-------|-------|-------|-------|
embeddedartists 2:1c6134c80dc5 86 * </pre>
embeddedartists 2:1c6134c80dc5 87 *
embeddedartists 2:1c6134c80dc5 88 * <b>Note: </b>As each file takes up at least one block it will limit the total number of files that
embeddedartists 2:1c6134c80dc5 89 * can be stored on the file system. The formula for this is roughly
embeddedartists 2:1c6134c80dc5 90 * <pre>
embeddedartists 2:1c6134c80dc5 91 * max_num_files = fs_size_in_bytes / erase_block_size
embeddedartists 2:1c6134c80dc5 92 * </pre>
embeddedartists 2:1c6134c80dc5 93 * For the SPI flash on the LPC4088 QuickStart Board it means
embeddedartists 2:1c6134c80dc5 94 * <pre>
embeddedartists 2:1c6134c80dc5 95 * max_num_files = 8MByte / 4KByte = 2048
embeddedartists 2:1c6134c80dc5 96 * </pre>
embeddedartists 2:1c6134c80dc5 97 *
embeddedartists 2:1c6134c80dc5 98 * Some of the blocks are used to store the table of content (TOC) at the very end of
embeddedartists 2:1c6134c80dc5 99 * the spi flash. The TOC will contain a list of all blocks on the flash (even those
embeddedartists 2:1c6134c80dc5 100 * not used by the file system) and will mark each block as "reserved","in use" or "free".
embeddedartists 2:1c6134c80dc5 101 * For a file system that is 4MByte on a 8MByte flash half of the blocks will be marked
embeddedartists 2:1c6134c80dc5 102 * as "reserved".
embeddedartists 2:1c6134c80dc5 103 *
embeddedartists 2:1c6134c80dc5 104 * <b>Note: </b>The file system will not store any date/time information.
embeddedartists 2:1c6134c80dc5 105 *
embeddedartists 2:1c6134c80dc5 106 * <b>Note: </b>The file system will not store any file attributes (hidden/system/read only).
embeddedartists 2:1c6134c80dc5 107 *
embeddedartists 2:1c6134c80dc5 108 * <b>Note: </b>The file system stores the absolute path of each file (path + file name) in
embeddedartists 2:1c6134c80dc5 109 * the file's first block's first 256 bytes. Folders are never stored themselves. This has
embeddedartists 2:1c6134c80dc5 110 * the drawback that the file system cannot hold empty folders.
embeddedartists 0:0fdadbc3d852 111 */
embeddedartists 0:0fdadbc3d852 112 class QSPIFileSystem : public FileSystemLike {
embeddedartists 0:0fdadbc3d852 113 public:
embeddedartists 0:0fdadbc3d852 114
embeddedartists 0:0fdadbc3d852 115 /** Create the File System for accessing a QSPI Flash
embeddedartists 0:0fdadbc3d852 116 *
embeddedartists 0:0fdadbc3d852 117 * @param name The name used to access the virtual filesystem
embeddedartists 0:0fdadbc3d852 118 */
embeddedartists 0:0fdadbc3d852 119 QSPIFileSystem(const char* name);
embeddedartists 0:0fdadbc3d852 120
embeddedartists 0:0fdadbc3d852 121 virtual FileHandle *open(const char *filename, int flags);
embeddedartists 0:0fdadbc3d852 122 virtual int remove(const char *filename);
embeddedartists 0:0fdadbc3d852 123 virtual int rename(const char *oldname, const char *newname);
embeddedartists 0:0fdadbc3d852 124 virtual DirHandle *opendir(const char *name);
embeddedartists 0:0fdadbc3d852 125 virtual int mkdir(const char *name, mode_t mode);
embeddedartists 0:0fdadbc3d852 126
embeddedartists 0:0fdadbc3d852 127 /** Creates a new file system on the QSPI flash.
embeddedartists 0:0fdadbc3d852 128 * The file system will have the specified size and will always
embeddedartists 0:0fdadbc3d852 129 * be positioned at the end of the QSPI flash. If the fsSizeInMB is
embeddedartists 0:0fdadbc3d852 130 * less than the size of the QSPI flash the lower end of the flash
embeddedartists 0:0fdadbc3d852 131 * will be left untouched.
embeddedartists 0:0fdadbc3d852 132 *
embeddedartists 0:0fdadbc3d852 133 * @param fsSizeInMB The size of the file system
embeddedartists 0:0fdadbc3d852 134 *
embeddedartists 0:0fdadbc3d852 135 * @returns
embeddedartists 0:0fdadbc3d852 136 * 0 on success,
embeddedartists 0:0fdadbc3d852 137 * -1 on failure.
embeddedartists 0:0fdadbc3d852 138 */
embeddedartists 0:0fdadbc3d852 139 int format(unsigned int fsSizeInMB = 8);
embeddedartists 0:0fdadbc3d852 140
embeddedartists 0:0fdadbc3d852 141 /** Tests if there is a file system present on the QSPI flash
embeddedartists 0:0fdadbc3d852 142 *
embeddedartists 0:0fdadbc3d852 143 * @returns
embeddedartists 0:0fdadbc3d852 144 * True if a valid file system could be found,
embeddedartists 0:0fdadbc3d852 145 * False on failure.
embeddedartists 0:0fdadbc3d852 146 */
embeddedartists 0:0fdadbc3d852 147 bool isformatted();
embeddedartists 0:0fdadbc3d852 148
embeddedartists 0:0fdadbc3d852 149 /** Retrieves the start and end addresses for the file system.
embeddedartists 0:0fdadbc3d852 150 * The pStartAddr and pEndAddr will only be assigned values if the
embeddedartists 0:0fdadbc3d852 151 * function returns true.
embeddedartists 0:0fdadbc3d852 152 *
embeddedartists 0:0fdadbc3d852 153 * @param pStartAddr Will return the start of the file system area
embeddedartists 0:0fdadbc3d852 154 * @param pEndAddr Will return the end of the file system area
embeddedartists 0:0fdadbc3d852 155 *
embeddedartists 0:0fdadbc3d852 156 * @returns
embeddedartists 0:0fdadbc3d852 157 * True if there is a file system,
embeddedartists 0:0fdadbc3d852 158 * False on failure.
embeddedartists 0:0fdadbc3d852 159 */
embeddedartists 0:0fdadbc3d852 160 bool getMemoryBoundaries(uint32_t* pStartAddr, uint32_t* pEndAddr);
embeddedartists 0:0fdadbc3d852 161 };
embeddedartists 0:0fdadbc3d852 162
embeddedartists 0:0fdadbc3d852 163 #endif