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.

363 lines
13 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. #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. gpio_set_pin_output(pin);
  59. status ? gpio_write_pin_high(pin) : gpio_write_pin_low(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_write_register(EXPANDER_ADDR(board->i2c_address), EXPANDER_IODIRB, (const uint8_t*)&iodir, sizeof(iodir), BOARD_I2C_TIMEOUT);
  66. i2c_write_register(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_write_register(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_write_register(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_write_register(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_write_register(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_write_register(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_write_register(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_write_register(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_write_register(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_ping_address(EXPANDER_ADDR(board->i2c_address), BOARD_I2C_TIMEOUT) != I2C_STATUS_SUCCESS) {
  112. continue;
  113. }
  114. if (board_slave_config(board)) {
  115. board->initialized = true;
  116. }
  117. }
  118. }
  119. inline bool board_is_master(board_info_t* board) {
  120. if (board) {
  121. return board->master;
  122. }
  123. return false;
  124. }
  125. inline uint8_t matrix2board(uint8_t row) { return row % NUM_ROWS; }
  126. inline uint8_t board_index(uint8_t row) { return row / NUM_ROWS; }
  127. static board_info_t* get_master_board(void) {
  128. if (master_board == NULL) {
  129. for (uint8_t i = 0; i < NUM_BOARDS; i++) {
  130. if (boards[i].master) {
  131. master_board = &boards[i];
  132. return master_board;
  133. }
  134. }
  135. }
  136. return NULL;
  137. }
  138. inline bool board_is_initialized(board_info_t* board) { return board == NULL ? false : board->initialized; }
  139. static board_info_t* get_board_by_index(uint8_t board_index) {
  140. if (board_index >= 0 && board_index < NUM_BOARDS) {
  141. if (!board_is_initialized(&boards[board_index])) {
  142. return NULL;
  143. }
  144. return &boards[board_index];
  145. }
  146. return NULL;
  147. }
  148. static board_info_t* get_board(uint8_t row) {
  149. uint8_t idx = board_index(row);
  150. if (idx >= 0 && idx < NUM_BOARDS) {
  151. if (!board_is_initialized(&boards[idx])) {
  152. return NULL;
  153. }
  154. return &boards[idx];
  155. }
  156. return NULL;
  157. }
  158. static uint8_t board_merge_led_status(board_info_t* board, uint8_t data) {
  159. if (!board_is_initialized(board)) {
  160. return data;
  161. }
  162. for (uint8_t i = 0; i < NUM_LEDS; i++) {
  163. bool status = board->led_status[i];
  164. if (status) {
  165. data |= (uint8_t)1 << PIN2INDEX(board->led_pins[i]);
  166. } else {
  167. data &= PIN2MASK(board->led_pins[i]);
  168. }
  169. }
  170. return data;
  171. }
  172. //
  173. // Functions for slave
  174. //
  175. static uint8_t board_read_slave_cols(board_info_t* board) {
  176. if (!board_is_initialized(board)) {
  177. return 0xff;
  178. }
  179. uint8_t data = 0xff;
  180. i2c_status_t res = i2c_read_register(EXPANDER_ADDR(board->i2c_address), EXPANDER_GPIOA, &data, sizeof(data), BOARD_I2C_TIMEOUT);
  181. return (res < 0) ? 0xff : data;
  182. }
  183. static void board_select_slave_row(board_info_t* board, uint8_t board_row) {
  184. if (!board_is_initialized(board)) {
  185. return;
  186. }
  187. uint8_t pin = board->row_pins[board_row];
  188. uint8_t iodir = board_merge_led_config(board, PIN2MASK(pin));
  189. uint8_t status = board_merge_led_status(board, PIN2MASK(pin));
  190. i2c_write_register(EXPANDER_ADDR(board->i2c_address), EXPANDER_IODIRB, (const uint8_t*)&iodir, sizeof(iodir), BOARD_I2C_TIMEOUT);
  191. i2c_write_register(EXPANDER_ADDR(board->i2c_address), EXPANDER_OLATB, (const uint8_t*)&status, sizeof(status), BOARD_I2C_TIMEOUT);
  192. }
  193. static void board_unselect_slave_rows(board_info_t* board) {
  194. if (!board_is_initialized(board)) {
  195. return;
  196. }
  197. uint8_t iodir = board_merge_led_config(board, 0xff);
  198. uint8_t data = board_merge_led_status(board, 0x00);
  199. i2c_write_register(EXPANDER_ADDR(board->i2c_address), EXPANDER_IODIRB, (const uint8_t*)&iodir, sizeof(iodir), BOARD_I2C_TIMEOUT);
  200. i2c_write_register(EXPANDER_ADDR(board->i2c_address), EXPANDER_OLATB, (const uint8_t*)&data, sizeof(data), BOARD_I2C_TIMEOUT);
  201. }
  202. static void board_unselect_slave_row(board_info_t* board, uint8_t board_row) { board_unselect_slave_rows(board); }
  203. /*
  204. * row : matrix row (not board row)
  205. */
  206. static bool board_read_cols_on_slave_row(board_info_t* board, matrix_row_t current_matrix[], uint8_t row) {
  207. matrix_row_t last_row_value = current_matrix[row];
  208. current_matrix[row] = 0;
  209. uint8_t board_row = matrix2board(row);
  210. board_select_slave_row(board, board_row);
  211. wait_us(30);
  212. uint8_t cols = board_read_slave_cols(board);
  213. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  214. uint8_t pin = board->col_pins[col_index];
  215. uint8_t pin_state = cols & PIN2BIT(pin);
  216. current_matrix[row] |= pin_state ? 0 : (1 << col_index);
  217. }
  218. board_unselect_slave_row(board, board_row);
  219. return (last_row_value != current_matrix[row]);
  220. }
  221. //
  222. // Functions for master board
  223. //
  224. static void board_select_master_row(board_info_t* board, uint8_t board_row) {
  225. gpio_set_pin_output(board->row_pins[board_row]);
  226. gpio_write_pin_low(board->row_pins[board_row]);
  227. }
  228. static void board_unselect_master_row(board_info_t* board, uint8_t board_row) { gpio_set_pin_input_high(board->row_pins[board_row]); }
  229. static void board_unselect_master_rows(board_info_t* board) {
  230. if (!board) {
  231. return;
  232. }
  233. for (uint8_t x = 0; x < NUM_ROWS; x++) {
  234. gpio_set_pin_input(board->row_pins[x]);
  235. }
  236. }
  237. /*
  238. * row : matrix row (not board row)
  239. */
  240. static bool board_read_cols_on_master_row(board_info_t* board, matrix_row_t current_matrix[], uint8_t row) {
  241. matrix_row_t last_row_value = current_matrix[row];
  242. current_matrix[row] = 0;
  243. uint8_t board_row = matrix2board(row);
  244. board_select_master_row(board, board_row);
  245. wait_us(30);
  246. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  247. uint8_t pin_state = gpio_read_pin(board->col_pins[col_index]);
  248. current_matrix[row] |= pin_state ? 0 : (1 << col_index);
  249. }
  250. board_unselect_master_row(board, board_row);
  251. return (last_row_value != current_matrix[row]);
  252. }
  253. static void board_master_init(void) {
  254. board_info_t* board = get_master_board();
  255. if (!board) {
  256. return;
  257. }
  258. for (uint8_t x = 0; x < NUM_COLS; x++) {
  259. gpio_set_pin_input_high(board->col_pins[x]);
  260. }
  261. board->initialized = true;
  262. }
  263. static void board_setup(void) {
  264. for (uint8_t i = 0; i < NUM_BOARDS; i++) {
  265. board_info_t* board = &boards[i];
  266. board->interface = get_interface(board);
  267. }
  268. }
  269. //
  270. // Public functions
  271. //
  272. // NOTE: Do not call this while matrix scanning...
  273. void board_set_led_by_index(uint8_t board_index, uint8_t led_index, bool status) {
  274. board_info_t* board = get_board_by_index(board_index);
  275. if (!board) return;
  276. if (led_index < 0 || led_index > NUM_LEDS) return;
  277. (*board->interface->set_led)(board, led_index, status);
  278. }
  279. bool board_read_cols_on_row(matrix_row_t current_matrix[], uint8_t row) {
  280. bool result = false;
  281. board_info_t* board = get_board(row);
  282. if (!board) {
  283. return false;
  284. }
  285. result = (*board->interface->read_cols_on_row)(board, current_matrix, row);
  286. return result;
  287. }
  288. void board_select_row(uint8_t row) {
  289. board_info_t* board = get_board(row);
  290. if (!board) {
  291. return;
  292. }
  293. uint8_t board_row = matrix2board(row);
  294. (*board->interface->select_row)(board, board_row);
  295. }
  296. void board_unselect_row(uint8_t row) {
  297. board_info_t* board = get_board(row);
  298. if (!board) {
  299. return;
  300. }
  301. uint8_t board_row = matrix2board(row);
  302. (*board->interface->unselect_row)(board, board_row);
  303. }
  304. void board_unselect_rows(void) {
  305. for (uint8_t i = 0; i < NUM_BOARDS; i++) {
  306. board_info_t* board = &boards[i];
  307. (*board->interface->unselect_rows)(board);
  308. }
  309. }
  310. void board_init(void) {
  311. board_setup();
  312. board_master_init();
  313. board_slave_init();
  314. board_unselect_rows();
  315. }