AlcapDAQ  1
Macros | Typedefs | Enumerations | Functions
A3818Upgrade.h File Reference
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>
#include <stdarg.h>
#include <CAENVMElib.h>
#include "p30.h"
#include <a3818.h>

Go to the source code of this file.

Macros

#define BITSTREAM_BYTES   1756544
 
#define FIRST_FIRMWARE_PAGE_BASE_WORD_ADDRESS   0x000000
 
#define SECOND_FIRMWARE_PAGE_BASE_WORD_ADDRESS   0x200000
 
#define THIRD_FIRMWARE_PAGE_BASE_WORD_ADDRESS   0x400000
 
#define FOURTH_FIRMWARE_PAGE_BASE_WORD_ADDRESS   0x600000
 

Typedefs

typedef enum A3818Upgrade_Mode A3818Upgrade_Mode
 

Enumerations

enum  A3818Upgrade_Mode { A3818_UPGRADE_FULL = 0, A3818_UPGRADE_ERASE_ONLY = 1, A3818_UPGRADE_VERIFY_ONLY = 2 }
 

Functions

int A3818UpgradeFromFile (int32_t A3818_handle, FILE *binfile, int fwcopy, A3818Upgrade_Mode mode)
 
int A3818UpgradeFromMem (int32_t A3818_handle, char *fwdata, int fwsize, int fwcopy, A3818Upgrade_Mode mode)
 

Macro Definition Documentation

#define BITSTREAM_BYTES   1756544

Definition at line 48 of file A3818Upgrade.h.

Referenced by A3818UpgradeFromFile(), and A3818UpgradeFromMem().

#define FIRST_FIRMWARE_PAGE_BASE_WORD_ADDRESS   0x000000

Definition at line 51 of file A3818Upgrade.h.

#define FOURTH_FIRMWARE_PAGE_BASE_WORD_ADDRESS   0x600000

Definition at line 54 of file A3818Upgrade.h.

#define SECOND_FIRMWARE_PAGE_BASE_WORD_ADDRESS   0x200000

Definition at line 52 of file A3818Upgrade.h.

#define THIRD_FIRMWARE_PAGE_BASE_WORD_ADDRESS   0x400000

Definition at line 53 of file A3818Upgrade.h.

Typedef Documentation

Enumeration Type Documentation

Enumerator
A3818_UPGRADE_FULL 
A3818_UPGRADE_ERASE_ONLY 
A3818_UPGRADE_VERIFY_ONLY 

Definition at line 39 of file A3818Upgrade.h.

