df_mem.c

Go to the documentation of this file.
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 //------------ Standard functions for read/write 1 sector to 1 sector ram buffer -----------------
00292 
00293 
00303 Ctrl_status    df_df_2_ram( U32 addr, U8 *ram)
00304 {
00305    df_read_open(addr);
00306    df_read_sector_2_ram(ram);
00307    df_read_close();
00308    return CTRL_GOOD;
00309 }
00310 
00311 
00321 Ctrl_status    df_ram_2_df(U32 addr, U8 *ram)
00322 {
00323    df_write_open(addr);
00324    df_write_sector_from_ram(ram);
00325    df_write_close();
00326    return CTRL_GOOD;
00327 }
00328 

Generated on Fri Sep 11 14:39:50 2009 for ATMEL by  doxygen 1.5.3