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.

240 lines
8.1 KiB

  1. /* Copyright 2018 Jason Williams (Wilba)
  2. * Copyright 2021 Doni Crosby
  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. #include "is31fl3736-mono.h"
  18. #include "i2c_master.h"
  19. #include "gpio.h"
  20. #include "wait.h"
  21. #define IS31FL3736_PWM_REGISTER_COUNT 192 // actually 96
  22. #define IS31FL3736_LED_CONTROL_REGISTER_COUNT 24
  23. #ifndef IS31FL3736_I2C_TIMEOUT
  24. # define IS31FL3736_I2C_TIMEOUT 100
  25. #endif
  26. #ifndef IS31FL3736_I2C_PERSISTENCE
  27. # define IS31FL3736_I2C_PERSISTENCE 0
  28. #endif
  29. #ifndef IS31FL3736_PWM_FREQUENCY
  30. # define IS31FL3736_PWM_FREQUENCY IS31FL3736_PWM_FREQUENCY_8K4_HZ // PFS - IS31FL3736B only
  31. #endif
  32. #ifndef IS31FL3736_SW_PULLUP
  33. # define IS31FL3736_SW_PULLUP IS31FL3736_PUR_0_OHM
  34. #endif
  35. #ifndef IS31FL3736_CS_PULLDOWN
  36. # define IS31FL3736_CS_PULLDOWN IS31FL3736_PDR_0_OHM
  37. #endif
  38. #ifndef IS31FL3736_GLOBAL_CURRENT
  39. # define IS31FL3736_GLOBAL_CURRENT 0xFF
  40. #endif
  41. const uint8_t i2c_addresses[IS31FL3736_DRIVER_COUNT] = {
  42. IS31FL3736_I2C_ADDRESS_1,
  43. #ifdef IS31FL3736_I2C_ADDRESS_2
  44. IS31FL3736_I2C_ADDRESS_2,
  45. # ifdef IS31FL3736_I2C_ADDRESS_3
  46. IS31FL3736_I2C_ADDRESS_3,
  47. # ifdef IS31FL3736_I2C_ADDRESS_4
  48. IS31FL3736_I2C_ADDRESS_4,
  49. # endif
  50. # endif
  51. #endif
  52. };
  53. // These buffers match the IS31FL3736 PWM registers.
  54. // The control buffers match the page 0 LED On/Off registers.
  55. // Storing them like this is optimal for I2C transfers to the registers.
  56. // We could optimize this and take out the unused registers from these
  57. // buffers and the transfers in is31fl3736_write_pwm_buffer() but it's
  58. // probably not worth the extra complexity.
  59. typedef struct is31fl3736_driver_t {
  60. uint8_t pwm_buffer[IS31FL3736_PWM_REGISTER_COUNT];
  61. bool pwm_buffer_dirty;
  62. uint8_t led_control_buffer[IS31FL3736_LED_CONTROL_REGISTER_COUNT];
  63. bool led_control_buffer_dirty;
  64. } PACKED is31fl3736_driver_t;
  65. is31fl3736_driver_t driver_buffers[IS31FL3736_DRIVER_COUNT] = {{
  66. .pwm_buffer = {0},
  67. .pwm_buffer_dirty = false,
  68. .led_control_buffer = {0},
  69. .led_control_buffer_dirty = false,
  70. }};
  71. void is31fl3736_write_register(uint8_t index, uint8_t reg, uint8_t data) {
  72. #if IS31FL3736_I2C_PERSISTENCE > 0
  73. for (uint8_t i = 0; i < IS31FL3736_I2C_PERSISTENCE; i++) {
  74. if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3736_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  75. }
  76. #else
  77. i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3736_I2C_TIMEOUT);
  78. #endif
  79. }
  80. void is31fl3736_select_page(uint8_t index, uint8_t page) {
  81. is31fl3736_write_register(index, IS31FL3736_REG_COMMAND_WRITE_LOCK, IS31FL3736_COMMAND_WRITE_LOCK_MAGIC);
  82. is31fl3736_write_register(index, IS31FL3736_REG_COMMAND, page);
  83. }
  84. void is31fl3736_write_pwm_buffer(uint8_t index) {
  85. // Assumes page 1 is already selected.
  86. // Transmit PWM registers in 12 transfers of 16 bytes.
  87. // Iterate over the pwm_buffer contents at 16 byte intervals.
  88. for (uint8_t i = 0; i < IS31FL3736_PWM_REGISTER_COUNT; i += 16) {
  89. #if IS31FL3736_I2C_PERSISTENCE > 0
  90. for (uint8_t j = 0; j < IS31FL3736_I2C_PERSISTENCE; j++) {
  91. if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3736_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
  92. }
  93. #else
  94. i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3736_I2C_TIMEOUT);
  95. #endif
  96. }
  97. }
  98. void is31fl3736_init_drivers(void) {
  99. i2c_init();
  100. #if defined(IS31FL3736_SDB_PIN)
  101. gpio_set_pin_output(IS31FL3736_SDB_PIN);
  102. gpio_write_pin_high(IS31FL3736_SDB_PIN);
  103. #endif
  104. for (uint8_t i = 0; i < IS31FL3736_DRIVER_COUNT; i++) {
  105. is31fl3736_init(i);
  106. }
  107. for (int i = 0; i < IS31FL3736_LED_COUNT; i++) {
  108. is31fl3736_set_led_control_register(i, true);
  109. }
  110. for (uint8_t i = 0; i < IS31FL3736_DRIVER_COUNT; i++) {
  111. is31fl3736_update_led_control_registers(i);
  112. }
  113. }
  114. void is31fl3736_init(uint8_t index) {
  115. // In order to avoid the LEDs being driven with garbage data
  116. // in the LED driver's PWM registers, shutdown is enabled last.
  117. // Set up the mode and other settings, clear the PWM registers,
  118. // then disable software shutdown.
  119. is31fl3736_select_page(index, IS31FL3736_COMMAND_LED_CONTROL);
  120. // Turn off all LEDs.
  121. for (uint8_t i = 0; i < IS31FL3736_LED_CONTROL_REGISTER_COUNT; i++) {
  122. is31fl3736_write_register(index, i, 0x00);
  123. }
  124. is31fl3736_select_page(index, IS31FL3736_COMMAND_PWM);
  125. // Set PWM on all LEDs to 0
  126. // No need to setup Breath registers to PWM as that is the default.
  127. for (uint8_t i = 0; i < IS31FL3736_PWM_REGISTER_COUNT; i++) {
  128. is31fl3736_write_register(index, i, 0x00);
  129. }
  130. is31fl3736_select_page(index, IS31FL3736_COMMAND_FUNCTION);
  131. // Set de-ghost pull-up resistors (SWx)
  132. is31fl3736_write_register(index, IS31FL3736_FUNCTION_REG_SW_PULLUP, IS31FL3736_SW_PULLUP);
  133. // Set de-ghost pull-down resistors (CSx)
  134. is31fl3736_write_register(index, IS31FL3736_FUNCTION_REG_CS_PULLDOWN, IS31FL3736_CS_PULLDOWN);
  135. // Set global current to maximum.
  136. is31fl3736_write_register(index, IS31FL3736_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3736_GLOBAL_CURRENT);
  137. // Disable software shutdown.
  138. is31fl3736_write_register(index, IS31FL3736_FUNCTION_REG_CONFIGURATION, ((IS31FL3736_PWM_FREQUENCY & 0b111) << 3) | 0x01);
  139. // Wait 10ms to ensure the device has woken up.
  140. wait_ms(10);
  141. }
  142. void is31fl3736_set_value(int index, uint8_t value) {
  143. is31fl3736_led_t led;
  144. if (index >= 0 && index < IS31FL3736_LED_COUNT) {
  145. memcpy_P(&led, (&g_is31fl3736_leds[index]), sizeof(led));
  146. if (driver_buffers[led.driver].pwm_buffer[led.v] == value) {
  147. return;
  148. }
  149. driver_buffers[led.driver].pwm_buffer[led.v] = value;
  150. driver_buffers[led.driver].pwm_buffer_dirty = true;
  151. }
  152. }
  153. void is31fl3736_set_value_all(uint8_t value) {
  154. for (int i = 0; i < IS31FL3736_LED_COUNT; i++) {
  155. is31fl3736_set_value(i, value);
  156. }
  157. }
  158. void is31fl3736_set_led_control_register(uint8_t index, bool value) {
  159. is31fl3736_led_t led;
  160. memcpy_P(&led, (&g_is31fl3736_leds[index]), sizeof(led));
  161. // The PWM register for a matrix position (0x00 to 0xBF) is interleaved, so:
  162. // A1=0x00 A2=0x02 A3=0x04 A4=0x06 A5=0x08 A6=0x0A A7=0x0C A8=0x0E
  163. // B1=0x10 B2=0x12 B3=0x14
  164. // But also, the LED control registers (0x00 to 0x17) are also interleaved, so:
  165. // A1-A4=0x00 A5-A8=0x01
  166. uint8_t control_register = led.v / 8;
  167. uint8_t bit_value = led.v % 8;
  168. if (value) {
  169. driver_buffers[led.driver].led_control_buffer[control_register] |= (1 << bit_value);
  170. } else {
  171. driver_buffers[led.driver].led_control_buffer[control_register] &= ~(1 << bit_value);
  172. }
  173. driver_buffers[led.driver].led_control_buffer_dirty = true;
  174. }
  175. void is31fl3736_update_pwm_buffers(uint8_t index) {
  176. if (driver_buffers[index].pwm_buffer_dirty) {
  177. is31fl3736_select_page(index, IS31FL3736_COMMAND_PWM);
  178. is31fl3736_write_pwm_buffer(index);
  179. driver_buffers[index].pwm_buffer_dirty = false;
  180. }
  181. }
  182. void is31fl3736_update_led_control_registers(uint8_t index) {
  183. if (driver_buffers[index].led_control_buffer_dirty) {
  184. is31fl3736_select_page(index, IS31FL3736_COMMAND_LED_CONTROL);
  185. for (uint8_t i = 0; i < IS31FL3736_LED_CONTROL_REGISTER_COUNT; i++) {
  186. is31fl3736_write_register(index, i, driver_buffers[index].led_control_buffer[i]);
  187. }
  188. driver_buffers[index].led_control_buffer_dirty = false;
  189. }
  190. }
  191. void is31fl3736_flush(void) {
  192. for (uint8_t i = 0; i < IS31FL3736_DRIVER_COUNT; i++) {
  193. is31fl3736_update_pwm_buffers(i);
  194. }
  195. }