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.

284 lines
10 KiB

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