fpga.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. {
  46. if ( val )
  47. {
  48. BITBAND( PROGBREG->FIOSET, PROGBBIT ) = 1;
  49. }
  50. else
  51. {
  52. BITBAND( PROGBREG->FIOCLR, PROGBBIT ) = 1;
  53. }
  54. }
  55. void fpga_set_cclk( uint8_t val )
  56. {
  57. if ( val )
  58. {
  59. BITBAND( CCLKREG->FIOSET, CCLKBIT ) = 1;
  60. }
  61. else
  62. {
  63. BITBAND( CCLKREG->FIOCLR, CCLKBIT ) = 1;
  64. }
  65. }
  66. int fpga_get_initb()
  67. {
  68. return BITBAND( INITBREG->FIOPIN, INITBBIT );
  69. }
  70. void fpga_init()
  71. {
  72. /* mainly GPIO directions */
  73. BITBAND( CCLKREG->FIODIR, CCLKBIT ) = 1; /* CCLK */
  74. BITBAND( DONEREG->FIODIR, DONEBIT ) = 0; /* DONE */
  75. BITBAND( PROGBREG->FIODIR, PROGBBIT ) = 1; /* PROG_B */
  76. BITBAND( DINREG->FIODIR, DINBIT ) = 1; /* DIN */
  77. BITBAND( INITBREG->FIODIR, INITBBIT ) = 0; /* INIT_B */
  78. LPC_GPIO2->FIOMASK1 = 0;
  79. SPI_OFFLOAD = 0;
  80. fpga_set_cclk( 0 ); /* initial clk=0 */
  81. }
  82. int fpga_get_done( void )
  83. {
  84. return BITBAND( DONEREG->FIOPIN, DONEBIT );
  85. }
  86. void fpga_postinit()
  87. {
  88. LPC_GPIO2->FIOMASK1 = 0;
  89. }
  90. void fpga_pgm( uint8_t *filename )
  91. {
  92. int MAXRETRIES = 10;
  93. int retries = MAXRETRIES;
  94. uint8_t data;
  95. int i;
  96. tick_t timeout;
  97. do
  98. {
  99. i = 0;
  100. timeout = getticks() + 100;
  101. fpga_set_prog_b( 0 );
  102. if ( BITBAND( PROGBREG->FIOPIN, PROGBBIT ) )
  103. {
  104. printf( "PROGB is stuck high!\n" );
  105. led_panic();
  106. }
  107. uart_putc( 'P' );
  108. fpga_set_prog_b( 1 );
  109. while ( !fpga_get_initb() )
  110. {
  111. if ( getticks() > timeout )
  112. {
  113. printf( "no response from FPGA trying to initiate configuration!\n" );
  114. led_panic();
  115. }
  116. };
  117. if ( fpga_get_done() )
  118. {
  119. printf( "DONE is stuck high!\n" );
  120. led_panic();
  121. }
  122. LPC_GPIO2->FIOMASK1 = ~( BV( 0 ) );
  123. uart_putc( 'p' );
  124. /* open configware file */
  125. file_open( filename, FA_READ );
  126. if ( file_res )
  127. {
  128. uart_putc( '?' );
  129. uart_putc( 0x30 + file_res );
  130. return;
  131. }
  132. uart_putc( 'C' );
  133. for ( ;; )
  134. {
  135. data = rle_file_getc();
  136. i++;
  137. if ( file_status || file_res )
  138. {
  139. break; /* error or eof */
  140. }
  141. FPGA_SEND_BYTE_SERIAL( data );
  142. }
  143. uart_putc( 'c' );
  144. file_close();
  145. printf( "fpga_pgm: %d bytes programmed\n", i );
  146. delay_ms( 1 );
  147. }
  148. while ( !fpga_get_done() && retries-- );
  149. if ( !fpga_get_done() )
  150. {
  151. printf( "FPGA failed to configure after %d tries.\n", MAXRETRIES );
  152. led_panic();
  153. }
  154. printf( "FPGA configured\n" );
  155. fpga_postinit();
  156. }
  157. void fpga_rompgm()
  158. {
  159. int MAXRETRIES = 10;
  160. int retries = MAXRETRIES;
  161. uint8_t data;
  162. int i;
  163. tick_t timeout;
  164. do
  165. {
  166. i = 0;
  167. timeout = getticks() + 100;
  168. fpga_set_prog_b( 0 );
  169. uart_putc( 'P' );
  170. fpga_set_prog_b( 1 );
  171. while ( !fpga_get_initb() )
  172. {
  173. if ( getticks() > timeout )
  174. {
  175. printf( "no response from FPGA trying to initiate configuration!\n" );
  176. led_panic();
  177. }
  178. };
  179. if ( fpga_get_done() )
  180. {
  181. printf( "DONE is stuck high!\n" );
  182. led_panic();
  183. }
  184. LPC_GPIO2->FIOMASK1 = ~( BV( 0 ) );
  185. uart_putc( 'p' );
  186. /* open configware file */
  187. rle_mem_init( cfgware, sizeof( cfgware ) );
  188. printf( "sizeof(cfgware) = %d\n", sizeof( cfgware ) );
  189. for ( ;; )
  190. {
  191. data = rle_mem_getc();
  192. if ( rle_state )
  193. {
  194. break;
  195. }
  196. i++;
  197. FPGA_SEND_BYTE_SERIAL( data );
  198. }
  199. uart_putc( 'c' );
  200. printf( "fpga_pgm: %d bytes programmed\n", i );
  201. delay_ms( 1 );
  202. }
  203. while ( !fpga_get_done() && retries-- );
  204. if ( !fpga_get_done() )
  205. {
  206. printf( "FPGA failed to configure after %d tries.\n", MAXRETRIES );
  207. led_panic();
  208. }
  209. printf( "FPGA configured\n" );
  210. fpga_postinit();
  211. }