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 "df_mem.h" 00048 #include "df.h" 00049 00050 //_____ D E F I N I T I O N ________________________________________________ 00051 00052 // Global value to manage the write protection on DataFlash 00053 Bool g_b_df_protected = FALSE; 00054 Bool g_b_df_protected_last = FALSE; 00055 00056 void df_check_init( void ); 00057 00058 00059 //_____ D E C L A R A T I O N ______________________________________________ 00060 00061 00064 void df_mem_init(void) 00065 { 00066 df_init(); // Init the DF driver and its communication link. 00067 } 00068 00069 00077 Ctrl_status df_test_unit_ready(void) 00078 { 00079 if( g_b_df_protected != g_b_df_protected_last ) 00080 { 00081 g_b_df_protected_last = g_b_df_protected; 00082 return CTRL_BUSY; 00083 } 00084 return( (OK==df_mem_check()) ? CTRL_GOOD : CTRL_NO_PRESENT); 00085 } 00086 00087 00097 Ctrl_status df_read_capacity( U32 _MEM_TYPE_SLOW_ *u32_nb_sector ) 00098 { 00099 #ifdef DF_4_MB // AT45DB321 memories 00100 *u32_nb_sector = ((DF_NB_MEM*4*1024L*1024L)/512)-1; 00101 #endif 00102 #ifdef DF_8_MB // AT45DB642 memories 00103 *u32_nb_sector = ((DF_NB_MEM*8*1024L*1024L)/512)-1; 00104 #endif 00105 return df_test_unit_ready(); 00106 } 00107 00108 00117 Bool df_wr_protect(void) 00118 { 00119 return g_b_df_protected; 00120 } 00121 00122 00127 Bool df_removal(void) 00128 { 00129 return FALSE; 00130 } 00131 00132 00133 00134 //------------ STANDARD FUNCTIONS to read/write the memory -------------------- 00135 00145 Ctrl_status df_read_10( U32 addr , U16 nb_sector ) 00146 { 00147 U8 status = OK; 00148 #if (DF_NB_MEM == 1) // 1 DATAFLASH 00149 df_read_open(addr); // wait device is not busy, then send command & address 00150 status = df_read_sector(nb_sector); // transfer data from memory to USB 00151 00152 #else // 2 or 4 DATAFLASH 00153 U32 next_sector_addr = addr; 00154 U16 nb_sectors_remaining = nb_sector; 00155 00156 #ifdef DF_4_MB // 512B PAGES 00157 while( (nb_sectors_remaining != 0) && (status == OK)) 00158 { 00159 df_read_open(next_sector_addr); // wait device is not busy, then send command & address 00160 status = df_read_sector(1); // transfer the page from memory to USB 00161 df_read_close(); 00162 nb_sectors_remaining--; 00163 next_sector_addr++; 00164 } 00165 #else // 1024B PAGES 00166 while( (nb_sectors_remaining != 0) && (status == OK)) 00167 { 00168 df_read_open(next_sector_addr); // wait device is not busy, then send command & address 00169 if ((LSB0(next_sector_addr)&0x01) == 0) 00170 { 00171 if (nb_sectors_remaining == 1) 00172 { 00173 status = df_read_sector(1); 00174 df_read_close(); 00175 nb_sectors_remaining--; 00176 next_sector_addr++; 00177 } 00178 else 00179 { 00180 status = df_read_sector(2); 00181 df_read_close(); 00182 nb_sectors_remaining -= 2; 00183 next_sector_addr += 2; 00184 } 00185 } 00186 else 00187 { 00188 status = df_read_sector(1); 00189 df_read_close(); 00190 nb_sectors_remaining--; 00191 next_sector_addr++; 00192 } 00193 } 00194 #endif 00195 #endif 00196 00197 df_read_close(); 00198 if(status == KO) 00199 return CTRL_FAIL; 00200 return CTRL_GOOD; 00201 } 00202 00203 00213 Ctrl_status df_write_10( U32 addr , U16 nb_sector ) 00214 { 00215 #if (DF_NB_MEM != 1) // if more than 1 memory, variables are needed for zones mangement 00216 U32 next_sector_addr = addr; 00217 U16 nb_sectors_remaining = nb_sector; 00218 #endif 00219 00220 if( g_b_df_protected ) return CTRL_FAIL; 00221 00222 #if (DF_NB_MEM == 1) /* 1 DATAFLASH */ 00223 df_write_open(addr); // wait device is not busy, then send command & address 00224 if( KO == df_write_sector(nb_sector) ) // transfer data from memory to USB 00225 { 00226 df_mem_init(); 00227 return CTRL_FAIL; 00228 } 00229 #else /* 2 or 4 DATAFLASH */ 00230 #ifdef DF_4_MB // 512B PAGES 00231 while (nb_sectors_remaining != 0) 00232 { 00233 df_write_open(next_sector_addr); // wait device is not busy, then send command & address 00234 if( KO == df_write_sector(1)) // transfer the page from memory to USB 00235 { 00236 df_mem_init(); 00237 return CTRL_FAIL; 00238 } 00239 df_write_close(); 00240 nb_sectors_remaining--; 00241 next_sector_addr++; 00242 } 00243 #else // 1024B PAGES 00244 while (nb_sectors_remaining != 0) 00245 { 00246 df_write_open(next_sector_addr); // wait device is not busy, then send command & address 00247 if ((LSB0(next_sector_addr)&0x01) == 0) 00248 { 00249 if (nb_sectors_remaining == 1) 00250 { 00251 if( KO == df_write_sector(1)) // transfer the page from memory to USB 00252 { 00253 df_mem_init(); 00254 return CTRL_FAIL; 00255 } 00256 df_write_close(); 00257 nb_sectors_remaining--; 00258 next_sector_addr++; 00259 } 00260 else 00261 { 00262 if( KO == df_write_sector(2)) // transfer the page from memory to USB 00263 { 00264 df_mem_init(); 00265 return CTRL_FAIL; 00266 } 00267 df_write_close(); 00268 nb_sectors_remaining -= 2; 00269 next_sector_addr += 2; 00270 } 00271 } 00272 else 00273 { 00274 if( KO == df_write_sector(1)) // transfer the page from memory to USB 00275 { 00276 df_mem_init(); 00277 return CTRL_FAIL; 00278 } 00279 df_write_close(); 00280 nb_sectors_remaining--; 00281 next_sector_addr++; 00282 } 00283 } 00284 #endif 00285 #endif 00286 df_write_close(); // unselect memory 00287 return CTRL_GOOD; 00288 } 00289 00290 00291 00292 //------------ FUNCTIONS FOR USED WITH USB HOST MODE (host mass storage)----------------- 00293 00294 #if( USB_HOST_FEATURE==ENABLE ) 00308 Ctrl_status df_host_write_10( U32 addr , U16 nb_sector ) 00309 { 00310 #if (DF_NB_MEM != 1) // if more than 1 memory, variables are needed for zones mangement 00311 U32 next_sector_addr = addr; 00312 U16 nb_sectors_remaining = nb_sector; 00313 #endif 00314 00315 #if (DF_NB_MEM == 1) /* 1 DATAFLASH */ 00316 df_read_open(addr); // wait device is not busy, then send command & address 00317 df_host_read_sector(nb_sector); // transfer data from memory to USB 00318 #else /* 2 or 4 DATAFLASH */ 00319 #ifdef DF_4_MB // 512B PAGES 00320 while (nb_sectors_remaining != 0) 00321 { 00322 df_read_open(next_sector_addr); // wait device is not busy, then send command & address 00323 df_host_read_sector(1); // transfer the page from memory to USB 00324 df_read_close(); 00325 nb_sectors_remaining--; 00326 next_sector_addr++; 00327 } 00328 #else // 1024B PAGES 00329 while (nb_sectors_remaining != 0) 00330 { 00331 df_read_open(next_sector_addr); // wait device is not busy, then send command & address 00332 if ((LSB0(next_sector_addr)&0x01) == 0) 00333 { 00334 if (nb_sectors_remaining == 1) 00335 { 00336 df_host_read_sector(1); 00337 df_read_close(); 00338 nb_sectors_remaining--; 00339 next_sector_addr++; 00340 } 00341 else 00342 { 00343 df_host_read_sector(2); 00344 df_read_close(); 00345 nb_sectors_remaining -= 2; 00346 next_sector_addr += 2; 00347 } 00348 } 00349 else 00350 { 00351 df_host_read_sector(1); 00352 df_read_close(); 00353 nb_sectors_remaining--; 00354 next_sector_addr++; 00355 } 00356 } 00357 #endif 00358 #endif 00359 df_read_close(); // unselect memory 00360 return CTRL_GOOD; 00361 } 00362 00363 00377 Ctrl_status df_host_read_10( U32 addr , U16 nb_sector ) 00378 { 00379 #if (DF_NB_MEM != 1) // if more than 1 memory, variables are needed for zones mangement 00380 U32 next_sector_addr = addr; 00381 U16 nb_sectors_remaining = nb_sector; 00382 #endif 00383 00384 #if (DF_NB_MEM == 1) /* 1 DATAFLASH */ 00385 df_write_open(addr); // wait device is not busy, then send command & address 00386 df_host_write_sector(nb_sector); // transfer data from memory to USB 00387 #else /* 2 or 4 DATAFLASH */ 00388 #ifdef DF_4_MB // 512B PAGES 00389 while (nb_sectors_remaining != 0) 00390 { 00391 df_write_open(next_sector_addr); // wait device is not busy, then send command & address 00392 df_host_write_sector(1); // transfer the page from memory to USB 00393 df_write_close(); 00394 nb_sectors_remaining--; 00395 next_sector_addr++; 00396 } 00397 #else // 1024B PAGES 00398 while (nb_sectors_remaining != 0) 00399 { 00400 df_write_open(next_sector_addr); // wait device is not busy, then send command & address 00401 if ((LSB0(next_sector_addr)&0x01) == 0) 00402 { 00403 if (nb_sectors_remaining == 1) 00404 { 00405 df_host_write_sector(1); 00406 df_write_close(); 00407 nb_sectors_remaining--; 00408 next_sector_addr++; 00409 } 00410 else 00411 { 00412 df_host_write_sector(2); 00413 df_write_close(); 00414 nb_sectors_remaining -= 2; 00415 next_sector_addr += 2; 00416 } 00417 } 00418 else 00419 { 00420 df_host_write_sector(1); 00421 df_write_close(); 00422 nb_sectors_remaining--; 00423 next_sector_addr++; 00424 } 00425 } 00426 #endif 00427 #endif 00428 df_write_close(); // unselect memory 00429 return CTRL_GOOD; 00430 } 00431 #endif // USB_HOST_FEATURE==ENABLE 00432 00433 00434 //------------ Standard functions for read/write 1 sector to 1 sector ram buffer ----------------- 00435 00436 00446 Ctrl_status df_df_2_ram( U32 addr, U8 *ram) 00447 { 00448 df_read_open(addr); 00449 df_read_sector_2_ram(ram); 00450 df_read_close(); 00451 return CTRL_GOOD; 00452 } 00453 00454 00464 Ctrl_status df_ram_2_df(U32 addr, U8 *ram) 00465 { 00466 df_write_open(addr); 00467 df_write_sector_from_ram(ram); 00468 df_write_close(); 00469 return CTRL_GOOD; 00470 } 00471