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.
 
 
 

784 lines
15 KiB

  1. /***********************************************************************
  2. * Copyright (c) 2014-2016 Ioannis Charalampidis
  3. * Copyright (c) 2015 Simon Schulz - github.com/fishpepper
  4. Copyright © 2019 Jean Michault.
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. *************************************************************************/
  16. #include <wiringPi.h>
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <stdbool.h>
  21. #include <time.h>
  22. #include "CCDebugger.h"
  23. /**
  24. * Switch reset pin
  25. */
  26. void cc_setDDDirection( uint8_t direction );
  27. /**
  28. * Software-overridable instruction table that can be used
  29. * for supporting other CCDebug-Compatible chips purely by software
  30. */
  31. uint8_t instr[16];
  32. /**
  33. * Local properties
  34. */
  35. int pinRST= PIN_RST;
  36. int pinDC= PIN_DC;
  37. int pinDD= PIN_DD;
  38. uint8_t errorFlag=0;
  39. uint8_t ddIsOutput=false;
  40. uint8_t inDebugMode=false;
  41. uint8_t cc_active=false;
  42. /**
  43. * Instruction table indices
  44. */
  45. #define INSTR_VERSION 0
  46. #define I_HALT 1
  47. #define I_RESUME 2
  48. #define I_RD_CONFIG 3
  49. #define I_WR_CONFIG 4
  50. #define I_DEBUG_INSTR_1 5
  51. #define I_DEBUG_INSTR_2 6
  52. #define I_DEBUG_INSTR_3 7
  53. #define I_GET_CHIP_ID 8
  54. #define I_GET_PC 9
  55. #define I_READ_STATUS 10
  56. #define I_STEP_INSTR 11
  57. #define I_CHIP_ERASE 12
  58. #define I_SET_HW_BRKPNT 13
  59. #define I_GET_BM 14
  60. #define I_BURST_WRITE 15
  61. void cc_delay_calibrate( );
  62. int cc_init( int pRST, int pDC, int pDD )
  63. {
  64. if(pRST>=0) pinRST=pRST;
  65. if(pDC>=0) pinDC=pDC;
  66. if(pDD>=0) pinDD=pDD;
  67. if(wiringPiSetup() == -1){
  68. printf("no wiring pi detected\n");
  69. return 0;
  70. }
  71. cc_delay_calibrate();
  72. // Prepare CC Pins
  73. pinMode(pinDC, OUTPUT);
  74. pinMode(pinDD, OUTPUT);
  75. pinMode(pinRST, OUTPUT);
  76. digitalWrite(pinDC, LOW);
  77. digitalWrite(pinDD, LOW);
  78. digitalWrite(pinRST, LOW);
  79. // Prepare default direction
  80. cc_setDDDirection(INPUT);
  81. // Default CCDebug instruction set for CC254x
  82. instr[INSTR_VERSION] = 1;
  83. instr[I_HALT] = 0x40;
  84. instr[I_RESUME] = 0x48;
  85. instr[I_RD_CONFIG] = 0x20;
  86. instr[I_WR_CONFIG] = 0x18;
  87. instr[I_DEBUG_INSTR_1] = 0x51;
  88. instr[I_DEBUG_INSTR_2] = 0x52;
  89. instr[I_DEBUG_INSTR_3] = 0x53;
  90. instr[I_GET_CHIP_ID] = 0x68;
  91. instr[I_GET_PC] = 0x28;
  92. instr[I_READ_STATUS] = 0x30;
  93. instr[I_STEP_INSTR] = 0x58;
  94. instr[I_CHIP_ERASE] = 0x10;
  95. // We are active by default
  96. cc_active = true;
  97. };
  98. /**
  99. * Activate/Deactivate debugger
  100. */
  101. void cc_setActive( uint8_t on )
  102. {
  103. // Reset error flag
  104. errorFlag = CC_ERROR_NONE;
  105. // Continue only if active
  106. if (on == cc_active) return;
  107. cc_active = on;
  108. if (on) {
  109. // Prepare CC Pins
  110. pinMode(pinDC, OUTPUT);
  111. pinMode(pinDD, OUTPUT);
  112. pinMode(pinRST, OUTPUT);
  113. digitalWrite(pinDC, LOW);
  114. digitalWrite(pinDD, LOW);
  115. digitalWrite(pinRST, LOW);
  116. // Default direction is INPUT
  117. cc_setDDDirection(INPUT);
  118. } else {
  119. // Before deactivating, exit debug mode
  120. if (inDebugMode)
  121. cc_exit();
  122. // Put everything in inactive mode
  123. pinMode(pinDC, INPUT);
  124. pinMode(pinDD, INPUT);
  125. pinMode(pinRST, INPUT);
  126. digitalWrite(pinDC, LOW);
  127. digitalWrite(pinDD, LOW);
  128. digitalWrite(pinRST, LOW);
  129. }
  130. }
  131. /**
  132. * Return the error flag
  133. */
  134. uint8_t cc_error()
  135. {
  136. return errorFlag;
  137. }
  138. /////////////////////////////////////////////////////////////////////
  139. /////////////////////////////////////////////////////////////////////
  140. //// LOW LEVEL FUNCTIONS ////
  141. /////////////////////////////////////////////////////////////////////
  142. /////////////////////////////////////////////////////////////////////
  143. /**
  144. * Delay a particular number of cycles
  145. */
  146. struct timespec tp={0,0};
  147. static int cc_delay_mult=50;
  148. void cc_delay( unsigned char d )
  149. {
  150. volatile unsigned int i = cc_delay_mult*d;
  151. while( i-- );
  152. //tp.tv_nsec=40*d;
  153. //nanosleep(&tp,NULL);
  154. }
  155. /* provas konsideri la rapidecon de la procesoro */
  156. void cc_delay_calibrate( )
  157. {
  158. long time0=micros();
  159. cc_delay(200);
  160. cc_delay(200);
  161. cc_delay(200);
  162. cc_delay(200);
  163. cc_delay(200);
  164. long time1=micros();
  165. cc_delay_mult=cc_delay_mult*200/(time1-time0);
  166. }
  167. /**
  168. * Enter debug mode
  169. */
  170. uint8_t cc_enter()
  171. {
  172. if (!cc_active) {
  173. errorFlag = CC_ERROR_NOT_ACTIVE;
  174. return 0;
  175. }
  176. // =============
  177. // Reset error flag
  178. errorFlag = CC_ERROR_NONE;
  179. // Enter debug mode
  180. digitalWrite(pinRST, LOW);
  181. cc_delay(200);
  182. digitalWrite(pinDC, HIGH);
  183. cc_delay(3);
  184. digitalWrite(pinDC, LOW);
  185. cc_delay(3);
  186. digitalWrite(pinDC, HIGH);
  187. cc_delay(3);
  188. digitalWrite(pinDC, LOW);
  189. cc_delay(4);
  190. digitalWrite(pinRST, HIGH);
  191. cc_delay(200);
  192. // We are now in debug mode
  193. inDebugMode = 1;
  194. // =============
  195. // Success
  196. return 0;
  197. };
  198. /**
  199. * Write a uint8_t to the debugger
  200. */
  201. uint8_t cc_write( uint8_t data )
  202. {
  203. if (!cc_active) {
  204. errorFlag = CC_ERROR_NOT_ACTIVE;
  205. return 0;
  206. };
  207. if (!inDebugMode) {
  208. errorFlag = CC_ERROR_NOT_DEBUGGING;
  209. return 0;
  210. }
  211. // =============
  212. uint8_t cnt;
  213. // Make sure DD is on output
  214. cc_setDDDirection(OUTPUT);
  215. // Sent uint8_ts
  216. for (cnt = 8; cnt; cnt--) {
  217. // First put data bit on bus
  218. if (data & 0x80)
  219. digitalWrite(pinDD, HIGH);
  220. else
  221. digitalWrite(pinDD, LOW);
  222. // Place clock on high (other end reads data)
  223. digitalWrite(pinDC, HIGH);
  224. // Shift & Delay
  225. data <<= 1;
  226. cc_delay(2);
  227. // Place clock down
  228. digitalWrite(pinDC, LOW);
  229. cc_delay(2);
  230. }
  231. // =============
  232. return 0;
  233. }
  234. /**
  235. * Wait until input is ready for reading
  236. */
  237. uint8_t cc_switchRead(uint8_t maxWaitCycles)
  238. {
  239. if (!cc_active) {
  240. errorFlag = CC_ERROR_NOT_ACTIVE;
  241. return 0;
  242. }
  243. if (!inDebugMode) {
  244. errorFlag = CC_ERROR_NOT_DEBUGGING;
  245. return 0;
  246. }
  247. // =============
  248. uint8_t cnt;
  249. uint8_t didWait = 0;
  250. // Switch to input
  251. cc_setDDDirection(INPUT);
  252. // Wait at least 83 ns before checking state t(dir_change)
  253. cc_delay(2);
  254. // Wait for DD to go LOW (Chip is READY)
  255. while (digitalRead(pinDD) == HIGH) {
  256. // Do 8 clock cycles
  257. for (cnt = 8; cnt; cnt--) {
  258. digitalWrite(pinDC, HIGH);
  259. cc_delay(2);
  260. digitalWrite(pinDC, LOW);
  261. cc_delay(2);
  262. }
  263. // Let next function know that we did wait
  264. didWait = 1;
  265. // Check if we ran out if wait cycles
  266. if (!--maxWaitCycles) {
  267. // If we are waiting for too long, we have lost the chip,
  268. // so also assume we are out of debugging mode
  269. errorFlag = CC_ERROR_NOT_WIRED;
  270. inDebugMode = 0;
  271. return 0;
  272. }
  273. }
  274. // Wait t(sample_wait)
  275. if (didWait) cc_delay(2);
  276. // =============
  277. return 0;
  278. }
  279. /**
  280. * Switch to output
  281. */
  282. uint8_t cc_switchWrite()
  283. {
  284. cc_setDDDirection(OUTPUT);
  285. return 0;
  286. }
  287. /**
  288. * Read an input uint8_t
  289. */
  290. uint8_t cc_read()
  291. {
  292. if (!cc_active) {
  293. errorFlag = CC_ERROR_NOT_ACTIVE;
  294. return 0;
  295. }
  296. // =============
  297. uint8_t cnt;
  298. uint8_t data = 0;
  299. // Switch to input
  300. cc_setDDDirection(INPUT);
  301. // Send 8 clock pulses if we are HIGH
  302. for (cnt = 8; cnt; cnt--) {
  303. digitalWrite(pinDC, HIGH);
  304. cc_delay(2);
  305. // Shift and read
  306. data <<= 1;
  307. if (digitalRead(pinDD) == HIGH)
  308. data |= 0x01;
  309. digitalWrite(pinDC, LOW);
  310. cc_delay(2);
  311. }
  312. // =============
  313. return data;
  314. }
  315. /**
  316. * Switch reset pin
  317. */
  318. void cc_setDDDirection( uint8_t direction )
  319. {
  320. // Switch direction if changed
  321. if (direction == ddIsOutput) return;
  322. ddIsOutput = direction;
  323. // Handle new direction
  324. if (ddIsOutput) {
  325. digitalWrite(pinDD, LOW); // Disable pull-up
  326. pinMode(pinDD, OUTPUT); // Enable output
  327. digitalWrite(pinDD, LOW); // Switch to low
  328. } else {
  329. digitalWrite(pinDD, LOW); // Disable pull-up
  330. pinMode(pinDD, INPUT); // Disable output
  331. digitalWrite(pinDD, LOW); // Don't use output pull-up
  332. }
  333. }
  334. /////////////////////////////////////////////////////////////////////
  335. /////////////////////////////////////////////////////////////////////
  336. //// HIGH LEVEL FUNCTIONS ////
  337. /////////////////////////////////////////////////////////////////////
  338. /////////////////////////////////////////////////////////////////////
  339. /**
  340. * Exit from debug mode
  341. */
  342. uint8_t cc_exit()
  343. {
  344. if (!cc_active) {
  345. errorFlag = CC_ERROR_NOT_ACTIVE;
  346. return 0;
  347. }
  348. if (!inDebugMode) {
  349. errorFlag = CC_ERROR_NOT_DEBUGGING;
  350. return 0;
  351. }
  352. uint8_t bAns;
  353. cc_write( instr[I_RESUME] ); // RESUME
  354. cc_switchRead(250);
  355. bAns = cc_read(); // debug status
  356. cc_switchWrite();
  357. inDebugMode = 0;
  358. return 0;
  359. }
  360. /**
  361. * Get debug configuration
  362. */
  363. uint8_t cc_getConfig() {
  364. if (!cc_active) {
  365. errorFlag = CC_ERROR_NOT_ACTIVE;
  366. return 0;
  367. }
  368. if (!inDebugMode) {
  369. errorFlag = CC_ERROR_NOT_DEBUGGING;
  370. return 0;
  371. }
  372. uint8_t bAns;
  373. cc_write( instr[I_RD_CONFIG] ); // RD_CONFIG
  374. cc_switchRead(250);
  375. bAns = cc_read(); // Config
  376. cc_switchWrite();
  377. return bAns;
  378. }
  379. /**
  380. * Set debug configuration
  381. */
  382. uint8_t cc_setConfig( uint8_t config ) {
  383. if (!cc_active) {
  384. errorFlag = CC_ERROR_NOT_ACTIVE;
  385. return 0;
  386. }
  387. if (!inDebugMode) {
  388. errorFlag = CC_ERROR_NOT_DEBUGGING;
  389. return 0;
  390. }
  391. uint8_t bAns;
  392. cc_write( instr[I_WR_CONFIG] ); // WR_CONFIG
  393. cc_write( config );
  394. cc_switchRead(250);
  395. bAns = cc_read(); // Config
  396. cc_switchWrite();
  397. return bAns;
  398. }
  399. /**
  400. * Invoke a debug instruction with 1 opcode
  401. */
  402. uint8_t cc_exec( uint8_t oc0 )
  403. {
  404. if (!cc_active) {
  405. errorFlag = CC_ERROR_NOT_ACTIVE;
  406. return 0;
  407. }
  408. if (!inDebugMode) {
  409. errorFlag = CC_ERROR_NOT_DEBUGGING;
  410. return 0;
  411. }
  412. uint8_t bAns;
  413. cc_write( instr[I_DEBUG_INSTR_1] ); // DEBUG_INSTR + 1b
  414. cc_write( oc0 );
  415. cc_switchRead(250);
  416. bAns = cc_read(); // Accumulator
  417. cc_switchWrite();
  418. return bAns;
  419. }
  420. /**
  421. * Invoke a debug instruction with 2 opcodes
  422. */
  423. uint8_t cc_exec2( uint8_t oc0, uint8_t oc1 )
  424. {
  425. if (!cc_active) {
  426. errorFlag = CC_ERROR_NOT_ACTIVE;
  427. return 0;
  428. }
  429. if (!inDebugMode) {
  430. errorFlag = CC_ERROR_NOT_DEBUGGING;
  431. return 0;
  432. }
  433. uint8_t bAns;
  434. cc_write( instr[I_DEBUG_INSTR_2] ); // DEBUG_INSTR + 2b
  435. cc_write( oc0 );
  436. cc_write( oc1 );
  437. cc_switchRead(250);
  438. bAns = cc_read(); // Accumulator
  439. cc_switchWrite();
  440. return bAns;
  441. }
  442. /**
  443. * Invoke a debug instruction with 3 opcodes
  444. */
  445. uint8_t cc_exec3( uint8_t oc0, uint8_t oc1, uint8_t oc2 )
  446. {
  447. if (!cc_active) {
  448. errorFlag = CC_ERROR_NOT_ACTIVE;
  449. return 0;
  450. }
  451. if (!inDebugMode) {
  452. errorFlag = CC_ERROR_NOT_DEBUGGING;
  453. return 0;
  454. }
  455. uint8_t bAns;
  456. cc_write( instr[I_DEBUG_INSTR_3] ); // DEBUG_INSTR + 3b
  457. cc_write( oc0 );
  458. cc_write( oc1 );
  459. cc_write( oc2 );
  460. cc_switchRead(250);
  461. bAns = cc_read(); // Accumulator
  462. cc_switchWrite();
  463. return bAns;
  464. }
  465. /**
  466. * Invoke a debug instruction with 1 opcode + 16-bit immediate
  467. */
  468. uint8_t cc_execi( uint8_t oc0, unsigned short c0 )
  469. {
  470. if (!cc_active) {
  471. errorFlag = CC_ERROR_NOT_ACTIVE;
  472. return 0;
  473. }
  474. if (!inDebugMode) {
  475. errorFlag = CC_ERROR_NOT_DEBUGGING;
  476. return 0;
  477. }
  478. uint8_t bAns;
  479. cc_write( instr[I_DEBUG_INSTR_3] ); // DEBUG_INSTR + 3b
  480. cc_write( oc0 );
  481. cc_write( (c0 >> 8) & 0xFF );
  482. cc_write( c0 & 0xFF );
  483. cc_switchRead(250);
  484. bAns = cc_read(); // Accumulator
  485. cc_switchWrite();
  486. return bAns;
  487. }
  488. /**
  489. * Return chip ID
  490. */
  491. unsigned short cc_getChipID() {
  492. if (!cc_active) {
  493. errorFlag = CC_ERROR_NOT_ACTIVE;
  494. return 0;
  495. }
  496. if (!inDebugMode) {
  497. errorFlag = CC_ERROR_NOT_DEBUGGING;
  498. return 0;
  499. }
  500. unsigned short bAns;
  501. uint8_t bRes;
  502. cc_write( instr[I_GET_CHIP_ID] ); // GET_CHIP_ID
  503. cc_switchRead(250);
  504. bRes = cc_read(); // High order
  505. bAns = bRes << 8;
  506. bRes = cc_read(); // Low order
  507. bAns |= bRes;
  508. cc_switchWrite();
  509. return bAns;
  510. }
  511. /**
  512. * Return PC
  513. */
  514. unsigned short cc_getPC() {
  515. if (!cc_active) {
  516. errorFlag = CC_ERROR_NOT_ACTIVE;
  517. return 0;
  518. }
  519. if (!inDebugMode) {
  520. errorFlag = CC_ERROR_NOT_DEBUGGING;
  521. return 0;
  522. }
  523. unsigned short bAns;
  524. uint8_t bRes;
  525. cc_write( instr[I_GET_PC] ); // GET_PC
  526. cc_switchRead(250);
  527. bRes = cc_read(); // High order
  528. bAns = bRes << 8;
  529. bRes = cc_read(); // Low order
  530. bAns |= bRes;
  531. cc_switchWrite();
  532. return bAns;
  533. }
  534. /**
  535. * Return debug status
  536. */
  537. uint8_t cc_getStatus() {
  538. if (!cc_active) {
  539. errorFlag = CC_ERROR_NOT_ACTIVE;
  540. return 0;
  541. }
  542. if (!inDebugMode) {
  543. errorFlag = CC_ERROR_NOT_DEBUGGING;
  544. return 0;
  545. }
  546. uint8_t bAns;
  547. cc_write( instr[I_READ_STATUS] ); // READ_STATUS
  548. cc_switchRead(250);
  549. bAns = cc_read(); // debug status
  550. cc_switchWrite();
  551. return bAns;
  552. }
  553. /**
  554. * Step instruction
  555. */
  556. uint8_t cc_step() {
  557. if (!cc_active) {
  558. errorFlag = CC_ERROR_NOT_ACTIVE;
  559. return 0;
  560. }
  561. if (!inDebugMode) {
  562. errorFlag = CC_ERROR_NOT_DEBUGGING;
  563. return 0;
  564. }
  565. uint8_t bAns;
  566. cc_write( instr[I_STEP_INSTR] ); // STEP_INSTR
  567. cc_switchRead(250);
  568. bAns = cc_read(); // Accumulator
  569. cc_switchWrite();
  570. return bAns;
  571. }
  572. /**
  573. * resume instruction
  574. */
  575. uint8_t cc_resume() {
  576. if (!cc_active) {
  577. errorFlag = CC_ERROR_NOT_ACTIVE;
  578. return 0;
  579. }
  580. if (!inDebugMode) {
  581. errorFlag = CC_ERROR_NOT_DEBUGGING;
  582. return 0;
  583. }
  584. uint8_t bAns;
  585. cc_write( instr[I_RESUME] ); //RESUME
  586. cc_switchRead(250);
  587. bAns = cc_read(); // Accumulator
  588. cc_switchWrite();
  589. return bAns;
  590. }
  591. /**
  592. * halt instruction
  593. */
  594. uint8_t cc_halt() {
  595. if (!cc_active) {
  596. errorFlag = CC_ERROR_NOT_ACTIVE;
  597. return 0;
  598. }
  599. if (!inDebugMode) {
  600. errorFlag = CC_ERROR_NOT_DEBUGGING;
  601. return 0;
  602. }
  603. uint8_t bAns;
  604. cc_write( instr[I_HALT] ); //HALT
  605. cc_switchRead(250);
  606. bAns = cc_read(); // Accumulator
  607. cc_switchWrite();
  608. return bAns;
  609. }
  610. /**
  611. * Mass-erase all chip configuration & Lock Bits
  612. */
  613. uint8_t cc_chipErase()
  614. {
  615. if (!cc_active) {
  616. errorFlag = CC_ERROR_NOT_ACTIVE;
  617. return 0;
  618. };
  619. if (!inDebugMode) {
  620. errorFlag = CC_ERROR_NOT_DEBUGGING;
  621. return 0;
  622. }
  623. uint8_t bAns;
  624. cc_write( instr[I_CHIP_ERASE] ); // CHIP_ERASE
  625. cc_switchRead(250);
  626. bAns = cc_read(); // Debug status
  627. cc_switchWrite();
  628. return bAns;
  629. }
  630. /**
  631. * Update the debug instruction table
  632. */
  633. uint8_t cc_updateInstructionTable( uint8_t newTable[16] )
  634. {
  635. // Copy table entries
  636. for (uint8_t i=0; i<16; i++)
  637. instr[i] = newTable[i];
  638. // Return the new version
  639. return instr[INSTR_VERSION];
  640. }
  641. /**
  642. * Get the instruction table version
  643. */
  644. uint8_t cc_getInstructionTableVersion()
  645. {
  646. // Return version of instruction table
  647. return instr[INSTR_VERSION];
  648. }