cic.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. printf("CIC state: %s\n", get_cic_statename(get_cic_state()));
  10. }
  11. inline char *get_cic_statefriendlyname(enum cicstates state) {
  12. return cicstatefriendly[state];
  13. }
  14. inline char *get_cic_statename(enum cicstates state) {
  15. return cicstatenames[state];
  16. }
  17. enum cicstates get_cic_state() {
  18. uint32_t count;
  19. uint32_t togglecount = 0;
  20. uint8_t state, state_old;
  21. state_old = BITBAND(SNES_CIC_STATUS_REG->FIOPIN, SNES_CIC_STATUS_BIT);
  22. /* this loop samples at ~10MHz */
  23. for(count=0; count<CIC_SAMPLECOUNT; count++) {
  24. state = BITBAND(SNES_CIC_STATUS_REG->FIOPIN, SNES_CIC_STATUS_BIT);
  25. if(state != state_old) {
  26. togglecount++;
  27. }
  28. state_old = state;
  29. }
  30. printf("%ld\n", togglecount);
  31. /* CIC_TOGGLE_THRESH_PAIR > CIC_TOGGLE_THRESH_SCIC */
  32. if(togglecount > CIC_TOGGLE_THRESH_PAIR) {
  33. return CIC_PAIR;
  34. } else if(togglecount > CIC_TOGGLE_THRESH_SCIC) {
  35. return CIC_SCIC;
  36. } else if(state) {
  37. return CIC_OK;
  38. } else return CIC_FAIL;
  39. }
  40. void cic_init(int allow_pairmode) {
  41. BITBAND(SNES_CIC_PAIR_REG->FIODIR, SNES_CIC_PAIR_BIT) = 1;
  42. if(allow_pairmode) {
  43. BITBAND(SNES_CIC_PAIR_REG->FIOCLR, SNES_CIC_PAIR_BIT) = 1;
  44. } else {
  45. BITBAND(SNES_CIC_PAIR_REG->FIOSET, SNES_CIC_PAIR_BIT) = 1;
  46. }
  47. }
  48. /* prepare GPIOs for pair mode + set initial modes */
  49. void cic_pair(int init_vmode, int init_d4) {
  50. cic_videomode(init_vmode);
  51. cic_d4(init_d4);
  52. BITBAND(SNES_CIC_D0_REG->FIODIR, SNES_CIC_D0_BIT) = 1;
  53. BITBAND(SNES_CIC_D1_REG->FIODIR, SNES_CIC_D1_BIT) = 1;
  54. }
  55. void cic_videomode(int value) {
  56. if(value) {
  57. BITBAND(SNES_CIC_D0_REG->FIOSET, SNES_CIC_D0_BIT) = 1;
  58. } else {
  59. BITBAND(SNES_CIC_D0_REG->FIOCLR, SNES_CIC_D0_BIT) = 1;
  60. }
  61. }
  62. void cic_d4(int value) {
  63. if(value) {
  64. BITBAND(SNES_CIC_D1_REG->FIOSET, SNES_CIC_D1_BIT) = 1;
  65. } else {
  66. BITBAND(SNES_CIC_D1_REG->FIOCLR, SNES_CIC_D1_BIT) = 1;
  67. }
  68. }