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

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