cic.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. 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<CIC_SAMPLECOUNT; 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. /* CIC_TOGGLE_THRESH_PAIR > CIC_TOGGLE_THRESH_SCIC */
  27. if(togglecount > CIC_TOGGLE_THRESH_PAIR) {
  28. return CIC_PAIR;
  29. } else if(togglecount > CIC_TOGGLE_THRESH_SCIC) {
  30. return CIC_SCIC;
  31. } else if(state) {
  32. return CIC_OK;
  33. } else 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. BITBAND(SNES_CIC_D0_REG->FIODIR, SNES_CIC_D0_BIT) = 0;
  43. BITBAND(SNES_CIC_D1_REG->FIODIR, SNES_CIC_D1_BIT) = 0;
  44. }
  45. /* prepare GPIOs for pair mode + set initial modes */
  46. void cic_pair(int init_vmode, int init_d4) {
  47. cic_videomode(init_vmode);
  48. cic_d4(init_d4);
  49. BITBAND(SNES_CIC_D0_REG->FIODIR, SNES_CIC_D0_BIT) = 1;
  50. BITBAND(SNES_CIC_D1_REG->FIODIR, SNES_CIC_D1_BIT) = 1;
  51. }
  52. void cic_videomode(int value) {
  53. if(value) {
  54. BITBAND(SNES_CIC_D0_REG->FIOSET, SNES_CIC_D0_BIT) = 1;
  55. } else {
  56. BITBAND(SNES_CIC_D0_REG->FIOCLR, SNES_CIC_D0_BIT) = 1;
  57. }
  58. }
  59. void cic_d4(int value) {
  60. if(value) {
  61. BITBAND(SNES_CIC_D1_REG->FIOSET, SNES_CIC_D1_BIT) = 1;
  62. } else {
  63. BITBAND(SNES_CIC_D1_REG->FIOCLR, SNES_CIC_D1_BIT) = 1;
  64. }
  65. }