- Main Page
- Modules
- Data Structures
- Files
- Related Pages

# df_mem.c

Go to the documentation of this file.

    00001 /*This file has been prepared for Doxygen automatic documentation generation.*/
    00013
    00014 /* Copyright (c) 2007, 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  * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
    00030  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    00031  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
    00032  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
    00033  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    00034  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    00035  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    00036  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00037  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    00038  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00039  */
    00040
    00041 //_____  I N C L U D E S ___________________________________________________
    00042
    00043 #include "config.h"                         // system configuration
    00044 #include "df_mem.h"
    00045 #include "df.h"
    00046
    00047 //_____ D E F I N I T I O N ________________________________________________
    00048
    00049 // Global value to manage the write protection on DataFlash
    00050 Bool g_b_df_protected = FALSE;
    00051 Bool g_b_df_protected_last = FALSE;
    00052
    00053 void  df_check_init( void );
    00054
    00055
    00056 //_____ D E C L A R A T I O N ______________________________________________
    00057
    00058
    00061 void df_mem_init(void)
    00062 {
    00063    df_init();        // Init the DF driver and its communication link.
    00064 }
    00065
    00066
    00074 Ctrl_status df_test_unit_ready(void)
    00075 {
    00076    if( g_b_df_protected != g_b_df_protected_last )
    00077    {
    00078       g_b_df_protected_last = g_b_df_protected;
    00079       return CTRL_BUSY;
    00080    }
    00081    return( (OK==df_mem_check()) ? CTRL_GOOD : CTRL_NO_PRESENT);
    00082 }
    00083
    00084
    00094 Ctrl_status df_read_capacity( U32 _MEM_TYPE_SLOW_ *u32_nb_sector )
    00095 {
    00096 #ifdef DF_4_MB              // AT45DB321 memories
    00097    *u32_nb_sector = ((DF_NB_MEM*4*1024L*1024L)/512)-1;
    00098 #endif
    00099 #ifdef DF_8_MB              // AT45DB642 memories
    00100    *u32_nb_sector = ((DF_NB_MEM*8*1024L*1024L)/512)-1;
    00101 #endif
    00102    return df_test_unit_ready();
    00103 }
    00104
    00105
    00114 Bool  df_wr_protect(void)
    00115 {
    00116    return g_b_df_protected;
    00117 }
    00118
    00119
    00124 Bool  df_removal(void)
    00125 {
    00126    return FALSE;
    00127 }
    00128
    00129
    00130
    00131 //------------ STANDARD FUNCTIONS to read/write the memory --------------------
    00132
    00142 Ctrl_status df_read_10( U32 addr , U16 nb_sector )
    00143 {
    00144    U8 status = OK;
    00145 #if   (DF_NB_MEM == 1)     // 1 DATAFLASH
    00146    df_read_open(addr);                    // wait device is not busy, then send command & address
    00147    status = df_read_sector(nb_sector);             // transfer data from memory to USB
    00148
    00149 #else                      // 2 or 4 DATAFLASH
    00150    U32   next_sector_addr = addr;
    00151    U16   nb_sectors_remaining = nb_sector;
    00152
    00153    #ifdef DF_4_MB             // 512B PAGES
    00154    while( (nb_sectors_remaining != 0) && (status == OK))
    00155    {
    00156       df_read_open(next_sector_addr);     // wait device is not busy, then send command & address
    00157       status = df_read_sector(1);                  // transfer the page from memory to USB
    00158       df_read_close();
    00159       nb_sectors_remaining--;
    00160       next_sector_addr++;
    00161    }
    00162    #else                      // 1024B PAGES
    00163    while( (nb_sectors_remaining != 0) && (status == OK))
    00164    {
    00165       df_read_open(next_sector_addr);     // wait device is not busy, then send command & address
    00166       if ((LSB0(next_sector_addr)&0x01) == 0)
    00167       {
    00168         if (nb_sectors_remaining == 1)
    00169         {
    00170            status = df_read_sector(1);
    00171            df_read_close();
    00172            nb_sectors_remaining--;
    00173            next_sector_addr++;
    00174         }
    00175         else
    00176         {
    00177           status = df_read_sector(2);
    00178           df_read_close();
    00179           nb_sectors_remaining -= 2;
    00180           next_sector_addr += 2;
    00181         }
    00182       }
    00183       else
    00184       {
    00185         status = df_read_sector(1);
    00186         df_read_close();
    00187         nb_sectors_remaining--;
    00188         next_sector_addr++;
    00189       }
    00190    }
    00191    #endif
    00192 #endif
    00193
    00194    df_read_close();
    00195    if(status == KO)
    00196       return CTRL_FAIL;
    00197    return CTRL_GOOD;
    00198 }
    00199
    00200
    00210 Ctrl_status df_write_10( U32 addr , U16 nb_sector )
    00211 {
    00212 #if   (DF_NB_MEM != 1)                 // if more than 1 memory, variables are needed for zones mangement
    00213    U32   next_sector_addr = addr;
    00214    U16   nb_sectors_remaining = nb_sector;
    00215 #endif
    00216
    00217    if( g_b_df_protected ) return CTRL_FAIL;
    00218
    00219 #if      (DF_NB_MEM == 1)  /* 1 DATAFLASH */
    00220    df_write_open(addr);                    // wait device is not busy, then send command & address
    00221    if( KO == df_write_sector(nb_sector) )  // transfer data from memory to USB
    00222    {
    00223       df_mem_init();
    00224       return CTRL_FAIL;
    00225    }
    00226 #else                      /* 2 or 4 DATAFLASH */
    00227    #ifdef DF_4_MB       // 512B PAGES
    00228    while (nb_sectors_remaining != 0)
    00229    {
    00230       df_write_open(next_sector_addr);     // wait device is not busy, then send command & address
    00231       if( KO == df_write_sector(1))        // transfer the page from memory to USB
    00232       {
    00233          df_mem_init();
    00234          return CTRL_FAIL;
    00235       }
    00236       df_write_close();
    00237       nb_sectors_remaining--;
    00238       next_sector_addr++;
    00239    }
    00240    #else                // 1024B PAGES
    00241    while (nb_sectors_remaining != 0)
    00242    {
    00243       df_write_open(next_sector_addr);     // wait device is not busy, then send command & address
    00244       if ((LSB0(next_sector_addr)&0x01) == 0)
    00245       {
    00246         if (nb_sectors_remaining == 1)
    00247         {
    00248           if( KO == df_write_sector(1))    // transfer the page from memory to USB
    00249           {
    00250              df_mem_init();
    00251              return CTRL_FAIL;
    00252           }
    00253           df_write_close();
    00254           nb_sectors_remaining--;
    00255           next_sector_addr++;
    00256         }
    00257         else
    00258         {
    00259           if( KO == df_write_sector(2))    // transfer the page from memory to USB
    00260           {
    00261              df_mem_init();
    00262              return CTRL_FAIL;
    00263           }
    00264           df_write_close();
    00265           nb_sectors_remaining -= 2;
    00266           next_sector_addr += 2;
    00267         }
    00268       }
    00269       else
    00270       {
    00271         if( KO == df_write_sector(1))      // transfer the page from memory to USB
    00272         {
    00273            df_mem_init();
    00274            return CTRL_FAIL;
    00275         }
    00276         df_write_close();
    00277         nb_sectors_remaining--;
    00278         next_sector_addr++;
    00279       }
    00280    }
    00281    #endif
    00282 #endif
    00283    df_write_close();                    // unselect memory
    00284    return CTRL_GOOD;
    00285 }
    00286
    00287
    00288
    00289 //------------ FUNCTIONS FOR USED WITH USB HOST MODE (host mass storage)-----------------
    00290
    00291 #if( USB_HOST_FEATURE==ENABLE )
    00305 Ctrl_status df_host_write_10( U32 addr , U16 nb_sector )
    00306 {
    00307 #if   (DF_NB_MEM != 1)                 // if more than 1 memory, variables are needed for zones mangement
    00308    U32   next_sector_addr = addr;
    00309    U16   nb_sectors_remaining = nb_sector;
    00310 #endif
    00311
    00312 #if   (DF_NB_MEM == 1)     /* 1 DATAFLASH */
    00313    df_read_open(addr);                    // wait device is not busy, then send command & address
    00314    df_host_read_sector(nb_sector);             // transfer data from memory to USB
    00315 #else                      /* 2 or 4 DATAFLASH */
    00316    #ifdef DF_4_MB             // 512B PAGES
    00317    while (nb_sectors_remaining != 0)
    00318    {
    00319       df_read_open(next_sector_addr);     // wait device is not busy, then send command & address
    00320       df_host_read_sector(1);                  // transfer the page from memory to USB
    00321       df_read_close();
    00322       nb_sectors_remaining--;
    00323       next_sector_addr++;
    00324    }
    00325    #else                      // 1024B PAGES
    00326    while (nb_sectors_remaining != 0)
    00327    {
    00328       df_read_open(next_sector_addr);     // wait device is not busy, then send command & address
    00329       if ((LSB0(next_sector_addr)&0x01) == 0)
    00330       {
    00331         if (nb_sectors_remaining == 1)
    00332         {
    00333            df_host_read_sector(1);
    00334            df_read_close();
    00335            nb_sectors_remaining--;
    00336            next_sector_addr++;
    00337         }
    00338         else
    00339         {
    00340           df_host_read_sector(2);
    00341           df_read_close();
    00342           nb_sectors_remaining -= 2;
    00343           next_sector_addr += 2;
    00344         }
    00345       }
    00346       else
    00347       {
    00348         df_host_read_sector(1);
    00349         df_read_close();
    00350         nb_sectors_remaining--;
    00351         next_sector_addr++;
    00352       }
    00353    }
    00354    #endif
    00355 #endif
    00356    df_read_close();                    // unselect memory
    00357    return CTRL_GOOD;
    00358 }
    00359
    00360
    00374 Ctrl_status df_host_read_10( U32 addr , U16 nb_sector )
    00375 {
    00376 #if   (DF_NB_MEM != 1)                 // if more than 1 memory, variables are needed for zones mangement
    00377    U32   next_sector_addr = addr;
    00378    U16   nb_sectors_remaining = nb_sector;
    00379 #endif
    00380
    00381 #if      (DF_NB_MEM == 1)  /* 1 DATAFLASH */
    00382    df_write_open(addr);                    // wait device is not busy, then send command & address
    00383    df_host_write_sector(nb_sector);             // transfer data from memory to USB
    00384 #else                      /* 2 or 4 DATAFLASH */
    00385    #ifdef DF_4_MB       // 512B PAGES
    00386    while (nb_sectors_remaining != 0)
    00387    {
    00388       df_write_open(next_sector_addr);     // wait device is not busy, then send command & address
    00389       df_host_write_sector(1);                  // transfer the page from memory to USB
    00390       df_write_close();
    00391       nb_sectors_remaining--;
    00392       next_sector_addr++;
    00393    }
    00394    #else                // 1024B PAGES
    00395    while (nb_sectors_remaining != 0)
    00396    {
    00397       df_write_open(next_sector_addr);     // wait device is not busy, then send command & address
    00398       if ((LSB0(next_sector_addr)&0x01) == 0)
    00399       {
    00400         if (nb_sectors_remaining == 1)
    00401         {
    00402           df_host_write_sector(1);
    00403           df_write_close();
    00404           nb_sectors_remaining--;
    00405           next_sector_addr++;
    00406         }
    00407         else
    00408         {
    00409           df_host_write_sector(2);
    00410           df_write_close();
    00411           nb_sectors_remaining -= 2;
    00412           next_sector_addr += 2;
    00413         }
    00414       }
    00415       else
    00416       {
    00417         df_host_write_sector(1);
    00418         df_write_close();
    00419         nb_sectors_remaining--;
    00420         next_sector_addr++;
    00421       }
    00422    }
    00423    #endif
    00424 #endif
    00425    df_write_close();                    // unselect memory
    00426    return CTRL_GOOD;
    00427 }
    00428 #endif   // USB_HOST_FEATURE==ENABLE
    00429
    00430
    00431 //------------ Standard functions for read/write 1 sector to 1 sector ram buffer -----------------
    00432
    00433
    00443 Ctrl_status    df_df_2_ram( U32 addr, U8 *ram)
    00444 {
    00445    df_read_open(addr);
    00446    df_read_sector_2_ram(ram);
    00447    df_read_close();
    00448    return CTRL_GOOD;
    00449 }
    00450
    00451
    00461 Ctrl_status    df_ram_2_df(U32 addr, U8 *ram)
    00462 {
    00463    df_write_open(addr);
    00464    df_write_sector_from_ram(ram);
    00465    df_write_close();
    00466    return CTRL_GOOD;
    00467 }
    00468