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.

227 lines
6.1 KiB

2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change
3 years ago
2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change
3 years ago
2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change
3 years ago
2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change
3 years ago
  1. /* Copyright 2020 Nick Brassel (tzarc)
  2. *
  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 3 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 <https://www.gnu.org/licenses/>.
  15. */
  16. #include "spi_master.h"
  17. #include "timer.h"
  18. static pin_t currentSlavePin = NO_PIN;
  19. #if defined(K20x) || defined(KL2x)
  20. static SPIConfig spiConfig = {NULL, 0, 0, 0};
  21. #else
  22. static SPIConfig spiConfig = {false, NULL, 0, 0, 0, 0};
  23. #endif
  24. __attribute__((weak)) void spi_init(void) {
  25. static bool is_initialised = false;
  26. if (!is_initialised) {
  27. is_initialised = true;
  28. // Try releasing special pins for a short time
  29. setPinInput(SPI_SCK_PIN);
  30. setPinInput(SPI_MOSI_PIN);
  31. setPinInput(SPI_MISO_PIN);
  32. chThdSleepMilliseconds(10);
  33. #if defined(USE_GPIOV1)
  34. palSetPadMode(PAL_PORT(SPI_SCK_PIN), PAL_PAD(SPI_SCK_PIN), SPI_SCK_PAL_MODE);
  35. palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), SPI_MOSI_PAL_MODE);
  36. palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), SPI_MISO_PAL_MODE);
  37. #else
  38. palSetPadMode(PAL_PORT(SPI_SCK_PIN), PAL_PAD(SPI_SCK_PIN), PAL_MODE_ALTERNATE(SPI_SCK_PAL_MODE) | PAL_OUTPUT_TYPE_PUSHPULL | PAL_OUTPUT_SPEED_HIGHEST);
  39. palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), PAL_MODE_ALTERNATE(SPI_MOSI_PAL_MODE) | PAL_OUTPUT_TYPE_PUSHPULL | PAL_OUTPUT_SPEED_HIGHEST);
  40. palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), PAL_MODE_ALTERNATE(SPI_MISO_PAL_MODE) | PAL_OUTPUT_TYPE_PUSHPULL | PAL_OUTPUT_SPEED_HIGHEST);
  41. #endif
  42. }
  43. }
  44. bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor) {
  45. if (currentSlavePin != NO_PIN || slavePin == NO_PIN) {
  46. return false;
  47. }
  48. uint16_t roundedDivisor = 2;
  49. while (roundedDivisor < divisor) {
  50. roundedDivisor <<= 1;
  51. }
  52. if (roundedDivisor < 2 || roundedDivisor > 256) {
  53. return false;
  54. }
  55. #if defined(K20x) || defined(KL2x)
  56. spiConfig.tar0 = SPIx_CTARn_FMSZ(7) | SPIx_CTARn_ASC(1);
  57. if (lsbFirst) {
  58. spiConfig.tar0 |= SPIx_CTARn_LSBFE;
  59. }
  60. switch (mode) {
  61. case 0:
  62. break;
  63. case 1:
  64. spiConfig.tar0 |= SPIx_CTARn_CPHA;
  65. break;
  66. case 2:
  67. spiConfig.tar0 |= SPIx_CTARn_CPOL;
  68. break;
  69. case 3:
  70. spiConfig.tar0 |= SPIx_CTARn_CPHA | SPIx_CTARn_CPOL;
  71. break;
  72. }
  73. switch (roundedDivisor) {
  74. case 2:
  75. spiConfig.tar0 |= SPIx_CTARn_BR(0);
  76. break;
  77. case 4:
  78. spiConfig.tar0 |= SPIx_CTARn_BR(1);
  79. break;
  80. case 8:
  81. spiConfig.tar0 |= SPIx_CTARn_BR(3);
  82. break;
  83. case 16:
  84. spiConfig.tar0 |= SPIx_CTARn_BR(4);
  85. break;
  86. case 32:
  87. spiConfig.tar0 |= SPIx_CTARn_BR(5);
  88. break;
  89. case 64:
  90. spiConfig.tar0 |= SPIx_CTARn_BR(6);
  91. break;
  92. case 128:
  93. spiConfig.tar0 |= SPIx_CTARn_BR(7);
  94. break;
  95. case 256:
  96. spiConfig.tar0 |= SPIx_CTARn_BR(8);
  97. break;
  98. }
  99. #elif defined(HT32)
  100. spiConfig.cr0 = SPI_CR0_SELOEN;
  101. spiConfig.cr1 = SPI_CR1_MODE | 8; // 8 bits and in master mode
  102. if (lsbFirst) {
  103. spiConfig.cr1 |= SPI_CR1_FIRSTBIT;
  104. }
  105. switch (mode) {
  106. case 0:
  107. spiConfig.cr1 |= SPI_CR1_FORMAT_MODE0;
  108. break;
  109. case 1:
  110. spiConfig.cr1 |= SPI_CR1_FORMAT_MODE1;
  111. break;
  112. case 2:
  113. spiConfig.cr1 |= SPI_CR1_FORMAT_MODE2;
  114. break;
  115. case 3:
  116. spiConfig.cr1 |= SPI_CR1_FORMAT_MODE3;
  117. break;
  118. }
  119. spiConfig.cpr = (roundedDivisor - 1) >> 1;
  120. #else
  121. spiConfig.cr1 = 0;
  122. if (lsbFirst) {
  123. spiConfig.cr1 |= SPI_CR1_LSBFIRST;
  124. }
  125. switch (mode) {
  126. case 0:
  127. break;
  128. case 1:
  129. spiConfig.cr1 |= SPI_CR1_CPHA;
  130. break;
  131. case 2:
  132. spiConfig.cr1 |= SPI_CR1_CPOL;
  133. break;
  134. case 3:
  135. spiConfig.cr1 |= SPI_CR1_CPHA | SPI_CR1_CPOL;
  136. break;
  137. }
  138. switch (roundedDivisor) {
  139. case 2:
  140. break;
  141. case 4:
  142. spiConfig.cr1 |= SPI_CR1_BR_0;
  143. break;
  144. case 8:
  145. spiConfig.cr1 |= SPI_CR1_BR_1;
  146. break;
  147. case 16:
  148. spiConfig.cr1 |= SPI_CR1_BR_1 | SPI_CR1_BR_0;
  149. break;
  150. case 32:
  151. spiConfig.cr1 |= SPI_CR1_BR_2;
  152. break;
  153. case 64:
  154. spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_0;
  155. break;
  156. case 128:
  157. spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_1;
  158. break;
  159. case 256:
  160. spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0;
  161. break;
  162. }
  163. #endif
  164. currentSlavePin = slavePin;
  165. spiConfig.ssport = PAL_PORT(slavePin);
  166. spiConfig.sspad = PAL_PAD(slavePin);
  167. setPinOutput(slavePin);
  168. spiStart(&SPI_DRIVER, &spiConfig);
  169. spiSelect(&SPI_DRIVER);
  170. return true;
  171. }
  172. spi_status_t spi_write(uint8_t data) {
  173. uint8_t rxData;
  174. spiExchange(&SPI_DRIVER, 1, &data, &rxData);
  175. return rxData;
  176. }
  177. spi_status_t spi_read(void) {
  178. uint8_t data = 0;
  179. spiReceive(&SPI_DRIVER, 1, &data);
  180. return data;
  181. }
  182. spi_status_t spi_transmit(const uint8_t *data, uint16_t length) {
  183. spiSend(&SPI_DRIVER, length, data);
  184. return SPI_STATUS_SUCCESS;
  185. }
  186. spi_status_t spi_receive(uint8_t *data, uint16_t length) {
  187. spiReceive(&SPI_DRIVER, length, data);
  188. return SPI_STATUS_SUCCESS;
  189. }
  190. void spi_stop(void) {
  191. if (currentSlavePin != NO_PIN) {
  192. spiUnselect(&SPI_DRIVER);
  193. spiStop(&SPI_DRIVER);
  194. currentSlavePin = NO_PIN;
  195. }
  196. }