m20add.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * m20add.c
  3. */
  4. #include "m20add.h"
  5. #include "tca6424.h"
  6. #include "rgb_ring.h"
  7. #include "i2c_master.h"
  8. void set_pin(uint16_t pin)
  9. {
  10. uint8_t data = tca6424_read_port(GET_PORT(pin));
  11. data |= ( 1 << GET_PIN(pin));
  12. tca6424_write_port(GET_PORT(pin), data);
  13. }
  14. void clear_pin(uint16_t pin)
  15. {
  16. uint8_t data = tca6424_read_port(GET_PORT(pin));
  17. data &= ~( 1 << GET_PIN(pin));
  18. tca6424_write_port(GET_PORT(pin), data);
  19. }
  20. uint8_t read_pin(uint16_t pin)
  21. {
  22. uint8_t data = tca6424_read_port(GET_PORT(pin));
  23. return (data & (1<<GET_PIN(pin))) ? 1 : 0;
  24. }
  25. void matrix_init_kb(void) {
  26. #ifdef RGBLIGHT_ENABLE
  27. rgb_ring_init();
  28. #endif
  29. matrix_init_user();
  30. }
  31. void matrix_scan_kb(void) {
  32. #ifdef RGBLIGHT_ENABLE
  33. rgb_ring_task();
  34. #endif
  35. matrix_scan_user();
  36. }
  37. static uint16_t caps_lock_pin = DEF_PIN(TCA6424_PORT2, 3);
  38. static uint16_t scroll_lock_pin = DEF_PIN(TCA6424_PORT0, 1);
  39. bool led_update_kb(led_t led_state) {
  40. bool res = led_update_user(led_state);
  41. if (res) {
  42. led_state.caps_lock ? set_pin(caps_lock_pin) : clear_pin(caps_lock_pin);
  43. led_state.scroll_lock ? set_pin(scroll_lock_pin) : clear_pin(scroll_lock_pin);
  44. }
  45. return res;
  46. }
  47. // override the default implementation to avoid re-initialization
  48. void i2c_init(void)
  49. {
  50. static bool initialized = false;
  51. if (initialized) {
  52. return;
  53. } else {
  54. initialized = true;
  55. }
  56. // Try releasing special pins for a short time
  57. palSetPadMode(I2C1_SCL_BANK, I2C1_SCL, PAL_MODE_INPUT);
  58. palSetPadMode(I2C1_SDA_BANK, I2C1_SDA, PAL_MODE_INPUT);
  59. chThdSleepMilliseconds(10);
  60. palSetPadMode(I2C1_SCL_BANK, I2C1_SCL, PAL_MODE_ALTERNATE(I2C1_SCL_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN);
  61. palSetPadMode(I2C1_SDA_BANK, I2C1_SDA, PAL_MODE_ALTERNATE(I2C1_SDA_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN);
  62. }
  63. #define REBOOT_MAGIC 0x41544B42
  64. void shutdown_user(void)
  65. {
  66. // set the magic number for resetting to the bootloader
  67. *(uint32_t *)(&(RTCD1.rtc->BKP0R)) = REBOOT_MAGIC;
  68. }