Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
Repozitorijs ir arhivēts. Tam var aplūkot failus un to var klonēt, bet nevar iesūtīt jaunas izmaiņas, kā arī atvērt jaunas problēmas/izmaiņu pieprasījumus.

kimera.c 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 "kimera.h"
  19. #include "debug.h"
  20. uint8_t mux_mapping[MUX_COUNT] = {
  21. MUX_FOR_ROW, MUX_FOR_COL, MUX_FOR_COL, MUX_FOR_COL
  22. };
  23. uint8_t row_mapping[MATRIX_ROWS] = {
  24. 0, 1, 2, 3, 4, 5, 6, 7,
  25. UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED,
  26. UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED
  27. };
  28. uint8_t col_mapping[MATRIX_COLS] = {
  29. 8, 9, 10, 11, 12, 13, 14, 15,
  30. 16, 17, 18, 19, 20, 21, 22, 23,
  31. 24, 25, 26, 27, 28, 29, 30, 31
  32. };
  33. uint8_t row_max_count = MUX_PORTS * 1;
  34. uint8_t col_max_count = MUX_PORTS * (MUX_COUNT - 1);
  35. uint16_t shift_out_cache = 0;
  36. void kimera_init(void)
  37. {
  38. // read config
  39. write_matrix_mapping();
  40. if (read_matrix_mapping()) {
  41. write_matrix_mapping();
  42. }
  43. // init shift out pins
  44. MOSI_DDR |= (1<<MOSI_BIT);
  45. SCK_DDR |= (1<<SCK_BIT);
  46. RCK_DDR |= (1<<RCK_BIT);
  47. RCK_PORT |= (1<<RCK_BIT);
  48. // init spi
  49. SPCR |= ((1<<SPE) | (1<<MSTR));
  50. SPSR |= ((1<<SPI2X));
  51. }
  52. uint8_t read_matrix_mapping(void)
  53. {
  54. uint8_t error = 0;
  55. uint8_t mux_config = 0;
  56. row_max_count = 0;
  57. col_max_count = 0;
  58. mux_config = eeprom_read_byte(EECONFIG_MUX_MAPPING);
  59. if (mux_config == 0 || (mux_config & (1<<7))) {
  60. error++;
  61. return error;
  62. }
  63. for (uint8_t mux = 0; mux < MUX_COUNT; mux++) {
  64. mux_mapping[mux] = mux_config & (1 << mux);
  65. if (mux_mapping[mux] == MUX_FOR_COL) {
  66. col_max_count += MUX_PORTS;
  67. }
  68. else {
  69. row_max_count += MUX_PORTS;
  70. }
  71. }
  72. if ((col_max_count == 0) || (row_max_count == 0)) {
  73. error++;
  74. }
  75. uint8_t *mapping = EECONFIG_ROW_COL_MAPPING;
  76. for (uint8_t row = 0; row < row_max_count; row++) {
  77. row_mapping[row] = eeprom_read_byte(mapping++);
  78. if (row_mapping[row] != UNCONFIGURED) {
  79. if (mux_mapping[PX_TO_MUX(row_mapping[row])] != MUX_FOR_ROW) {
  80. row_mapping[row] = UNCONFIGURED;
  81. error++;
  82. }
  83. }
  84. }
  85. for (uint8_t col = 0; col < col_max_count; col++) {
  86. col_mapping[col] = eeprom_read_byte(mapping++);
  87. if (col_mapping[col] != UNCONFIGURED) {
  88. if (mux_mapping[PX_TO_MUX(col_mapping[col])] != MUX_FOR_COL) {
  89. col_mapping[col] = UNCONFIGURED;
  90. error++;
  91. }
  92. }
  93. }
  94. return error;
  95. }
  96. void write_matrix_mapping(void)
  97. {
  98. uint8_t mux_config = 0;
  99. row_max_count = 0;
  100. col_max_count = 0;
  101. for (uint8_t mux = 0; mux < MUX_COUNT; mux++) {
  102. mux_config |= (mux_mapping[mux] << mux);
  103. if (mux_mapping[mux] == MUX_FOR_COL) {
  104. col_max_count += MUX_PORTS;
  105. }
  106. else {
  107. row_max_count += MUX_PORTS;
  108. }
  109. }
  110. eeprom_write_byte(EECONFIG_MUX_MAPPING, mux_config);
  111. uint8_t *mapping = EECONFIG_ROW_COL_MAPPING;
  112. for (uint8_t row = 0; row < row_max_count; row++) {
  113. eeprom_write_byte(mapping++, row_mapping[row]);
  114. }
  115. for (uint8_t col = 0; col < col_max_count; col++) {
  116. eeprom_write_byte(mapping++, col_mapping[col]);
  117. }
  118. }
  119. void shift_out_word(uint16_t data)
  120. {
  121. SPDR = ((data>>8) & 0xFF);
  122. while (!(SPSR & (1<<SPIF)));
  123. SPDR = (data & 0xFF);
  124. while (!(SPSR & (1<<SPIF)));
  125. RCK_PORT &= ~(1<<RCK_BIT);
  126. RCK_PORT |= (1<<RCK_BIT);
  127. }
  128. void init_cols(void)
  129. {
  130. // init mux io pins
  131. for (uint8_t mux = 0; mux < MUX_COUNT; mux++) {
  132. uint8_t bit = MUX_TO_ZX_BIT(mux);
  133. if (mux_mapping[mux] == MUX_FOR_COL) {
  134. ZX_DDR &= ~(1 << bit);
  135. ZX_PORT |= (1 << bit);
  136. }
  137. else {
  138. ZX_DDR |= (1 << bit);
  139. ZX_PORT |= (1 << bit);
  140. }
  141. }
  142. }
  143. matrix_row_t read_cols(void)
  144. {
  145. matrix_row_t cols = 0;
  146. for (uint8_t col = 0; col < col_max_count; col++) {
  147. uint8_t px = col_mapping[col];
  148. if (px != UNCONFIGURED) {
  149. uint8_t mux = PX_TO_MUX(px);
  150. shift_out_word((shift_out_cache | PX_TO_SHIFT_OUT(px)) & ~(MUX_INH_TO_SHIFT_OUT(mux)));
  151. _delay_us(10);
  152. if (!(ZX_PIN & (1 << MUX_TO_ZX_BIT(mux)))) {
  153. cols |= (1UL << col);
  154. }
  155. }
  156. }
  157. return cols;
  158. }
  159. void unselect_rows(void)
  160. {
  161. shift_out_word(0);
  162. }
  163. void select_row(uint8_t row)
  164. {
  165. uint8_t px = row_mapping[row];
  166. if (px != UNCONFIGURED) {
  167. uint8_t mux = PX_TO_MUX(px);
  168. ZX_PORT &= ~(1 << MUX_TO_ZX_BIT(mux));
  169. shift_out_cache = ((MUX_OFF_TO_SHIFT_OUT | PX_TO_SHIFT_OUT(px)) & ~(MUX_INH_TO_SHIFT_OUT(mux)));
  170. shift_out_word(shift_out_cache);
  171. }
  172. }