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

# df.c File Reference

`#include "config.h"`
`#include "lib_mcu/usb/usb_drv.h"`
`#include "lib_mcu/spi/spi_drv.h"`
`#include "df.h"`

Include dependency graph for df.c:

Go to the source code of this file.

## Functions
static void | df_wait_busy (void)
| This function waits until the DataFlash is not busy.
static void | df_chipselect_current (void)
| This function physically selects the current addressed memory.
void | df_init (void)
| This function initializes the SPI bus over which the DF is controlled.
static void | df_chipselect_memzone (U8 memzone)
| This function selects a DF memory according to the sector pointer.
static U32 | df_translate_addr (Uint32 log_sect_addr)
| This function translates the logical sector address to the physical byte address (1 logical sector = 512 bytes) In function of the memory configuration (number 1x/2x/4x, pages 512b/1024b) :.
Bool | df_mem_check (void)
| This function performs a memory check on all DF.
Bool | df_read_open (Uint32 pos)
| This function opens a DF memory in read mode at a given sector address.
void | df_read_close (void)
| This function unselects the current DF memory.
Bool | df_read_sector (Uint16 nb_sector)
| This function is optimized and writes nb-sector * 512 Bytes from DataFlash memory to USB controller.
Bool | df_write_open (Uint32 pos)
| This function opens a DF memory in write mode at a given sector address.
void | df_write_close (void)
| This function fills the end of the logical sector (512B) and launch page programming.
Bool | df_write_sector (Uint16 nb_sector)
| This function is optimized and writes nb-sector * 512 Bytes from USB controller to DataFlash memory.
bit | df_host_write_sector (Uint16 nb_sector)
| Funtions to link USB HOST flow with data flash.
bit | df_host_read_sector (Uint16 nb_sector)
Bool | df_read_sector_2_ram (U8 *ram)
| This function read one DF sector and load it into a ram buffer.
Bool | df_write_sector_from_ram (U8 *ram)
| This function write one DF sector from a ram buffer.
## Variables
static U32 | gl_ptr_mem
static U8 | df_mem_busy
static U8 | df_select

## Detailed Description

This file contains the low-level dataflash routines - Compiler: IAR EWAVR and GNU GCC for AVR

- Supported devices: AT90USB1287, AT90USB1286, AT90USB647, AT90USB646
  Author:
    Atmel Corporation: http://www.atmel.com
    Support and FAQ: http://support.atmel.no/

Definition in file df.c.

## Function Documentation

static void df_wait_busy | ( | void |  | ) | `[static]`

This function waits until the DataFlash is not busy.

Definition at line 222 of file df.c.

References df_chipselect_current(), Df_desel_all, DF_MEM_BUSY, DF_MSK_BIT_BUSY, DF_RD_STATUS, Spi_ack_write, Spi_read_data, and Spi_write_data.

Referenced by df_read_open(), and df_write_open().

    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 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void df_chipselect_current | ( | void |  | ) | `[static]`

This function physically selects the current addressed memory.

Definition at line 112 of file df.c.

References Df_desel_all, df_select, Df_select_0, and Df_select_1.

Referenced by df_mem_check(), df_read_open(), df_wait_busy(), and df_write_open().

    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 }

Here is the caller graph for this function:

void df_init | ( | void |  | ) |

This function initializes the SPI bus over which the DF is controlled.

Definition at line 64 of file df.c.

Referenced by df_mem_init().

    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 }

Here is the caller graph for this function:

static void df_chipselect_memzone | ( | U8 | memzone | ) | `[static]`

This function selects a DF memory according to the sector pointer.

    //! The "df_select" variable contains the current memory addressed
    //! Refer to the documentation of the function df_translate_addr() for more information about zones management
    //!

Definition at line 88 of file df.c.

References df_select.

Referenced by df_read_open(), and df_write_open().

    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 }

Here is the caller graph for this function:

static U32 df_translate_addr | ( | Uint32 | log_sect_addr | ) | `[static]`

This function translates the logical sector address to the physical byte address (1 logical sector = 512 bytes) In function of the memory configuration (number 1x/2x/4x, pages 512b/1024b) :.

