選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
このリポジトリはアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュや、課題・プルリクエストのオープンはできません。

kimera.c 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 <avr/interrupt.h>
  18. #include <avr/wdt.h>
  19. #include <util/delay.h>
  20. #include "action.h"
  21. #include "suspend.h"
  22. #include "i2cmaster.h"
  23. #include "kimera.h"
  24. #include "debug.h"
  25. #define SCL_CLOCK 400000L
  26. #define SCL_DURATION (1000000L/SCL_CLOCK)/2
  27. extern uint8_t i2c_force_stop;
  28. static uint8_t row_mapping[PX_COUNT] = {
  29. #ifndef TWO_HEADED_KIMERA
  30. 0, 1, 2, 3, 4, 5, 6, 7,
  31. UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED,
  32. UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED,
  33. UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED
  34. #else
  35. 0, 1, 2, 3, 4, 5, 6, 7,
  36. 32, 33, 34, 35, 36, 37, 38, 39,
  37. UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED,
  38. UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED
  39. #endif
  40. };
  41. static uint8_t col_mapping[PX_COUNT] = {
  42. #ifndef TWO_HEADED_KIMERA
  43. 8, 9, 10, 11, 12, 13, 14, 15,
  44. 16, 17, 18, 19, 20, 21, 22, 23,
  45. 24, 25, 26, 27, 28, 29, 30, 31,
  46. UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED
  47. #else
  48. 8, 9, 10, 11, 12, 13, 14, 15,
  49. 16, 17, 18, 19, 20, 21, 22, 23,
  50. 40, 41, 42, 43, 44, 45, 46, 47,
  51. 48, 49, 50, 51, 52, 53, 54, 55
  52. #endif
  53. };
  54. #ifndef TWO_HEADED_KIMERA
  55. static uint8_t row_count = 8;
  56. static uint8_t col_count = 24;
  57. #else
  58. static uint8_t row_count = 16;
  59. static uint8_t col_count = 32;
  60. static uint8_t row_left_count = 8;
  61. static uint8_t col_left_count = 16;
  62. static matrix_row_t col_left_mask;
  63. #endif
  64. static uint8_t data[EXP_COUNT][EXP_PORT_COUNT];
  65. static uint8_t exp_status = 0;
  66. void kimera_init(void)
  67. {
  68. /* read config */
  69. //write_matrix_mapping(); /* debug */
  70. if (read_matrix_mapping()) {
  71. write_matrix_mapping();
  72. }
  73. /* init i2c */
  74. i2c_init();
  75. /* init watch dog */
  76. wdt_init();
  77. /* init i/o expanders */
  78. kimera_scan();
  79. }
  80. void wdt_init(void)
  81. {
  82. cli();
  83. wdt_reset();
  84. wdt_intr_enable(WDTO_1S);
  85. sei();
  86. }
  87. uint8_t read_matrix_mapping(void)
  88. {
  89. uint8_t error = 0;
  90. /* read number of rows and cols */
  91. uint8_t rows = eeprom_read_byte(EECONFIG_ROW_COUNT);
  92. uint8_t cols = eeprom_read_byte(EECONFIG_COL_COUNT);
  93. if (rows == 0) error++;
  94. if (rows == UNCONFIGURED) error++;
  95. if (cols == 0) error++;
  96. if (cols == UNCONFIGURED) error++;
  97. if (rows + cols > PX_COUNT) error++;
  98. if (error) return error;
  99. row_count = rows;
  100. col_count = cols;
  101. #ifdef TWO_HEADED_KIMERA
  102. row_left_count = (rows + 1) / 2;
  103. col_left_count = (cols + 1) / 2;
  104. col_left_mask = (1 << row_left_count) - 1;
  105. #endif
  106. /* read row mapping */
  107. uint8_t *mapping = EECONFIG_ROW_COL_MAPPING;
  108. for (uint8_t i = 0; i < PX_COUNT; i++) {
  109. if (i < row_count) {
  110. row_mapping[i] = eeprom_read_byte(mapping++);
  111. if (row_mapping[i] >= PX_COUNT) error++;
  112. }
  113. else {
  114. row_mapping[i] = UNCONFIGURED;
  115. }
  116. }
  117. /* read col mapping*/
  118. for (uint8_t i = 0; i < PX_COUNT; i++) {
  119. if (i < col_count) {
  120. col_mapping[i] = eeprom_read_byte(mapping++);
  121. if (col_mapping[i] >= PX_COUNT) error++;
  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. wdt_reset();
  147. uint8_t ret;
  148. for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
  149. ret = i2c_start(EXP_ADDR(exp) | I2C_READ);
  150. if (exp_status & (1<<exp)) {
  151. if (ret) {
  152. dprintf("lost: %d\n", exp);
  153. exp_status &= ~(1<<exp);
  154. clear_keyboard();
  155. }
  156. }
  157. else {
  158. if (!ret) {
  159. dprintf("found: %d\n", exp);
  160. exp_status |= (1<<exp);
  161. i2c_stop();
  162. expander_init(exp);
  163. clear_keyboard();
  164. }
  165. }
  166. }
  167. }
  168. inline
  169. uint8_t kimera_matrix_rows(void)
  170. {
  171. return row_count;
  172. }
  173. inline
  174. uint8_t kimera_matrix_cols(void)
  175. {
  176. #ifndef TWO_HEADED_KIMERA
  177. return col_count;
  178. #else
  179. return col_left_count;
  180. #endif
  181. }
  182. matrix_row_t kimera_read_cols(uint8_t row)
  183. {
  184. init_data(0xFF);
  185. /* read all input registers */
  186. for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
  187. expander_read_input(exp, data[exp]);
  188. }
  189. /* make cols */
  190. matrix_row_t cols = 0;
  191. for (uint8_t col = 0; col < col_count; col++) {
  192. uint8_t px = col_mapping[col];
  193. if (px != UNCONFIGURED) {
  194. if (!(data[PX_TO_EXP(px)][PX_TO_PORT(px)] & (1 << PX_TO_PIN(px)))) {
  195. cols |= (1UL << col);
  196. }
  197. }
  198. }
  199. #ifdef TWO_HEADED_KIMERA
  200. if (row < row_left_count) {
  201. cols &= col_left_mask;
  202. }
  203. else {
  204. cols >>= col_left_count;
  205. }
  206. #endif
  207. return cols;
  208. }
  209. void kimera_unselect_rows(void)
  210. {
  211. /* set all output registers to 0xFF */
  212. init_data(0xFF);
  213. for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
  214. expander_write_config(exp, data[exp]);
  215. }
  216. }
  217. void kimera_select_row(uint8_t row)
  218. {
  219. /* set selected row to low */
  220. init_data(0xFF);
  221. uint8_t px = row_mapping[row];
  222. if (px != UNCONFIGURED) {
  223. uint8_t exp = PX_TO_EXP(px);
  224. data[exp][PX_TO_PORT(px)] &= ~(1 << PX_TO_PIN(px));
  225. expander_write_config(exp, data[exp]);
  226. }
  227. }
  228. void expander_init(uint8_t exp)
  229. {
  230. init_data(0x00);
  231. /* write inversion register */
  232. /*
  233. for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
  234. expander_write_inversion(exp, data[exp]);
  235. }
  236. */
  237. /* set output bit */
  238. /*
  239. for (uint8_t row = 0; row < row_count; row++) {
  240. uint8_t px = row_mapping[row];
  241. if (px != UNCONFIGURED) {
  242. data[PX_TO_EXP(px)][PX_TO_PORT(px)] &= ~(1 << PX_TO_PIN(px));
  243. }
  244. }
  245. */
  246. /* write config registers */
  247. //expander_write_config(exp, data[exp]);
  248. /* write output registers */
  249. expander_write_output(exp, data[exp]);
  250. }
  251. uint8_t expander_write(uint8_t exp, uint8_t command, uint8_t *data)
  252. {
  253. wdt_reset();
  254. uint8_t addr = EXP_ADDR(exp);
  255. uint8_t ret;
  256. ret = i2c_start(addr | I2C_WRITE);
  257. if (ret) goto stop;
  258. ret = i2c_write(command);
  259. if (ret) goto stop;
  260. ret = i2c_write(*data++);
  261. if (ret) goto stop;
  262. ret = i2c_write(*data);
  263. stop:
  264. i2c_stop();
  265. return ret;
  266. }
  267. uint8_t expander_read(uint8_t exp, uint8_t command, uint8_t *data)
  268. {
  269. wdt_reset();
  270. uint8_t addr = EXP_ADDR(exp);
  271. uint8_t ret;
  272. ret = i2c_start(addr | I2C_WRITE);
  273. if (ret) goto stop;
  274. ret = i2c_write(command);
  275. if (ret) goto stop;
  276. ret = i2c_start(addr | I2C_READ);
  277. if (ret) goto stop;
  278. *data++ = i2c_readAck();
  279. *data = i2c_readNak();
  280. stop:
  281. i2c_stop();
  282. return ret;
  283. }
  284. inline
  285. uint8_t expander_write_output(uint8_t exp, uint8_t *data)
  286. {
  287. return expander_write(exp, EXP_COMM_OUTPUT_0, data);
  288. }
  289. inline
  290. uint8_t expander_write_inversion(uint8_t exp, uint8_t *data)
  291. {
  292. return expander_write(exp, EXP_COMM_INVERSION_0, data);
  293. }
  294. inline
  295. uint8_t expander_write_config(uint8_t exp, uint8_t *data)
  296. {
  297. return expander_write(exp, EXP_COMM_CONFIG_0, data);
  298. }
  299. inline
  300. uint8_t expander_read_input(uint8_t exp, uint8_t *data)
  301. {
  302. return expander_read(exp, EXP_COMM_INPUT_0, data);
  303. }
  304. void init_data(uint8_t value)
  305. {
  306. for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
  307. for (uint8_t port = 0; port < EXP_PORT_COUNT; port++) {
  308. data[exp][port] = value;
  309. }
  310. }
  311. }
  312. ISR(WDT_vect)
  313. {
  314. dprintf("i2c timeout\n");
  315. /* let slave to release SDA */
  316. TWCR = 0;
  317. DDRD |= (1<<PD0);
  318. DDRD &= ~(1<<PD1);
  319. if (!(PIND & (1<<PD1))) {
  320. for (uint8_t i = 0; i < 9; i++) {
  321. PORTD &= ~(1<<PD0);
  322. _delay_us(SCL_DURATION);
  323. PORTD |= (1<<PD0);
  324. _delay_us(SCL_DURATION);
  325. }
  326. }
  327. /* send stop condition */
  328. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
  329. /* escape from loop */
  330. i2c_force_stop = 1;
  331. }