00001 /*This file is prepared for Doxygen automatic documentation generation.*/ 00013 00014 /* Copyright (c) 2009 Atmel Corporation. All rights reserved. 00015 * 00016 * Redistribution and use in source and binary forms, with or without 00017 * modification, are permitted provided that the following conditions are met: 00018 * 00019 * 1. Redistributions of source code must retain the above copyright notice, 00020 * this list of conditions and the following disclaimer. 00021 * 00022 * 2. Redistributions in binary form must reproduce the above copyright notice, 00023 * this list of conditions and the following disclaimer in the documentation 00024 * and/or other materials provided with the distribution. 00025 * 00026 * 3. The name of Atmel may not be used to endorse or promote products derived 00027 * from this software without specific prior written permission. 00028 * 00029 * 4. This software may only be redistributed and used in connection with an Atmel 00030 * AVR product. 00031 * 00032 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED 00033 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00034 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND 00035 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, 00036 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00037 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00038 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00039 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00040 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00041 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00042 */ 00043 00044 //_____ I N C L U D E S ___________________________________________________ 00045 00046 #include "config.h" // system configuration 00047 #include "conf_sdmmc.h" 00048 #include "mmc_sd_mem.h" 00049 #include "mmc_sd.h" 00050 00051 //_____ M A C R O S ________________________________________________________ 00052 00053 00054 //_____ P R I V A T E D E C L A R A T I O N _____________________________ 00055 00056 00057 //_____ D E F I N I T I O N ________________________________________________ 00058 00059 00060 //extern xdata U32 mmc_sd_mem_size; 00061 extern xdata U32 MMC_SD_DISK_SIZE; 00062 extern volatile U32 mmc_sd_last_address; 00063 extern bit mmc_sd_init_done; 00064 00065 U8 mmc_sd_presence_status = 0; 00066 extern bit mmc_sd_init_done; 00067 00068 00069 00070 //_____ D E C L A R A T I O N ______________________________________________ 00071 00082 void mmc_sd_mem_init(void) 00083 { 00084 mmc_sd_init(); // Init the SPI bus and the MMC/SD card 00085 } 00086 00087 00103 Ctrl_status mmc_sd_test_unit_ready(void) 00104 { 00105 Sdmmc_access_signal_on(); 00106 if (mmc_sd_init_done == FALSE) 00107 { 00108 mmc_sd_mem_init(); 00109 } 00110 switch (mmc_sd_presence_status) 00111 { 00112 case MMCSD_REMOVED: 00113 mmc_sd_init_done = FALSE; 00114 if (OK == mmc_sd_mem_check()) 00115 { 00116 mmc_sd_presence_status = MMCSD_INSERTED; 00117 Sdmmc_access_signal_off(); 00118 return CTRL_BUSY; 00119 } 00120 Sdmmc_access_signal_off(); 00121 return CTRL_NO_PRESENT; 00122 00123 case MMCSD_INSERTED: 00124 if (OK != mmc_sd_mem_check()) 00125 { 00126 mmc_sd_presence_status = MMCSD_REMOVING; 00127 mmc_sd_init_done = FALSE; 00128 Sdmmc_access_signal_off(); 00129 return CTRL_BUSY; 00130 } 00131 Sdmmc_access_signal_off(); 00132 return CTRL_GOOD; 00133 00134 case MMCSD_REMOVING: 00135 mmc_sd_presence_status = MMCSD_REMOVED; 00136 Sdmmc_access_signal_off(); 00137 return CTRL_NO_PRESENT; 00138 00139 default: 00140 mmc_sd_presence_status = MMCSD_REMOVED; 00141 Sdmmc_access_signal_off(); 00142 return CTRL_BUSY; 00143 } 00144 00145 Sdmmc_access_signal_off(); 00146 return CTRL_BUSY; 00147 /* 00148 if (OK==mmc_sd_mem_check()) 00149 { 00150 if (mmc_sd_status_changed == FALSE) 00151 { 00152 mmc_sd_status_changed = TRUE; 00153 return CTRL_BUSY; // BUSY token must be returned to indicate a status change ! 00154 } 00155 else 00156 return CTRL_GOOD; // the 2nd time the host will ask for unit_ready, we can answer GOOD if we have returned BUSY first ! 00157 } 00158 else 00159 { 00160 if (mmc_sd_status_changed == TRUE) 00161 { 00162 mmc_sd_status_changed = FALSE; 00163 return CTRL_BUSY; // BUSY token must be returned to indicate a status change ! 00164 } 00165 else 00166 return CTRL_NO_PRESENT; 00167 } 00168 */ 00169 } 00170 00171 00183 Ctrl_status mmc_sd_read_capacity( U32 _MEM_TYPE_SLOW_ *u32_nb_sector ) 00184 { 00185 // mmc_sd_check_presence(); // ommited because creates interferences with "mmc_sd_test_unit_ready()" function 00186 Sdmmc_access_signal_on(); 00187 if (mmc_sd_init_done == FALSE) 00188 { 00189 mmc_sd_mem_init(); 00190 } 00191 if (mmc_sd_init_done == TRUE) 00192 { 00193 *u32_nb_sector = mmc_sd_last_block_address; 00194 Sdmmc_access_signal_off(); 00195 return CTRL_GOOD; 00196 } 00197 else 00198 { 00199 Sdmmc_access_signal_off(); 00200 return CTRL_NO_PRESENT; 00201 } 00202 } 00203 00204 00216 Bool mmc_sd_wr_protect(void) 00217 { 00218 return FALSE; 00219 } 00220 00221 00231 Bool mmc_sd_removal(void) 00232 { 00233 return FALSE; 00234 // return ((OK == mmc_sd_check_presence()) ? FALSE : TRUE); 00235 } 00236 00237 00238 00239 //------------ STANDARD FUNCTIONS to read/write the memory -------------------- 00240 00257 Ctrl_status mmc_sd_read_10( U32 addr , U16 nb_sector ) 00258 { 00259 bit status; 00260 if (mmc_sd_init_done == FALSE) 00261 { 00262 mmc_sd_mem_init(); 00263 } 00264 if (mmc_sd_init_done == TRUE) 00265 { 00266 Sdmmc_access_signal_on(); 00267 mmc_sd_read_open(addr); 00268 status = mmc_sd_read_sector(nb_sector); 00269 mmc_sd_read_close(); 00270 Sdmmc_access_signal_off(); 00271 if (status == OK) 00272 return CTRL_GOOD; 00273 else 00274 return CTRL_NO_PRESENT; 00275 } 00276 else 00277 return CTRL_NO_PRESENT; 00278 } 00279 00280 00294 Ctrl_status mmc_sd_write_10( U32 addr , U16 nb_sector ) 00295 { 00296 bit status; 00297 if (mmc_sd_init_done == FALSE) 00298 { 00299 mmc_sd_mem_init(); 00300 } 00301 if (mmc_sd_init_done == TRUE) 00302 { 00303 Sdmmc_access_signal_on(); 00304 mmc_sd_write_open(addr); 00305 status = mmc_sd_write_sector(nb_sector); 00306 mmc_sd_write_close(); 00307 Sdmmc_access_signal_off(); 00308 if (status == OK) 00309 return CTRL_GOOD; 00310 else 00311 return CTRL_NO_PRESENT; 00312 } 00313 else 00314 return CTRL_NO_PRESENT; 00315 } 00316 00317 00318 //------------ Standard functions for read/write 1 sector to 1 sector ram buffer ----------------- 00319 00333 Ctrl_status mmc_ram_2_mmc(U32 addr, U8 *ram) 00334 { 00335 #if (MMC_SD_RAM == ENABLE) 00336 Sdmmc_access_signal_on(); 00337 mmc_sd_check_presence(); 00338 if (mmc_sd_init_done == FALSE) 00339 { 00340 mmc_sd_mem_init(); 00341 } 00342 00343 if (mmc_sd_init_done == TRUE) 00344 { 00345 mmc_sd_write_open(addr); 00346 if (KO == mmc_sd_write_sector_from_ram(ram)) 00347 { 00348 mmc_sd_write_close(); 00349 Sdmmc_access_signal_off(); 00350 return CTRL_NO_PRESENT; 00351 } 00352 mmc_sd_write_close(); 00353 Sdmmc_access_signal_off(); 00354 return CTRL_GOOD; 00355 } 00356 Sdmmc_access_signal_off(); 00357 #endif // (MMC_SD_RAM == ENABLE) 00358 return CTRL_NO_PRESENT; 00359 } 00360 00373 Ctrl_status mmc_mmc_2_ram( U32 addr, U8 *ram) 00374 { 00375 #if (MMC_SD_RAM == ENABLE) 00376 Sdmmc_access_signal_on(); 00377 mmc_sd_check_presence(); 00378 if (mmc_sd_init_done == FALSE) 00379 { 00380 mmc_sd_mem_init(); 00381 } 00382 00383 if (mmc_sd_init_done == TRUE) 00384 { 00385 mmc_sd_read_open(addr); 00386 if (KO == mmc_sd_read_sector_to_ram(ram)) 00387 { 00388 mmc_sd_write_close(); 00389 Sdmmc_access_signal_off(); 00390 return CTRL_NO_PRESENT; 00391 } 00392 mmc_sd_read_close(); 00393 Sdmmc_access_signal_off(); 00394 return CTRL_GOOD; 00395 } 00396 Sdmmc_access_signal_off(); 00397 #endif // (MMC_SD_RAM == ENABLE) 00398 return CTRL_NO_PRESENT; 00399 } 00400 00401