ergodox_ez.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "ergodox_ez.h"
  2. #include "i2cmaster.h"
  3. bool i2c_initialized = 0;
  4. uint8_t mcp23018_status = 0x20;
  5. __attribute__ ((weak))
  6. void * matrix_init_user(void) {
  7. return NULL;
  8. };
  9. __attribute__ ((weak))
  10. void * matrix_scan_user(void) {
  11. return NULL;
  12. };
  13. void * matrix_init_kb(void) {
  14. // keyboard LEDs (see "PWM on ports OC1(A|B|C)" in "teensy-2-0.md")
  15. TCCR1A = 0b10101001; // set and configure fast PWM
  16. TCCR1B = 0b00001001; // set and configure fast PWM
  17. // (tied to Vcc for hardware convenience)
  18. DDRB &= ~(1<<4); // set B(4) as input
  19. PORTB &= ~(1<<4); // set B(4) internal pull-up disabled
  20. // unused pins - C7, D4, D5, D7, E6
  21. // set as input with internal pull-ip enabled
  22. DDRC &= ~(1<<7);
  23. DDRD &= ~(1<<7 | 1<<5 | 1<<4);
  24. DDRE &= ~(1<<6);
  25. PORTC |= (1<<7);
  26. PORTD |= (1<<7 | 1<<5 | 1<<4);
  27. PORTE |= (1<<6);
  28. ergodox_blink_all_leds();
  29. if (matrix_init_user) {
  30. (*matrix_init_user)();
  31. }
  32. return NULL;
  33. };
  34. void * matrix_scan_kb(void) {
  35. if (matrix_scan_user) {
  36. (*matrix_scan_user)();
  37. }
  38. return NULL;
  39. };
  40. void ergodox_blink_all_leds(void)
  41. {
  42. ergodox_led_all_off();
  43. ergodox_led_all_set(LED_BRIGHTNESS_HI);
  44. ergodox_right_led_1_on();
  45. _delay_ms(50);
  46. ergodox_right_led_2_on();
  47. _delay_ms(50);
  48. ergodox_right_led_3_on();
  49. _delay_ms(50);
  50. ergodox_right_led_1_off();
  51. _delay_ms(50);
  52. ergodox_right_led_2_off();
  53. _delay_ms(50);
  54. ergodox_right_led_3_off();
  55. //ergodox_led_all_on();
  56. //_delay_ms(333);
  57. ergodox_led_all_off();
  58. }
  59. uint8_t init_mcp23018(void) {
  60. mcp23018_status = 0x20;
  61. // I2C subsystem
  62. if (i2c_initialized == 0) {
  63. i2c_init(); // on pins D(1,0)
  64. i2c_initialized++;
  65. _delay_ms(1000);
  66. }
  67. // set pin direction
  68. // - unused : input : 1
  69. // - input : input : 1
  70. // - driving : output : 0
  71. mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
  72. mcp23018_status = i2c_write(IODIRA); if (mcp23018_status) goto out;
  73. mcp23018_status = i2c_write(0b00000000); if (mcp23018_status) goto out;
  74. mcp23018_status = i2c_write(0b00111111); if (mcp23018_status) goto out;
  75. i2c_stop();
  76. // set pull-up
  77. // - unused : on : 1
  78. // - input : on : 1
  79. // - driving : off : 0
  80. mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
  81. mcp23018_status = i2c_write(GPPUA); if (mcp23018_status) goto out;
  82. mcp23018_status = i2c_write(0b00000000); if (mcp23018_status) goto out;
  83. mcp23018_status = i2c_write(0b00111111); if (mcp23018_status) goto out;
  84. out:
  85. i2c_stop();
  86. return mcp23018_status;
  87. }