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.

242 lines
8.3 KiB

5 years ago
  1. /* Copyright 2017 Jason Williams
  2. * Copyright 2018 Jack Humbert
  3. * Copyright 2019 Clueboard
  4. * Copyright 2021 Doni Crosby
  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 "is31fl3731-simple.h"
  20. #include "i2c_master.h"
  21. #include "wait.h"
  22. // This is a 7-bit address, that gets left-shifted and bit 0
  23. // set to 0 for write, 1 for read (as per I2C protocol)
  24. // The address will vary depending on your wiring:
  25. // 0b1110100 AD <-> GND
  26. // 0b1110111 AD <-> VCC
  27. // 0b1110101 AD <-> SCL
  28. // 0b1110110 AD <-> SDA
  29. #define ISSI_ADDR_DEFAULT 0x74
  30. #define ISSI_REG_CONFIG 0x00
  31. #define ISSI_REG_CONFIG_PICTUREMODE 0x00
  32. #define ISSI_REG_CONFIG_AUTOPLAYMODE 0x08
  33. #define ISSI_REG_CONFIG_AUDIOPLAYMODE 0x18
  34. #define ISSI_CONF_PICTUREMODE 0x00
  35. #define ISSI_CONF_AUTOFRAMEMODE 0x04
  36. #define ISSI_CONF_AUDIOMODE 0x08
  37. #define ISSI_REG_PICTUREFRAME 0x01
  38. // Not defined in the datasheet -- See AN for IC
  39. #define ISSI_REG_GHOST_IMAGE_PREVENTION 0xC2 // Set bit 4 to enable de-ghosting
  40. #define ISSI_REG_SHUTDOWN 0x0A
  41. #define ISSI_REG_AUDIOSYNC 0x06
  42. #define ISSI_COMMANDREGISTER 0xFD
  43. #define ISSI_BANK_FUNCTIONREG 0x0B // helpfully called 'page nine'
  44. #ifndef ISSI_TIMEOUT
  45. # define ISSI_TIMEOUT 100
  46. #endif
  47. #ifndef ISSI_PERSISTENCE
  48. # define ISSI_PERSISTENCE 0
  49. #endif
  50. // Transfer buffer for TWITransmitData()
  51. uint8_t g_twi_transfer_buffer[20];
  52. // These buffers match the IS31FL3731 PWM registers 0x24-0xB3.
  53. // Storing them like this is optimal for I2C transfers to the registers.
  54. // We could optimize this and take out the unused registers from these
  55. // buffers and the transfers in IS31FL3731_write_pwm_buffer() but it's
  56. // probably not worth the extra complexity.
  57. uint8_t g_pwm_buffer[LED_DRIVER_COUNT][144];
  58. bool g_pwm_buffer_update_required[LED_DRIVER_COUNT] = {false};
  59. /* There's probably a better way to init this... */
  60. #if LED_DRIVER_COUNT == 1
  61. uint8_t g_led_control_registers[LED_DRIVER_COUNT][18] = {{0}};
  62. #elif LED_DRIVER_COUNT == 2
  63. uint8_t g_led_control_registers[LED_DRIVER_COUNT][18] = {{0}, {0}};
  64. #elif LED_DRIVER_COUNT == 3
  65. uint8_t g_led_control_registers[LED_DRIVER_COUNT][18] = {{0}, {0}, {0}};
  66. #elif LED_DRIVER_COUNT == 4
  67. uint8_t g_led_control_registers[LED_DRIVER_COUNT][18] = {{0}, {0}, {0}, {0}};
  68. #endif
  69. bool g_led_control_registers_update_required[LED_DRIVER_COUNT] = {false};
  70. // This is the bit pattern in the LED control registers
  71. // (for matrix A, add one to register for matrix B)
  72. //
  73. // reg - b7 b6 b5 b4 b3 b2 b1 b0
  74. // 0x00 - R08,R07,R06,R05,R04,R03,R02,R01
  75. // 0x02 - G08,G07,G06,G05,G04,G03,G02,R00
  76. // 0x04 - B08,B07,B06,B05,B04,B03,G01,G00
  77. // 0x06 - - , - , - , - , - ,B02,B01,B00
  78. // 0x08 - - , - , - , - , - , - , - , -
  79. // 0x0A - B17,B16,B15, - , - , - , - , -
  80. // 0x0C - G17,G16,B14,B13,B12,B11,B10,B09
  81. // 0x0E - R17,G15,G14,G13,G12,G11,G10,G09
  82. // 0x10 - R16,R15,R14,R13,R12,R11,R10,R09
  83. void IS31FL3731_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
  84. g_twi_transfer_buffer[0] = reg;
  85. g_twi_transfer_buffer[1] = data;
  86. #if ISSI_PERSISTENCE > 0
  87. for (uint8_t i = 0; i < ISSI_PERSISTENCE; i++) {
  88. if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, ISSI_TIMEOUT) == 0) {
  89. break;
  90. }
  91. }
  92. #else
  93. i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, ISSI_TIMEOUT);
  94. #endif
  95. }
  96. void IS31FL3731_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) {
  97. // assumes bank is already selected
  98. // transmit PWM registers in 9 transfers of 16 bytes
  99. // g_twi_transfer_buffer[] is 20 bytes
  100. // iterate over the pwm_buffer contents at 16 byte intervals
  101. for (int i = 0; i < 144; i += 16) {
  102. // set the first register, e.g. 0x24, 0x34, 0x44, etc.
  103. g_twi_transfer_buffer[0] = 0x24 + i;
  104. // copy the data from i to i+15
  105. // device will auto-increment register for data after the first byte
  106. // thus this sets registers 0x24-0x33, 0x34-0x43, etc. in one transfer
  107. for (int j = 0; j < 16; j++) {
  108. g_twi_transfer_buffer[1 + j] = pwm_buffer[i + j];
  109. }
  110. #if ISSI_PERSISTENCE > 0
  111. for (uint8_t i = 0; i < ISSI_PERSISTENCE; i++) {
  112. if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, ISSI_TIMEOUT) == 0) break;
  113. }
  114. #else
  115. i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, ISSI_TIMEOUT);
  116. #endif
  117. }
  118. }
  119. void IS31FL3731_init(uint8_t addr) {
  120. // In order to avoid the LEDs being driven with garbage data
  121. // in the LED driver's PWM registers, first enable software shutdown,
  122. // then set up the mode and other settings, clear the PWM registers,
  123. // then disable software shutdown.
  124. // select "function register" bank
  125. IS31FL3731_write_register(addr, ISSI_COMMANDREGISTER, ISSI_BANK_FUNCTIONREG);
  126. // enable software shutdown
  127. IS31FL3731_write_register(addr, ISSI_REG_SHUTDOWN, 0x00);
  128. #ifdef ISSI_3731_DEGHOST // set to enable de-ghosting of the array
  129. IS31FL3731_write_register(addr, ISSI_REG_GHOST_IMAGE_PREVENTION, 0x10);
  130. #endif
  131. // this delay was copied from other drivers, might not be needed
  132. wait_ms(10);
  133. // picture mode
  134. IS31FL3731_write_register(addr, ISSI_REG_CONFIG, ISSI_REG_CONFIG_PICTUREMODE);
  135. // display frame 0
  136. IS31FL3731_write_register(addr, ISSI_REG_PICTUREFRAME, 0x00);
  137. // audio sync off
  138. IS31FL3731_write_register(addr, ISSI_REG_AUDIOSYNC, 0x00);
  139. // select bank 0
  140. IS31FL3731_write_register(addr, ISSI_COMMANDREGISTER, 0);
  141. // turn off all LEDs in the LED control register
  142. for (int i = 0x00; i <= 0x11; i++) {
  143. IS31FL3731_write_register(addr, i, 0x00);
  144. }
  145. // turn off all LEDs in the blink control register (not really needed)
  146. for (int i = 0x12; i <= 0x23; i++) {
  147. IS31FL3731_write_register(addr, i, 0x00);
  148. }
  149. // set PWM on all LEDs to 0
  150. for (int i = 0x24; i <= 0xB3; i++) {
  151. IS31FL3731_write_register(addr, i, 0x00);
  152. }
  153. // select "function register" bank
  154. IS31FL3731_write_register(addr, ISSI_COMMANDREGISTER, ISSI_BANK_FUNCTIONREG);
  155. // disable software shutdown
  156. IS31FL3731_write_register(addr, ISSI_REG_SHUTDOWN, 0x01);
  157. // select bank 0 and leave it selected.
  158. // most usage after initialization is just writing PWM buffers in bank 0
  159. // as there's not much point in double-buffering
  160. IS31FL3731_write_register(addr, ISSI_COMMANDREGISTER, 0);
  161. }
  162. void IS31FL3731_set_value(int index, uint8_t value) {
  163. is31_led led;
  164. if (index >= 0 && index < DRIVER_LED_TOTAL) {
  165. memcpy_P(&led, (&g_is31_leds[index]), sizeof(led));
  166. // Subtract 0x24 to get the second index of g_pwm_buffer
  167. g_pwm_buffer[led.driver][led.v - 0x24] = value;
  168. g_pwm_buffer_update_required[led.driver] = true;
  169. }
  170. }
  171. void IS31FL3731_set_value_all(uint8_t value) {
  172. for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
  173. IS31FL3731_set_value(i, value);
  174. }
  175. }
  176. void IS31FL3731_set_led_control_register(uint8_t index, bool value) {
  177. is31_led led;
  178. memcpy_P(&led, (&g_is31_leds[index]), sizeof(led));
  179. uint8_t control_register = (led.v - 0x24) / 8;
  180. uint8_t bit_value = (led.v - 0x24) % 8;
  181. if (value) {
  182. g_led_control_registers[led.driver][control_register] |= (1 << bit_value);
  183. } else {
  184. g_led_control_registers[led.driver][control_register] &= ~(1 << bit_value);
  185. }
  186. g_led_control_registers_update_required[led.driver] = true;
  187. }
  188. void IS31FL3731_update_pwm_buffers(uint8_t addr, uint8_t index) {
  189. if (g_pwm_buffer_update_required[index]) {
  190. IS31FL3731_write_pwm_buffer(addr, g_pwm_buffer[index]);
  191. g_pwm_buffer_update_required[index] = false;
  192. }
  193. }
  194. void IS31FL3731_update_led_control_registers(uint8_t addr, uint8_t index) {
  195. if (g_led_control_registers_update_required[index]) {
  196. for (int i = 0; i < 18; i++) {
  197. IS31FL3731_write_register(addr, i, g_led_control_registers[index][i]);
  198. }
  199. g_led_control_registers_update_required[index] = false;
  200. }
  201. }