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

# flash_lib.h File Reference

`#include "config.h"`

Include dependency graph for flash_lib.h:

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

Go to the source code of this file.

## Defines
#define | Flash_read_id1() ( (*boot_flash_read_sig)(0x0000))
| This macro function allows to read device ID1 of the product.
#define | Flash_read_id2() ( (*boot_flash_read_sig)(0x0002))
| This macro function allows to read device ID2 of the product.
#define | Flash_read_id3() ( (*boot_flash_read_sig)(0x0004))
| This macro function allows to read device ID3 of the product.
#define | Flash_read_osccal() ( (*boot_flash_read_sig)(0x0001))
| This macro function allows to read the OSCAL byte of the product.
#define | Flash_read_fuse_low() ( (*boot_flash_read_fuse)(0x0000))
| This macro function allows to read the low fuse byte of the product.
#define | Flash_read_fuse_high() ( (*boot_flash_read_fuse)(0x0003))
| This macro function allows to read device high fuse byte of the product.
#define | Flash_read_fuse_extended() ( (*boot_flash_read_fuse)(0x0002))
| This macro function allows to read extended fuse byte of the product.
#define | Flash_read_lock() ( (*boot_flash_read_fuse)(0x0001))
| This macro function allows to read lock byte of the product.
## Functions
Bool | flash_lib_check (void)
| This function checks the presence of bootloader.
void | flash_wr_byte (Uint32 addr_byte, Uchar value)
| This function allows to write a byte in the flash memory.
Uchar | flash_wr_block (Byte _MemType_ *src, Uint32 dst, U16 n)
| This function allows to write up to 65535 bytes in the flash memory.
U8 | flash_rd_byte (Uchar farcode *addr)
| This function allows to read a byte in the flash memory.
U16 | flash_rd_word (U16 farcode *addr)
| This function allows to read a word in the flash memory.
## Variables
U8(* | boot_flash_read_sig )(unsigned long adr)
U8(* | boot_flash_read_fuse )(unsigned long adr)

## Detailed Description

This file contains a set of routines to perform flash access.

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

## Define Documentation

#define Flash_read_id1 | ( |  | ) | ( (*boot_flash_read_sig)(0x0000))

This macro function allows to read device ID1 of the product.

Returns:
  word Read value

Definition at line 57 of file flash_lib.h.

#define Flash_read_id2 | ( |  | ) | ( (*boot_flash_read_sig)(0x0002))

This macro function allows to read device ID2 of the product.

Returns:
  word Read value

Definition at line 63 of file flash_lib.h.

#define Flash_read_id3 | ( |  | ) | ( (*boot_flash_read_sig)(0x0004))

This macro function allows to read device ID3 of the product.

Returns:
  word Read value

Definition at line 69 of file flash_lib.h.

#define Flash_read_osccal | ( |  | ) | ( (*boot_flash_read_sig)(0x0001))

This macro function allows to read the OSCAL byte of the product.

Returns:
  word Read value

Definition at line 75 of file flash_lib.h.

#define Flash_read_fuse_low | ( |  | ) | ( (*boot_flash_read_fuse)(0x0000))

This macro function allows to read the low fuse byte of the product.

Returns:
  word Read value

Definition at line 81 of file flash_lib.h.

#define Flash_read_fuse_high | ( |  | ) | ( (*boot_flash_read_fuse)(0x0003))

This macro function allows to read device high fuse byte of the product.

Returns:
  word Read value

Definition at line 87 of file flash_lib.h.

#define Flash_read_fuse_extended | ( |  | ) | ( (*boot_flash_read_fuse)(0x0002))

This macro function allows to read extended fuse byte of the product.

Returns:
  word Read value

Definition at line 93 of file flash_lib.h.

#define Flash_read_lock | ( |  | ) | ( (*boot_flash_read_fuse)(0x0001))

This macro function allows to read lock byte of the product.

Returns:
  word Read value

Definition at line 99 of file flash_lib.h.

## Function Documentation

Bool flash_lib_check | ( | void |  | ) |

This function checks the presence of bootloader.

Returns:
  FALSE, if no code loaded in bootloader area

Definition at line 72 of file flash_lib.c.

References boot_flash_page_erase_and_write.

    00073 {
    00074    return (*((code U16*)((U32)boot_flash_page_erase_and_write*2)) != 0xFFFF);
    00075 }

void flash_wr_byte | ( | Uint32 | addr_byte,
|  | Uchar | value |
| ) |  |  |

This function allows to write a byte in the flash memory.

Parameters:

  | addr_byte | Address in flash memory to write the byte.
  | value | Value to write in the flash memory

Definition at line 83 of file flash_lib.c.

References Disable_flash, Enable_flash, and flash_wr_block().

    00084 {
    00085    Enable_flash();
    00086    flash_wr_block(&value, addr_byte, 1);
    00087    Disable_flash();
    00088 }

Here is the call graph for this function:

Uchar flash_wr_block | ( | Byte _MemType_ * | src,
|  | Uint32 | dst,
|  | U16 | n |
| ) |  |  |

