fpga.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. uart_putc('P');
  87. fpga_set_prog_b(1);
  88. while(!fpga_get_initb()){
  89. if(getticks() > timeout) {
  90. printf("no response from FPGA trying to initiate configuration!\n");
  91. led_panic();
  92. }
  93. };
  94. LPC_GPIO2->FIOMASK1 = ~(BV(0));
  95. uart_putc('p');
  96. /* open configware file */
  97. file_open(filename, FA_READ);
  98. if(file_res) {
  99. uart_putc('?');
  100. uart_putc(0x30+file_res);
  101. return;
  102. }
  103. uart_putc('C');
  104. for (;;) {
  105. data = rle_file_getc();
  106. i++;
  107. if (file_status || file_res) break; /* error or eof */
  108. FPGA_SEND_BYTE_SERIAL(data);
  109. }
  110. uart_putc('c');
  111. file_close();
  112. printf("fpga_pgm: %d bytes programmed\n", i);
  113. delay_ms(1);
  114. } while (!fpga_get_done() && retries--);
  115. if(!fpga_get_done()) {
  116. printf("FPGA failed to configure after %d tries.\n", MAXRETRIES);
  117. led_panic();
  118. }
  119. printf("FPGA configured\n");
  120. fpga_postinit();
  121. }
  122. void fpga_rompgm() {
  123. int MAXRETRIES = 10;
  124. int retries = MAXRETRIES;
  125. uint8_t data;
  126. int i;
  127. tick_t timeout;
  128. do {
  129. i=0;
  130. timeout = getticks() + 100;
  131. fpga_set_prog_b(0);
  132. uart_putc('P');
  133. fpga_set_prog_b(1);
  134. while(!fpga_get_initb()){
  135. if(getticks() > timeout) {
  136. printf("no response from FPGA trying to initiate configuration!\n");
  137. led_panic();
  138. }
  139. };
  140. LPC_GPIO2->FIOMASK1 = ~(BV(0));
  141. uart_putc('p');
  142. /* open configware file */
  143. rle_mem_init(cfgware, sizeof(cfgware));
  144. printf("sizeof(cfgware) = %d\n", sizeof(cfgware));
  145. for (;;) {
  146. data = rle_mem_getc();
  147. if(rle_state) break;
  148. i++;
  149. FPGA_SEND_BYTE_SERIAL(data);
  150. }
  151. uart_putc('c');
  152. printf("fpga_pgm: %d bytes programmed\n", i);
  153. delay_ms(1);
  154. } while (!fpga_get_done() && retries--);
  155. if(!fpga_get_done()) {
  156. printf("FPGA failed to configure after %d tries.\n", MAXRETRIES);
  157. led_panic();
  158. }
  159. printf("FPGA configured\n");
  160. fpga_postinit();
  161. }