logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

is31fl3741-mono.c (9719B)


  1. /* Copyright 2017 Jason Williams
  2. * Copyright 2018 Jack Humbert
  3. * Copyright 2018 Yiancar
  4. * Copyright 2020 MelGeek
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "is31fl3741-mono.h"
  20. #include "i2c_master.h"
  21. #include "gpio.h"
  22. #include "wait.h"
  23. #define IS31FL3741_PWM_0_REGISTER_COUNT 180
  24. #define IS31FL3741_PWM_1_REGISTER_COUNT 171
  25. #define IS31FL3741_SCALING_0_REGISTER_COUNT 180
  26. #define IS31FL3741_SCALING_1_REGISTER_COUNT 171
  27. #ifndef IS31FL3741_I2C_TIMEOUT
  28. # define IS31FL3741_I2C_TIMEOUT 100
  29. #endif
  30. #ifndef IS31FL3741_I2C_PERSISTENCE
  31. # define IS31FL3741_I2C_PERSISTENCE 0
  32. #endif
  33. #ifndef IS31FL3741_CONFIGURATION
  34. # define IS31FL3741_CONFIGURATION 0x01
  35. #endif
  36. #ifndef IS31FL3741_PWM_FREQUENCY
  37. # define IS31FL3741_PWM_FREQUENCY IS31FL3741_PWM_FREQUENCY_29K_HZ
  38. #endif
  39. #ifndef IS31FL3741_SW_PULLUP
  40. # define IS31FL3741_SW_PULLUP IS31FL3741_PUR_32K_OHM
  41. #endif
  42. #ifndef IS31FL3741_CS_PULLDOWN
  43. # define IS31FL3741_CS_PULLDOWN IS31FL3741_PDR_32K_OHM
  44. #endif
  45. #ifndef IS31FL3741_GLOBAL_CURRENT
  46. # define IS31FL3741_GLOBAL_CURRENT 0xFF
  47. #endif
  48. const uint8_t i2c_addresses[IS31FL3741_DRIVER_COUNT] = {
  49. IS31FL3741_I2C_ADDRESS_1,
  50. #ifdef IS31FL3741_I2C_ADDRESS_2
  51. IS31FL3741_I2C_ADDRESS_2,
  52. # ifdef IS31FL3741_I2C_ADDRESS_3
  53. IS31FL3741_I2C_ADDRESS_3,
  54. # ifdef IS31FL3741_I2C_ADDRESS_4
  55. IS31FL3741_I2C_ADDRESS_4,
  56. # endif
  57. # endif
  58. #endif
  59. };
  60. // These buffers match the IS31FL3741 and IS31FL3741A PWM registers.
  61. // The scaling buffers match the page 2 and 3 LED On/Off registers.
  62. // Storing them like this is optimal for I2C transfers to the registers.
  63. // We could optimize this and take out the unused registers from these
  64. // buffers and the transfers in is31fl3741_write_pwm_buffer() but it's
  65. // probably not worth the extra complexity.
  66. typedef struct is31fl3741_driver_t {
  67. uint8_t pwm_buffer_0[IS31FL3741_PWM_0_REGISTER_COUNT];
  68. uint8_t pwm_buffer_1[IS31FL3741_PWM_1_REGISTER_COUNT];
  69. bool pwm_buffer_dirty;
  70. uint8_t scaling_buffer_0[IS31FL3741_SCALING_0_REGISTER_COUNT];
  71. uint8_t scaling_buffer_1[IS31FL3741_SCALING_1_REGISTER_COUNT];
  72. bool scaling_buffer_dirty;
  73. } PACKED is31fl3741_driver_t;
  74. is31fl3741_driver_t driver_buffers[IS31FL3741_DRIVER_COUNT] = {{
  75. .pwm_buffer_0 = {0},
  76. .pwm_buffer_1 = {0},
  77. .pwm_buffer_dirty = false,
  78. .scaling_buffer_0 = {0},
  79. .scaling_buffer_1 = {0},
  80. .scaling_buffer_dirty = false,
  81. }};
  82. void is31fl3741_write_register(uint8_t index, uint8_t reg, uint8_t data) {
  83. #if IS31FL3741_I2C_PERSISTENCE > 0
  84. for (uint8_t i = 0; i < IS31FL3741_I2C_PERSISTENCE; i++) {
  85. if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  86. }
  87. #else
  88. i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3741_I2C_TIMEOUT);
  89. #endif
  90. }
  91. void is31fl3741_select_page(uint8_t index, uint8_t page) {
  92. is31fl3741_write_register(index, IS31FL3741_REG_COMMAND_WRITE_LOCK, IS31FL3741_COMMAND_WRITE_LOCK_MAGIC);
  93. is31fl3741_write_register(index, IS31FL3741_REG_COMMAND, page);
  94. }
  95. void is31fl3741_write_pwm_buffer(uint8_t index) {
  96. is31fl3741_select_page(index, IS31FL3741_COMMAND_PWM_0);
  97. // Transmit PWM0 registers in 6 transfers of 30 bytes.
  98. // Iterate over the pwm_buffer_0 contents at 30 byte intervals.
  99. for (uint8_t i = 0; i < IS31FL3741_PWM_0_REGISTER_COUNT; i += 30) {
  100. #if IS31FL3741_I2C_PERSISTENCE > 0
  101. for (uint8_t j = 0; j < IS31FL3741_I2C_PERSISTENCE; j++) {
  102. if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer_0 + i, 30, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  103. }
  104. #else
  105. i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer_0 + i, 30, IS31FL3741_I2C_TIMEOUT);
  106. #endif
  107. }
  108. is31fl3741_select_page(index, IS31FL3741_COMMAND_PWM_1);
  109. // Transmit PWM1 registers in 9 transfers of 19 bytes.
  110. // Iterate over the pwm_buffer_1 contents at 19 byte intervals.
  111. for (uint8_t i = 0; i < IS31FL3741_PWM_1_REGISTER_COUNT; i += 19) {
  112. #if IS31FL3741_I2C_PERSISTENCE > 0
  113. for (uint8_t i = 0; i < IS31FL3741_I2C_PERSISTENCE; i++) {
  114. if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer_1 + i, 19, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  115. }
  116. #else
  117. i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer_1 + i, 19, IS31FL3741_I2C_TIMEOUT);
  118. #endif
  119. }
  120. }
  121. void is31fl3741_init_drivers(void) {
  122. i2c_init();
  123. #if defined(IS31FL3741_SDB_PIN)
  124. gpio_set_pin_output(IS31FL3741_SDB_PIN);
  125. gpio_write_pin_high(IS31FL3741_SDB_PIN);
  126. #endif
  127. for (uint8_t i = 0; i < IS31FL3741_DRIVER_COUNT; i++) {
  128. is31fl3741_init(i);
  129. }
  130. for (int i = 0; i < IS31FL3741_LED_COUNT; i++) {
  131. is31fl3741_set_led_control_register(i, true);
  132. }
  133. for (uint8_t i = 0; i < IS31FL3741_DRIVER_COUNT; i++) {
  134. is31fl3741_update_led_control_registers(i);
  135. }
  136. }
  137. void is31fl3741_init(uint8_t index) {
  138. // In order to avoid the LEDs being driven with garbage data
  139. // in the LED driver's PWM registers, shutdown is enabled last.
  140. // Set up the mode and other settings, clear the PWM registers,
  141. // then disable software shutdown.
  142. // Unlock the command register.
  143. is31fl3741_select_page(index, IS31FL3741_COMMAND_FUNCTION);
  144. // Set to Normal operation
  145. is31fl3741_write_register(index, IS31FL3741_FUNCTION_REG_CONFIGURATION, IS31FL3741_CONFIGURATION);
  146. // Set Golbal Current Control Register
  147. is31fl3741_write_register(index, IS31FL3741_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3741_GLOBAL_CURRENT);
  148. // Set Pull up & Down for SWx CSy
  149. is31fl3741_write_register(index, IS31FL3741_FUNCTION_REG_PULLDOWNUP, ((IS31FL3741_CS_PULLDOWN << 4) | IS31FL3741_SW_PULLUP));
  150. // Set PWM frequency
  151. is31fl3741_write_register(index, IS31FL3741_FUNCTION_REG_PWM_FREQUENCY, (IS31FL3741_PWM_FREQUENCY & 0b1111));
  152. // is31fl3741_update_led_scaling_registers(index, 0xFF, 0xFF, 0xFF);
  153. // Wait 10ms to ensure the device has woken up.
  154. wait_ms(10);
  155. }
  156. uint8_t get_pwm_value(uint8_t driver, uint16_t reg) {
  157. if (reg & 0x100) {
  158. return driver_buffers[driver].pwm_buffer_1[reg & 0xFF];
  159. } else {
  160. return driver_buffers[driver].pwm_buffer_0[reg];
  161. }
  162. }
  163. void set_pwm_value(uint8_t driver, uint16_t reg, uint8_t value) {
  164. if (reg & 0x100) {
  165. driver_buffers[driver].pwm_buffer_1[reg & 0xFF] = value;
  166. } else {
  167. driver_buffers[driver].pwm_buffer_0[reg] = value;
  168. }
  169. }
  170. void is31fl3741_set_value(int index, uint8_t value) {
  171. is31fl3741_led_t led;
  172. if (index >= 0 && index < IS31FL3741_LED_COUNT) {
  173. memcpy_P(&led, (&g_is31fl3741_leds[index]), sizeof(led));
  174. if (get_pwm_value(led.driver, led.v) == value) {
  175. return;
  176. }
  177. set_pwm_value(led.driver, led.v, value);
  178. driver_buffers[led.driver].pwm_buffer_dirty = true;
  179. }
  180. }
  181. void is31fl3741_set_value_all(uint8_t value) {
  182. for (int i = 0; i < IS31FL3741_LED_COUNT; i++) {
  183. is31fl3741_set_value(i, value);
  184. }
  185. }
  186. void set_scaling_value(uint8_t driver, uint16_t reg, uint8_t value) {
  187. if (reg & 0x100) {
  188. driver_buffers[driver].scaling_buffer_1[reg & 0xFF] = value;
  189. } else {
  190. driver_buffers[driver].scaling_buffer_0[reg] = value;
  191. }
  192. }
  193. void is31fl3741_set_led_control_register(uint8_t index, bool value) {
  194. is31fl3741_led_t led;
  195. memcpy_P(&led, (&g_is31fl3741_leds[index]), sizeof(led));
  196. set_scaling_value(led.driver, led.v, value ? 0xFF : 0x00);
  197. driver_buffers[led.driver].scaling_buffer_dirty = true;
  198. }
  199. void is31fl3741_update_pwm_buffers(uint8_t index) {
  200. if (driver_buffers[index].pwm_buffer_dirty) {
  201. is31fl3741_write_pwm_buffer(index);
  202. driver_buffers[index].pwm_buffer_dirty = false;
  203. }
  204. }
  205. void is31fl3741_set_pwm_buffer(const is31fl3741_led_t *pled, uint8_t value) {
  206. set_pwm_value(pled->driver, pled->v, value);
  207. driver_buffers[pled->driver].pwm_buffer_dirty = true;
  208. }
  209. void is31fl3741_update_led_control_registers(uint8_t index) {
  210. if (driver_buffers[index].scaling_buffer_dirty) {
  211. is31fl3741_select_page(index, IS31FL3741_COMMAND_SCALING_0);
  212. for (uint8_t i = 0; i < IS31FL3741_SCALING_0_REGISTER_COUNT; i++) {
  213. is31fl3741_write_register(index, i, driver_buffers[index].scaling_buffer_0[i]);
  214. }
  215. is31fl3741_select_page(index, IS31FL3741_COMMAND_SCALING_1);
  216. for (uint8_t i = 0; i < IS31FL3741_SCALING_1_REGISTER_COUNT; i++) {
  217. is31fl3741_write_register(index, i, driver_buffers[index].scaling_buffer_1[i]);
  218. }
  219. driver_buffers[index].scaling_buffer_dirty = false;
  220. }
  221. }
  222. void is31fl3741_set_scaling_registers(const is31fl3741_led_t *pled, uint8_t value) {
  223. set_scaling_value(pled->driver, pled->v, value);
  224. driver_buffers[pled->driver].scaling_buffer_dirty = true;
  225. }
  226. void is31fl3741_flush(void) {
  227. for (uint8_t i = 0; i < IS31FL3741_DRIVER_COUNT; i++) {
  228. is31fl3741_update_pwm_buffers(i);
  229. }
  230. }