Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.

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