39  {
40 /* Error Types */
41  A3818_UPGRADE_FULL = 0,

Function Documentation

int A3818UpgradeFromFile ( int32_t  A3818_handle,
FILE *  binfile,
int  fwcopy,
A3818Upgrade_Mode  mode 
)

Definition at line 52 of file A3818Upgrade.c.

References A3818_EnableBPIAccess(), A3818_EnableSPIAccess(), A3818_UPGRADE_ERASE_ONLY, A3818_UPGRADE_FULL, A3818_UPGRADE_VERIFY_ONLY, BITSTREAM_BYTES, c, c1, eraseFirmware(), FIRST_FIRMWARE_PAGE_BASE_WORD_ADDRESS, FOURTH_FIRMWARE_PAGE_BASE_WORD_ADDRESS, get_time(), i, printf(), SECOND_FIRMWARE_PAGE_BASE_WORD_ADDRESS, THIRD_FIRMWARE_PAGE_BASE_WORD_ADDRESS, verifyFlash(), and writeFlash().

Referenced by main().

52  {
53 
54  uint32_t baseAddress;
55  long currentTime, elapsedTime;
56  int finish;
57  unsigned char c, c1;
58  uint32_t bp;
59  uint32_t bufferLength;
60 
61  uint32_t *buffer;
62  unsigned int verifyErrors = 0;
63  buffer = malloc(BITSTREAM_BYTES * sizeof(uint32_t));
64  switch(fwcopy) {
65  case 0 :
67  break;
68  case 1 :
70  break;
71  case 2 :
73  break;
74  case 3 :
76  break;
77  default :
79  break;
80 
81  }
82 
83  bp = 0;
84  finish = 0;
85 
86  // Carica il bitsream in un buffer di memoria
87  currentTime = get_time();
88  while( !finish ) {
89 
90  c = (unsigned char)fgetc(binfile); // read one byte from file
91  c1 = (unsigned char)fgetc(binfile); // read one byte from file (Strataflash a 16 bit)
92 
93 #ifdef __SWAP__
94  swap = 0;
95 
96  // Swap primo byte
97  for( i = 0; i < 8; i++ )
98  if( c & (1 << i) )
99  swap = swap | (0x80 >> i);
100 
101  swap1 = 0;
102  // Swap secondo byte
103  for( i = 0; i < 8; i++ )
104  if( c1 & (1 << i) )
105  swap1 = swap1 | (0x80 >> i);
106 
107  buffer[bp] = (uint32_t) ((swap1 <<8) | swap); // HACK : swap o non swap?
108 #else
109  buffer[bp] = (uint32_t) ((c1 <<8) | c);
110 #endif // __SWAP__
111 
112  bp++;
113  if( feof(binfile) )
114  finish = 1;
115  } // end of while loop
116 
117  bufferLength = (--bp);
118 
119  if ((bufferLength*2) != BITSTREAM_BYTES) {
120  printf("\nERROR: Input BIN file length (%d bytes) is different than expected bitstream size (%d bytes). Exiting.....\n", bufferLength*2, BITSTREAM_BYTES);
121  free(buffer);
122  return -1;
123  }
124 
125  elapsedTime = get_time() - currentTime;
126  printf("\nBitstream (%d bytes) loaded in %ld milliseconds\n", bufferLength*2, elapsedTime);
127 
128  A3818_EnableBPIAccess(A3818_handle);
129 
130  /* Cancellazione della zona di flash riservata al firmware */
131  if ( (mode == A3818_UPGRADE_FULL ) ||
132  (mode == A3818_UPGRADE_ERASE_ONLY) ) {
133  printf("Erasing flash ");
134  fflush(stdout);
135  currentTime = get_time();
136  eraseFirmware(A3818_handle, baseAddress, fwcopy);
137  elapsedTime = get_time() - currentTime;
138  printf("\nFlash erased in %ld milliseconds\n", elapsedTime);
139  }
140 
141  /* Programmazione immagine firmware in flash */
142  if ( (mode == A3818_UPGRADE_FULL ) ) {
143  currentTime = get_time();
144  writeFlash(A3818_handle, buffer, bufferLength, baseAddress);
145  elapsedTime = get_time() - currentTime;
146  printf("\n%d 16-bit words programmed in %ld milliseconds\n", bufferLength, elapsedTime);
147  }
148 
149  /* Verifica immagine firmware in flash */
150  if ( (mode == A3818_UPGRADE_FULL ) ||
151  (mode == A3818_UPGRADE_VERIFY_ONLY) ) {
152  currentTime = get_time();
153  verifyErrors = verifyFlash(A3818_handle, buffer, bufferLength, baseAddress);
154  elapsedTime = get_time() - currentTime;
155  printf("\n%d 16-bit words verified in %ld milliseconds\n", bufferLength, elapsedTime);
156  }
157 
158  A3818_EnableSPIAccess(A3818_handle);
159 
160 
161  if( verifyErrors > 0 ) {
162  printf("\n\n%d errors found during verify!\n",verifyErrors);
163  } else {
164  printf("\n\nFirmware updated without errors. Written %d words\n",bufferLength);
165  }
166  free(buffer);
167  return 0;
168 }
int A3818UpgradeFromMem ( int32_t  A3818_handle,
char *  fwdata,
int  fwsize,
int  fwcopy,
A3818Upgrade_Mode  mode 
)

Definition at line 171 of file A3818Upgrade.c.

References A3818_EnableBPIAccess(), A3818_EnableSPIAccess(), A3818_UPGRADE_ERASE_ONLY, A3818_UPGRADE_FULL, A3818_UPGRADE_VERIFY_ONLY, BITSTREAM_BYTES, c, c1, eraseFirmware(), FIRST_FIRMWARE_PAGE_BASE_WORD_ADDRESS, FOURTH_FIRMWARE_PAGE_BASE_WORD_ADDRESS, get_time(), i, printf(), SECOND_FIRMWARE_PAGE_BASE_WORD_ADDRESS, THIRD_FIRMWARE_PAGE_BASE_WORD_ADDRESS, verifyFlash(), and writeFlash().

Referenced by main().

171  {
172  int fi = 0;
173  uint32_t baseAddress;
174  long currentTime, elapsedTime;
175  int finish;
176  unsigned char c, c1;
177  uint32_t bp;
178  uint32_t bufferLength;
179 
180  uint32_t *buffer;
181  unsigned int verifyErrors = 0;
182  buffer = malloc(BITSTREAM_BYTES * sizeof(uint32_t));
183  switch(fwcopy) {
184  case 0 :
186  break;
187  case 1 :
189  break;
190  case 2 :
192  break;
193  case 3 :
195  break;
196  default :
198  break;
199 
200  }
201 
202  bp = 0;
203  finish = 0;
204 
205  // Carica il bitsream in un buffer di memoria
206  currentTime = get_time();
207  while( !finish ) {
208 
209  c = (unsigned char)fwdata[fi++]; // read one byte from file
210  c1 = (unsigned char)fwdata[fi++]; // read one byte from file (Strataflash a 16 bit)
211 
212 #ifdef __SWAP__
213  swap = 0;
214 
215  // Swap primo byte
216  for( i = 0; i < 8; i++ )
217  if( c & (1 << i) )
218  swap = swap | (0x80 >> i);
219 
220  swap1 = 0;
221  // Swap secondo byte
222  for( i = 0; i < 8; i++ )
223  if( c1 & (1 << i) )
224  swap1 = swap1 | (0x80 >> i);
225 
226  buffer[bp] = (uint32_t) ((swap1 <<8) | swap); // HACK : swap o non swap?
227 #else
228  buffer[bp] = (uint32_t) ((c1 <<8) | c);
229 #endif // __SWAP__
230 
231  bp++;
232  if( fi >= fwsize )
233  finish = 1;
234  } // end of while loop
235 
236  bufferLength = (--bp);
237 
238  if ((bufferLength*2) != BITSTREAM_BYTES) {
239  printf("\nERROR: Input BIN file length (%d bytes) is different than expected bitstream size (%d bytes). Exiting.....\n", bufferLength*2, BITSTREAM_BYTES);
240  free(buffer);
241  return -1;
242  }
243 
244  elapsedTime = get_time() - currentTime;
245  printf("\nBitstream (%d bytes) loaded in %ld milliseconds\n", bufferLength*2, elapsedTime);
246 
247  A3818_EnableBPIAccess(A3818_handle);
248 
249  /* Cancellazione della zona di flash riservata al firmware */
250  if ( (mode == A3818_UPGRADE_FULL ) ||
251  (mode == A3818_UPGRADE_ERASE_ONLY) ) {
252  printf("Erasing flash ");
253  fflush(stdout);
254  currentTime = get_time();
255  eraseFirmware(A3818_handle, baseAddress, fwcopy);
256  elapsedTime = get_time() - currentTime;
257  printf("\nFlash erased in %ld milliseconds\n", elapsedTime);
258  }
259 
260  /* Programmazione immagine firmware in flash */
261  if ( (mode == A3818_UPGRADE_FULL ) ) {
262  currentTime = get_time();
263  writeFlash(A3818_handle, buffer, bufferLength, baseAddress);
264  elapsedTime = get_time() - currentTime;
265  printf("\n%d 16-bit words programmed in %ld milliseconds\n", bufferLength, elapsedTime);
266  }
267 
268  /* Verifica immagine firmware in flash */
269  if ( (mode == A3818_UPGRADE_FULL ) ||
270  (mode == A3818_UPGRADE_VERIFY_ONLY) ) {
271  currentTime = get_time();
272  verifyErrors = verifyFlash(A3818_handle, buffer, bufferLength, baseAddress);
273  elapsedTime = get_time() - currentTime;
274  printf("\n%d 16-bit words verified in %ld milliseconds\n", bufferLength, elapsedTime);
275  }
276 
277  A3818_EnableSPIAccess(A3818_handle);
278 
279 
280  if( verifyErrors > 0 ) {
281  printf("\n\n%d errors found during verify!\n",verifyErrors);
282  } else {
283  printf("\n\nFirmware updated without errors. Written %d words\n",bufferLength);
284  }
285  free(buffer);
286  return 0;
287 }