Fixed version not to issue warning messages. Original: Revision 26:8f15aa3b052b by Dieter Graef. https://developer.mbed.org/users/DieterGraef/code/SDFileSystem/

Dependencies:   FATFileSystem

Dependents:   F746_AudioPlayerSD F746_SD_WavPlayer F746_SD_GraphicEqualizer_ren0620 F746_SD_TextFile_RW ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SDFileSystem.cpp Source File

SDFileSystem.cpp

00001 //-----------------------------------------------------------------------
00002 //  Fixed version not to issue warning message by N.Mikami
00003 //      January 09, 2017
00004 //-----------------------------------------------------------------------
00005 
00006 /* SD/MMC File System Library
00007  * Copyright (c) 2016 Neil Thiessen
00008  * Modified for the use with STM32F746 Discovery (C) 2016 Dieter Greaf
00009  * Licensed under the Apache License, Version 2.0 (the "License");
00010  * you may not use this file except in compliance with the License.
00011  * You may obtain a copy of the License at
00012  *
00013  *     http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  * Unless required by applicable law or agreed to in writing, software
00016  * distributed under the License is distributed on an "AS IS" BASIS,
00017  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  * See the License for the specific language governing permissions and
00019  * limitations under the License.
00020  */
00021 
00022 #include "SDFileSystem.h"
00023 #include "diskio.h"
00024 #include "SDCRC.h"
00025 //for cache flush function
00026 #include "SD_Helper.h"
00027 
00028 SDFileSystem::SDFileSystem( const char* name)
00029     : FATFileSystem(name), m_Cd(PC_13)
00030 {
00031     //Initialize the member variables
00032     uint8_t initstat;
00033 
00034 // Commented out in modified versin
00035 //    void* h;
00036 
00037     m_CardType = CARD_NONE;
00038     m_Crc = true;
00039     m_LargeFrames = false;
00040     m_WriteValidation = true;
00041     m_Status = STA_NOINIT;
00042     m_Cd.mode(PullUp);
00043     m_CdAssert = 0;
00044     m_Cd.rise(this, &SDFileSystem::onCardRemoval);
00045 
00046 // Followings are original
00047 //    h=(void*)&SDFileSystem::DMA2_Stream3_IRQHandler;
00048 //    NVIC_SetVector(DMA2_Stream3_IRQn,(uint32_t)h);
00049 //    h=(void*)&SDFileSystem::DMA2_Stream6_IRQHandler;
00050 //    NVIC_SetVector(DMA2_Stream6_IRQn,(uint32_t)h);
00051 //    h=(void*)&SDFileSystem::SDMMC1_IRQHandler;
00052 //    NVIC_SetVector(SDMMC1_IRQn,(uint32_t)h);
00053 
00054 // Modified
00055     NVIC_SetVector(DMA2_Stream3_IRQn,
00056                    (uint32_t)(&SDFileSystem::DMA2_Stream3_IRQHandler));
00057     NVIC_SetVector(DMA2_Stream6_IRQn,
00058                    (uint32_t)(&SDFileSystem::DMA2_Stream6_IRQHandler));
00059     NVIC_SetVector(SDMMC1_IRQn,
00060                    (uint32_t)(&SDFileSystem::SDMMC1_IRQHandler));
00061 
00062     BSP_SD_Clear_Busy();
00063     initstat=BSP_SD_Init();
00064    if (initstat!=MSD_OK)
00065    {
00066        m_Status |= STA_NOINIT;
00067    }
00068    else
00069    {
00070         m_Status &= ~STA_NOINIT;
00071    }
00072 }
00073 
00074 bool SDFileSystem::card_present()
00075 {
00076     //Check the card socket
00077     checkSocket();
00078 
00079     //Return whether or not a card is present
00080     return !(m_Status & STA_NODISK);
00081 }
00082 
00083 SDFileSystem::CardType SDFileSystem::card_type()
00084 {
00085     //Check the card socket
00086     checkSocket();
00087 
00088     //Return the card type
00089     return m_CardType;
00090 }
00091 
00092 bool SDFileSystem::crc()
00093 {
00094     //Return whether or not CRC is enabled
00095     return m_Crc;
00096 }
00097 
00098 void SDFileSystem::crc(bool enabled)
00099 {
00100     //Check the card socket
00101     checkSocket();
00102 
00103     //Just update the member variable if the card isn't initialized
00104     if (m_Status & STA_NOINIT) {
00105         m_Crc = enabled;
00106         return;
00107     }
00108 
00109     //Enable or disable CRC
00110     if (enabled && !m_Crc) {
00111         //Send CMD59(0x00000001) to enable CRC
00112         m_Crc = true;
00113         BSP_SD_CommandTransaction(CMD59, 0x00000001);
00114     } else if (!enabled && m_Crc) {
00115         //Send CMD59(0x00000000) to disableAPP/MBED/targets/hal/TARGET_STM/TARGET_STM32F7/TARGET_DISCO_F746NG CRC
00116         BSP_SD_CommandTransaction(CMD59, 0x00000000);
00117         m_Crc = false;
00118     }
00119 }
00120 
00121 bool SDFileSystem::large_frames()
00122 {
00123     //Return whether or not 16-bit frames are enabled
00124     return m_LargeFrames;
00125 }
00126 
00127 void SDFileSystem::large_frames(bool enabled)
00128 {
00129     //Set whether or not 16-bit frames are enabled
00130     m_LargeFrames = enabled;
00131 }
00132 
00133 bool SDFileSystem::write_validation()
00134 {
00135     //Return whether or not write validation is enabled
00136     return m_WriteValidation;
00137 }
00138 
00139 void SDFileSystem::write_validation(bool enabled)
00140 {
00141     //Set whether or not write validation is enabled
00142     m_WriteValidation = enabled;
00143 }
00144 
00145 int SDFileSystem::unmount()
00146 {
00147     //Unmount the filesystem
00148     FATFileSystem::unmount();
00149 
00150     //Change the status to not initialized, and the card type to unknown
00151     m_Status |= STA_NOINIT;
00152     m_CardType = CARD_UNKNOWN;
00153 
00154     //Always succeeds
00155     return 0;
00156 }
00157 
00158 int SDFileSystem::disk_initialize()
00159 {
00160 
00161     //Make sure there's a card in the socket before proceeding
00162     checkSocket();
00163     if (m_Status & STA_NODISK)
00164         return m_Status;
00165    BSP_SD_GetCardInfo(&m_CardInfo);
00166 
00167    switch(m_CardInfo.CardType)
00168    {
00169    case STD_CAPACITY_SD_CARD_V1_1:
00170        { m_CardType =  CARD_SD;
00171          break; }
00172     case STD_CAPACITY_SD_CARD_V2_0:
00173        { m_CardType =  CARD_SD;
00174          break; }
00175     case HIGH_CAPACITY_SD_CARD:
00176        { m_CardType =  CARD_SDHC;
00177          break; }
00178     case MULTIMEDIA_CARD:
00179        { m_CardType =  CARD_MMC;
00180          break; }
00181     case SECURE_DIGITAL_IO_CARD:
00182        { m_CardType =  CARD_SD;
00183          break; }
00184     case HIGH_SPEED_MULTIMEDIA_CARD:
00185        { m_CardType =  CARD_MMC;
00186          break; }
00187     case SECURE_DIGITAL_IO_COMBO_CARD:
00188        { m_CardType =  CARD_SD;
00189          break; }
00190     case HIGH_CAPACITY_MMC_CARD:
00191        { m_CardType =  CARD_MMC;
00192          break; }
00193     default:
00194        {m_CardType = CARD_UNKNOWN;
00195        return m_Status;}
00196    }
00197     //The card is now initialized
00198     m_Status &= ~STA_NOINIT;
00199 
00200     //Return the disk status
00201     return m_Status;
00202 }
00203 
00204 int SDFileSystem::disk_status()
00205 {
00206     //Check the card socket
00207     checkSocket();
00208 
00209     //Return the disk status
00210     return m_Status;
00211 }
00212 
00213 int SDFileSystem::disk_read(uint8_t* buffer, uint32_t sector, uint32_t count)
00214 {
00215     int retval;
00216     //Make sure the card is initialized before proceeding
00217     if (m_Status & STA_NOINIT)
00218         return RES_NOTRDY;
00219     __DSB();
00220     __ISB();
00221     while(BSP_SD_Get_Busy()==1){;}
00222     BSP_SD_Set_Busy();
00223     //Read a single block, or multiple blocks
00224     if (count > 1) {
00225         BSP_SD_Set_RX_Busy();
00226         SCB_InvalidateDCache_by_Addr((uint32_t *)buffer,(512*count));
00227         retval=BSP_SD_ReadBlocks_DMA((uint32_t *)buffer, (uint64_t) (sector * 512),512, count);
00228         while((BSP_SD_Get_RX_Busy()==1)&&(retval==MSD_OK)){;}
00229         CPU_CACHE_Flush((uint32_t *)buffer,(512*count));
00230         BSP_SD_Clear_Busy();
00231         return (retval ? RES_ERROR : RES_OK);
00232     } else {
00233         BSP_SD_Set_RX_Busy();
00234         SCB_InvalidateDCache_by_Addr((uint32_t *)buffer,(512));
00235         retval= BSP_SD_ReadBlocks_DMA((uint32_t *)buffer, (uint64_t) (sector * 512), 512, 1);
00236         while((BSP_SD_Get_RX_Busy()==1)&&(retval==MSD_OK)){;}
00237         CPU_CACHE_Flush((uint32_t *)buffer,(512));
00238         BSP_SD_Clear_Busy();
00239         return (retval ? RES_ERROR : RES_OK);
00240     }
00241 }
00242 
00243 int SDFileSystem::disk_write(const uint8_t* buffer, uint32_t sector, uint32_t count)
00244 {
00245     int retval;
00246     //Make sure the card is initialized before proceeding
00247     if (m_Status & STA_NOINIT)
00248         return RES_NOTRDY;
00249     __DSB();
00250     __ISB();
00251     while(BSP_SD_Get_Busy()==1){;}
00252     BSP_SD_Set_Busy();
00253     //Make sure the card isn't write protected before proceeding
00254     if (m_Status & STA_PROTECT)
00255     {
00256     BSP_SD_Clear_Busy();
00257     return RES_WRPRT;
00258     }
00259     //Write a single block, or multiple blocks
00260     if (count > 1) {
00261         CPU_CACHE_Flush((uint32_t *)buffer,(512*count));
00262         BSP_SD_Set_TX_Busy();
00263         retval= BSP_SD_WriteBlocks_DMA((uint32_t *)buffer, (uint64_t) (sector * 512), 512, count);
00264          while((BSP_SD_Get_TX_Busy()==1)&&(retval==MSD_OK)){;}
00265          BSP_SD_Clear_Busy();
00266          return (retval? RES_ERROR : RES_OK);
00267     } else {
00268         CPU_CACHE_Flush((uint32_t *)buffer,(512));
00269         BSP_SD_Set_TX_Busy();
00270         retval= BSP_SD_WriteBlocks_DMA((uint32_t *)buffer, (uint64_t) (sector * 512), 512, 1);
00271          while((BSP_SD_Get_TX_Busy()==1)&&(retval==MSD_OK)){;}
00272          BSP_SD_Clear_Busy();
00273          return (retval? RES_ERROR : RES_OK);
00274 
00275     }
00276 }
00277 
00278 int SDFileSystem::disk_sync()
00279 {
00280     //Select the card so we're forced to wait for the end of any internal write processes
00281     __DSB();
00282     __ISB();
00283     while(BSP_SD_Get_Busy()==1){;}
00284     BSP_SD_Set_Busy();
00285     while(BSP_SD_GetStatus()==SD_TRANSFER_BUSY){;}
00286     if(BSP_SD_GetStatus()==SD_TRANSFER_OK)
00287     {
00288         BSP_SD_Clear_Busy();
00289         return RES_OK;
00290     } else {
00291         BSP_SD_Clear_Busy();
00292         return RES_ERROR;
00293     }
00294 }
00295 
00296 uint32_t SDFileSystem::disk_sectors()
00297 {
00298     uint32_t sectors=0;
00299     //Make sure the card is initialized before proceeding
00300     if (m_Status & STA_NOINIT)
00301         return 0;
00302     __DSB();
00303     __ISB();
00304     while(BSP_SD_Get_Busy()==1){;}
00305     BSP_SD_Set_Busy();
00306     BSP_SD_GetCardInfo(&m_CardInfo);
00307     sectors=m_CardInfo.CardCapacity>>9;
00308     BSP_SD_Clear_Busy();
00309     return sectors;
00310 }
00311 
00312 void SDFileSystem::onCardRemoval()
00313 {
00314     //Check the card socket
00315     checkSocket();
00316 }
00317 
00318 inline void SDFileSystem::checkSocket()
00319 {
00320     //Use the card detect switch (if available) to determine if the socket is occupied
00321     if (m_CdAssert != -1) {
00322         if (m_Status & STA_NODISK) {
00323             if (m_Cd == m_CdAssert) {
00324                 //The socket is now occupied
00325                 m_Status &= ~STA_NODISK;
00326                 m_CardType = CARD_UNKNOWN;
00327             }
00328         } else {
00329             if (m_Cd != m_CdAssert) {
00330                 //The socket is now empty
00331                 m_Status |= (STA_NODISK | STA_NOINIT);
00332                 m_CardType = CARD_NONE;
00333             }
00334         }
00335     }
00336 }
00337 
00338 
00339 /*interrupthandlers */
00340 /**
00341   * @brief  This function handles DMA2 Stream 3 interrupt request.
00342   * @param  None
00343   * @retval None
00344   */
00345 void SDFileSystem::DMA2_Stream3_IRQHandler(void)
00346 {
00347   BSP_SD_DMA_Rx_IRQHandler();
00348   BSP_SD_Clear_RX_Busy();
00349 }
00350 
00351 /**
00352   * @brief  This function handles DMA2 Stream 6 interrupt request.
00353   * @param  None
00354   * @retval None
00355   */
00356 void SDFileSystem::DMA2_Stream6_IRQHandler(void)
00357 {
00358   BSP_SD_DMA_Tx_IRQHandler();
00359   BSP_SD_Clear_TX_Busy();
00360 }
00361 
00362 /**
00363   * @brief  This function handles SDIO interrupt request.
00364   * @param  None
00365   * @retval None
00366   */
00367 void SDFileSystem::SDMMC1_IRQHandler(void)
00368 {
00369   BSP_SD_IRQHandler();
00370 }
00371