board.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. #include <stdint.h>
  17. #include <stdbool.h>
  18. #include "wait.h"
  19. #include "print.h"
  20. #include "debug.h"
  21. #include "matrix.h"
  22. #include "quantum.h"
  23. #include "board.h"
  24. #include "i2c_master.h"
  25. static board_info_t boards[NUM_BOARDS] = BOARD_INFOS;
  26. static board_info_t* master_board = NULL;
  27. static bool board_is_master(board_info_t* board);
  28. static bool board_is_initialized(board_info_t* board);
  29. static board_info_t* get_board_by_index(uint8_t board_index);
  30. static uint8_t board_merge_led_config(board_info_t* board, uint8_t iodir);
  31. static uint8_t board_merge_led_status(board_info_t* board, uint8_t data);
  32. static void board_master_init(void);
  33. static void board_slave_init(void);
  34. //
  35. // board interface
  36. //
  37. static void board_select_master_row(board_info_t* board, uint8_t row);
  38. static void board_unselect_master_row(board_info_t* board, uint8_t row);
  39. static void board_unselect_master_rows(board_info_t* board);
  40. static bool board_read_cols_on_master_row(board_info_t* board, matrix_row_t current_matrix[], uint8_t row);
  41. static void board_set_master_led(board_info_t* board, uint8_t led_index, bool status);
  42. static void board_select_slave_row(board_info_t* board, uint8_t row);
  43. static void board_unselect_slave_row(board_info_t* board, uint8_t row);
  44. static void board_unselect_slave_rows(board_info_t* board);
  45. static bool board_read_cols_on_slave_row(board_info_t* board, matrix_row_t current_matrix[], uint8_t row);
  46. static void board_set_slave_led(board_info_t* board, uint8_t led_index, bool status);
  47. static board_interface_t master_interface = {board_select_master_row, board_unselect_master_row, board_unselect_master_rows, board_read_cols_on_master_row, board_set_master_led};
  48. static board_interface_t slave_interface = {board_select_slave_row, board_unselect_slave_row, board_unselect_slave_rows, board_read_cols_on_slave_row, board_set_slave_led};
  49. static board_interface_t* get_interface(board_info_t* board) {
  50. if (board_is_master(board)) {
  51. return &master_interface;
  52. }
  53. return &slave_interface;
  54. }
  55. static void board_set_master_led(board_info_t* board, uint8_t led_index, bool status) {
  56. pin_t pin = board->led_pins[led_index];
  57. board->led_status[led_index] = status;
  58. setPinOutput(pin);
  59. status ? writePinHigh(pin) : writePinLow(pin);
  60. }
  61. static void board_set_slave_led(board_info_t* board, uint8_t led_index, bool status) {
  62. board->led_status[led_index] = status;
  63. uint8_t iodir = board_merge_led_config(board, 0xff);
  64. uint8_t data = board_merge_led_status(board, 0x00);
  65. i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_IODIRB, (const uint8_t*)&iodir, sizeof(iodir), BOARD_I2C_TIMEOUT);
  66. i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_OLATB, (const uint8_t*)&data, sizeof(data), BOARD_I2C_TIMEOUT);
  67. }
  68. static uint8_t board_merge_led_config(board_info_t* board, uint8_t iodir) {
  69. for (uint8_t i = 0; i < NUM_LEDS; i++) {
  70. iodir &= PIN2MASK(board->led_pins[i]);
  71. }
  72. return iodir;
  73. }
  74. static bool board_slave_config(board_info_t* board) {
  75. uint8_t set = 0xff;
  76. uint8_t clear = 0x00;
  77. i2c_status_t res = 0;
  78. // Set to input
  79. res = i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_IODIRA, (const uint8_t*)&set, sizeof(set), BOARD_I2C_TIMEOUT);
  80. if (res < 0) return false;
  81. // RESTRICTION: LEDs only on PORT B.
  82. set = board_merge_led_config(board, set);
  83. res = i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_IODIRB, (const uint8_t*)&set, sizeof(set), BOARD_I2C_TIMEOUT);
  84. if (res < 0) return false;
  85. set = 0xff;
  86. // Pull up for input - enable
  87. res = i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_GPPUA, (const uint8_t*)&set, sizeof(set), BOARD_I2C_TIMEOUT);
  88. if (res < 0) return false;
  89. res = i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_GPPUB, (const uint8_t*)&set, sizeof(set), BOARD_I2C_TIMEOUT);
  90. if (res < 0) return false;
  91. // Disable interrupt
  92. res = i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_GPINTENA, (const uint8_t*)&clear, sizeof(clear), BOARD_I2C_TIMEOUT);
  93. if (res < 0) return false;
  94. res = i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_GPINTENB, (const uint8_t*)&clear, sizeof(clear), BOARD_I2C_TIMEOUT);
  95. if (res < 0) return false;
  96. // Polarity - same logic
  97. res = i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_IPOLA, (const uint8_t*)&clear, sizeof(clear), BOARD_I2C_TIMEOUT);
  98. if (res < 0) return false;
  99. res = i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_IPOLB, (const uint8_t*)&clear, sizeof(clear), BOARD_I2C_TIMEOUT);
  100. if (res < 0) return false;
  101. return true;
  102. }
  103. static void board_slave_init(void) {
  104. i2c_init();
  105. _delay_ms(500);
  106. for (uint8_t i = 0; i < NUM_BOARDS; i++) {
  107. board_info_t* board = &boards[i];
  108. if (board_is_master(board)) {
  109. continue;
  110. }
  111. if (i2c_start(EXPANDER_ADDR(board->i2c_address), BOARD_I2C_TIMEOUT) != I2C_STATUS_SUCCESS) {
  112. continue;
  113. }
  114. i2c_stop();
  115. if (board_slave_config(board)) {
  116. board->initialized = true;
  117. }
  118. }
  119. }
  120. inline bool board_is_master(board_info_t* board) {
  121. if (board) {
  122. return board->master;
  123. }
  124. return false;
  125. }
  126. inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
  127. inline uint8_t matrix2board(uint8_t row) { return row % NUM_ROWS; }
  128. inline uint8_t board_index(uint8_t row) { return row / NUM_ROWS; }
  129. static board_info_t* get_master_board(void) {
  130. if (master_board == NULL) {
  131. for (uint8_t i = 0; i < NUM_BOARDS; i++) {
  132. if (boards[i].master) {
  133. master_board = &boards[i];
  134. return master_board;
  135. }
  136. }
  137. }
  138. return NULL;
  139. }
  140. inline bool board_is_initialized(board_info_t* board) { return board == NULL ? false : board->initialized; }
  141. static board_info_t* get_board_by_index(uint8_t board_index) {
  142. if (board_index >= 0 && board_index < NUM_BOARDS) {
  143. if (!board_is_initialized(&boards[board_index])) {
  144. return NULL;
  145. }
  146. return &boards[board_index];
  147. }
  148. return NULL;
  149. }
  150. static board_info_t* get_board(uint8_t row) {
  151. uint8_t idx = board_index(row);
  152. if (idx >= 0 && idx < NUM_BOARDS) {
  153. if (!board_is_initialized(&boards[idx])) {
  154. return NULL;
  155. }
  156. return &boards[idx];
  157. }
  158. return NULL;
  159. }
  160. static uint8_t board_merge_led_status(board_info_t* board, uint8_t data) {
  161. if (!board_is_initialized(board)) {
  162. return data;
  163. }
  164. for (uint8_t i = 0; i < NUM_LEDS; i++) {
  165. bool status = board->led_status[i];
  166. if (status) {
  167. data |= (uint8_t)1 << PIN2INDEX(board->led_pins[i]);
  168. } else {
  169. data &= PIN2MASK(board->led_pins[i]);
  170. }
  171. }
  172. return data;
  173. }
  174. //
  175. // Functions for slave
  176. //
  177. static uint8_t board_read_slave_cols(board_info_t* board) {
  178. if (!board_is_initialized(board)) {
  179. return 0xff;
  180. }
  181. uint8_t data = 0xff;
  182. i2c_status_t res = i2c_readReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_GPIOA, &data, sizeof(data), BOARD_I2C_TIMEOUT);
  183. return (res < 0) ? 0xff : data;
  184. }
  185. static void board_select_slave_row(board_info_t* board, uint8_t board_row) {
  186. if (!board_is_initialized(board)) {
  187. return;
  188. }
  189. uint8_t pin = board->row_pins[board_row];
  190. uint8_t iodir = board_merge_led_config(board, PIN2MASK(pin));
  191. uint8_t status = board_merge_led_status(board, PIN2MASK(pin));
  192. i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_IODIRB, (const uint8_t*)&iodir, sizeof(iodir), BOARD_I2C_TIMEOUT);
  193. i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_OLATB, (const uint8_t*)&status, sizeof(status), BOARD_I2C_TIMEOUT);
  194. }
  195. static void board_unselect_slave_rows(board_info_t* board) {
  196. if (!board_is_initialized(board)) {
  197. return;
  198. }
  199. uint8_t iodir = board_merge_led_config(board, 0xff);
  200. uint8_t data = board_merge_led_status(board, 0x00);
  201. i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_IODIRB, (const uint8_t*)&iodir, sizeof(iodir), BOARD_I2C_TIMEOUT);
  202. i2c_writeReg(EXPANDER_ADDR(board->i2c_address), EXPANDER_OLATB, (const uint8_t*)&data, sizeof(data), BOARD_I2C_TIMEOUT);
  203. }
  204. static void board_unselect_slave_row(board_info_t* board, uint8_t board_row) { board_unselect_slave_rows(board); }
  205. /*
  206. * row : matrix row (not board row)
  207. */
  208. static bool board_read_cols_on_slave_row(board_info_t* board, matrix_row_t current_matrix[], uint8_t row) {
  209. matrix_row_t last_row_value = current_matrix[row];
  210. current_matrix[row] = 0;
  211. uint8_t board_row = matrix2board(row);
  212. board_select_slave_row(board, board_row);
  213. wait_us(30);
  214. uint8_t cols = board_read_slave_cols(board);
  215. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  216. uint8_t pin = board->col_pins[col_index];
  217. uint8_t pin_state = cols & PIN2BIT(pin);
  218. current_matrix[row] |= pin_state ? 0 : (1 << col_index);
  219. }
  220. board_unselect_slave_row(board, board_row);
  221. return (last_row_value != current_matrix[row]);
  222. }
  223. //
  224. // Functions for master board
  225. //
  226. static void board_select_master_row(board_info_t* board, uint8_t board_row) {
  227. setPinOutput(board->row_pins[board_row]);
  228. writePinLow(board->row_pins[board_row]);
  229. }
  230. static void board_unselect_master_row(board_info_t* board, uint8_t board_row) { setPinInputHigh(board->row_pins[board_row]); }
  231. static void board_unselect_master_rows(board_info_t* board) {
  232. if (!board) {
  233. return;
  234. }
  235. for (uint8_t x = 0; x < NUM_ROWS; x++) {
  236. setPinInput(board->row_pins[x]);
  237. }
  238. }
  239. /*
  240. * row : matrix row (not board row)
  241. */
  242. static bool board_read_cols_on_master_row(board_info_t* board, matrix_row_t current_matrix[], uint8_t row) {
  243. matrix_row_t last_row_value = current_matrix[row];
  244. current_matrix[row] = 0;
  245. uint8_t board_row = matrix2board(row);
  246. board_select_master_row(board, board_row);
  247. wait_us(30);
  248. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  249. uint8_t pin_state = readPin(board->col_pins[col_index]);
  250. current_matrix[row] |= pin_state ? 0 : (1 << col_index);
  251. }
  252. board_unselect_master_row(board, board_row);
  253. return (last_row_value != current_matrix[row]);
  254. }
  255. static void board_master_init(void) {
  256. board_info_t* board = get_master_board();
  257. if (!board) {
  258. return;
  259. }
  260. for (uint8_t x = 0; x < NUM_COLS; x++) {
  261. setPinInputHigh(board->col_pins[x]);
  262. }
  263. board->initialized = true;
  264. }
  265. static void board_setup(void) {
  266. for (uint8_t i = 0; i < NUM_BOARDS; i++) {
  267. board_info_t* board = &boards[i];
  268. board->interface = get_interface(board);
  269. }
  270. }
  271. //
  272. // Public functions
  273. //
  274. // NOTE: Do not call this while matrix scanning...
  275. void board_set_led_by_index(uint8_t board_index, uint8_t led_index, bool status) {
  276. board_info_t* board = get_board_by_index(board_index);
  277. if (!board) return;
  278. if (led_index < 0 || led_index > NUM_LEDS) return;
  279. (*board->interface->set_led)(board, led_index, status);
  280. }
  281. bool board_read_cols_on_row(matrix_row_t current_matrix[], uint8_t row) {
  282. bool result = false;
  283. board_info_t* board = get_board(row);
  284. if (!board) {
  285. return false;
  286. }
  287. result = (*board->interface->read_cols_on_row)(board, current_matrix, row);
  288. return result;
  289. }
  290. void board_select_row(uint8_t row) {
  291. board_info_t* board = get_board(row);
  292. if (!board) {
  293. return;
  294. }
  295. uint8_t board_row = matrix2board(row);
  296. (*board->interface->select_row)(board, board_row);
  297. }
  298. void board_unselect_row(uint8_t row) {
  299. board_info_t* board = get_board(row);
  300. if (!board) {
  301. return;
  302. }
  303. uint8_t board_row = matrix2board(row);
  304. (*board->interface->unselect_row)(board, board_row);
  305. }
  306. void board_unselect_rows(void) {
  307. for (uint8_t i = 0; i < NUM_BOARDS; i++) {
  308. board_info_t* board = &boards[i];
  309. (*board->interface->unselect_rows)(board);
  310. }
  311. }
  312. void board_init(void) {
  313. board_setup();
  314. board_master_init();
  315. board_slave_init();
  316. board_unselect_rows();
  317. }