cic.c 1.8 KB

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