qmk

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.z3bra.org/qmk.git
Log | Files | Refs | Submodules | LICENSE

ws2812.c (8776B)


      1 /*
      2  * light weight WS2812 lib V2.0b
      3  *
      4  * Controls WS2811/WS2812/WS2812B RGB-LEDs
      5  * Author: Tim (cpldcpu@gmail.com)
      6  *
      7  * Jan 18th, 2014  v2.0b Initial Version
      8  * Nov 29th, 2015  v2.3  Added SK6812RGBW support
      9  *
     10  * This program is free software: you can redistribute it and/or modify
     11  * it under the terms of the GNU General Public License as published by
     12  * the Free Software Foundation, either version 2 of the License, or
     13  * (at your option) any later version.
     14  *
     15  * This program is distributed in the hope that it will be useful,
     16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18  * GNU General Public License for more details.
     19  *
     20  * You should have received a copy of the GNU General Public License
     21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     22  */
     23 
     24 #include "ws2812.h"
     25 #include <avr/interrupt.h>
     26 #include <avr/io.h>
     27 #include <util/delay.h>
     28 #include "debug.h"
     29 
     30 #if !defined(LED_ARRAY) && defined(RGB_MATRIX_ENABLE)
     31 // LED color buffer
     32 LED_TYPE led[DRIVER_LED_TOTAL];
     33 #    define LED_ARRAY led
     34 #endif
     35 
     36 #ifdef RGBW_BB_TWI
     37 
     38 // Port for the I2C
     39 #    define I2C_DDR DDRD
     40 #    define I2C_PIN PIND
     41 #    define I2C_PORT PORTD
     42 
     43 // Pins to be used in the bit banging
     44 #    define I2C_CLK 0
     45 #    define I2C_DAT 1
     46 
     47 #    define I2C_DATA_HI()           \
     48         I2C_DDR &= ~(1 << I2C_DAT); \
     49         I2C_PORT |= (1 << I2C_DAT);
     50 #    define I2C_DATA_LO()          \
     51         I2C_DDR |= (1 << I2C_DAT); \
     52         I2C_PORT &= ~(1 << I2C_DAT);
     53 
     54 #    define I2C_CLOCK_HI()          \
     55         I2C_DDR &= ~(1 << I2C_CLK); \
     56         I2C_PORT |= (1 << I2C_CLK);
     57 #    define I2C_CLOCK_LO()         \
     58         I2C_DDR |= (1 << I2C_CLK); \
     59         I2C_PORT &= ~(1 << I2C_CLK);
     60 
     61 #    define I2C_DELAY 1
     62 
     63 void I2C_WriteBit(unsigned char c) {
     64     if (c > 0) {
     65         I2C_DATA_HI();
     66     } else {
     67         I2C_DATA_LO();
     68     }
     69 
     70     I2C_CLOCK_HI();
     71     _delay_us(I2C_DELAY);
     72 
     73     I2C_CLOCK_LO();
     74     _delay_us(I2C_DELAY);
     75 
     76     if (c > 0) {
     77         I2C_DATA_LO();
     78     }
     79 
     80     _delay_us(I2C_DELAY);
     81 }
     82 
     83 // Inits bitbanging port, must be called before using the functions below
     84 //
     85 void I2C_Init(void) {
     86     I2C_PORT &= ~((1 << I2C_DAT) | (1 << I2C_CLK));
     87 
     88     I2C_CLOCK_HI();
     89     I2C_DATA_HI();
     90 
     91     _delay_us(I2C_DELAY);
     92 }
     93 
     94 // Send a START Condition
     95 //
     96 void I2C_Start(void) {
     97     // set both to high at the same time
     98     I2C_DDR &= ~((1 << I2C_DAT) | (1 << I2C_CLK));
     99     _delay_us(I2C_DELAY);
    100 
    101     I2C_DATA_LO();
    102     _delay_us(I2C_DELAY);
    103 
    104     I2C_CLOCK_LO();
    105     _delay_us(I2C_DELAY);
    106 }
    107 
    108 // Send a STOP Condition
    109 //
    110 void I2C_Stop(void) {
    111     I2C_CLOCK_HI();
    112     _delay_us(I2C_DELAY);
    113 
    114     I2C_DATA_HI();
    115     _delay_us(I2C_DELAY);
    116 }
    117 
    118 // write a byte to the I2C slave device
    119 //
    120 unsigned char I2C_Write(unsigned char c) {
    121     for (char i = 0; i < 8; i++) {
    122         I2C_WriteBit(c & 128);
    123 
    124         c <<= 1;
    125     }
    126 
    127     I2C_WriteBit(0);
    128     _delay_us(I2C_DELAY);
    129     _delay_us(I2C_DELAY);
    130 
    131     // _delay_us(I2C_DELAY);
    132     // return I2C_ReadBit();
    133     return 0;
    134 }
    135 
    136 #endif
    137 
    138 #ifdef RGB_MATRIX_ENABLE
    139 // Set an led in the buffer to a color
    140 void inline ws2812_setled(int i, uint8_t r, uint8_t g, uint8_t b) {
    141     led[i].r = r;
    142     led[i].g = g;
    143     led[i].b = b;
    144 }
    145 
    146 void ws2812_setled_all(uint8_t r, uint8_t g, uint8_t b) {
    147     for (int i = 0; i < sizeof(led) / sizeof(led[0]); i++) {
    148         led[i].r = r;
    149         led[i].g = g;
    150         led[i].b = b;
    151     }
    152 }
    153 #endif
    154 
    155 // Setleds for standard RGB
    156 void inline ws2812_setleds(LED_TYPE *ledarray, uint16_t leds) {
    157     // ws2812_setleds_pin(ledarray,leds, _BV(ws2812_pin));
    158     ws2812_setleds_pin(ledarray, leds, _BV(RGB_DI_PIN & 0xF));
    159 }
    160 
    161 void inline ws2812_setleds_pin(LED_TYPE *ledarray, uint16_t leds, uint8_t pinmask) {
    162     // ws2812_DDRREG |= pinmask; // Enable DDR
    163     // new universal format (DDR)
    164     _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= pinmask;
    165 
    166     ws2812_sendarray_mask((uint8_t *)ledarray, leds + leds + leds, pinmask);
    167     _delay_us(50);
    168 }
    169 
    170 // Setleds for SK6812RGBW
    171 void inline ws2812_setleds_rgbw(LED_TYPE *ledarray, uint16_t leds) {
    172 #ifdef RGBW_BB_TWI
    173     uint8_t sreg_prev, twcr_prev;
    174     sreg_prev = SREG;
    175     twcr_prev = TWCR;
    176     cli();
    177     TWCR &= ~(1 << TWEN);
    178     I2C_Init();
    179     I2C_Start();
    180     I2C_Write(0x84);
    181     uint16_t datlen = leds << 2;
    182     uint8_t  curbyte;
    183     uint8_t *data = (uint8_t *)ledarray;
    184     while (datlen--) {
    185         curbyte = *data++;
    186         I2C_Write(curbyte);
    187     }
    188     I2C_Stop();
    189     SREG = sreg_prev;
    190     TWCR = twcr_prev;
    191 #endif
    192 
    193     // ws2812_DDRREG |= _BV(ws2812_pin); // Enable DDR
    194     // new universal format (DDR)
    195     _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= _BV(RGB_DI_PIN & 0xF);
    196 
    197     ws2812_sendarray_mask((uint8_t *)ledarray, leds << 2, _BV(RGB_DI_PIN & 0xF));
    198 
    199 #ifndef RGBW_BB_TWI
    200     _delay_us(80);
    201 #endif
    202 }
    203 
    204 void ws2812_sendarray(uint8_t *data, uint16_t datlen) { ws2812_sendarray_mask(data, datlen, _BV(RGB_DI_PIN & 0xF)); }
    205 
    206 /*
    207   This routine writes an array of bytes with RGB values to the Dataout pin
    208   using the fast 800kHz clockless WS2811/2812 protocol.
    209 */
    210 
    211 // Timing in ns
    212 #define w_zeropulse 350
    213 #define w_onepulse 900
    214 #define w_totalperiod 1250
    215 
    216 // Fixed cycles used by the inner loop
    217 #define w_fixedlow 2
    218 #define w_fixedhigh 4
    219 #define w_fixedtotal 8
    220 
    221 // Insert NOPs to match the timing, if possible
    222 #define w_zerocycles (((F_CPU / 1000) * w_zeropulse) / 1000000)
    223 #define w_onecycles (((F_CPU / 1000) * w_onepulse + 500000) / 1000000)
    224 #define w_totalcycles (((F_CPU / 1000) * w_totalperiod + 500000) / 1000000)
    225 
    226 // w1 - nops between rising edge and falling edge - low
    227 #define w1 (w_zerocycles - w_fixedlow)
    228 // w2   nops between fe low and fe high
    229 #define w2 (w_onecycles - w_fixedhigh - w1)
    230 // w3   nops to complete loop
    231 #define w3 (w_totalcycles - w_fixedtotal - w1 - w2)
    232 
    233 #if w1 > 0
    234 #    define w1_nops w1
    235 #else
    236 #    define w1_nops 0
    237 #endif
    238 
    239 // The only critical timing parameter is the minimum pulse length of the "0"
    240 // Warn or throw error if this timing can not be met with current F_CPU settings.
    241 #define w_lowtime ((w1_nops + w_fixedlow) * 1000000) / (F_CPU / 1000)
    242 #if w_lowtime > 550
    243 #    error "Light_ws2812: Sorry, the clock speed is too low. Did you set F_CPU correctly?"
    244 #elif w_lowtime > 450
    245 #    warning "Light_ws2812: The timing is critical and may only work on WS2812B, not on WS2812(S)."
    246 #    warning "Please consider a higher clockspeed, if possible"
    247 #endif
    248 
    249 #if w2 > 0
    250 #    define w2_nops w2
    251 #else
    252 #    define w2_nops 0
    253 #endif
    254 
    255 #if w3 > 0
    256 #    define w3_nops w3
    257 #else
    258 #    define w3_nops 0
    259 #endif
    260 
    261 #define w_nop1 "nop      \n\t"
    262 #define w_nop2 "rjmp .+0 \n\t"
    263 #define w_nop4 w_nop2 w_nop2
    264 #define w_nop8 w_nop4 w_nop4
    265 #define w_nop16 w_nop8 w_nop8
    266 
    267 void inline ws2812_sendarray_mask(uint8_t *data, uint16_t datlen, uint8_t maskhi) {
    268     uint8_t curbyte, ctr, masklo;
    269     uint8_t sreg_prev;
    270 
    271     // masklo  =~maskhi&ws2812_PORTREG;
    272     // maskhi |=        ws2812_PORTREG;
    273     masklo = ~maskhi & _SFR_IO8((RGB_DI_PIN >> 4) + 2);
    274     maskhi |= _SFR_IO8((RGB_DI_PIN >> 4) + 2);
    275     sreg_prev = SREG;
    276     cli();
    277 
    278     while (datlen--) {
    279         curbyte = (*data++);
    280 
    281         asm volatile("       ldi   %0,8  \n\t"
    282                      "loop%=:            \n\t"
    283                      "       out   %2,%3 \n\t"  //  '1' [01] '0' [01] - re
    284 #if (w1_nops & 1)
    285                      w_nop1
    286 #endif
    287 #if (w1_nops & 2)
    288                          w_nop2
    289 #endif
    290 #if (w1_nops & 4)
    291                              w_nop4
    292 #endif
    293 #if (w1_nops & 8)
    294                                  w_nop8
    295 #endif
    296 #if (w1_nops & 16)
    297                                      w_nop16
    298 #endif
    299                      "       sbrs  %1,7  \n\t"  //  '1' [03] '0' [02]
    300                      "       out   %2,%4 \n\t"  //  '1' [--] '0' [03] - fe-low
    301                      "       lsl   %1    \n\t"  //  '1' [04] '0' [04]
    302 #if (w2_nops & 1)
    303                      w_nop1
    304 #endif
    305 #if (w2_nops & 2)
    306                          w_nop2
    307 #endif
    308 #if (w2_nops & 4)
    309                              w_nop4
    310 #endif
    311 #if (w2_nops & 8)
    312                                  w_nop8
    313 #endif
    314 #if (w2_nops & 16)
    315                                      w_nop16
    316 #endif
    317                      "       out   %2,%4 \n\t"  //  '1' [+1] '0' [+1] - fe-high
    318 #if (w3_nops & 1)
    319                      w_nop1
    320 #endif
    321 #if (w3_nops & 2)
    322                          w_nop2
    323 #endif
    324 #if (w3_nops & 4)
    325                              w_nop4
    326 #endif
    327 #if (w3_nops & 8)
    328                                  w_nop8
    329 #endif
    330 #if (w3_nops & 16)
    331                                      w_nop16
    332 #endif
    333 
    334                      "       dec   %0    \n\t"  //  '1' [+2] '0' [+2]
    335                      "       brne  loop%=\n\t"  //  '1' [+3] '0' [+4]
    336                      : "=&d"(ctr)
    337                      : "r"(curbyte), "I"(_SFR_IO_ADDR(_SFR_IO8((RGB_DI_PIN >> 4) + 2))), "r"(maskhi), "r"(masklo));
    338     }
    339 
    340     SREG = sreg_prev;
    341 }