AlcapDAQ  1
flash.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <malloc.h>
4 
5 #include "flash.h"
6 #include "CAENComm.h"
7 
8 static int GetFlashStatus32(cvFlashAccess *Flash, uint8_t *Status)
9 {
10  int res = 0;
11  uint32_t reg32;
12 
13 
14  // Enable flash
15  res |= CAENComm_Write32(Flash->Handle,Flash->Sel_Flash,Flash->FlashEnable);
16  // Status read Command
17  res |= CAENComm_Write32(Flash->Handle, Flash->RW_Flash, STATUS_READ_CMD);
18  // Status read
19  res |= CAENComm_Read32(Flash->Handle, Flash->RW_Flash, &reg32);
20  // Disable flash
21  res |= CAENComm_Write32(Flash->Handle, Flash->Sel_Flash, !Flash->FlashEnable);
22  *Status = (uint8_t)(reg32 & 0xFF);
23 
24  return res;
25 
26 }
27 
28 static int GetFlashStatus16(cvFlashAccess *Flash, uint8_t *Status)
29 {
30  int res = 0;
31  uint16_t reg16;
32 
33 
34  // Enable flash
35  res |= CAENComm_Write16(Flash->Handle,Flash->Sel_Flash,Flash->FlashEnable);
36  // Status read Command
37  res |= CAENComm_Write16(Flash->Handle, Flash->RW_Flash, STATUS_READ_CMD);
38  // Status read
39  res |= CAENComm_Read16(Flash->Handle, Flash->RW_Flash, &reg16);
40  // Disable flash
41  res |= CAENComm_Write16(Flash->Handle, Flash->Sel_Flash, !Flash->FlashEnable);
42  *Status = (uint8_t)(reg16 & 0xFF);
43 
44  return res;
45 
46 }
47 
48 // *****************************************************************************
49 // GetFlashStatus: read the status register of the flash
50 // Parameters: Status: pointer to the Status variable
51 // Return: 0 on Success, < 0 on error
52 // *****************************************************************************
53 static int GetFlashStatus(cvFlashAccess *Flash, uint8_t *Status) {
54 
55  int ret;
56 
57  if (Flash->RegSize == 4)
58  ret = GetFlashStatus32(Flash, Status);
59  else if (Flash->RegSize == 2)
60  ret = GetFlashStatus16(Flash, Status);
61  else
62  ret = CvUpgrade_FileAccessError; /* Unsupported RegSize */
63 
64  return ret;
65 
66 }
67 
68 
69 static int WriteFlashPage32(cvFlashAccess *Flash, uint8_t *data, int pagenum)
70 {
71  unsigned int i;
72  int res = 0;
73  uint8_t stat;
74  uint32_t flash_addr;
75 
76  int wcnt = 0;
77 
78  uint32_t Wbuff[MAX_MULTIACCESS_BUFSIZE]; // Buffers for the Multi Read/Write
79  uint32_t Addrs[MAX_MULTIACCESS_BUFSIZE];
81 
82  if (Flash->PageSize == 264) // 4 and 8 Mbit
83  flash_addr = (uint32_t)pagenum << 9;
84  else if (Flash->PageSize == 528) // 16 and 32 Mbit
85  flash_addr = (uint32_t)pagenum << 10;
86  else // 64 Mbit
87  flash_addr = (uint32_t)pagenum << 11;
88 
89  // Enable flash
90  res |= CAENComm_Write32(Flash->Handle, Flash->Sel_Flash, Flash->FlashEnable);
91 
92  // Opcode
93  Wbuff[wcnt] = MAIN_MEM_PAGE_PROG_TH_BUF1_CMD;
94  Addrs[wcnt] = Flash->RW_Flash;
95  ++wcnt;
96  // Page address
97  Wbuff[wcnt] = (flash_addr>>16) & 0xFF;
98  Addrs[wcnt] = Flash->RW_Flash;
99  ++wcnt;
100  Wbuff[wcnt] = (flash_addr>>8) & 0xFF;
101  Addrs[wcnt] = Flash->RW_Flash;
102  ++wcnt;
103  Wbuff[wcnt] = flash_addr & 0xFF;
104  Addrs[wcnt] = Flash->RW_Flash;
105  ++wcnt;
106 
107  // Fill buffer
108  for (i=0; i<Flash->PageSize; i++) {
109  Addrs[wcnt] = Flash->RW_Flash;
110  Wbuff[wcnt] = (uint32_t)data[i];
111  ++wcnt;
112  }
113 
114  // Write Flash Page
115  res |= CAENComm_MultiWrite32(Flash->Handle, Addrs, wcnt, Wbuff, ECs);
116 
117  // Disable flash
118  res |= CAENComm_Write32(Flash->Handle, Flash->Sel_Flash, !Flash->FlashEnable);
119 
120  // wait for Tep (Time of erase and programming)
121  do
122  res |= GetFlashStatus(Flash, &stat);
123  while (!(stat & 0x80));
124 
125  return res;
126 }
127 
128 static int WriteFlashPage16(cvFlashAccess *Flash, uint8_t *data, int pagenum)
129 {
130  unsigned int i;
131  int res = 0;
132  uint8_t stat;
133  uint32_t flash_addr;
134 
135  int wcnt = 0;
136 
137  uint16_t Wbuff[MAX_MULTIACCESS_BUFSIZE]; // Buffers for the Multi Read/Write
138  uint32_t Addrs[MAX_MULTIACCESS_BUFSIZE];
140 
141  if (Flash->PageSize == 264) // 4 and 8 Mbit
142  flash_addr = (uint32_t)pagenum << 9;
143  else if (Flash->PageSize == 528) // 16 and 32 Mbit
144  flash_addr = (uint32_t)pagenum << 10;
145  else // 64 Mbit
146  flash_addr = (uint32_t)pagenum << 11;
147 
148  // Enable flash
149  res |= CAENComm_Write16(Flash->Handle, Flash->Sel_Flash, Flash->FlashEnable);
150 
151  // Opcode
152  Wbuff[wcnt] = MAIN_MEM_PAGE_PROG_TH_BUF1_CMD;
153  Addrs[wcnt] = Flash->RW_Flash;
154  ++wcnt;
155  // Page address
156  Wbuff[wcnt] = (flash_addr>>16) & 0xFF;
157  Addrs[wcnt] = Flash->RW_Flash;
158  ++wcnt;
159  Wbuff[wcnt] = (flash_addr>>8) & 0xFF;
160  Addrs[wcnt] = Flash->RW_Flash;
161  ++wcnt;
162  Wbuff[wcnt] = flash_addr & 0xFF;
163  Addrs[wcnt] = Flash->RW_Flash;
164  ++wcnt;
165 
166  // Fill buffer
167  for (i=0; i<Flash->PageSize; i++) {
168  Addrs[wcnt] = Flash->RW_Flash;
169  Wbuff[wcnt] = (uint32_t)data[i];
170  ++wcnt;
171  }
172 
173  // Write Flash Page
174  res |= CAENComm_MultiWrite16(Flash->Handle, Addrs, wcnt, Wbuff, ECs);
175 
176  // Disable flash
177  res |= CAENComm_Write16(Flash->Handle, Flash->Sel_Flash, !Flash->FlashEnable);
178 
179  // wait for Tep (Time of erase and programming)
180  do
181  res |= GetFlashStatus(Flash, &stat);
182  while (!(stat & 0x80));
183 
184  return res;
185 }
186 
187 
188 static int ReadFlashPage32(cvFlashAccess *Flash, uint8_t *data, int pagenum)
189 {
190  unsigned int i;
191  int res = 0;
192  uint32_t flash_addr;
193  uint32_t Wbuff[MAX_MULTIACCESS_BUFSIZE]; // Buffers for the Multi Read/Write
194  uint32_t Addrs[MAX_MULTIACCESS_BUFSIZE];
195  uint32_t Rbuff[MAX_MULTIACCESS_BUFSIZE];
197 
198  int wcnt = 0, rcnt = 0;
199 
200  if (Flash->PageSize == 264) // 4 and 8 Mbit
201  flash_addr = (uint32_t)pagenum << 9;
202  else if (Flash->PageSize == 528) // 16 and 32 Mbit
203  flash_addr = (uint32_t)pagenum << 10;
204  else // 64 Mbit
205  flash_addr = (uint32_t)pagenum << 11;
206 
207  // Enable flash
208  res |= CAENComm_Write32(Flash->Handle, Flash->Sel_Flash, Flash->FlashEnable);
209 
210  // Opcode
211  Wbuff[wcnt] = MAIN_MEM_PAGE_READ_CMD;
212  Addrs[wcnt] = Flash->RW_Flash;
213  ++wcnt;
214  // Page address
215  Wbuff[wcnt] = (flash_addr>>16) & 0xFF;
216  Addrs[wcnt] = Flash->RW_Flash;
217  ++wcnt;
218  Wbuff[wcnt] = (flash_addr>>8) & 0xFF;
219  Addrs[wcnt] = Flash->RW_Flash;
220  ++wcnt;
221  Wbuff[wcnt] = flash_addr & 0xFF;
222  Addrs[wcnt] = Flash->RW_Flash;
223  ++wcnt;
224  // additional don't care bytes
225  for (i=0; i<4; i++) {
226  Wbuff[wcnt] = 0;
227  Addrs[wcnt] = Flash->RW_Flash;
228  ++wcnt;
229  }
230 
231  res |= CAENComm_MultiWrite32(Flash->Handle, Addrs, wcnt, Wbuff, ECs);
232 
233  // Read Flash Page
234  rcnt = 0;
235  for (i=0; i<Flash->PageSize; i++) {
236  Addrs[rcnt] = Flash->RW_Flash;
237  ++rcnt;
238  }
239  res |= CAENComm_MultiRead32(Flash->Handle, Addrs, Flash->PageSize, Rbuff, ECs);
240 
241  // Disable flash
242  res |= CAENComm_Write32(Flash->Handle, Flash->Sel_Flash, !Flash->FlashEnable);
243 
244  // transfer the 32bit data buffer to the 8 bit buffer
245  for (i=0; i<Flash->PageSize; i++)
246  data[i] = (uint8_t)Rbuff[i];
247 
248  return res;
249 }
250 
251 
252 static int ReadFlashPage16(cvFlashAccess *Flash, uint8_t *data, int pagenum)
253 {
254  unsigned int i;
255  int res = 0;
256  uint32_t flash_addr;
257  uint16_t Wbuff[MAX_MULTIACCESS_BUFSIZE]; // Buffers for the Multi Read/Write
258  uint32_t Addrs[MAX_MULTIACCESS_BUFSIZE];
259  uint16_t Rbuff[MAX_MULTIACCESS_BUFSIZE];
261 
262  int wcnt = 0, rcnt = 0;
263 
264  if (Flash->PageSize == 264) // 4 and 8 Mbit
265  flash_addr = (uint32_t)pagenum << 9;
266  else if (Flash->PageSize == 528) // 16 and 32 Mbit
267  flash_addr = (uint32_t)pagenum << 10;
268  else // 64 Mbit
269  flash_addr = (uint32_t)pagenum << 11;
270 
271  // Enable flash
272  res |= CAENComm_Write16(Flash->Handle, Flash->Sel_Flash, Flash->FlashEnable);
273 
274  // Opcode
275  Wbuff[wcnt] = MAIN_MEM_PAGE_READ_CMD;
276  Addrs[wcnt] = Flash->RW_Flash;
277  ++wcnt;
278  // Page address
279  Wbuff[wcnt] = (flash_addr>>16) & 0xFF;
280  Addrs[wcnt] = Flash->RW_Flash;
281  ++wcnt;
282  Wbuff[wcnt] = (flash_addr>>8) & 0xFF;
283  Addrs[wcnt] = Flash->RW_Flash;
284  ++wcnt;
285  Wbuff[wcnt] = flash_addr & 0xFF;
286  Addrs[wcnt] = Flash->RW_Flash;
287  ++wcnt;
288  // additional don't care bytes
289  for (i=0; i<4; i++) {
290  Wbuff[wcnt] = 0;
291  Addrs[wcnt] = Flash->RW_Flash;
292  ++wcnt;
293  }
294 
295  // Send opcodes to flash
296  res |= CAENComm_MultiWrite16(Flash->Handle, Addrs, wcnt, Wbuff, ECs);
297 
298  // Read Flash Page
299  rcnt = 0;
300  for (i=0; i<Flash->PageSize; i++) {
301  Addrs[rcnt] = Flash->RW_Flash;
302  ++rcnt;
303  }
304  res |= CAENComm_MultiRead16(Flash->Handle, Addrs, Flash->PageSize, Rbuff, ECs);
305 
306  // Disable flash
307  res |= CAENComm_Write16(Flash->Handle, Flash->Sel_Flash, !Flash->FlashEnable);
308 
309  // transfer the 32bit data buffer to the 8 bit buffer
310  for (i=0; i<Flash->PageSize; i++)
311  data[i] = (uint8_t)Rbuff[i];
312 
313  return res;
314 }
315 
316 static int ReadFlashSecurityReg32(cvFlashAccess *Flash, uint8_t *data)
317 {
318  int i;
319  int res = 0;
320  uint32_t data32[2048];
321 
322  // enable flash (NCS = 0)
323  res |= CAENComm_Write32(Flash->Handle, Flash->Sel_Flash, Flash->FlashEnable);
324 
325  // write opcode
327  // additional don't care bytes
328  for (i=0; i<3; i++)
329  res |= CAENComm_Write32(Flash->Handle, Flash->RW_Flash, 0);
330 
331  // read flash page
332  for (i=0; i<AT45_IDREG_LENGTH; i++)
333  res |= CAENComm_Read32(Flash->Handle, Flash->RW_Flash,(data32+i));
334 
335  // disable flash (NCS = 1)
336  res |= CAENComm_Write32(Flash->Handle, Flash->Sel_Flash, !Flash->FlashEnable);
337  for (i=0; i<AT45_IDREG_LENGTH; i++)
338  data[i] = (uint8_t)data32[i];
339 
340  return res;
341 }
342 
343 static int ReadFlashSecurityReg16(cvFlashAccess *Flash, uint8_t *data)
344 {
345  int i;
346  int res = 0;
347  uint16_t data16[2048];
348 
349  // enable flash (NCS = 0)
350  res |= CAENComm_Write16(Flash->Handle, Flash->Sel_Flash, 0);
351 
352  // write opcode
354  // additional don't care bytes
355  for (i=0; i<3; i++)
356  res |= CAENComm_Write16(Flash->Handle, Flash->RW_Flash, Flash->FlashEnable);
357 
358  // read flash page
359  for (i=0; i<AT45_IDREG_LENGTH; i++)
360  res |= CAENComm_Read16(Flash->Handle, Flash->RW_Flash,(data16+i));
361 
362  // disable flash (NCS = 1)
363  res |= CAENComm_Write16(Flash->Handle, Flash->Sel_Flash, !Flash->FlashEnable);
364  for (i=0; i<AT45_IDREG_LENGTH; i++)
365  data[i] = (uint8_t)data16[i];
366 
367  return res;
368 }
369 
370 
371 
372 // *****************************************************************************
373 // WriteFlashPage: write one page of the flash memory
374 // Parameters: data: pointer to the data buffer
375 // pagenum: flash page number
376 // Return: 0 on Success, < 0 on error
377 // *****************************************************************************
378 int WriteFlashPage(cvFlashAccess *Flash, uint8_t *data, int pagenum) {
379 
380  int ret;
381 
382  if (Flash->RegSize == 4)
383  ret = WriteFlashPage32(Flash, data, pagenum);
384  else if (Flash->RegSize == 2)
385  ret = WriteFlashPage16(Flash, data, pagenum);
386  else
387  ret = CvUpgrade_FileAccessError; /* Unsupported RegSize */
388 
389  return ret;
390 
391 }
392 
393 // *****************************************************************************
394 // ReadFlashPage: read one page of the flash memory
395 // Parameters: data: pointer to the data buffer
396 // pagenum: flash page number
397 // Return: 0 on Success, < 0 on error
398 // *****************************************************************************
399 int ReadFlashPage(cvFlashAccess *Flash, uint8_t *data, int pagenum) {
400 
401  int ret;
402 
403  if (Flash->RegSize == 4)
404  ret = ReadFlashPage32(Flash, data, pagenum);
405  else if (Flash->RegSize == 2)
406  ret = ReadFlashPage16(Flash, data, pagenum);
407  else
408  ret = CvUpgrade_FileAccessError; /* Unsupported RegSize */
409 
410  return ret;
411 
412 }
413 
414 
415 // *****************************************************************************
416 // ReadFlashSecurityReg
417 // *****************************************************************************
418 int ReadFlashSecurityReg(cvFlashAccess *Flash, uint8_t *data) {
419 
420  int ret;
421 
422  if (Flash->RegSize == 4)
423  ret = ReadFlashSecurityReg32(Flash, data);
424  else if (Flash->RegSize == 2)
425  ret = ReadFlashSecurityReg16(Flash, data);
426  else
427  ret = CvUpgrade_FileAccessError; /* Unsupported RegSize */
428 
429  return ret;
430 
431 }