ATMEL: df.c File Reference

- 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 225 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().

    00226 {
    00227    // Read the status register until the DataFlash is not busy.
    00228    df_chipselect_current();
    00229    Spi_write_data(DF_RD_STATUS); // Send the read status register cmd
    00230                                  // + 1st step to clear the SPIF bit
    00231    Spi_write_data(0);            // dummy write that:
    00232                                  // - finalize the clear of the SPIF bit (access to SPDR)
    00233                                  // - get status register
    00234                                  // - does the 1st step to clear the SPIF bit
    00235    // Following Spi_read_data() finalize the clear of SPIF by accessing SPDR
    00236    while ((Spi_read_data() & DF_MSK_BIT_BUSY) == DF_MEM_BUSY)
    00237    {
    00238       Spi_write_data(0);         // dummy write to get new status
    00239                                  // + 1st step to clear the SPIF bit
    00240    }
    00241    Df_desel_all();               // unselect memory to leave STATUS request mode
    00242    Spi_ack_write();              // Final step to clear the SPIF bit.
    00243 }

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 115 of file df.c.

References Df_desel_all, df_select, and Df_select_0.

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

    00116 {
    00117    Df_desel_all();
    00118    switch (df_select)
    00119    {
    00120       case 0:
    00121       Df_select_0();
    00122       break;
    00123 #if (DF_NB_MEM > 1)
    00124       case 1:
    00125       Df_select_1();
    00126       break;
    00127 #endif
    00128 #if (DF_NB_MEM > 2)
    00129       case 2:
    00130       Df_select_2();
    00131       break;
    00132       case 3:
    00133       Df_select_3();
    00134       break;
    00135 #endif
    00136    }
    00137 }

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 67 of file df.c.

Referenced by df_mem_init().

    00068 {
    00069    // Init the SPI communication link between the DF and the DF driver.
    00070    Df_init_spi();
    00071    Spi_select_master();
    00072    Spi_set_mode(SPI_MODE_3);
    00073    Spi_init_bus();
    00074    Spi_set_rate(SPI_RATE_0);  // SCK freq == fosc/2.
    00075    Spi_disable_ss();
    00076    Spi_enable();
    00077
    00078    // Data Flash Controller init.
    00079    df_mem_busy = 0;           // all memories ready
    00080    df_select = 0;
    00081 }

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 91 of file df.c.

References df_select.

Referenced by df_read_open(), and df_write_open().

    00092 {
    00093 #if (DF_NB_MEM == 1)
    00094    df_select = 0;
    00095 #else
    00096    #if (DF_NB_MEM == 2)
    00097       #if (DF_PAGE_SIZE == 512)
    00098    df_select = (memzone>>0)&0x01;
    00099       #else
    00100    df_select = (memzone>>1)&0x01;
    00101       #endif
    00102    #else
    00103       #if (DF_PAGE_SIZE == 512)
    00104    df_select = (memzone>>0)&0x03;
    00105       #else
    00106    df_select = (memzone>>1)&0x03;
    00107       #endif
    00108    #endif
    00109 #endif
    00110 }

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 168 of file df.c.

Referenced by df_read_open(), and df_write_open().

    00169 {
    00170 #if (DF_NB_MEM == 1)
    00171    return (log_sect_addr << 9);  // this case if only one memory...
    00172 #else
    00173    #if (DF_NB_MEM == 2)
    00174       #if (DF_PAGE_SIZE == 512)
    00175    return ((log_sect_addr&0xFFFFFFFE) << 8);
    00176       #else
    00177    return (((log_sect_addr&0xFFFFFFFC) << 8) | ((log_sect_addr&0x00000001) << 9));
    00178       #endif
    00179    #else
    00180       #if (DF_PAGE_SIZE == 512)
    00181    return ((log_sect_addr&0xFFFFFFFC) << 7);
    00182       #else
    00183    return (((log_sect_addr&0xFFFFFFF8) << 7) | ((log_sect_addr&0x00000001) << 9));
    00184       #endif
    00185    #endif
    00186 #endif
    00187 }

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 197 of file df.c.

