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.

910 lines
24 KiB

4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
  1. /**************************************************
  2. TPI programmer for ATtiny4/5/9/10/20/40
  3. Make the connections as shown below.
  4. To use:
  5. ***** Buad rate must be set to 9600 ****
  6. - Upload to arduino and power off
  7. - Connect ATtiny10 as shown
  8. - Power on and open the serial monitor
  9. - If things are working so far you should
  10. see "NVM enabled" and "ATtiny10/20/40 connected".
  11. - Input one-letter commands via serial monitor:
  12. D = dump memory. Displays all current memory
  13. on the chip
  14. E = erase chip. Erases all program memory
  15. automatically done at time of programming
  16. P = write program. After sending this, paste
  17. the program from the hex file into the
  18. serial monitor.
  19. S = set fuse. follow the instructions to set
  20. one of the three fuses.
  21. C = clear fuse. follow the instructions to clear
  22. one of the three fuses.
  23. L = Set Lock Bits No further programming & verification
  24. possible
  25. H = Toggle High Voltage Programming
  26. T = Toggle +12v enabled by High, or Low
  27. R/r = Quick reset
  28. - Finally, power off the arduino and remove the
  29. Attiny10/20/40
  30. Arduino ATtiny10
  31. ----------+ +----------------
  32. (SS#) 10 |--[R]-----| 6 (RESET#/PB3)
  33. | |
  34. (MOSI) 11 |--[R]--+--| 1 (TPIDATA/PB0)
  35. | | |
  36. (MISO) 12 |--[R]--+ |
  37. | |
  38. (SCK) 13 |--[R]-----| 3 (TPICLK/PB1)
  39. ----------+ +----------------
  40. * *
  41. ----------+ +----------------
  42. (HVP) 9 |--- | 6 (RESET#/PB3)
  43. | |
  44. * *
  45. -[R]- = a 220 - 1K Ohm resistor
  46. * *
  47. this picture : 2011/12/08 by pcm1723
  48. modified :2015/02/27 by KD
  49. * *
  50. thanks to pcm1723 for tpitest.pde upon which
  51. this is based
  52. **************************************************
  53. Updates:
  54. Apr 02, 2018: Ksdsksd@gmail.com
  55. Added Lock bit setting to main menu
  56. Jan 23, 2017: Ksdsksd@gmail.com
  57. Thanks to InoueTaichi Fixed incorrect #define Tiny40
  58. Mar 05, 2015: Ksdsksd@gamil.com
  59. Added notifications to setting and clearing the system flags.
  60. Feb 23, 2015: Ksdsksd@gamil.com
  61. Changed the programmer Diagram, This is the config I use, and get a sucessful programming of a tiny10 at 9600 baud.
  62. Mar 22, 2014: Ksdsksd@gmail.com
  63. Added the quick reset to high before resetting the device.
  64. Added code to stop the SPI and float the pins for testing the device while connected.
  65. Mar 20, 2014: Ksdsksd@gmail.com
  66. Added a quick reset by sending 'r' or 'R' via the serial monitor.
  67. Added a High voltage programming option from pin 9, toggled by 'H'
  68. Added a High/low option for providing 12v to the reset pin, toggled by 'T'
  69. Mar 17, 2014: Ksdsksd@gmail.com
  70. Had some trouble with the nibbles being swapped when programming on the 10 & 20,
  71. added b1,b2 to hold the serial data before calling byteval()
  72. Added Nat Blundell's patch to the code
  73. Apr 10, 2013: Ksdsksd@gmail.com
  74. Applied Fix for setting and clearing flags
  75. Feb 7, 2013: Ksdsksd@gmail.com
  76. Fixed programming timer, had intitial start at zero instead of current time.
  77. Dec 11, 2012: Ksdsksd@gmail.com
  78. Added detect and programming for 4/5/9
  79. Dec 5-6, 2012: Ksdsksd@gmail.com
  80. Incorperated read, and verify into program. Now have no program size limitation by using 328p.
  81. Changed the outHex routines consolidated them into 1, number to be printed, and number of nibbles
  82. Added a type check to distinguish between Tiny10/20/40
  83. Added an auto word size check to ensure that there is the proper amount of words written for a 10/20/40
  84. Removed Read program, Verify, and Finish from options
  85. Changed baud rate to 19200 for delay from data written to the chip, to prevent serial buffer overrun.
  86. Oct 5, 2012: Ksdsksd@gmail.com
  87. *** Noticed that when programming, the verification fails
  88. at times by last 1-2 bytes programmed, and the Tiny would act erratic.
  89. Quick fix was adding 3 NOP's to the end the Tiny's code, and ignoring the errors, the Tiny then performed as expected.
  90. Oct 4, 2012: Ksdsksd@gmail.com
  91. Moved all Serial printed strings to program space
  92. Added code to detect Tiny20
  93. */
  94. #include <SPI.h>
  95. #include "pins_arduino.h"
  96. // define the instruction set bytes
  97. #define SLD 0x20
  98. #define SLDp 0x24
  99. #define SST 0x60
  100. #define SSTp 0x64
  101. #define SSTPRH 0x69
  102. #define SSTPRL 0x68
  103. // see functions below ////////////////////////////////
  104. // SIN 0b0aa1aaaa replace a with 6 address bits
  105. // SOUT 0b1aa1aaaa replace a with 6 address bits
  106. // SLDCS 0b1000aaaa replace a with address bits
  107. // SSTCS 0b1100aaaa replace a with address bits
  108. ///////////////////////////////////////////////////////
  109. #define SKEY 0xE0
  110. #define NVM_PROGRAM_ENABLE 0x1289AB45CDD888FFULL // the ULL means unsigned long long
  111. #define NVMCMD 0x33
  112. #define NVMCSR 0x32
  113. #define NVM_NOP 0x00
  114. #define NVM_CHIP_ERASE 0x10
  115. #define NVM_SECTION_ERASE 0x14
  116. #define NVM_WORD_WRITE 0x1D
  117. #define HVReset 9
  118. #define Tiny4_5 10
  119. #define Tiny9 1
  120. #define Tiny10 1
  121. #define Tiny20 2
  122. #define Tiny40 4
  123. #define TimeOut 1
  124. #define HexError 2
  125. #define TooLarge 3
  126. // represents the current pointer register value
  127. unsigned short adrs = 0x0000;
  128. // used for storing a program file
  129. uint8_t data[16]; //program data
  130. unsigned int progSize = 0; //program size in bytes
  131. // used for various purposes
  132. long startTime;
  133. int timeout;
  134. uint8_t b, b1, b2, b3;
  135. boolean idChecked;
  136. boolean correct;
  137. char type; // type of chip connected 1 = Tiny10, 2 = Tiny20
  138. char HVP = 0;
  139. char HVON = 0;
  140. void setup() {
  141. // set up serial
  142. Serial.begin(9600); // you cant increase this, it'll overrun the buffer
  143. Serial.println(F("setupend")); //anything?
  144. // set up SPI
  145. /* SPI.begin();
  146. SPI.setBitOrder(LSBFIRST);
  147. SPI.setDataMode(SPI_MODE0);
  148. SPI.setClockDivider(SPI_CLOCK_DIV32);
  149. */ start_tpi();
  150. pinMode(HVReset, OUTPUT);
  151. // initialize memory pointer register
  152. setPointer(0x0000);
  153. timeout = 20000;
  154. idChecked = false;
  155. } // end setup()
  156. void hvserial()
  157. {
  158. if (HVP)
  159. Serial.println(F("***High Voltage Programming Enabled***"));
  160. else
  161. Serial.println(F("High Voltage Programming Disabled"));
  162. // M
  163. Serial.print(HVON ? F("HIGH") : F("LOW"));
  164. Serial.print(F(" supplies 12v"));
  165. }
  166. void hvReset(char highLow)
  167. {
  168. if (HVP)
  169. {
  170. if (HVON) //if high enables 12v
  171. highLow = !highLow; // invert the typical reset
  172. digitalWrite(HVReset, highLow);
  173. }
  174. else
  175. digitalWrite(SS, highLow);
  176. }
  177. void quickReset()
  178. {
  179. digitalWrite(SS, HIGH);
  180. delay(1);
  181. digitalWrite(SS, LOW);
  182. delay(10);
  183. digitalWrite(SS, HIGH);
  184. }
  185. void start_tpi() {
  186. SPI.begin();
  187. SPI.setBitOrder(LSBFIRST);
  188. SPI.setDataMode(SPI_MODE0);
  189. SPI.setClockDivider(SPI_CLOCK_DIV32);
  190. // enter TPI programming mode
  191. hvReset(LOW);
  192. // digitalWrite(SS, LOW); // assert RESET on tiny
  193. delay(1); // t_RST min = 400 ns @ Vcc = 5 V
  194. SPI.transfer(0xff); // activate TPI by emitting
  195. SPI.transfer(0xff); // 16 or more pulses on TPICLK
  196. SPI.transfer(0xff); // while holding TPIDATA to "1"
  197. writeCSS(0x02, 0x04); // TPIPCR, guard time = 8bits (default=128)
  198. send_skey(NVM_PROGRAM_ENABLE); // enable NVM interface
  199. // wait for NVM to be enabled
  200. while ((readCSS(0x00) & 0x02) < 1) {
  201. // wait
  202. }
  203. Serial.println(F("NVM enabled"));
  204. }
  205. void setLockBits() {
  206. Serial.print(F("Locking... Are you sure? Y/N"));
  207. while (Serial.available() < 1);
  208. char yn = Serial.read();
  209. if (yn == 'n' || yn == 'N')
  210. return;
  211. setPointer(0x3F00);
  212. writeIO(NVMCMD, NVM_WORD_WRITE);
  213. tpi_send_byte(SSTp);
  214. tpi_send_byte(0);
  215. tpi_send_byte(SSTp);
  216. tpi_send_byte(0xFF);
  217. while ((readIO(NVMCSR) & (1 << 7)) != 0x00);
  218. Serial.print(F("Locked..."));
  219. }
  220. void loop() {
  221. Serial.print(F("Start"));
  222. if (!idChecked) {
  223. // start_tpi();
  224. checkID();
  225. idChecked = true;
  226. finish();
  227. }
  228. // when ready, send ready signal '.' and wait
  229. Serial.print(F("\n>"));
  230. while (Serial.available() < 1) {
  231. // wait
  232. }
  233. start_tpi();
  234. // the first byte is a command
  235. //** 'P' = program the ATtiny using the read program
  236. //** 'D' = dump memory to serial monitor
  237. //** 'E' = erase chip. erases current program memory.(done automatically by 'P')
  238. //** 'S' = set fuse
  239. //** 'C' = clear fuse
  240. //** 'L' = Set Lock Bits
  241. char comnd = Sread();
  242. switch ( comnd ) {
  243. case 'r':
  244. case'R':
  245. quickReset();
  246. break;
  247. case 'D':
  248. dumpMemory();
  249. break;
  250. case 'H':
  251. HVP = !HVP;
  252. hvserial();
  253. break;
  254. case 'T':
  255. HVON = !HVON;
  256. hvserial();
  257. break;
  258. case 'P':
  259. if (!writeProgram()) {
  260. startTime = millis();
  261. while (millis() - startTime < 8000)
  262. Serial.read();// if exited due to error, disregard all other serial data
  263. }
  264. break;
  265. case 'E':
  266. eraseChip();
  267. break;
  268. case 'S':
  269. setConfig(true);
  270. break;
  271. case 'C':
  272. setConfig(false);
  273. break;
  274. case 'L':
  275. setLockBits();
  276. break;
  277. default:
  278. Serial.println(F("Received unknown command"));
  279. }
  280. finish();
  281. }
  282. void ERROR_pgmSize(void)
  283. {
  284. Serial.println(F("program size is 0??"));
  285. }
  286. void ERROR_data(char i)
  287. {
  288. Serial.println(F("couldn't receive data:"));
  289. switch (i) {
  290. case TimeOut:
  291. Serial.println(F("timed out"));
  292. break;
  293. case HexError:
  294. Serial.println(F("hex file format error"));
  295. break;
  296. case TooLarge:
  297. Serial.println(F("program is too large"));
  298. break;
  299. default:
  300. break;
  301. }
  302. }
  303. // print the register, SRAM, config and signature memory
  304. void dumpMemory() {
  305. unsigned int len;
  306. uint8_t i;
  307. // initialize memory pointer register
  308. setPointer(0x0000);
  309. Serial.println(F("Current memory state:"));
  310. if (type != Tiny4_5)
  311. len = 0x400 * type; //the memory length for a 10/20/40 is 1024/2048/4096
  312. else
  313. len = 0x200; //tiny 4/5 has 512 bytes
  314. len += 0x4000;
  315. while (adrs < len) {
  316. // read the byte at the current pointer address
  317. // and increment address
  318. tpi_send_byte(SLDp);
  319. b = tpi_receive_byte(); // get data byte
  320. // read all the memory, but only print
  321. // the register, SRAM, config and signature memory
  322. if ((0x0000 <= adrs && adrs <= 0x005F) // register/SRAM
  323. | (0x3F00 <= adrs && adrs <= 0x3F01) // NVM lock bits
  324. | (0x3F40 <= adrs && adrs <= 0x3F41) // config
  325. | (0x3F80 <= adrs && adrs <= 0x3F81) // calibration
  326. | (0x3FC0 <= adrs && adrs <= 0x3FC3) // ID
  327. | (0x4000 <= adrs && adrs <= len - 1) ) { // program
  328. // print +number along the top
  329. if ((0x00 == adrs)
  330. | (0x3f00 == adrs) // NVM lock bits
  331. | (0x3F40 == adrs) // config
  332. | (0x3F80 == adrs) // calibration
  333. | (0x3FC0 == adrs) // ID
  334. | (0x4000 == adrs) ) {
  335. Serial.println();
  336. if (adrs == 0x0000) {
  337. Serial.print(F("registers, SRAM"));
  338. }
  339. if (adrs == 0x3F00) {
  340. Serial.print(F("NVM lock"));
  341. }
  342. if (adrs == 0x3F40) {
  343. Serial.print(F("configuration"));
  344. }
  345. if (adrs == 0x3F80) {
  346. Serial.print(F("calibration"));
  347. }
  348. if (adrs == 0x3FC0) {
  349. Serial.print(F("device ID"));
  350. }
  351. if (adrs == 0x4000) {
  352. Serial.print(F("program"));
  353. }
  354. Serial.println();
  355. for (i = 0; i < 5; i++)
  356. Serial.print(F(" "));
  357. for (i = 0; i < 16; i++) {
  358. Serial.print(F(" +"));
  359. Serial.print(i, HEX);
  360. }
  361. }
  362. // print number on the left
  363. if (0 == (0x000f & adrs)) {
  364. Serial.println();
  365. outHex(adrs, 4);
  366. Serial.print(F(": ")); // delimiter
  367. }
  368. outHex(b, 2);
  369. Serial.print(F(" "));
  370. }
  371. adrs++; // increment memory address
  372. if (adrs == 0x0060) {
  373. // skip reserved memory
  374. setPointer(0x3F00);
  375. }
  376. }
  377. Serial.println(F(" "));
  378. } // end dumpMemory()
  379. // receive and translate the contents of a hex file, Program and verify on the fly
  380. boolean writeProgram() {
  381. char datlength[] = "00";
  382. char addr[] = "0000";
  383. char something[] = "00";
  384. char chksm[] = "00";
  385. unsigned int currentByte = 0;
  386. progSize = 0;
  387. uint8_t linelength = 0;
  388. boolean fileEnd = false;
  389. unsigned short tadrs;
  390. tadrs = adrs = 0x4000;
  391. correct = true;
  392. unsigned long pgmStartTime = millis();
  393. eraseChip(); // erase chip
  394. char words = (type != Tiny4_5 ? type : 1);
  395. char b1, b2;
  396. // read in the data and
  397. while (!fileEnd) {
  398. startTime = millis();
  399. while (Serial.available() < 1) {
  400. if (millis() - startTime > timeout) {
  401. ERROR_data(TimeOut);
  402. return false;
  403. }
  404. if (pgmStartTime == 0)
  405. pgmStartTime = millis();
  406. }
  407. if (Sread() != ':') { // maybe it was a newline??
  408. if (Sread() != ':') {
  409. ERROR_data(HexError);
  410. return false;
  411. }
  412. }
  413. // read data length
  414. datlength[0] = Sread();
  415. datlength[1] = Sread();
  416. linelength = byteval(datlength[0], datlength[1]);
  417. // read address. if "0000" currentByte = 0
  418. addr[0] = Sread();
  419. addr[1] = Sread();
  420. addr[2] = Sread();
  421. addr[3] = Sread();
  422. if (linelength != 0x00 && addr[0] == '0' && addr[1] == '0' && addr[2] == '0' && addr[3] == '0')
  423. currentByte = 0;
  424. // read type thingy. "01" means end of file
  425. something[0] = Sread();
  426. something[1] = Sread();
  427. if (something[1] == '1') {
  428. fileEnd = true;
  429. }
  430. if (something[1] == '2') {
  431. for (int i = 0; i <= linelength; i++) {
  432. Sread();
  433. Sread();
  434. }
  435. }
  436. else {
  437. // read in the data
  438. for (int k = 0; k < linelength; k++) {
  439. while (Serial.available() < 1) {
  440. if (millis() - startTime > timeout) {
  441. ERROR_data(TimeOut);
  442. return false;
  443. }
  444. }
  445. b1 = Sread();
  446. b2 = Sread();
  447. data[currentByte] = byteval(b1, b2);
  448. currentByte++;
  449. progSize++;
  450. if (progSize > (type != Tiny4_5 ? type * 1024 : 512)) {
  451. ERROR_data(TooLarge);
  452. return 0;
  453. }
  454. if (fileEnd) //has the end of the file been reached?
  455. while (currentByte < 2 * words) { // append zeros to align the word count to program
  456. data[currentByte] = 0;
  457. currentByte++;
  458. }
  459. if ( currentByte == 2 * words ) { // is the word/Dword/Qword here?
  460. currentByte = 0; // yes, reset counter
  461. setPointer(tadrs); // point to the address to program
  462. writeIO(NVMCMD, NVM_WORD_WRITE);
  463. for (int i = 0; i < 2 * words; i += 2) { // loop for each word size depending on micro
  464. // now write a word to program memory
  465. tpi_send_byte(SSTp);
  466. tpi_send_byte(data[i]); // LSB first
  467. tpi_send_byte(SSTp);
  468. tpi_send_byte(data[i + 1]); // then MSB
  469. SPI.transfer(0xff); //send idle between words
  470. SPI.transfer(0xff); //send idle between words
  471. }
  472. while ((readIO(NVMCSR) & (1 << 7)) != 0x00) {} // wait for write to finish
  473. writeIO(NVMCMD, NVM_NOP);
  474. SPI.transfer(0xff);
  475. SPI.transfer(0xff);
  476. //verify written words
  477. setPointer(tadrs);
  478. for (int c = 0; c < 2 * words; c++) {
  479. tpi_send_byte(SLDp);
  480. b = tpi_receive_byte(); // get data byte
  481. if (b != data[c]) {
  482. correct = false;
  483. Serial.println(F("program error:"));
  484. Serial.print(F("byte "));
  485. outHex(adrs, 4);
  486. Serial.print(F(" expected "));
  487. outHex(data[c], 2);
  488. Serial.print(F(" read "));
  489. outHex(b, 2);
  490. Serial.println();
  491. if (!correct)
  492. return false;
  493. }
  494. }
  495. tadrs += 2 * words;
  496. }
  497. }
  498. // read in the checksum.
  499. startTime = millis();
  500. while (Serial.available() == 0) {
  501. if (millis() - startTime > timeout) {
  502. ERROR_data(TimeOut);
  503. return false;
  504. }
  505. }
  506. chksm[0] = Sread();
  507. chksm[1] = Sread();
  508. }
  509. }
  510. // the program was successfully written
  511. Serial.print(F("Successfully wrote program: "));
  512. Serial.print(progSize, DEC);
  513. Serial.print(F(" of "));
  514. if (type != Tiny4_5)
  515. Serial.print(1024 * type, DEC);
  516. else
  517. Serial.print(512, DEC);
  518. Serial.print(F(" bytes\n in "));
  519. Serial.print((millis() - pgmStartTime) / 1000.0, DEC);
  520. Serial.print(F(" Seconds"));
  521. // digitalWrite(SS, HIGH); // release RESET
  522. return true;
  523. }
  524. void eraseChip() {
  525. // initialize memory pointer register
  526. setPointer(0x4001); // need the +1 for chip erase
  527. // erase the chip
  528. writeIO(NVMCMD, NVM_CHIP_ERASE);
  529. tpi_send_byte(SSTp);
  530. tpi_send_byte(0xAA);
  531. tpi_send_byte(SSTp);
  532. tpi_send_byte(0xAA);
  533. tpi_send_byte(SSTp);
  534. tpi_send_byte(0xAA);
  535. tpi_send_byte(SSTp);
  536. tpi_send_byte(0xAA);
  537. while ((readIO(NVMCSR) & (1 << 7)) != 0x00) {
  538. // wait for erasing to finish
  539. }
  540. Serial.println(F("chip erased"));
  541. }
  542. void setConfig(boolean val) {
  543. // get current config byte
  544. setPointer(0x3F40);
  545. tpi_send_byte(SLD);
  546. b = tpi_receive_byte();
  547. Serial.println(F("input one of these letters"));
  548. Serial.println(F("c = system clock output"));
  549. Serial.println(F("w = watchdog timer on"));
  550. Serial.println(F("r = disable reset"));
  551. Serial.println(F("x = cancel. don't change anything"));
  552. while (Serial.available() < 1) {
  553. // wait
  554. }
  555. char comnd = Serial.read();
  556. setPointer(0x3F40);
  557. writeIO(NVMCMD, (val ? NVM_WORD_WRITE : NVM_SECTION_ERASE) );
  558. if (comnd == 'c') {
  559. tpi_send_byte(SSTp);
  560. if (val) {
  561. tpi_send_byte(b & 0b11111011);
  562. } else {
  563. tpi_send_byte(b | 0x04);
  564. }
  565. tpi_send_byte(SSTp);
  566. tpi_send_byte(0xFF);
  567. } else if (comnd == 'w') {
  568. tpi_send_byte(SSTp);
  569. if (val) {
  570. tpi_send_byte(b & 0b11111101);
  571. } else {
  572. tpi_send_byte(b | 0x02);
  573. }
  574. tpi_send_byte(SSTp);
  575. tpi_send_byte(0xFF);
  576. } else if (comnd == 'r') {
  577. tpi_send_byte(SSTp);
  578. if (val) {
  579. tpi_send_byte(b & 0b11111110);
  580. } else {
  581. tpi_send_byte(b | 0x01);
  582. }
  583. tpi_send_byte(SSTp);
  584. tpi_send_byte(0xFF);
  585. } else if (comnd == 'x') {
  586. // do nothing
  587. } else {
  588. Serial.println(F("received unknown command. Cancelling"));
  589. }
  590. while ((readIO(NVMCSR) & (1 << 7)) != 0x00) {
  591. // wait for write to finish
  592. }
  593. writeIO(NVMCMD, NVM_NOP);
  594. SPI.transfer(0xff);
  595. SPI.transfer(0xff);
  596. if (comnd != 'x') {
  597. Serial.print(F("\n\nSuccessfully "));
  598. if (val)
  599. Serial.print(F("Set "));
  600. else
  601. Serial.print(F("Cleared "));
  602. Serial.print(F("\""));
  603. if (comnd == 'w')
  604. Serial.print(F("Watchdog"));
  605. else if (comnd == 'c')
  606. Serial.print(F("Clock Output"));
  607. else if (comnd == 'r')
  608. Serial.print(F("Reset"));
  609. Serial.println(F("\" Flag\n"));
  610. }
  611. }
  612. void finish() {
  613. writeCSS(0x00, 0x00);
  614. SPI.transfer(0xff);
  615. SPI.transfer(0xff);
  616. hvReset(HIGH);
  617. // digitalWrite(SS, HIGH); // release RESET
  618. delay(1); // t_RST min = 400 ns @ Vcc = 5 V
  619. SPI.end();
  620. DDRB &= 0b11000011; //tri-state spi so target can be tested
  621. PORTB &= 0b11000011;
  622. }
  623. void checkID() {
  624. // check the device ID
  625. uint8_t id1, id2, id3;
  626. setPointer(0x3FC0);
  627. tpi_send_byte(SLDp);
  628. id1 = tpi_receive_byte();
  629. tpi_send_byte(SLDp);
  630. id2 = tpi_receive_byte();
  631. tpi_send_byte(SLDp);
  632. id3 = tpi_receive_byte();
  633. if (id1 == 0x1E && id2 == 0x8F && id3 == 0x0A) {
  634. Serial.print(F("ATtiny4"));
  635. type = Tiny4_5;
  636. } else if (id1 == 0x1E && id2 == 0x8F && id3 == 0x09) {
  637. Serial.print(F("ATtiny5"));
  638. type = Tiny4_5;
  639. } else if (id1 == 0x1E && id2 == 0x90 && id3 == 0x08) {
  640. Serial.print(F("ATtiny9"));
  641. type = Tiny9;
  642. } else if (id1 == 0x1E && id2 == 0x90 && id3 == 0x03) {
  643. Serial.print(F("ATtiny10"));
  644. type = Tiny10;
  645. } else if (id1 == 0x1E && id2 == 0x91 && id3 == 0x0f) {
  646. Serial.print(F("ATtiny20"));
  647. type = Tiny20;
  648. } else if (id1 == 0x1E && id2 == 0x92 && id3 == 0x0e) {
  649. Serial.print(F("ATtiny40"));
  650. type = Tiny40;
  651. } else {
  652. Serial.print(F("Unknown chip"));
  653. }
  654. Serial.println(F(" connected"));
  655. }
  656. /*
  657. send a byte in one TPI frame (12 bits)
  658. (1 start + 8 data + 1 parity + 2 stop)
  659. using 2 SPI data bytes (2 x 8 = 16 clocks)
  660. (with 4 extra idle bits)
  661. */
  662. void tpi_send_byte( uint8_t data ) {
  663. // compute partiy bit
  664. uint8_t par = data;
  665. par ^= (par >> 4); // b[7:4] (+) b[3:0]
  666. par ^= (par >> 2); // b[3:2] (+) b[1:0]
  667. par ^= (par >> 1); // b[1] (+) b[0]
  668. // REMEMBER: this is in LSBfirst mode and idle is high
  669. // (2 idle) + (1 start bit) + (data[4:0])
  670. SPI.transfer(0x03 | (data << 3));
  671. // (data[7:5]) + (1 parity) + (2 stop bits) + (2 idle)
  672. SPI.transfer(0xf0 | (par << 3) | (data >> 5));
  673. } // end tpi_send_byte()
  674. /*
  675. receive TPI 12-bit format byte data
  676. via SPI 2 bytes (16 clocks) or 3 bytes (24 clocks)
  677. */
  678. uint8_t tpi_receive_byte( void ) {
  679. //uint8_t b1, b2, b3;
  680. // keep transmitting high(idle) while waiting for a start bit
  681. do {
  682. b1 = SPI.transfer(0xff);
  683. } while (0xff == b1);
  684. // get (partial) data bits
  685. b2 = SPI.transfer(0xff);
  686. // if the first byte(b1) contains less than 4 data bits
  687. // we need to get a third byte to get the parity and stop bits
  688. if (0x0f == (0x0f & b1)) {
  689. b3 = SPI.transfer(0xff);
  690. }
  691. // now shift the bits into the right positions
  692. // b1 should hold only idle and start bits = 0b01111111
  693. while (0x7f != b1) { // data not aligned
  694. b2 <<= 1; // shift left data bits
  695. if (0x80 & b1) { // carry from 1st byte
  696. b2 |= 1; // set bit
  697. }
  698. b1 <<= 1;
  699. b1 |= 0x01; // fill with idle bit (1)
  700. }
  701. // now the data byte is stored in b2
  702. return ( b2 );
  703. } // end tpi_receive_byte()
  704. // send the 64 bit NVM key
  705. void send_skey(uint64_t nvm_key) {
  706. tpi_send_byte(SKEY);
  707. while (nvm_key) {
  708. tpi_send_byte(nvm_key & 0xFF);
  709. nvm_key >>= 8;
  710. }
  711. } // end send_skey()
  712. // sets the pointer address
  713. void setPointer(unsigned short address) {
  714. adrs = address;
  715. tpi_send_byte(SSTPRL);
  716. tpi_send_byte(address & 0xff);
  717. tpi_send_byte(SSTPRH);
  718. tpi_send_byte((address >> 8) & 0xff);
  719. }
  720. // writes using SOUT
  721. void writeIO(uint8_t address, uint8_t value) {
  722. // SOUT 0b1aa1aaaa replace a with 6 address bits
  723. tpi_send_byte(0x90 | (address & 0x0F) | ((address & 0x30) << 1));
  724. tpi_send_byte(value);
  725. }
  726. // reads using SIN
  727. uint8_t readIO(uint8_t address) {
  728. // SIN 0b0aa1aaaa replace a with 6 address bits
  729. tpi_send_byte(0x10 | (address & 0x0F) | ((address & 0x30) << 1));
  730. return tpi_receive_byte();
  731. }
  732. // writes to CSS
  733. void writeCSS(uint8_t address, uint8_t value) {
  734. tpi_send_byte(0xC0 | address);
  735. tpi_send_byte(value);
  736. }
  737. // reads from CSS
  738. uint8_t readCSS(uint8_t address) {
  739. tpi_send_byte(0x80 | address);
  740. return tpi_receive_byte();
  741. }
  742. // converts two chars to one byte
  743. // c1 is MS, c2 is LS
  744. uint8_t byteval(char c1, char c2) {
  745. uint8_t by;
  746. if (c1 <= '9') {
  747. by = c1 - '0';
  748. } else {
  749. by = c1 - 'A' + 10;
  750. }
  751. by = by << 4;
  752. if (c2 <= '9') {
  753. by += c2 - '0';
  754. } else {
  755. by += c2 - 'A' + 10;
  756. }
  757. return by;
  758. }
  759. char Sread(void) {
  760. while (Serial.available() < 1) {}
  761. return Serial.read();
  762. }
  763. void outHex(unsigned int n, char l) { // call with the number to be printed, and # of nibbles expected.
  764. for (char count = l - 1; count > 0; count--) { // quick and dirty to add zeros to the hex value
  765. if (((n >> (count * 4)) & 0x0f) == 0) // if MSB is 0
  766. Serial.print(F("0")); //prepend a 0
  767. else
  768. break; //exit the for loop
  769. }
  770. Serial.print(n, HEX);
  771. }
  772. // end of file