Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

kimera.c 7.8KB

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