Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: DISCO-F746NG_SDFileSystem uzairkhan DISCO-F746NG_Scope_copy
Fork of SDFileSystem by
SDFileSystem.cpp@26:8f15aa3b052b, 2016-04-13 (annotated)
- Committer:
- DieterGraef
- Date:
- Wed Apr 13 08:51:27 2016 +0000
- Revision:
- 26:8f15aa3b052b
- Parent:
- 25:391eade4ef85
- Child:
- 27:8d192c180436
solved cache issues.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| neilt6 | 0:2a6d8a096edc | 1 | /* SD/MMC File System Library |
| neilt6 | 22:3fa5eaf48e81 | 2 | * Copyright (c) 2016 Neil Thiessen |
| DieterGraef | 23:c03ef1abef0e | 3 | * Modified for the use with STM32F746 Discovery (C) 2016 Dieter Greaf |
| neilt6 | 0:2a6d8a096edc | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| neilt6 | 0:2a6d8a096edc | 5 | * you may not use this file except in compliance with the License. |
| neilt6 | 0:2a6d8a096edc | 6 | * You may obtain a copy of the License at |
| neilt6 | 0:2a6d8a096edc | 7 | * |
| neilt6 | 0:2a6d8a096edc | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| neilt6 | 0:2a6d8a096edc | 9 | * |
| neilt6 | 0:2a6d8a096edc | 10 | * Unless required by applicable law or agreed to in writing, software |
| neilt6 | 0:2a6d8a096edc | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| neilt6 | 0:2a6d8a096edc | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| neilt6 | 0:2a6d8a096edc | 13 | * See the License for the specific language governing permissions and |
| neilt6 | 0:2a6d8a096edc | 14 | * limitations under the License. |
| neilt6 | 0:2a6d8a096edc | 15 | */ |
| neilt6 | 0:2a6d8a096edc | 16 | |
| neilt6 | 0:2a6d8a096edc | 17 | #include "SDFileSystem.h" |
| neilt6 | 18:2286a4e7fa31 | 18 | #include "diskio.h" |
| neilt6 | 18:2286a4e7fa31 | 19 | #include "SDCRC.h" |
| DieterGraef | 23:c03ef1abef0e | 20 | //for cache flush function |
| DieterGraef | 23:c03ef1abef0e | 21 | #include "SD_Helper.h" |
| neilt6 | 0:2a6d8a096edc | 22 | |
| DieterGraef | 23:c03ef1abef0e | 23 | SDFileSystem::SDFileSystem( const char* name) |
| DieterGraef | 23:c03ef1abef0e | 24 | : FATFileSystem(name), m_Cd(PC_13) |
| neilt6 | 0:2a6d8a096edc | 25 | { |
| neilt6 | 0:2a6d8a096edc | 26 | //Initialize the member variables |
| DieterGraef | 23:c03ef1abef0e | 27 | uint8_t initstat; |
| DieterGraef | 23:c03ef1abef0e | 28 | void* h; |
| neilt6 | 6:55a26a56046a | 29 | m_CardType = CARD_NONE; |
| neilt6 | 7:61db99e52c0d | 30 | m_Crc = true; |
| neilt6 | 6:55a26a56046a | 31 | m_LargeFrames = false; |
| neilt6 | 13:635147efa748 | 32 | m_WriteValidation = true; |
| neilt6 | 0:2a6d8a096edc | 33 | m_Status = STA_NOINIT; |
| DieterGraef | 23:c03ef1abef0e | 34 | m_Cd.mode(PullUp); |
| DieterGraef | 23:c03ef1abef0e | 35 | m_CdAssert = 0; |
| DieterGraef | 23:c03ef1abef0e | 36 | m_Cd.rise(this, &SDFileSystem::onCardRemoval); |
| DieterGraef | 23:c03ef1abef0e | 37 | h=(void*)&SDFileSystem::DMA2_Stream3_IRQHandler; |
| DieterGraef | 23:c03ef1abef0e | 38 | NVIC_SetVector(DMA2_Stream3_IRQn,(uint32_t)h); |
| DieterGraef | 23:c03ef1abef0e | 39 | h=(void*)&SDFileSystem::DMA2_Stream6_IRQHandler; |
| DieterGraef | 23:c03ef1abef0e | 40 | NVIC_SetVector(DMA2_Stream6_IRQn,(uint32_t)h); |
| DieterGraef | 23:c03ef1abef0e | 41 | h=(void*)&SDFileSystem::SDMMC1_IRQHandler; |
| DieterGraef | 23:c03ef1abef0e | 42 | NVIC_SetVector(SDMMC1_IRQn,(uint32_t)h); |
| DieterGraef | 24:698affe9560c | 43 | BSP_SD_Clear_Busy(); |
| DieterGraef | 23:c03ef1abef0e | 44 | initstat=BSP_SD_Init(); |
| DieterGraef | 23:c03ef1abef0e | 45 | if (initstat!=MSD_OK) |
| DieterGraef | 23:c03ef1abef0e | 46 | { |
| DieterGraef | 23:c03ef1abef0e | 47 | m_Status |= STA_NOINIT; |
| DieterGraef | 23:c03ef1abef0e | 48 | } |
| DieterGraef | 23:c03ef1abef0e | 49 | else |
| DieterGraef | 23:c03ef1abef0e | 50 | { |
| DieterGraef | 23:c03ef1abef0e | 51 | m_Status &= ~STA_NOINIT; |
| DieterGraef | 23:c03ef1abef0e | 52 | } |
| neilt6 | 0:2a6d8a096edc | 53 | } |
| neilt6 | 0:2a6d8a096edc | 54 | |
| neilt6 | 20:2c1e8d442f68 | 55 | bool SDFileSystem::card_present() |
| neilt6 | 0:2a6d8a096edc | 56 | { |
| neilt6 | 20:2c1e8d442f68 | 57 | //Check the card socket |
| neilt6 | 0:2a6d8a096edc | 58 | checkSocket(); |
| neilt6 | 0:2a6d8a096edc | 59 | |
| neilt6 | 20:2c1e8d442f68 | 60 | //Return whether or not a card is present |
| neilt6 | 20:2c1e8d442f68 | 61 | return !(m_Status & STA_NODISK); |
| neilt6 | 20:2c1e8d442f68 | 62 | } |
| neilt6 | 0:2a6d8a096edc | 63 | |
| neilt6 | 20:2c1e8d442f68 | 64 | SDFileSystem::CardType SDFileSystem::card_type() |
| neilt6 | 20:2c1e8d442f68 | 65 | { |
| neilt6 | 20:2c1e8d442f68 | 66 | //Check the card socket |
| neilt6 | 20:2c1e8d442f68 | 67 | checkSocket(); |
| neilt6 | 18:2286a4e7fa31 | 68 | |
| neilt6 | 0:2a6d8a096edc | 69 | //Return the card type |
| neilt6 | 0:2a6d8a096edc | 70 | return m_CardType; |
| neilt6 | 0:2a6d8a096edc | 71 | } |
| neilt6 | 0:2a6d8a096edc | 72 | |
| neilt6 | 7:61db99e52c0d | 73 | bool SDFileSystem::crc() |
| neilt6 | 6:55a26a56046a | 74 | { |
| neilt6 | 6:55a26a56046a | 75 | //Return whether or not CRC is enabled |
| neilt6 | 7:61db99e52c0d | 76 | return m_Crc; |
| neilt6 | 6:55a26a56046a | 77 | } |
| neilt6 | 6:55a26a56046a | 78 | |
| neilt6 | 7:61db99e52c0d | 79 | void SDFileSystem::crc(bool enabled) |
| neilt6 | 6:55a26a56046a | 80 | { |
| neilt6 | 20:2c1e8d442f68 | 81 | //Check the card socket |
| neilt6 | 6:55a26a56046a | 82 | checkSocket(); |
| neilt6 | 6:55a26a56046a | 83 | |
| neilt6 | 6:55a26a56046a | 84 | //Just update the member variable if the card isn't initialized |
| neilt6 | 6:55a26a56046a | 85 | if (m_Status & STA_NOINIT) { |
| neilt6 | 7:61db99e52c0d | 86 | m_Crc = enabled; |
| neilt6 | 6:55a26a56046a | 87 | return; |
| neilt6 | 6:55a26a56046a | 88 | } |
| neilt6 | 6:55a26a56046a | 89 | |
| neilt6 | 6:55a26a56046a | 90 | //Enable or disable CRC |
| neilt6 | 7:61db99e52c0d | 91 | if (enabled && !m_Crc) { |
| neilt6 | 6:55a26a56046a | 92 | //Send CMD59(0x00000001) to enable CRC |
| neilt6 | 7:61db99e52c0d | 93 | m_Crc = true; |
| DieterGraef | 23:c03ef1abef0e | 94 | BSP_SD_CommandTransaction(CMD59, 0x00000001); |
| neilt6 | 7:61db99e52c0d | 95 | } else if (!enabled && m_Crc) { |
| DieterGraef | 23:c03ef1abef0e | 96 | //Send CMD59(0x00000000) to disableAPP/MBED/targets/hal/TARGET_STM/TARGET_STM32F7/TARGET_DISCO_F746NG CRC |
| DieterGraef | 23:c03ef1abef0e | 97 | BSP_SD_CommandTransaction(CMD59, 0x00000000); |
| neilt6 | 7:61db99e52c0d | 98 | m_Crc = false; |
| neilt6 | 6:55a26a56046a | 99 | } |
| neilt6 | 6:55a26a56046a | 100 | } |
| neilt6 | 6:55a26a56046a | 101 | |
| neilt6 | 6:55a26a56046a | 102 | bool SDFileSystem::large_frames() |
| neilt6 | 6:55a26a56046a | 103 | { |
| neilt6 | 6:55a26a56046a | 104 | //Return whether or not 16-bit frames are enabled |
| neilt6 | 6:55a26a56046a | 105 | return m_LargeFrames; |
| neilt6 | 6:55a26a56046a | 106 | } |
| neilt6 | 6:55a26a56046a | 107 | |
| neilt6 | 6:55a26a56046a | 108 | void SDFileSystem::large_frames(bool enabled) |
| neilt6 | 6:55a26a56046a | 109 | { |
| neilt6 | 6:55a26a56046a | 110 | //Set whether or not 16-bit frames are enabled |
| neilt6 | 6:55a26a56046a | 111 | m_LargeFrames = enabled; |
| neilt6 | 6:55a26a56046a | 112 | } |
| neilt6 | 6:55a26a56046a | 113 | |
| neilt6 | 13:635147efa748 | 114 | bool SDFileSystem::write_validation() |
| neilt6 | 13:635147efa748 | 115 | { |
| neilt6 | 13:635147efa748 | 116 | //Return whether or not write validation is enabled |
| neilt6 | 13:635147efa748 | 117 | return m_WriteValidation; |
| neilt6 | 13:635147efa748 | 118 | } |
| neilt6 | 13:635147efa748 | 119 | |
| neilt6 | 13:635147efa748 | 120 | void SDFileSystem::write_validation(bool enabled) |
| neilt6 | 13:635147efa748 | 121 | { |
| neilt6 | 13:635147efa748 | 122 | //Set whether or not write validation is enabled |
| neilt6 | 13:635147efa748 | 123 | m_WriteValidation = enabled; |
| neilt6 | 13:635147efa748 | 124 | } |
| neilt6 | 13:635147efa748 | 125 | |
| neilt6 | 11:67ddc53e3983 | 126 | int SDFileSystem::unmount() |
| neilt6 | 11:67ddc53e3983 | 127 | { |
| neilt6 | 11:67ddc53e3983 | 128 | //Unmount the filesystem |
| neilt6 | 11:67ddc53e3983 | 129 | FATFileSystem::unmount(); |
| neilt6 | 11:67ddc53e3983 | 130 | |
| neilt6 | 20:2c1e8d442f68 | 131 | //Change the status to not initialized, and the card type to unknown |
| neilt6 | 11:67ddc53e3983 | 132 | m_Status |= STA_NOINIT; |
| neilt6 | 20:2c1e8d442f68 | 133 | m_CardType = CARD_UNKNOWN; |
| neilt6 | 11:67ddc53e3983 | 134 | |
| neilt6 | 11:67ddc53e3983 | 135 | //Always succeeds |
| neilt6 | 11:67ddc53e3983 | 136 | return 0; |
| neilt6 | 11:67ddc53e3983 | 137 | } |
| neilt6 | 11:67ddc53e3983 | 138 | |
| neilt6 | 0:2a6d8a096edc | 139 | int SDFileSystem::disk_initialize() |
| neilt6 | 0:2a6d8a096edc | 140 | { |
| neilt6 | 0:2a6d8a096edc | 141 | |
| neilt6 | 0:2a6d8a096edc | 142 | //Make sure there's a card in the socket before proceeding |
| neilt6 | 0:2a6d8a096edc | 143 | checkSocket(); |
| neilt6 | 0:2a6d8a096edc | 144 | if (m_Status & STA_NODISK) |
| neilt6 | 0:2a6d8a096edc | 145 | return m_Status; |
| DieterGraef | 23:c03ef1abef0e | 146 | BSP_SD_GetCardInfo(&m_CardInfo); |
| neilt6 | 12:eebddab6eff2 | 147 | |
| DieterGraef | 23:c03ef1abef0e | 148 | switch(m_CardInfo.CardType) |
| DieterGraef | 23:c03ef1abef0e | 149 | { |
| DieterGraef | 23:c03ef1abef0e | 150 | case STD_CAPACITY_SD_CARD_V1_1: |
| DieterGraef | 23:c03ef1abef0e | 151 | { m_CardType = CARD_SD; |
| DieterGraef | 23:c03ef1abef0e | 152 | break; } |
| DieterGraef | 23:c03ef1abef0e | 153 | case STD_CAPACITY_SD_CARD_V2_0: |
| DieterGraef | 23:c03ef1abef0e | 154 | { m_CardType = CARD_SD; |
| DieterGraef | 23:c03ef1abef0e | 155 | break; } |
| DieterGraef | 23:c03ef1abef0e | 156 | case HIGH_CAPACITY_SD_CARD: |
| DieterGraef | 23:c03ef1abef0e | 157 | { m_CardType = CARD_SDHC; |
| DieterGraef | 23:c03ef1abef0e | 158 | break; } |
| DieterGraef | 23:c03ef1abef0e | 159 | case MULTIMEDIA_CARD: |
| DieterGraef | 23:c03ef1abef0e | 160 | { m_CardType = CARD_MMC; |
| DieterGraef | 23:c03ef1abef0e | 161 | break; } |
| DieterGraef | 23:c03ef1abef0e | 162 | case SECURE_DIGITAL_IO_CARD: |
| DieterGraef | 23:c03ef1abef0e | 163 | { m_CardType = CARD_SD; |
| DieterGraef | 23:c03ef1abef0e | 164 | break; } |
| DieterGraef | 23:c03ef1abef0e | 165 | case HIGH_SPEED_MULTIMEDIA_CARD: |
| DieterGraef | 23:c03ef1abef0e | 166 | { m_CardType = CARD_MMC; |
| DieterGraef | 23:c03ef1abef0e | 167 | break; } |
| DieterGraef | 23:c03ef1abef0e | 168 | case SECURE_DIGITAL_IO_COMBO_CARD: |
| DieterGraef | 23:c03ef1abef0e | 169 | { m_CardType = CARD_SD; |
| DieterGraef | 23:c03ef1abef0e | 170 | break; } |
| DieterGraef | 23:c03ef1abef0e | 171 | case HIGH_CAPACITY_MMC_CARD: |
| DieterGraef | 23:c03ef1abef0e | 172 | { m_CardType = CARD_MMC; |
| DieterGraef | 23:c03ef1abef0e | 173 | break; } |
| DieterGraef | 23:c03ef1abef0e | 174 | default: |
| DieterGraef | 23:c03ef1abef0e | 175 | {m_CardType = CARD_UNKNOWN; |
| DieterGraef | 23:c03ef1abef0e | 176 | return m_Status;} |
| DieterGraef | 23:c03ef1abef0e | 177 | } |
| neilt6 | 0:2a6d8a096edc | 178 | //The card is now initialized |
| neilt6 | 0:2a6d8a096edc | 179 | m_Status &= ~STA_NOINIT; |
| neilt6 | 0:2a6d8a096edc | 180 | |
| neilt6 | 9:1906befe7f30 | 181 | //Return the disk status |
| neilt6 | 0:2a6d8a096edc | 182 | return m_Status; |
| neilt6 | 0:2a6d8a096edc | 183 | } |
| neilt6 | 0:2a6d8a096edc | 184 | |
| neilt6 | 0:2a6d8a096edc | 185 | int SDFileSystem::disk_status() |
| neilt6 | 0:2a6d8a096edc | 186 | { |
| neilt6 | 20:2c1e8d442f68 | 187 | //Check the card socket |
| neilt6 | 0:2a6d8a096edc | 188 | checkSocket(); |
| neilt6 | 0:2a6d8a096edc | 189 | |
| neilt6 | 9:1906befe7f30 | 190 | //Return the disk status |
| neilt6 | 0:2a6d8a096edc | 191 | return m_Status; |
| neilt6 | 0:2a6d8a096edc | 192 | } |
| neilt6 | 0:2a6d8a096edc | 193 | |
| neilt6 | 21:d10a519c0910 | 194 | int SDFileSystem::disk_read(uint8_t* buffer, uint32_t sector, uint32_t count) |
| neilt6 | 0:2a6d8a096edc | 195 | { |
| DieterGraef | 23:c03ef1abef0e | 196 | int retval; |
| neilt6 | 9:1906befe7f30 | 197 | //Make sure the card is initialized before proceeding |
| neilt6 | 0:2a6d8a096edc | 198 | if (m_Status & STA_NOINIT) |
| neilt6 | 0:2a6d8a096edc | 199 | return RES_NOTRDY; |
| DieterGraef | 25:391eade4ef85 | 200 | __DSB(); |
| DieterGraef | 25:391eade4ef85 | 201 | __ISB(); |
| DieterGraef | 24:698affe9560c | 202 | while(BSP_SD_Get_Busy()==1){;} |
| DieterGraef | 24:698affe9560c | 203 | BSP_SD_Set_Busy(); |
| neilt6 | 11:67ddc53e3983 | 204 | //Read a single block, or multiple blocks |
| neilt6 | 11:67ddc53e3983 | 205 | if (count > 1) { |
| DieterGraef | 23:c03ef1abef0e | 206 | BSP_SD_Set_RX_Busy(); |
| DieterGraef | 26:8f15aa3b052b | 207 | SCB_InvalidateDCache_by_Addr((uint32_t *)buffer,(512*count)); |
| DieterGraef | 23:c03ef1abef0e | 208 | retval=BSP_SD_ReadBlocks_DMA((uint32_t *)buffer, (uint64_t) (sector * 512),512, count); |
| DieterGraef | 24:698affe9560c | 209 | while((BSP_SD_Get_RX_Busy()==1)&&(retval==MSD_OK)){;} |
| DieterGraef | 23:c03ef1abef0e | 210 | CPU_CACHE_Flush((uint32_t *)buffer,(512*count)); |
| DieterGraef | 24:698affe9560c | 211 | BSP_SD_Clear_Busy(); |
| DieterGraef | 23:c03ef1abef0e | 212 | return (retval ? RES_ERROR : RES_OK); |
| neilt6 | 11:67ddc53e3983 | 213 | } else { |
| DieterGraef | 23:c03ef1abef0e | 214 | BSP_SD_Set_RX_Busy(); |
| DieterGraef | 26:8f15aa3b052b | 215 | SCB_InvalidateDCache_by_Addr((uint32_t *)buffer,(512)); |
| DieterGraef | 23:c03ef1abef0e | 216 | retval= BSP_SD_ReadBlocks_DMA((uint32_t *)buffer, (uint64_t) (sector * 512), 512, 1); |
| DieterGraef | 24:698affe9560c | 217 | while((BSP_SD_Get_RX_Busy()==1)&&(retval==MSD_OK)){;} |
| DieterGraef | 23:c03ef1abef0e | 218 | CPU_CACHE_Flush((uint32_t *)buffer,(512)); |
| DieterGraef | 24:698affe9560c | 219 | BSP_SD_Clear_Busy(); |
| DieterGraef | 23:c03ef1abef0e | 220 | return (retval ? RES_ERROR : RES_OK); |
| neilt6 | 0:2a6d8a096edc | 221 | } |
| neilt6 | 0:2a6d8a096edc | 222 | } |
| neilt6 | 0:2a6d8a096edc | 223 | |
| neilt6 | 21:d10a519c0910 | 224 | int SDFileSystem::disk_write(const uint8_t* buffer, uint32_t sector, uint32_t count) |
| neilt6 | 0:2a6d8a096edc | 225 | { |
| DieterGraef | 23:c03ef1abef0e | 226 | int retval; |
| neilt6 | 9:1906befe7f30 | 227 | //Make sure the card is initialized before proceeding |
| neilt6 | 0:2a6d8a096edc | 228 | if (m_Status & STA_NOINIT) |
| neilt6 | 0:2a6d8a096edc | 229 | return RES_NOTRDY; |
| DieterGraef | 25:391eade4ef85 | 230 | __DSB(); |
| DieterGraef | 25:391eade4ef85 | 231 | __ISB(); |
| DieterGraef | 24:698affe9560c | 232 | while(BSP_SD_Get_Busy()==1){;} |
| DieterGraef | 24:698affe9560c | 233 | BSP_SD_Set_Busy(); |
| neilt6 | 9:1906befe7f30 | 234 | //Make sure the card isn't write protected before proceeding |
| neilt6 | 0:2a6d8a096edc | 235 | if (m_Status & STA_PROTECT) |
| DieterGraef | 24:698affe9560c | 236 | { |
| DieterGraef | 24:698affe9560c | 237 | BSP_SD_Clear_Busy(); |
| DieterGraef | 24:698affe9560c | 238 | return RES_WRPRT; |
| DieterGraef | 24:698affe9560c | 239 | } |
| neilt6 | 11:67ddc53e3983 | 240 | //Write a single block, or multiple blocks |
| neilt6 | 11:67ddc53e3983 | 241 | if (count > 1) { |
| DieterGraef | 23:c03ef1abef0e | 242 | CPU_CACHE_Flush((uint32_t *)buffer,(512*count)); |
| DieterGraef | 23:c03ef1abef0e | 243 | BSP_SD_Set_TX_Busy(); |
| DieterGraef | 23:c03ef1abef0e | 244 | retval= BSP_SD_WriteBlocks_DMA((uint32_t *)buffer, (uint64_t) (sector * 512), 512, count); |
| DieterGraef | 24:698affe9560c | 245 | while((BSP_SD_Get_TX_Busy()==1)&&(retval==MSD_OK)){;} |
| DieterGraef | 24:698affe9560c | 246 | BSP_SD_Clear_Busy(); |
| DieterGraef | 23:c03ef1abef0e | 247 | return (retval? RES_ERROR : RES_OK); |
| neilt6 | 11:67ddc53e3983 | 248 | } else { |
| DieterGraef | 23:c03ef1abef0e | 249 | CPU_CACHE_Flush((uint32_t *)buffer,(512)); |
| DieterGraef | 23:c03ef1abef0e | 250 | BSP_SD_Set_TX_Busy(); |
| DieterGraef | 23:c03ef1abef0e | 251 | retval= BSP_SD_WriteBlocks_DMA((uint32_t *)buffer, (uint64_t) (sector * 512), 512, 1); |
| DieterGraef | 24:698affe9560c | 252 | while((BSP_SD_Get_TX_Busy()==1)&&(retval==MSD_OK)){;} |
| DieterGraef | 24:698affe9560c | 253 | BSP_SD_Clear_Busy(); |
| DieterGraef | 23:c03ef1abef0e | 254 | return (retval? RES_ERROR : RES_OK); |
| DieterGraef | 23:c03ef1abef0e | 255 | |
| neilt6 | 0:2a6d8a096edc | 256 | } |
| neilt6 | 0:2a6d8a096edc | 257 | } |
| neilt6 | 0:2a6d8a096edc | 258 | |
| neilt6 | 0:2a6d8a096edc | 259 | int SDFileSystem::disk_sync() |
| neilt6 | 0:2a6d8a096edc | 260 | { |
| neilt6 | 0:2a6d8a096edc | 261 | //Select the card so we're forced to wait for the end of any internal write processes |
| DieterGraef | 25:391eade4ef85 | 262 | __DSB(); |
| DieterGraef | 25:391eade4ef85 | 263 | __ISB(); |
| DieterGraef | 24:698affe9560c | 264 | while(BSP_SD_Get_Busy()==1){;} |
| DieterGraef | 24:698affe9560c | 265 | BSP_SD_Set_Busy(); |
| DieterGraef | 23:c03ef1abef0e | 266 | while(BSP_SD_GetStatus()==SD_TRANSFER_BUSY){;} |
| DieterGraef | 23:c03ef1abef0e | 267 | if(BSP_SD_GetStatus()==SD_TRANSFER_OK) |
| DieterGraef | 23:c03ef1abef0e | 268 | { |
| DieterGraef | 24:698affe9560c | 269 | BSP_SD_Clear_Busy(); |
| neilt6 | 10:395539a1481a | 270 | return RES_OK; |
| neilt6 | 10:395539a1481a | 271 | } else { |
| DieterGraef | 24:698affe9560c | 272 | BSP_SD_Clear_Busy(); |
| neilt6 | 10:395539a1481a | 273 | return RES_ERROR; |
| neilt6 | 10:395539a1481a | 274 | } |
| neilt6 | 0:2a6d8a096edc | 275 | } |
| neilt6 | 0:2a6d8a096edc | 276 | |
| neilt6 | 21:d10a519c0910 | 277 | uint32_t SDFileSystem::disk_sectors() |
| neilt6 | 0:2a6d8a096edc | 278 | { |
| DieterGraef | 23:c03ef1abef0e | 279 | uint32_t sectors=0; |
| neilt6 | 9:1906befe7f30 | 280 | //Make sure the card is initialized before proceeding |
| neilt6 | 0:2a6d8a096edc | 281 | if (m_Status & STA_NOINIT) |
| neilt6 | 0:2a6d8a096edc | 282 | return 0; |
| DieterGraef | 25:391eade4ef85 | 283 | __DSB(); |
| DieterGraef | 25:391eade4ef85 | 284 | __ISB(); |
| DieterGraef | 24:698affe9560c | 285 | while(BSP_SD_Get_Busy()==1){;} |
| DieterGraef | 24:698affe9560c | 286 | BSP_SD_Set_Busy(); |
| DieterGraef | 24:698affe9560c | 287 | BSP_SD_GetCardInfo(&m_CardInfo); |
| DieterGraef | 24:698affe9560c | 288 | sectors=m_CardInfo.CardCapacity>>9; |
| DieterGraef | 24:698affe9560c | 289 | BSP_SD_Clear_Busy(); |
| DieterGraef | 23:c03ef1abef0e | 290 | return sectors; |
| neilt6 | 0:2a6d8a096edc | 291 | } |
| neilt6 | 0:2a6d8a096edc | 292 | |
| neilt6 | 13:635147efa748 | 293 | void SDFileSystem::onCardRemoval() |
| neilt6 | 13:635147efa748 | 294 | { |
| neilt6 | 20:2c1e8d442f68 | 295 | //Check the card socket |
| neilt6 | 13:635147efa748 | 296 | checkSocket(); |
| neilt6 | 13:635147efa748 | 297 | } |
| neilt6 | 13:635147efa748 | 298 | |
| neilt6 | 13:635147efa748 | 299 | inline void SDFileSystem::checkSocket() |
| neilt6 | 0:2a6d8a096edc | 300 | { |
| neilt6 | 16:c2c1f0b16380 | 301 | //Use the card detect switch (if available) to determine if the socket is occupied |
| neilt6 | 20:2c1e8d442f68 | 302 | if (m_CdAssert != -1) { |
| neilt6 | 20:2c1e8d442f68 | 303 | if (m_Status & STA_NODISK) { |
| neilt6 | 20:2c1e8d442f68 | 304 | if (m_Cd == m_CdAssert) { |
| neilt6 | 20:2c1e8d442f68 | 305 | //The socket is now occupied |
| neilt6 | 20:2c1e8d442f68 | 306 | m_Status &= ~STA_NODISK; |
| neilt6 | 20:2c1e8d442f68 | 307 | m_CardType = CARD_UNKNOWN; |
| neilt6 | 20:2c1e8d442f68 | 308 | } |
| neilt6 | 20:2c1e8d442f68 | 309 | } else { |
| neilt6 | 20:2c1e8d442f68 | 310 | if (m_Cd != m_CdAssert) { |
| neilt6 | 20:2c1e8d442f68 | 311 | //The socket is now empty |
| neilt6 | 20:2c1e8d442f68 | 312 | m_Status |= (STA_NODISK | STA_NOINIT); |
| neilt6 | 20:2c1e8d442f68 | 313 | m_CardType = CARD_NONE; |
| neilt6 | 20:2c1e8d442f68 | 314 | } |
| neilt6 | 20:2c1e8d442f68 | 315 | } |
| neilt6 | 0:2a6d8a096edc | 316 | } |
| neilt6 | 0:2a6d8a096edc | 317 | } |
| neilt6 | 0:2a6d8a096edc | 318 | |
| neilt6 | 0:2a6d8a096edc | 319 | |
| DieterGraef | 23:c03ef1abef0e | 320 | /*interrupthandlers */ |
| DieterGraef | 23:c03ef1abef0e | 321 | /** |
| DieterGraef | 23:c03ef1abef0e | 322 | * @brief This function handles DMA2 Stream 3 interrupt request. |
| DieterGraef | 23:c03ef1abef0e | 323 | * @param None |
| DieterGraef | 23:c03ef1abef0e | 324 | * @retval None |
| DieterGraef | 23:c03ef1abef0e | 325 | */ |
| DieterGraef | 23:c03ef1abef0e | 326 | void SDFileSystem::DMA2_Stream3_IRQHandler(void) |
| neilt6 | 11:67ddc53e3983 | 327 | { |
| DieterGraef | 23:c03ef1abef0e | 328 | BSP_SD_DMA_Rx_IRQHandler(); |
| DieterGraef | 23:c03ef1abef0e | 329 | BSP_SD_Clear_RX_Busy(); |
| neilt6 | 11:67ddc53e3983 | 330 | } |
| neilt6 | 11:67ddc53e3983 | 331 | |
| DieterGraef | 23:c03ef1abef0e | 332 | /** |
| DieterGraef | 23:c03ef1abef0e | 333 | * @brief This function handles DMA2 Stream 6 interrupt request. |
| DieterGraef | 23:c03ef1abef0e | 334 | * @param None |
| DieterGraef | 23:c03ef1abef0e | 335 | * @retval None |
| DieterGraef | 23:c03ef1abef0e | 336 | */ |
| DieterGraef | 23:c03ef1abef0e | 337 | void SDFileSystem::DMA2_Stream6_IRQHandler(void) |
| neilt6 | 11:67ddc53e3983 | 338 | { |
| DieterGraef | 23:c03ef1abef0e | 339 | BSP_SD_DMA_Tx_IRQHandler(); |
| DieterGraef | 23:c03ef1abef0e | 340 | BSP_SD_Clear_TX_Busy(); |
| neilt6 | 11:67ddc53e3983 | 341 | } |
| neilt6 | 9:1906befe7f30 | 342 | |
| DieterGraef | 23:c03ef1abef0e | 343 | /** |
| DieterGraef | 23:c03ef1abef0e | 344 | * @brief This function handles SDIO interrupt request. |
| DieterGraef | 23:c03ef1abef0e | 345 | * @param None |
| DieterGraef | 23:c03ef1abef0e | 346 | * @retval None |
| DieterGraef | 23:c03ef1abef0e | 347 | */ |
| DieterGraef | 23:c03ef1abef0e | 348 | void SDFileSystem::SDMMC1_IRQHandler(void) |
| neilt6 | 11:67ddc53e3983 | 349 | { |
| DieterGraef | 23:c03ef1abef0e | 350 | BSP_SD_IRQHandler(); |
| neilt6 | 9:1906befe7f30 | 351 | } |
