diff options
Diffstat (limited to 'drivers/avr/ws2812.c')
| -rw-r--r-- | drivers/avr/ws2812.c | 129 |
1 files changed, 2 insertions, 127 deletions
diff --git a/drivers/avr/ws2812.c b/drivers/avr/ws2812.c index dc7e8d48a..82d985c20 100644 --- a/drivers/avr/ws2812.c +++ b/drivers/avr/ws2812.c | |||
| @@ -36,108 +36,6 @@ | |||
| 36 | void ws2812_sendarray(uint8_t *array, uint16_t length); | 36 | void ws2812_sendarray(uint8_t *array, uint16_t length); |
| 37 | void ws2812_sendarray_mask(uint8_t *array, uint16_t length, uint8_t pinmask); | 37 | void ws2812_sendarray_mask(uint8_t *array, uint16_t length, uint8_t pinmask); |
| 38 | 38 | ||
| 39 | #ifdef RGBW_BB_TWI | ||
| 40 | |||
| 41 | // Port for the I2C | ||
| 42 | # define I2C_DDR DDRD | ||
| 43 | # define I2C_PIN PIND | ||
| 44 | # define I2C_PORT PORTD | ||
| 45 | |||
| 46 | // Pins to be used in the bit banging | ||
| 47 | # define I2C_CLK 0 | ||
| 48 | # define I2C_DAT 1 | ||
| 49 | |||
| 50 | # define I2C_DATA_HI() \ | ||
| 51 | I2C_DDR &= ~(1 << I2C_DAT); \ | ||
| 52 | I2C_PORT |= (1 << I2C_DAT); | ||
| 53 | # define I2C_DATA_LO() \ | ||
| 54 | I2C_DDR |= (1 << I2C_DAT); \ | ||
| 55 | I2C_PORT &= ~(1 << I2C_DAT); | ||
| 56 | |||
| 57 | # define I2C_CLOCK_HI() \ | ||
| 58 | I2C_DDR &= ~(1 << I2C_CLK); \ | ||
| 59 | I2C_PORT |= (1 << I2C_CLK); | ||
| 60 | # define I2C_CLOCK_LO() \ | ||
| 61 | I2C_DDR |= (1 << I2C_CLK); \ | ||
| 62 | I2C_PORT &= ~(1 << I2C_CLK); | ||
| 63 | |||
| 64 | # define I2C_DELAY 1 | ||
| 65 | |||
| 66 | void I2C_WriteBit(unsigned char c) { | ||
| 67 | if (c > 0) { | ||
| 68 | I2C_DATA_HI(); | ||
| 69 | } else { | ||
| 70 | I2C_DATA_LO(); | ||
| 71 | } | ||
| 72 | |||
| 73 | I2C_CLOCK_HI(); | ||
| 74 | _delay_us(I2C_DELAY); | ||
| 75 | |||
| 76 | I2C_CLOCK_LO(); | ||
| 77 | _delay_us(I2C_DELAY); | ||
| 78 | |||
| 79 | if (c > 0) { | ||
| 80 | I2C_DATA_LO(); | ||
| 81 | } | ||
| 82 | |||
| 83 | _delay_us(I2C_DELAY); | ||
| 84 | } | ||
| 85 | |||
| 86 | // Inits bitbanging port, must be called before using the functions below | ||
| 87 | // | ||
| 88 | void I2C_Init(void) { | ||
| 89 | I2C_PORT &= ~((1 << I2C_DAT) | (1 << I2C_CLK)); | ||
| 90 | |||
| 91 | I2C_CLOCK_HI(); | ||
| 92 | I2C_DATA_HI(); | ||
| 93 | |||
| 94 | _delay_us(I2C_DELAY); | ||
| 95 | } | ||
| 96 | |||
| 97 | // Send a START Condition | ||
| 98 | // | ||
| 99 | void I2C_Start(void) { | ||
| 100 | // set both to high at the same time | ||
| 101 | I2C_DDR &= ~((1 << I2C_DAT) | (1 << I2C_CLK)); | ||
| 102 | _delay_us(I2C_DELAY); | ||
| 103 | |||
| 104 | I2C_DATA_LO(); | ||
| 105 | _delay_us(I2C_DELAY); | ||
| 106 | |||
| 107 | I2C_CLOCK_LO(); | ||
| 108 | _delay_us(I2C_DELAY); | ||
| 109 | } | ||
| 110 | |||
| 111 | // Send a STOP Condition | ||
| 112 | // | ||
| 113 | void I2C_Stop(void) { | ||
| 114 | I2C_CLOCK_HI(); | ||
| 115 | _delay_us(I2C_DELAY); | ||
| 116 | |||
| 117 | I2C_DATA_HI(); | ||
| 118 | _delay_us(I2C_DELAY); | ||
| 119 | } | ||
| 120 | |||
| 121 | // write a byte to the I2C slave device | ||
| 122 | // | ||
| 123 | unsigned char I2C_Write(unsigned char c) { | ||
| 124 | for (char i = 0; i < 8; i++) { | ||
| 125 | I2C_WriteBit(c & 128); | ||
| 126 | |||
| 127 | c <<= 1; | ||
| 128 | } | ||
| 129 | |||
| 130 | I2C_WriteBit(0); | ||
| 131 | _delay_us(I2C_DELAY); | ||
| 132 | _delay_us(I2C_DELAY); | ||
| 133 | |||
| 134 | // _delay_us(I2C_DELAY); | ||
| 135 | // return I2C_ReadBit(); | ||
| 136 | return 0; | ||
| 137 | } | ||
| 138 | |||
| 139 | #endif | ||
| 140 | |||
| 141 | // Setleds for standard RGB | 39 | // Setleds for standard RGB |
| 142 | void inline ws2812_setleds(LED_TYPE *ledarray, uint16_t leds) { | 40 | void inline ws2812_setleds(LED_TYPE *ledarray, uint16_t leds) { |
| 143 | // ws2812_setleds_pin(ledarray,leds, _BV(ws2812_pin)); | 41 | // ws2812_setleds_pin(ledarray,leds, _BV(ws2812_pin)); |
| @@ -145,38 +43,15 @@ void inline ws2812_setleds(LED_TYPE *ledarray, uint16_t leds) { | |||
| 145 | } | 43 | } |
| 146 | 44 | ||
| 147 | void inline ws2812_setleds_pin(LED_TYPE *ledarray, uint16_t leds, uint8_t pinmask) { | 45 | void inline ws2812_setleds_pin(LED_TYPE *ledarray, uint16_t leds, uint8_t pinmask) { |
| 148 | #ifdef RGBW_BB_TWI | ||
| 149 | uint8_t sreg_prev, twcr_prev; | ||
| 150 | sreg_prev = SREG; | ||
| 151 | twcr_prev = TWCR; | ||
| 152 | cli(); | ||
| 153 | TWCR &= ~(1 << TWEN); | ||
| 154 | I2C_Init(); | ||
| 155 | I2C_Start(); | ||
| 156 | I2C_Write(0x84); | ||
| 157 | uint16_t datlen = leds << 2; | ||
| 158 | uint8_t curbyte; | ||
| 159 | uint8_t *data = (uint8_t *)ledarray; | ||
| 160 | while (datlen--) { | ||
| 161 | curbyte = *data++; | ||
| 162 | I2C_Write(curbyte); | ||
| 163 | } | ||
| 164 | I2C_Stop(); | ||
| 165 | SREG = sreg_prev; | ||
| 166 | TWCR = twcr_prev; | ||
| 167 | #endif | ||
| 168 | // ws2812_DDRREG |= pinmask; // Enable DDR | ||
| 169 | // new universal format (DDR) | 46 | // new universal format (DDR) |
| 170 | _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= pinmask; | 47 | _SFR_IO8((RGB_DI_PIN >> 4) + 1) |= pinmask; |
| 171 | 48 | ||
| 172 | ws2812_sendarray_mask((uint8_t *)ledarray, leds * sizeof(LED_TYPE), pinmask); | 49 | ws2812_sendarray_mask((uint8_t *)ledarray, leds * sizeof(LED_TYPE), pinmask); |
| 173 | 50 | ||
| 174 | #ifndef RGBW_BB_TWI | 51 | #ifdef RGBW |
| 175 | # ifdef RGBW | ||
| 176 | _delay_us(80); | 52 | _delay_us(80); |
| 177 | # else | 53 | #else |
| 178 | _delay_us(50); | 54 | _delay_us(50); |
| 179 | # endif | ||
| 180 | #endif | 55 | #endif |
| 181 | } | 56 | } |
| 182 | 57 | ||