Referenced by df_test_unit_ready().

    00198 {
    00199    // DF memory check.
    00200    for(df_select=0; df_select<DF_NB_MEM; df_select++)
    00201    {
    00202       df_chipselect_current();
    00203       Spi_write_data(DF_RD_STATUS); // Send the read status register cmd
    00204                                     // + 1st step to clear the SPIF bit
    00205       Spi_write_data(0);            // dummy write that:
    00206                                     // - finalize the clear of the SPIF bit (access to SPDR)
    00207                                     // - get status register
    00208                                     // - does the 1st step to clear the SPIF bit
    00209       // Following Spi_read_data() finalize the clear of SPIF by accessing SPDR.
    00210       // Check the DF density.
    00211       if ((Spi_read_data() & DF_MSK_DENSITY) != DF_DENSITY)
    00212       {
    00213         // Unexpected value.
    00214         Df_desel_all();
    00215         return (KO);
    00216       }
    00217       Df_desel_all();
    00218    }
    00219    return OK;
    00220 }

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 255 of file df.c.

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

    00256 {
    00257    // Set the global memory ptr at a Byte address.
    00258    gl_ptr_mem = df_translate_addr(pos);
    00259
    00260    // Select the DF that memory "pos" points to (the "df_select" variable will be updated)
    00261    df_chipselect_memzone(LSB0(pos));
    00262
    00263    // If the DF memory is busy, wait until it's not.
    00264    if (is_df_busy(df_select))
    00265    {
    00266     df_release_busy(df_select);
    00267     df_wait_busy();                              // wait end of programming
    00268    }
    00269
    00270    // Physically assert the selected dataflash
    00271    df_chipselect_current();
    00272
    00273    //#
    00274    //# Initiate a page read at a given sector address.
    00275    //#
    00276    // Send read main command, + first step to clear the SPIF bit
    00277    Spi_write_data(DF_RD_MAIN);
    00278    // Final step to clear the SPIF bit will be done on the next write
    00279
    00280    // Send the three address Bytes made of:
    00281    // - the page-address(first xbits),
    00282    // - the Byte-address within the page(last ybits).
    00283    // (x and y depending on the DF type).
    00284    // NOTE: the bits of gl_ptr_mem above the 24bits are not useful for the local
    00285    // DF addressing. They are used for DF discrimination when there are several
    00286    // DFs.
    00287    Spi_write_data((MSB1(gl_ptr_mem) << DF_SHFT_B1) | (MSB2(gl_ptr_mem) >> DF_SHFT_B2));
    00288    Spi_write_data(((MSB2(gl_ptr_mem) & ~DF_PAGE_MASK) << DF_SHFT_B1) | (MSB2(gl_ptr_mem) & DF_PAGE_MASK));
    00289    Spi_write_data(MSB3(gl_ptr_mem));
    00290    // Final step to clear the SPIF bit will be done on the next write
    00291
    00292    // 4 dummy writes for reading delay
    00293    Spi_write_data(0xFF);
    00294    Spi_write_data(0xFF);
    00295    Spi_write_data(0xFF);
    00296    Spi_write_data(0xFF); // Tx 0xFF, first step to clear the SPIF bit
    00297    Spi_ack_write();      // Final step to clear the SPIF bit.
    00298
    00299    return OK;
    00300 }

Here is the caller graph for this function:

void df_read_close | ( | void |  | ) |

This function unselects the current DF memory.

Definition at line 305 of file df.c.

Referenced by df_df_2_ram(), and df_read_10().

    00306 {
    00307   Df_desel_all();   // Unselect memory
    00308 }

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 326 of file df.c.

