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.

234 lines
7.0 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2006 - 2020 by Olaf Rempel *
  3. * razzor AT kopf MINUS tisch DOT de *
  4. * *
  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; version 2 of the License, *
  8. * *
  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. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include <avr/io.h>
  20. #include "avrdevice.h"
  21. #include "spi_isp.h"
  22. #include "target.h"
  23. #include <util/delay.h>
  24. /* F_CPU /4 (1.8432MHz) */
  25. #define SPI_MODE4 ((1<<SPE) | (1<<MSTR))
  26. /* F_CPU /16 (460.8kHz) */
  27. #define SPI_MODE3 ((1<<SPE) | (1<<MSTR) | (1<<SPR0))
  28. /* F_CPU /64 (115.2kHz) */
  29. #define SPI_MODE2 ((1<<SPE) | (1<<MSTR) | (1<<SPR1))
  30. /* F_CPU /128 (57.6kHz) */
  31. #define SPI_MODE1 ((1<<SPE) | (1<<MSTR) | (1<<SPR1) | (1<<SPR0))
  32. static const uint8_t m_spi_modes[4] = { SPI_MODE1, SPI_MODE2, SPI_MODE3, SPI_MODE4 };
  33. static uint8_t m_last_cmd;
  34. static uint8_t m_last_val;
  35. static uint16_t m_last_addr;
  36. /* ***********************************************************************
  37. * spi_rxtx
  38. * - transmit one byte to target, and return received one
  39. * *********************************************************************** */
  40. static uint8_t spi_rxtx(uint8_t val)
  41. {
  42. SPDR = val;
  43. loop_until_bit_is_set(SPSR, SPIF);
  44. return SPDR;
  45. } /* spi_rxtx */
  46. /* ***********************************************************************
  47. * spi_set_clk
  48. * *********************************************************************** */
  49. void spi_set_clk(uint8_t mode)
  50. {
  51. static uint8_t spi_speed;
  52. if (mode == SPI_SET_CLK_MAX)
  53. {
  54. spi_speed = (sizeof(m_spi_modes) -1);
  55. }
  56. else if ((mode == SPI_SET_CLK_DEC) &&
  57. (spi_speed > 0)
  58. )
  59. {
  60. spi_speed--;
  61. }
  62. /* enable SPI master mode */
  63. SPCR = m_spi_modes[spi_speed];
  64. } /* spi_set_clk */
  65. /* ***********************************************************************
  66. * spi_init
  67. * *********************************************************************** */
  68. void spi_init(uint8_t enable)
  69. {
  70. if (enable)
  71. {
  72. ISP_ACTIVE();
  73. spi_set_clk(SPI_SET_CLK_MAX);
  74. }
  75. else
  76. {
  77. ISP_INACTIVE();
  78. SPCR = 0x00;
  79. }
  80. } /* spi_init */
  81. /* ***********************************************************************
  82. * isp_enter_progmode
  83. * *********************************************************************** */
  84. uint8_t isp_enter_progmode(void)
  85. {
  86. uint8_t sync;
  87. spi_rxtx(CMD_PROG_ENABLE_1);
  88. spi_rxtx(CMD_PROG_ENABLE_2);
  89. sync = spi_rxtx(0x00);
  90. spi_rxtx(0x00);
  91. return (sync == CMD_PROG_ENABLE_2);
  92. } /* isp_enter_progmode */
  93. /* ***********************************************************************
  94. * isp_mem_write
  95. * - write a byte to target flash/eeprom
  96. * *********************************************************************** */
  97. void isp_mem_write(uint8_t cmd, uint16_t addr, uint8_t val)
  98. {
  99. spi_rxtx(cmd);
  100. spi_rxtx(addr >> 8);
  101. spi_rxtx(addr & 0xFF);
  102. spi_rxtx(val);
  103. /* remember values for polling */
  104. m_last_cmd = cmd;
  105. m_last_addr = addr;
  106. m_last_val = val;
  107. } /* isp_mem_write */
  108. /* ***********************************************************************
  109. * isp_mem_read
  110. * - read a byte from target flash/eeprom
  111. * *********************************************************************** */
  112. uint8_t isp_mem_read(uint8_t cmd, uint16_t addr)
  113. {
  114. spi_rxtx(cmd);
  115. spi_rxtx(addr >> 8);
  116. spi_rxtx(addr & 0xFF);
  117. return spi_rxtx(0x00);
  118. } /* isp_mem_read */
  119. /* ***********************************************************************
  120. * isp_mem_poll
  121. * - wait until byte/page is written to target memory
  122. * *********************************************************************** */
  123. void isp_mem_poll(const avr_device_t * p_device)
  124. {
  125. uint8_t cmd, val, poll = 0xFF;
  126. if (((m_last_val == 0x00) && (p_device->flags & POLL_00)) ||
  127. ((m_last_val == 0x7F) && (p_device->flags & POLL_7F)) ||
  128. ((m_last_val == 0x80) && (p_device->flags & POLL_80)) ||
  129. ((m_last_val == 0xFF) && (p_device->flags & POLL_FF))
  130. ) {
  131. /* wait default time */
  132. _delay_ms(15);
  133. return;
  134. }
  135. if (m_last_cmd == CMD_WRITE_EEPROM) {
  136. cmd = CMD_READ_EEPROM;
  137. } else {
  138. /* CMD_WRITE_FLASH -> CMD_READ_FLASH */
  139. cmd = (m_last_cmd & 0x08) | 0x20;
  140. }
  141. /* poll until we get correct value */
  142. do {
  143. val = isp_mem_read(cmd, m_last_addr);
  144. } while ((val != m_last_val) && poll--);
  145. } /* isp_mem_poll */
  146. /* ***********************************************************************
  147. * isp_mem_pagewrite
  148. * *********************************************************************** */
  149. void isp_mem_pagewrite(void)
  150. {
  151. spi_rxtx(CMD_WRITE_FLASH_PAGE);
  152. spi_rxtx(m_last_addr >> 8);
  153. spi_rxtx(m_last_addr & 0xFF);
  154. spi_rxtx(0x00);
  155. } /* isp_mem_pagewrite */
  156. /* ***********************************************************************
  157. * isp_read_signature
  158. * *********************************************************************** */
  159. void isp_read_signature(uint8_t * p_signature)
  160. {
  161. uint8_t i;
  162. for (i = 0; i < 3; i++)
  163. {
  164. p_signature[i] = isp_mem_read(CMD_READ_SIG_1, (CMD_READ_SIG_2 << 8) | i);
  165. }
  166. } /* isp_read_signature */
  167. /* ***********************************************************************
  168. * isp_cmd3
  169. * *********************************************************************** */
  170. uint8_t isp_cmd3(uint8_t cmd1, uint8_t cmd2,
  171. uint8_t cmd3)
  172. {
  173. uint8_t result;
  174. spi_rxtx(cmd1);
  175. spi_rxtx(cmd2);
  176. result = spi_rxtx(cmd3);
  177. _delay_ms(10);
  178. return result;
  179. } /* isp_cmd3 */
  180. /* ***********************************************************************
  181. * isp_cmd4
  182. * *********************************************************************** */
  183. uint8_t isp_cmd4(uint8_t cmd1, uint8_t cmd2,
  184. uint8_t cmd3, uint8_t cmd4)
  185. {
  186. uint8_t result;
  187. spi_rxtx(cmd1);
  188. spi_rxtx(cmd2);
  189. spi_rxtx(cmd3);
  190. result = spi_rxtx(cmd4);
  191. _delay_ms(10);
  192. return result;
  193. } /* isp_cmd3 */