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

- File List
- Globals

# flash_lib.c

Go to the documentation of this file.

    00001 /*This file is prepared for Doxygen automatic documentation generation.*/
    00013
    00014 /* Copyright (c) 2009 Atmel Corporation. All rights reserved.
    00015  *
    00016  * Redistribution and use in source and binary forms, with or without
    00017  * modification, are permitted provided that the following conditions are met:
    00018  *
    00019  * 1. Redistributions of source code must retain the above copyright notice,
    00020  * this list of conditions and the following disclaimer.
    00021  *
    00022  * 2. Redistributions in binary form must reproduce the above copyright notice,
    00023  * this list of conditions and the following disclaimer in the documentation
    00024  * and/or other materials provided with the distribution.
    00025  *
    00026  * 3. The name of Atmel may not be used to endorse or promote products derived
    00027  * from this software without specific prior written permission.
    00028  *
    00029  * 4. This software may only be redistributed and used in connection with an Atmel
    00030  * AVR product.
    00031  *
    00032  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
    00033  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    00034  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
    00035  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
    00036  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    00037  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    00038  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    00039  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00040  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    00041  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00042  */
    00043
    00044 /*_____ I N C L U D E S ____________________________________________________*/
    00045 #include "config.h"
    00046 #include "flash_lib.h"
    00047 #include "flash_drv.h"
    00048
    00049
    00050 // These functions pointers are used to call functions entry points in bootloader
    00051 void (*boot_flash_page_erase_and_write)(unsigned long adr)=(void (*)(unsigned long))(LAST_BOOT_ENTRY-12);
    00052 U8 (*boot_flash_read_sig) (unsigned long adr)=(U8 (*)(unsigned long))(LAST_BOOT_ENTRY-10);
    00053 U8 (*boot_flash_read_fuse) (unsigned long adr)=(U8 (*)(unsigned long))(LAST_BOOT_ENTRY-8);
    00054 void (*boot_flash_fill_temp_buffer) (unsigned int data,unsigned int adr)=(void (*)(unsigned int, unsigned int))(LAST_BOOT_ENTRY-6);
    00055 void (*boot_flash_prg_page) (unsigned long adr)=(void (*)(unsigned long))(LAST_BOOT_ENTRY-4);
    00056 void (*boot_flash_page_erase) (unsigned long adr)=(void (*)(unsigned long))(LAST_BOOT_ENTRY-2);
    00057 void (*boot_lock_wr_bits) (unsigned char val)=(void (*)(unsigned char))(LAST_BOOT_ENTRY);
    00058
    00059
    00060
    00061 /*_____ M A C R O S ________________________________________________________*/
    00062
    00063 /*_____ D E F I N I T I O N S ______________________________________________*/
    00064
    00065 /*_____ D E C L A R A T I O N S ____________________________________________*/
    00066
    00067
    00075 void flash_wr_byte(Uint32 addr_byte, Uchar value)
    00076 {
    00077   Enable_flash();
    00078   flash_wr_block(&value, addr_byte, 1);
    00079   Disable_flash();
    00080 }
    00081
    00082
    00092 Uchar flash_wr_block(Byte _MemType_* src, Uint32 dst, U16 n)
    00093 {
    00094    U16 nb_word, temp16;
    00095    U32 address;
    00096    U32 save_page_adr;
    00097    U8 page_is_blank;
    00098
    00099    while(n)  // While there is data to load from src buffer
    00100    {
    00101       page_is_blank=TRUE;
    00102       address=dst-(LOW(dst)%FLASH_PAGE_SIZE); // Compute the start of the page to be modified
    00103       save_page_adr=address; //memorize page addr
    00104
    00105       // For each word in this page
    00106       for(nb_word=0;nb_word<FLASH_PAGE_SIZE/2;nb_word++)
    00107       {
    00108          if(n) //Still some data to load from src
    00109          {
    00110             if(address>=dst) //current address is inside the target range adr
    00111             {
    00112                MSB(temp16)=(*(U8*)src); //load MSB of word from buffer src
    00113                src++; n--;
    00114                if(n) //Still some data to load ?
    00115                {
    00116                   LSB(temp16)=(*(U8*)src); //load LSB of word from buffer src
    00117                   src++; n--;
    00118                }
    00119                else  //only the MSB of the working belong to src buffer
    00120                {     //load LSB form exisying flash
    00121                   LSB(temp16)=flash_rd_byte((U8 farcode*)address+1);
    00122                }
    00123             }
    00124             else  //current word addr out of dst target
    00125             {     //load MSB from existing flash
    00126                MSB(temp16)=flash_rd_byte((U8 farcode*)address);
    00127                if(address+1==dst) // Is LSB word addr in dst range ?
    00128                {
    00129                   LSB(temp16)=(*(U8*)src);
    00130                   src++; n--;
    00131                }
    00132                else // LSB read from existing flash
    00133                {
    00134                   LSB(temp16)=flash_rd_byte((U8 farcode*)address+1);
    00135                }
    00136             }
    00137          }
    00138          else  //complete page with words from existing flash
    00139          {
    00140             temp16=flash_rd_word((U16 farcode*)address);
    00141          }
    00142          //Load temp buffer
    00143          (*boot_flash_fill_temp_buffer)(temp16,address);
    00144          address+=2;
    00145       }
    00146       address=save_page_adr;
    00147       for(nb_word=0;nb_word<FLASH_PAGE_SIZE/2;nb_word++)
    00148       {
    00149          if(flash_rd_word((U16 farcode*)address)!=0xFFFF) //Check for Blank page
    00150          {
    00151             page_is_blank=FALSE;
    00152             break;
    00153          }
    00154          address+=2;
    00155       }
    00156       // Now launch prog sequence (with or without page erase)
    00157       address=save_page_adr;
    00158       if(page_is_blank)  { (*boot_flash_prg_page)(save_page_adr); }
    00159       else{(*boot_flash_page_erase_and_write)(save_page_adr);}
    00160       //- First Flash address update for the next page
    00161       address = save_page_adr + FLASH_PAGE_SIZE;
    00162    }
    00163    return TRUE;
    00164 }
    00165
    00166
    00174 U8 flash_rd_byte(U8 farcode* addr)
    00175 {
    00176   unsigned char temp;
    00177   Enable_flash();
    00178   temp = *addr;
    00179   Disable_flash();
    00180   return temp;
    00181 }
    00182
    00190 U16 flash_rd_word(U16 farcode* addr)
    00191 {
    00192   Union16 temp;
    00193   Enable_flash();
    00194   temp.b[1] = flash_rd_byte ((Uchar farcode*) addr);
    00195   temp.b[0] = flash_rd_byte ((Uchar farcode*)addr+1);
    00196   Disable_flash();
    00197   return temp.w;
    00198 }
    00199
    00200
    00201
    00202
    00203
    00204
    00205