Referenced by df_read_10().

    00327 {
    00328    U8 i;
    00329 #ifdef DF_CODE_SIZE_OPTIMIZATION
    00330    U8 j;
    00331 #endif
    00332    do
    00333    {
    00334       for (i = 8; i != 0; i--)
    00335       {
    00336          Disable_interrupt();    // Global disable.
    00337
    00338          // Principle: send any Byte to get a Byte.
    00339          // Spi_write_data(0): send any Byte + 1st step to clear the SPIF bit.
    00340          // Spi_read_data(): get the Byte + final step to clear the SPIF bit.
    00341 #ifdef DF_CODE_SIZE_OPTIMIZATION
    00342          for(j=0;j<64;j++)
    00343          {
    00344             Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00345          }
    00346 #else
    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          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00409          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00410          Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00411 #endif
    00412          Usb_send_in();          // Send the FIFO IN content to the USB Host.
    00413
    00414          Enable_interrupt();     // Global interrupt re-enable.
    00415
    00416          // Wait until the tx is done so that we may write to the FIFO IN again.
    00417          while(Is_usb_write_enabled()==FALSE)
    00418          {
    00419             if(!Is_usb_endpoint_enabled())
    00420                return KO; // USB Reset
    00421          }
    00422       }
    00423       gl_ptr_mem += 512;         // increment global address pointer
    00424       nb_sector--;               // 1 more sector read
    00425       #if (DF_NB_MEM == 1)       // end of page ?
    00426          #if (DF_PAGE_SIZE == 512)
    00427             Df_desel_all();
    00428             if (nb_sector != 0)
    00429               df_read_open(gl_ptr_mem>>9);
    00430          #else
    00431             if ((MSB2(gl_ptr_mem) & DF_PAGE_MASK) == 0x00)
    00432             {
    00433                Df_desel_all();
    00434                if (nb_sector != 0)
    00435                  df_read_open(gl_ptr_mem>>9);
    00436             }
    00437          #endif
    00438       #endif
    00439    }
    00440    while (nb_sector != 0);
    00441
    00442    return OK;
    00443 }

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 455 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().

    00456 {
    00457    // Set the global memory ptr at a Byte address.
    00458    gl_ptr_mem = df_translate_addr(pos);
    00459
    00460    // Select the DF that memory "pos" points to
    00461    df_chipselect_memzone(LSB0(pos));
    00462
    00463    // If the DF memory is busy, wait until it's not.
    00464    if (is_df_busy(df_select))
    00465    {
    00466     df_release_busy(df_select);
    00467     df_wait_busy();                              // wait end of programming
    00468    }
    00469
    00470 #if DF_PAGE_SIZE > 512
    00471    // Physically assert the selected dataflash
    00472    df_chipselect_current();
    00473
    00474    //#
    00475    //# Transfer the current page content in buffer1.
    00476    //#
    00477    // Send Main Mem page to Buffer1 command, + first step to clear the SPIF bit
    00478    Spi_write_data(DF_TF_BUF_1);
    00479    // Final step to clear the SPIF bit will be done on the next write
    00480
    00481    // Send the three address Bytes made of:
    00482    // - the page-address(first xbits),
    00483    // - remaining don't care bits(last ybits).
    00484    // (x and y depending on the DF type).
    00485    // NOTE: the bits of gl_ptr_mem above the 24bits are not useful for the local
    00486    // DF addressing. They are used for DF discrimination when there are several
    00487    // DFs.
    00488    Spi_write_data((MSB1(gl_ptr_mem) << DF_SHFT_B1) | (MSB2(gl_ptr_mem) >> DF_SHFT_B2));
    00489    Spi_write_data(MSB2(gl_ptr_mem) << DF_SHFT_B1);
    00490    Spi_write_data(0);       // Remaining don't care bits.
    00491    Spi_ack_write();         // Final step to clear the SPIF bit.
    00492
    00493    Df_desel_all();          // Unselect memory to validate the command
    00494
    00495    df_wait_busy();               // Wait end of page transfer
    00496 #endif
    00497
    00498    // Physically assert the selected dataflash
    00499    df_chipselect_current();
    00500
    00501    //#
    00502    //# Initiate a page write at a given sector address.
    00503    //#
    00504    // Send Main Memory Page Program Through Buffer1 command,
    00505    // + first step to clear the SPIF bit
    00506    Spi_write_data(DF_PG_BUF_1);
    00507    // Final step to clear the SPIF bit will be done on the next write
    00508
    00509    // Send the three address Bytes made of:
    00510    //  (.) the page-address(first xbits),
    00511    //  (.) the Byte-address within the page(last ybits).
    00512    // (x and y depending on the DF type).
    00513    // NOTE: the bits of gl_ptr_mem above the 24bits are not useful for the local
    00514    // DF addressing. They are used for DF discrimination when there are several
    00515    // DFs.
    00516    Spi_write_data((MSB1(gl_ptr_mem) << DF_SHFT_B1) | (MSB2(gl_ptr_mem) >> DF_SHFT_B2));
    00517    Spi_write_data(((MSB2(gl_ptr_mem) & ~DF_PAGE_MASK) << DF_SHFT_B1) | (MSB2(gl_ptr_mem) & DF_PAGE_MASK));
    00518    Spi_write_data(MSB3(gl_ptr_mem));
    00519    Spi_ack_write();         // Final step to clear the SPIF bit.
    00520
    00521    return OK;
    00522 }

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 527 of file df.c.

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

    00528 {
    00529    //#
    00530    //# While end of logical sector (512B) not reached, zero-fill the remaining
    00531    //# memory Bytes.
    00532    //#
    00533    while ((MSB3(gl_ptr_mem) != 0x00) || ((MSB2(gl_ptr_mem) & 0x01) != 0x00))
    00534    {
    00535       Spi_write_data(0x00);   // - Final step to clear the SPIF bit,
    00536                               // - Write 0x00
    00537                               // - first step to clear the SPIF bit.
    00538       gl_ptr_mem++;
    00539    }
    00540    Spi_ack_write();           // Final step to clear the SPIF bit.
    00541
    00542    Df_desel_all();            // Launch page programming (or simply unselect memory
    00543                               // if the while loop was not performed).
    00544    df_set_busy(df_select);    // Current memory is busy
    00545 }

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 562 of file df.c.

