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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 <util/delay.h>
  18. #include "i2cmaster.h"
  19. #include "kimera.h"
  20. #include "debug.h"
  21. uint8_t row_mapping[PX_COUNT] = {
  22. 0, 1, 2, 3, 4, 5, 6, 7,
  23. UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED,
  24. UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED,
  25. UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED
  26. };
  27. uint8_t col_mapping[PX_COUNT] = {
  28. 8, 9, 10, 11, 12, 13, 14, 15,
  29. 16, 17, 18, 19, 20, 21, 22, 23,
  30. 24, 25, 26, 27, 28, 29, 30, 31,
  31. UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED
  32. };
  33. uint8_t row_count = 8;
  34. uint8_t col_count = 24;
  35. uint8_t data[EXP_COUNT][EXP_PORT_COUNT];
  36. void kimera_init(void)
  37. {
  38. /* read config */
  39. write_matrix_mapping(); /* debug */
  40. if (read_matrix_mapping()) {
  41. write_matrix_mapping();
  42. }
  43. /* init i2c */
  44. i2c_init();
  45. /* init i/o expander */
  46. expander_init();
  47. }
  48. uint8_t read_matrix_mapping(void)
  49. {
  50. uint8_t error = 0;
  51. /* read number of rows and cols */
  52. row_count = eeprom_read_byte(EECONFIG_ROW_COUNT);
  53. col_count = eeprom_read_byte(EECONFIG_COL_COUNT);
  54. if (row_count == 0) error++;
  55. if (row_count == UNCONFIGURED) error++;
  56. if (col_count == 0) error++;
  57. if (col_count == UNCONFIGURED) error++;
  58. if (row_count + col_count > PX_COUNT) error++;
  59. /* read row mapping */
  60. uint8_t *mapping = EECONFIG_ROW_COL_MAPPING;
  61. for (uint8_t i = 0; i < PX_COUNT; i++) {
  62. if (i < row_count) {
  63. row_mapping[i] = eeprom_read_byte(mapping++);
  64. if (row_mapping[i] >= PX_COUNT) error++;
  65. }
  66. else {
  67. row_mapping[i] = UNCONFIGURED;
  68. }
  69. }
  70. /* read col mapping*/
  71. for (uint8_t i = 0; i < PX_COUNT; i++) {
  72. if (i < col_count) {
  73. col_mapping[i] = eeprom_read_byte(mapping++);
  74. if (col_mapping[i] >= PX_COUNT) error++;
  75. }
  76. else {
  77. col_mapping[i] = UNCONFIGURED;
  78. }
  79. }
  80. return error;
  81. }
  82. void write_matrix_mapping(void)
  83. {
  84. /* write number of rows and cols */
  85. eeprom_write_byte(EECONFIG_ROW_COUNT, row_count);
  86. eeprom_write_byte(EECONFIG_COL_COUNT, col_count);
  87. /* write row mapping */
  88. uint8_t *mapping = EECONFIG_ROW_COL_MAPPING;
  89. for (uint8_t row = 0; row < row_count; row++) {
  90. eeprom_write_byte(mapping++, row_mapping[row]);
  91. }
  92. /* write col mapping */
  93. for (uint8_t col = 0; col < col_count; col++) {
  94. eeprom_write_byte(mapping++, col_mapping[col]);
  95. }
  96. }
  97. matrix_row_t read_cols(void)
  98. {
  99. init_data(0x00);
  100. /* read all input registers */
  101. for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
  102. expander_read_input(exp, data[exp]);
  103. }
  104. /* make cols */
  105. matrix_row_t cols = 0;
  106. for (uint8_t col = 0; col < col_count; col++) {
  107. uint8_t px = col_mapping[col];
  108. if (px != UNCONFIGURED) {
  109. if (data[PX_TO_EXP(px)][PX_TO_PORT(px)] & (1 << PX_TO_PIN(px))) {
  110. cols |= (1UL << col);
  111. }
  112. }
  113. }
  114. return cols;
  115. }
  116. void unselect_rows(void)
  117. {
  118. /* set all output registers to 0xFF */
  119. init_data(0xFF);
  120. for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
  121. expander_write_output(exp, data[exp]);
  122. }
  123. }
  124. void select_row(uint8_t row)
  125. {
  126. /* set selected row to low */
  127. init_data(0xFF);
  128. uint8_t px = row_mapping[row];
  129. if (px != UNCONFIGURED) {
  130. uint8_t exp = PX_TO_EXP(px);
  131. data[exp][PX_TO_PORT(px)] &= ~(1 << PX_TO_PIN(px));
  132. expander_write_output(exp, data[exp]);
  133. }
  134. }
  135. void expander_init(void)
  136. {
  137. init_data(0xFF);
  138. /* write inversion register */
  139. for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
  140. expander_write_inversion(exp, data[exp]);
  141. }
  142. /* set output bit */
  143. for (uint8_t row = 0; row < row_count; row++) {
  144. uint8_t px = row_mapping[row];
  145. if (px != UNCONFIGURED) {
  146. data[PX_TO_EXP(px)][PX_TO_PORT(px)] &= ~(1 << PX_TO_PIN(px));
  147. }
  148. }
  149. /* write config registers */
  150. for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
  151. expander_write_config(exp, data[exp]);
  152. }
  153. }
  154. uint8_t expander_write(uint8_t exp, uint8_t command, uint8_t *data)
  155. {
  156. uint8_t addr = EXP_ADDR(exp);
  157. uint8_t ret;
  158. ret = i2c_start(addr | I2C_WRITE);
  159. if (ret) goto stop;
  160. ret = i2c_write(command);
  161. if (ret) goto stop;
  162. ret = i2c_write(*data++);
  163. if (ret) goto stop;
  164. ret = i2c_write(*data);
  165. stop:
  166. i2c_stop();
  167. return ret;
  168. }
  169. uint8_t expander_read(uint8_t exp, uint8_t command, uint8_t *data)
  170. {
  171. uint8_t addr = EXP_ADDR(exp);
  172. uint8_t ret;
  173. ret = i2c_start(addr | I2C_WRITE);
  174. if (ret) goto stop;
  175. ret = i2c_write(command);
  176. if (ret) goto stop;
  177. ret = i2c_start(addr | I2C_READ);
  178. if (ret) goto stop;
  179. *data++ = i2c_readAck();
  180. *data = i2c_readNak();
  181. stop:
  182. i2c_stop();
  183. return ret;
  184. }
  185. inline
  186. uint8_t expander_write_output(uint8_t exp, uint8_t *data)
  187. {
  188. return expander_write(exp, EXP_COMM_OUTPUT_0, data);
  189. }
  190. inline
  191. uint8_t expander_write_inversion(uint8_t exp, uint8_t *data)
  192. {
  193. return expander_write(exp, EXP_COMM_INVERSION_0, data);
  194. }
  195. inline
  196. uint8_t expander_write_config(uint8_t exp, uint8_t *data)
  197. {
  198. return expander_write(exp, EXP_COMM_CONFIG_0, data);
  199. }
  200. inline
  201. uint8_t expander_read_input(uint8_t exp, uint8_t *data)
  202. {
  203. return expander_read(exp, EXP_COMM_INPUT_0, data);
  204. }
  205. void init_data(uint8_t value)
  206. {
  207. for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
  208. for (uint8_t port = 0; port < EXP_PORT_COUNT; port++) {
  209. data[exp][port] = value;
  210. }
  211. }
  212. }