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.

208 lines
7.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 2018 Jack Humbert
  2. * Copyright 2018 Yiancar
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /* This library is only valid for STM32 processors.
  18. * This library follows the convention of the AVR i2c_master library.
  19. * As a result addresses are expected to be already shifted (addr << 1).
  20. * I2CD1 is the default driver which corresponds to pins B6 and B7. This
  21. * can be changed.
  22. * Please ensure that HAL_USE_I2C is TRUE in the halconf.h file and that
  23. * STM32_I2C_USE_I2C1 is TRUE in the mcuconf.h file. Pins B6 and B7 are used
  24. * but using any other I2C pins should be trivial.
  25. */
  26. #include "quantum.h"
  27. #include "i2c_master.h"
  28. #include <string.h>
  29. #include <ch.h>
  30. #include <hal.h>
  31. #ifndef I2C1_SCL_PIN
  32. # define I2C1_SCL_PIN B6
  33. #endif
  34. #ifndef I2C1_SDA_PIN
  35. # define I2C1_SDA_PIN B7
  36. #endif
  37. #ifdef USE_I2CV1
  38. # ifndef I2C1_OPMODE
  39. # define I2C1_OPMODE OPMODE_I2C
  40. # endif
  41. # ifndef I2C1_CLOCK_SPEED
  42. # define I2C1_CLOCK_SPEED 100000 /* 400000 */
  43. # endif
  44. # ifndef I2C1_DUTY_CYCLE
  45. # define I2C1_DUTY_CYCLE STD_DUTY_CYCLE /* FAST_DUTY_CYCLE_2 */
  46. # endif
  47. #else
  48. // The default timing values below configures the I2C clock to 400khz assuming a 72Mhz clock
  49. // For more info : https://www.st.com/en/embedded-software/stsw-stm32126.html
  50. # ifndef I2C1_TIMINGR_PRESC
  51. # define I2C1_TIMINGR_PRESC 0U
  52. # endif
  53. # ifndef I2C1_TIMINGR_SCLDEL
  54. # define I2C1_TIMINGR_SCLDEL 7U
  55. # endif
  56. # ifndef I2C1_TIMINGR_SDADEL
  57. # define I2C1_TIMINGR_SDADEL 0U
  58. # endif
  59. # ifndef I2C1_TIMINGR_SCLH
  60. # define I2C1_TIMINGR_SCLH 38U
  61. # endif
  62. # ifndef I2C1_TIMINGR_SCLL
  63. # define I2C1_TIMINGR_SCLL 129U
  64. # endif
  65. #endif
  66. #ifndef I2C_DRIVER
  67. # define I2C_DRIVER I2CD1
  68. #endif
  69. #ifdef USE_GPIOV1
  70. # ifndef I2C1_SCL_PAL_MODE
  71. # define I2C1_SCL_PAL_MODE PAL_MODE_ALTERNATE_OPENDRAIN
  72. # endif
  73. # ifndef I2C1_SDA_PAL_MODE
  74. # define I2C1_SDA_PAL_MODE PAL_MODE_ALTERNATE_OPENDRAIN
  75. # endif
  76. #else
  77. // The default PAL alternate modes are used to signal that the pins are used for I2C
  78. # ifndef I2C1_SCL_PAL_MODE
  79. # define I2C1_SCL_PAL_MODE 4
  80. # endif
  81. # ifndef I2C1_SDA_PAL_MODE
  82. # define I2C1_SDA_PAL_MODE 4
  83. # endif
  84. #endif
  85. static uint8_t i2c_address;
  86. static const I2CConfig i2cconfig = {
  87. #if defined(USE_I2CV1_CONTRIB)
  88. I2C1_CLOCK_SPEED,
  89. #elif defined(USE_I2CV1)
  90. I2C1_OPMODE,
  91. I2C1_CLOCK_SPEED,
  92. I2C1_DUTY_CYCLE,
  93. #elif defined(WB32F3G71xx)
  94. I2C1_OPMODE,
  95. I2C1_CLOCK_SPEED,
  96. #else
  97. // This configures the I2C clock to 400khz assuming a 72Mhz clock
  98. // For more info : https://www.st.com/en/embedded-software/stsw-stm32126.html
  99. STM32_TIMINGR_PRESC(I2C1_TIMINGR_PRESC) | STM32_TIMINGR_SCLDEL(I2C1_TIMINGR_SCLDEL) | STM32_TIMINGR_SDADEL(I2C1_TIMINGR_SDADEL) | STM32_TIMINGR_SCLH(I2C1_TIMINGR_SCLH) | STM32_TIMINGR_SCLL(I2C1_TIMINGR_SCLL), 0, 0
  100. #endif
  101. };
  102. static i2c_status_t chibios_to_qmk(const msg_t* status) {
  103. switch (*status) {
  104. case I2C_NO_ERROR:
  105. return I2C_STATUS_SUCCESS;
  106. case I2C_TIMEOUT:
  107. return I2C_STATUS_TIMEOUT;
  108. // I2C_BUS_ERROR, I2C_ARBITRATION_LOST, I2C_ACK_FAILURE, I2C_OVERRUN, I2C_PEC_ERROR, I2C_SMB_ALERT
  109. default:
  110. return I2C_STATUS_ERROR;
  111. }
  112. }
  113. __attribute__((weak)) void i2c_init(void) {
  114. static bool is_initialised = false;
  115. if (!is_initialised) {
  116. is_initialised = true;
  117. // Try releasing special pins for a short time
  118. palSetLineMode(I2C1_SCL_PIN, PAL_MODE_INPUT);
  119. palSetLineMode(I2C1_SDA_PIN, PAL_MODE_INPUT);
  120. chThdSleepMilliseconds(10);
  121. #if defined(USE_GPIOV1)
  122. palSetLineMode(I2C1_SCL_PIN, I2C1_SCL_PAL_MODE);
  123. palSetLineMode(I2C1_SDA_PIN, I2C1_SDA_PAL_MODE);
  124. #else
  125. palSetLineMode(I2C1_SCL_PIN, PAL_MODE_ALTERNATE(I2C1_SCL_PAL_MODE) | PAL_OUTPUT_TYPE_OPENDRAIN);
  126. palSetLineMode(I2C1_SDA_PIN, PAL_MODE_ALTERNATE(I2C1_SDA_PAL_MODE) | PAL_OUTPUT_TYPE_OPENDRAIN);
  127. #endif
  128. }
  129. }
  130. i2c_status_t i2c_start(uint8_t address) {
  131. i2c_address = address;
  132. i2cStart(&I2C_DRIVER, &i2cconfig);
  133. return I2C_STATUS_SUCCESS;
  134. }
  135. i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout) {
  136. i2c_address = address;
  137. i2cStart(&I2C_DRIVER, &i2cconfig);
  138. msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), data, length, 0, 0, TIME_MS2I(timeout));
  139. return chibios_to_qmk(&status);
  140. }
  141. i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout) {
  142. i2c_address = address;
  143. i2cStart(&I2C_DRIVER, &i2cconfig);
  144. msg_t status = i2cMasterReceiveTimeout(&I2C_DRIVER, (i2c_address >> 1), data, length, TIME_MS2I(timeout));
  145. return chibios_to_qmk(&status);
  146. }
  147. i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout) {
  148. i2c_address = devaddr;
  149. i2cStart(&I2C_DRIVER, &i2cconfig);
  150. uint8_t complete_packet[length + 1];
  151. for (uint16_t i = 0; i < length; i++) {
  152. complete_packet[i + 1] = data[i];
  153. }
  154. complete_packet[0] = regaddr;
  155. msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), complete_packet, length + 1, 0, 0, TIME_MS2I(timeout));
  156. return chibios_to_qmk(&status);
  157. }
  158. i2c_status_t i2c_writeReg16(uint8_t devaddr, uint16_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout) {
  159. i2c_address = devaddr;
  160. i2cStart(&I2C_DRIVER, &i2cconfig);
  161. uint8_t complete_packet[length + 2];
  162. for (uint16_t i = 0; i < length; i++) {
  163. complete_packet[i + 2] = data[i];
  164. }
  165. complete_packet[0] = regaddr >> 8;
  166. complete_packet[1] = regaddr & 0xFF;
  167. msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), complete_packet, length + 2, 0, 0, TIME_MS2I(timeout));
  168. return chibios_to_qmk(&status);
  169. }
  170. i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) {
  171. i2c_address = devaddr;
  172. i2cStart(&I2C_DRIVER, &i2cconfig);
  173. msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), &regaddr, 1, data, length, TIME_MS2I(timeout));
  174. return chibios_to_qmk(&status);
  175. }
  176. i2c_status_t i2c_readReg16(uint8_t devaddr, uint16_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) {
  177. i2c_address = devaddr;
  178. i2cStart(&I2C_DRIVER, &i2cconfig);
  179. uint8_t register_packet[2] = {regaddr >> 8, regaddr & 0xFF};
  180. msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), register_packet, 2, data, length, TIME_MS2I(timeout));
  181. return chibios_to_qmk(&status);
  182. }
  183. void i2c_stop(void) {
  184. i2cStop(&I2C_DRIVER);
  185. }