aw20216.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* Copyright 2021 Jasper Chan
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "aw20216.h"
  17. #include "spi_master.h"
  18. /* The AW20216 appears to be somewhat similar to the IS31FL743, although quite
  19. * a few things are different, such as the command byte format and page ordering.
  20. * The LED addresses start from 0x00 instead of 0x01.
  21. */
  22. #define AWINIC_ID 0b1010 << 4
  23. #define AW_PAGE_FUNCTION 0x00 << 1 // PG0, Function registers
  24. #define AW_PAGE_PWM 0x01 << 1 // PG1, LED PWM control
  25. #define AW_PAGE_SCALING 0x02 << 1 // PG2, LED current scaling control
  26. #define AW_PAGE_PATCHOICE 0x03 << 1 // PG3, Pattern choice?
  27. #define AW_PAGE_PWMSCALING 0x04 << 1 // PG4, LED PWM + Scaling control?
  28. #define AW_WRITE 0
  29. #define AW_READ 1
  30. #define AW_REG_CONFIGURATION 0x00 // PG0
  31. #define AW_REG_GLOBALCURRENT 0x01 // PG0
  32. // Default value of AW_REG_CONFIGURATION
  33. // D7:D4 = 1011, SWSEL (SW1~SW12 active)
  34. // D3 = 0?, reserved (apparently this should be 1 but it doesn't seem to matter)
  35. // D2:D1 = 00, OSDE (open/short detection enable)
  36. // D0 = 0, CHIPEN (write 1 to enable LEDs when hardware enable pulled high)
  37. #define AW_CONFIG_DEFAULT 0b10110000
  38. #define AW_CHIPEN 1
  39. #ifndef AW_SCALING_MAX
  40. # define AW_SCALING_MAX 150
  41. #endif
  42. #ifndef AW_GLOBAL_CURRENT_MAX
  43. # define AW_GLOBAL_CURRENT_MAX 150
  44. #endif
  45. #ifndef DRIVER_1_CS
  46. # define DRIVER_1_CS B13
  47. #endif
  48. #ifndef DRIVER_1_EN
  49. # define DRIVER_1_EN C13
  50. #endif
  51. uint8_t g_spi_transfer_buffer[20] = {0};
  52. aw_led g_pwm_buffer[DRIVER_LED_TOTAL];
  53. bool g_pwm_buffer_update_required[DRIVER_LED_TOTAL];
  54. bool AW20216_write_register(pin_t slave_pin, uint8_t page, uint8_t reg, uint8_t data) {
  55. // Do we need to call spi_stop() if this fails?
  56. if (!spi_start(slave_pin, false, 0, 16)) {
  57. return false;
  58. }
  59. g_spi_transfer_buffer[0] = (AWINIC_ID | page | AW_WRITE);
  60. g_spi_transfer_buffer[1] = reg;
  61. g_spi_transfer_buffer[2] = data;
  62. if (spi_transmit(g_spi_transfer_buffer, 3) != SPI_STATUS_SUCCESS) {
  63. spi_stop();
  64. return false;
  65. }
  66. spi_stop();
  67. return true;
  68. }
  69. bool AW20216_init_scaling(void) {
  70. // Set constant current to the max, control brightness with PWM
  71. aw_led led;
  72. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  73. led = g_aw_leds[i];
  74. if (led.driver == 0) {
  75. AW20216_write_register(DRIVER_1_CS, AW_PAGE_SCALING, led.r, AW_SCALING_MAX);
  76. AW20216_write_register(DRIVER_1_CS, AW_PAGE_SCALING, led.g, AW_SCALING_MAX);
  77. AW20216_write_register(DRIVER_1_CS, AW_PAGE_SCALING, led.b, AW_SCALING_MAX);
  78. }
  79. #ifdef DRIVER_2_CS
  80. else if (led.driver == 1) {
  81. AW20216_write_register(DRIVER_2_CS, AW_PAGE_SCALING, led.r, AW_SCALING_MAX);
  82. AW20216_write_register(DRIVER_2_CS, AW_PAGE_SCALING, led.g, AW_SCALING_MAX);
  83. AW20216_write_register(DRIVER_2_CS, AW_PAGE_SCALING, led.b, AW_SCALING_MAX);
  84. }
  85. #endif
  86. }
  87. return true;
  88. }
  89. bool AW20216_soft_enable(void) {
  90. AW20216_write_register(DRIVER_1_CS, AW_PAGE_FUNCTION, AW_REG_CONFIGURATION, AW_CONFIG_DEFAULT | AW_CHIPEN);
  91. #ifdef DRIVER_2_CS
  92. AW20216_write_register(DRIVER_2_CS, AW_PAGE_FUNCTION, AW_REG_CONFIGURATION, AW_CONFIG_DEFAULT | AW_CHIPEN);
  93. #endif
  94. return true;
  95. }
  96. void AW20216_update_pwm(int index, uint8_t red, uint8_t green, uint8_t blue) {
  97. aw_led led = g_aw_leds[index];
  98. if (led.driver == 0) {
  99. AW20216_write_register(DRIVER_1_CS, AW_PAGE_PWM, led.r, red);
  100. AW20216_write_register(DRIVER_1_CS, AW_PAGE_PWM, led.g, green);
  101. AW20216_write_register(DRIVER_1_CS, AW_PAGE_PWM, led.b, blue);
  102. }
  103. #ifdef DRIVER_2_CS
  104. else if (led.driver == 1) {
  105. AW20216_write_register(DRIVER_2_CS, AW_PAGE_PWM, led.r, red);
  106. AW20216_write_register(DRIVER_2_CS, AW_PAGE_PWM, led.g, green);
  107. AW20216_write_register(DRIVER_2_CS, AW_PAGE_PWM, led.b, blue);
  108. }
  109. #endif
  110. return;
  111. }
  112. void AW20216_init(void) {
  113. // All LEDs should start with all scaling and PWM registers as off
  114. setPinOutput(DRIVER_1_EN);
  115. writePinHigh(DRIVER_1_EN);
  116. AW20216_write_register(DRIVER_1_CS, AW_PAGE_FUNCTION, AW_REG_GLOBALCURRENT, AW_GLOBAL_CURRENT_MAX);
  117. #ifdef DRIVER_2_EN
  118. setPinOutput(DRIVER_2_EN);
  119. writePinHigh(DRIVER_2_EN);
  120. AW20216_write_register(DRIVER_2_CS, AW_PAGE_FUNCTION, AW_REG_GLOBALCURRENT, AW_GLOBAL_CURRENT_MAX);
  121. #endif
  122. AW20216_init_scaling();
  123. AW20216_soft_enable();
  124. return;
  125. }
  126. void AW20216_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
  127. g_pwm_buffer[index].r = red;
  128. g_pwm_buffer[index].g = green;
  129. g_pwm_buffer[index].b = blue;
  130. g_pwm_buffer_update_required[index] = true;
  131. return;
  132. }
  133. void AW20216_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
  134. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  135. AW20216_set_color(i, red, green, blue);
  136. }
  137. return;
  138. }
  139. void AW20216_update_pwm_buffers(void) {
  140. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  141. if (g_pwm_buffer_update_required[i]) {
  142. AW20216_update_pwm(i, g_pwm_buffer[i].r, g_pwm_buffer[i].g, g_pwm_buffer[i].b);
  143. g_pwm_buffer_update_required[i] = false;
  144. }
  145. }
  146. return;
  147. }