Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
Dieses Repo ist archiviert. Du kannst Dateien sehen und es klonen, kannst aber nicht pushen oder Issues/Pull-Requests öffnen.

kimera.c 7.9KB

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