This demo reads a bitmap from a FAT formatted SD-card, copies it to flash and displays it on the screen. The demo is based on the following project: https://os.mbed.com/users/DieterGraef/code/DISCO-F746NG_SDFileSystem/
Dependencies: LCD_DISCO_F746NG TS_DISCO_F746NG mbed FATFileSystem
Fork of DISCO-F746NG_SDFileSystem by
SDFileSystem/SDFileSystem.h@4:95e30a911d97, 2018-04-19 (annotated)
- Committer:
- Lightsource
- Date:
- Thu Apr 19 19:59:54 2018 +0000
- Revision:
- 4:95e30a911d97
Demo that reads a bitmap from SD-card, copies it to flash and displays it on the screen.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Lightsource | 4:95e30a911d97 | 1 | /* SD/MMC File System Library |
Lightsource | 4:95e30a911d97 | 2 | * Copyright (c) 2016 Neil Thiessen |
Lightsource | 4:95e30a911d97 | 3 | * Modified for the use with STM32F746 Discovery (C) 2016 Dieter Greaf |
Lightsource | 4:95e30a911d97 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Lightsource | 4:95e30a911d97 | 5 | * you may not use this file except in compliance with the License. |
Lightsource | 4:95e30a911d97 | 6 | * You may obtain a copy of the License at |
Lightsource | 4:95e30a911d97 | 7 | * |
Lightsource | 4:95e30a911d97 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Lightsource | 4:95e30a911d97 | 9 | * |
Lightsource | 4:95e30a911d97 | 10 | * Unless required by applicable law or agreed to in writing, software |
Lightsource | 4:95e30a911d97 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Lightsource | 4:95e30a911d97 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Lightsource | 4:95e30a911d97 | 13 | * See the License for the specific language governing permissions and |
Lightsource | 4:95e30a911d97 | 14 | * limitations under the License. |
Lightsource | 4:95e30a911d97 | 15 | */ |
Lightsource | 4:95e30a911d97 | 16 | |
Lightsource | 4:95e30a911d97 | 17 | #ifndef SD_FILE_SYSTEM_H |
Lightsource | 4:95e30a911d97 | 18 | #define SD_FILE_SYSTEM_H |
Lightsource | 4:95e30a911d97 | 19 | |
Lightsource | 4:95e30a911d97 | 20 | #include "mbed.h" |
Lightsource | 4:95e30a911d97 | 21 | #include "stm32746g_discovery_sd.h" |
Lightsource | 4:95e30a911d97 | 22 | #include "FATFileSystem.h" |
Lightsource | 4:95e30a911d97 | 23 | |
Lightsource | 4:95e30a911d97 | 24 | /** SDFileSystem class. |
Lightsource | 4:95e30a911d97 | 25 | * Used for creating a virtual file system for accessing SD/MMC cards via SPI. |
Lightsource | 4:95e30a911d97 | 26 | * |
Lightsource | 4:95e30a911d97 | 27 | * Example: |
Lightsource | 4:95e30a911d97 | 28 | * @code |
Lightsource | 4:95e30a911d97 | 29 | * #include "mbed.h" |
Lightsource | 4:95e30a911d97 | 30 | * #include "SDFileSystem.h" |
Lightsource | 4:95e30a911d97 | 31 | * |
Lightsource | 4:95e30a911d97 | 32 | * //Create an SDFileSystem object |
Lightsource | 4:95e30a911d97 | 33 | * SDFileSystem sd("sd"); |
Lightsource | 4:95e30a911d97 | 34 | * |
Lightsource | 4:95e30a911d97 | 35 | * int main() |
Lightsource | 4:95e30a911d97 | 36 | * { |
Lightsource | 4:95e30a911d97 | 37 | * //Mount the filesystem |
Lightsource | 4:95e30a911d97 | 38 | * sd.mount(); |
Lightsource | 4:95e30a911d97 | 39 | * |
Lightsource | 4:95e30a911d97 | 40 | * //Perform a write test |
Lightsource | 4:95e30a911d97 | 41 | * printf("\nWriting to SD card..."); |
Lightsource | 4:95e30a911d97 | 42 | * FILE *fp = fopen("/sd/sdtest.txt", "w"); |
Lightsource | 4:95e30a911d97 | 43 | * if (fp != NULL) { |
Lightsource | 4:95e30a911d97 | 44 | * fprintf(fp, "We're writing to an SD card!"); |
Lightsource | 4:95e30a911d97 | 45 | * fclose(fp); |
Lightsource | 4:95e30a911d97 | 46 | * printf("success!\n"); |
Lightsource | 4:95e30a911d97 | 47 | * } else { |
Lightsource | 4:95e30a911d97 | 48 | * printf("failed!\n"); |
Lightsource | 4:95e30a911d97 | 49 | * } |
Lightsource | 4:95e30a911d97 | 50 | * |
Lightsource | 4:95e30a911d97 | 51 | * //Perform a read test |
Lightsource | 4:95e30a911d97 | 52 | * printf("Reading from SD card..."); |
Lightsource | 4:95e30a911d97 | 53 | * fp = fopen("/sd/sdtest.txt", "r"); |
Lightsource | 4:95e30a911d97 | 54 | * if (fp != NULL) { |
Lightsource | 4:95e30a911d97 | 55 | * char c = fgetc(fp); |
Lightsource | 4:95e30a911d97 | 56 | * if (c == 'W') |
Lightsource | 4:95e30a911d97 | 57 | * printf("success!\n"); |
Lightsource | 4:95e30a911d97 | 58 | * else |
Lightsource | 4:95e30a911d97 | 59 | * printf("incorrect char (%c)!\n", c); |
Lightsource | 4:95e30a911d97 | 60 | * fclose(fp); |
Lightsource | 4:95e30a911d97 | 61 | * } else { |
Lightsource | 4:95e30a911d97 | 62 | * printf("failed!\n"); |
Lightsource | 4:95e30a911d97 | 63 | * } |
Lightsource | 4:95e30a911d97 | 64 | * |
Lightsource | 4:95e30a911d97 | 65 | * //Unmount the filesystem |
Lightsource | 4:95e30a911d97 | 66 | * sd.unmount(); |
Lightsource | 4:95e30a911d97 | 67 | * } |
Lightsource | 4:95e30a911d97 | 68 | * @endcode |
Lightsource | 4:95e30a911d97 | 69 | |
Lightsource | 4:95e30a911d97 | 70 | */ |
Lightsource | 4:95e30a911d97 | 71 | #define CMD59 (0x40 | 59) |
Lightsource | 4:95e30a911d97 | 72 | using namespace mbed; |
Lightsource | 4:95e30a911d97 | 73 | |
Lightsource | 4:95e30a911d97 | 74 | class SDFileSystem : public FATFileSystem |
Lightsource | 4:95e30a911d97 | 75 | { |
Lightsource | 4:95e30a911d97 | 76 | public: |
Lightsource | 4:95e30a911d97 | 77 | /** Represents the different card detect switch types |
Lightsource | 4:95e30a911d97 | 78 | */ |
Lightsource | 4:95e30a911d97 | 79 | enum SwitchType { |
Lightsource | 4:95e30a911d97 | 80 | SWITCH_NONE, /**< No card detect switch (assumes socket is always occupied) */ |
Lightsource | 4:95e30a911d97 | 81 | SWITCH_POS_NO, /**< Switch shorts to VDD when the socket is occupied (positive logic, normally open) */ |
Lightsource | 4:95e30a911d97 | 82 | SWITCH_POS_NC, /**< Switch shorts to VDD when the socket is empty (positive logic, normally closed) */ |
Lightsource | 4:95e30a911d97 | 83 | SWITCH_NEG_NO, /**< Switch shorts to GND when the socket is occupied (negative logic, normally open) */ |
Lightsource | 4:95e30a911d97 | 84 | SWITCH_NEG_NC /**< Switch shorts to GND when the socket is empty (negative logic, normally closed) */ |
Lightsource | 4:95e30a911d97 | 85 | }; |
Lightsource | 4:95e30a911d97 | 86 | |
Lightsource | 4:95e30a911d97 | 87 | /** Represents the different SD/MMC card types |
Lightsource | 4:95e30a911d97 | 88 | */ |
Lightsource | 4:95e30a911d97 | 89 | enum CardType { |
Lightsource | 4:95e30a911d97 | 90 | CARD_NONE, /**< No card is present */ |
Lightsource | 4:95e30a911d97 | 91 | CARD_MMC, /**< MMC card */ |
Lightsource | 4:95e30a911d97 | 92 | CARD_SD, /**< Standard capacity SD card */ |
Lightsource | 4:95e30a911d97 | 93 | CARD_SDHC, /**< High capacity SD card */ |
Lightsource | 4:95e30a911d97 | 94 | CARD_UNKNOWN /**< Unknown or unsupported card */ |
Lightsource | 4:95e30a911d97 | 95 | }; |
Lightsource | 4:95e30a911d97 | 96 | |
Lightsource | 4:95e30a911d97 | 97 | /** Create a virtual file system for accessing SD/MMC cards via SPI |
Lightsource | 4:95e30a911d97 | 98 | * |
Lightsource | 4:95e30a911d97 | 99 | * @param mosi The SPI data out pin. |
Lightsource | 4:95e30a911d97 | 100 | * @param miso The SPI data in pin. |
Lightsource | 4:95e30a911d97 | 101 | * @param sclk The SPI clock pin. |
Lightsource | 4:95e30a911d97 | 102 | * @param cs The SPI chip select pin. |
Lightsource | 4:95e30a911d97 | 103 | * @param name The name used to access the virtual filesystem. |
Lightsource | 4:95e30a911d97 | 104 | * @param cd The card detect pin. |
Lightsource | 4:95e30a911d97 | 105 | * @param cdtype The type of card detect switch. |
Lightsource | 4:95e30a911d97 | 106 | * @param hz The SPI bus frequency (defaults to 1MHz). |
Lightsource | 4:95e30a911d97 | 107 | */ |
Lightsource | 4:95e30a911d97 | 108 | SDFileSystem( const char* name); |
Lightsource | 4:95e30a911d97 | 109 | |
Lightsource | 4:95e30a911d97 | 110 | /** Determine whether or not a card is present |
Lightsource | 4:95e30a911d97 | 111 | * |
Lightsource | 4:95e30a911d97 | 112 | * @returns |
Lightsource | 4:95e30a911d97 | 113 | * 'true' if a card is present, |
Lightsource | 4:95e30a911d97 | 114 | * 'false' if no card is present. |
Lightsource | 4:95e30a911d97 | 115 | */ |
Lightsource | 4:95e30a911d97 | 116 | bool card_present(); |
Lightsource | 4:95e30a911d97 | 117 | |
Lightsource | 4:95e30a911d97 | 118 | /** Get the detected SD/MMC card type |
Lightsource | 4:95e30a911d97 | 119 | * |
Lightsource | 4:95e30a911d97 | 120 | * @returns The detected card type as a CardType enum. |
Lightsource | 4:95e30a911d97 | 121 | * |
Lightsource | 4:95e30a911d97 | 122 | * @note Valid after the filesystem has been mounted. |
Lightsource | 4:95e30a911d97 | 123 | */ |
Lightsource | 4:95e30a911d97 | 124 | SDFileSystem::CardType card_type(); |
Lightsource | 4:95e30a911d97 | 125 | |
Lightsource | 4:95e30a911d97 | 126 | /** Get whether or not CRC is enabled for commands and data |
Lightsource | 4:95e30a911d97 | 127 | * |
Lightsource | 4:95e30a911d97 | 128 | * @returns |
Lightsource | 4:95e30a911d97 | 129 | * 'true' if CRC is enabled for commands and data, |
Lightsource | 4:95e30a911d97 | 130 | * 'false' if CRC is disabled for commands and data. |
Lightsource | 4:95e30a911d97 | 131 | */ |
Lightsource | 4:95e30a911d97 | 132 | bool crc(); |
Lightsource | 4:95e30a911d97 | 133 | |
Lightsource | 4:95e30a911d97 | 134 | /** Set whether or not CRC is enabled for commands and data |
Lightsource | 4:95e30a911d97 | 135 | * |
Lightsource | 4:95e30a911d97 | 136 | * @param enabled Whether or not to enable CRC for commands and data. |
Lightsource | 4:95e30a911d97 | 137 | */ |
Lightsource | 4:95e30a911d97 | 138 | void crc(bool enabled); |
Lightsource | 4:95e30a911d97 | 139 | |
Lightsource | 4:95e30a911d97 | 140 | /** Get whether or not 16-bit frames are enabled for data read/write operations |
Lightsource | 4:95e30a911d97 | 141 | * |
Lightsource | 4:95e30a911d97 | 142 | * @returns |
Lightsource | 4:95e30a911d97 | 143 | * 'true' if 16-bit frames will be used during data read/write operations, |
Lightsource | 4:95e30a911d97 | 144 | * 'false' if 8-bit frames will be used during data read/write operations. |
Lightsource | 4:95e30a911d97 | 145 | */ |
Lightsource | 4:95e30a911d97 | 146 | bool large_frames(); |
Lightsource | 4:95e30a911d97 | 147 | |
Lightsource | 4:95e30a911d97 | 148 | /** Set whether or not 16-bit frames are enabled for data read/write operations |
Lightsource | 4:95e30a911d97 | 149 | * |
Lightsource | 4:95e30a911d97 | 150 | * @param enabled Whether or not 16-bit frames are enabled for data read/write operations. |
Lightsource | 4:95e30a911d97 | 151 | */ |
Lightsource | 4:95e30a911d97 | 152 | void large_frames(bool enabled); |
Lightsource | 4:95e30a911d97 | 153 | |
Lightsource | 4:95e30a911d97 | 154 | /** Get whether or not write validation is enabled for data write operations |
Lightsource | 4:95e30a911d97 | 155 | * |
Lightsource | 4:95e30a911d97 | 156 | * @returns |
Lightsource | 4:95e30a911d97 | 157 | * 'true' if data writes will be verified using CMD13, |
Lightsource | 4:95e30a911d97 | 158 | * 'false' if data writes will not be verified. |
Lightsource | 4:95e30a911d97 | 159 | */ |
Lightsource | 4:95e30a911d97 | 160 | bool write_validation(); |
Lightsource | 4:95e30a911d97 | 161 | |
Lightsource | 4:95e30a911d97 | 162 | /** Set whether or not write validation is enabled for data write operations |
Lightsource | 4:95e30a911d97 | 163 | * |
Lightsource | 4:95e30a911d97 | 164 | * @param enabled Whether or not write validation is enabled for data write operations. |
Lightsource | 4:95e30a911d97 | 165 | */ |
Lightsource | 4:95e30a911d97 | 166 | void write_validation(bool enabled); |
Lightsource | 4:95e30a911d97 | 167 | |
Lightsource | 4:95e30a911d97 | 168 | virtual int unmount(); |
Lightsource | 4:95e30a911d97 | 169 | virtual int disk_initialize(); |
Lightsource | 4:95e30a911d97 | 170 | virtual int disk_status(); |
Lightsource | 4:95e30a911d97 | 171 | virtual int disk_read(uint8_t* buffer, uint32_t sector, uint32_t count); |
Lightsource | 4:95e30a911d97 | 172 | virtual int disk_write(const uint8_t* buffer, uint32_t sector, uint32_t count); |
Lightsource | 4:95e30a911d97 | 173 | virtual int disk_sync(); |
Lightsource | 4:95e30a911d97 | 174 | virtual uint32_t disk_sectors(); |
Lightsource | 4:95e30a911d97 | 175 | |
Lightsource | 4:95e30a911d97 | 176 | private: |
Lightsource | 4:95e30a911d97 | 177 | Timer m_Timer; |
Lightsource | 4:95e30a911d97 | 178 | InterruptIn m_Cd; |
Lightsource | 4:95e30a911d97 | 179 | int m_CdAssert; |
Lightsource | 4:95e30a911d97 | 180 | SDFileSystem::CardType m_CardType; |
Lightsource | 4:95e30a911d97 | 181 | bool m_Crc; |
Lightsource | 4:95e30a911d97 | 182 | bool m_LargeFrames; |
Lightsource | 4:95e30a911d97 | 183 | bool m_WriteValidation; |
Lightsource | 4:95e30a911d97 | 184 | int m_Status; |
Lightsource | 4:95e30a911d97 | 185 | HAL_SD_CardInfoTypedef m_CardInfo; |
Lightsource | 4:95e30a911d97 | 186 | //Internal methods |
Lightsource | 4:95e30a911d97 | 187 | void onCardRemoval(); |
Lightsource | 4:95e30a911d97 | 188 | void checkSocket(); |
Lightsource | 4:95e30a911d97 | 189 | void DMA2_Stream3_IRQHandler(); |
Lightsource | 4:95e30a911d97 | 190 | void DMA2_Stream6_IRQHandler(); |
Lightsource | 4:95e30a911d97 | 191 | void SDMMC1_IRQHandler(); |
Lightsource | 4:95e30a911d97 | 192 | }; |
Lightsource | 4:95e30a911d97 | 193 | |
Lightsource | 4:95e30a911d97 | 194 | #endif |