cic.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <arm/NXP/LPC17xx/LPC17xx.h>
  2. #include "bits.h"
  3. #include "config.h"
  4. #include "uart.h"
  5. #include "cic.h"
  6. char *cicstatenames[4] = { "CIC_OK", "CIC_FAIL", "CIC_PAIR", "CIC_SCIC" };
  7. char *cicstatefriendly[4] = {"Original or no CIC", "Original CIC(failed)", "SuperCIC enhanced", "SuperCIC detected, not used"};
  8. void print_cic_state()
  9. {
  10. printf( "CIC state: %s\n", get_cic_statename( get_cic_state() ) );
  11. }
  12. inline char *get_cic_statefriendlyname( enum cicstates state )
  13. {
  14. return cicstatefriendly[state];
  15. }
  16. inline char *get_cic_statename( enum cicstates state )
  17. {
  18. return cicstatenames[state];
  19. }
  20. enum cicstates get_cic_state()
  21. {
  22. uint32_t count;
  23. uint32_t togglecount = 0;
  24. uint8_t state, state_old;
  25. state_old = BITBAND( SNES_CIC_STATUS_REG->FIOPIN, SNES_CIC_STATUS_BIT );
  26. /* this loop samples at ~10MHz */
  27. for ( count = 0; count < CIC_SAMPLECOUNT; count++ )
  28. {
  29. state = BITBAND( SNES_CIC_STATUS_REG->FIOPIN, SNES_CIC_STATUS_BIT );
  30. if ( state != state_old )
  31. {
  32. togglecount++;
  33. }
  34. state_old = state;
  35. }
  36. printf( "%ld\n", togglecount );
  37. /* CIC_TOGGLE_THRESH_PAIR > CIC_TOGGLE_THRESH_SCIC */
  38. if ( togglecount > CIC_TOGGLE_THRESH_PAIR )
  39. {
  40. return CIC_PAIR;
  41. }
  42. else if ( togglecount > CIC_TOGGLE_THRESH_SCIC )
  43. {
  44. return CIC_SCIC;
  45. }
  46. else if ( state )
  47. {
  48. return CIC_OK;
  49. }
  50. else
  51. {
  52. return CIC_FAIL;
  53. }
  54. }
  55. void cic_init( int allow_pairmode )
  56. {
  57. BITBAND( SNES_CIC_PAIR_REG->FIODIR, SNES_CIC_PAIR_BIT ) = 1;
  58. if ( allow_pairmode )
  59. {
  60. BITBAND( SNES_CIC_PAIR_REG->FIOCLR, SNES_CIC_PAIR_BIT ) = 1;
  61. }
  62. else
  63. {
  64. BITBAND( SNES_CIC_PAIR_REG->FIOSET, SNES_CIC_PAIR_BIT ) = 1;
  65. }
  66. }
  67. /* prepare GPIOs for pair mode + set initial modes */
  68. void cic_pair( int init_vmode, int init_d4 )
  69. {
  70. cic_videomode( init_vmode );
  71. cic_d4( init_d4 );
  72. BITBAND( SNES_CIC_D0_REG->FIODIR, SNES_CIC_D0_BIT ) = 1;
  73. BITBAND( SNES_CIC_D1_REG->FIODIR, SNES_CIC_D1_BIT ) = 1;
  74. }
  75. void cic_videomode( int value )
  76. {
  77. if ( value )
  78. {
  79. BITBAND( SNES_CIC_D0_REG->FIOSET, SNES_CIC_D0_BIT ) = 1;
  80. }
  81. else
  82. {
  83. BITBAND( SNES_CIC_D0_REG->FIOCLR, SNES_CIC_D0_BIT ) = 1;
  84. }
  85. }
  86. void cic_d4( int value )
  87. {
  88. if ( value )
  89. {
  90. BITBAND( SNES_CIC_D1_REG->FIOSET, SNES_CIC_D1_BIT ) = 1;
  91. }
  92. else
  93. {
  94. BITBAND( SNES_CIC_D1_REG->FIOCLR, SNES_CIC_D1_BIT ) = 1;
  95. }
  96. }