fpga.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // insert cool lenghty disclaimer here
  2. // fpga.c: FPGA (re)programming
  3. /*
  4. FPGA pin mapping
  5. ================
  6. FPGA AVR dir
  7. ------------------------
  8. PROG_B PD3 OUT
  9. CCLK PD4 OUT
  10. CS_B PD7 OUT
  11. INIT_B PB2 IN
  12. RDWR_B PB3 OUT
  13. D7 PC0 OUT
  14. D6 PC1 OUT
  15. D5 PC2 OUT
  16. D4 PC3 OUT
  17. D3 PC4 OUT
  18. D2 PC5 OUT
  19. D1 PC6 OUT
  20. D0 PC7 OUT
  21. */
  22. #include <avr/pgmspace.h>
  23. #include "fpga.h"
  24. #include "config.h"
  25. #include "uart.h"
  26. #include "sdcard.h"
  27. #include "diskio.h"
  28. #include "ff.h"
  29. #include "fileops.h"
  30. DWORD get_fattime(void) {
  31. return 0L;
  32. }
  33. void set_prog_b(uint8_t val) {
  34. if(val) {
  35. PORTD |= _BV(PD3);
  36. } else {
  37. PORTD &= ~_BV(PD3);
  38. }
  39. }
  40. void set_cs_b(uint8_t val) {
  41. if(val) {
  42. PORTD |= _BV(PD7);
  43. } else {
  44. PORTD &= ~_BV(PD7);
  45. }
  46. }
  47. void set_rdwr_b(uint8_t val) {
  48. if(val) {
  49. PORTB |= _BV(PB3);
  50. } else {
  51. PORTB &= ~_BV(PB3);
  52. }
  53. }
  54. void set_cclk(uint8_t val) {
  55. if(val) {
  56. PORTD |= _BV(PD4);
  57. } else {
  58. PORTD &= ~_BV(PD4);
  59. }
  60. }
  61. void fpga_init() {
  62. DDRB |= _BV(PB3); // PB3 is output
  63. DDRB &= ~_BV(PB2); // PB2 is input
  64. DDRC = 0xff; // for FPGA config, all PORTC pins are outputs
  65. DDRD |= _BV(PD3) | _BV(PD4) | _BV(PD7); // PD3, PD4, PD7 are outputs
  66. set_cclk(0); // initial clk=0
  67. }
  68. int fpga_get_done(void) {
  69. return 0;
  70. }
  71. void fpga_postinit() {
  72. DDRA |= _BV(PA0) | _BV(PA1) | _BV(PA2) | _BV(PA4) | _BV(PA5) | _BV(PA6); // MAPPER+NEXTADDR output
  73. DDRB |= _BV(PB2) | _BV(PB1) | _BV(PB0); // turn PB2 into output, enable AVR_BANK
  74. }
  75. void fpga_pgm(char* filename) {
  76. set_prog_b(0);
  77. uart_putc('P');
  78. set_prog_b(1);
  79. loop_until_bit_is_set(PINB, PB2);
  80. uart_putc('p');
  81. FIL in;
  82. FRESULT res;
  83. UINT bytes_read;
  84. // open configware file
  85. res=f_open(&in, filename, FA_READ);
  86. if(res) {
  87. uart_putc('?');
  88. return;
  89. }
  90. // file open successful
  91. set_cs_b(0);
  92. set_rdwr_b(0);
  93. for (;;) {
  94. res = f_read(&in, file_buf, sizeof(file_buf), &bytes_read);
  95. if (res || bytes_read == 0) break; // error or eof
  96. for(int i=0; i<bytes_read; i++) {
  97. FPGA_SEND_BYTE(file_buf[i]);
  98. }
  99. }
  100. f_close(&in);
  101. fpga_postinit();
  102. }
  103. void set_avr_read(uint8_t val) {
  104. if(val) {
  105. PORTB |= _BV(PB3);
  106. } else {
  107. PORTB &= ~_BV(PB3);
  108. }
  109. }
  110. void set_avr_write(uint8_t val) {
  111. if(val) {
  112. PORTB |= _BV(PB2);
  113. } else {
  114. PORTB &= ~_BV(PB2);
  115. }
  116. }
  117. void set_avr_ena(uint8_t val) {
  118. if(val) {
  119. PORTD |= _BV(PD7);
  120. } else {
  121. PORTD &= ~_BV(PD7);
  122. }
  123. }
  124. void set_avr_nextaddr(uint8_t val) {
  125. if(val) {
  126. PORTA |= _BV(PA4);
  127. } else {
  128. PORTA &= ~_BV(PA4);
  129. }
  130. }
  131. void set_avr_addr_reset(uint8_t val) {
  132. if(val) {
  133. PORTA |= _BV(PA5);
  134. } else {
  135. PORTA &= ~_BV(PA5);
  136. }
  137. }
  138. void set_avr_data(uint8_t data) {
  139. PORTC = data;
  140. }
  141. void set_avr_addr_en(uint8_t val) {
  142. if(val) {
  143. PORTA |= _BV(PA6);
  144. } else {
  145. PORTA &= ~_BV(PA6);
  146. }
  147. }
  148. void set_avr_mapper(uint8_t val) {
  149. PORTA &= 0xF0;
  150. PORTA |= val&0x07;
  151. }
  152. void set_avr_bank(uint8_t val) {
  153. PORTB &= 0xFC;
  154. PORTB |= val&0x03;
  155. }