Port of flash_cc2531 to FreeBSD. This is likely more just include a wiringPi compatible library for FreeBSD. Any new files are BSD licensed and NOT GPLv3 license.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

333 lines
9.5 KiB

  1. /***********************************************************************
  2. Copyright © 2019 Jean Michault.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. *************************************************************************/
  14. #include <wiringPi.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <stdbool.h>
  18. #include <string.h>
  19. #include <stdint.h>
  20. #include <unistd.h>
  21. #include "CCDebugger.h"
  22. uint8_t buffer[601];
  23. uint8_t data[260];
  24. uint8_t buf1[1024];
  25. uint8_t buf2[1024];
  26. struct page
  27. {
  28. uint32_t minoffset,maxoffset;
  29. uint8_t datas[2048];
  30. } Pages[128];
  31. void readXDATA(uint16_t offset,uint8_t *bytes, int len)
  32. {
  33. cc_execi(0x90, offset ); //MOV DPTR,#data16
  34. for ( int i=0 ; i<len;i++)
  35. {
  36. bytes[i] = cc_exec(0xE0); //MOVX A,@DPTR
  37. cc_exec(0xA3); // INC DPTR
  38. }
  39. }
  40. void writeXDATA(uint16_t offset,uint8_t *bytes, int len)
  41. {
  42. cc_execi(0x90,offset); //MOV DPTR,#data16
  43. for ( int i=0 ; i<len;i++)
  44. {
  45. cc_exec2(0x74,bytes[i]); // MOV A,#data
  46. cc_exec(0xF0); //MOVX @DPTR,A
  47. cc_exec(0xA3); // INC DPTR
  48. }
  49. }
  50. void readPage(int page,uint8_t *buf)
  51. {
  52. uint8_t bank=page>>4;
  53. // get FMAP
  54. uint8_t res = cc_exec2(0xE5, 0xC7);
  55. // select bank
  56. res = (res & 0xF8) | (bank & 0x07);
  57. res = cc_exec3(0x75, 0xC7, res); // MOV direct,#data
  58. // calculer l'adresse de destination
  59. uint32_t offset = ((page&0xf)<<11) + Pages[page].minoffset;
  60. // Setup DPTR
  61. cc_execi( 0x90, 0x8000+offset ); // MOV DPTR,#data16
  62. for(int i=0 ; i<2048 ;i++)
  63. {
  64. res = cc_exec ( 0xE0 ); // MOVX A,@DPTR
  65. buf[i] = res;
  66. res = cc_exec ( 0xA3 ); // INC DPTR
  67. }
  68. }
  69. uint8_t verif1[2048];
  70. uint8_t verif2[2048];
  71. int verifPage(int page)
  72. {
  73. do
  74. {
  75. readPage(page,verif1);
  76. readPage(page,verif2);
  77. } while (memcmp(verif1,verif2,2048));
  78. for(int i=Pages[page].minoffset ; i<Pages[page].maxoffset ;i++)
  79. {
  80. if(verif1[i] != Pages[page].datas[i])
  81. {
  82. printf("\nerror at 0x%x, 0x%x instead of 0x%x\n",i,verif1[i],Pages[page].datas[i]);
  83. return 1;
  84. }
  85. }
  86. return 0;
  87. }
  88. int writePage(int page)
  89. {
  90. uint8_t bank=page>>4;
  91. // get FMAP
  92. uint8_t res = cc_exec2(0xE5, 0xC7);
  93. // select bank
  94. res = (res & 0xF8) | (bank & 0x07);
  95. res = cc_exec3(0x75, 0xC7, res); // MOV direct,#data
  96. // calculer l'adresse de destination
  97. uint32_t offset = ((page&0xf)<<11) + Pages[page].minoffset;
  98. uint32_t len = Pages[page].maxoffset-Pages[page].minoffset+1;
  99. // configure DMA-0 pour DEBUG --> RAM
  100. uint8_t dma_desc0[8];
  101. dma_desc0[0] = 0x62;// src[15:8]
  102. dma_desc0[1] = 0x60;// src[7:0]
  103. dma_desc0[2] = 0x00;// dest[15:8]
  104. dma_desc0[3] = 0x00;// dest[7:0]
  105. dma_desc0[4] = (len>>8)&0xff;
  106. dma_desc0[5] = (len&0xff);
  107. dma_desc0[6] = 0x1f; //wordsize=0,tmode=0,trig=0x1F
  108. dma_desc0[7] = 0x19;//srcinc=0,destinc=1,irqmask=1,m8=0,priority=1
  109. writeXDATA( 0x1000, dma_desc0, 8 );
  110. cc_exec3( 0x75, 0xD4, 0x00);
  111. cc_exec3( 0x75, 0xD5, 0x10);
  112. // configure DMA-1 pour RAM --> FLASH
  113. uint8_t dma_desc1[8];
  114. dma_desc1[0] = 0x00;// src[15:8]
  115. dma_desc1[1] = 0x00;// src[7:0]
  116. dma_desc1[2] = 0x62;// dest[15:8]
  117. dma_desc1[3] = 0x73;// dest[7:0]
  118. dma_desc1[4] = (len>>8)&0xff;
  119. dma_desc1[5] = (len&0xff);
  120. dma_desc1[6] = 0x12; //wordsize=0,tmode=0,trig=0x12
  121. dma_desc1[7] = 0x42;//srcinc=1,destinc=0,irqmask=1,m8=0,priority=2
  122. writeXDATA( 0x1008, dma_desc1, 8 );
  123. cc_exec3( 0x75, 0xD2, 0x08);
  124. cc_exec3( 0x75, 0xD3, 0x10);
  125. // clear flash status
  126. readXDATA(0x6270, &res, 1);
  127. res &=0x1F;
  128. writeXDATA(0x6270, &res, 1);
  129. // clear DMAIRQ 0 et 1
  130. res = cc_exec2(0xE5, 0xD1);
  131. res &= ~1;
  132. res &= ~2;
  133. cc_exec3(0x75,0xD1,res);
  134. // disarm DMA Channel 0 et 1
  135. res = cc_exec2(0xE5, 0xD6);
  136. res &= ~1;
  137. res &= ~2;
  138. cc_exec3(0x75,0xD6,res);
  139. // Upload to RAM through DMA-0
  140. // arm DMA channel 0 :
  141. res = cc_exec2(0xE5, 0xD6);
  142. res |= 1;
  143. cc_exec3(0x75,0xD6,res);
  144. cc_delay(200);
  145. // transfert de données en mode burst
  146. cc_write(0x80|( (len>>8)&0x7) );
  147. cc_write(len&0xff);
  148. for(int i=0 ; i<len ;i++)
  149. cc_write(Pages[page].datas[i+Pages[page].minoffset]);
  150. // wait DMA end :
  151. do
  152. {
  153. cc_delay(100);
  154. res = cc_exec2(0xE5, 0xD1);
  155. res &= 1;
  156. } while (res==0);
  157. // Clear DMA IRQ flag
  158. res = cc_exec2(0xE5, 0xD1);
  159. res &= ~1;
  160. cc_exec3(0x75,0xD1,res);
  161. // disarm DMA Channel 1
  162. res = cc_exec2(0xE5, 0xD6);
  163. res &= ~2;
  164. cc_exec3(0x75,0xD6,res);
  165. // écrire l'adresse de destination dans FADDRH FADDRL
  166. offset = page<<11 + Pages[page].minoffset;
  167. res=(offset>>2)&0xff;
  168. writeXDATA( 0x6271, &res,1);
  169. res=(offset>>10)&0xff;
  170. writeXDATA( 0x6272, &res,1);
  171. // arm DMA channel 1 :
  172. res = cc_exec2(0xE5, 0xD6);
  173. res |= 2;
  174. cc_exec3(0x75,0xD6,res);
  175. cc_delay(200);
  176. // lancer la copie vers la FLASH
  177. readXDATA(0x6270, &res, 1);
  178. res |= 2;
  179. writeXDATA(0x6270, &res, 1);
  180. // wait DMA end :
  181. do
  182. {
  183. sleep(1);
  184. res = cc_exec2(0xE5, 0xD1);
  185. res &= 2;
  186. } while (res==0);
  187. // vérifie qu'il n'y a pas eu de flash abort
  188. readXDATA(0x6270, &res, 1);
  189. if (res&0x20)
  190. {
  191. fprintf(stderr," flash error !!!\n");
  192. exit(1);
  193. }
  194. }
  195. int main(int argc,char **argv)
  196. {
  197. if( argc <2 ) { fprintf(stderr,"usage : %s file_to_flash\n",argv[0]); exit(1); }
  198. FILE * ficin = fopen(argv[1],"r");
  199. if(!ficin) { fprintf(stderr," Can't open file %s.\n",argv[1]); exit(1); }
  200. // on initialise les ports GPIO et le debugger
  201. cc_init(24,27,28);
  202. // entrée en mode debug
  203. cc_enter();
  204. // envoi de la commande getChipID :
  205. uint16_t ID;
  206. ID = cc_getChipID();
  207. printf(" ID = %04x.\n",ID);
  208. for (int page=0 ; page<128 ; page++)
  209. {
  210. memset(Pages[page].datas,0xff,2048);
  211. Pages[page].minoffset=0xffff;
  212. Pages[page].maxoffset=0;
  213. }
  214. uint16_t ela=0; // extended linear address
  215. uint32_t sla=0; // start linear address
  216. // read hex file
  217. int line=0;
  218. int maxpage=0;
  219. while(fgets(buffer,600,ficin))
  220. {
  221. int sum=0,cksum,type;
  222. uint32_t addr,len;
  223. line++;
  224. if(line%10==0) { printf("\r reading line %d.");fflush(stdout); }
  225. if(buffer[0] != ':') { fprintf(stderr,"incorrect hex file ( : missing)\n"); exit(1); }
  226. if(strlen(buffer)<3 ) { fprintf(stderr,"incorrect hex file ( incomplete line)\n"); exit(1); }
  227. if(!sscanf(buffer+1,"%02x",&len)) { fprintf(stderr,"incorrect hex file (incorrect length\n"); exit(1); }
  228. if(strlen(buffer)<(11 + (len * 2))) { fprintf(stderr,"incorrect hex file ( incomplete line)\n"); exit(1); }
  229. if(!sscanf(buffer+3,"%04x",&addr)) { fprintf(stderr,"incorrect hex file (incorrect addr)\n"); exit(1); }
  230. if(!sscanf(buffer+7,"%02x",&type)) { fprintf(stderr,"incorrect hex file (incorrect record type\n"); exit(1); }
  231. if(type == 4)
  232. {
  233. if(!sscanf(buffer+9,"%04x",&ela)) { fprintf(stderr,"incorrect hex file (incorrect extended addr)\n"); exit(1); }
  234. sla=ela<<16;
  235. continue;
  236. }
  237. if(type == 5)
  238. {
  239. if(!sscanf(buffer+9,"%08x",&sla)) { fprintf(stderr,"incorrect hex file (incorrect extended addr)\n"); exit(1); }
  240. ela = sla>>16;
  241. continue;
  242. }
  243. if(type==1) // EOF
  244. {
  245. break;
  246. }
  247. if(type) { fprintf(stderr,"incorrect hex file (record type %d not implemented\n",type); exit(1); }
  248. sum = (len & 255) + ((addr >> 8) & 255) + (addr & 255) + (type & 255);
  249. int i;
  250. for( i=0 ; i<len ; i++)
  251. {
  252. if(!sscanf(buffer+9+2*i,"%02x",&data[i])) { fprintf(stderr,"incorrect hex file (incorrect data)\n"); exit(1); }
  253. sum+=data[i];
  254. }
  255. if(!sscanf(buffer+9+2*i,"%02x",&cksum)) { fprintf(stderr,"incorrect hex file line %d (incorrect checksum)\n",line); exit(1); }
  256. if ( ((sum & 255) + (cksum & 255)) & 255 ) { fprintf(stderr,"incorrect hex file line %d (bad checksum) %x %x\n",line,(-sum)&255,cksum); exit(1); }
  257. // stock datas
  258. int page= (sla+addr)>>11;
  259. if (page>maxpage) maxpage=page;
  260. uint16_t start=(sla+addr)&0x7ff;
  261. if(start+len> 2048)
  262. {
  263. if (page+1>maxpage) maxpage=page+1;
  264. memcpy(&Pages[page+1].datas[0]
  265. ,data,(start+len-2048));
  266. if(0 < Pages[page].minoffset) Pages[page].minoffset=0;
  267. if( (start+len-2048-1) > Pages[page].maxoffset) Pages[page].maxoffset=start+len-2048-1;
  268. len=2048-start;
  269. }
  270. memcpy(&Pages[page].datas[start]
  271. ,data,len);
  272. if(start < Pages[page].minoffset) Pages[page].minoffset=start;
  273. if( (start+len-1) > Pages[page].maxoffset) Pages[page].maxoffset=start+len-1;
  274. }
  275. printf("\n file loaded (%d lines read).\n",line);
  276. // activer DMA
  277. uint8_t conf=cc_getConfig();
  278. conf &= ~0x4;
  279. cc_setConfig(conf);
  280. for (int page=0 ; page <= maxpage ; page++)
  281. {
  282. if(Pages[page].maxoffset<Pages[page].minoffset) continue;
  283. printf("\rwriting page %3d/%3d.",page+1,maxpage+1);
  284. fflush(stdout);
  285. writePage(page);
  286. }
  287. printf("\n");
  288. // lire les données et les vérifier
  289. int badPage=0;
  290. for (int page=0 ; page <= maxpage ; page++)
  291. {
  292. if(Pages[page].maxoffset<Pages[page].minoffset) continue;
  293. printf("\rverifying page %3d/%3d.",page+1,maxpage+1);
  294. fflush(stdout);
  295. badPage += verifPage(page);
  296. }
  297. printf("\n");
  298. if (!badPage)
  299. printf(" flash OK.\n");
  300. else
  301. printf(" Errors found in %d pages.\n",badPage);
  302. // sortie du mode debug et désactivation :
  303. cc_setActive(false);
  304. fclose(ficin);
  305. }