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.

117 lines
3.3 KiB

[Keyboard] add support for ModelM USB board (#9846) * add support for ModelM USB board * EMI improvement: remove unnecessary toggling of MOSI pin * address review comments * Update keyboards/mschwingen/modelm/rules.mk Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> * Update keyboards/mschwingen/modelm/rules.mk Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> * Update keyboards/mschwingen/modelm/config.h Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> * Update keyboards/mschwingen/modelm/config.h Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> * Update keyboards/mschwingen/modelm/rules.mk Co-authored-by: Drashna Jaelre <drashna@live.com> * Update keyboards/mschwingen/modelm/keymaps/default/keymap.c Co-authored-by: Drashna Jaelre <drashna@live.com> * update printf usage * add comment * EMI improvement: remove unnecessary toggling of MOSI signal * remove trailing space * use shorter macros as suggested in review by noroadsleft, re-format table to line up columns * Update keyboards/mschwingen/modelm/config.h Co-authored-by: Ryan <fauxpark@gmail.com> * Update keyboards/mschwingen/modelm/rules.mk Co-authored-by: Ryan <fauxpark@gmail.com> * Update keyboards/mschwingen/modelm/rules.mk Co-authored-by: Ryan <fauxpark@gmail.com> * Update keyboards/mschwingen/modelm/rules.mk Co-authored-by: Ryan <fauxpark@gmail.com> * Update keyboards/mschwingen/modelm/README.md Co-authored-by: Ryan <fauxpark@gmail.com> * Update keyboards/mschwingen/modelm/README.md Co-authored-by: Ryan <fauxpark@gmail.com> * Apply suggestions from code review use spi_read from core insteads of our own copy Co-authored-by: Ryan <fauxpark@gmail.com> * include spi_master.c to use spi_read() * Update keyboards/mschwingen/modelm/README.md Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> * Apply suggestions from code review: correct indenting in keymap Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> * Apply suggestions from code review use automatic variant defines from makefile instead of defining our own Co-authored-by: Drashna Jaelre <drashna@live.com> * Update keyboards/mschwingen/modelm/rules.mk: use QUANTUM_LIB_SRC for uart.c Co-authored-by: Drashna Jaelre <drashna@live.com> Co-authored-by: Michael Schwingen <michael@schwingen.org> Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> Co-authored-by: Drashna Jaelre <drashna@live.com> Co-authored-by: Ryan <fauxpark@gmail.com>
3 years ago
[Keyboard] add support for ModelM USB board (#9846) * add support for ModelM USB board * EMI improvement: remove unnecessary toggling of MOSI pin * address review comments * Update keyboards/mschwingen/modelm/rules.mk Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> * Update keyboards/mschwingen/modelm/rules.mk Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> * Update keyboards/mschwingen/modelm/config.h Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> * Update keyboards/mschwingen/modelm/config.h Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> * Update keyboards/mschwingen/modelm/rules.mk Co-authored-by: Drashna Jaelre <drashna@live.com> * Update keyboards/mschwingen/modelm/keymaps/default/keymap.c Co-authored-by: Drashna Jaelre <drashna@live.com> * update printf usage * add comment * EMI improvement: remove unnecessary toggling of MOSI signal * remove trailing space * use shorter macros as suggested in review by noroadsleft, re-format table to line up columns * Update keyboards/mschwingen/modelm/config.h Co-authored-by: Ryan <fauxpark@gmail.com> * Update keyboards/mschwingen/modelm/rules.mk Co-authored-by: Ryan <fauxpark@gmail.com> * Update keyboards/mschwingen/modelm/rules.mk Co-authored-by: Ryan <fauxpark@gmail.com> * Update keyboards/mschwingen/modelm/rules.mk Co-authored-by: Ryan <fauxpark@gmail.com> * Update keyboards/mschwingen/modelm/README.md Co-authored-by: Ryan <fauxpark@gmail.com> * Update keyboards/mschwingen/modelm/README.md Co-authored-by: Ryan <fauxpark@gmail.com> * Apply suggestions from code review use spi_read from core insteads of our own copy Co-authored-by: Ryan <fauxpark@gmail.com> * include spi_master.c to use spi_read() * Update keyboards/mschwingen/modelm/README.md Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> * Apply suggestions from code review: correct indenting in keymap Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> * Apply suggestions from code review use automatic variant defines from makefile instead of defining our own Co-authored-by: Drashna Jaelre <drashna@live.com> * Update keyboards/mschwingen/modelm/rules.mk: use QUANTUM_LIB_SRC for uart.c Co-authored-by: Drashna Jaelre <drashna@live.com> Co-authored-by: Michael Schwingen <michael@schwingen.org> Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> Co-authored-by: Drashna Jaelre <drashna@live.com> Co-authored-by: Ryan <fauxpark@gmail.com>
3 years ago
  1. /*
  2. * Copyright 2020 Michael Schwingen
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdint.h>
  17. #include <stdbool.h>
  18. #include "util.h"
  19. #include "matrix.h"
  20. #include "debounce.h"
  21. #include "quantum.h"
  22. #include "spi_master.h"
  23. #include "print.h"
  24. #include "modelm.h"
  25. #define DEBUG 0
  26. #define SPI_TIMEOUT 100
  27. /* Keyboard Matrix Assignments */
  28. static uint16_t row_bits[MATRIX_ROWS] = {
  29. 0x4000, 0x8000, 0x2000, 0x1000, 0x0800, 0x0400, 0x0100, 0x0200,
  30. 0x0040, 0x0080, 0x0020, 0x0010, 0x0008, 0x0004, 0x0001, 0x0002};
  31. static const pin_t col_pins[MATRIX_COLS] = {D1, D4, D7, B4, F7, F6, F5, F4};
  32. static void select_col(uint8_t col) {
  33. setPinOutput(col_pins[col]);
  34. writePinLow(col_pins[col]);
  35. }
  36. static void unselect_col(uint8_t col) { setPinInputHigh(col_pins[col]); }
  37. static void unselect_cols(void) {
  38. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  39. setPinInputHigh(col_pins[x]);
  40. }
  41. }
  42. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  43. uint16_t row_data;
  44. bool matrix_changed = false;
  45. // Select col and wait for col selecton to stabilize
  46. select_col(current_col);
  47. matrix_io_delay();
  48. writePinLow(SR_LOAD_PIN);
  49. writePinHigh(SR_LOAD_PIN);
  50. row_data = spi_read() << 8;
  51. row_data |= spi_read();
  52. #if DEBUG
  53. print_hex8(~row_data);
  54. uprint(" ");
  55. #endif
  56. // For each row...
  57. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  58. // Store last value of row prior to reading
  59. matrix_row_t last_row_value = current_matrix[row_index];
  60. matrix_row_t current_row_value = last_row_value;
  61. // Check row pin state
  62. if ((row_data & row_bits[row_index]) == 0) {
  63. // Pin LO, set col bit
  64. current_row_value |= (MATRIX_ROW_SHIFTER << current_col);
  65. } else {
  66. // Pin HI, clear col bit
  67. current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col);
  68. }
  69. // Determine if the matrix changed state
  70. if ((last_row_value != current_row_value)) {
  71. matrix_changed = true;
  72. current_matrix[row_index] = current_row_value;
  73. }
  74. }
  75. // Unselect col
  76. unselect_col(current_col);
  77. return matrix_changed;
  78. }
  79. void matrix_init_custom(void) {
  80. unselect_cols();
  81. // set 4MHz SPI clock
  82. SPSR = 0;
  83. SPCR = _BV(SPE) | _BV(MSTR) | _BV(CPOL);
  84. }
  85. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  86. bool changed = false;
  87. #if DEBUG
  88. uprint("\r\nScan: ");
  89. #endif
  90. // Set col, read rows
  91. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  92. changed |= read_rows_on_col(current_matrix, current_col);
  93. }
  94. update_layer_leds();
  95. return changed;
  96. }