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

# df.h File Reference

`#include "config.h"`

Include dependency graph for df.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Defines
#define | MEM_BSY 0
#define | MEM_OK 1
#define | MEM_KO 2
#define | DF_MSK_DENSITY ((Byte)0x3C)
#define | DF_MSK_BIT_BUSY ((Byte)0x80)
#define | DF_MEM_BUSY ((Byte)0x00)
#define | DF_RD_STATUS ((Byte)0xD7)
#define | DF_PG_ERASE ((Byte)0x81)
#define | DF_BK_ERASE ((Byte)0x50)
#define | DF_WR_BUF_1 ((Byte)0x84)
#define | DF_WR_BUF_2 ((Byte)0x87)
#define | DF_B1_MAIN ((Byte)0x83)
#define | DF_B2_MAIN ((Byte)0x86)
#define | DF_RD_MAIN ((Byte)0xD2)
#define | DF_TF_BUF_1 ((Byte)0x53)
#define | DF_TF_BUF_2 ((Byte)0x55)
#define | DF_RD_BUF_1 ((Byte)0xD4)
#define | DF_RD_BUF_2 ((Byte)0xD6)
#define | DF_4MB ((Byte)0)
#define | DF_8MB ((Byte)1)
#define | DF_16MB ((Byte)2)
#define | DF_32MB ((Byte)3)
#define | DF_64MB ((Byte)4)
#define | df_set_busy(i) (df_mem_busy |= (1<<i))
#define | df_release_busy(i) (df_mem_busy &= ~(1<<i))
#define | is_df_busy(i) (((df_mem_busy&(1<<i)) != 0) ? TRUE : FALSE)
## Functions
void | df_init (void)
| This function initializes the SPI bus over which the DF is controlled.
Bool | df_mem_check (void)
| This function performs a memory check on all DF.
Bool | df_read_open (Uint32)
| 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_write_open (Uint32)
| 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)
| Funtions to link USB DEVICE flow with data flash.
Bool | df_read_sector (Uint16)
| This function is optimized and writes nb-sector * 512 Bytes from DataFlash memory to USB controller.
bit | df_host_write_sector (Uint16)
| Funtions to link USB HOST flow with data flash.
bit | df_host_read_sector (Uint16)
Bool | df_read_sector_2_ram (U8 *ram)
| Functions to read/write one sector (512btes) with ram buffer pointer.
Bool | df_write_sector_from_ram (U8 *ram)
| This function write one DF sector from a ram buffer.

## 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.h.

## Define Documentation

#define MEM_BSY 0

Definition at line 51 of file df.h.

#define MEM_OK 1

Definition at line 52 of file df.h.

#define MEM_KO 2

Definition at line 53 of file df.h.

#define DF_MSK_DENSITY ((Byte)0x3C)

Definition at line 56 of file df.h.

Referenced by df_mem_check().

#define DF_MSK_BIT_BUSY ((Byte)0x80)

Definition at line 57 of file df.h.

Referenced by df_wait_busy().

#define DF_MEM_BUSY ((Byte)0x00)

Definition at line 58 of file df.h.

Referenced by df_wait_busy().

#define DF_RD_STATUS ((Byte)0xD7)

Definition at line 60 of file df.h.

Referenced by df_mem_check(), and df_wait_busy().

#define DF_PG_ERASE ((Byte)0x81)

Definition at line 61 of file df.h.

#define DF_BK_ERASE ((Byte)0x50)

Definition at line 62 of file df.h.

#define DF_WR_BUF_1 ((Byte)0x84)

Definition at line 64 of file df.h.

#define DF_WR_BUF_2 ((Byte)0x87)

Definition at line 65 of file df.h.

#define DF_B1_MAIN ((Byte)0x83)

Definition at line 66 of file df.h.

#define DF_B2_MAIN ((Byte)0x86)

Definition at line 67 of file df.h.

#define DF_RD_MAIN ((Byte)0xD2)

Definition at line 69 of file df.h.

Referenced by df_read_open().

#define DF_TF_BUF_1 ((Byte)0x53)

Definition at line 70 of file df.h.

Referenced by df_write_open().

#define DF_TF_BUF_2 ((Byte)0x55)

Definition at line 71 of file df.h.

#define DF_RD_BUF_1 ((Byte)0xD4)

Definition at line 72 of file df.h.

