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.
 
 
 

152 lines
4.0 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. void writeHexLine(FILE * fic,uint8_t *buf, int len,int offset)
  23. {
  24. int i=0;
  25. for(i=0 ; i<len ; i++)
  26. if(buf[i] != 0xff) break;
  27. if(i==len) return;
  28. int sum=len+(offset&0xff)+((offset>>8)&0xff);
  29. fprintf(fic,":%02X%04X00",len,offset);
  30. for(int i=0 ; i<len;i++)
  31. {
  32. fprintf(fic,"%02X",buf[i]);
  33. sum += buf[i];
  34. }
  35. fprintf(fic,"%02X\n",(-sum)&0xff);
  36. }
  37. uint8_t buf1[1024];
  38. uint8_t buf2[1024];
  39. void read1k(int bank,uint16_t offset,uint8_t * buf)
  40. {
  41. // get FMAP
  42. uint8_t res = cc_exec2(0xE5, 0xC7);
  43. // select bank
  44. res = (res & 0xF8) | (bank & 0x07);
  45. res = cc_exec3(0x75, 0xC7, res); // MOV direct,#data
  46. // Setup DPTR
  47. cc_execi( 0x90, 0x8000+offset ); // MOV DPTR,#data16
  48. for(int i=0 ; i<1024 ;i++)
  49. {
  50. res = cc_exec ( 0xE0 ); // MOVX A,@DPTR
  51. buf[i] = res;
  52. res = cc_exec ( 0xA3 ); // INC DPTR
  53. }
  54. }
  55. void helpo()
  56. {
  57. fprintf(stderr,"usage : cc_read [-d pin_DD] [-c pin_DC] [-r pin_reset] out_file\n");
  58. fprintf(stderr," -c : change pin_DC (default 27)\n");
  59. fprintf(stderr," -d : change pin_DD (default 28)\n");
  60. fprintf(stderr," -r : change reset pin (default 24)\n");
  61. fprintf(stderr," -m : change multiplier for time delay (default auto)\n");
  62. }
  63. int main(int argc,char *argv[])
  64. {
  65. int opt;
  66. int rePin=-1;
  67. int dcPin=-1;
  68. int ddPin=-1;
  69. int setMult=-1;
  70. while( (opt=getopt(argc,argv,"m:d:c:r:h?")) != -1)
  71. {
  72. switch(opt)
  73. {
  74. case 'm' :
  75. setMult=atoi(optarg);
  76. break;
  77. case 'd' : // DD pinglo
  78. ddPin=atoi(optarg);
  79. break;
  80. case 'c' : // DC pinglo
  81. dcPin=atoi(optarg);
  82. break;
  83. case 'r' : // restarigi pinglo
  84. rePin=atoi(optarg);
  85. break;
  86. case 'h' : // helpo
  87. case '?' : // helpo
  88. helpo();
  89. exit(0);
  90. break;
  91. }
  92. }
  93. if( optind >= argc ) { helpo(); exit(1); }
  94. FILE * ficout = fopen(argv[optind],"w");
  95. if(!ficout) { fprintf(stderr," Can't open file %s.\n",argv[optind]); exit(1); }
  96. // initialize GPIO ports
  97. cc_init(rePin,dcPin,ddPin);
  98. if(setMult>0) cc_setmult(setMult);
  99. // enter debug mode
  100. cc_enter();
  101. // get ChipID :
  102. uint16_t ID;
  103. ID = cc_getChipID();
  104. printf(" ID = %04x.\n",ID);
  105. uint16_t offset=0;
  106. uint8_t bank=0;
  107. int progress=1;
  108. // int nbread=0;
  109. for( bank=0 ; bank<8 ; bank++)
  110. {
  111. printf(".");fflush(stdout);
  112. if(! (bank&1))
  113. {
  114. uint8_t sum=2+4+(bank/2);
  115. fprintf(ficout,":02000004%04X%02X\n",bank/2,(-sum)&255 );
  116. }
  117. offset=0;
  118. for ( uint16_t i=0 ; i<32 ; i++ )
  119. {
  120. do
  121. {
  122. read1k(bank,i*1024, buf1);
  123. read1k(bank,i*1024, buf2);
  124. // nbread++;
  125. //if(memcmp(buf1,buf2,1024))
  126. // {printf("x");fflush(stdout);}
  127. } while(memcmp(buf1,buf2,1024));
  128. for(uint16_t j=0 ; j<64 ; j++)
  129. writeHexLine(ficout,buf1+j*16, 16,(bank&1)*32*1024+ i*1024+j*16);
  130. printf("\r reading %dk/256k",progress++);fflush(stdout);
  131. }
  132. }
  133. // fprintf(stderr,"nbread=%d\n",nbread);
  134. fprintf(ficout,":00000001FF\n");
  135. // exit from debug
  136. cc_setActive(false);
  137. // reboot
  138. cc_reset();
  139. fclose(ficout);
  140. }