You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
5.0 KiB

  1. /* Copyright 2019 ENDO Katsuhiro <ka2hiro@kagizaraya.jp>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #pragma once
  17. #define NUM_ROWS 4
  18. #define NUM_COLS 6
  19. #define NUM_LEDS 2
  20. #define LED_GREEN 0
  21. #define LED_YELLOW 1
  22. typedef struct board_info_t board_info_t;
  23. typedef struct board_interface_t board_interface_t;
  24. struct board_info_t {
  25. bool master;
  26. bool initialized;
  27. uint8_t i2c_address;
  28. pin_t row_pins[NUM_ROWS];
  29. pin_t col_pins[NUM_COLS];
  30. pin_t led_pins[NUM_LEDS];
  31. bool led_status[NUM_LEDS];
  32. board_interface_t* interface;
  33. };
  34. struct board_interface_t {
  35. void (*select_row)(board_info_t* board, uint8_t row);
  36. void (*unselect_row)(board_info_t* board, uint8_t row);
  37. void (*unselect_rows)(board_info_t* board);
  38. bool (*read_cols_on_row)(board_info_t* board, matrix_row_t current_matrix[], uint8_t row);
  39. void (*set_led)(board_info_t* board, uint8_t led_index, bool status);
  40. };
  41. #define BOARD_I2C_TIMEOUT 20
  42. #define GPA0 0x00
  43. #define GPA1 0x01
  44. #define GPA2 0x02
  45. #define GPA3 0x03
  46. #define GPA4 0x04
  47. #define GPA5 0x05
  48. #define GPA6 0x06
  49. #define GPA7 0x07
  50. #define GPB0 0x08
  51. #define GPB1 0x09
  52. #define GPB2 0x0A
  53. #define GPB3 0x0B
  54. #define GPB4 0x0C
  55. #define GPB5 0x0D
  56. #define GPB6 0x0E
  57. #define GPB7 0x0F
  58. //#define PORTA 0x00
  59. //#define PORTB 0x01
  60. #define PORT_MASK 0x08
  61. #define PIN_MASK 0x07
  62. #define PIN2REGISTER(reg, pin) (reg & ((pin & PORT_MASK) >> 3))
  63. #define PIN2PORT(pin) ((pin & PORT_MASK) >> 3)
  64. #define PIN2MASK(pin) (~(1 << PIN2INDEX(pin)))
  65. #define PIN2INDEX(pin) (pin & ~PORT_MASK)
  66. #define PIN2BIT(pin) (1 << PIN2INDEX(pin))
  67. #define EXPANDER_ADDR(addr) (addr << 1)
  68. #define EXPANDER_IODIR(pin) (0x00 | PIN2PORT(pin))
  69. #define EXPANDER_IPOL(pin) (0x02 | PIN2PORT(pin))
  70. #define EXPANDER_GPINTEN(pin) (0x04 | PIN2PORT(pin))
  71. #define EXPANDER_DEFVAL(pin) (0x06 | PIN2PORT(pin))
  72. #define EXPANDER_INTCON(pin) (0x08 | PIN2PORT(pin))
  73. #define EXPANDER_IOCON(pin) (0x0A | PIN2PORT(pin))
  74. #define EXPANDER_GPPU(pin) (0x0C | PIN2PORT(pin))
  75. #define EXPANDER_INTF(pin) (0x0E | PIN2PORT(pin))
  76. #define EXPANDER_INTCAP(pin) (0x10 | PIN2PORT(pin))
  77. #define EXPANDER_GPIO(pin) (0x12 | PIN2PORT(pin))
  78. #define EXPANDER_OLAT(pin) (0x14 | PIN2PORT(pin))
  79. #define EXPANDER_IODIRA 0x00
  80. #define EXPANDER_IODIRB 0x01
  81. #define EXPANDER_IPOLA 0x02
  82. #define EXPANDER_IPOLB 0x03
  83. #define EXPANDER_GPINTENA 0x04
  84. #define EXPANDER_GPINTENB 0x05
  85. #define EXPANDER_DEFVALA 0x06
  86. #define EXPANDER_DEFVALB 0x07
  87. #define EXPANDER_INTCONA 0x08
  88. #define EXPANDER_INTCONB 0x09
  89. #define EXPANDER_IOCONA 0x0A
  90. #define EXPANDER_IOCONB 0x0B
  91. #define EXPANDER_GPPUA 0x0C
  92. #define EXPANDER_GPPUB 0x0D
  93. #define EXPANDER_INTFA 0x0E
  94. #define EXPANDER_INTFB 0x0F
  95. #define EXPANDER_INTCAPA 0x10
  96. #define EXPANDER_INTCAPB 0x11
  97. #define EXPANDER_GPIOA 0x12
  98. #define EXPANDER_GPIOB 0x13
  99. #define EXPANDER_OLATA 0x14
  100. #define EXPANDER_OLATB 0x15
  101. //
  102. // Default board config
  103. //
  104. #ifndef NUM_BOARDS
  105. # define NUM_BOARDS 2
  106. #endif
  107. // clang-format off
  108. #ifndef BOARD_INFOS
  109. #if NUM_BOARDS == 2
  110. #define BOARD_INFOS \
  111. { \
  112. { \
  113. true, \
  114. false, \
  115. 0x00, \
  116. { D4, D5, D6, D7 }, \
  117. { D1, D0, C3, C2, C1, C0 }, \
  118. { B1, B2 }, \
  119. { false, false }, \
  120. NULL \
  121. }, \
  122. { \
  123. false, \
  124. false, \
  125. 0x20, \
  126. { GPB4, GPB5, GPB6, GPB7 }, \
  127. { GPA7, GPA6, GPA5, GPA4, GPA3, GPA2 }, \
  128. { GPB0, GPB1 }, \
  129. { false, false }, \
  130. NULL \
  131. }, \
  132. }
  133. #elif NUM_BOARDS == 3
  134. #define BOARD_INFOS \
  135. { \
  136. { \
  137. true, \
  138. false, \
  139. 0x00, \
  140. { D4, D5, D6, D7 }, \
  141. { D1, D0, C3, C2, C1, C0 }, \
  142. { B1, B2 }, \
  143. { false, false }, \
  144. NULL \
  145. }, \
  146. { \
  147. false, \
  148. false, \
  149. 0x20, \
  150. { GPB4, GPB5, GPB6, GPB7 }, \
  151. { GPA7, GPA6, GPA5, GPA4, GPA3, GPA2 }, \
  152. { GPB0, GPB1 }, \
  153. { false, false }, \
  154. NULL \
  155. }, \
  156. { \
  157. false, \
  158. false, \
  159. 0x21, \
  160. { GPB4, GPB5, GPB6, GPB7 }, \
  161. { GPA7, GPA6, GPA5, GPA4, GPA3, GPA2 }, \
  162. { GPB0, GPB1 }, \
  163. { false, false }, \
  164. NULL \
  165. }, \
  166. }
  167. #endif
  168. #endif
  169. // clang-format on
  170. void board_set_led_by_index(uint8_t board_index, uint8_t led_index, bool status);
  171. bool board_read_cols_on_row(matrix_row_t current_matrix[], uint8_t row);
  172. void board_select_row(uint8_t row);
  173. void board_unselect_row(uint8_t row);
  174. void board_unselect_rows(void);
  175. void board_init(void);