#define DF_RD_BUF_2 ((Byte)0xD6)

Definition at line 73 of file df.h.

#define DF_4MB ((Byte)0)

Definition at line 76 of file df.h.

#define DF_8MB ((Byte)1)

Definition at line 77 of file df.h.

#define DF_16MB ((Byte)2)

Definition at line 78 of file df.h.

#define DF_32MB ((Byte)3)

Definition at line 79 of file df.h.

#define DF_64MB ((Byte)4)

Definition at line 80 of file df.h.

#define df_set_busy | ( | i |  | ) | (df_mem_busy |= (1<<i))

Definition at line 86 of file df.h.

Referenced by df_host_write_sector(), df_write_close(), and df_write_sector().

#define df_release_busy | ( | i |  | ) | (df_mem_busy &= ~(1<<i))

Definition at line 87 of file df.h.

Referenced by df_read_open(), and df_write_open().

#define is_df_busy | ( | i |  | ) | (((df_mem_busy&(1<<i)) != 0) ? TRUE : FALSE)

Definition at line 88 of file df.h.

Referenced by df_read_open(), and df_write_open().

## Function Documentation

void df_init | ( | void |  | ) |

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

Definition at line 64 of file df.c.

References Df_init_spi, df_mem_busy, df_select, Spi_disable_ss, Spi_enable, Spi_init_bus, SPI_MODE_3, SPI_RATE_0, Spi_select_master, Spi_set_mode, and Spi_set_rate.

    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 }

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.

References df_chipselect_current(), Df_desel_all, DF_MSK_DENSITY, DF_NB_MEM, DF_RD_STATUS, df_select, KO, OK, Spi_read_data, and Spi_write_data.

    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 call 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.

References df_chipselect_current(), df_chipselect_memzone(), DF_RD_MAIN, df_release_busy, df_select, df_translate_addr(), df_wait_busy(), gl_ptr_mem, is_df_busy, LSB0, MSB1, MSB2, MSB3, OK, Spi_ack_write, and Spi_write_data.

    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 call graph for this function:

void df_read_close | ( | void |  | ) |

This function unselects the current DF memory.

Definition at line 302 of file df.c.

References Df_desel_all.

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

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.

References df_chipselect_current(), df_chipselect_memzone(), Df_desel_all, df_release_busy, df_select, DF_TF_BUF_1, df_translate_addr(), df_wait_busy(), gl_ptr_mem, is_df_busy, LSB0, MSB1, MSB2, MSB3, OK, Spi_ack_write, and Spi_write_data.

    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 call 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.

References Df_desel_all, df_select, df_set_busy, gl_ptr_mem, MSB2, MSB3, Spi_ack_write, and Spi_write_data.

    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 }

Bool df_write_sector | ( | Uint16 | nb_sector | ) |

Funtions to link USB DEVICE flow with data flash.

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.

References Df_desel_all, df_select, df_set_busy, df_write_open(), gl_ptr_mem, i, Is_usb_endpoint_enabled, Is_usb_read_enabled, j, KO, MSB2, OK, Spi_ack_write, Spi_write_data, Usb_ack_receive_out, and Usb_read_byte.

    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 call 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.

References Df_desel_all, df_read_open(), FALSE, gl_ptr_mem, i, Is_usb_endpoint_enabled, Is_usb_write_enabled, j, KO, MSB2, OK, Spi_read_data, Spi_write_data, Usb_send_in, and Usb_write_byte.

    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 call 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.

References Df_desel_all, df_select, df_set_busy, df_write_open(), FALSE, gl_ptr_mem, Host_ack_in_received, Host_unfreeze_pipe, i, Is_host_read_enabled, MSB2, OK, Spi_ack_write, Spi_write_data, and Usb_read_byte.

    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 call 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.

References Df_desel_all, df_read_open(), FALSE, gl_ptr_mem, Host_ack_out_sent, Host_send_out, Host_unfreeze_pipe, i, Is_host_out_sent, MSB2, OK, Spi_read_data, Spi_write_data, and Usb_write_byte.

    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 }

Here is the call graph for this function:

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

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

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.

References gl_ptr_mem, i, OK, Spi_read_data, and Spi_write_data.

    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 }

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.

References gl_ptr_mem, i, OK, Spi_ack_write, and Spi_write_data.

    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 }