fpga.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* sd2snes - SD card based universal cartridge for the SNES
  2. Copyright (C) 2009-2010 Maximilian Rehkopf <otakon@gmx.net>
  3. AVR firmware portion
  4. Inspired by and based on code from sd2iec, written by Ingo Korb et al.
  5. See sdcard.c|h, config.h.
  6. FAT file system access based on code by ChaN, Jim Brain, Ingo Korb,
  7. see ff.c|h.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; version 2 of the License only.
  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. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. fpga.c: FPGA (re)configuration
  19. */
  20. /*
  21. FPGA pin mapping
  22. ================
  23. CCLK P0.11 out
  24. PROG_B P1.15 out
  25. INIT_B P2.9 in
  26. DIN P2.8 out
  27. DONE P0.22 in
  28. */
  29. #include <arm/NXP/LPC17xx/LPC17xx.h>
  30. #include "bits.h"
  31. #include "fpga.h"
  32. #include "fpga_spi.h"
  33. #include "config.h"
  34. #include "uart.h"
  35. #include "diskio.h"
  36. #include "integer.h"
  37. #include "ff.h"
  38. #include "fileops.h"
  39. #include "spi.h"
  40. #include "led.h"
  41. #include "timer.h"
  42. #include "rle.h"
  43. #include "cfgware.h"
  44. void fpga_set_prog_b(uint8_t val) {
  45. if(val)
  46. BITBAND(PROGBREG->FIOSET, PROGBBIT) = 1;
  47. else
  48. BITBAND(PROGBREG->FIOCLR, PROGBBIT) = 1;
  49. }
  50. void fpga_set_cclk(uint8_t val) {
  51. if(val)
  52. BITBAND(CCLKREG->FIOSET, CCLKBIT) = 1;
  53. else
  54. BITBAND(CCLKREG->FIOCLR, CCLKBIT) = 1;
  55. }
  56. int fpga_get_initb() {
  57. return BITBAND(INITBREG->FIOPIN, INITBBIT);
  58. }
  59. void fpga_init() {
  60. /* mainly GPIO directions */
  61. BITBAND(CCLKREG->FIODIR, CCLKBIT) = 1; /* CCLK */
  62. BITBAND(DONEREG->FIODIR, DONEBIT) = 0; /* DONE */
  63. BITBAND(PROGBREG->FIODIR, PROGBBIT) = 1; /* PROG_B */
  64. BITBAND(DINREG->FIODIR, DINBIT) = 1; /* DIN */
  65. BITBAND(INITBREG->FIODIR, INITBBIT) = 0; /* INIT_B */
  66. LPC_GPIO2->FIOMASK1 = 0;
  67. SPI_OFFLOAD=0;
  68. fpga_set_cclk(0); /* initial clk=0 */
  69. }
  70. int fpga_get_done(void) {
  71. return BITBAND(DONEREG->FIOPIN, DONEBIT);
  72. }
  73. void fpga_postinit() {
  74. LPC_GPIO2->FIOMASK1 = 0;
  75. }
  76. void fpga_pgm(uint8_t* filename) {
  77. int MAXRETRIES = 10;
  78. int retries = MAXRETRIES;
  79. uint8_t data;
  80. int i;
  81. tick_t timeout;
  82. do {
  83. i=0;
  84. timeout = getticks() + 100;
  85. fpga_set_prog_b(0);
  86. if(BITBAND(PROGBREG->FIOPIN, PROGBBIT)) {
  87. printf("PROGB is stuck high!\n");
  88. led_panic();
  89. }
  90. uart_putc('P');
  91. fpga_set_prog_b(1);
  92. while(!fpga_get_initb()){
  93. if(getticks() > timeout) {
  94. printf("no response from FPGA trying to initiate configuration!\n");
  95. led_panic();
  96. }
  97. };
  98. if(fpga_get_done()) {
  99. printf("DONE is stuck high!\n");
  100. led_panic();
  101. }
  102. LPC_GPIO2->FIOMASK1 = ~(BV(0));
  103. uart_putc('p');
  104. /* open configware file */
  105. file_open(filename, FA_READ);
  106. if(file_res) {
  107. uart_putc('?');
  108. uart_putc(0x30+file_res);
  109. return;
  110. }
  111. uart_putc('C');
  112. for (;;) {
  113. data = rle_file_getc();
  114. i++;
  115. if (file_status || file_res) break; /* error or eof */
  116. FPGA_SEND_BYTE_SERIAL(data);
  117. }
  118. uart_putc('c');
  119. file_close();
  120. printf("fpga_pgm: %d bytes programmed\n", i);
  121. delay_ms(1);
  122. } while (!fpga_get_done() && retries--);
  123. if(!fpga_get_done()) {
  124. printf("FPGA failed to configure after %d tries.\n", MAXRETRIES);
  125. led_panic();
  126. }
  127. printf("FPGA configured\n");
  128. fpga_postinit();
  129. }
  130. void fpga_rompgm() {
  131. int MAXRETRIES = 10;
  132. int retries = MAXRETRIES;
  133. uint8_t data;
  134. int i;
  135. tick_t timeout;
  136. do {
  137. i=0;
  138. timeout = getticks() + 100;
  139. fpga_set_prog_b(0);
  140. uart_putc('P');
  141. fpga_set_prog_b(1);
  142. while(!fpga_get_initb()){
  143. if(getticks() > timeout) {
  144. printf("no response from FPGA trying to initiate configuration!\n");
  145. led_panic();
  146. }
  147. };
  148. if(fpga_get_done()) {
  149. printf("DONE is stuck high!\n");
  150. led_panic();
  151. }
  152. LPC_GPIO2->FIOMASK1 = ~(BV(0));
  153. uart_putc('p');
  154. /* open configware file */
  155. rle_mem_init(cfgware, sizeof(cfgware));
  156. printf("sizeof(cfgware) = %d\n", sizeof(cfgware));
  157. for (;;) {
  158. data = rle_mem_getc();
  159. if(rle_state) break;
  160. i++;
  161. FPGA_SEND_BYTE_SERIAL(data);
  162. }
  163. uart_putc('c');
  164. printf("fpga_pgm: %d bytes programmed\n", i);
  165. delay_ms(1);
  166. } while (!fpga_get_done() && retries--);
  167. if(!fpga_get_done()) {
  168. printf("FPGA failed to configure after %d tries.\n", MAXRETRIES);
  169. led_panic();
  170. }
  171. printf("FPGA configured\n");
  172. fpga_postinit();
  173. }