main.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <arm/NXP/LPC17xx/LPC17xx.h>
  2. #include <string.h>
  3. #include "config.h"
  4. #include "obj/autoconf.h"
  5. #include "clock.h"
  6. #include "uart.h"
  7. #include "bits.h"
  8. #include "power.h"
  9. #include "timer.h"
  10. #include "ff.h"
  11. #include "diskio.h"
  12. #include "led.h"
  13. #include "sdnative.h"
  14. #include "crc.h"
  15. #include "fileops.h"
  16. #include "iap.h"
  17. #define EMC0TOGGLE (3<<4)
  18. #define MR0R (1<<1)
  19. int i;
  20. volatile enum diskstates disk_state;
  21. extern volatile tick_t ticks;
  22. int (*chain)(void);
  23. int main(void) {
  24. SNES_CIC_PAIR_REG->FIODIR = BV(SNES_CIC_PAIR_BIT);
  25. BITBAND(SNES_CIC_PAIR_REG->FIOSET, SNES_CIC_PAIR_BIT) = 1;
  26. /* LPC_GPIO2->FIODIR = BV(0) | BV(1) | BV(2); */
  27. // LPC_GPIO0->FIODIR = BV(16);
  28. /* connect UART3 on P0[25:26] + SSP0 on P0[15:18] + MAT3.0 on P0[10] */
  29. LPC_PINCON->PINSEL1 = BV(18) | BV(19) | BV(20) | BV(21) /* UART3 */
  30. | BV(3) | BV(5); /* SSP0 (FPGA) except SS */
  31. LPC_PINCON->PINSEL0 = BV(31); /* SSP0 */
  32. /* | BV(13) | BV(15) | BV(17) | BV(19) SSP1 (SD) */
  33. /* pull-down CIC data lines */
  34. LPC_PINCON->PINMODE0 = BV(0) | BV(1) | BV(2) | BV(3);
  35. clock_disconnect();
  36. power_init();
  37. timer_init();
  38. DBG_UART uart_init();
  39. led_init();
  40. readled(0);
  41. rdyled(1);
  42. writeled(0);
  43. /* do this last because the peripheral init()s change PCLK dividers */
  44. clock_init();
  45. // LPC_PINCON->PINSEL0 |= BV(20) | BV(21); /* MAT3.0 (FPGA clock) */
  46. sdn_init();
  47. for(i = 0; i < 20; i++) uart_putc('-');
  48. uart_putc('\n');
  49. DBG_BL printf("chksum=%08lx\n", *(uint32_t*)28);
  50. /*DBG_BL*/ printf("\n\nsd2snes mk.2 bootloader\nver.: " VER "\ncpu clock: %ld Hz\n", CONFIG_CPU_FREQUENCY);
  51. DBG_BL printf("PCONP=%lx\n", LPC_SC->PCONP);
  52. /* setup timer (fpga clk) */
  53. LPC_TIM3->CTCR=0;
  54. LPC_TIM3->EMR=EMC0TOGGLE;
  55. LPC_TIM3->MCR=MR0R;
  56. LPC_TIM3->MR0=1;
  57. LPC_TIM3->TCR=1;
  58. NVIC->ICER[0] = 0xffffffff;
  59. NVIC->ICER[1] = 0xffffffff;
  60. FLASH_RES res = flash_file((uint8_t*)"/sd2snes/firmware.img");
  61. if(res == ERR_FLASHPREP || res == ERR_FLASHERASE || res == ERR_FLASH) {
  62. rdyled(0);
  63. writeled(1);
  64. }
  65. if(res == ERR_FILEHD || res == ERR_FILECHK) {
  66. rdyled(0);
  67. readled(1);
  68. }
  69. DBG_BL printf("flash result = %d\n", res);
  70. if(res != ERR_OK) {
  71. if((res = check_flash()) != ERR_OK) {
  72. DBG_BL printf("check_flash() failed with error %d, not booting.\n", res);
  73. while(1) {
  74. toggle_rdy_led();
  75. delay_ms(500);
  76. }
  77. }
  78. }
  79. NVIC_DisableIRQ(RIT_IRQn);
  80. NVIC_DisableIRQ(UART_IRQ);
  81. SCB->VTOR=FW_START+0x00000100;
  82. chain = (void*)(*((uint32_t*)(FW_START+0x00000104)));
  83. uart_putc("0123456789abcdef"[((uint32_t)chain>>28)&15]);
  84. uart_putc("0123456789abcdef"[((uint32_t)chain>>24)&15]);
  85. uart_putc("0123456789abcdef"[((uint32_t)chain>>20)&15]);
  86. uart_putc("0123456789abcdef"[((uint32_t)chain>>16)&15]);
  87. uart_putc("0123456789abcdef"[((uint32_t)chain>>12)&15]);
  88. uart_putc("0123456789abcdef"[((uint32_t)chain>>8)&15]);
  89. uart_putc("0123456789abcdef"[((uint32_t)chain>>4)&15]);
  90. uart_putc("0123456789abcdef"[((uint32_t)chain)&15]);
  91. uart_putc('\n');
  92. chain();
  93. while(1);
  94. }