Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
Este repositório está arquivado. Você pode visualizar os arquivos e realizar clone, mas não poderá realizar push nem abrir issues e pull requests.

kimera.c 5.0KB

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