MEMORIES WITH PAGES OF 512 BYTES : ================================== Consider the logical sector address as "xx..xxba", where 'b' and 'a' are the two last bits, and "xx..xx" an undefined number of more significant bits

- If 1 memory is used, this logical sector address directly matches with the physical sector address So the physical sector address is "xx..xxba"
- If 2 memories are used, the bit 'a' indicates the memory concerned, and the rest of the address indicates the physical address So the physical sector address is "xx...xxb"
- If 4 memories are used, the bits 'ba' indicates the memory concerned, and the rest of the address indicates the physical address So the physical sector address is "xx....xx"

MEMORIES WITH PAGES OF 1024 BYTES : ================================== Consider the logical sector address as "xx..xxcba", where 'c', 'b' and 'a' are the three last bits, and "xx..xx" an undefined number of more significant bits

- If 1 memory is used, this logical sector address directly matches with the physical sector address So the physical sector address is "xx..xxcba"
- If 2 memories are used, the bit 'b' indicates the memory concerned, and the rest of the address indicates the physical address So the physical sector address is "xx...xxca"
- If 4 memories are used, the bits 'cb' indicates the memory concerned, and the rest of the address indicates the physical address So the physical sector address is "xx....xxa"

Parameters:

  | log_sect_addr | logical sector address

Returns:
  U32 physical byte address

Definition at line 165 of file df.c.

Referenced by df_read_open(), and df_write_open().

    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 }

Here is the caller graph for this function:

Bool df_mem_check | ( | void |  | ) |

This function performs a memory check on all DF.

Returns:
  bit The memory is ready -> OK The memory check failed -> KO

Definition at line 194 of file df.c.

Referenced by df_test_unit_ready().

    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 }

Here is the caller graph for this function:

Bool df_read_open | ( | Uint32 | pos | ) |

This function opens a DF memory in read mode at a given sector address.

NOTE: Address may not be synchronized on the beginning of a page (depending on the DF page size).

Parameters:

  | pos | Logical sector address

Returns:
  bit The open succeeded -> OK

Definition at line 252 of file df.c.

Referenced by df_df_2_ram(), df_host_read_sector(), df_read_10(), and df_read_sector().

    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 }

Here is the caller graph for this function:

void df_read_close | ( | void |  | ) |

This function unselects the current DF memory.

Definition at line 302 of file df.c.

Referenced by df_df_2_ram(), and df_read_10().

    00303 {
    00304   Df_desel_all();   // Unselect memory
    00305 }

Here is the caller graph for this function:

Bool df_read_sector | ( | Uint16 | nb_sector | ) |

This function is optimized and writes nb-sector * 512 Bytes from DataFlash memory to USB controller.

NOTE:

- First call must be preceded by a call to the df_read_open() function,
- The USB EPIN must have been previously selected,
- USB ping-pong buffers are free,
- As 512 is always a sub-multiple of page size, there is no need to check page end for each Bytes,
- Interrupts are disabled during transfer to avoid timer interrupt,
- nb_sector always >= 1, cannot be zero.

Parameters:

  | nb_sector | number of contiguous sectors to read [IN]

Returns:
  The read succeeded -> OK

Definition at line 323 of file df.c.

Referenced by df_read_10().

    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 }

Here is the caller graph for this function:

Bool df_write_open | ( | Uint32 | pos | ) |

This function opens a DF memory in write mode at a given sector address.

NOTE: If page buffer > 512 bytes, page content is first loaded in buffer to be partially updated by write_byte or write64 functions.

Parameters:

  | pos | Sector address

Returns:
  The open succeeded -> OK

Definition at line 452 of file df.c.

Referenced by df_host_read_10(), df_host_write_sector(), df_ram_2_df(), df_write_10(), and df_write_sector().

    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 }

Here is the caller graph for this function:

void df_write_close | ( | void |  | ) |

This function fills the end of the logical sector (512B) and launch page programming.

Definition at line 524 of file df.c.

Referenced by df_host_read_10(), df_ram_2_df(), and df_write_10().

    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 }

Here is the caller graph for this function:

