/*This file has been prepared for Doxygen automatic documentation generation.*/
/*! \file *********************************************************************
 *
 * \brief Driver routines to read (no write) datas stored in AVRMega flash
 * These routines can be stored and executed in all flash space.
 *
 * - Compiler:           IAR EWAVR for AVR
 * - Supported devices:  All AVRMega devices
 *
 * \author               Atmel Corporation: http://www.atmel.com \n
 *                       Support and FAQ: http://support.atmel.no/
 *
 *****************************************************************************/

/* Copyright (c) 2007, Atmel Corporation All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3. The name of ATMEL may not be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef __IAR_SYSTEMS_ASM__
#error Assembler file supported only by IAR EWAVR for AVR
#endif


//_____ I N C L U D E S ______________________________________________________

#define  ENABLE_BIT_DEFINITIONS
#include <ioavr.h>
 
   NAME  flash_drv(16)

//_____ D E C L A R A T I O N S ______________________________________________

   PUBLIC   flash_read_sig
   PUBLIC   flash_read_fuse
   

//! @{
//! \verbatim

   RSEG     CODE

//! @brief This macro function allows to read device IDs of the product.
//!
//! @param  add   Address of device ID to read.
//!
//! @return byte  Read value (R16)
//!
flash_read_sig:
   RCALL    WAIT_SPMEN                       // Wait for SPMEN flag cleared
   MOV      R31,R17                         
   MOV      R30,R16                          // move adress to z pointer (R31=ZH R30=ZL)
   LDI      R20, ((1<<SPMEN) | (1<<SIGRD))  
   OUT      SPMCSR, R20                      // argument 2 decides function (r18)
   LPM                                       // Store program memory
   MOV      R16, R0                          // Store return value
   RJMP     WAIT_SPMEN                       // Wait for SPMEN flag cleared


//! @brief This macro function allows to read a fuse byte of the product.
//!
//! @param  add   Address of fuse to read.
//! 
//! @return byte  Read value (R16)
//!
flash_read_fuse:
   RCALL    WAIT_SPMEN                       // Wait for SPMEN flag cleared
   MOV      R31,R17                         
   MOV      R30,R16                          // move adress to z pointer (R31=ZH R30=ZL)
   LDI      R20,((1<<SPMEN) | (1<<BLBSET))  
   OUT      SPMCSR, R20                      // argument 2 decides function (r18)
   LPM                                       // Store program memory
   MOV      R16, R0                          // Store return value
   RJMP     WAIT_SPMEN                       // Wait for SPMEN flag cleared


//! @brief Performs an active wait on SPME flag
//!
WAIT_SPMEN:
   MOV      R0, R18
   IN       R18,SPMCSR                       // get SPMCR into r18
   SBRC     R18,SPMEN
   RJMP     WAIT_SPMEN                       // Wait for SPMEN flag cleared
   MOV      R18, R0
   RET

   END

//! \endverbatim
//! @}
