3
|
1 |
/*
|
|
2 |
canfestival@canopencanada.ca
|
|
3 |
|
|
4 |
See COPYING file for copyrights details.
|
|
5 |
|
|
6 |
This library is free software; you can redistribute it and/or
|
|
7 |
modify it under the terms of the GNU Lesser General Public
|
|
8 |
License as published by the Free Software Foundation; either
|
|
9 |
version 2.1 of the License, or (at your option) any later version.
|
|
10 |
|
|
11 |
This library is distributed in the hope that it will be useful,
|
|
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 |
Lesser General Public License for more details.
|
|
15 |
|
|
16 |
You should have received a copy of the GNU Lesser General Public
|
|
17 |
License along with this library; if not, write to the Free Software
|
|
18 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
19 |
|
|
20 |
flash.c
|
|
21 |
save to / retrieve from the non-volatile memory
|
|
22 |
to be tested
|
|
23 |
- can we write/read into an address without working with the whole page (256bytes)
|
|
24 |
*/
|
|
25 |
|
|
26 |
|
|
27 |
#include <stdio.h>
|
|
28 |
#include <stdlib.h>
|
|
29 |
|
|
30 |
#include "applicfg.h"
|
|
31 |
#include "data.h"
|
|
32 |
#include "objdictdef.h"
|
|
33 |
|
|
34 |
#include "lpc2138_defs.h" /* LPC21xx definitions */
|
|
35 |
|
|
36 |
#define IAP_LOCATION 0x7ffffff1
|
|
37 |
|
|
38 |
|
|
39 |
// define a page of data of NVRAM_BLOCK_SIZE bytes
|
|
40 |
//
|
|
41 |
short data_len; /* 0 to NVRAM_BLOCK_SIZE bytes */
|
|
42 |
short data_num_pages;
|
|
43 |
unsigned int *data_page = NULL;
|
|
44 |
unsigned int data_addr;
|
|
45 |
|
|
46 |
unsigned int *regs_page = NULL;
|
|
47 |
|
|
48 |
// local definitons
|
|
49 |
void ee_erase(unsigned int ,unsigned int[]); //function erases EEPROM
|
|
50 |
void ee_write_page(unsigned int); //function adds a record in EEPROM
|
|
51 |
void ee_read_page(unsigned int); //function reads the latest valid record in EEPROM
|
|
52 |
|
|
53 |
typedef void (*IAP)(unsigned int [],unsigned int[]);
|
|
54 |
IAP iap_entry;
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
/************************************************************************/
|
|
59 |
/* */
|
|
60 |
/* function: */
|
|
61 |
/* void ee_erase(unsigned int command_ee,unsigned int result_ee[]) */
|
|
62 |
/* */
|
|
63 |
/* type: void */
|
|
64 |
/* */
|
|
65 |
/* parameters: */
|
|
66 |
/* command_ee - Not used. */
|
|
67 |
/* result_ee[0] - Returns a response to the last IAP command used. */
|
|
68 |
/* 0 - EEPROM successfully erased. */
|
|
69 |
/* For all other response values, see microcontroller */
|
|
70 |
/* User Manual, IAP Commands and Status Codes Summary. */
|
|
71 |
/* result_ee[1] - Not used. */
|
|
72 |
/* */
|
|
73 |
/* version: 1.1 (01/27/2006) */
|
|
74 |
/* */
|
|
75 |
/* constants defined in LPC2k_ee.h used in this function: */
|
|
76 |
/* EE_SEC_L - microcontroller's Flash sector where EEPROM begins */
|
|
77 |
/* EE_SEC_H - microcontroller's Flash sector where EEPROM ends */
|
|
78 |
/* EE_CCLK - microcontroller's system clock (cclk) */
|
|
79 |
/* */
|
|
80 |
/* description: */
|
|
81 |
/* This function erases LPC2000 on-chip Flash sectors selected to act */
|
|
82 |
/* as an EEPROM. All Flash sectors between EE_SEC_L abd EE_SEC_H */
|
|
83 |
/* (including these sectors) will be erased using the In Application */
|
|
84 |
/* Programming (IAP) routines (see User Manual for more details). */
|
|
85 |
/* Also, this function disables all interrupts while erasing the */
|
|
86 |
/* EEPROM. If this is not needed, three lines of the ee_erase */
|
|
87 |
/* subroutine can simply be commented-out without affecting the */
|
|
88 |
/* routine performance at all. */
|
|
89 |
/* */
|
|
90 |
/* revision history: */
|
|
91 |
/* - Rev. 1.1 adds interrupt disable feature. */
|
|
92 |
/* */
|
|
93 |
/************************************************************************/
|
|
94 |
void ee_erase(unsigned int command_ee,unsigned int result_ee[])
|
|
95 |
{
|
|
96 |
unsigned int command_iap[5];
|
|
97 |
unsigned int result_iap[3];
|
|
98 |
unsigned long int enabled_interrupts;
|
|
99 |
|
|
100 |
enabled_interrupts = VICIntEnable; //disable all interrupts
|
|
101 |
VICIntEnClr = enabled_interrupts;
|
|
102 |
|
|
103 |
command_iap[0]=50; // prepare sectors from EE_SEC_L to EE_SEC_H for erase
|
|
104 |
command_iap[1]=EE_SEC_L;
|
|
105 |
command_iap[2]=EE_SEC_H;
|
|
106 |
iap_entry=(IAP) IAP_LOCATION;
|
|
107 |
iap_entry(command_iap,result_iap);
|
|
108 |
|
|
109 |
command_iap[0]=52; // erase sectors from EE_SEC_L to EE_SEC_H
|
|
110 |
command_iap[1]=EE_SEC_L;
|
|
111 |
command_iap[2]=EE_SEC_H;
|
|
112 |
command_iap[3]=EE_CCLK;
|
|
113 |
iap_entry=(IAP) IAP_LOCATION;
|
|
114 |
iap_entry(command_iap,result_iap);
|
|
115 |
|
|
116 |
command_iap[0]=53; // blankcheck sectors from EE_SEC_L to EE_SEC_H
|
|
117 |
command_iap[1]=EE_SEC_L;
|
|
118 |
command_iap[2]=EE_SEC_H;
|
|
119 |
iap_entry=(IAP) IAP_LOCATION;
|
|
120 |
iap_entry(command_iap,result_iap);
|
|
121 |
|
|
122 |
VICIntEnable = enabled_interrupts; //restore interrupt enable register
|
|
123 |
|
|
124 |
result_ee[0]=result_iap[0];
|
|
125 |
return;
|
|
126 |
}
|
|
127 |
|
|
128 |
/************************************************************************/
|
|
129 |
/* */
|
|
130 |
/* function: */
|
|
131 |
/* void ee_write(unsigned int command_ee,unsigned int result_ee[]) */
|
|
132 |
/* */
|
|
133 |
/* type: void */
|
|
134 |
/* */
|
|
135 |
/* parameters: */
|
|
136 |
/* command_ee - An address of a content of ee_data type that has */
|
|
137 |
/* to be programmed into EEPROM. */
|
|
138 |
/* result_ee[0] - Returns a response to the last IAP command used. */
|
|
139 |
/* 0 - data successfully programmed in EEPROM. */
|
|
140 |
/* 501 - no space in EEPROM to program data. */
|
|
141 |
/* For all other response values, see microcontroller */
|
|
142 |
/* User Manual, IAP Commands and Status Codes Summary. */
|
|
143 |
/* result_ee[1] - Not used. */
|
|
144 |
/* */
|
|
145 |
/* version: 1.1 (01/27/2006) */
|
|
146 |
/* */
|
|
147 |
/* constants defined in LPC2k_ee.h used in this function: */
|
|
148 |
/* EE_BUFFER_SIZE - IAP buffer size; must be 256 or 512 */
|
|
149 |
/* NO_SPACE_IN_EEPROM - EEPROM is full and no data can be programmed */
|
|
150 |
/* EE_BUFFER_MASK - parameter used for interfacing with IAP */
|
|
151 |
/* EE_REC_SIZE - ee_data structure size in bytes */
|
|
152 |
/* EE_SEC_L - micro's Flash sector where EEPROM begins */
|
|
153 |
/* EE_SEC_H - micro's Flash sector where EEPROM ends */
|
|
154 |
/* EE_CCLK - micro's system clock (cclk) */
|
|
155 |
/* */
|
|
156 |
/* description: */
|
|
157 |
/* This function writes a single structure of ee_data type into the */
|
|
158 |
/* EEPROM using an In Application Programming (IAP) routines (see */
|
|
159 |
/* User Manual for more details). command_ee contains an address of */
|
|
160 |
/* this structure. EEPROM is scanned for the last (if any) record */
|
|
161 |
/* identifier (EE_REC_ID), and a new record is added next to it. */
|
|
162 |
/* Also, this function disables all interrupts while erasing the */
|
|
163 |
/* EEPROM. If this is not needed, three lines of the ee_write */
|
|
164 |
/* subroutine can simply be commented-out without affecting the */
|
|
165 |
/* routine performance at all. */
|
|
166 |
/* */
|
|
167 |
/* revision history: */
|
|
168 |
/* - Rev. 1.1 fixes a bug related to verifying a content written into */
|
|
169 |
/* the EEPROM. 1.0 was reporting missmatch even when there were no */
|
|
170 |
/* problems at all. */
|
|
171 |
/* Rev. 1.1 adds interrupt disable feature. */
|
|
172 |
/* */
|
|
173 |
/************************************************************************/
|
|
174 |
|
|
175 |
void ee_write_page(unsigned int addr)
|
|
176 |
{
|
|
177 |
unsigned long int enabled_interrupts;
|
|
178 |
// unsigned char ee_buffer[16];
|
|
179 |
unsigned int command_iap[5], result_iap[3];
|
|
180 |
|
|
181 |
enabled_interrupts = VICIntEnable; //disable all interrupts
|
|
182 |
VICIntEnClr = enabled_interrupts;
|
|
183 |
|
|
184 |
iap_entry = (IAP) IAP_LOCATION;
|
|
185 |
|
|
186 |
// prepare sectors from EE_SEC_L to EE_SEC_H for erase
|
|
187 |
command_iap[0] = 50;
|
|
188 |
command_iap[1] = EE_SEC_L;
|
|
189 |
command_iap[2] = EE_SEC_H;
|
|
190 |
iap_entry(command_iap, result_iap);
|
|
191 |
|
|
192 |
// copy RAM to flash/eeprom
|
|
193 |
command_iap[0] = 51;
|
|
194 |
command_iap[1] = (unsigned int) (addr & EE_START_MASK); // 256 kb boundary
|
|
195 |
command_iap[2] = (unsigned int) (data_page); // should be on a word boundary
|
|
196 |
command_iap[3] = 256;
|
|
197 |
command_iap[4] = EE_CCLK;
|
|
198 |
iap_entry(command_iap, result_iap);
|
|
199 |
|
|
200 |
#if 0
|
|
201 |
// compare RAM and flash/eeprom
|
|
202 |
command_iap[0] = 56;
|
|
203 |
command_iap[1] = (unsigned int) data;
|
|
204 |
command_iap[2] = addr;
|
|
205 |
command_iap[3] = dlen;
|
|
206 |
iap_entry(command_iap, result_iap);
|
|
207 |
#endif
|
|
208 |
|
|
209 |
VICIntEnable = enabled_interrupts; //restore interrupt enable register
|
|
210 |
}
|
|
211 |
|
|
212 |
|
|
213 |
/************************************************************************/
|
|
214 |
/* */
|
|
215 |
/* function: */
|
|
216 |
/* void ee_read(unsigned int command_ee,unsigned int result_ee[]) */
|
|
217 |
/* */
|
|
218 |
/* type: void */
|
|
219 |
/* */
|
|
220 |
/* parameters: */
|
|
221 |
/* command_ee - Not used. */
|
|
222 |
/* result_ee[0] - Returns a response. */
|
|
223 |
/* 0 - data successfully found in EEPROM. */
|
|
224 |
/* 500 - no data/records available in EEPROM. */
|
|
225 |
/* result_ee[1] - an address of the last record of ee_data type */
|
|
226 |
/* in EEPROM. */
|
|
227 |
/* */
|
|
228 |
/* version: 1.1 (01/27/2006) */
|
|
229 |
/* */
|
|
230 |
/* constants defined in LPC2k_ee.h used in this function: */
|
|
231 |
/* NO_RECORDS_AVAILABLE - EEPROM is empty/no records identifiable */
|
|
232 |
/* with a record identifier (EE_REC_ID) found */
|
|
233 |
/* EE_ADR_L - micro's Flash address from where EEPROM begins */
|
|
234 |
/* EE_REC_SIZE - size (in bytes) of a ee_data structure */
|
|
235 |
/* */
|
|
236 |
/* description: */
|
|
237 |
/* This function scans an EEPROM content looking for the last record */
|
|
238 |
/* that can be identified with a record identifier (EE_REC_ID). When */
|
|
239 |
/* such data is found, its address is passed as result_ee[1]. */
|
|
240 |
/* */
|
|
241 |
/* revision history: */
|
|
242 |
/* - Rev. 1.0 had problems with accessing the last record in a fully */
|
|
243 |
/* occupied EEPROM. Rev. 1.1 fixes this. */
|
|
244 |
/* */
|
|
245 |
/************************************************************************/
|
|
246 |
void ee_read_page(unsigned int addr)
|
|
247 |
{
|
|
248 |
memcpy(data_page, (unsigned int *)addr, sizeof(unsigned int)*64);
|
|
249 |
}
|
|
250 |
|
|
251 |
|
|
252 |
//////////////////////////////////////////////////////////////////////////////////////////
|
|
253 |
//////////////////////////////////////////////////////////////////////////////////////////
|
|
254 |
|
|
255 |
|
|
256 |
/*
|
|
257 |
CAN FESTIVAL interface functions
|
|
258 |
*/
|
|
259 |
|
|
260 |
|
|
261 |
int _get_data_len(int type)
|
|
262 |
{
|
|
263 |
int len = 0; /* number of bytes */
|
|
264 |
switch(type)
|
|
265 |
{
|
|
266 |
case boolean:
|
|
267 |
len = 1;
|
|
268 |
break;
|
|
269 |
|
|
270 |
case int8:
|
|
271 |
case uint8:
|
|
272 |
len = 1;
|
|
273 |
break;
|
|
274 |
case int16:
|
|
275 |
case uint16:
|
|
276 |
len = 2;
|
|
277 |
break;
|
|
278 |
case int24:
|
|
279 |
case uint24:
|
|
280 |
len = 3;
|
|
281 |
break;
|
|
282 |
case int32:
|
|
283 |
case uint32:
|
|
284 |
case real32:
|
|
285 |
len = 4;
|
|
286 |
break;
|
|
287 |
case int40:
|
|
288 |
case uint40:
|
|
289 |
len = 5;
|
|
290 |
break;
|
|
291 |
case int48:
|
|
292 |
case uint48:
|
|
293 |
len = 6;
|
|
294 |
break;
|
|
295 |
case int56:
|
|
296 |
case uint56:
|
|
297 |
len = 7;
|
|
298 |
break;
|
|
299 |
case int64:
|
|
300 |
case uint64:
|
|
301 |
case real64:
|
|
302 |
len = 8;
|
|
303 |
break;
|
|
304 |
#if 0
|
|
305 |
/* TO DO */
|
|
306 |
case visible_string:
|
|
307 |
case octet_string:
|
|
308 |
case unicode_string:
|
|
309 |
case time_of_day:
|
|
310 |
case time_difference:
|
|
311 |
#endif
|
|
312 |
}
|
|
313 |
|
|
314 |
return len;
|
|
315 |
}
|
|
316 |
|
|
317 |
|
|
318 |
|