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 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. 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. 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. uint8_t row_count = 8;
  56. uint8_t col_count = 24;
  57. #else
  58. uint8_t row_count = 16;
  59. uint8_t col_count = 32;
  60. #endif
  61. uint8_t data[EXP_COUNT][EXP_PORT_COUNT];
  62. uint8_t exp_status = 0;
  63. void kimera_init(void)
  64. {
  65. /* read config */
  66. //write_matrix_mapping(); /* debug */
  67. if (read_matrix_mapping()) {
  68. write_matrix_mapping();
  69. }
  70. /* init i2c */
  71. i2c_init();
  72. /* init i/o expanders */
  73. kimera_scan();
  74. /* init watch dog */
  75. wdt_init();
  76. }
  77. void wdt_init(void)
  78. {
  79. cli();
  80. wdt_reset();
  81. wdt_intr_enable(WDTO_1S);
  82. sei();
  83. }
  84. uint8_t read_matrix_mapping(void)
  85. {
  86. uint8_t error = 0;
  87. /* read number of rows and cols */
  88. row_count = eeprom_read_byte(EECONFIG_ROW_COUNT);
  89. col_count = eeprom_read_byte(EECONFIG_COL_COUNT);
  90. if (row_count == 0) error++;
  91. if (row_count == UNCONFIGURED) error++;
  92. if (col_count == 0) error++;
  93. if (col_count == UNCONFIGURED) error++;
  94. if (row_count + col_count > PX_COUNT) error++;
  95. /* read row mapping */
  96. uint8_t *mapping = EECONFIG_ROW_COL_MAPPING;
  97. for (uint8_t i = 0; i < PX_COUNT; i++) {
  98. if (i < row_count) {
  99. row_mapping[i] = eeprom_read_byte(mapping++);
  100. if (row_mapping[i] >= PX_COUNT) error++;
  101. }
  102. else {
  103. row_mapping[i] = UNCONFIGURED;
  104. }
  105. }
  106. /* read col mapping*/
  107. for (uint8_t i = 0; i < PX_COUNT; i++) {
  108. if (i < col_count) {
  109. col_mapping[i] = eeprom_read_byte(mapping++);
  110. if (col_mapping[i] >= PX_COUNT) error++;
  111. }
  112. else {
  113. col_mapping[i] = UNCONFIGURED;
  114. }
  115. }
  116. return error;
  117. }
  118. void write_matrix_mapping(void)
  119. {
  120. /* write number of rows and cols */
  121. eeprom_write_byte(EECONFIG_ROW_COUNT, row_count);
  122. eeprom_write_byte(EECONFIG_COL_COUNT, col_count);
  123. /* write row mapping */
  124. uint8_t *mapping = EECONFIG_ROW_COL_MAPPING;
  125. for (uint8_t row = 0; row < row_count; row++) {
  126. eeprom_write_byte(mapping++, row_mapping[row]);
  127. }
  128. /* write col mapping */
  129. for (uint8_t col = 0; col < col_count; col++) {
  130. eeprom_write_byte(mapping++, col_mapping[col]);
  131. }
  132. }
  133. void kimera_scan(void)
  134. {
  135. wdt_reset();
  136. uint8_t ret;
  137. for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
  138. ret = i2c_start(EXP_ADDR(exp) | I2C_READ);
  139. if (exp_status & (1<<exp)) {
  140. if (ret) {
  141. dprintf("lost: %d\n", exp);
  142. exp_status &= ~(1<<exp);
  143. clear_keyboard();
  144. }
  145. }
  146. else {
  147. if (!ret) {
  148. dprintf("found: %d\n", exp);
  149. exp_status |= (1<<exp);
  150. i2c_stop();
  151. expander_init(exp);
  152. clear_keyboard();
  153. }
  154. }
  155. }
  156. }
  157. matrix_row_t read_cols(void)
  158. {
  159. init_data(0xFF);
  160. /* read all input registers */
  161. for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
  162. expander_read_input(exp, data[exp]);
  163. }
  164. /* make cols */
  165. matrix_row_t cols = 0;
  166. for (uint8_t col = 0; col < col_count; col++) {
  167. uint8_t px = col_mapping[col];
  168. if (px != UNCONFIGURED) {
  169. if (!(data[PX_TO_EXP(px)][PX_TO_PORT(px)] & (1 << PX_TO_PIN(px)))) {
  170. cols |= (1UL << col);
  171. }
  172. }
  173. }
  174. return cols;
  175. }
  176. void unselect_rows(void)
  177. {
  178. /* set all output registers to 0xFF */
  179. init_data(0xFF);
  180. for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
  181. expander_write_config(exp, data[exp]);
  182. }
  183. }
  184. void select_row(uint8_t row)
  185. {
  186. /* set selected row to low */
  187. init_data(0xFF);
  188. uint8_t px = row_mapping[row];
  189. if (px != UNCONFIGURED) {
  190. uint8_t exp = PX_TO_EXP(px);
  191. data[exp][PX_TO_PORT(px)] &= ~(1 << PX_TO_PIN(px));
  192. expander_write_config(exp, data[exp]);
  193. }
  194. }
  195. void expander_init(uint8_t exp)
  196. {
  197. init_data(0x00);
  198. /* write inversion register */
  199. /*
  200. for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
  201. expander_write_inversion(exp, data[exp]);
  202. }
  203. */
  204. /* set output bit */
  205. /*
  206. for (uint8_t row = 0; row < row_count; row++) {
  207. uint8_t px = row_mapping[row];
  208. if (px != UNCONFIGURED) {
  209. data[PX_TO_EXP(px)][PX_TO_PORT(px)] &= ~(1 << PX_TO_PIN(px));
  210. }
  211. }
  212. */
  213. /* write config registers */
  214. //expander_write_config(exp, data[exp]);
  215. /* write output registers */
  216. expander_write_output(exp, data[exp]);
  217. }
  218. uint8_t expander_write(uint8_t exp, uint8_t command, uint8_t *data)
  219. {
  220. wdt_reset();
  221. uint8_t addr = EXP_ADDR(exp);
  222. uint8_t ret;
  223. ret = i2c_start(addr | I2C_WRITE);
  224. if (ret) goto stop;
  225. ret = i2c_write(command);
  226. if (ret) goto stop;
  227. ret = i2c_write(*data++);
  228. if (ret) goto stop;
  229. ret = i2c_write(*data);
  230. stop:
  231. i2c_stop();
  232. return ret;
  233. }
  234. uint8_t expander_read(uint8_t exp, uint8_t command, uint8_t *data)
  235. {
  236. wdt_reset();
  237. uint8_t addr = EXP_ADDR(exp);
  238. uint8_t ret;
  239. ret = i2c_start(addr | I2C_WRITE);
  240. if (ret) goto stop;
  241. ret = i2c_write(command);
  242. if (ret) goto stop;
  243. ret = i2c_start(addr | I2C_READ);
  244. if (ret) goto stop;
  245. *data++ = i2c_readAck();
  246. *data = i2c_readNak();
  247. stop:
  248. i2c_stop();
  249. return ret;
  250. }
  251. inline
  252. uint8_t expander_write_output(uint8_t exp, uint8_t *data)
  253. {
  254. return expander_write(exp, EXP_COMM_OUTPUT_0, data);
  255. }
  256. inline
  257. uint8_t expander_write_inversion(uint8_t exp, uint8_t *data)
  258. {
  259. return expander_write(exp, EXP_COMM_INVERSION_0, data);
  260. }
  261. inline
  262. uint8_t expander_write_config(uint8_t exp, uint8_t *data)
  263. {
  264. return expander_write(exp, EXP_COMM_CONFIG_0, data);
  265. }
  266. inline
  267. uint8_t expander_read_input(uint8_t exp, uint8_t *data)
  268. {
  269. return expander_read(exp, EXP_COMM_INPUT_0, data);
  270. }
  271. void init_data(uint8_t value)
  272. {
  273. for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
  274. for (uint8_t port = 0; port < EXP_PORT_COUNT; port++) {
  275. data[exp][port] = value;
  276. }
  277. }
  278. }
  279. ISR(WDT_vect)
  280. {
  281. dprintf("i2c timeout\n");
  282. /* let slave to release SDA */
  283. TWCR = 0;
  284. DDRD |= (1<<PD0);
  285. DDRD &= ~(1<<PD1);
  286. if (!(PIND & (1<<PD1))) {
  287. for (uint8_t i = 0; i < 9; i++) {
  288. PORTD &= ~(1<<PD0);
  289. _delay_us(SCL_DURATION);
  290. PORTD |= (1<<PD0);
  291. _delay_us(SCL_DURATION);
  292. }
  293. }
  294. /* send stop condition */
  295. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
  296. /* escape from loop */
  297. i2c_force_stop = 1;
  298. }