diff options
Diffstat (limited to 'quantum/light_ws2812.c')
| -rwxr-xr-x | quantum/light_ws2812.c | 137 |
1 files changed, 136 insertions, 1 deletions
diff --git a/quantum/light_ws2812.c b/quantum/light_ws2812.c index 401845e85..d38dac4c6 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 | { |
| @@ -41,6 +157,25 @@ void inline ws2812_setleds_rgbw(struct cRGBW *ledarray, uint16_t leds) | |||
| 41 | _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= _BV(RGB_DI_PIN & 0xF); | 157 | _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= _BV(RGB_DI_PIN & 0xF); |
| 42 | 158 | ||
| 43 | ws2812_sendarray_mask((uint8_t*)ledarray,leds<<2,_BV(RGB_DI_PIN & 0xF)); | 159 | ws2812_sendarray_mask((uint8_t*)ledarray,leds<<2,_BV(RGB_DI_PIN & 0xF)); |
| 160 | |||
| 161 | #ifdef RGBW_BB_TWI | ||
| 162 | cli(); | ||
| 163 | TWCR = 0; | ||
| 164 | I2C_Init(); | ||
| 165 | I2C_Start(); | ||
| 166 | I2C_Write(0x84); | ||
| 167 | uint16_t datlen = leds<<2; | ||
| 168 | uint8_t curbyte; | ||
| 169 | uint8_t * data = (uint8_t*)ledarray; | ||
| 170 | while (datlen--) { | ||
| 171 | curbyte=*data++; | ||
| 172 | I2C_Write(curbyte % 0x10); | ||
| 173 | } | ||
| 174 | I2C_Stop(); | ||
| 175 | sei(); | ||
| 176 | #endif | ||
| 177 | |||
| 178 | |||
| 44 | _delay_us(80); | 179 | _delay_us(80); |
| 45 | } | 180 | } |
| 46 | 181 | ||
| @@ -123,7 +258,7 @@ void inline ws2812_sendarray_mask(uint8_t *data,uint16_t datlen,uint8_t maskhi) | |||
| 123 | cli(); | 258 | cli(); |
| 124 | 259 | ||
| 125 | while (datlen--) { | 260 | while (datlen--) { |
| 126 | curbyte=*data++; | 261 | curbyte=(*data++) % 0x10; |
| 127 | 262 | ||
| 128 | asm volatile( | 263 | asm volatile( |
| 129 | " ldi %0,8 \n\t" | 264 | " ldi %0,8 \n\t" |
