Sei sulla pagina 1di 6

C:\Documents and Settings\RABBIT\Escritorio\PruebaMemoriaImprimirArreglo\rtsp_api.

martes, 16 de junio de 2015 12:19

/*******************************************************************************
Run Time Self Programming API
Company:
Microchip Technology Inc.
File Name:
rtsp_api.s
Summary:
This file has flash routines
Description:
This file consists of Flash Erase, Flash Read and Flash Page modify
and Flash Programming routines that are called by the main function which are
used to erase a page, read a page, modify a page and program a row of flash memory
respectively.
*******************************************************************************/
/*******************************************************************************
Copyright (c) 2012 released Microchip Technology Inc. All rights reserved.
Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).
You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.
SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*******************************************************************************/
/*****************************************************************************
*****************************************************************************
Section: Included Files
*****************************************************************************
*****************************************************************************/
.include "xc.inc"
/*****************************************************************************
*****************************************************************************
Section: File Scope or Global Constants
*****************************************************************************
-1-

C:\Documents and Settings\RABBIT\Escritorio\PruebaMemoriaImprimirArreglo\rtsp_api.s

martes, 16 de junio de 2015 12:19

*****************************************************************************/
.equ
.equ
.equ
.global
.global
.global
.global
.global
.global

FLASH_PAGE_ERASE_CODE, 0x4003
FLASH_ROW_PROG_CODE, 0x4002
FLASH_WORD_PROG_CODE, 0x4001
_FlashPageRead
_FlashPageErase
_FlashPageWrite
_FlashPageModify
_FlashWordModify
_FlashWordRead

.section .text
/******************************************************************************
Flash Page Read
Read EIGHT rows (PAGE) of memory, upper PM byte is discarded
*******************************************************************************/
_FlashPageRead:
push
TBLPAG
mov
w0, TBLPAG
mov
#1024, w3
readNext:
tblrdl [w1],[w2++]
tblrdh [w1++],w6 ; Discard PM upper byte
dec
w3, w3
bra
nz, readNext
clr w0
pop
TBLPAG
return

/******************************************************************************
Flash Page Modify
Read EIGHT rows (PAGE) of memory, upper PM byte is discarded
*******************************************************************************/
_FlashPageModify:
sl
w0,#7,w0
add w0,w3,w3
modifyNext:
mov
dec
bra

[w2++],[w3++] ; Discard PM upper byte


w1, w1
nz, modifyNext

return
/******************************************************************************
Flash Page Erase
Erase EIGHT rows (PAGE) of memory
-2-

C:\Documents and Settings\RABBIT\Escritorio\PruebaMemoriaImprimirArreglo\rtsp_api.s

martes, 16 de junio de 2015 12:19

*******************************************************************************/
_FlashPageErase:
push
TBLPAG
mov
w0, TBLPAG ; Init Pointer to page to be erased
mov
w0,NVMADRU ; Init Pointer to row to be erased
mov w1,NVMADR
tblwtl w1, [w1]
mov
mov
bset
disi
mov
mov
mov
mov
mov
nop
nop

#FLASH_PAGE_ERASE_CODE,w7
w7, NVMCON
w7,#WR;NVMCON,#15
#5
; Block all interrupt with priority <7 for next 5 instructions
#0x55, W0
W0, NVMKEY
#0xAA, W0
W0, NVMKEY
w7,NVMCON ; Start Program Operation

erase_wait:
btsc
NVMCON, #WR
bra
erase_wait
clr

w0

pop
return

TBLPAG

nop
/******************************************************************************
Flash Page Program
Program EIGHT rows (PAGE) of memory,
Each row contains 192 bytes of data (64 instructions, with upper PM byte == NOP)
*******************************************************************************/

_FlashPageWrite:
push
TBLPAG
MOV W0, W9
MOV w1, w8
mov #0xFA, w0
mov w0, TBLPAG
mov #0, w1
mov #0,w6
mov #7,w5
row_loop:
mov #64,w3
mov #0, w1
pinst_loop:

; Add loc to be written

;TBLPAG has to point to the SRAM latch section

; w5=row counter

;clear w1 - address is relative to the row start address


-3-

C:\Documents and Settings\RABBIT\Escritorio\PruebaMemoriaImprimirArreglo\rtsp_api.s

TBLWTL.b [w2++], [w1]


TBLWTH.b w6, [w1++] ; write 0 to high byte to be decoded as nop
TBLWTL.b
[w2++], [w1++]
TBLWTL.b [w2++], [w1]
TBLWTH.b w6, [w1++] ; write 0 to high byte to be decoded as nop
TBLWTL.b
[w2++], [w1++]
dec2
dec2
;
;

dec
bra
MOV
MOV

w1,w1
w1,w1
w3, w3
NZ, pinst_loop

w9, NVMADRU
w8, NVMADR

;point to the row to be written to

; Setup NVCON for row program


mov
#FLASH_WORD_PROG_CODE,w7
mov
w7, NVMCON
bset
w7,#15
disi #5
; Block all interrupt with priority <7 for next 5 instructions
mov
#0x55, W0
mov
W0, NVMKEY
mov
#0xAA, W0
mov
W0, NVMKEY

mov
w7,NVMCON ; Start Program Operation
nop
nop
add w8,#4,w8
prog_wait:
btsc
NVMCON, #WR
bra
prog_wait
dec
w3, w3
bra
NZ, pinst_loop

;
;
;
;

mov
mov
add
mov
dec
bra

#256,w1
w8,w0
;
w1,w0,w0
w0,w8
;

; load with row size


load previous row address
; compute new row address
load new row address in w8

w5, w5
nz, row_loop ; test if all rows are written

clr w0
pop
TBLPAG
return
_FlashWordModify:
-4-

martes, 16 de junio de 2015 12:19

C:\Documents and Settings\RABBIT\Escritorio\PruebaMemoriaImprimirArreglo\rtsp_api.s

push
sl
add
mov
mov
add
MOV
MOV

TBLPAG
w0,#7,w0
w0,w3,w3
#0xFA, w0
w0, TBLPAG
w1,w3,w3
W2, W9; Add loc to be written
w3, w8

mov #0, w3
mov #0, w6
; Copy the bytes to the latches.
TBLWTL.b [w4++], [w3]
TBLWTH.b w6, [w3++] ; write 0 to high byte to be decoded as nop
TBLWTL.b
[w4++], [w3++]
TBLWTL.b [w4++], [w3]
TBLWTH.b w6, [w3++] ; write 0 to high byte to be decoded as nop
TBLWTL.b
[w4++], [w3++]
MOV w9, NVMADRU
MOV w8, NVMADR
; Setup NVCON for word program
mov
#FLASH_WORD_PROG_CODE,w7
MOV W7, NVMCON
bset w7,#15
disi #5
MOV
MOV
MOV
MOV

#0x55, W0
W0, NVMKEY
#0xAA, W0
W0, NVMKEY

mov
w7,NVMCON
; Start Program Operation
;bset w7,#WR;NVMCON, #WR
nop
nop
wait:
btsc NVMCON, #WR
bra wait
clr W0
pop TBLPAG
return
_FlashWordRead:
push TBLPAG
sl
w0,#7,w0
add
w0,w3,w3
-5-

martes, 16 de junio de 2015 12:19

C:\Documents and Settings\RABBIT\Escritorio\PruebaMemoriaImprimirArreglo\rtsp_api.s

add
mov
mov

w1,w3,w3
w2, TBLPAG
#2, w5

readloop:
tblrdl.b [w3], [w4++]
tblrdh.b [w3++], [w4++]
tblrdl.b [w3++], [w4]
dec w5, w5
bra nz, readloop
clr w0
pop TBLPAG
return
.end

-6-

martes, 16 de junio de 2015 12:19

Potrebbero piacerti anche