This function allows to write up to 65535 bytes in the flash memory.

This function manages alignement issue.

Parameters:

  | *src | Address of data to write.
  | dst | Start address in flash memory where write data
  | n | number of byte to write

This function manages alignement issue.

Parameters:

  | src | Address of data to write.
  | dst | Start address in flash memory where write data
  | n | number of byte to write

Definition at line 98 of file flash_lib.c.

References FALSE, flash_rd_byte(), flash_rd_word(), LOW, LSB, MSB, and TRUE.

    00099 {
    00100    U16 nb_word, temp16;
    00101    U32 address;
    00102    U32 save_page_adr;
    00103    U8 page_is_blank;
    00104
    00105    while(n)                                     // While there is data to load from src buffer
    00106    {
    00107       page_is_blank=TRUE;
    00108       address=dst-(LOW(dst)%FLASH_PAGE_SIZE);   // Compute the start of the page to be modified
    00109       save_page_adr=address;                    // Memorize page addr
    00110
    00111       // For each word in this page
    00112       for(nb_word=0;nb_word<FLASH_PAGE_SIZE/2;nb_word++)
    00113       {
    00114          if(n)                                  // Still some data to load from src
    00115          {
    00116             if(address>=dst)                    // Current address is inside the target range adr
    00117             {
    00118                MSB(temp16)=(*(U8*)src);         // Load MSB of word from buffer src
    00119                src++; n--;
    00120                if(n)                            // Still some data to load ?
    00121                {
    00122                   LSB(temp16)=(*(U8*)src);      // Load LSB of word from buffer src
    00123                   src++; n--;
    00124                }
    00125                else                             // Only the MSB of the working belong to src buffer
    00126                {                                // Load LSB form exisying flash
    00127                   LSB(temp16)=flash_rd_byte((U8 farcode*)address+1);
    00128                }
    00129             }
    00130             else                                // Current word addr out of dst target
    00131             {                                   // Load MSB from existing flash
    00132                MSB(temp16)=flash_rd_byte((U8 farcode*)address);
    00133                if(address+1==dst)               // Is LSB word addr in dst range ?
    00134                {
    00135                   LSB(temp16)=(*(U8*)src);
    00136                   src++; n--;
    00137                }
    00138                else                             // LSB read from existing flash
    00139                {
    00140                   LSB(temp16)=flash_rd_byte((U8 farcode*)address+1);
    00141                }
    00142             }
    00143          }
    00144          else                                   // Complete page with words from existing flash
    00145          {
    00146             temp16=flash_rd_word((U16 farcode*)address);
    00147          }
    00148          //Load temp buffer
    00149          (*boot_flash_fill_temp_buffer)(temp16,address);
    00150          address+=2;
    00151       }
    00152       address=save_page_adr;
    00153       for(nb_word=0;nb_word<FLASH_PAGE_SIZE/2;nb_word++)
    00154       {
    00155          if(flash_rd_word((U16 farcode*)address)!=0xFFFF)   // Check for Blank page
    00156          {
    00157             page_is_blank=FALSE;
    00158             break;
    00159          }
    00160          address+=2;
    00161       }
    00162       // Now launch prog sequence (with or without page erase)
    00163       address=save_page_adr;
    00164       if(page_is_blank)  { (*boot_flash_prg_page)(save_page_adr); }
    00165       else{(*boot_flash_page_erase_and_write)(save_page_adr);}
    00166       //- First Flash address update for the next page
    00167       address = save_page_adr + FLASH_PAGE_SIZE;
    00168    }
    00169    return TRUE;
    00170 }

Here is the call graph for this function:

U8 flash_rd_byte | ( | U8 farcode * | addr | ) |

This function allows to read a byte in the flash memory.

Parameters:

  | *add | Address of flash memory to read.

Returns:
  byte Read value

Definition at line 178 of file flash_lib.c.

References Disable_flash, and Enable_flash.

    00179 {
    00180    unsigned char temp;
    00181    Enable_flash();
    00182    temp = *addr;
    00183    Disable_flash();
    00184    return temp;
    00185 }

U16 flash_rd_word | ( | U16 farcode * | addr | ) |

This function allows to read a word in the flash memory.

Parameters:

  | *add | Address of flash memory to read.

Returns:
  word Read value

Definition at line 193 of file flash_lib.c.

References Union16::b, Disable_flash, Enable_flash, flash_rd_byte(), and Union16::w.

    00194 {
    00195    Union16 temp;
    00196    Enable_flash();
    00197    temp.b[1] = flash_rd_byte ((Uchar farcode*) addr);
    00198    temp.b[0] = flash_rd_byte ((Uchar farcode*)addr+1);
    00199    Disable_flash();
    00200    return temp.w;
    00201 }

Here is the call graph for this function:

## Variable Documentation

U8(* boot_flash_read_sig)(unsigned long adr)

Definition at line 57 of file flash_lib.c.

U8(* boot_flash_read_fuse)(unsigned long adr)

Definition at line 58 of file flash_lib.c.