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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

kimera.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. Copyright 2014 Kai Ryu <[email protected]>
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #define KIMERA_C
  15. #include <stdbool.h>
  16. #include <avr/eeprom.h>
  17. #include "action.h"
  18. #include "i2cmaster.h"
  19. #include "kimera.h"
  20. #include "debug.h"
  21. #include "print.h"
  22. static uint8_t row_mapping[PX_COUNT] = {
  23. 0, 1, 2, 3, 4, 5, 6, 7,
  24. UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED,
  25. UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED,
  26. UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED
  27. };
  28. static uint8_t col_mapping[PX_COUNT] = {
  29. 8, 9, 10, 11, 12, 13, 14, 15,
  30. 16, 17, 18, 19, 20, 21, 22, 23,
  31. 24, 25, 26, 27, 28, 29, 30, 31,
  32. UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED
  33. };
  34. static uint8_t row_count = 8;
  35. static uint8_t col_count = 24;
  36. static uint8_t row_left_count = 8;
  37. static uint8_t col_left_count = 24;
  38. static matrix_row_t col_left_mask;
  39. static uint8_t combining = COMBINING_NONE;
  40. static uint8_t data[EXP_COUNT][EXP_PORT_COUNT];
  41. static uint8_t exp_in_use = 3;
  42. static uint8_t exp_online = 0;
  43. static uint8_t read_matrix_mapping(void);
  44. static void write_matrix_mapping(void);
  45. static void expander_init(uint8_t exp);
  46. static uint8_t expander_write(uint8_t exp, uint8_t command, uint8_t *data);
  47. static uint8_t expander_read(uint8_t exp, uint8_t command, uint8_t *data);
  48. static uint8_t expander_write_output(uint8_t exp, uint8_t *data);
  49. static uint8_t expander_write_inversion(uint8_t exp, uint8_t *data);
  50. static uint8_t expander_write_config(uint8_t exp, uint8_t *data);
  51. static uint8_t expander_read_input(uint8_t exp, uint8_t *data);
  52. static void init_data(uint8_t value);
  53. void kimera_init(void)
  54. {
  55. /* read config */
  56. //write_matrix_mapping(); /* debug */
  57. if (read_matrix_mapping()) {
  58. xprintf("Matrix Mapping Error!\n");
  59. write_matrix_mapping();
  60. }
  61. /* init i/o expanders */
  62. kimera_scan();
  63. }
  64. uint8_t read_matrix_mapping(void)
  65. {
  66. uint8_t error = 0;
  67. /* read number of rows and cols */
  68. uint8_t rows = eeprom_read_byte(EECONFIG_ROW_COUNT);
  69. uint8_t cols = eeprom_read_byte(EECONFIG_COL_COUNT);
  70. if (rows == 0) error++;
  71. else if (rows == UNCONFIGURED) error++;
  72. else if (rows & COMBINING_BIT) {
  73. if (combining != COMBINING_NONE) error++;
  74. combining = COMBINING_ROW;
  75. rows -= COMBINING_BIT;
  76. }
  77. if (cols == 0) error++;
  78. else if (cols == UNCONFIGURED) error++;
  79. else if (cols & COMBINING_BIT) {
  80. if (combining != COMBINING_NONE) error++;
  81. combining = COMBINING_COL;
  82. cols -= COMBINING_BIT;
  83. }
  84. if (rows + cols > PX_COUNT) error++;
  85. if (error) return error;
  86. row_count = rows;
  87. col_count = cols;
  88. if (combining != COMBINING_NONE) {
  89. row_left_count = (rows + 1) / 2;
  90. col_left_count = (cols + 1) / 2;
  91. col_left_mask = (1 << col_left_count) - 1;
  92. }
  93. /* read row mapping */
  94. uint8_t *mapping = EECONFIG_ROW_COL_MAPPING;
  95. uint8_t exp;
  96. for (uint8_t i = 0; i < PX_COUNT; i++) {
  97. if (i < row_count) {
  98. row_mapping[i] = eeprom_read_byte(mapping++);
  99. if (row_mapping[i] >= PX_COUNT) {
  100. error++;
  101. }
  102. else {
  103. exp = PX_TO_EXP(row_mapping[i]);
  104. exp_in_use |= (1<<exp);
  105. }
  106. }
  107. else {
  108. row_mapping[i] = UNCONFIGURED;
  109. }
  110. }
  111. /* read col mapping*/
  112. for (uint8_t i = 0; i < PX_COUNT; i++) {
  113. if (i < col_count) {
  114. col_mapping[i] = eeprom_read_byte(mapping++);
  115. if (col_mapping[i] >= PX_COUNT) {
  116. error++;
  117. }
  118. else {
  119. exp = PX_TO_EXP(col_mapping[i]);
  120. exp_in_use |= (1<<exp);
  121. }
  122. }
  123. else {
  124. col_mapping[i] = UNCONFIGURED;
  125. }
  126. }
  127. return error;
  128. }
  129. void write_matrix_mapping(void)
  130. {
  131. /* write number of rows and cols */
  132. eeprom_write_byte(EECONFIG_ROW_COUNT, row_count);
  133. eeprom_write_byte(EECONFIG_COL_COUNT, col_count);
  134. /* write row mapping */
  135. uint8_t *mapping = EECONFIG_ROW_COL_MAPPING;
  136. for (uint8_t row = 0; row < row_count; row++) {
  137. eeprom_write_byte(mapping++, row_mapping[row]);
  138. }
  139. /* write col mapping */
  140. for (uint8_t col = 0; col < col_count; col++) {
  141. eeprom_write_byte(mapping++, col_mapping[col]);
  142. }
  143. }
  144. void kimera_scan(void)
  145. {
  146. uint8_t ret;
  147. for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
  148. if (exp_in_use & (1<<exp)) {
  149. ret = i2c_start(EXP_ADDR(exp) | I2C_WRITE);
  150. if (ret == 0) {
  151. i2c_stop();
  152. if ((exp_online & (1<<exp)) == 0) {
  153. xprintf("found: %d\n", exp);
  154. exp_online |= (1<<exp);
  155. expander_init(exp);
  156. clear_keyboard();
  157. }
  158. }
  159. else {
  160. if ((exp_online & (1<<exp)) != 0) {
  161. xprintf("lost: %d\n", exp);
  162. exp_online &= ~(1<<exp);
  163. clear_keyboard();
  164. }
  165. }
  166. }
  167. }
  168. print("Exp in use: "); pbin(exp_in_use); print("\n");
  169. print("Exp online: "); pbin(exp_online); print("\n");
  170. }
  171. inline
  172. uint8_t kimera_matrix_rows(void)
  173. {
  174. if (combining == COMBINING_ROW) {
  175. return row_left_count;
  176. }
  177. else {
  178. return row_count;
  179. }
  180. }
  181. inline
  182. uint8_t kimera_matrix_cols(void)
  183. {
  184. if (combining == COMBINING_COL) {
  185. return col_left_count;
  186. }
  187. else {
  188. return col_count;
  189. }
  190. }
  191. void kimera_read_cols(void)
  192. {
  193. /* read all input registers */
  194. init_data(0xFF);
  195. for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
  196. expander_read_input(exp, data[exp]);
  197. }
  198. }
  199. uint8_t kimera_get_col(uint8_t row, uint8_t col)
  200. {
  201. if (combining == COMBINING_ROW) {
  202. if (row >= row_left_count) {
  203. col += col_left_count;
  204. }
  205. }
  206. uint8_t px = col_mapping[col];
  207. if (px != UNCONFIGURED) {
  208. if (!(data[PX_TO_EXP(px)][PX_TO_PORT(px)] & (1 << PX_TO_PIN(px)))) {
  209. return 1;
  210. }
  211. }
  212. return 0;
  213. }
  214. matrix_row_t kimera_read_row(uint8_t row)
  215. {
  216. kimera_read_cols();
  217. /* make cols */
  218. matrix_row_t cols = 0;
  219. for (uint8_t col = 0; col < col_count; col++) {
  220. uint8_t px = col_mapping[col];
  221. if (px != UNCONFIGURED) {
  222. if (!(data[PX_TO_EXP(px)][PX_TO_PORT(px)] & (1 << PX_TO_PIN(px)))) {
  223. cols |= (1UL << col);
  224. }
  225. }
  226. }
  227. if (combining == COMBINING_COL) {
  228. if (row < row_left_count) {
  229. cols &= col_left_mask;
  230. }
  231. else {
  232. cols >>= col_left_count;
  233. }
  234. }
  235. return cols;
  236. }
  237. void kimera_unselect_rows(void)
  238. {
  239. /* set all output registers to 0xFF */
  240. init_data(0xFF);
  241. for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
  242. expander_write_config(exp, data[exp]);
  243. }
  244. }
  245. void kimera_select_row(uint8_t row)
  246. {
  247. /* set selected row to low */
  248. init_data(0xFF);
  249. uint8_t px = row_mapping[row];
  250. if (px != UNCONFIGURED) {
  251. uint8_t exp = PX_TO_EXP(px);
  252. data[exp][PX_TO_PORT(px)] &= ~(1 << PX_TO_PIN(px));
  253. expander_write_config(exp, data[exp]);
  254. }
  255. if (combining == COMBINING_ROW) {
  256. if (row < row_left_count) {
  257. kimera_select_row(row + row_left_count);
  258. }
  259. }
  260. }
  261. void expander_init(uint8_t exp)
  262. {
  263. init_data(0x00);
  264. /* write inversion register */
  265. /*
  266. for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
  267. expander_write_inversion(exp, data[exp]);
  268. }
  269. */
  270. /* set output bit */
  271. /*
  272. for (uint8_t row = 0; row < row_count; row++) {
  273. uint8_t px = row_mapping[row];
  274. if (px != UNCONFIGURED) {
  275. data[PX_TO_EXP(px)][PX_TO_PORT(px)] &= ~(1 << PX_TO_PIN(px));
  276. }
  277. }
  278. */
  279. /* write config registers */
  280. //expander_write_config(exp, data[exp]);
  281. /* write output registers */
  282. expander_write_output(exp, data[exp]);
  283. }
  284. uint8_t expander_write(uint8_t exp, uint8_t command, uint8_t *data)
  285. {
  286. if ((exp_online & (1<<exp)) == 0) {
  287. return 0;
  288. }
  289. uint8_t addr = EXP_ADDR(exp);
  290. uint8_t ret;
  291. ret = i2c_start(addr | I2C_WRITE);
  292. if (ret) goto stop;
  293. ret = i2c_write(command);
  294. if (ret) goto stop;
  295. ret = i2c_write(*data++);
  296. if (ret) goto stop;
  297. ret = i2c_write(*data);
  298. stop:
  299. i2c_stop();
  300. return ret;
  301. }
  302. uint8_t expander_read(uint8_t exp, uint8_t command, uint8_t *data)
  303. {
  304. if ((exp_online & (1<<exp)) == 0) {
  305. return 0;
  306. }
  307. uint8_t addr = EXP_ADDR(exp);
  308. uint8_t ret;
  309. ret = i2c_start(addr | I2C_WRITE);
  310. if (ret) goto stop;
  311. ret = i2c_write(command);
  312. if (ret) goto stop;
  313. ret = i2c_rep_start(addr | I2C_READ);
  314. if (ret) goto stop;
  315. *data++ = i2c_readAck();
  316. *data = i2c_readNak();
  317. stop:
  318. i2c_stop();
  319. return ret;
  320. }
  321. inline
  322. uint8_t expander_write_output(uint8_t exp, uint8_t *data)
  323. {
  324. return expander_write(exp, EXP_COMM_OUTPUT_0, data);
  325. }
  326. inline
  327. uint8_t expander_write_inversion(uint8_t exp, uint8_t *data)
  328. {
  329. return expander_write(exp, EXP_COMM_INVERSION_0, data);
  330. }
  331. inline
  332. uint8_t expander_write_config(uint8_t exp, uint8_t *data)
  333. {
  334. return expander_write(exp, EXP_COMM_CONFIG_0, data);
  335. }
  336. inline
  337. uint8_t expander_read_input(uint8_t exp, uint8_t *data)
  338. {
  339. return expander_read(exp, EXP_COMM_INPUT_0, data);
  340. }
  341. void init_data(uint8_t value)
  342. {
  343. for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
  344. for (uint8_t port = 0; port < EXP_PORT_COUNT; port++) {
  345. data[exp][port] = value;
  346. }
  347. }
  348. }