/******************************************************************************* * Copyright (C) 1999-2011 Maxim Integrated Products, All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL MAXIM INTEGRATED PRODUCTS BE LIABLE FOR ANY CLAIM, DAMAGES * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * Except as contained in this notice, the name of Maxim Integrated Products * shall not be used except as stated in the Maxim Integrated Products * Branding Policy. * * The mere transfer of this software does not imply any licenses * of trade secrets, proprietary technology, copyrights, patents, * trademarks, maskwork rights, or any other form of intellectual * property whatsoever. Maxim Integrated Products retains all ownership rights. * * Module Name: DS4830 flash memory read and write assembly routines * Description: Demonstrates flash memory read and write * Filename: assembly.asm * Author: SKJ * Compiler: IAR Compiler version 2.20I * ******************************************************************************* */ NAME flash_asm RSEG CSTACK:DATA:SORT:NOROOT(1) PUBLIC read_block_from_flash PUBLIC write_block_to_flash PUBLIC clear_block_in_flash /******************************************************************************* DS4830 Utility ROM flash write functions address (in Byte mode) mentioned below. ********** !!Notice!! ********** Important Note: Each MAXIM Micro Controller has different address for flash read and write functions. Refer related documentation for correct address. ********************************************************************************/ UROM_flashWrite EQU 10870h UROM_copyBuffer EQU 10936h UROM_flashErasePage EQU 108B6h RSEG CODE:CODE:REORDER:NOROOT(1) /******************************************************************************* * Assembly Function: clear_block_in_flash * * This function calls Ulitilty ROM function to erase one of the 256-word pages * in the main flash block. * * Input: flash address (Byte Mode) in A0 System Register * Output: Status in A0 System Register * 0 if success * non-zero if page erase fails * ********* * Notes: ********* * 1. Interrupts are disabled while in this function. * * 2. The watchdog is not disabled and continues counting down to reset. * User code must handle the watchdog appropriately when calling this * function. (See page erase time in Device Datasheet) ******************************************************************************/ //clear_block_in_flash starts here clear_block_in_flash: //Push registers to stack that are destroyed push PSF push LC[0] push LC[1] push GR push AP push APC push DPC //Set all pointers to word mode move DPC, #01Ch //Set word count to 128 move LC[0], #128 //IAR copies first function parameter to A[0] //Set A[0] as Active -> flash address (Byte Mode Address) move AP, #0 //Shift right to give Word Address (Flash) sr //Call Utility ROM function and Erase page containing A0 flash Address call UROM_flashErasePage //Clear A0 move Acc, #0 //IAR copies function return parameter in A[0]. //Carry is set on error move Acc.0, C clear_exit: //Pop destroyed registers off stack pop DPC pop APC pop AP pop GR pop LC[1] pop LC[0] pop PSF ret //returns A[0] //End clear_block_in_flash RSEG CODE:CODE:REORDER:NOROOT(1) /******************************************************************************* * Assembly Function: write_block_to_flash * * This function calls Ulitilty ROM function to write one word of SRAM data * to flash. * * Input: Flash address (Byte Mode) in A0 System Register * SRAM address (Byte Mode) in A1 System Register * No of words (to write) in A2 System Register * * Output: Status in A0 * 0 if success * non-zero if write fails * ********* * Notes: ********* * 1. This function assumes that the flash has already been erased before * calling this function. * 2. The watchdog is not disabled and continues counting down to reset. * User code must handle the watchdog appropriately when calling this * function. (See word write time in Device Datasheet) * 3. Most of MAXQ devices has Supply Voltage monitoring (SVM) option. It is * recommended that user code should enable SVM with appropriate supply * voltage configuration. * This function check supply voltage health during Flash write operation. * This stops flash write, if it observed SVM Flag is set. ******************************************************************************/ //write_block_to_flash starts here write_block_to_flash: // Supply Voltage monitoring #define SVM M1[9] #define SVMI SVM.3 //Push registers to stack that are destroyed push PSF push LC[0] push LC[1] push GR push AP push APC push DPC //Set all pointers to word mode move DPC, #01Ch //Move Word Count to LC[0] move LC[0], A[2] //No increment move APC, #0 //Set A[1] as active move AP, #1 //Shift right to give word address (SRAM) sr //Move SRAM address (Word Address) to DP move DP[0], A[1] //Set A[0] as active move AP, #0 //Shift right to give word address (Flash) sr write_loop: //Get data from SRAM address move A[1], @DP[0]++ //Move SVM interrupt to carry move C, SVMI //If SVM flag set, exit flash write jump C, write_exit //Call Utility ROM function for word write. call UROM_flashWrite //Carry is set on error jump C, write_exit //Increment Flash Address (A0) add #1 djnz LC[0], write_loop //Set carry to 0 to indicate passing move C, #0 write_exit: //Set A0 as active move AP, #0 //Clear A0 move ACC, #0 //Set to C for error status move ACC.0, C //Pop destroyed registes off stack pop DPC pop APC pop AP pop GR pop LC[1] pop LC[0] pop PSF ret //End write_block_to_flash /******************************************************************************* * Assembly Function: read_block_from_flash * * This function calls Ulitilty ROM function to read a block of memory from * flash to sram variable. * * Input: Flash address (Byte Mode) in A0 System Register * SRAM address (Byte Mode) in A1 System Register * No of words (to read) in A2 System Register * * Output: Status in A0 * 0 if success * non-zero if write fails * ********* * Notes: ********* * 1. The watchdog is not disabled and continues counting down to reset. User code must handle the watchdog appropriately when calling this function. (See page erase time in Device Datasheet) ******************************************************************************/ RSEG CODE:CODE:REORDER:NOROOT(1) //read_block_from_flash starts here read_block_from_flash: //Push registers onto stack that will be destroyed push DPC push BP push OFFS push DP[0] push LC[0] push APC push AP push PSF //Set all pointers to word mode move DPC, #01Ch //No increment move APC, #0 //Set to A1, Aram Address move AP, #1 //Shift right to give word mode address (SRAM Address) sr //Move sram address to BP move BP, A[1] //Set offset to 0 move OFFS, #0 //Set to A0, flash address move AP, #0 //Shift right to give word address (Flash Address) sr //Add to Address flash add #08000h //Move flash address to DP0 move DP[0], A[0] //Move number of words to loop count move LC[0], A[2] /************************************************************************* Call Utilty ROM Function to call to read data from flash Inputs DP[0] – Location to read from in data space (include 8000h offset if reading from program memory). BP[Offs] – Location to write to in data space. LC[0] – Number of bytes or words to copy (depending on mode). *************************************************************************/ call UROM_copyBuffer //Pop registers off stack pop PSF pop AP pop APC pop LC[0] pop DP[0] pop OFFS pop BP pop DPC ret //End read_block_from_flash