subtitle "flsh_lib.asm" ;;; ********************************************************************* ;;; Library Name: flsh_lib.asm ;;; Purpose: Routines for reading and writing flash (program) ;;; memory. Modularized from code for AN732. ;;; Revision: 0.1 ;;; Date: 03 October 2005 ;;; Author: See below. Modularized by L. Wyard-Scott. ;;; Copyright: See below. ;;; Device: PIC16F873, PIC16F874, PIC16F876, PIC16F877. ;;; PIC16F873A, PIC16F874A, PIC16F876A, PIC16F877A. ;;; ********************************************************************* ;;; (start copyright) ;;;====================================================================== ;;; Software License Agreement ;;; ;;; The software supplied herewith by Microchip Technology Incorporated ;;; (the "Company") for its PICmicro® Microcontroller is intended and ;;; supplied to you, the Company’s customer, for use solely and ;;; exclusively on Microchip PICmicro Microcontroller products. The ;;; software is owned by the Company and/or its supplier, and is ;;; protected under applicable copyright laws. All rights are reserved. ;;; Any use in violation of the foregoing restrictions may subject the ;;; user to criminal sanctions under applicable laws, as well as to ;;; civil liability for the breach of the terms and conditions of this ;;; license. ;;; ;;; THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES, ;;; WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED ;;; TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A ;;; PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, ;;; IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR ;;; CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. ;;; ;;;====================================================================== ;;; Filename: boot877.asm ;;;====================================================================== ;;; Author: Mike Garbutt ;;; Company: Microchip Technology Inc. ;;; Revision: 1.00 ;;; Date: 26 June 2000 ;;; Assembled using MPASM V2.40 ;;;====================================================================== ;;; (end copyright) ;;; ********************************************************************* ;;; Special Directions: ;;; To use these routines, include "flsh_lib.h" in the source code ;;; calling the routines. This file contains information about the ;;; routines and other equates that the calling code requires. ;;; ********************************************************************* ;;; Revision History: ;;; 0.1 - 03 October 2005 ;;; - Added support for 16F87XA devices. ;;; 0.0 - 13 November 2002 ;;; - Creation. Made modular. ;;; ********************************************************************* ;;; Ideas for future work: ;;; ;;; ********************************************************************* list ; Turn on list output. Note that device ; type needs to be specified to the ; assembler (on the command line, or through ; the MPLAB IDE). ;; Include register, bit, and other info specific to ;; the specified device. ifdef __16F873 #include messg "Assembling flsh_lib.asm for PIC16F873." constant EEADRHMASK = 0x0F endif ifdef __16F874 #include messg "Assembling flsh_lib.asm for PIC16F874." constant EEADRHMASK = 0x0F endif ifdef __16F876 #include messg "Assembling flsh_lib.asm for PIC16F876." constant EEADRHMASK = 0x1F endif ifdef __16F877 #include messg "Assembling flsh_lib.asm for PIC16F877." constant EEADRHMASK = 0x1F endif ifdef __16F873A #define A_Device #include messg "Assembling flsh_lib.asm for PIC16F873A." constant EEADRHMASK = 0x0F endif ifdef __16F874A #define A_Device #include messg "Assembling flsh_lib.asm for PIC16F874A." constant EEADRHMASK = 0x0F endif ifdef __16F876A #define A_Device #include messg "Assembling flsh_lib.asm for PIC16F876A." constant EEADRHMASK = 0x1F endif ifdef __16F877A #define A_Device #include messg "Assembling flsh_lib.asm for PIC16F877A." constant EEADRHMASK = 0x1F endif ;;; -------------------------------------------------------------------- ;;; Assembler Equates Section. Define assembly-time constants here ;;; using the EQU assembler directive. ;;; -------------------------------------------------------------------- ;;; --------------------------------------------------------------------- ;;; Variable Address Assignments. ;;; --------------------------------------------------------------------- UDATA ; Start of the uninitialized data section. ; The following statements reserve memory, ; the address of which is allocated by the ; linker. ;;; -------------------------------------------------------------------- ;;; Macros. (File-specific) ;;; -------------------------------------------------------------------- ;;; -------------------------------------------------------------------- ;;; Subroutines. ;;; -------------------------------------------------------------------- CODE ; Start the code section. ;;; ******************************************************************** ;;; Subroutine Name: FLASH_Read ;;; Description: Reads a word from the flash (program) memory. ;;; Requires: Address to read from in EEADRH:EEADR. ;;; Returns: The program word in EEDATH:EEDATA. ;;; Locations Affected: W is destroyed. ;;; ******************************************************************** FLASH_Read: global FLASH_Read banksel EEADRH ;; Mask the high-byte of the address to keep within ;; range, determined by the device type. movlw EEADRHMASK andwf EEADRH,F banksel EECON1 ; Change from bank2 to bank3. movlw 0x80 ; Enable reads from program flash. movwf EECON1 bsf EECON1,RD ; Read from flash. nop ; Processor waits while reading. nop ;; The program word is now located in EEDATH:EEDATA. return ;;; ******************************************************************** ;;; Subroutine Name: FLASH_Write ;;; Description: Writes a word to the flash (program memory). ;;; Requires: Address to write to in EEADRH:EEADR. ;;; Program word to write in EEDATH:EEDATA ;;; NOTE: for 16F87XA devices, writes need ;;; to occur in blocks of 4 addresses, starting ;;; with A1=A0=0 (aligned). i.e. Four calls ;;; to this routine (or FLASH_Read) are required for ;;; an effective write. See the 16F87XA datasheet ;;; for more information. ;;; Returns: Nothing. ;;; Locations Affected: W is destroyed. ;;; ******************************************************************** FLASH_Write: global FLASH_Write banksel EECON1 ; Change to bank3. movlw 0x84 ; Enable writes to program flash. movwf EECON1 ;; The following is a required technique that is aimed ;; at preventing accidental modification of program memory. movlw 0x55 ; Do timed access writes. movwf EECON2 movlw 0xaa movwf EECON2 bsf EECON1,WR ; Begin writing to flash. nop ; Processor halts here while writing. nop ;; The program word has been written. return ;;; -------------------------------------------------------------------- ;;; Constant Data (if not placed along with the subroutines). ;;; -------------------------------------------------------------------- END