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.

260 lines
6.9 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. #ifndef WB32F3G71xx
  49. uint16_t roundedDivisor = 2;
  50. while (roundedDivisor < divisor) {
  51. roundedDivisor <<= 1;
  52. }
  53. if (roundedDivisor < 2 || roundedDivisor > 256) {
  54. return false;
  55. }
  56. #endif
  57. #if defined(K20x) || defined(KL2x)
  58. spiConfig.tar0 = SPIx_CTARn_FMSZ(7) | SPIx_CTARn_ASC(1);
  59. if (lsbFirst) {
  60. spiConfig.tar0 |= SPIx_CTARn_LSBFE;
  61. }
  62. switch (mode) {
  63. case 0:
  64. break;
  65. case 1:
  66. spiConfig.tar0 |= SPIx_CTARn_CPHA;
  67. break;
  68. case 2:
  69. spiConfig.tar0 |= SPIx_CTARn_CPOL;
  70. break;
  71. case 3:
  72. spiConfig.tar0 |= SPIx_CTARn_CPHA | SPIx_CTARn_CPOL;
  73. break;
  74. }
  75. switch (roundedDivisor) {
  76. case 2:
  77. spiConfig.tar0 |= SPIx_CTARn_BR(0);
  78. break;
  79. case 4:
  80. spiConfig.tar0 |= SPIx_CTARn_BR(1);
  81. break;
  82. case 8:
  83. spiConfig.tar0 |= SPIx_CTARn_BR(3);
  84. break;
  85. case 16:
  86. spiConfig.tar0 |= SPIx_CTARn_BR(4);
  87. break;
  88. case 32:
  89. spiConfig.tar0 |= SPIx_CTARn_BR(5);
  90. break;
  91. case 64:
  92. spiConfig.tar0 |= SPIx_CTARn_BR(6);
  93. break;
  94. case 128:
  95. spiConfig.tar0 |= SPIx_CTARn_BR(7);
  96. break;
  97. case 256:
  98. spiConfig.tar0 |= SPIx_CTARn_BR(8);
  99. break;
  100. }
  101. #elif defined(HT32)
  102. spiConfig.cr0 = SPI_CR0_SELOEN;
  103. spiConfig.cr1 = SPI_CR1_MODE | 8; // 8 bits and in master mode
  104. if (lsbFirst) {
  105. spiConfig.cr1 |= SPI_CR1_FIRSTBIT;
  106. }
  107. switch (mode) {
  108. case 0:
  109. spiConfig.cr1 |= SPI_CR1_FORMAT_MODE0;
  110. break;
  111. case 1:
  112. spiConfig.cr1 |= SPI_CR1_FORMAT_MODE1;
  113. break;
  114. case 2:
  115. spiConfig.cr1 |= SPI_CR1_FORMAT_MODE2;
  116. break;
  117. case 3:
  118. spiConfig.cr1 |= SPI_CR1_FORMAT_MODE3;
  119. break;
  120. }
  121. spiConfig.cpr = (roundedDivisor - 1) >> 1;
  122. #elif defined(WB32F3G71xx)
  123. if (!lsbFirst) {
  124. osalDbgAssert(lsbFirst != FALSE, "unsupported lsbFirst");
  125. }
  126. if (divisor < 1) {
  127. return false;
  128. }
  129. spiConfig.SPI_BaudRatePrescaler = (divisor << 2);
  130. switch (mode) {
  131. case 0:
  132. spiConfig.SPI_CPHA = SPI_CPHA_1Edge;
  133. spiConfig.SPI_CPOL = SPI_CPOL_Low;
  134. break;
  135. case 1:
  136. spiConfig.SPI_CPHA = SPI_CPHA_2Edge;
  137. spiConfig.SPI_CPOL = SPI_CPOL_Low;
  138. break;
  139. case 2:
  140. spiConfig.SPI_CPHA = SPI_CPHA_1Edge;
  141. spiConfig.SPI_CPOL = SPI_CPOL_High;
  142. break;
  143. case 3:
  144. spiConfig.SPI_CPHA = SPI_CPHA_2Edge;
  145. spiConfig.SPI_CPOL = SPI_CPOL_High;
  146. break;
  147. }
  148. #else
  149. spiConfig.cr1 = 0;
  150. if (lsbFirst) {
  151. spiConfig.cr1 |= SPI_CR1_LSBFIRST;
  152. }
  153. switch (mode) {
  154. case 0:
  155. break;
  156. case 1:
  157. spiConfig.cr1 |= SPI_CR1_CPHA;
  158. break;
  159. case 2:
  160. spiConfig.cr1 |= SPI_CR1_CPOL;
  161. break;
  162. case 3:
  163. spiConfig.cr1 |= SPI_CR1_CPHA | SPI_CR1_CPOL;
  164. break;
  165. }
  166. switch (roundedDivisor) {
  167. case 2:
  168. break;
  169. case 4:
  170. spiConfig.cr1 |= SPI_CR1_BR_0;
  171. break;
  172. case 8:
  173. spiConfig.cr1 |= SPI_CR1_BR_1;
  174. break;
  175. case 16:
  176. spiConfig.cr1 |= SPI_CR1_BR_1 | SPI_CR1_BR_0;
  177. break;
  178. case 32:
  179. spiConfig.cr1 |= SPI_CR1_BR_2;
  180. break;
  181. case 64:
  182. spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_0;
  183. break;
  184. case 128:
  185. spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_1;
  186. break;
  187. case 256:
  188. spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0;
  189. break;
  190. }
  191. #endif
  192. currentSlavePin = slavePin;
  193. spiConfig.ssport = PAL_PORT(slavePin);
  194. spiConfig.sspad = PAL_PAD(slavePin);
  195. setPinOutput(slavePin);
  196. spiStart(&SPI_DRIVER, &spiConfig);
  197. spiSelect(&SPI_DRIVER);
  198. return true;
  199. }
  200. spi_status_t spi_write(uint8_t data) {
  201. uint8_t rxData;
  202. spiExchange(&SPI_DRIVER, 1, &data, &rxData);
  203. return rxData;
  204. }
  205. spi_status_t spi_read(void) {
  206. uint8_t data = 0;
  207. spiReceive(&SPI_DRIVER, 1, &data);
  208. return data;
  209. }
  210. spi_status_t spi_transmit(const uint8_t *data, uint16_t length) {
  211. spiSend(&SPI_DRIVER, length, data);
  212. return SPI_STATUS_SUCCESS;
  213. }
  214. spi_status_t spi_receive(uint8_t *data, uint16_t length) {
  215. spiReceive(&SPI_DRIVER, length, data);
  216. return SPI_STATUS_SUCCESS;
  217. }
  218. void spi_stop(void) {
  219. if (currentSlavePin != NO_PIN) {
  220. spiUnselect(&SPI_DRIVER);
  221. spiStop(&SPI_DRIVER);
  222. currentSlavePin = NO_PIN;
  223. }
  224. }