SDFileSystem for STM32F746NG DISCOVERY with 4bit SDMMC interface on fixed pins
Dependents: DISCO-F746NG_SDFileSystem uzairkhan DISCO-F746NG_Scope_copy
Fork of SDFileSystem by
SDFileSystem.cpp@24:698affe9560c, 2016-04-12 (annotated)
- Committer:
- DieterGraef
- Date:
- Tue Apr 12 13:47:47 2016 +0000
- Revision:
- 24:698affe9560c
- Parent:
- 23:c03ef1abef0e
- Child:
- 25:391eade4ef85
added a lock mechanism to make it usable in RTOS environment.
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 | 24:698affe9560c | 200 | while(BSP_SD_Get_Busy()==1){;} |
DieterGraef | 24:698affe9560c | 201 | BSP_SD_Set_Busy(); |
neilt6 | 11:67ddc53e3983 | 202 | //Read a single block, or multiple blocks |
neilt6 | 11:67ddc53e3983 | 203 | if (count > 1) { |
DieterGraef | 23:c03ef1abef0e | 204 | BSP_SD_Set_RX_Busy(); |
DieterGraef | 23:c03ef1abef0e | 205 | retval=BSP_SD_ReadBlocks_DMA((uint32_t *)buffer, (uint64_t) (sector * 512),512, count); |
DieterGraef | 24:698affe9560c | 206 | while((BSP_SD_Get_RX_Busy()==1)&&(retval==MSD_OK)){;} |
DieterGraef | 23:c03ef1abef0e | 207 | CPU_CACHE_Flush((uint32_t *)buffer,(512*count)); |
DieterGraef | 24:698affe9560c | 208 | BSP_SD_Clear_Busy(); |
DieterGraef | 23:c03ef1abef0e | 209 | return (retval ? RES_ERROR : RES_OK); |
neilt6 | 11:67ddc53e3983 | 210 | } else { |
DieterGraef | 23:c03ef1abef0e | 211 | BSP_SD_Set_RX_Busy(); |
DieterGraef | 23:c03ef1abef0e | 212 | retval= BSP_SD_ReadBlocks_DMA((uint32_t *)buffer, (uint64_t) (sector * 512), 512, 1); |
DieterGraef | 24:698affe9560c | 213 | while((BSP_SD_Get_RX_Busy()==1)&&(retval==MSD_OK)){;} |
DieterGraef | 23:c03ef1abef0e | 214 | CPU_CACHE_Flush((uint32_t *)buffer,(512)); |
DieterGraef | 24:698affe9560c | 215 | BSP_SD_Clear_Busy(); |
DieterGraef | 23:c03ef1abef0e | 216 | return (retval ? RES_ERROR : RES_OK); |
neilt6 | 0:2a6d8a096edc | 217 | } |
neilt6 | 0:2a6d8a096edc | 218 | } |
neilt6 | 0:2a6d8a096edc | 219 | |
neilt6 | 21:d10a519c0910 | 220 | int SDFileSystem::disk_write(const uint8_t* buffer, uint32_t sector, uint32_t count) |
neilt6 | 0:2a6d8a096edc | 221 | { |
DieterGraef | 23:c03ef1abef0e | 222 | int retval; |
neilt6 | 9:1906befe7f30 | 223 | //Make sure the card is initialized before proceeding |
neilt6 | 0:2a6d8a096edc | 224 | if (m_Status & STA_NOINIT) |
neilt6 | 0:2a6d8a096edc | 225 | return RES_NOTRDY; |
DieterGraef | 24:698affe9560c | 226 | while(BSP_SD_Get_Busy()==1){;} |
DieterGraef | 24:698affe9560c | 227 | BSP_SD_Set_Busy(); |
neilt6 | 9:1906befe7f30 | 228 | //Make sure the card isn't write protected before proceeding |
neilt6 | 0:2a6d8a096edc | 229 | if (m_Status & STA_PROTECT) |
DieterGraef | 24:698affe9560c | 230 | { |
DieterGraef | 24:698affe9560c | 231 | BSP_SD_Clear_Busy(); |
DieterGraef | 24:698affe9560c | 232 | return RES_WRPRT; |
DieterGraef | 24:698affe9560c | 233 | } |
neilt6 | 11:67ddc53e3983 | 234 | //Write a single block, or multiple blocks |
neilt6 | 11:67ddc53e3983 | 235 | if (count > 1) { |
DieterGraef | 23:c03ef1abef0e | 236 | CPU_CACHE_Flush((uint32_t *)buffer,(512*count)); |
DieterGraef | 23:c03ef1abef0e | 237 | BSP_SD_Set_TX_Busy(); |
DieterGraef | 23:c03ef1abef0e | 238 | retval= BSP_SD_WriteBlocks_DMA((uint32_t *)buffer, (uint64_t) (sector * 512), 512, count); |
DieterGraef | 24:698affe9560c | 239 | while((BSP_SD_Get_TX_Busy()==1)&&(retval==MSD_OK)){;} |
DieterGraef | 24:698affe9560c | 240 | BSP_SD_Clear_Busy(); |
DieterGraef | 23:c03ef1abef0e | 241 | return (retval? RES_ERROR : RES_OK); |
neilt6 | 11:67ddc53e3983 | 242 | } else { |
DieterGraef | 23:c03ef1abef0e | 243 | CPU_CACHE_Flush((uint32_t *)buffer,(512)); |
DieterGraef | 23:c03ef1abef0e | 244 | BSP_SD_Set_TX_Busy(); |
DieterGraef | 23:c03ef1abef0e | 245 | retval= BSP_SD_WriteBlocks_DMA((uint32_t *)buffer, (uint64_t) (sector * 512), 512, 1); |
DieterGraef | 24:698affe9560c | 246 | while((BSP_SD_Get_TX_Busy()==1)&&(retval==MSD_OK)){;} |
DieterGraef | 24:698affe9560c | 247 | BSP_SD_Clear_Busy(); |
DieterGraef | 23:c03ef1abef0e | 248 | return (retval? RES_ERROR : RES_OK); |
DieterGraef | 23:c03ef1abef0e | 249 | |
neilt6 | 0:2a6d8a096edc | 250 | } |
neilt6 | 0:2a6d8a096edc | 251 | } |
neilt6 | 0:2a6d8a096edc | 252 | |
neilt6 | 0:2a6d8a096edc | 253 | int SDFileSystem::disk_sync() |
neilt6 | 0:2a6d8a096edc | 254 | { |
neilt6 | 0:2a6d8a096edc | 255 | //Select the card so we're forced to wait for the end of any internal write processes |
DieterGraef | 24:698affe9560c | 256 | while(BSP_SD_Get_Busy()==1){;} |
DieterGraef | 24:698affe9560c | 257 | BSP_SD_Set_Busy(); |
DieterGraef | 23:c03ef1abef0e | 258 | while(BSP_SD_GetStatus()==SD_TRANSFER_BUSY){;} |
DieterGraef | 23:c03ef1abef0e | 259 | if(BSP_SD_GetStatus()==SD_TRANSFER_OK) |
DieterGraef | 23:c03ef1abef0e | 260 | { |
DieterGraef | 24:698affe9560c | 261 | BSP_SD_Clear_Busy(); |
neilt6 | 10:395539a1481a | 262 | return RES_OK; |
neilt6 | 10:395539a1481a | 263 | } else { |
DieterGraef | 24:698affe9560c | 264 | BSP_SD_Clear_Busy(); |
neilt6 | 10:395539a1481a | 265 | return RES_ERROR; |
neilt6 | 10:395539a1481a | 266 | } |
neilt6 | 0:2a6d8a096edc | 267 | } |
neilt6 | 0:2a6d8a096edc | 268 | |
neilt6 | 21:d10a519c0910 | 269 | uint32_t SDFileSystem::disk_sectors() |
neilt6 | 0:2a6d8a096edc | 270 | { |
DieterGraef | 23:c03ef1abef0e | 271 | uint32_t sectors=0; |
neilt6 | 9:1906befe7f30 | 272 | //Make sure the card is initialized before proceeding |
neilt6 | 0:2a6d8a096edc | 273 | if (m_Status & STA_NOINIT) |
neilt6 | 0:2a6d8a096edc | 274 | return 0; |
DieterGraef | 24:698affe9560c | 275 | while(BSP_SD_Get_Busy()==1){;} |
DieterGraef | 24:698affe9560c | 276 | BSP_SD_Set_Busy(); |
DieterGraef | 24:698affe9560c | 277 | BSP_SD_GetCardInfo(&m_CardInfo); |
DieterGraef | 24:698affe9560c | 278 | sectors=m_CardInfo.CardCapacity>>9; |
DieterGraef | 24:698affe9560c | 279 | BSP_SD_Clear_Busy(); |
DieterGraef | 23:c03ef1abef0e | 280 | return sectors; |
neilt6 | 0:2a6d8a096edc | 281 | } |
neilt6 | 0:2a6d8a096edc | 282 | |
neilt6 | 13:635147efa748 | 283 | void SDFileSystem::onCardRemoval() |
neilt6 | 13:635147efa748 | 284 | { |
neilt6 | 20:2c1e8d442f68 | 285 | //Check the card socket |
neilt6 | 13:635147efa748 | 286 | checkSocket(); |
neilt6 | 13:635147efa748 | 287 | } |
neilt6 | 13:635147efa748 | 288 | |
neilt6 | 13:635147efa748 | 289 | inline void SDFileSystem::checkSocket() |
neilt6 | 0:2a6d8a096edc | 290 | { |
neilt6 | 16:c2c1f0b16380 | 291 | //Use the card detect switch (if available) to determine if the socket is occupied |
neilt6 | 20:2c1e8d442f68 | 292 | if (m_CdAssert != -1) { |
neilt6 | 20:2c1e8d442f68 | 293 | if (m_Status & STA_NODISK) { |
neilt6 | 20:2c1e8d442f68 | 294 | if (m_Cd == m_CdAssert) { |
neilt6 | 20:2c1e8d442f68 | 295 | //The socket is now occupied |
neilt6 | 20:2c1e8d442f68 | 296 | m_Status &= ~STA_NODISK; |
neilt6 | 20:2c1e8d442f68 | 297 | m_CardType = CARD_UNKNOWN; |
neilt6 | 20:2c1e8d442f68 | 298 | } |
neilt6 | 20:2c1e8d442f68 | 299 | } else { |
neilt6 | 20:2c1e8d442f68 | 300 | if (m_Cd != m_CdAssert) { |
neilt6 | 20:2c1e8d442f68 | 301 | //The socket is now empty |
neilt6 | 20:2c1e8d442f68 | 302 | m_Status |= (STA_NODISK | STA_NOINIT); |
neilt6 | 20:2c1e8d442f68 | 303 | m_CardType = CARD_NONE; |
neilt6 | 20:2c1e8d442f68 | 304 | } |
neilt6 | 20:2c1e8d442f68 | 305 | } |
neilt6 | 0:2a6d8a096edc | 306 | } |
neilt6 | 0:2a6d8a096edc | 307 | } |
neilt6 | 0:2a6d8a096edc | 308 | |
neilt6 | 0:2a6d8a096edc | 309 | |
DieterGraef | 23:c03ef1abef0e | 310 | /*interrupthandlers */ |
DieterGraef | 23:c03ef1abef0e | 311 | /** |
DieterGraef | 23:c03ef1abef0e | 312 | * @brief This function handles DMA2 Stream 3 interrupt request. |
DieterGraef | 23:c03ef1abef0e | 313 | * @param None |
DieterGraef | 23:c03ef1abef0e | 314 | * @retval None |
DieterGraef | 23:c03ef1abef0e | 315 | */ |
DieterGraef | 23:c03ef1abef0e | 316 | void SDFileSystem::DMA2_Stream3_IRQHandler(void) |
neilt6 | 11:67ddc53e3983 | 317 | { |
DieterGraef | 23:c03ef1abef0e | 318 | BSP_SD_DMA_Rx_IRQHandler(); |
DieterGraef | 23:c03ef1abef0e | 319 | BSP_SD_Clear_RX_Busy(); |
neilt6 | 11:67ddc53e3983 | 320 | } |
neilt6 | 11:67ddc53e3983 | 321 | |
DieterGraef | 23:c03ef1abef0e | 322 | /** |
DieterGraef | 23:c03ef1abef0e | 323 | * @brief This function handles DMA2 Stream 6 interrupt request. |
DieterGraef | 23:c03ef1abef0e | 324 | * @param None |
DieterGraef | 23:c03ef1abef0e | 325 | * @retval None |
DieterGraef | 23:c03ef1abef0e | 326 | */ |
DieterGraef | 23:c03ef1abef0e | 327 | void SDFileSystem::DMA2_Stream6_IRQHandler(void) |
neilt6 | 11:67ddc53e3983 | 328 | { |
DieterGraef | 23:c03ef1abef0e | 329 | BSP_SD_DMA_Tx_IRQHandler(); |
DieterGraef | 23:c03ef1abef0e | 330 | BSP_SD_Clear_TX_Busy(); |
neilt6 | 11:67ddc53e3983 | 331 | } |
neilt6 | 9:1906befe7f30 | 332 | |
DieterGraef | 23:c03ef1abef0e | 333 | /** |
DieterGraef | 23:c03ef1abef0e | 334 | * @brief This function handles SDIO interrupt request. |
DieterGraef | 23:c03ef1abef0e | 335 | * @param None |
DieterGraef | 23:c03ef1abef0e | 336 | * @retval None |
DieterGraef | 23:c03ef1abef0e | 337 | */ |
DieterGraef | 23:c03ef1abef0e | 338 | void SDFileSystem::SDMMC1_IRQHandler(void) |
neilt6 | 11:67ddc53e3983 | 339 | { |
DieterGraef | 23:c03ef1abef0e | 340 | BSP_SD_IRQHandler(); |
neilt6 | 9:1906befe7f30 | 341 | } |