diff options
Diffstat (limited to 'quantum/light_ws2812.c')
| -rwxr-xr-x | quantum/light_ws2812.c | 143 |
1 files changed, 141 insertions, 2 deletions
diff --git a/quantum/light_ws2812.c b/quantum/light_ws2812.c index 401845e85..497543339 100755 --- a/quantum/light_ws2812.c +++ b/quantum/light_ws2812.c | |||
| @@ -16,6 +16,122 @@ | |||
| 16 | #include <util/delay.h> | 16 | #include <util/delay.h> |
| 17 | #include "debug.h" | 17 | #include "debug.h" |
| 18 | 18 | ||
| 19 | #define RGBW_BB_TWI 1 | ||
| 20 | |||
| 21 | #ifdef RGBW_BB_TWI | ||
| 22 | |||
| 23 | // Port for the I2C | ||
| 24 | #define I2C_DDR DDRD | ||
| 25 | #define I2C_PIN PIND | ||
| 26 | #define I2C_PORT PORTD | ||
| 27 | |||
| 28 | // Pins to be used in the bit banging | ||
| 29 | #define I2C_CLK 0 | ||
| 30 | #define I2C_DAT 1 | ||
| 31 | |||
| 32 | #define I2C_DATA_HI()\ | ||
| 33 | I2C_DDR &= ~ (1 << I2C_DAT);\ | ||
| 34 | I2C_PORT |= (1 << I2C_DAT); | ||
| 35 | #define I2C_DATA_LO()\ | ||
| 36 | I2C_DDR |= (1 << I2C_DAT);\ | ||
| 37 | I2C_PORT &= ~ (1 << I2C_DAT); | ||
| 38 | |||
| 39 | #define I2C_CLOCK_HI()\ | ||
| 40 | I2C_DDR &= ~ (1 << I2C_CLK);\ | ||
| 41 | I2C_PORT |= (1 << I2C_CLK); | ||
| 42 | #define I2C_CLOCK_LO()\ | ||
| 43 | I2C_DDR |= (1 << I2C_CLK);\ | ||
| 44 | I2C_PORT &= ~ (1 << I2C_CLK); | ||
| 45 | |||
| 46 | #define I2C_DELAY 1 | ||
| 47 | |||
| 48 | void I2C_WriteBit(unsigned char c) | ||
| 49 | { | ||
| 50 | if (c > 0) | ||
| 51 | { | ||
| 52 | I2C_DATA_HI(); | ||
| 53 | } | ||
| 54 | else | ||
| 55 | { | ||
| 56 | I2C_DATA_LO(); | ||
| 57 | } | ||
| 58 | |||
| 59 | I2C_CLOCK_HI(); | ||
| 60 | _delay_us(I2C_DELAY); | ||
| 61 | |||
| 62 | I2C_CLOCK_LO(); | ||
| 63 | _delay_us(I2C_DELAY); | ||
| 64 | |||
| 65 | if (c > 0) | ||
| 66 | { | ||
| 67 | I2C_DATA_LO(); | ||
| 68 | } | ||
| 69 | |||
| 70 | _delay_us(I2C_DELAY); | ||
| 71 | } | ||
| 72 | |||
| 73 | // Inits bitbanging port, must be called before using the functions below | ||
| 74 | // | ||
| 75 | void I2C_Init() | ||
| 76 | { | ||
| 77 | I2C_PORT &= ~ ((1 << I2C_DAT) | (1 << I2C_CLK)); | ||
| 78 | |||
| 79 | I2C_CLOCK_HI(); | ||
| 80 | I2C_DATA_HI(); | ||
| 81 | |||
| 82 | _delay_us(I2C_DELAY); | ||
| 83 | } | ||
| 84 | |||
| 85 | // Send a START Condition | ||
| 86 | // | ||
| 87 | void I2C_Start() | ||
| 88 | { | ||
| 89 | // set both to high at the same time | ||
| 90 | I2C_DDR &= ~ ((1 << I2C_DAT) | (1 << I2C_CLK)); | ||
| 91 | _delay_us(I2C_DELAY); | ||
| 92 | |||
| 93 | I2C_DATA_LO(); | ||
| 94 | _delay_us(I2C_DELAY); | ||
| 95 | |||
| 96 | I2C_CLOCK_LO(); | ||
| 97 | _delay_us(I2C_DELAY); | ||
| 98 | } | ||
| 99 | |||
| 100 | // Send a STOP Condition | ||
| 101 | // | ||
| 102 | void I2C_Stop() | ||
| 103 | { | ||
| 104 | I2C_CLOCK_HI(); | ||
| 105 | _delay_us(I2C_DELAY); | ||
| 106 | |||
| 107 | I2C_DATA_HI(); | ||
| 108 | _delay_us(I2C_DELAY); | ||
| 109 | } | ||
| 110 | |||
| 111 | // write a byte to the I2C slave device | ||
| 112 | // | ||
| 113 | unsigned char I2C_Write(unsigned char c) | ||
| 114 | { | ||
| 115 | for (char i = 0; i < 8; i++) | ||
| 116 | { | ||
| 117 | I2C_WriteBit(c & 128); | ||
| 118 | |||
| 119 | c <<= 1; | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | I2C_WriteBit(0); | ||
| 124 | _delay_us(I2C_DELAY); | ||
| 125 | _delay_us(I2C_DELAY); | ||
| 126 | |||
| 127 | // _delay_us(I2C_DELAY); | ||
| 128 | //return I2C_ReadBit(); | ||
| 129 | return 0; | ||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | #endif | ||
| 134 | |||
| 19 | // Setleds for standard RGB | 135 | // Setleds for standard RGB |
| 20 | void inline ws2812_setleds(struct cRGB *ledarray, uint16_t leds) | 136 | void inline ws2812_setleds(struct cRGB *ledarray, uint16_t leds) |
| 21 | { | 137 | { |
| @@ -36,12 +152,35 @@ void inline ws2812_setleds_pin(struct cRGB *ledarray, uint16_t leds, uint8_t pin | |||
| 36 | // Setleds for SK6812RGBW | 152 | // Setleds for SK6812RGBW |
| 37 | void inline ws2812_setleds_rgbw(struct cRGBW *ledarray, uint16_t leds) | 153 | void inline ws2812_setleds_rgbw(struct cRGBW *ledarray, uint16_t leds) |
| 38 | { | 154 | { |
| 155 | |||
| 156 | #ifdef RGBW_BB_TWI | ||
| 157 | cli(); | ||
| 158 | TWCR = 0; | ||
| 159 | I2C_Init(); | ||
| 160 | I2C_Start(); | ||
| 161 | I2C_Write(0x84); | ||
| 162 | uint16_t datlen = leds<<2; | ||
| 163 | uint8_t curbyte; | ||
| 164 | uint8_t * data = (uint8_t*)ledarray; | ||
| 165 | while (datlen--) { | ||
| 166 | curbyte=*data++; | ||
| 167 | I2C_Write(curbyte); | ||
| 168 | } | ||
| 169 | I2C_Stop(); | ||
| 170 | sei(); | ||
| 171 | #else | ||
| 172 | _delay_us(80); | ||
| 173 | #endif | ||
| 174 | |||
| 175 | |||
| 39 | // ws2812_DDRREG |= _BV(ws2812_pin); // Enable DDR | 176 | // ws2812_DDRREG |= _BV(ws2812_pin); // Enable DDR |
| 40 | // new universal format (DDR) | 177 | // new universal format (DDR) |
| 41 | _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= _BV(RGB_DI_PIN & 0xF); | 178 | _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= _BV(RGB_DI_PIN & 0xF); |
| 42 | 179 | ||
| 43 | ws2812_sendarray_mask((uint8_t*)ledarray,leds<<2,_BV(RGB_DI_PIN & 0xF)); | 180 | ws2812_sendarray_mask((uint8_t*)ledarray,leds<<2,_BV(RGB_DI_PIN & 0xF)); |
| 44 | _delay_us(80); | 181 | |
| 182 | |||
| 183 | |||
| 45 | } | 184 | } |
| 46 | 185 | ||
| 47 | void ws2812_sendarray(uint8_t *data,uint16_t datlen) | 186 | void ws2812_sendarray(uint8_t *data,uint16_t datlen) |
| @@ -123,7 +262,7 @@ void inline ws2812_sendarray_mask(uint8_t *data,uint16_t datlen,uint8_t maskhi) | |||
| 123 | cli(); | 262 | cli(); |
| 124 | 263 | ||
| 125 | while (datlen--) { | 264 | while (datlen--) { |
| 126 | curbyte=*data++; | 265 | curbyte=(*data++); |
| 127 | 266 | ||
| 128 | asm volatile( | 267 | asm volatile( |
| 129 | " ldi %0,8 \n\t" | 268 | " ldi %0,8 \n\t" |
