Netcruzer Library API  V2.03
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups Pages
nz_appConfig.h
Go to the documentation of this file.
1 
67 #ifndef NZ_APPCONFIG_H
68 #define NZ_APPCONFIG_H
69 
70 
71 /* ************************************************************************************************
72 =============== Modify firmware upgrading and Netcruzer Bootloader ===============
73  - Modify bootloader on PIC, so that at startup it checks if there is any configuration data in external
74  Flash. If not, it stays in Bootloader mode until config data is uploaded.
75  - Modify "Netcruzer USB Bootloader" with new mode where it will automatically upload firmware and config
76  from HEX file as soon as board has been programmed. Add a "Run" button to app that becomes available
77  once this has been uploaded to device.
78 
79 =============== Low cost future version using external FLASH - Currently NOT USED ===============
80 No EEPROM method for configuration data. Store in external serial Flash. Erase sectors 4k.
81 Configuration data is stored as Id, Value entries. Flash is devided in blocks of 8 bytes.
82 Each 8 byte block has following format:
83 - 0-1: First 2 bytes is ID, length and deleted info
84  x--- ---- ---- ---- = Deleted bit
85  -xxx x--- ---- ---- = Length, gives multiples of 8 byte blocks used. First block has 6 bytes of data, subsequent
86  blocks 8 bytes. This gives max of 6 + (16 x 8) = 134 bytes
87  ---- -xxx xxxx xxxx = ID, max of 2048 settings
88 - 2-7: Data of 6 bytes. If more is needed, length above is used to assign more 8 byte blocks
89 
90 Each 4K erase block in flash has following format:
91 - 64 Bytes = Used bit fields (512 bits = 512 x 8 byte blocks). Bit is cleared if pointed to 8 byte block is used
92 - 64 Bytes = Deleted bit fields (512 bits = 512 x 8 byte blocks). Bit is cleared if pointed to 8 byte block is
93  deleted. This will happen if a setting is updated. In this case the old setting will have it's deleted
94  bit cleared, and written to next available position in flash.
95 - 3968 Bytes = 496 x 8 byte blocks
96 
97 In internal RAM of CPU, a cach area is reserved. Default is 256 bytes. Will contain latest read Id/Value data.
98 Is saved in 8 byte format, same as defined above. Implemented as circular buffer. Each time a setting is needed that
99 is not in cach, it is read from Flash, and added to cach. Entries are deleted from head until enough space is made for
100 newly read setting to be saved.
101 ************************************************************************************************ */
102 
103 
105 // Application Configuration - documentation module
183 //CFG_BLOCK_XXX Structures are used in CFG_STRUCT. Ensure each CFG_BLOCK_XXX
186 //structure is smaller than 255 bytes.
187 //Ensure ((size of block)+4) is a 32 byte multiple. Ensure size is 4 less then 32 byte
188 //multiple, seeing that each CFG_BLOCK_XXX is preceded by a CFG_BLOCK_HDR (4 bytes long).
189 //Each block MUST have a 'reserve' member (byte array), even if it is 0.
190 //
191 //The only reason all CFG_BLOCKs should be 32 byte multiples, is because EEPROM has
192 //a page write size of 32. So, if we change a whole CFG_BLOCK, and the boundaries
193 //are on 32 byte blocks, chances are it will require less "EEPROM block writes". Each
194 //"EEPROM block write" takes about 5 ms, so this speeds things up a bit. BUT, it will
195 //not make a big difference if CFG_BLOCKs are 16 byte multiples. Do NOT use less than
196 //16 byte multiples, because the CFG_BLOCK_HDR.size counts in multiples of 16 bytes!
197 
198 //Config block ID. The following ranges are reserved:
199 // - 0x0000 to 0x00ff: Used to mark config blocks as deleted
200 // - 0xff00 to 0xffff: Used to mark config blocks as free
201 // - 0x8000 to 0xefff: Used for TABLE_BLOCKS = a constant CFG_BLOCK
202 #define CFG_BLOCK_INFO_ID 0x0100
203 #define CFG_BLOCK_NET_ID 0x0101
204 #define CFG_BLOCK_WIFI_ID 0x0102
205 #define CFG_BLOCK_SNMP_ID 0x0103
206 #define CFG_BLOCK_GENERAL_ID 0x0104
207 #define CFG_BLOCK_NET2_ID 0x0105
208 #define CFG_BLOCK_SER_0TO7_ID 0x0108
209 #define CFG_BLOCK_SER_8TO15_ID 0x0109
210 #define CFG_BLOCK_SER_16TO23_ID 0x010A
211 #define CFG_BLOCK_SER_24TO31_ID 0x010B
212 #define CFG_BLOCK_STRING_31X8_ID 0x0110
213 #define CFG_BLOCK_UC66_PORT_ID 0x0114
214 #define CFG_BLOCK_SBC66_PORT_ID 0x0115
215 #define CFG_BLOCK_RESERVE_ID 0xFF00
216 
217 
221 typedef struct __attribute__((__packed__))
222 {
223  WORD id;
224  BYTE reserved;
225  BYTE size;
228 } CFG_BLOCK_HDR;
229 
230 
231 
234 //The TABLE_STRUCT is a structure defining static constants.
235 //They can not be change, like the CFG_STRUCT.
236 
237 //Table block ID, use range 0x8000 to 0xefff.
238 #define TABLE_BLOCK_UC66_PORT_ID 0x8200
239 #define TABLE_BLOCK_SBC66_PORT_ID 0x8220
240 
241 
245 typedef struct __attribute__((__packed__))
246 {
247  WORD id;
248  BYTE reserved;
249  BYTE size;
253 
254 
258 #define CFG_BLOCK_SIZE(cfgBlk) (sizeof (((CFG_STRUCT*) 0)->cfgBlk) - sizeof (((CFG_STRUCT*) 0)->cfgBlk.reserve))
259 
260 
261 
264 
268 typedef struct __attribute__((__packed__))
269 {
270  CFG_BLOCK_HDR hdr;
271 
272  //Size of configuration data part of CFG_STRUCT. The last part of CFG_STRUCT is TABLE_BLOCKs, and is
273  //not part of the configuration data.
274  WORD confSize;
275 
284  BYTE fillConfUpdated; //Can maybe use for adding to 'confSize', to make it 24 bits
285  volatile BYTE eeConfUpdated;
286 
287  BYTE reserve[24];
289 
290 
294 typedef struct __attribute__((__packed__))
295 {
296  union {
297  struct
298  {
299  unsigned char bCurr : 2; //Indicates which is current firmware. 0=Unknown, 1=FirmwareA, 2=FirmwareB
300  unsigned char bFill : 6; //Fill to ensure flags take up a 8-bit WORD
301  } bits;
302  BYTE val;
303  } flags;
305 #define CURRENT_FIRMWARE_UNKNOWN 0
306 #define CURRENT_FIRMWARE_A 1
307 #define CURRENT_FIRMWARE_B 2
308 
310 #ifndef THIS_IS_NZ_APPCONFIG_C
311  extern FIRMWARE_FLAGS firmwareFlags;
312 #endif
313 
314 
315 //External EEPROM Memory defines. IMPORTANT!!! Ensure all start addresses are on 32 byte boundaries
316 #define XEEMEM_CFG_STRUCT_SIZE 0x0C00 //3 kBytes
317 
318 #define XEEMEM_CFG_INFO_START_ADR 0x0000
319 #define XEEMEM_CFG_INFO_END_ADR 0x001F
320 #define XEEMEM_CFG_COPY_START_ADR 0x0020
321 #define XEEMEM_CFG_COPY_END_ADR 0x03FF
322 #define XEEMEM_CFG_COPY_STOP_ADR 0x0400 /* Address of first byte outside of CFG_COPY area */
323 #define XEEMEM_CFG_COPY_BLOCKS ((XEEMEM_CFG_COPY_END_ADR-XEEMEM_CFG_COPY_START_ADR+1)/32)
324 #define XEEMEM_CFG_STRUCT_START_ADR 0x0400
325 #define XEEMEM_CFG_STRUCT_END_ADR (XEEMEM_CFG_STRUCT_START_ADR + XEEMEM_CFG_STRUCT_SIZE - 1)
326 #define XEEMEM_USER_START_ADR (XEEMEM_CFG_STRUCT_START_ADR + XEEMEM_CFG_STRUCT_SIZE)
327 #define XEEMEM_USER_END_ADR (XEEPROM_SIZE - 1)
328 
332 #define cfgIncCfgCopyAdr(adr) ((adr>=(XEEMEM_CFG_COPY_STOP_ADR-32)) ? XEEMEM_CFG_COPY_START_ADR : (adr+32))
333 
334 
338 typedef struct __attribute__((packed))
339 {
340  WORD_VAL magicNumber;
341  WORD_VAL cfgVersion;
342  WORD_VAL cfgChecksum;
343  WORD_VAL cfgCRC;
344 } CFG_INFO;
345 #define CFG_INFO_MAGIC_NUMBER 0x536A
346 #define CFG_VERSION_MAJOR 1
347 #define CFG_VERSION_MINOR 0
348 
349 #define CFG_COPY_DATA_SIZE 26
350 
373 typedef struct __attribute__((packed))
374 {
375  BYTE IncNum;
376  WORD Address;
377  BYTE Size;
378  BYTE CRC;
379  BYTE Data[CFG_COPY_DATA_SIZE];
380  BYTE IncNumPlusOne;
381 } CFG_COPY;
382 
383 
387 void cfgInit(BYTE* pCfgStructInfo);
388 
394 BYTE cfgIsXeepromValid(void);
395 
400 void cfgInvalidateXeeprom(void);
401 
415 BYTE cfgGetDfltArray(WORD offset, BYTE* buf, BYTE length);
416 
428 #define cfgGetDfltStruct(member, struc) cfgGetDfltArray(offsetof(CFG_STRUCT, member), ((BYTE*)&struc), sizeof(struc))
429 
443 BYTE cfgBlkGetArray(WORD cfgBlkId, WORD offset, BYTE* buf, BYTE length);
444 
456 BYTE cfgGetArray(WORD offset, BYTE* buf, BYTE length);
457 
466 #define cfgGetStruct(member, struc) cfgGetArray(offsetof(CFG_STRUCT, member), ((BYTE*)&struc), sizeof(struc))
467 
468 
477 //#define cfgGetByte(offset) (*(((BYTE*)&cfgStruct) + offset));
478 #define cfgGetByte(offset) (((BYTE*)&cfgStruct)[offset]);
479 
486 void cfgSaveByte(WORD offset, BYTE val);
487 
498 void cfgSaveArray(WORD offsetCfg, BYTE* buf, WORD length);
499 
503 void cfgSave(void);
504 
505 #endif