main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. BYTE file_buf[512] GCC_ALIGN_WORKAROUND;
  21. FATFS fatfs;
  22. FIL file_handle;
  23. FRESULT file_res;
  24. uint8_t file_lfn[258];
  25. uint16_t file_block_off, file_block_max;
  26. enum filestates file_status;
  27. volatile enum diskstates disk_state;
  28. extern volatile tick_t ticks;
  29. int ( *chain )( void );
  30. int main( void )
  31. {
  32. SNES_CIC_PAIR_REG->FIODIR = BV( SNES_CIC_PAIR_BIT );
  33. BITBANG( SNES_CIC_PAIR_REG->FIOSET, SNES_CIC_PAIR_BIT ) = 1;
  34. /* LPC_GPIO2->FIODIR = BV(0) | BV(1) | BV(2); */
  35. // LPC_GPIO0->FIODIR = BV(16);
  36. /* connect UART3 on P0[25:26] + SSP0 on P0[15:18] + MAT3.0 on P0[10] */
  37. LPC_PINCON->PINSEL1 = BV( 18 ) | BV( 19 ) | BV( 20 ) | BV( 21 ) /* UART3 */
  38. | BV( 3 ) | BV( 5 ); /* SSP0 (FPGA) except SS */
  39. LPC_PINCON->PINSEL0 = BV( 31 ); /* SSP0 */
  40. /* | BV(13) | BV(15) | BV(17) | BV(19) SSP1 (SD) */
  41. /* pull-down CIC data lines */
  42. LPC_PINCON->PINMODE0 = BV( 0 ) | BV( 1 ) | BV( 2 ) | BV( 3 );
  43. clock_disconnect();
  44. power_init();
  45. timer_init();
  46. DBG_UART uart_init();
  47. led_init();
  48. readled( 0 );
  49. rdyled( 1 );
  50. writeled( 0 );
  51. /* do this last because the peripheral init()s change PCLK dividers */
  52. clock_init();
  53. // LPC_PINCON->PINSEL0 |= BV(20) | BV(21); /* MAT3.0 (FPGA clock) */
  54. sdn_init();
  55. for ( i = 0; i < 20; i++ )
  56. {
  57. uart_putc( '-' );
  58. }
  59. uart_putc( '\n' );
  60. DBG_BL printf( "chksum=%08lx\n", *( uint32_t * )28 );
  61. /*DBG_BL*/ printf( "\n\nsd2snes mk.2 bootloader\nver.: " VER "\ncpu clock: %ld Hz\n", CONFIG_CPU_FREQUENCY );
  62. DBG_BL printf( "PCONP=%lx\n", LPC_SC->PCONP );
  63. /* setup timer (fpga clk) */
  64. LPC_TIM3->CTCR = 0;
  65. LPC_TIM3->EMR = EMC0TOGGLE;
  66. LPC_TIM3->MCR = MR0R;
  67. LPC_TIM3->MR0 = 1;
  68. LPC_TIM3->TCR = 1;
  69. NVIC->ICER[0] = 0xffffffff;
  70. NVIC->ICER[1] = 0xffffffff;
  71. FLASH_RES res = flash_file( ( uint8_t * )"/sd2snes/firmware.img" );
  72. if ( res == ERR_FLASHPREP || res == ERR_FLASHERASE || res == ERR_FLASH )
  73. {
  74. rdyled( 0 );
  75. writeled( 1 );
  76. }
  77. if ( res == ERR_FILEHD || res == ERR_FILECHK )
  78. {
  79. rdyled( 0 );
  80. readled( 1 );
  81. }
  82. DBG_BL printf( "flash result = %d\n", res );
  83. if ( res != ERR_OK )
  84. {
  85. if ( ( res = check_flash() ) != ERR_OK )
  86. {
  87. DBG_BL printf( "check_flash() failed with error %d, not booting.\n", res );
  88. while ( 1 )
  89. {
  90. toggle_rdy_led();
  91. delay_ms( 500 );
  92. }
  93. }
  94. }
  95. NVIC_DisableIRQ( RIT_IRQn );
  96. NVIC_DisableIRQ( UART_IRQ );
  97. SCB->VTOR = FW_START + 0x00000100;
  98. chain = ( void * )( *( ( uint32_t * )( FW_START + 0x00000104 ) ) );
  99. uart_putc( "0123456789abcdef"[( ( uint32_t )chain >> 28 ) & 15] );
  100. uart_putc( "0123456789abcdef"[( ( uint32_t )chain >> 24 ) & 15] );
  101. uart_putc( "0123456789abcdef"[( ( uint32_t )chain >> 20 ) & 15] );
  102. uart_putc( "0123456789abcdef"[( ( uint32_t )chain >> 16 ) & 15] );
  103. uart_putc( "0123456789abcdef"[( ( uint32_t )chain >> 12 ) & 15] );
  104. uart_putc( "0123456789abcdef"[( ( uint32_t )chain >> 8 ) & 15] );
  105. uart_putc( "0123456789abcdef"[( ( uint32_t )chain >> 4 ) & 15] );
  106. uart_putc( "0123456789abcdef"[( ( uint32_t )chain ) & 15] );
  107. uart_putc( '\n' );
  108. chain();
  109. while ( 1 );
  110. }