Referenced by df_write_10().

    00563 {
    00564    U8 i;
    00565 #ifdef DF_CODE_SIZE_OPTIMIZATION
    00566    U8 j;
    00567 #endif
    00568    do
    00569    {
    00570       //# Write 8x64b = 512b from the USB FIFO OUT.
    00571       for (i = 8; i != 0; i--)
    00572       {
    00573          // Wait end of rx in USB EPOUT.
    00574          while(!Is_usb_read_enabled())
    00575          {
    00576             if(!Is_usb_endpoint_enabled())
    00577               return KO; // USB Reset
    00578          }
    00579
    00580          Disable_interrupt();    // Global disable.
    00581
    00582          // SPI write principle: send a Byte then clear the SPIF flag.
    00583          // Spi_write_data(Usb_read_byte()): (.) Final step to clear the SPIF bit,
    00584          //                                  (.) send a Byte read from USB,
    00585          //                                  (.) 1st step to clear the SPIF bit.
    00586 #ifdef DF_CODE_SIZE_OPTIMIZATION
    00587          for(j=0;j<64;j++)
    00588          {
    00589             Spi_write_data(Usb_read_byte());
    00590          }
    00591 #else
    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          Spi_write_data(Usb_read_byte());
    00654          Spi_write_data(Usb_read_byte());
    00655          Spi_write_data(Usb_read_byte());
    00656 #endif
    00657          Spi_ack_write();        // Final step to clear the SPIF bit.
    00658
    00659          Usb_ack_receive_out();  // USB EPOUT read acknowledgement.
    00660
    00661          Enable_interrupt();     // Global re-enable.
    00662       } // for (i = 8; i != 0; i--)
    00663
    00664       gl_ptr_mem += 512;         // Update the memory pointer.
    00665       nb_sector--;               // 1 more sector written
    00666
    00667       //# Launch page programming if end of page.
    00668       //#
    00669       #if DF_PAGE_SIZE > 512
    00670          // Check if end of 1024b page.
    00671          if ((MSB2(gl_ptr_mem) & DF_PAGE_MASK) == 0x00)
    00672          {
    00673             Df_desel_all();               // Launch page programming
    00674             df_set_busy(df_select);       // memory is busy
    00675             #if (DF_NB_MEM == 1)
    00676                if (nb_sector != 0)
    00677                  df_write_open(gl_ptr_mem>>9);
    00678             #endif
    00679          }
    00680       #else
    00681          // Always end of page.
    00682          Df_desel_all();                  // Launch page programming
    00683          df_set_busy(df_select);          // memory is busy
    00684          #if (DF_NB_MEM == 1)
    00685             if (nb_sector != 0)
    00686               df_write_open(gl_ptr_mem>>9);
    00687          #endif
    00688       #endif
    00689    }
    00690    while (nb_sector != 0);
    00691
    00692    return OK;                  // Write done
    00693 }

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 720 of file df.c.

