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.

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