fpga.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. void fpga_set_prog_b(uint8_t val) {
  44. if(val)
  45. BITBAND(PROGBREG->FIOSET, PROGBBIT) = 1;
  46. else
  47. BITBAND(PROGBREG->FIOCLR, PROGBBIT) = 1;
  48. }
  49. void fpga_set_cclk(uint8_t val) {
  50. if(val)
  51. BITBAND(CCLKREG->FIOSET, CCLKBIT) = 1;
  52. else
  53. BITBAND(CCLKREG->FIOCLR, CCLKBIT) = 1;
  54. }
  55. int fpga_get_initb() {
  56. return BITBAND(INITBREG->FIOPIN, INITBBIT);
  57. }
  58. void fpga_init() {
  59. /* mainly GPIO directions */
  60. BITBAND(CCLKREG->FIODIR, CCLKBIT) = 1; /* CCLK */
  61. BITBAND(DONEREG->FIODIR, DONEBIT) = 0; /* DONE */
  62. BITBAND(PROGBREG->FIODIR, PROGBBIT) = 1; /* PROG_B */
  63. BITBAND(DINREG->FIODIR, DINBIT) = 1; /* DIN */
  64. BITBAND(INITBREG->FIODIR, INITBBIT) = 0; /* INIT_B */
  65. LPC_GPIO2->FIOMASK1 = 0;
  66. SPI_OFFLOAD=0;
  67. fpga_set_cclk(0); /* initial clk=0 */
  68. }
  69. int fpga_get_done(void) {
  70. return BITBAND(DONEREG->FIOPIN, DONEBIT);
  71. }
  72. void fpga_postinit() {
  73. LPC_GPIO2->FIOMASK1 = 0;
  74. }
  75. void fpga_pgm(uint8_t* filename) {
  76. int MAXRETRIES = 10;
  77. int retries = MAXRETRIES;
  78. uint8_t data;
  79. int i;
  80. tick_t timeout;
  81. do {
  82. i=0;
  83. timeout = getticks() + 100;
  84. fpga_set_prog_b(0);
  85. uart_putc('P');
  86. fpga_set_prog_b(1);
  87. while(!fpga_get_initb()){
  88. if(getticks() > timeout) {
  89. printf("no response from FPGA trying to initiate configuration!\n");
  90. led_panic();
  91. }
  92. };
  93. LPC_GPIO2->FIOMASK1 = ~(BV(0));
  94. uart_putc('p');
  95. /* open configware file */
  96. file_open(filename, FA_READ);
  97. if(file_res) {
  98. uart_putc('?');
  99. uart_putc(0x30+file_res);
  100. return;
  101. }
  102. uart_putc('C');
  103. for (;;) {
  104. data = rle_file_getc();
  105. i++;
  106. if (file_status || file_res) break; /* error or eof */
  107. FPGA_SEND_BYTE_SERIAL(data);
  108. }
  109. uart_putc('c');
  110. file_close();
  111. printf("fpga_pgm: %d bytes programmed\n", i);
  112. delay_ms(1);
  113. } while (!fpga_get_done() && retries--);
  114. if(!fpga_get_done()) {
  115. printf("FPGA failed to configure after %d tries.\n", MAXRETRIES);
  116. led_panic();
  117. }
  118. printf("FPGA configured\n");
  119. fpga_postinit();
  120. }