Bool df_write_sector | ( | Uint16 | nb_sector | ) |

This function is optimized and writes nb-sector * 512 Bytes from USB controller to DataFlash memory.

Funtions to link USB DEVICE flow with data flash.

NOTE:

- First call must be preceded by a call to the df_write_open() function,
- As 512 is always a sub-multiple of page size, there is no need to check page end for each Bytes,
- The USB EPOUT must have been previously selected,
- Interrupts are disabled during transfer to avoid timer interrupt,
- nb_sector always >= 1, cannot be zero.

Parameters:

  | nb_sector | number of contiguous sectors to write [IN]

Returns:
  The write succeeded -> OK

Definition at line 559 of file df.c.

Referenced by df_write_10().

    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 }

Here is the caller graph for this function:

bit df_host_write_sector | ( | Uint16 | nb_sector | ) |

Funtions to link USB HOST flow with data flash.

This function is optimized and writes nb-sector * 512 Bytes from USB HOST controller to DataFlash memory DATA FLOW is: USB => DF NOTE:

- This function should be used only when using the USB controller in HOST mode
- First call must be preceded by a call to the df_write_open() function,
- As 512 is always a sub-multiple of page size, there is no need to check page end for each Bytes,
- The USB PIPE OUT must have been previously selected,
- Interrupts are disabled during transfer to avoid timer interrupt,
- nb_sector always >= 1, cannot be zero.

Warning:
  code:?? bytes (function code length)

Parameters:

  | nb_sector | number of contiguous sectors to write [IN]

Returns:
  bit The write succeeded -> OK /

Definition at line 717 of file df.c.

Referenced by df_host_read_10().

    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 }

Here is the caller graph for this function:

bit df_host_read_sector | ( | Uint16 | nb_sector | ) |

This function is optimized and writes nb-sector * 512 Bytes from DataFlash memory to USB HOST controller DATA FLOW is: DF => USB NOTE:

- This function should ne used only when using the USB controller in HOST mode
- First call must be preceded by a call to the df_read_open() function,
- The USB PIPE IN must have been previously selected,
- USB ping-pong buffers are free,
- As 512 is always a sub-multiple of page size, there is no need to check page end for each Bytes,
- Interrupts are disabled during transfer to avoid timer interrupt,
- nb_sector always >= 1, cannot be zero.

Warning:
  code:?? bytes (function code length)

Parameters:

  | nb_sector | number of contiguous sectors to read [IN]

Returns:
  bit The read succeeded -> OK /

Definition at line 861 of file df.c.

    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 }

Bool df_read_sector_2_ram | ( | U8 * | ram | ) |

This function read one DF sector and load it into a ram buffer.

Functions to read/write one sector (512btes) with ram buffer pointer.

NOTE:

- First call must be preceded by a call to the df_read_open() function,

Parameters:

  | *ram | pointer to ram buffer

Returns:
  The read succeeded -> OK

Definition at line 981 of file df.c.

Referenced by df_df_2_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 }

Here is the caller graph for this function:

Bool df_write_sector_from_ram | ( | U8 * | ram | ) |

This function write one DF sector from a ram buffer.

NOTE:

- First call must be preceded by a call to the df_write_open() function,

Parameters:

  | *ram | pointer to ram buffer

Returns:
  The read succeeded -> OK

Definition at line 1004 of file df.c.

Referenced by df_ram_2_df().

    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 }

Here is the caller graph for this function:

## Variable Documentation

U32 gl_ptr_mem`[static]`

Definition at line 51 of file df.c.

Referenced by df_host_read_sector(), df_host_write_sector(), df_read_open(), df_read_sector(), df_read_sector_2_ram(), df_write_close(), df_write_open(), df_write_sector(), and df_write_sector_from_ram().

U8 df_mem_busy`[static]`

Definition at line 52 of file df.c.

Referenced by df_init().

U8 df_select`[static]`

Definition at line 53 of file df.c.

Referenced by df_chipselect_current(), df_chipselect_memzone(), df_host_write_sector(), df_init(), df_mem_check(), df_read_open(), df_write_close(), df_write_open(), and df_write_sector().