logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

board.c (13029B)


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