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

- File List
- Globals

# flash_boot_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_boot_lib.h"
    00047 #include "flash_boot_drv.h"
    00048
    00049
    00050 #ifndef FLASH_SIZE
    00051 #error You must define FLASH_SIZE in bytes in config.h
    00052 #endif
    00053
    00054
    00055
    00056 /*_____ M A C R O S ________________________________________________________*/
    00057
    00058 /*_____ D E F I N I T I O N S ______________________________________________*/
    00059
    00060 /*_____ D E C L A R A T I O N S ____________________________________________*/
    00061
    00062
    00063 #pragma location="BOOT"
    00064 void flash_wr_byte(U16 addr_byte, Uchar value)
    00065 {
    00066   Enable_flash();
    00067   flash_wr_block(&value, addr_byte, 1);
    00068   Disable_flash();
    00069 }
    00070
    00071
    00072 #pragma location="BOOT"
    00073 Uchar flash_wr_block(Byte _MemType_* src, U16 dst, U16 n)
    00074 {
    00075    U16 nb_word, temp16;
    00076    U16 address;
    00077    U16 save_page_adr;
    00078
    00079    while(n)  // While there is data to load from src buffer
    00080    {
    00081       address=dst-(LOW(dst)%FLASH_PAGE_SIZE); // Compute the start of the page to be modified
    00082       save_page_adr=address; //memorize page addr
    00083       // For each word in this page
    00084       for(nb_word=0;nb_word<FLASH_PAGE_SIZE/2;nb_word++)
    00085       {
    00086          if(n) //Still some data to load from src
    00087          {
    00088             if(address>=dst) //current address is inside the target range adr
    00089             {
    00090                MSB(temp16)=(*(U8*)src); //load MSB of word from buffer src
    00091                src++; n--;
    00092                if(n) //Still some data to load ?
    00093                {
    00094                   LSB(temp16)=(*(U8*)src); //load LSB of word from buffer src
    00095                   src++; n--;
    00096                }
    00097                else  //only the MSB of the working belong to src buffer
    00098                {     //load LSB form exisying flash
    00099                   LSB(temp16)=flash_rd_byte((U8 farcode*)address+1);
    00100                }
    00101             }
    00102             else  //current word addr out of dst target
    00103             {     //load MSB from existing flash
    00104                MSB(temp16)=flash_rd_byte((U8 farcode*)address);
    00105                if(address+1==dst) // Is LSB word addr in dst range ?
    00106                {
    00107                   LSB(temp16)=(*(U8*)src);
    00108                   src++; n--;
    00109                }
    00110                else // LSB read from existing flash
    00111                {
    00112                   LSB(temp16)=flash_rd_byte((U8 farcode*)address+1);
    00113                }
    00114             }
    00115          }
    00116          else  //complete page with words from existing flash
    00117          {
    00118             temp16=flash_rd_word((Uint16 farcode*)address);
    00119          }
    00120          flash_fill_temp_buffer(temp16,address);
    00121          address+=2;
    00122       }
    00123       address=save_page_adr;
    00124       for(nb_word=0;nb_word<FLASH_PAGE_SIZE/2;nb_word++)
    00125       {
    00126          if(flash_rd_word((Uint16 farcode*)address)!=0xFFFF)
    00127          {
    00128             Flash_page_erase(save_page_adr);
    00129             break;
    00130          }
    00131          address+=2;
    00132       }
    00133       Flash_page_write(save_page_adr);
    00134
    00135       //- First Flash address update for the next page
    00136       address = save_page_adr + FLASH_PAGE_SIZE;
    00137
    00138    }
    00139    return TRUE;
    00140 }
    00141 #pragma location="BOOT"
    00142 void flash_erase(void)
    00143 {
    00144 #if FLASH_SIZE > 0
    00145   U16 nb_page;
    00146
    00147   Enable_flash();
    00148   nb_page=0;
    00149   do {
    00150     Flash_page_erase(nb_page);
    00151     nb_page += FLASH_PAGE_SIZE;
    00152   } while (nb_page<(FLASH_SIZE));
    00153   Disable_flash();
    00154 #endif
    00155
    00156 }
    00157
    00158 Uchar flash_rd_byte(Uchar farcode* addr)
    00159 {
    00160   unsigned char temp;
    00161
    00162   Enable_flash();
    00163   temp = *addr;
    00164   Disable_flash();
    00165   return temp;
    00166 }
    00167
    00168
    00169 Uint16 flash_rd_word(Uint16 farcode* addr)
    00170 {
    00171   Union16 temp;
    00172
    00173   Enable_flash();
    00174   temp.b[1] = flash_rd_byte ((Uchar farcode*) addr);
    00175   temp.b[0] = flash_rd_byte ((Uchar farcode*)addr+1);
    00176   Disable_flash();
    00177
    00178   return temp.w;
    00179 }
    00180
    00181
    00182