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.
 
 
 

810 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. return 1;
  98. };
  99. /**
  100. * Activate/Deactivate debugger
  101. */
  102. void cc_setActive( uint8_t on )
  103. {
  104. // Reset error flag
  105. errorFlag = CC_ERROR_NONE;
  106. // Continue only if active
  107. if (on == cc_active) return;
  108. cc_active = on;
  109. if (on) {
  110. // Prepare CC Pins
  111. pinMode(pinDC, OUTPUT);
  112. pinMode(pinDD, OUTPUT);
  113. pinMode(pinRST, OUTPUT);
  114. digitalWrite(pinDC, LOW);
  115. digitalWrite(pinDD, LOW);
  116. digitalWrite(pinRST, LOW);
  117. // Default direction is INPUT
  118. cc_setDDDirection(INPUT);
  119. } else {
  120. // Before deactivating, exit debug mode
  121. if (inDebugMode)
  122. cc_exit();
  123. // Put everything in inactive mode
  124. pinMode(pinDC, INPUT);
  125. pinMode(pinDD, INPUT);
  126. pinMode(pinRST, INPUT);
  127. digitalWrite(pinDC, LOW);
  128. digitalWrite(pinDD, LOW);
  129. digitalWrite(pinRST, LOW);
  130. }
  131. }
  132. /**
  133. * Return the error flag
  134. */
  135. uint8_t cc_error()
  136. {
  137. return errorFlag;
  138. }
  139. /////////////////////////////////////////////////////////////////////
  140. /////////////////////////////////////////////////////////////////////
  141. //// LOW LEVEL FUNCTIONS ////
  142. /////////////////////////////////////////////////////////////////////
  143. /////////////////////////////////////////////////////////////////////
  144. /**
  145. * Delay a particular number of cycles
  146. */
  147. struct timespec tp={0,0};
  148. static int cc_delay_mult=80;
  149. /* delay d * .4 us */
  150. void cc_delay( unsigned int d )
  151. {
  152. volatile unsigned int i = cc_delay_mult*d;
  153. while( i-- );
  154. //tp.tv_nsec=40*d;
  155. //nanosleep(&tp,NULL);
  156. }
  157. void cc_setmult(int mult)
  158. {
  159. cc_delay_mult=mult;
  160. }
  161. /* provas konsideri la rapidecon de la procesoro */
  162. void cc_delay_calibrate( )
  163. {
  164. #if 1
  165. /* The ioctl overhead on FreeBSD far exceeds min timing */
  166. cc_delay_mult = 0;
  167. #else
  168. long time0=micros();
  169. cc_delay(200);
  170. cc_delay(200);
  171. cc_delay(200);
  172. cc_delay(200);
  173. cc_delay(200);
  174. long time1=micros();
  175. /* make 1000 delay units == 400us */
  176. cc_delay_mult=cc_delay_mult*400/(time1-time0);
  177. printf("time0: %ld, time1: %ld, time1-time0: %ld\n", time0, time1, time1 - time0);
  178. printf("dmul: %d\n", cc_delay_mult);
  179. #endif
  180. }
  181. /**
  182. * Enter debug mode
  183. */
  184. uint8_t cc_enter()
  185. {
  186. if (!cc_active) {
  187. errorFlag = CC_ERROR_NOT_ACTIVE;
  188. return 0;
  189. }
  190. // =============
  191. // Reset error flag
  192. errorFlag = CC_ERROR_NONE;
  193. // Enter debug mode
  194. digitalWrite(pinRST, LOW);
  195. cc_delay(200);
  196. digitalWrite(pinDC, HIGH);
  197. cc_delay(3);
  198. digitalWrite(pinDC, LOW);
  199. cc_delay(3);
  200. digitalWrite(pinDC, HIGH);
  201. cc_delay(3);
  202. digitalWrite(pinDC, LOW);
  203. cc_delay(4);
  204. digitalWrite(pinRST, HIGH);
  205. cc_delay(200);
  206. // We are now in debug mode
  207. inDebugMode = 1;
  208. // =============
  209. // Success
  210. return 0;
  211. };
  212. /**
  213. * Write a uint8_t to the debugger
  214. */
  215. uint8_t cc_write( uint8_t data )
  216. {
  217. if (!cc_active) {
  218. errorFlag = CC_ERROR_NOT_ACTIVE;
  219. return 0;
  220. };
  221. if (!inDebugMode) {
  222. errorFlag = CC_ERROR_NOT_DEBUGGING;
  223. return 0;
  224. }
  225. // =============
  226. uint8_t cnt;
  227. // Make sure DD is on output
  228. cc_setDDDirection(OUTPUT);
  229. // Sent uint8_ts
  230. for (cnt = 8; cnt; cnt--) {
  231. // First put data bit on bus
  232. if (data & 0x80)
  233. digitalWrite(pinDD, HIGH);
  234. else
  235. digitalWrite(pinDD, LOW);
  236. // Place clock on high (other end reads data)
  237. digitalWrite(pinDC, HIGH);
  238. // Shift & Delay
  239. data <<= 1;
  240. cc_delay(2);
  241. // Place clock down
  242. digitalWrite(pinDC, LOW);
  243. cc_delay(2);
  244. }
  245. // =============
  246. return 0;
  247. }
  248. /**
  249. * Wait until input is ready for reading
  250. */
  251. uint8_t cc_switchRead(uint8_t maxWaitCycles)
  252. {
  253. if (!cc_active) {
  254. errorFlag = CC_ERROR_NOT_ACTIVE;
  255. return 0;
  256. }
  257. if (!inDebugMode) {
  258. errorFlag = CC_ERROR_NOT_DEBUGGING;
  259. return 0;
  260. }
  261. // =============
  262. uint8_t cnt;
  263. uint8_t didWait = 0;
  264. // Switch to input
  265. cc_setDDDirection(INPUT);
  266. // Wait at least 83 ns before checking state t(dir_change)
  267. cc_delay(2);
  268. // Wait for DD to go LOW (Chip is READY)
  269. while (digitalRead(pinDD) == HIGH) {
  270. // Do 8 clock cycles
  271. for (cnt = 8; cnt; cnt--) {
  272. digitalWrite(pinDC, HIGH);
  273. cc_delay(2);
  274. digitalWrite(pinDC, LOW);
  275. cc_delay(2);
  276. }
  277. // Let next function know that we did wait
  278. didWait = 1;
  279. // Check if we ran out if wait cycles
  280. if (!--maxWaitCycles) {
  281. // If we are waiting for too long, we have lost the chip,
  282. // so also assume we are out of debugging mode
  283. errorFlag = CC_ERROR_NOT_WIRED;
  284. inDebugMode = 0;
  285. return 0;
  286. }
  287. }
  288. // Wait t(sample_wait)
  289. if (didWait) cc_delay(2);
  290. // =============
  291. return 0;
  292. }
  293. /**
  294. * Switch to output
  295. */
  296. uint8_t cc_switchWrite()
  297. {
  298. cc_setDDDirection(OUTPUT);
  299. return 0;
  300. }
  301. /**
  302. * Read an input uint8_t
  303. */
  304. uint8_t cc_read()
  305. {
  306. if (!cc_active) {
  307. errorFlag = CC_ERROR_NOT_ACTIVE;
  308. return 0;
  309. }
  310. // =============
  311. uint8_t cnt;
  312. uint8_t data = 0;
  313. // Switch to input
  314. cc_setDDDirection(INPUT);
  315. // Send 8 clock pulses if we are HIGH
  316. for (cnt = 8; cnt; cnt--) {
  317. digitalWrite(pinDC, HIGH);
  318. cc_delay(2);
  319. // Shift and read
  320. data <<= 1;
  321. if (digitalRead(pinDD) == HIGH)
  322. data |= 0x01;
  323. digitalWrite(pinDC, LOW);
  324. cc_delay(2);
  325. }
  326. // =============
  327. return data;
  328. }
  329. /**
  330. * Switch reset pin
  331. */
  332. void cc_setDDDirection( uint8_t direction )
  333. {
  334. // Switch direction if changed
  335. if (direction == ddIsOutput) return;
  336. ddIsOutput = direction;
  337. // Handle new direction
  338. if (ddIsOutput) {
  339. digitalWrite(pinDD, LOW); // Disable pull-up
  340. pinMode(pinDD, OUTPUT); // Enable output
  341. digitalWrite(pinDD, LOW); // Switch to low
  342. } else {
  343. digitalWrite(pinDD, LOW); // Disable pull-up
  344. pinMode(pinDD, INPUT); // Disable output
  345. digitalWrite(pinDD, LOW); // Don't use output pull-up
  346. }
  347. }
  348. void cc_reset()
  349. {
  350. pinMode(pinDC, INPUT);
  351. pinMode(pinDD, INPUT);
  352. pinMode(pinRST, OUTPUT);
  353. cc_delay(200);
  354. pinMode(pinRST, INPUT);
  355. cc_delay(500);
  356. pinMode(pinRST, INPUT);
  357. }
  358. /////////////////////////////////////////////////////////////////////
  359. /////////////////////////////////////////////////////////////////////
  360. //// HIGH LEVEL FUNCTIONS ////
  361. /////////////////////////////////////////////////////////////////////
  362. /////////////////////////////////////////////////////////////////////
  363. /**
  364. * Exit from debug mode
  365. */
  366. uint8_t cc_exit()
  367. {
  368. if (!cc_active) {
  369. errorFlag = CC_ERROR_NOT_ACTIVE;
  370. return 0;
  371. }
  372. if (!inDebugMode) {
  373. errorFlag = CC_ERROR_NOT_DEBUGGING;
  374. return 0;
  375. }
  376. uint8_t bAns;
  377. cc_write( instr[I_RESUME] ); // RESUME
  378. cc_switchRead(250);
  379. bAns = cc_read(); // debug status
  380. cc_switchWrite();
  381. inDebugMode = 0;
  382. return 0;
  383. }
  384. /**
  385. * Get debug configuration
  386. */
  387. uint8_t cc_getConfig() {
  388. if (!cc_active) {
  389. errorFlag = CC_ERROR_NOT_ACTIVE;
  390. return 0;
  391. }
  392. if (!inDebugMode) {
  393. errorFlag = CC_ERROR_NOT_DEBUGGING;
  394. return 0;
  395. }
  396. uint8_t bAns;
  397. cc_write( instr[I_RD_CONFIG] ); // RD_CONFIG
  398. cc_switchRead(250);
  399. bAns = cc_read(); // Config
  400. cc_switchWrite();
  401. return bAns;
  402. }
  403. /**
  404. * Set debug configuration
  405. */
  406. uint8_t cc_setConfig( uint8_t config ) {
  407. if (!cc_active) {
  408. errorFlag = CC_ERROR_NOT_ACTIVE;
  409. return 0;
  410. }
  411. if (!inDebugMode) {
  412. errorFlag = CC_ERROR_NOT_DEBUGGING;
  413. return 0;
  414. }
  415. uint8_t bAns;
  416. cc_write( instr[I_WR_CONFIG] ); // WR_CONFIG
  417. cc_write( config );
  418. cc_switchRead(250);
  419. bAns = cc_read(); // Config
  420. cc_switchWrite();
  421. return bAns;
  422. }
  423. /**
  424. * Invoke a debug instruction with 1 opcode
  425. */
  426. uint8_t cc_exec( uint8_t oc0 )
  427. {
  428. if (!cc_active) {
  429. errorFlag = CC_ERROR_NOT_ACTIVE;
  430. return 0;
  431. }
  432. if (!inDebugMode) {
  433. errorFlag = CC_ERROR_NOT_DEBUGGING;
  434. return 0;
  435. }
  436. uint8_t bAns;
  437. cc_write( instr[I_DEBUG_INSTR_1] ); // DEBUG_INSTR + 1b
  438. cc_write( oc0 );
  439. cc_switchRead(250);
  440. bAns = cc_read(); // Accumulator
  441. cc_switchWrite();
  442. return bAns;
  443. }
  444. /**
  445. * Invoke a debug instruction with 2 opcodes
  446. */
  447. uint8_t cc_exec2( uint8_t oc0, uint8_t oc1 )
  448. {
  449. if (!cc_active) {
  450. errorFlag = CC_ERROR_NOT_ACTIVE;
  451. return 0;
  452. }
  453. if (!inDebugMode) {
  454. errorFlag = CC_ERROR_NOT_DEBUGGING;
  455. return 0;
  456. }
  457. uint8_t bAns;
  458. cc_write( instr[I_DEBUG_INSTR_2] ); // DEBUG_INSTR + 2b
  459. cc_write( oc0 );
  460. cc_write( oc1 );
  461. cc_switchRead(250);
  462. bAns = cc_read(); // Accumulator
  463. cc_switchWrite();
  464. return bAns;
  465. }
  466. /**
  467. * Invoke a debug instruction with 3 opcodes
  468. */
  469. uint8_t cc_exec3( uint8_t oc0, uint8_t oc1, uint8_t oc2 )
  470. {
  471. if (!cc_active) {
  472. errorFlag = CC_ERROR_NOT_ACTIVE;
  473. return 0;
  474. }
  475. if (!inDebugMode) {
  476. errorFlag = CC_ERROR_NOT_DEBUGGING;
  477. return 0;
  478. }
  479. uint8_t bAns;
  480. cc_write( instr[I_DEBUG_INSTR_3] ); // DEBUG_INSTR + 3b
  481. cc_write( oc0 );
  482. cc_write( oc1 );
  483. cc_write( oc2 );
  484. cc_switchRead(250);
  485. bAns = cc_read(); // Accumulator
  486. cc_switchWrite();
  487. return bAns;
  488. }
  489. /**
  490. * Invoke a debug instruction with 1 opcode + 16-bit immediate
  491. */
  492. uint8_t cc_execi( uint8_t oc0, unsigned short c0 )
  493. {
  494. if (!cc_active) {
  495. errorFlag = CC_ERROR_NOT_ACTIVE;
  496. return 0;
  497. }
  498. if (!inDebugMode) {
  499. errorFlag = CC_ERROR_NOT_DEBUGGING;
  500. return 0;
  501. }
  502. uint8_t bAns;
  503. cc_write( instr[I_DEBUG_INSTR_3] ); // DEBUG_INSTR + 3b
  504. cc_write( oc0 );
  505. cc_write( (c0 >> 8) & 0xFF );
  506. cc_write( c0 & 0xFF );
  507. cc_switchRead(250);
  508. bAns = cc_read(); // Accumulator
  509. cc_switchWrite();
  510. return bAns;
  511. }
  512. /**
  513. * Return chip ID
  514. */
  515. unsigned short cc_getChipID() {
  516. if (!cc_active) {
  517. errorFlag = CC_ERROR_NOT_ACTIVE;
  518. return 0;
  519. }
  520. if (!inDebugMode) {
  521. errorFlag = CC_ERROR_NOT_DEBUGGING;
  522. return 0;
  523. }
  524. unsigned short bAns;
  525. uint8_t bRes;
  526. cc_write( instr[I_GET_CHIP_ID] ); // GET_CHIP_ID
  527. cc_switchRead(250);
  528. bRes = cc_read(); // High order
  529. bAns = bRes << 8;
  530. bRes = cc_read(); // Low order
  531. bAns |= bRes;
  532. cc_switchWrite();
  533. return bAns;
  534. }
  535. /**
  536. * Return PC
  537. */
  538. unsigned short cc_getPC() {
  539. if (!cc_active) {
  540. errorFlag = CC_ERROR_NOT_ACTIVE;
  541. return 0;
  542. }
  543. if (!inDebugMode) {
  544. errorFlag = CC_ERROR_NOT_DEBUGGING;
  545. return 0;
  546. }
  547. unsigned short bAns;
  548. uint8_t bRes;
  549. cc_write( instr[I_GET_PC] ); // GET_PC
  550. cc_switchRead(250);
  551. bRes = cc_read(); // High order
  552. bAns = bRes << 8;
  553. bRes = cc_read(); // Low order
  554. bAns |= bRes;
  555. cc_switchWrite();
  556. return bAns;
  557. }
  558. /**
  559. * Return debug status
  560. */
  561. uint8_t cc_getStatus() {
  562. if (!cc_active) {
  563. errorFlag = CC_ERROR_NOT_ACTIVE;
  564. return 0;
  565. }
  566. if (!inDebugMode) {
  567. errorFlag = CC_ERROR_NOT_DEBUGGING;
  568. return 0;
  569. }
  570. uint8_t bAns;
  571. cc_write( instr[I_READ_STATUS] ); // READ_STATUS
  572. cc_switchRead(250);
  573. bAns = cc_read(); // debug status
  574. cc_switchWrite();
  575. return bAns;
  576. }
  577. /**
  578. * Step instruction
  579. */
  580. uint8_t cc_step() {
  581. if (!cc_active) {
  582. errorFlag = CC_ERROR_NOT_ACTIVE;
  583. return 0;
  584. }
  585. if (!inDebugMode) {
  586. errorFlag = CC_ERROR_NOT_DEBUGGING;
  587. return 0;
  588. }
  589. uint8_t bAns;
  590. cc_write( instr[I_STEP_INSTR] ); // STEP_INSTR
  591. cc_switchRead(250);
  592. bAns = cc_read(); // Accumulator
  593. cc_switchWrite();
  594. return bAns;
  595. }
  596. /**
  597. * resume instruction
  598. */
  599. uint8_t cc_resume() {
  600. if (!cc_active) {
  601. errorFlag = CC_ERROR_NOT_ACTIVE;
  602. return 0;
  603. }
  604. if (!inDebugMode) {
  605. errorFlag = CC_ERROR_NOT_DEBUGGING;
  606. return 0;
  607. }
  608. uint8_t bAns;
  609. cc_write( instr[I_RESUME] ); //RESUME
  610. cc_switchRead(250);
  611. bAns = cc_read(); // Accumulator
  612. cc_switchWrite();
  613. return bAns;
  614. }
  615. /**
  616. * halt instruction
  617. */
  618. uint8_t cc_halt() {
  619. if (!cc_active) {
  620. errorFlag = CC_ERROR_NOT_ACTIVE;
  621. return 0;
  622. }
  623. if (!inDebugMode) {
  624. errorFlag = CC_ERROR_NOT_DEBUGGING;
  625. return 0;
  626. }
  627. uint8_t bAns;
  628. cc_write( instr[I_HALT] ); //HALT
  629. cc_switchRead(250);
  630. bAns = cc_read(); // Accumulator
  631. cc_switchWrite();
  632. return bAns;
  633. }
  634. /**
  635. * Mass-erase all chip configuration & Lock Bits
  636. */
  637. uint8_t cc_chipErase()
  638. {
  639. if (!cc_active) {
  640. errorFlag = CC_ERROR_NOT_ACTIVE;
  641. return 0;
  642. };
  643. if (!inDebugMode) {
  644. errorFlag = CC_ERROR_NOT_DEBUGGING;
  645. return 0;
  646. }
  647. uint8_t bAns;
  648. cc_write( instr[I_CHIP_ERASE] ); // CHIP_ERASE
  649. cc_switchRead(250);
  650. bAns = cc_read(); // Debug status
  651. cc_switchWrite();
  652. return bAns;
  653. }
  654. /**
  655. * Update the debug instruction table
  656. */
  657. uint8_t cc_updateInstructionTable( uint8_t newTable[16] )
  658. {
  659. // Copy table entries
  660. for (uint8_t i=0; i<16; i++)
  661. instr[i] = newTable[i];
  662. // Return the new version
  663. return instr[INSTR_VERSION];
  664. }
  665. /**
  666. * Get the instruction table version
  667. */
  668. uint8_t cc_getInstructionTableVersion()
  669. {
  670. // Return version of instruction table
  671. return instr[INSTR_VERSION];
  672. }