Netcruzer Library API  V2.03
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups Pages
nz_xflashDefsSbc66.h
Go to the documentation of this file.
1 
35 #ifndef XFLASH_DEFS_H
36 #define XFLASH_DEFS_H
37 
38 
40 //Hex File
42 // Types of memory regions in hex file. Defines the address ranges of sections of code in the hex file. Data in hex file
43 //is not packed, but is packed in External Flash:
44 // - MEMTYPE_PROGRAM
45 // Program Memory located in hex file from 0x3800 (0x1C00 x 2) to 54FFF (0x2A800 x 2 - 1). Is packed and stored in External
46 // flash (3 bytes for each 4 in hex file) in "FirmwareA" or "FirmwareB".
47 //
48 // - MEMTYPE_XEEPROM
49 // Data for the external EEPROM. Is stored in External FLASH ("FirmwareX Configuration" area) for SBC66EC, and
50 // written to external EEPROM by firmware on device. This data is contained in the hex file from Word address
51 // 0x800000 (byte address 0x1000000). When we receive data located in this address range,
52 // it has to be stored in the "FirmwareX Configuration" section of the external FLASH. Only odd addresses in the
53 // hex file contains data. Each odd address contains 2 bytes of data, even addresses do NOT contain any data.
54 // Under certian conditions, the "User Program" will read this data, and program the External EEPROM. These conditions include when new
55 // firmware is uploaded to the board, or if the configuration data in the external EEPROM becomes corrupt.
56 //
57 // - MEMTYPE_XFLASH
58 // Data for the external FLASH.
59 // It is possible programming the "User Space" in the external flash with data located in the hex file.
60 // For the SBC66EC, this data is contained in the hex file from Word address 0x820000 (byte address 0x1040000).
61 // When we receive data located in this address range, it is programmed to the "User Space" area of the external
62 // FLASH. Maximum size is 0xE0000 Words = 1,835,008 Bytes. Only odd addresses in the hex file contains data, so
63 // data = 1,835008/2 = 917,503. Each odd address contains 2 bytes of data, even addresses do NOT contain any data.
64 //
65 #define MEM_VECTORS_START 0x00000000
66 #define MEM_VECTORS_END 0x00000400 //One page of vectors + general purpose program memory. Word Address
67 //Bootloader resides in memory range 0x400-0x1BFF
68 
69 //Flash Page = hex address / 0x400
70 #if defined(__PIC24FJ256GB210__) || defined(__PIC24FJ256GB206__)
71  #define MEM_PROGRAM_START 0x00003800 //Byte address in internal Program Memory of PIC (Word adr = 0x1C00)
72  #define MEM_PROGRAM_LAST 0x00054FFF //Last Byte address in internal Program Memory of PIC (Word adr = 0x2A7FF)
73 
74  #define MEM_EEPROM_START 0xFFFFFFFF //No internal EEPROM
75  #define MEM_EEPROM_LAST 0xFFFFFFFF //No internal EEPROM
76 
77  #define MEM_CONFIG_START 0x00055000 //Byte address in internal Program Memory of PIC (Word adr = 0x2A800)
78  #define MEM_CONFIG_LAST 0x000557FF //Last Byte address in internal Program Memory of PIC (Word adr = 0x2ABFF)
79 
80  #define MEM_XEEPROM_START 0x01000000 //Byte address (in hex file) of start of "FirmwareX Configuration" in external Flash = 0x800000 word address
81  //SBC66EC has 8K Byte EEPROM. 16K Bytes of hex file space is needed because only 50% contains data
82  #define MEM_XEEPROM_LAST (MEM_XEEPROM_START + (XFLASH_FIRMWARE_CONFIG_SIZE_IN_SECTORS*0x1000*2) - 1)
83  //#define MEM_XEEPROM_LAST 0x01003FFF
84  //#define MEM_XEEPROM_LAST_MAX 0x0103FFFF //Maximum possible is 3FFFF. Only 50% contain data = 256/2 = 128 KBytes.
85 
86  #define MEM_XFLASH_START 0x01040000 //Byte address (in hex file) of start of User Space in external Flash = 0x820000 word address
87  //SBC66EC has 4096 Bytes of User Data (1 sector). 8K Bytes (1FFF) of hex file space is needed because only 50% contains data
88  #define MEM_XFLASH_LAST (MEM_XFLASH_START_SBC66EC + (XFLASH_USER_SPACE_SIZE_IN_SECTORS*0x1000*2) - 1)
89  //#define MEM_XFLASH_LAST 0x01041FFF
90  //#define MEM_XFLASH_LAST_MAX 0x011FFFFF //Maximum possible is 11FFFFF-1040000 = 16FFFF. Only 50% contain data = 1,835008/2 = 917,503 Bytes
91 
92  #define PROGRAM_MEM_WR_ROW_SIZE 128 //For PIC24F family, 128 (0x80) Words can be written at a time. This is 128 x 1.5 = 192 bytes. Upper byte of each odd address Word is not implemented
93  #define PROGRAM_MEM_WR_ROW_MASK (PROGRAM_MEM_WR_ROW_SIZE-1)
94 
95  #define PROGRAM_MEM_ERASE_BLOCK_SIZE 1024 //Smallest number of words that can be erased. To get bytes x 1.5
96  #define PROGRAM_MEM_ERASE_BLOCK_MASK (PROGRAM_MEM_ERASE_BLOCK_SIZE-1)
97 #elif defined(__PIC24FJ128GB210__) || defined(__PIC24FJ128GB206__) || defined(__PIC24FJ128GB106__)
98  #define MEM_PROGRAM_START 0x00003800 //Byte address in internal Program Memory of PIC (Word adr = 0x1C00)
99  #define MEM_PROGRAM_LAST 0x0002A7FF //Last Byte address in internal Program Memory of PIC (Word adr = 0x153FF)
100 
101  #define MEM_EEPROM_START 0xFFFFFFFF //No internal EEPROM
102  #define MEM_EEPROM_LAST 0xFFFFFFFF //No internal EEPROM
103 
104  #define MEM_CONFIG_START 0x0002A800 //Byte address in internal Program Memory of PIC (Word adr = 0x???)
105  #define MEM_CONFIG_LAST 0x0002AFFF //Last Byte address in internal Program Memory of PIC (Word adr = 0x???)
106 
107  #define MEM_XEEPROM_START 0x01000000 //Byte address (in hex file) of start of "FirmwareX Configuration" in external Flash = 0x800000 word address
108  //SBC66ECL has 8K Byte EEPROM. 16K Bytes of hex file space is needed because only 50% contains data
109  #define MEM_XEEPROM_LAST (MEM_XEEPROM_START + (XFLASH_FIRMWARE_CONFIG_SIZE_IN_SECTORS*0x1000*2) - 1)
110  //#define MEM_XEEPROM_LAST 0x01003FFF
111  //#define MEM_XEEPROM_LAST_MAX 0x0103FFFF //Maximum possible is 3FFFF. Only 50% contain data = 256/2 = 128 KBytes.
112 
113  #define MEM_XFLASH_START 0x01040000 //Byte address (in hex file) of start of User Space in external Flash = 0x820000 word address
114  //SBC66EC has 4096 Bytes of User Data (1 sector). 8K Bytes (1FFF) of hex file space is needed because only 50% contains data
115  #define MEM_XFLASH_LAST (MEM_XFLASH_START_SBC66ECL + (XFLASH_USER_SPACE_SIZE_IN_SECTORS*0x1000*2) - 1)
116  //#define MEM_XFLASH_LAST 0x01041FFF //Copied from SBC66EC - CHECK!!!!!!!!!!!!!!!!
117  //#define MEM_XFLASH_LAST_MAX 0x011FFFFF //Copied from SBC66EC - CHECK!!!!!!!!!!!!!!!! Maximum possible is 11FFFFF-1040000 = 16FFFF. Only 50% contain data = 1,835008/2 = 917,503 Bytes
118 
119  #define PROGRAM_MEM_WR_ROW_SIZE 128 //For PIC24F family, 128 (0x80) Words can be written at a time. This is 128 x 1.5 = 192 bytes. Upper byte of each odd address Word is not implemented
120  #define PROGRAM_MEM_WR_ROW_MASK (PROGRAM_MEM_WR_ROW_SIZE-1)
121 
122  #define PROGRAM_MEM_ERASE_BLOCK_SIZE 1024 //Smallest number of words that can be erased. To get bytes x 1.5
123  #define PROGRAM_MEM_ERASE_BLOCK_MASK (PROGRAM_MEM_ERASE_BLOCK_SIZE-1)
124 #else
125  #error "This bootloader only covers the PIC24FJ256GB210 family devices. Please see another folder for the bootloader appropriate for the selected device."
126 #endif
127 
128 
129 
131 //External FLASH "User Space", located in hex file
133 //It is possible programming the "User Space" in the external flash with data located in the hex file.
134 //This data is contained in the hex file from Word address 0x820000 (byte address 0x1040000). When we receive data located in
135 //this address range, it is programmed to the "User Space" area of the external FLASH. Maximum size is 0xE0000 Words = 1,835,008 Bytes
136 //Only odd addresses in the hex file contains data. Each odd address contains 2 bytes of data, even addresses do NOT contain any data.
137 #define XFLASH_USERSPACE_START 0x00820000 //Beginning of "External FLASH User Space" memory, Word address. Byte address = 0x1040000. This is the address as given in hex file
138 #define XFLASH_USERSPACE_END 0x00900000 //End of "External FLASH User Space" memory. Word address. Byte address = 0x1200000. This address does not get updated, but the one just below it does.
139 
140 
141 
143 //External FLASH
145 //FirmwareA and FirmwareB contains the Microcontroller's program memory, in a Packed form! Each 3 bytes = one 2 Word Instructions.
146 //The last 8 bytes of the XFLASH_FIRMWAREX_CONFIG and XFLASH_MODIFIED_CONFIG will be used by the bootloader. It will have
147 //a checksum, and a magic number. If the magic number is wrong, it is not valid. When new firmware is downloaded, the
148 //XFLASH_FIRMWAREX_CONFIG is updated with data from new firmware, and after bootloader validates it, the magic number is
149 //set. This ensure it is only set as "valid" after validation. During the "User Program", if the user changes configuration,
150 //and decided to save it as the new default configuration, then it is written to the XFLASH_MODIFIED_CONFIG area,
151 //and the XFLASH_FIRMWAREX_CONFIG area is marked as invalid. If the user again saved new default config, it is now written to
152 //XFLASH_FIRMWAREX_CONFIG, and XFLASH_MODIFIED_CONFIG area is marked as invalid. Each time the user saves new data, it is stored
153 //in either XFLASH_FIRMWAREX_CONFIG or XFLASH_MODIFIED_CONFIG (alternates one, then other....).
154 #define XFLASH_FIRMWAREA_INFO_ADR 0 //Byte address of start of "FirmwareA Info"
155 #define XFLASH_FIRMWAREA_ADR 0x1000 //Byte address of start of "FirmwareA"
156 #define XFLASH_FIRMWAREA_CONFIG 0x40000 //Byte address of start of "FirmwareA Configuration"
157 
158 #define XFLASH_FIRMWAREB_INFO_ADR 0x42000 //Byte address of start of "FirmwareB Info"
159 #define XFLASH_FIRMWAREB_ADR 0x43000 //Byte address of start of "FirmwareB"
160 #define XFLASH_FIRMWAREB_CONFIG 0x82000 //Byte address of start of "FirmwareB Configuration"
161 
162 #define XFLASH_MODIFIED_CONFIG 0x84000
163 
164 #define XFLASH_FIRMWAREB_OFFSET_FROMA (XFLASH_FIRMWAREB_INFO_ADR - XFLASH_FIRMWAREA_INFO_ADR)
165 
166 #define XFLASH_FIRMWARE_SIZE_IN_SECTORS 63 //Indicates size of FirmwareA and FirmwareB areas in 4096 byte sectors = 63x4096 = 258,048 Bytes
167 #define XFLASH_FIRMWARE_CONFIG_SIZE_IN_SECTORS 2 //Indicates size of "FirmwareA Config" and "FirmwareB Config" in 4096 byte sectors = 2x4096 = 8k
168 #define XFLASH_SECTOR_SIZE 4096 //Byte size of a sector of external FLASH. A sector is the smallest area that can be erased.
169 
170 //Byte address of start of "User Space". User space is followed by File System, which is located at address given by MPFS_RESERVE_BLOCK
171 //define in main project. User can use the space in external FLASH from XFLASH_USER_SPACE_ADR up till (MPFS_RESERVE_BLOCK - 1). To increase
172 //user space, increase MPFS_RESERVE_BLOCK value.
173 #define XFLASH_USER_SPACE_ADR 0x86000
174 
175 
176 
177 
181 typedef union __attribute__ ((packed)) _FIRMWARE_STATE
182 {
183  unsigned int Val;
184 
185  struct __attribute__ ((packed)){ //For lock/unlock config command
186  unsigned char LB;
187  unsigned char HB;
188  };
189 
190  struct __attribute__ ((packed)) {
191  unsigned int bNew : 1; //This is new firmware, will be used to upgrade board on next power cycle
192  unsigned int bCurrent : 1; //This is the current firmware
193  unsigned int bPrevious : 1; //This is the previous firmware. After upgrade, current firmware get's marked as Previous
194  unsigned int bRestored : 1; //This is the current firmware, which has been restored. Changed state from Previous to Restored
195  unsigned int bFaulty : 1; //This is faulty firmware. Changed state from Current. This happens when system is restored with previous firmware
196  unsigned int bFill : 11; //Fill till 16 bits
197  };
199 #define FIRMWARE_STATE_NEW 0b11111111
200 #define FIRMWARE_STATE_CURRENT 0b11111110
201 #define FIRMWARE_STATE_PREVIOUS 0b11111100
202 #define FIRMWARE_STATE_RESTORED 0b11111000
203 #define FIRMWARE_STATE_FAULTY 0b11110000
204 
205 
218 typedef struct __attribute__ ((packed)) _FIRMWARE_INFO
219 {
220  WORD_VAL magicNumber; //Must have the value 0x536A to be identified as valid data
221 
222  //The fwState variable is re-written after each change of firmware.
223  //Initial value will be 0b1111111111111111 (New), which indicates this is new firmware. On power up, the
224  //Bootloader will check if FirmwareA or FirmwareB is new, and if it is, upgrade firmware.
225  //After programming board with new firmware, value is set to 0b1111111111111110 (Current). The
226  //fwState of previous firmware is now set to 0b1111111111111100 (Previous). If the bootloader is
227  //used to restore previous firmware, the current firmware is marked as Restored = 0b1111111111111000
228  FIRMWARE_STATE fwState;
229 
230  WORD versionMinor;
231  WORD versionMajor;
232 
233  //Checksum's for "Program Memory" stored in External Flash
234  DWORD checksum1ProgMem; //First checksum, is sum of all bytes
235  DWORD checksum2ProgMem; //Two's complement type checksum
236 
237  //Checksum's for "External Eeprom" Memory stored in External Flash
238  DWORD checksum1XeeMem; //First checksum, is sum of all bytes
239  DWORD checksum2XeeMem; //Two's complement type checksum
240 
241  //Checksum's for "User Space" Memory stored in External Flash
242  DWORD checksum1XflashMem; //First checksum, is sum of all bytes
243  DWORD checksum2XflashMem; //Two's complement type checksum
244 } FIRMWARE_INFO;
245 #define FIRMWARE_INFO_MAGIC_NUMBER 0x536A
246 
247 
248 #endif
249