diff options
Diffstat (limited to 'drivers/led/issi/is31fl3218.c')
| -rw-r--r-- | drivers/led/issi/is31fl3218.c | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/drivers/led/issi/is31fl3218.c b/drivers/led/issi/is31fl3218.c new file mode 100644 index 000000000..d43863ac4 --- /dev/null +++ b/drivers/led/issi/is31fl3218.c | |||
| @@ -0,0 +1,96 @@ | |||
| 1 | /* Copyright 2018 Jason Williams (Wilba) | ||
| 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 "is31fl3218.h" | ||
| 17 | #include "i2c_master.h" | ||
| 18 | |||
| 19 | // This is the full 8-bit address | ||
| 20 | #define ISSI_ADDRESS 0b10101000 | ||
| 21 | |||
| 22 | // These are the register addresses | ||
| 23 | #define ISSI_REG_SHUTDOWN 0x00 | ||
| 24 | #define ISSI_REG_PWM 0x01 | ||
| 25 | #define ISSI_REG_CONTROL 0x13 | ||
| 26 | #define ISSI_REG_UPDATE 0x16 | ||
| 27 | #define ISSI_REG_RESET 0x17 | ||
| 28 | |||
| 29 | // Default timeout if no I2C response | ||
| 30 | #define ISSI_TIMEOUT 100 | ||
| 31 | |||
| 32 | // Reusable buffer for transfers | ||
| 33 | uint8_t g_twi_transfer_buffer[20]; | ||
| 34 | |||
| 35 | // IS31FL3218 has 18 PWM outputs and a fixed I2C address, so no chaining. | ||
| 36 | // If used as RGB LED driver, LEDs are assigned RGB,RGB,RGB,RGB,RGB,RGB | ||
| 37 | uint8_t g_pwm_buffer[18]; | ||
| 38 | bool g_pwm_buffer_update_required = false; | ||
| 39 | |||
| 40 | void IS31FL3218_write_register(uint8_t reg, uint8_t data) { | ||
| 41 | g_twi_transfer_buffer[0] = reg; | ||
| 42 | g_twi_transfer_buffer[1] = data; | ||
| 43 | i2c_transmit(ISSI_ADDRESS, g_twi_transfer_buffer, 2, ISSI_TIMEOUT); | ||
| 44 | } | ||
| 45 | |||
| 46 | void IS31FL3218_write_pwm_buffer(uint8_t *pwm_buffer) { | ||
| 47 | g_twi_transfer_buffer[0] = ISSI_REG_PWM; | ||
| 48 | for (int i = 0; i < 18; i++) { | ||
| 49 | g_twi_transfer_buffer[1 + i] = pwm_buffer[i]; | ||
| 50 | } | ||
| 51 | |||
| 52 | i2c_transmit(ISSI_ADDRESS, g_twi_transfer_buffer, 19, ISSI_TIMEOUT); | ||
| 53 | } | ||
| 54 | |||
| 55 | void IS31FL3218_init(void) { | ||
| 56 | // In case we ever want to reinitialize (?) | ||
| 57 | IS31FL3218_write_register(ISSI_REG_RESET, 0x00); | ||
| 58 | |||
| 59 | // Turn off software shutdown | ||
| 60 | IS31FL3218_write_register(ISSI_REG_SHUTDOWN, 0x01); | ||
| 61 | |||
| 62 | // Set all PWM values to zero | ||
| 63 | for (uint8_t i = 0; i < 18; i++) { | ||
| 64 | IS31FL3218_write_register(ISSI_REG_PWM + i, 0x00); | ||
| 65 | } | ||
| 66 | |||
| 67 | // Enable all channels | ||
| 68 | for (uint8_t i = 0; i < 3; i++) { | ||
| 69 | IS31FL3218_write_register(ISSI_REG_CONTROL + i, 0b00111111); | ||
| 70 | } | ||
| 71 | |||
| 72 | // Load PWM registers and LED Control register data | ||
| 73 | IS31FL3218_write_register(ISSI_REG_UPDATE, 0x01); | ||
| 74 | } | ||
| 75 | |||
| 76 | void IS31FL3218_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { | ||
| 77 | g_pwm_buffer[index * 3 + 0] = red; | ||
| 78 | g_pwm_buffer[index * 3 + 1] = green; | ||
| 79 | g_pwm_buffer[index * 3 + 2] = blue; | ||
| 80 | g_pwm_buffer_update_required = true; | ||
| 81 | } | ||
| 82 | |||
| 83 | void IS31FL3218_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { | ||
| 84 | for (int i = 0; i < 6; i++) { | ||
| 85 | IS31FL3218_set_color(i, red, green, blue); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | void IS31FL3218_update_pwm_buffers(void) { | ||
| 90 | if (g_pwm_buffer_update_required) { | ||
| 91 | IS31FL3218_write_pwm_buffer(g_pwm_buffer); | ||
| 92 | // Load PWM registers and LED Control register data | ||
| 93 | IS31FL3218_write_register(ISSI_REG_UPDATE, 0x01); | ||
| 94 | } | ||
| 95 | g_pwm_buffer_update_required = false; | ||
| 96 | } | ||
