fork download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. typedef enum _BOOL { FALSE = 0, TRUE } BOOL; // Undefined size
  5.  
  6.  
  7. typedef uint8_t BYTE; // 8-bit unsigned
  8. typedef uint16_t WORD; // 16-bit unsigned
  9. typedef uint32_t DWORD; // 32-bit unsigned
  10. typedef uint64_t QWORD; // 64-bit unsigned
  11. typedef int8_t CHAR; // 8-bit signed
  12. typedef int16_t SHORT; // 16-bit signed
  13. typedef int32_t LONG; // 32-bit signed
  14. typedef int64_t LONGLONG; // 64-bit signed
  15.  
  16.  
  17. typedef void VOID;
  18.  
  19. typedef int8_t CHAR8;
  20. typedef uint8_t UCHAR8;
  21.  
  22. typedef int16_t INT;
  23. typedef int8_t INT8;
  24. typedef int16_t INT16;
  25. typedef int32_t INT32;
  26. typedef int64_t INT64;
  27.  
  28. typedef uint16_t UINT;
  29. typedef uint8_t UINT8;
  30. typedef uint16_t UINT16;
  31. typedef uint32_t UINT32; // other name for 32-bit integer
  32. typedef uint64_t UINT64;
  33.  
  34.  
  35. typedef union _BYTE_VAL
  36. {
  37. BYTE Val;
  38. struct
  39. {
  40. unsigned char b0:1;
  41. unsigned char b1:1;
  42. unsigned char b2:1;
  43. unsigned char b3:1;
  44. unsigned char b4:1;
  45. unsigned char b5:1;
  46. unsigned char b6:1;
  47. unsigned char b7:1;
  48. } bits;
  49. } BYTE_VAL, BYTE_BITS;
  50.  
  51. typedef union _WORD_VAL
  52. {
  53. WORD Val;
  54. BYTE v[2];
  55. struct
  56. {
  57. BYTE LB;
  58. BYTE HB;
  59. } byte;
  60. struct
  61. {
  62. unsigned char b0:1;
  63. unsigned char b1:1;
  64. unsigned char b2:1;
  65. unsigned char b3:1;
  66. unsigned char b4:1;
  67. unsigned char b5:1;
  68. unsigned char b6:1;
  69. unsigned char b7:1;
  70. unsigned char b8:1;
  71. unsigned char b9:1;
  72. unsigned char b10:1;
  73. unsigned char b11:1;
  74. unsigned char b12:1;
  75. unsigned char b13:1;
  76. unsigned char b14:1;
  77. unsigned char b15:1;
  78. } bits;
  79. } WORD_VAL, WORD_BITS;
  80.  
  81. typedef union _DWORD_VAL
  82. {
  83. DWORD Val;
  84. WORD w[2];
  85. BYTE v[4];
  86. struct
  87. {
  88. WORD LW;
  89. WORD HW;
  90. } word;
  91. struct
  92. {
  93. BYTE LB;
  94. BYTE HB;
  95. BYTE UB;
  96. BYTE MB;
  97. } byte;
  98. struct
  99. {
  100. WORD_VAL low;
  101. WORD_VAL high;
  102. }wordUnion;
  103. struct
  104. {
  105. unsigned char b0:1;
  106. unsigned char b1:1;
  107. unsigned char b2:1;
  108. unsigned char b3:1;
  109. unsigned char b4:1;
  110. unsigned char b5:1;
  111. unsigned char b6:1;
  112. unsigned char b7:1;
  113. unsigned char b8:1;
  114. unsigned char b9:1;
  115. unsigned char b10:1;
  116. unsigned char b11:1;
  117. unsigned char b12:1;
  118. unsigned char b13:1;
  119. unsigned char b14:1;
  120. unsigned char b15:1;
  121. unsigned char b16:1;
  122. unsigned char b17:1;
  123. unsigned char b18:1;
  124. unsigned char b19:1;
  125. unsigned char b20:1;
  126. unsigned char b21:1;
  127. unsigned char b22:1;
  128. unsigned char b23:1;
  129. unsigned char b24:1;
  130. unsigned char b25:1;
  131. unsigned char b26:1;
  132. unsigned char b27:1;
  133. unsigned char b28:1;
  134. unsigned char b29:1;
  135. unsigned char b30:1;
  136. unsigned char b31:1;
  137. } bits;
  138. } DWORD_VAL;
  139.  
  140. #define LSB(a) ((a).v[0])
  141. #define MSB(a) ((a).v[1])
  142.  
  143. #define LOWER_LSB(a) ((a).v[0])
  144. #define LOWER_MSB(a) ((a).v[1])
  145. #define UPPER_LSB(a) ((a).v[2])
  146. #define UPPER_MSB(a) ((a).v[3])
  147.  
  148. typedef union _QWORD_VAL
  149. {
  150. QWORD Val;
  151. DWORD d[2];
  152. WORD w[4];
  153. BYTE v[8];
  154. struct
  155. {
  156. DWORD LD;
  157. DWORD HD;
  158. } dword;
  159. struct
  160. {
  161. WORD LW;
  162. WORD HW;
  163. WORD UW;
  164. WORD MW;
  165. } word;
  166. struct
  167. {
  168. unsigned char b0:1;
  169. unsigned char b1:1;
  170. unsigned char b2:1;
  171. unsigned char b3:1;
  172. unsigned char b4:1;
  173. unsigned char b5:1;
  174. unsigned char b6:1;
  175. unsigned char b7:1;
  176. unsigned char b8:1;
  177. unsigned char b9:1;
  178. unsigned char b10:1;
  179. unsigned char b11:1;
  180. unsigned char b12:1;
  181. unsigned char b13:1;
  182. unsigned char b14:1;
  183. unsigned char b15:1;
  184. unsigned char b16:1;
  185. unsigned char b17:1;
  186. unsigned char b18:1;
  187. unsigned char b19:1;
  188. unsigned char b20:1;
  189. unsigned char b21:1;
  190. unsigned char b22:1;
  191. unsigned char b23:1;
  192. unsigned char b24:1;
  193. unsigned char b25:1;
  194. unsigned char b26:1;
  195. unsigned char b27:1;
  196. unsigned char b28:1;
  197. unsigned char b29:1;
  198. unsigned char b30:1;
  199. unsigned char b31:1;
  200. unsigned char b32:1;
  201. unsigned char b33:1;
  202. unsigned char b34:1;
  203. unsigned char b35:1;
  204. unsigned char b36:1;
  205. unsigned char b37:1;
  206. unsigned char b38:1;
  207. unsigned char b39:1;
  208. unsigned char b40:1;
  209. unsigned char b41:1;
  210. unsigned char b42:1;
  211. unsigned char b43:1;
  212. unsigned char b44:1;
  213. unsigned char b45:1;
  214. unsigned char b46:1;
  215. unsigned char b47:1;
  216. unsigned char b48:1;
  217. unsigned char b49:1;
  218. unsigned char b50:1;
  219. unsigned char b51:1;
  220. unsigned char b52:1;
  221. unsigned char b53:1;
  222. unsigned char b54:1;
  223. unsigned char b55:1;
  224. unsigned char b56:1;
  225. unsigned char b57:1;
  226. unsigned char b58:1;
  227. unsigned char b59:1;
  228. unsigned char b60:1;
  229. unsigned char b61:1;
  230. unsigned char b62:1;
  231. unsigned char b63:1;
  232. } bits;
  233. } QWORD_VAL;
  234.  
  235.  
  236. // Section defining the address range to erase for the erase device command, along with the valid programming range to be reported by the QUERY_DEVICE command.
  237. #define VectorsStart 0x00000000
  238. // One page of vectors + general purpose program memory.
  239. // Bootloader resides in memory range 0x400-0x17FF
  240. #define VectorsEnd 0x00000400
  241. // Beginning of application program memory (not occupied by bootloader). **THIS VALUE MUST BE ALIGNED WITH BLOCK BOUNDRY** Also, in order to work correctly, make sure the StartPageToErase is set to erase this section.
  242. #define ProgramMemStart 0x00001400
  243.  
  244. //64KB variants (PIC24FJ64GB004)
  245.  
  246. // Bootloader and vectors occupy first six 1024 word (1536 bytes due to 25% unimplemented bytes) pages
  247. #define BeginPageToErase 5
  248. //Last full page of flash on the PIC24FJ256GB110, which does not contain the flash configuration words.
  249. #define MaxPageToEraseNoConfigs 41
  250. //Page 170 contains the flash configurations words on the PIC24FJ256GB110. Page 170 is also smaller than the rest of the (1536 byte) pages.
  251. #define MaxPageToEraseWithConfigs 42
  252. //Must be instruction word aligned address. This address does not get updated, but the one just below it does:
  253. //IE: If AddressToStopPopulating = 0x200, 0x1FF is the last programmed address (0x200 not programmed)
  254. #define ProgramMemStopNoConfigs 0x0000A800
  255.  
  256. //Must be instruction word aligned address. This address does not get updated, but the one just below it does: IE: If AddressToStopPopulating = 0x200, 0x1FF is the last programmed address (0x200 not programmed)
  257. #define ProgramMemStopWithConfigs 0x0000ABF8
  258. //0x2ABFA is start of CW3 on PIC24FJ256GB110 Family devices
  259. #define ConfigWordsStartAddress 0x0000ABF8
  260. #define ConfigWordsStopAddress 0x0000AC00
  261.  
  262.  
  263. //Switch State Variable Choices
  264. #define QUERY_DEVICE 0x02 //Command that the host uses to learn about the device (what regions can be programmed, and what type of memory is the region)
  265. #define UNLOCK_CONFIG 0x03 //Note, this command is used for both locking and unlocking the config bits (see the "//Unlock Configs Command Definitions" below)
  266. #define ERASE_DEVICE 0x04 //Host sends this command to start an erase operation. Firmware controls which pages should be erased.
  267. #define PROGRAM_DEVICE 0x05 //If host is going to send a full RequestDataBlockSize to be programmed, it uses this command.
  268. #define PROGRAM_COMPLETE 0x06 //If host send less than a RequestDataBlockSize to be programmed, or if it wished to program whatever was left in the buffer, it uses this command.
  269. #define GET_DATA 0x07 //The host sends this command in order to read out memory from the device. Used during verify (and read/export hex operations)
  270. #define RESET_DEVICE 0x08 //Resets the microcontroller, so it can update the config bits (if they were programmed, and so as to leave the bootloader (and potentially go back into the main application)
  271.  
  272. //Unlock Configs Command Definitions
  273. #define UNLOCKCONFIG 0x00 //Sub-command for the ERASE_DEVICE command
  274. #define LOCKCONFIG 0x01 //Sub-command for the ERASE_DEVICE command
  275.  
  276. //Query Device Response "Types"
  277. #define TypeProgramMemory 0x01 //When the host sends a QUERY_DEVICE command, need to respond by populating a list of valid memory regions that exist in the device (and should be programmed)
  278. #define TypeEEPROM 0x02
  279. #define TypeConfigWords 0x03
  280. #define TypeEndOfTypeList 0xFF //Sort of serves as a "null terminator" like number, which denotes the end of the memory region list has been reached.
  281.  
  282. //BootState Variable States
  283. #define IdleState 0x00
  284. #define NotIdleState 0x01
  285.  
  286. //OtherConstants
  287. #define InvalidAddress 0xFFFFFFFF
  288.  
  289. //Application and Microcontroller constants
  290. #define BytesPerFlashAddress 0x02 //For Flash memory: One byte per address on PIC18, two bytes per address on PIC24
  291.  
  292.  
  293. #define TotalPacketSize 0x40
  294. #define WORDSIZE 0x02 //PIC18 uses 2 byte instruction words, PIC24 uses 3 byte "instruction words" (which take 2 addresses, since each address is for a 16 bit word; the upper word contains a "phantom" byte which is unimplemented.).
  295. //#define FlashBlockSize 0x40 //For PIC18F87J50 family devices, a flash block is 64 bytes
  296. #define RequestDataBlockSize 56 //Number of bytes in the "Data" field of a standard request to/from the PC. Must be an even number from 2 to 56.
  297. #define BufferSize 0x20 //32 16-bit words of buffer
  298.  
  299.  
  300. typedef union __attribute__ ((packed)) _USB_HID_BOOTLOADER_COMMAND
  301. {
  302. struct __attribute__ ((packed)) {
  303. BYTE Command;
  304. DWORD Address:32;
  305. BYTE Size;
  306. BYTE PadBytes[(TotalPacketSize - 6) - (RequestDataBlockSize)];
  307. UINT Data[RequestDataBlockSize/WORDSIZE];
  308. };
  309.  
  310. BYTE Contents[64];
  311.  
  312. } PacketToFromPC;
  313.  
  314.  
  315. int main(void) {
  316.  
  317. int i;
  318. BYTE data[64] = {0x05, 0x00, 0x24, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0xD0, 0x02, 0x0E, 0xDC, 0xFF, 0x80, 0xA4, 0xDB, 0xFF, 0xFE, 0x76, 0x77, 0xFF, 0xFF, 0xFF, 0xFF, 0xF3, 0xF3, 0xF8, 0xF8, 0x1F, 0xA7, 0x65, 0xDD, 0xEE, 0xEE, 0xDF, 0xDF, 0xEE, 0xEE, 0xF8, 0xF8, 0xFF, 0xFF, 0xDF, 0xDF, 0xFF, 0xFF, 0x1F, 0x1F, 0xFD, 0xFD, 0xCD, 0xCD, 0xFF, 0xFF, 0xFD, 0xFD};
  319. PacketToFromPC PacketFromPC;
  320.  
  321. for(i = 0; i < 64; i++) {
  322. PacketFromPC.Contents[i] = data[i];
  323. }
  324.  
  325. printf("%d", PacketFromPC.Address);
  326.  
  327. // your code goes here
  328. return 0;
  329. }
  330.  
Success #stdin #stdout 0s 5544KB
stdin
Standard input is empty
stdout
9216