-
Notifications
You must be signed in to change notification settings - Fork 3
/
Memory.c
executable file
·213 lines (180 loc) · 5.61 KB
/
Memory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/********************************************************************
*
* PIC24F Serial Bootloader
*
*********************************************************************
* FileName: memory.c
* Dependencies: boot.c
* Processor: PIC24F Family
* Compiler: C30 v3.00 or later
* Company: Microchip Technology, Inc.
*
* 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.
*
*
* File Description:
*
* Flash program memory read and write functions for use with
* PIC24F Serial Bootloader.
*
* Change History:
*
* Author Revision # Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Brant Ivey 1.00 1-17-2008 Initial release with AN1157
*********************************************************************/
#include <p24fxxxx.h>
#include <GenericTypeDefs.h>
#include "Memory.h"
//Variables for storing runaway code protection keys
#ifdef USE_RUNAWAY_PROTECT
extern volatile WORD writeKey1;
extern volatile WORD writeKey2;
extern volatile WORD keyTest1;
extern volatile WORD keyTest2;
#endif
/********************************************************************
; Function: void WriteMem(WORD cmd)
;
; PreCondition: Appropriate data written to latches with WriteLatch
;
; Input: cmd - type of memory operation to perform
;
; Output: None.
;
; Side Effects:
;
; Overview: Write stored registers to flash memory
;*********************************************************************/
void WriteMem(WORD cmd)
{
NVMCON = cmd;
#ifdef USE_RUNAWAY_PROTECT
writeKey1-=5;
writeKey2+=6;
if(writeKey1 == keyTest1 && writeKey2 == keyTest2){
#endif
__builtin_write_NVM();
while(NVMCONbits.WR == 1);
#ifdef USE_RUNAWAY_PROTECT
}//end if(writeKey1...
keyTest1 = 0x0000;
keyTest2 = 0xAAAA;
#endif
}
/********************************************************************
; Function: void WriteLatch(WORD page, WORD addrLo,
; WORD dataHi, WORD dataLo)
;
; PreCondition: None.
;
; Input: page - upper byte of address
; addrLo - lower word of address
; dataHi - upper byte of data
; addrLo - lower word of data
;
; Output: None.
;
; Side Effects: TBLPAG changed
;
; Overview: Stores data to write in hardware latches
;*********************************************************************/
void WriteLatch(WORD page, WORD addrLo, WORD dataHi, WORD dataLo)
{
TBLPAG = page;
__builtin_tblwtl(addrLo,dataLo);
__builtin_tblwth(addrLo,dataHi);
}
/********************************************************************
; Function: DWORD ReadLatch(WORD page, WORD addrLo)
;
; PreCondition: None.
;
; Input: page - upper byte of address
; addrLo - lower word of address
;
; Output: data - 32-bit data in W1:W0
;
; Side Effects: TBLPAG changed
;
; Overview: Read from location in flash memory
;*********************************************************************/
DWORD ReadLatch(WORD page, WORD addrLo)
{
DWORD_VAL temp;
TBLPAG = page;
temp.word.LW = __builtin_tblrdl(addrLo);
temp.word.HW = __builtin_tblrdh(addrLo);
return temp.Val;
}
/*********************************************************************
; Function: void ResetDevice(WORD addr);
;
; PreCondition: None.
;
; Input: addr - 16-bit address to vector to
;
; Output: None.
;
; Side Effects: None.
;
; Overview: used to vector to user code
;**********************************************************************/
void ResetDevice(WORD addr)
{
asm("goto %0" : : "r"(addr));
}
/********************************************************************
; Function: void Erase(WORD page, WORD addrLo, WORD cmd);
;
; PreCondition: None.
;
; Input: page - upper byte of address
; addrLo - lower word of address
; cmd - type of memory operation to perform
;
; Output: None.
;
; Side Effects: TBLPAG changed
;
; Overview: Erases page of flash memory at input address
*********************************************************************/
void Erase(WORD page, WORD addrLo, WORD cmd)
{
WORD temp;
temp = TBLPAG;
TBLPAG = page;
NVMCON = cmd;
__builtin_tblwtl(addrLo,addrLo);
#ifdef USE_RUNAWAY_PROTECT
writeKey1+=7;
writeKey2+=3;
if(writeKey1 == keyTest1 && writeKey2 == keyTest2){
#endif
__builtin_write_NVM();
while(NVMCONbits.WR == 1);
#ifdef USE_RUNAWAY_PROTECT
}//end if(writekey1...
keyTest1 = 0x0000;
keyTest2 = 0xAAAA;
#endif
TBLPAG = temp;
}