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

# df.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 "lib_mcu/usb/usb_drv.h"            // usb driver definition
    00045 #include "lib_mcu/spi/spi_drv.h"            // spi driver definition
    00046 #include "df.h"                             // dataflash definition
    00047
    00048
    00049 //_____ D E F I N I T I O N ________________________________________________
    00050
    00051 static   U32   gl_ptr_mem;                   // memory data pointer
    00052 static   U8    df_mem_busy;                  // memories in busy state
    00053 static   U8    df_select;                    // current memory selected
    00054
    00055
    00056 //_____ D E C L A R A T I O N _____________________________________________
    00057
    00058 static  void    df_wait_busy(void);
    00059 static  void    df_chipselect_current(void);
    00060
    00061
    00064 void df_init (void)
    00065 {
    00066    // Init the SPI communication link between the DF and the DF driver.
    00067    Df_init_spi();
    00068    Spi_select_master();
    00069    Spi_set_mode(SPI_MODE_3);
    00070    Spi_init_bus();
    00071    Spi_set_rate(SPI_RATE_0);  // SCK freq == fosc/2.
    00072    Spi_disable_ss();
    00073    Spi_enable();
    00074
    00075    // Data Flash Controller init.
    00076    df_mem_busy = 0;           // all memories ready
    00077    df_select = 0;
    00078 }
    00079
    00080
    00088 static void df_chipselect_memzone(U8 memzone)
    00089 {
    00090 #if (DF_NB_MEM == 1)
    00091    df_select = 0;
    00092 #else
    00093    #if (DF_NB_MEM == 2)
    00094       #if (DF_PAGE_SIZE == 512)
    00095    df_select = (memzone>>0)&0x01;
    00096       #else
    00097    df_select = (memzone>>1)&0x01;
    00098       #endif
    00099    #else
    00100       #if (DF_PAGE_SIZE == 512)
    00101    df_select = (memzone>>0)&0x03;
    00102       #else
    00103    df_select = (memzone>>1)&0x03;
    00104       #endif
    00105    #endif
    00106 #endif
    00107 }
    00108
    00109
    00112 static void df_chipselect_current(void)
    00113 {
    00114    Df_desel_all();
    00115    switch (df_select)
    00116    {
    00117       case 0:
    00118       Df_select_0();
    00119       break;
    00120 #if (DF_NB_MEM > 1)
    00121       case 1:
    00122       Df_select_1();
    00123       break;
    00124 #endif
    00125 #if (DF_NB_MEM > 2)
    00126       case 2:
    00127       Df_select_2();
    00128       break;
    00129       case 3:
    00130       Df_select_3();
    00131       break;
    00132 #endif
    00133    }
    00134 }
    00135
    00136
    00165 static U32 df_translate_addr(Uint32 log_sect_addr)
    00166 {
    00167 #if (DF_NB_MEM == 1)
    00168    return (log_sect_addr << 9);  // this case if only one memory...
    00169 #else
    00170    #if (DF_NB_MEM == 2)
    00171       #if (DF_PAGE_SIZE == 512)
    00172    return ((log_sect_addr&0xFFFFFFFE) << 8);
    00173       #else
    00174    return (((log_sect_addr&0xFFFFFFFC) << 8) | ((log_sect_addr&0x00000001) << 9));
    00175       #endif
    00176    #else
    00177       #if (DF_PAGE_SIZE == 512)
    00178    return ((log_sect_addr&0xFFFFFFFC) << 7);
    00179       #else
    00180    return (((log_sect_addr&0xFFFFFFF8) << 7) | ((log_sect_addr&0x00000001) << 9));
    00181       #endif
    00182    #endif
    00183 #endif
    00184 }
    00185
    00186
    00194 Bool df_mem_check (void)
    00195 {
    00196    // DF memory check.
    00197    for(df_select=0; df_select<DF_NB_MEM; df_select++)
    00198    {
    00199       df_chipselect_current();
    00200       Spi_write_data(DF_RD_STATUS); // Send the read status register cmd
    00201                                     // + 1st step to clear the SPIF bit
    00202       Spi_write_data(0);            // dummy write that:
    00203                                     // - finalize the clear of the SPIF bit (access to SPDR)
    00204                                     // - get status register
    00205                                     // - does the 1st step to clear the SPIF bit
    00206       // Following Spi_read_data() finalize the clear of SPIF by accessing SPDR.
    00207       // Check the DF density.
    00208       if ((Spi_read_data() & DF_MSK_DENSITY) != DF_DENSITY)
    00209       {
    00210         // Unexpected value.
    00211         Df_desel_all();
    00212         return (KO);
    00213       }
    00214       Df_desel_all();
    00215    }
    00216    return OK;
    00217 }
    00218
    00219
    00222 static void df_wait_busy (void)
    00223 {
    00224    // Read the status register until the DataFlash is not busy.
    00225    df_chipselect_current();
    00226    Spi_write_data(DF_RD_STATUS); // Send the read status register cmd
    00227                                  // + 1st step to clear the SPIF bit
    00228    Spi_write_data(0);            // dummy write that:
    00229                                  // - finalize the clear of the SPIF bit (access to SPDR)
    00230                                  // - get status register
    00231                                  // - does the 1st step to clear the SPIF bit
    00232    // Following Spi_read_data() finalize the clear of SPIF by accessing SPDR
    00233    while ((Spi_read_data() & DF_MSK_BIT_BUSY) == DF_MEM_BUSY)
    00234    {
    00235       Spi_write_data(0);         // dummy write to get new status
    00236                                  // + 1st step to clear the SPIF bit
    00237    }
    00238    Df_desel_all();               // unselect memory to leave STATUS request mode
    00239    Spi_ack_write();              // Final step to clear the SPIF bit.
    00240 }
    00241
    00242
    00252 Bool df_read_open (Uint32 pos)
    00253 {
    00254    // Set the global memory ptr at a Byte address.
    00255    gl_ptr_mem = df_translate_addr(pos);
    00256
    00257    // Select the DF that memory "pos" points to (the "df_select" variable will be updated)
    00258    df_chipselect_memzone(LSB0(pos));
    00259
    00260    // If the DF memory is busy, wait until it's not.
    00261    if (is_df_busy(df_select))
    00262    {
    00263     df_release_busy(df_select);
    00264     df_wait_busy();                              // wait end of programming
    00265    }
    00266
    00267    // Physically assert the selected dataflash
    00268    df_chipselect_current();
    00269
    00270    //#
    00271    //# Initiate a page read at a given sector address.
    00272    //#
    00273    // Send read main command, + first step to clear the SPIF bit
    00274    Spi_write_data(DF_RD_MAIN);
    00275    // Final step to clear the SPIF bit will be done on the next write
    00276
    00277    // Send the three address Bytes made of:
    00278    // - the page-address(first xbits),
    00279    // - the Byte-address within the page(last ybits).
    00280    // (x and y depending on the DF type).
    00281    // NOTE: the bits of gl_ptr_mem above the 24bits are not useful for the local
    00282    // DF addressing. They are used for DF discrimination when there are several
    00283    // DFs.
    00284    Spi_write_data((MSB1(gl_ptr_mem) << DF_SHFT_B1) | (MSB2(gl_ptr_mem) >> DF_SHFT_B2));
    00285    Spi_write_data(((MSB2(gl_ptr_mem) & ~DF_PAGE_MASK) << DF_SHFT_B1) | (MSB2(gl_ptr_mem) & DF_PAGE_MASK));
    00286    Spi_write_data(MSB3(gl_ptr_mem));
    00287    // Final step to clear the SPIF bit will be done on the next write
    00288
    00289    // 4 dummy writes for reading delay
    00290    Spi_write_data(0xFF);
    00291    Spi_write_data(0xFF);
    00292    Spi_write_data(0xFF);
    00293    Spi_write_data(0xFF); // Tx 0xFF, first step to clear the SPIF bit
    00294    Spi_ack_write();      // Final step to clear the SPIF bit.
    00295
    00296    return OK;
    00297 }
    00298
    00299
    00302 void df_read_close (void)
    00303 {
    00304   Df_desel_all();   // Unselect memory
    00305 }
    00306
    00307
    00323 Bool df_read_sector (Uint16 nb_sector)
    00324 {
    00325    U8 i;
    00326 #ifdef DF_CODE_SIZE_OPTIMIZATION
    00327    U8 j;
    00328 #endif
    00329    do
    00330    {
    00331       for (i = 8; i != 0; i--)
    00332       {
    00333          Disable_interrupt();    // Global disable.
    00334
    00335          // Principle: send any Byte to get a Byte.
    00336          // Spi_write_data(0): send any Byte + 1st step to clear the SPIF bit.
    00337          // Spi_read_data(): get the Byte + final step to clear the SPIF bit.
    00338 #ifdef DF_CODE_SIZE_OPTIMIZATION
    00339          for(j=0;j<64;j++)
    00340          {
    00341             Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00342          }
    00343 #else
    00344          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00345          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00346          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00347          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00348          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00349          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00350          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00351          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00352          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00353          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00354          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00355          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00356          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00357          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00358          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00359          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00360          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00361          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00362          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00363          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00364          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00365          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00366          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00367          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00368          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00369          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00370          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00371          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00372          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00373          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00374          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00375          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00376          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00377          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00378          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00379          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00380          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00381          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00382          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00383          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00384          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00385          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00386          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00387          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00388          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00389          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00390          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00391          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00392          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00393          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00394          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00395          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00396          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00397          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00398          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00399          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00400          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00401          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00402          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00403          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00404          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00405          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00406          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00407          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00408 #endif
    00409          Usb_send_in();          // Send the FIFO IN content to the USB Host.
    00410
    00411          Enable_interrupt();     // Global interrupt re-enable.
    00412
    00413          // Wait until the tx is done so that we may write to the FIFO IN again.
    00414          while(Is_usb_write_enabled()==FALSE)
    00415          {
    00416             if(!Is_usb_endpoint_enabled())
    00417                return KO; // USB Reset
    00418          }
    00419       }
    00420       gl_ptr_mem += 512;         // increment global address pointer
    00421       nb_sector--;               // 1 more sector read
    00422       #if (DF_NB_MEM == 1)       // end of page ?
    00423          #if (DF_PAGE_SIZE == 512)
    00424             Df_desel_all();
    00425             if (nb_sector != 0)
    00426               df_read_open(gl_ptr_mem>>9);
    00427          #else
    00428             if ((MSB2(gl_ptr_mem) & DF_PAGE_MASK) == 0x00)
    00429             {
    00430                Df_desel_all();
    00431                if (nb_sector != 0)
    00432                  df_read_open(gl_ptr_mem>>9);
    00433             }
    00434          #endif
    00435       #endif
    00436    }
    00437    while (nb_sector != 0);
    00438
    00439    return OK;
    00440 }
    00441
    00442
    00452 Bool df_write_open (Uint32 pos)
    00453 {
    00454    // Set the global memory ptr at a Byte address.
    00455    gl_ptr_mem = df_translate_addr(pos);
    00456
    00457    // Select the DF that memory "pos" points to
    00458    df_chipselect_memzone(LSB0(pos));
    00459
    00460    // If the DF memory is busy, wait until it's not.
    00461    if (is_df_busy(df_select))
    00462    {
    00463     df_release_busy(df_select);
    00464     df_wait_busy();                              // wait end of programming
    00465    }
    00466
    00467 #if DF_PAGE_SIZE > 512
    00468    // Physically assert the selected dataflash
    00469    df_chipselect_current();
    00470
    00471    //#
    00472    //# Transfer the current page content in buffer1.
    00473    //#
    00474    // Send Main Mem page to Buffer1 command, + first step to clear the SPIF bit
    00475    Spi_write_data(DF_TF_BUF_1);
    00476    // Final step to clear the SPIF bit will be done on the next write
    00477
    00478    // Send the three address Bytes made of:
    00479    // - the page-address(first xbits),
    00480    // - remaining don't care bits(last ybits).
    00481    // (x and y depending on the DF type).
    00482    // NOTE: the bits of gl_ptr_mem above the 24bits are not useful for the local
    00483    // DF addressing. They are used for DF discrimination when there are several
    00484    // DFs.
    00485    Spi_write_data((MSB1(gl_ptr_mem) << DF_SHFT_B1) | (MSB2(gl_ptr_mem) >> DF_SHFT_B2));
    00486    Spi_write_data(MSB2(gl_ptr_mem) << DF_SHFT_B1);
    00487    Spi_write_data(0);       // Remaining don't care bits.
    00488    Spi_ack_write();         // Final step to clear the SPIF bit.
    00489
    00490    Df_desel_all();          // Unselect memory to validate the command
    00491
    00492    df_wait_busy();               // Wait end of page transfer
    00493 #endif
    00494
    00495    // Physically assert the selected dataflash
    00496    df_chipselect_current();
    00497
    00498    //#
    00499    //# Initiate a page write at a given sector address.
    00500    //#
    00501    // Send Main Memory Page Program Through Buffer1 command,
    00502    // + first step to clear the SPIF bit
    00503    Spi_write_data(DF_PG_BUF_1);
    00504    // Final step to clear the SPIF bit will be done on the next write
    00505
    00506    // Send the three address Bytes made of:
    00507    //  (.) the page-address(first xbits),
    00508    //  (.) the Byte-address within the page(last ybits).
    00509    // (x and y depending on the DF type).
    00510    // NOTE: the bits of gl_ptr_mem above the 24bits are not useful for the local
    00511    // DF addressing. They are used for DF discrimination when there are several
    00512    // DFs.
    00513    Spi_write_data((MSB1(gl_ptr_mem) << DF_SHFT_B1) | (MSB2(gl_ptr_mem) >> DF_SHFT_B2));
    00514    Spi_write_data(((MSB2(gl_ptr_mem) & ~DF_PAGE_MASK) << DF_SHFT_B1) | (MSB2(gl_ptr_mem) & DF_PAGE_MASK));
    00515    Spi_write_data(MSB3(gl_ptr_mem));
    00516    Spi_ack_write();         // Final step to clear the SPIF bit.
    00517
    00518    return OK;
    00519 }
    00520
    00521
    00524 void df_write_close (void)
    00525 {
    00526    //#
    00527    //# While end of logical sector (512B) not reached, zero-fill the remaining
    00528    //# memory Bytes.
    00529    //#
    00530    while ((MSB3(gl_ptr_mem) != 0x00) || ((MSB2(gl_ptr_mem) & 0x01) != 0x00))
    00531    {
    00532       Spi_write_data(0x00);   // - Final step to clear the SPIF bit,
    00533                               // - Write 0x00
    00534                               // - first step to clear the SPIF bit.
    00535       gl_ptr_mem++;
    00536    }
    00537    Spi_ack_write();           // Final step to clear the SPIF bit.
    00538
    00539    Df_desel_all();            // Launch page programming (or simply unselect memory
    00540                               // if the while loop was not performed).
    00541    df_set_busy(df_select);    // Current memory is busy
    00542 }
    00543
    00544
    00559 Bool df_write_sector (Uint16 nb_sector)
    00560 {
    00561    U8 i;
    00562 #ifdef DF_CODE_SIZE_OPTIMIZATION
    00563    U8 j;
    00564 #endif
    00565    do
    00566    {
    00567       //# Write 8x64b = 512b from the USB FIFO OUT.
    00568       for (i = 8; i != 0; i--)
    00569       {
    00570          // Wait end of rx in USB EPOUT.
    00571          while(!Is_usb_read_enabled())
    00572          {
    00573             if(!Is_usb_endpoint_enabled())
    00574               return KO; // USB Reset
    00575          }
    00576
    00577          Disable_interrupt();    // Global disable.
    00578
    00579          // SPI write principle: send a Byte then clear the SPIF flag.
    00580          // Spi_write_data(Usb_read_byte()): (.) Final step to clear the SPIF bit,
    00581          //                                  (.) send a Byte read from USB,
    00582          //                                  (.) 1st step to clear the SPIF bit.
    00583 #ifdef DF_CODE_SIZE_OPTIMIZATION
    00584          for(j=0;j<64;j++)
    00585          {
    00586             Spi_write_data(Usb_read_byte());
    00587          }
    00588 #else
    00589          Spi_write_data(Usb_read_byte());
    00590          Spi_write_data(Usb_read_byte());
    00591          Spi_write_data(Usb_read_byte());
    00592          Spi_write_data(Usb_read_byte());
    00593          Spi_write_data(Usb_read_byte());
    00594          Spi_write_data(Usb_read_byte());
    00595          Spi_write_data(Usb_read_byte());
    00596          Spi_write_data(Usb_read_byte());
    00597          Spi_write_data(Usb_read_byte());
    00598          Spi_write_data(Usb_read_byte());
    00599          Spi_write_data(Usb_read_byte());
    00600          Spi_write_data(Usb_read_byte());
    00601          Spi_write_data(Usb_read_byte());
    00602          Spi_write_data(Usb_read_byte());
    00603          Spi_write_data(Usb_read_byte());
    00604          Spi_write_data(Usb_read_byte());
    00605          Spi_write_data(Usb_read_byte());
    00606          Spi_write_data(Usb_read_byte());
    00607          Spi_write_data(Usb_read_byte());
    00608          Spi_write_data(Usb_read_byte());
    00609          Spi_write_data(Usb_read_byte());
    00610          Spi_write_data(Usb_read_byte());
    00611          Spi_write_data(Usb_read_byte());
    00612          Spi_write_data(Usb_read_byte());
    00613          Spi_write_data(Usb_read_byte());
    00614          Spi_write_data(Usb_read_byte());
    00615          Spi_write_data(Usb_read_byte());
    00616          Spi_write_data(Usb_read_byte());
    00617          Spi_write_data(Usb_read_byte());
    00618          Spi_write_data(Usb_read_byte());
    00619          Spi_write_data(Usb_read_byte());
    00620          Spi_write_data(Usb_read_byte());
    00621          Spi_write_data(Usb_read_byte());
    00622          Spi_write_data(Usb_read_byte());
    00623          Spi_write_data(Usb_read_byte());
    00624          Spi_write_data(Usb_read_byte());
    00625          Spi_write_data(Usb_read_byte());
    00626          Spi_write_data(Usb_read_byte());
    00627          Spi_write_data(Usb_read_byte());
    00628          Spi_write_data(Usb_read_byte());
    00629          Spi_write_data(Usb_read_byte());
    00630          Spi_write_data(Usb_read_byte());
    00631          Spi_write_data(Usb_read_byte());
    00632          Spi_write_data(Usb_read_byte());
    00633          Spi_write_data(Usb_read_byte());
    00634          Spi_write_data(Usb_read_byte());
    00635          Spi_write_data(Usb_read_byte());
    00636          Spi_write_data(Usb_read_byte());
    00637          Spi_write_data(Usb_read_byte());
    00638          Spi_write_data(Usb_read_byte());
    00639          Spi_write_data(Usb_read_byte());
    00640          Spi_write_data(Usb_read_byte());
    00641          Spi_write_data(Usb_read_byte());
    00642          Spi_write_data(Usb_read_byte());
    00643          Spi_write_data(Usb_read_byte());
    00644          Spi_write_data(Usb_read_byte());
    00645          Spi_write_data(Usb_read_byte());
    00646          Spi_write_data(Usb_read_byte());
    00647          Spi_write_data(Usb_read_byte());
    00648          Spi_write_data(Usb_read_byte());
    00649          Spi_write_data(Usb_read_byte());
    00650          Spi_write_data(Usb_read_byte());
    00651          Spi_write_data(Usb_read_byte());
    00652          Spi_write_data(Usb_read_byte());
    00653 #endif
    00654          Spi_ack_write();        // Final step to clear the SPIF bit.
    00655
    00656          Usb_ack_receive_out();  // USB EPOUT read acknowledgement.
    00657
    00658          Enable_interrupt();     // Global re-enable.
    00659       } // for (i = 8; i != 0; i--)
    00660
    00661       gl_ptr_mem += 512;         // Update the memory pointer.
    00662       nb_sector--;               // 1 more sector written
    00663
    00664       //# Launch page programming if end of page.
    00665       //#
    00666       #if DF_PAGE_SIZE > 512
    00667          // Check if end of 1024b page.
    00668          if ((MSB2(gl_ptr_mem) & DF_PAGE_MASK) == 0x00)
    00669          {
    00670             Df_desel_all();               // Launch page programming
    00671             df_set_busy(df_select);       // memory is busy
    00672             #if (DF_NB_MEM == 1)
    00673                if (nb_sector != 0)
    00674                  df_write_open(gl_ptr_mem>>9);
    00675             #endif
    00676          }
    00677       #else
    00678          // Always end of page.
    00679          Df_desel_all();                  // Launch page programming
    00680          df_set_busy(df_select);          // memory is busy
    00681          #if (DF_NB_MEM == 1)
    00682             if (nb_sector != 0)
    00683               df_write_open(gl_ptr_mem>>9);
    00684          #endif
    00685       #endif
    00686    }
    00687    while (nb_sector != 0);
    00688
    00689    return OK;                  // Write done
    00690 }
    00691
    00692
    00693 #if( USB_HOST_FEATURE==ENABLE )
    00717 bit df_host_write_sector (Uint16 nb_sector)
    00718 {
    00719   Byte i;
    00720
    00721    do
    00722    {
    00723     //# Write 8x64b = 512b from the USB FIFO OUT.
    00724     for (i = 8; i != 0; i--)
    00725     {
    00726       // Wait end of rx in USB PIPE IN.
    00727       Host_unfreeze_pipe();
    00728       while(Is_host_read_enabled()==FALSE);
    00729
    00730       Disable_interrupt();    // Global disable.
    00731
    00732       // SPI write principle: send a Byte then clear the SPIF flag.
    00733       // Spi_write_data(Usb_read_byte()): (.) Final step to clear the SPIF bit,
    00734       //                                  (.) send a Byte read from USB,
    00735       //                                  (.) 1st step to clear the SPIF bit.
    00736       Spi_write_data(Usb_read_byte());
    00737       Spi_write_data(Usb_read_byte());
    00738       Spi_write_data(Usb_read_byte());
    00739       Spi_write_data(Usb_read_byte());
    00740       Spi_write_data(Usb_read_byte());
    00741       Spi_write_data(Usb_read_byte());
    00742       Spi_write_data(Usb_read_byte());
    00743       Spi_write_data(Usb_read_byte());
    00744       Spi_write_data(Usb_read_byte());
    00745       Spi_write_data(Usb_read_byte());
    00746       Spi_write_data(Usb_read_byte());
    00747       Spi_write_data(Usb_read_byte());
    00748       Spi_write_data(Usb_read_byte());
    00749       Spi_write_data(Usb_read_byte());
    00750       Spi_write_data(Usb_read_byte());
    00751       Spi_write_data(Usb_read_byte());
    00752       Spi_write_data(Usb_read_byte());
    00753       Spi_write_data(Usb_read_byte());
    00754       Spi_write_data(Usb_read_byte());
    00755       Spi_write_data(Usb_read_byte());
    00756       Spi_write_data(Usb_read_byte());
    00757       Spi_write_data(Usb_read_byte());
    00758       Spi_write_data(Usb_read_byte());
    00759       Spi_write_data(Usb_read_byte());
    00760       Spi_write_data(Usb_read_byte());
    00761       Spi_write_data(Usb_read_byte());
    00762       Spi_write_data(Usb_read_byte());
    00763       Spi_write_data(Usb_read_byte());
    00764       Spi_write_data(Usb_read_byte());
    00765       Spi_write_data(Usb_read_byte());
    00766       Spi_write_data(Usb_read_byte());
    00767       Spi_write_data(Usb_read_byte());
    00768       Spi_write_data(Usb_read_byte());
    00769       Spi_write_data(Usb_read_byte());
    00770       Spi_write_data(Usb_read_byte());
    00771       Spi_write_data(Usb_read_byte());
    00772       Spi_write_data(Usb_read_byte());
    00773       Spi_write_data(Usb_read_byte());
    00774       Spi_write_data(Usb_read_byte());
    00775       Spi_write_data(Usb_read_byte());
    00776       Spi_write_data(Usb_read_byte());
    00777       Spi_write_data(Usb_read_byte());
    00778       Spi_write_data(Usb_read_byte());
    00779       Spi_write_data(Usb_read_byte());
    00780       Spi_write_data(Usb_read_byte());
    00781       Spi_write_data(Usb_read_byte());
    00782       Spi_write_data(Usb_read_byte());
    00783       Spi_write_data(Usb_read_byte());
    00784       Spi_write_data(Usb_read_byte());
    00785       Spi_write_data(Usb_read_byte());
    00786       Spi_write_data(Usb_read_byte());
    00787       Spi_write_data(Usb_read_byte());
    00788       Spi_write_data(Usb_read_byte());
    00789       Spi_write_data(Usb_read_byte());
    00790       Spi_write_data(Usb_read_byte());
    00791       Spi_write_data(Usb_read_byte());
    00792       Spi_write_data(Usb_read_byte());
    00793       Spi_write_data(Usb_read_byte());
    00794       Spi_write_data(Usb_read_byte());
    00795       Spi_write_data(Usb_read_byte());
    00796       Spi_write_data(Usb_read_byte());
    00797       Spi_write_data(Usb_read_byte());
    00798       Spi_write_data(Usb_read_byte());
    00799       Spi_write_data(Usb_read_byte());
    00800       Spi_ack_write();        // Final step to clear the SPIF bit.
    00801       Host_ack_in_received();  // USB PIPE IN read acknowledgement.
    00802
    00803       Enable_interrupt();     // Global re-enable.
    00804     } // for (i = 8; i != 0; i--)
    00805
    00806       gl_ptr_mem += 512;        // Update the memory pointer.
    00807       nb_sector--;              // 1 more sector written
    00808
    00809       //# Launch page programming if end of page.
    00810       //#
    00811       #if DF_PAGE_SIZE > 512
    00812          // Check if end of 1024b page.
    00813          if ((MSB2(gl_ptr_mem) & DF_PAGE_MASK) == 0x00)
    00814          {
    00815             Df_desel_all();         // Launch page programming
    00816             df_set_busy(df_select);     // memory is busy
    00817             #if (DF_NB_MEM == 1)
    00818                if (nb_sector != 0)
    00819                  df_write_open(gl_ptr_mem>>9);
    00820             #endif
    00821          }
    00822       #else
    00823          // Always end of page.
    00824          Df_desel_all();           // Launch page programming
    00825          df_set_busy(df_select);     // memory is busy
    00826          #if (DF_NB_MEM == 1)
    00827             if (nb_sector != 0)
    00828               df_write_open(gl_ptr_mem>>9);
    00829          #endif
    00830       #endif
    00831    }
    00832    while (nb_sector != 0);
    00833
    00834   return OK;                  // Write done
    00835 }
    00836
    00861 bit df_host_read_sector (Uint16 nb_sector)
    00862 {
    00863    U8 i;
    00864    do
    00865    {
    00866       for (i = 8; i != 0; i--)
    00867     {
    00868       Disable_interrupt();    // Global disable.
    00869
    00870       // Principle: send any Byte to get a Byte.
    00871       // Spi_write_data(0): send any Byte + 1st step to clear the SPIF bit.
    00872       // Spi_read_data(): get the Byte + final step to clear the SPIF bit.
    00873       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00874       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00875       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00876       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00877       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00878       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00879       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00880       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00881       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00882       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00883       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00884       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00885       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00886       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00887       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00888       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00889       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00890       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00891       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00892       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00893       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00894       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00895       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00896       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00897       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00898       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00899       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00900       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00901       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00902       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00903       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00904       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00905       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00906       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00907       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00908       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00909       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00910       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00911       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00912       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00913       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00914       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00915       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00916       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00917       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00918       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00919       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00920       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00921       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00922       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00923       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00924       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00925       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00926       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00927       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00928       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00929       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00930       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00931       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00932       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00933       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00934       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00935       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00936       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00937
    00938       Enable_interrupt();     // Global re-enable.
    00939
    00940       //#
    00941       //# Send the USB FIFO IN content to the USB Host.
    00942       //#
    00943       Host_send_out();       // Send the FIFO from the USB Host.
    00944       Host_unfreeze_pipe();
    00945       // Wait until the tx is done so that we may write to the FIFO IN again.
    00946       while(Is_host_out_sent()==FALSE);
    00947       Host_ack_out_sent();
    00948       }
    00949       gl_ptr_mem += 512;      // increment global address pointer
    00950       nb_sector--;            // 1 more sector read
    00951       #if (DF_NB_MEM == 1)    // end of page ?
    00952          #if (DF_PAGE_SIZE == 512)
    00953             Df_desel_all();
    00954             if (nb_sector != 0)
    00955               df_read_open(gl_ptr_mem>>9);
    00956          #else
    00957             if ((MSB2(gl_ptr_mem) & DF_PAGE_MASK) == 0x00)
    00958             {
    00959                Df_desel_all();
    00960                if (nb_sector != 0)
    00961                  df_read_open(gl_ptr_mem>>9);
    00962             }
    00963          #endif
    00964       #endif
    00965    }
    00966    while (nb_sector != 0);
    00967
    00968   return OK;   // Read done.
    00969 }
    00970 #endif // USB_HOST_FEATURE==ENABLE
    00971
    00981 Bool df_read_sector_2_ram(U8 *ram)
    00982 {
    00983    U16 i;
    00984    for(i=0;i<512;i++)
    00985    {
    00986       Spi_write_data(0);
    00987       *ram=Spi_read_data();
    00988       ram++;
    00989    }
    00990    gl_ptr_mem += 512;     // Update the memory pointer.
    00991    return OK;
    00992 }
    00993
    00994
    01004 Bool df_write_sector_from_ram(U8 *ram)
    01005 {
    01006    U16 i;
    01007    for(i=0;i<512;i++)
    01008    {
    01009       Spi_write_data(*ram);
    01010       ram++;
    01011    }
    01012    Spi_ack_write();        // Final step to clear the SPIF bit.
    01013    gl_ptr_mem += 512;      // Update the memory pointer.
    01014    return OK;
    01015 }
    01016