Referenced by df_host_read_10().

    00721 {
    00722   Byte i;
    00723
    00724    do
    00725    {
    00726     //# Write 8x64b = 512b from the USB FIFO OUT.
    00727     for (i = 8; i != 0; i--)
    00728     {
    00729       // Wait end of rx in USB PIPE IN.
    00730       Host_unfreeze_pipe();
    00731       while(Is_host_read_enabled()==FALSE);
    00732
    00733       Disable_interrupt();    // Global disable.
    00734
    00735       // SPI write principle: send a Byte then clear the SPIF flag.
    00736       // Spi_write_data(Usb_read_byte()): (.) Final step to clear the SPIF bit,
    00737       //                                  (.) send a Byte read from USB,
    00738       //                                  (.) 1st step to clear the SPIF bit.
    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_write_data(Usb_read_byte());
    00801       Spi_write_data(Usb_read_byte());
    00802       Spi_write_data(Usb_read_byte());
    00803       Spi_ack_write();        // Final step to clear the SPIF bit.
    00804       Host_ack_in_received();  // USB PIPE IN read acknowledgement.
    00805
    00806       Enable_interrupt();     // Global re-enable.
    00807     } // for (i = 8; i != 0; i--)
    00808
    00809       gl_ptr_mem += 512;        // Update the memory pointer.
    00810       nb_sector--;              // 1 more sector written
    00811
    00812       //# Launch page programming if end of page.
    00813       //#
    00814       #if DF_PAGE_SIZE > 512
    00815          // Check if end of 1024b page.
    00816          if ((MSB2(gl_ptr_mem) & DF_PAGE_MASK) == 0x00)
    00817          {
    00818             Df_desel_all();         // Launch page programming
    00819             df_set_busy(df_select);     // memory is busy
    00820             #if (DF_NB_MEM == 1)
    00821                if (nb_sector != 0)
    00822                  df_write_open(gl_ptr_mem>>9);
    00823             #endif
    00824          }
    00825       #else
    00826          // Always end of page.
    00827          Df_desel_all();           // Launch page programming
    00828          df_set_busy(df_select);     // memory is busy
    00829          #if (DF_NB_MEM == 1)
    00830             if (nb_sector != 0)
    00831               df_write_open(gl_ptr_mem>>9);
    00832          #endif
    00833       #endif
    00834    }
    00835    while (nb_sector != 0);
    00836
    00837   return OK;                  // Write done
    00838 }

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 864 of file df.c.

    00865 {
    00866    U8 i;
    00867    do
    00868    {
    00869       for (i = 8; i != 0; i--)
    00870     {
    00871       Disable_interrupt();    // Global disable.
    00872
    00873       // Principle: send any Byte to get a Byte.
    00874       // Spi_write_data(0): send any Byte + 1st step to clear the SPIF bit.
    00875       // Spi_read_data(): get the Byte + final step to clear the SPIF bit.
    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       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00938       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00939       Spi_write_data(0); Usb_write_byte(Spi_read_data());
    00940
    00941       Enable_interrupt();     // Global re-enable.
    00942
    00943       //#
    00944       //# Send the USB FIFO IN content to the USB Host.
    00945       //#
    00946       Host_send_out();       // Send the FIFO from the USB Host.
    00947       Host_unfreeze_pipe();
    00948       // Wait until the tx is done so that we may write to the FIFO IN again.
    00949       while(Is_host_out_sent()==FALSE);
    00950       Host_ack_out_sent();
    00951       }
    00952       gl_ptr_mem += 512;      // increment global address pointer
    00953       nb_sector--;            // 1 more sector read
    00954       #if (DF_NB_MEM == 1)    // end of page ?
    00955          #if (DF_PAGE_SIZE == 512)
    00956             Df_desel_all();
    00957             if (nb_sector != 0)
    00958               df_read_open(gl_ptr_mem>>9);
    00959          #else
    00960             if ((MSB2(gl_ptr_mem) & DF_PAGE_MASK) == 0x00)
    00961             {
    00962                Df_desel_all();
    00963                if (nb_sector != 0)
    00964                  df_read_open(gl_ptr_mem>>9);
    00965             }
    00966          #endif
    00967       #endif
    00968    }
    00969    while (nb_sector != 0);
    00970
    00971   return OK;   // Read done.
    00972 }

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 984 of file df.c.

Referenced by df_df_2_ram().

    00985 {
    00986    U16 i;
    00987    for(i=0;i<512;i++)
    00988    {
    00989       Spi_write_data(0);
    00990       *ram=Spi_read_data();
    00991       ram++;
    00992    }
    00993    gl_ptr_mem += 512;     // Update the memory pointer.
    00994    return OK;
    00995 }

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 1007 of file df.c.

Referenced by df_ram_2_df().

    01008 {
    01009    U16 i;
    01010    for(i=0;i<512;i++)
    01011    {
    01012       Spi_write_data(*ram);
    01013       ram++;
    01014    }
    01015    Spi_ack_write();        // Final step to clear the SPIF bit.
    01016    gl_ptr_mem += 512;      // Update the memory pointer.
    01017    return OK;
    01018 }

Here is the caller graph for this function:

## Variable Documentation

U32 gl_ptr_mem`[static]`

Definition at line 54 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 55 of file df.c.

Referenced by df_init().

U8 df_select`[static]`

Definition at line 56 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().