AVR ISP adapter (avr109/910 compatible, using ATmega16)
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.

1010 lines
28 KiB

  1. /***************************************************************************
  2. * C based avr910 / avr109 ISP Adapter *
  3. * *
  4. * Copyright (C) 2006 - 2020 by Olaf Rempel *
  5. * razzor AT kopf MINUS tisch DOT de *
  6. * *
  7. * This program is free software; you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation; version 2 of the License, *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this program; if not, write to the *
  18. * Free Software Foundation, Inc., *
  19. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  20. ***************************************************************************/
  21. #include <avr/io.h>
  22. #include <avr/interrupt.h>
  23. #include <string.h>
  24. #include "avrdevice.h"
  25. #include "display.h"
  26. #include "spi_isp.h"
  27. #include "target.h"
  28. #include "twi_master.h"
  29. #include "uart.h"
  30. #define MIN(a,b) (((a) < (b)) ? (a) : (b))
  31. #define TIMER_IRQFREQ_MS 10
  32. /* convert milliseconds to timer ticks */
  33. #define TIMER_MSEC2TICKS(x) ((x * F_CPU) / (TIMER_DIVISOR * 1000ULL))
  34. #define TIMER_MSEC2IRQCNT(x) (x / TIMER_IRQFREQ_MS)
  35. #define EV_NONE 0x00
  36. #define EV_STATE_ENTER 0x01
  37. #define EV_BUTTON_PRESSED 0x02
  38. #define EV_BUTTON_RELEASED 0x04
  39. #define EV_TIMEOUT 0x08
  40. #define EV_PROG_ENTER 0x10
  41. #define EV_PROG_LEAVE 0x20
  42. #define EV_PROG_ENTER_TWI 0x40
  43. #define STATE_IDLE 0x00 /* nothing */
  44. #define STATE_RESET_SYNC 0x01
  45. #define STATE_RESET_RETRY 0x02
  46. #define STATE_RESET_PROGMODE 0x03
  47. #define STATE_TWI_CHECK_BL 0x04
  48. #define STATE_TWI_PROGMODE 0x05
  49. #define LED_OFF 0x00
  50. #define LED_SLOW 0x20
  51. #define LED_FAST 0x08
  52. #define LED_ON 0x80
  53. static volatile uint8_t m_led_mode = LED_OFF;
  54. static volatile uint8_t m_reset_timer;
  55. static volatile uint8_t m_events;
  56. static uint8_t m_state;
  57. static uint8_t m_page_buf[256];
  58. static avr_device_t m_device;
  59. static uint16_t m_address = 0x0000;
  60. #if (USE_TWI_SUPPORT)
  61. static twi_chipinfo_t m_twi_chipinfo;
  62. static uint8_t m_twi_address;
  63. #endif /* (USE_TWI_SUPPORT) */
  64. static void reset_statemachine(uint8_t events)
  65. {
  66. static uint8_t reset_retries;
  67. static uint8_t reset_cause;
  68. uint8_t oldstate;
  69. uint8_t timer;
  70. /* shortcut: there is nothing to do */
  71. if ((events == EV_NONE) &&
  72. (m_events == EV_NONE)
  73. )
  74. {
  75. return;
  76. }
  77. cli();
  78. /* get button/timer events */
  79. events |= m_events;
  80. m_events = 0x00;
  81. /* disable timer */
  82. timer = m_reset_timer;
  83. m_reset_timer = 0x0000;
  84. sei();
  85. do {
  86. oldstate = m_state;
  87. switch (m_state)
  88. {
  89. case STATE_IDLE:
  90. if (events & EV_STATE_ENTER)
  91. {
  92. /* remove all events */
  93. events = EV_NONE;
  94. /* stop timer */
  95. timer = TIMER_MSEC2IRQCNT(0);
  96. spi_init(0);
  97. #if (USE_TWI_SUPPORT)
  98. twi_init(0);
  99. #endif
  100. /* put device in RUN mode */
  101. RESET_INACTIVE();
  102. m_led_mode = LED_OFF;
  103. }
  104. else if ((events & EV_PROG_ENTER) ||
  105. #if (USE_TWI_SUPPORT)
  106. ((events & EV_BUTTON_PRESSED) && (m_twi_address == 0x00))
  107. #else
  108. (events & EV_BUTTON_PRESSED)
  109. #endif /* (USE_TWI_SUPPORT) */
  110. )
  111. {
  112. reset_cause = events;
  113. events &= ~(EV_BUTTON_PRESSED | EV_PROG_ENTER);
  114. reset_retries = 5;
  115. /* enable SPI interface */
  116. spi_init(1);
  117. m_state = STATE_RESET_SYNC;
  118. }
  119. #if (USE_TWI_SUPPORT)
  120. else if ((events & EV_PROG_ENTER_TWI) ||
  121. ((events & EV_BUTTON_PRESSED) && (m_twi_address != 0x00))
  122. )
  123. {
  124. uint8_t result;
  125. reset_cause = events;
  126. events &= ~(EV_BUTTON_PRESSED | EV_PROG_ENTER_TWI);
  127. reset_retries = 5;
  128. twi_init(1);
  129. result = twi_switch_application(m_twi_address, BOOTTYPE_BOOTLOADER);
  130. if (result == TWI_NACK_ADDR)
  131. {
  132. /* no response from target, do normal reset */
  133. RESET_ACTIVE();
  134. }
  135. m_state = STATE_TWI_CHECK_BL;
  136. }
  137. #endif /* (USE_TWI_SUPPORT) */
  138. break;
  139. case STATE_RESET_SYNC:
  140. if (events & EV_STATE_ENTER)
  141. {
  142. events &= ~(EV_STATE_ENTER);
  143. timer = TIMER_MSEC2IRQCNT(10);
  144. /* put device in ISP mode */
  145. RESET_ACTIVE();
  146. m_led_mode = LED_ON;
  147. }
  148. else if (events & EV_TIMEOUT)
  149. {
  150. events &= ~(EV_TIMEOUT);
  151. memset(&m_device, 0x00, sizeof(avr_device_t));
  152. if (isp_enter_progmode())
  153. {
  154. isp_read_signature(m_device.sig);
  155. avrdevice_get_by_signature(&m_device, m_device.sig);
  156. m_state = STATE_RESET_PROGMODE;
  157. }
  158. else
  159. {
  160. m_state = STATE_RESET_RETRY;
  161. }
  162. }
  163. break;
  164. case STATE_RESET_RETRY:
  165. if (events & EV_STATE_ENTER)
  166. {
  167. events &= ~(EV_STATE_ENTER);
  168. timer = TIMER_MSEC2IRQCNT(50);
  169. /* put device in RUN mode */
  170. RESET_INACTIVE();
  171. m_led_mode = LED_OFF;
  172. }
  173. else if (events & EV_TIMEOUT)
  174. {
  175. events &= ~(EV_TIMEOUT);
  176. reset_retries--;
  177. if (reset_retries > 0)
  178. {
  179. /* try lower frequency */
  180. spi_set_clk(SPI_SET_CLK_DEC);
  181. m_state = STATE_RESET_SYNC;
  182. }
  183. else
  184. {
  185. /* got no sync */
  186. m_state = STATE_IDLE;
  187. }
  188. }
  189. break;
  190. case STATE_RESET_PROGMODE:
  191. if (events & EV_STATE_ENTER)
  192. {
  193. events &= ~(EV_STATE_ENTER);
  194. if (reset_cause == EV_BUTTON_PRESSED)
  195. {
  196. m_state = STATE_IDLE;
  197. }
  198. }
  199. else if (events & (EV_PROG_LEAVE | EV_BUTTON_PRESSED))
  200. {
  201. events &= ~(EV_PROG_LEAVE | EV_BUTTON_PRESSED);
  202. m_state = STATE_IDLE;
  203. }
  204. break;
  205. #if (USE_TWI_SUPPORT)
  206. case STATE_TWI_CHECK_BL:
  207. if (events & EV_STATE_ENTER)
  208. {
  209. events &= ~(EV_STATE_ENTER);
  210. timer = TIMER_MSEC2IRQCNT(10);
  211. }
  212. else if (events & EV_TIMEOUT)
  213. {
  214. uint8_t result;
  215. events &= ~(EV_TIMEOUT);
  216. /* put target in RUN mode */
  217. RESET_INACTIVE();
  218. m_led_mode = LED_ON;
  219. result = twi_read_chipinfo(m_twi_address, &m_twi_chipinfo);
  220. if (result == TWI_SUCCESS)
  221. {
  222. #if (USE_DISPLAY)
  223. char twi_version[16 +1];
  224. twi_read_version(m_twi_address, twi_version,
  225. sizeof(twi_version) -1);
  226. twi_version[16] = '\0';
  227. display_show_string(twi_version, 0);
  228. avrdevice_get_by_signature(&m_device, m_twi_chipinfo.sig);
  229. if (m_device.name[0] != '\0')
  230. {
  231. display_show_string(" ", 1);
  232. display_show_string(m_device.name, 1);
  233. }
  234. else
  235. {
  236. display_show_string(" 0X", 1);
  237. display_show_hex(m_twi_chipinfo.sig[0], 1);
  238. display_show_hex(m_twi_chipinfo.sig[1], 1);
  239. display_show_hex(m_twi_chipinfo.sig[2], 1);
  240. }
  241. display_set_mode(DISPLAY_MODE_SCROLL_ONCE);
  242. #endif /* (USE_DISPLAY) */
  243. m_state = STATE_TWI_PROGMODE;
  244. }
  245. else
  246. {
  247. reset_retries--;
  248. if (reset_retries > 0)
  249. {
  250. timer = TIMER_MSEC2IRQCNT(10);
  251. }
  252. else
  253. {
  254. #if (USE_DISPLAY)
  255. display_show_string("0x", 0);
  256. display_show_hex(m_twi_address, 1);
  257. display_show_string(":NAK", 1);
  258. display_set_mode(DISPLAY_MODE_SCROLL_ONCE);
  259. #endif /* (USE_DISPLAY) */
  260. m_state = STATE_IDLE;
  261. }
  262. }
  263. }
  264. break;
  265. case STATE_TWI_PROGMODE:
  266. if (events & EV_STATE_ENTER)
  267. {
  268. events &= ~(EV_STATE_ENTER);
  269. if (reset_cause == EV_BUTTON_PRESSED)
  270. {
  271. m_state = STATE_IDLE;
  272. }
  273. }
  274. else if (events & EV_PROG_LEAVE)
  275. {
  276. events &= ~(EV_PROG_LEAVE);
  277. m_state = STATE_IDLE;
  278. }
  279. if (m_state == STATE_IDLE)
  280. {
  281. twi_switch_application(m_twi_address, BOOTTYPE_APPLICATION);
  282. }
  283. break;
  284. #endif /* (USE_TWI_SUPPORT) */
  285. default:
  286. m_state = STATE_IDLE;
  287. break;
  288. }
  289. #if (USE_DISPLAY)
  290. if ((m_state == STATE_IDLE) &&
  291. ((oldstate == STATE_RESET_RETRY) ||
  292. (oldstate == STATE_RESET_PROGMODE)
  293. ))
  294. {
  295. if (m_device.name[0] != '\0')
  296. {
  297. display_show_string(m_device.name, 0);
  298. }
  299. else
  300. {
  301. display_show_string("unknown 0X", 0);
  302. display_show_hex(m_device.sig[0], 1);
  303. display_show_hex(m_device.sig[1], 1);
  304. display_show_hex(m_device.sig[2], 1);
  305. }
  306. display_set_mode(DISPLAY_MODE_SCROLL_ONCE);
  307. }
  308. #endif /* (USE_DISPLAY) */
  309. if (oldstate != m_state)
  310. {
  311. events |= EV_STATE_ENTER;
  312. }
  313. } while (oldstate != m_state);
  314. cli();
  315. /* start timer again */
  316. m_reset_timer = timer;
  317. sei();
  318. } /* reset_statemachine */
  319. static void reset_statemachine_wait(uint8_t events)
  320. {
  321. reset_statemachine(events);
  322. /* wait while timer is running or timer elapsed */
  323. while (m_reset_timer || m_events)
  324. {
  325. reset_statemachine(EV_NONE);
  326. }
  327. } /* reset_statemachine_wait */
  328. static void cmd_handler_isp(uint8_t cmd)
  329. {
  330. switch (cmd)
  331. {
  332. /* Write program memory, low byte */
  333. case 'c':
  334. m_led_mode = LED_FAST;
  335. isp_mem_write(CMD_LOAD_FLASH_LO, m_address, uart_recv());
  336. /* poll on byte addressed targets */
  337. if (m_device.pagemask == 0x00)
  338. {
  339. isp_mem_poll(&m_device);
  340. }
  341. uart_send('\r');
  342. break;
  343. /* Write program memory, high byte */
  344. case 'C':
  345. m_led_mode = LED_FAST;
  346. isp_mem_write(CMD_LOAD_FLASH_HI, m_address, uart_recv());
  347. /* poll on byte addressed targets */
  348. if (m_device.pagemask == 0x00)
  349. {
  350. isp_mem_poll(&m_device);
  351. }
  352. m_address++;
  353. uart_send('\r');
  354. break;
  355. /* Issue Page Write */
  356. case 'm':
  357. m_led_mode = LED_FAST;
  358. isp_mem_pagewrite();
  359. isp_mem_poll(&m_device);
  360. uart_send('\r');
  361. break;
  362. /* Read Lock Bits */
  363. case 'r':
  364. uart_send(isp_mem_read(CMD_READ_LOCK_1, CMD_READ_LOCK_2 << 8));
  365. uart_send('\r');
  366. break;
  367. /* Read program memory */
  368. case 'R':
  369. m_led_mode = LED_SLOW;
  370. uart_send(isp_mem_read(CMD_READ_FLASH_HI, m_address));
  371. uart_send(isp_mem_read(CMD_READ_FLASH_LO, m_address));
  372. m_address++;
  373. break;
  374. /* Read data memory */
  375. case 'd':
  376. m_led_mode = LED_SLOW;
  377. uart_send(isp_mem_read(CMD_READ_EEPROM, m_address));
  378. m_address++;
  379. break;
  380. /* Write data memory */
  381. case 'D':
  382. m_led_mode = LED_FAST;
  383. isp_mem_write(CMD_WRITE_EEPROM, m_address, uart_recv());
  384. isp_mem_poll(&m_device);
  385. m_address++;
  386. uart_send('\r');
  387. break;
  388. /* Chip erase */
  389. case 'e':
  390. isp_cmd4(CMD_CHIP_ERASE_1, CMD_CHIP_ERASE_2, 0x00, 0x00);
  391. uart_send('\r');
  392. break;
  393. /* Write lock bits */
  394. case 'l':
  395. isp_cmd4(CMD_WRITE_LOCK_1, CMD_WRITE_LOCK_2, 0x00, uart_recv());
  396. uart_send('\r');
  397. break;
  398. /* Read fusebits */
  399. case 'F':
  400. uart_send(isp_mem_read(CMD_READ_FUSE_1, CMD_READ_FUSE_2 << 8));
  401. break;
  402. /* Read high fusebits */
  403. case 'N':
  404. uart_send(isp_mem_read(CMD_READ_FUSE_H_1, CMD_READ_FUSE_H_2 << 8));
  405. break;
  406. /* Read extended fusebits */
  407. case 'Q':
  408. uart_send(isp_mem_read(CMD_READ_FUSE_E_1, CMD_READ_FUSE_E_2 << 8));
  409. break;
  410. /* Read signature bytes */
  411. case 's':
  412. uart_send(m_device.sig[2]);
  413. uart_send(m_device.sig[1]);
  414. uart_send(m_device.sig[0]);
  415. break;
  416. /* Block Write */
  417. case 'B':
  418. {
  419. uint16_t size, i;
  420. uint8_t type;
  421. m_led_mode = LED_FAST;
  422. size = uart_recv() << 8;
  423. size |= uart_recv();
  424. type = uart_recv();
  425. size = MIN(size, sizeof(m_page_buf));
  426. uart_recv_buf(m_page_buf, size);
  427. if (type == 'F')
  428. {
  429. for (i = 0; i < size; i += 2)
  430. {
  431. isp_mem_write(CMD_LOAD_FLASH_LO, m_address, m_page_buf[i]);
  432. isp_mem_write(CMD_LOAD_FLASH_HI, m_address, m_page_buf[i+1]);
  433. m_address++;
  434. if ((m_address & m_device.pagemask) == 0x00)
  435. {
  436. isp_mem_pagewrite();
  437. isp_mem_poll(&m_device);
  438. }
  439. }
  440. if ((m_device.pagemask != 0x00) &&
  441. (size != ((m_device.pagemask +1) << 1))
  442. )
  443. {
  444. isp_mem_pagewrite();
  445. isp_mem_poll(&m_device);
  446. }
  447. }
  448. else if (type == 'E')
  449. {
  450. for (i = 0; i < size; i++)
  451. {
  452. isp_mem_write(CMD_WRITE_EEPROM, m_address, m_page_buf[i]);
  453. isp_mem_poll(&m_device);
  454. m_address++;
  455. }
  456. }
  457. uart_send('\r');
  458. break;
  459. }
  460. /* Block Read */
  461. case 'g':
  462. {
  463. uint16_t size, i;
  464. uint8_t type;
  465. m_led_mode = LED_SLOW;
  466. size = uart_recv() << 8;
  467. size |= uart_recv();
  468. type = uart_recv();
  469. if (type == 'F')
  470. {
  471. for (i = 0; i < size; i += 2)
  472. {
  473. uart_send(isp_mem_read(CMD_READ_FLASH_LO, m_address));
  474. uart_send(isp_mem_read(CMD_READ_FLASH_HI, m_address));
  475. m_address++;
  476. }
  477. }
  478. else if (type == 'E')
  479. {
  480. for (i = 0; i < size; i++)
  481. {
  482. uart_send(isp_mem_read(CMD_READ_EEPROM, m_address));
  483. m_address++;
  484. }
  485. }
  486. break;
  487. }
  488. /* Write fusebits */
  489. case 'f':
  490. isp_cmd4(CMD_WRITE_FUSE_1, CMD_WRITE_FUSE_2, 0x00, uart_recv());
  491. uart_send('\r');
  492. break;
  493. /* Universial command */
  494. case ':':
  495. {
  496. uint8_t val[3];
  497. uart_recv_buf(val, sizeof(val));
  498. uart_send(isp_cmd3(val[0], val[1], val[2]));
  499. uart_send('\r');
  500. break;
  501. }
  502. /* New universal command */
  503. case '.':
  504. {
  505. uint8_t val[4];
  506. uart_recv_buf(val, sizeof(val));
  507. uart_send(isp_cmd4(val[0], val[1], val[2], val[3]));
  508. uart_send('\r');
  509. break;
  510. }
  511. default:
  512. uart_send('?');
  513. break;
  514. }
  515. } /* cmd_handler_isp */
  516. #if (USE_TWI_SUPPORT)
  517. static void cmd_handler_twi(uint8_t cmd)
  518. {
  519. switch (cmd)
  520. {
  521. /* Chip erase */
  522. case 'e':
  523. uart_send('\r');
  524. break;
  525. /* Read signature bytes */
  526. case 's':
  527. uart_send(m_twi_chipinfo.sig[2]);
  528. uart_send(m_twi_chipinfo.sig[1]);
  529. uart_send(m_twi_chipinfo.sig[0]);
  530. break;
  531. /* Block Write */
  532. case 'B':
  533. {
  534. uint16_t write_pos = 0;
  535. uint16_t size;
  536. uint8_t type;
  537. uint8_t result = TWI_SUCCESS;
  538. m_led_mode = LED_FAST;
  539. size = uart_recv() << 8;
  540. size |= uart_recv();
  541. type = uart_recv();
  542. size = MIN(size, sizeof(m_page_buf));
  543. uart_recv_buf(m_page_buf, size);
  544. memset(m_page_buf + size, 0xFF, sizeof(m_page_buf) - size);
  545. while ((write_pos < size) &&
  546. (result == TWI_SUCCESS)
  547. )
  548. {
  549. if (type == 'F')
  550. {
  551. result = twi_write_memory(m_twi_address, MEMTYPE_FLASH,
  552. (m_address << 1),
  553. m_page_buf + write_pos,
  554. m_twi_chipinfo.page_size);
  555. /* when accessing flash, m_address is a word address */
  556. m_address += (m_twi_chipinfo.page_size >> 1);
  557. write_pos += m_twi_chipinfo.page_size;
  558. }
  559. else
  560. {
  561. uint8_t write_size;
  562. /* one eeprom byte takes 3.5ms to programm */
  563. write_size = MIN(size, 4);
  564. result = twi_write_memory(m_twi_address, MEMTYPE_EEPROM,
  565. m_address,
  566. m_page_buf + write_pos,
  567. write_size);
  568. /* when accessing eeprom, m_address is a byte address */
  569. m_address += write_size;
  570. write_pos += write_size;
  571. }
  572. }
  573. uart_send((result == TWI_SUCCESS) ? '\r' : '!');
  574. break;
  575. }
  576. /* Block Read */
  577. case 'g':
  578. {
  579. uint16_t size;
  580. uint8_t type;
  581. m_led_mode = LED_SLOW;
  582. size = uart_recv() << 8;
  583. size |= uart_recv();
  584. type = uart_recv();
  585. size = MIN(size, sizeof(m_page_buf));
  586. if (type == 'F')
  587. {
  588. twi_read_memory(m_twi_address, MEMTYPE_FLASH,
  589. (m_address << 1),
  590. m_page_buf, size);
  591. m_address += (size >> 1);
  592. }
  593. else
  594. {
  595. twi_read_memory(m_twi_address, MEMTYPE_EEPROM,
  596. m_address,
  597. m_page_buf, size);
  598. m_address += size;
  599. }
  600. uart_send_buf(m_page_buf, size);
  601. break;
  602. }
  603. default:
  604. uart_send('?');
  605. break;
  606. }
  607. } /* cmd_handler_twi */
  608. #endif /* (USE_TWI_SUPPORT) */
  609. static void cmdloop(void) __attribute__ ((noreturn));
  610. static void cmdloop(void)
  611. {
  612. while (1)
  613. {
  614. uint8_t cmd;
  615. if (!uart_rx_ready())
  616. {
  617. reset_statemachine(EV_NONE);
  618. continue;
  619. }
  620. #if (USE_DISPLAY)
  621. if (m_state == STATE_RESET_PROGMODE)
  622. {
  623. uint16_t byte_address;
  624. byte_address = (m_address << 1);
  625. display_show_hex(byte_address >> 8, 0);
  626. display_show_hex(byte_address & 0xFF, 1);
  627. display_set_mode(DISPLAY_MODE_STATIC);
  628. }
  629. #endif /* (USE_DISPLAY) */
  630. cmd = uart_recv();
  631. switch (cmd)
  632. {
  633. /* Enter programming mode */
  634. case 'P':
  635. reset_statemachine_wait(EV_PROG_ENTER);
  636. uart_send((m_state == STATE_RESET_PROGMODE) ? '\r' : '!');
  637. #if (USE_TWI_SUPPORT)
  638. m_twi_address = 0x00;
  639. #endif
  640. break;
  641. /* Autoincrement address */
  642. case 'a':
  643. uart_send('Y');
  644. break;
  645. /* Set address */
  646. case 'A':
  647. m_address = (uart_recv() << 8);
  648. m_address |= uart_recv();
  649. uart_send('\r');
  650. break;
  651. /* Leave programming mode */
  652. case 'L':
  653. /* Exit Bootloader */
  654. case 'E':
  655. reset_statemachine_wait(EV_PROG_LEAVE);
  656. uart_send('\r');
  657. break;
  658. /* Select device type */
  659. case 'T':
  660. uart_recv(); // ignore
  661. uart_send('\r');
  662. break;
  663. /* Return supported device codes */
  664. case 't':
  665. avrdevice_iterate_devcodes(uart_send);
  666. uart_send(0x00);
  667. break;
  668. /* Return software identifier */
  669. case 'S':
  670. uart_send('A');
  671. uart_send('V');
  672. uart_send('R');
  673. uart_send('-');
  674. uart_send('I');
  675. uart_send('S');
  676. uart_send('P');
  677. break;
  678. /* Return software version */
  679. case 'V':
  680. uart_send('3');
  681. uart_send('8');
  682. break;
  683. /* Return hardware version */
  684. case 'v':
  685. uart_send('1');
  686. uart_send('2');
  687. break;
  688. /* Return programmer type */
  689. case 'p':
  690. uart_send('S');
  691. break;
  692. /* Set LED */
  693. case 'x':
  694. uart_recv();
  695. m_led_mode = LED_ON;
  696. break;
  697. /* Clear LED */
  698. case 'y':
  699. uart_recv();
  700. m_led_mode = LED_OFF;
  701. break;
  702. /* Report Block write Mode */
  703. case 'b':
  704. uart_send('Y');
  705. uart_send(sizeof(m_page_buf) >> 8);
  706. uart_send(sizeof(m_page_buf) & 0xFF);
  707. break;
  708. #if (USE_TWI_SUPPORT)
  709. case 'I':
  710. m_twi_address = uart_recv() & 0x7F;
  711. if (m_twi_address != 0x00)
  712. {
  713. reset_statemachine_wait(EV_PROG_ENTER_TWI);
  714. if (m_state == STATE_TWI_PROGMODE)
  715. {
  716. uart_send('\r');
  717. }
  718. else
  719. {
  720. uart_send('!');
  721. }
  722. }
  723. else
  724. {
  725. uart_send('\r');
  726. }
  727. break;
  728. case 'i':
  729. {
  730. uint8_t twi_addr;
  731. uint8_t write_size;
  732. uint8_t read_size;
  733. uint8_t result;
  734. twi_addr = uart_recv();
  735. write_size = uart_recv();
  736. read_size = uart_recv();
  737. uart_recv_buf(m_page_buf, write_size);
  738. result = twi_generic(twi_addr,
  739. m_page_buf, write_size,
  740. m_page_buf, read_size);
  741. uart_send_buf(m_page_buf, read_size);
  742. uart_send((result == TWI_SUCCESS) ? '\r' : '!');
  743. break;
  744. }
  745. #endif /* (USE_TWI_SUPPORT) */
  746. /* ESC */
  747. case 0x1B:
  748. break;
  749. default:
  750. #if (USE_TWI_SUPPORT)
  751. if (m_twi_address != 0x00)
  752. {
  753. cmd_handler_twi(cmd);
  754. }
  755. else
  756. #endif /* (USE_TWI_SUPPORT) */
  757. {
  758. cmd_handler_isp(cmd);
  759. }
  760. break;
  761. }
  762. }
  763. } /* cmdloop */
  764. /* time keeping */
  765. ISR(TIMER0_OVF_vect)
  766. {
  767. /* restart timer */
  768. TCNT0 = 0xFF - TIMER_MSEC2TICKS(TIMER_IRQFREQ_MS);
  769. static uint8_t prev_pressed;
  770. if (ISP_CHECK())
  771. {
  772. if (!prev_pressed)
  773. {
  774. m_events |= EV_BUTTON_PRESSED;
  775. prev_pressed = 1;
  776. }
  777. }
  778. else
  779. {
  780. if (prev_pressed)
  781. {
  782. m_events |= EV_BUTTON_RELEASED;
  783. prev_pressed = 0;
  784. }
  785. }
  786. if (m_reset_timer)
  787. {
  788. m_reset_timer--;
  789. if (m_reset_timer == 0)
  790. {
  791. m_events |= EV_TIMEOUT;
  792. }
  793. }
  794. /* update LED */
  795. static uint8_t led_timer;
  796. if (m_led_mode & ((led_timer++ & 0xFF) | 0x80))
  797. {
  798. ISP_LED_ON();
  799. }
  800. else
  801. {
  802. ISP_LED_OFF();
  803. }
  804. #if (USE_DISPLAY)
  805. display_update();
  806. #endif /* (USE_DISPLAY) */
  807. } /* TIMER0_OVF_vect */
  808. #if defined(__AVR_ATmega328P__)
  809. /*
  810. * For newer devices the watchdog timer remains active even after a
  811. * system reset. So disable it as soon as possible.
  812. * automagically called on startup
  813. */
  814. void disable_wdt_timer(void) __attribute__((naked, section(".init3")));
  815. void disable_wdt_timer(void)
  816. {
  817. MCUSR = 0;
  818. WDTCSR = (1<<WDCE) | (1<<WDE);
  819. WDTCSR = (0<<WDE);
  820. } /* disable_wdt_timer */
  821. #endif /* defined(__AVR_ATmega328P__) */
  822. int main(void)
  823. {
  824. GPIO_INIT();
  825. uart_init();
  826. spi_init(0);
  827. #if (USE_TWI_SUPPORT)
  828. twi_init(0);
  829. #endif
  830. TIMER_INIT();
  831. /* init statemachine */
  832. reset_statemachine(EV_BUTTON_PRESSED);
  833. sei();
  834. cmdloop();
  835. } /* main */