qmk-firmware/platforms/avr/drivers/ws2812_bitbang.c

175 lines
5.3 KiB
C
Raw Permalink Normal View History

2016-01-25 05:14:50 +08:00
/*
2019-08-31 02:19:03 +08:00
* light weight WS2812 lib V2.0b
*
* Controls WS2811/WS2812/WS2812B RGB-LEDs
* Author: Tim (cpldcpu@gmail.com)
*
* Jan 18th, 2014 v2.0b Initial Version
* Nov 29th, 2015 v2.3 Added SK6812RGBW support
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2016-01-25 05:14:50 +08:00
#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/delay.h>
Fix platforms/avr/drivers/ws2812.c (#17043) * Fix platforms/avr/drivers/ws2812.c `platforms/avr/drivers/ws2812.c` has been changed to use `DDRx_ADDRESS()` and `PORTx_ADDRESS()` instead of `_SFR_IO8()` in #8646. To use them, `#include <pin_defs.h>` is required. ## Error Log * create new keyboard ```shell bash-3.2$ qmk new-keyboard Ψ Generating a new QMK keyboard directory Name Your Keyboard Project For more infomation, see: https://docs.qmk.fm/#/hardware_keyboard_guidelines?id=naming-your-keyboardproject Keyboard Name? ws2812_test .................................. 36. WB32F3G71 Please enter your choice: [12] Ψ Created a new keyboard called ws2812_test. Ψ To start working on things, `cd` into keyboards/ws2812_test, Ψ or open the directory in your preferred text editor. Ψ And build with qmk compile -kb ws2812_test -km default. ``` * Enable RGBLIGHT. ```shell bash-3.2$ echo RGBLIGHT_ENABLE=yes >> ./keyboards/ws2812_test/rules.mk bash-3.2$ echo '#define RGB_DI_PIN B1' >> ./keyboards/ws2812_test/config.h bash-3.2$ echo '#define RGBLED_NUM 6' >> ./keyboards/ws2812_test/config.h ``` * Compile ```shell bash-3.2$ make ws2812_test:default QMK Firmware 0.16.9 Making ws2812_test with keymap default avr-gcc (Homebrew AVR GCC 8.4.0_2) 8.4.0 Copyright (C) 2018 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ..................... Compiling: quantum/process_keycode/process_rgb.c [OK] Compiling: platforms/avr/drivers/ws2812.c platforms/avr/drivers/ws2812.c: In function 'ws2812_setleds': platforms/avr/drivers/ws2812.c:40:5: error: implicit declaration of function 'DDRx_ADDRESS' [-Werror=implicit-function-declaration] DDRx_ADDRESS(RGB_DI_PIN) |= pinmask(RGB_DI_PIN); ^~~~~~~~~~~~ In file included from <command-line>: ./keyboards/ws2812_test/config.h:21:20: error: 'B1' undeclared (first use in this function); did you mean 'PB1'? #define RGB_DI_PIN B1 ^~ platforms/avr/drivers/ws2812.c:40:18: note: in expansion of macro 'RGB_DI_PIN' DDRx_ADDRESS(RGB_DI_PIN) |= pinmask(RGB_DI_PIN); ^~~~~~~~~~ ./keyboards/ws2812_test/config.h:21:20: note: each undeclared identifier is reported only once for each function it appears in #define RGB_DI_PIN B1 ^~ platforms/avr/drivers/ws2812.c:40:18: note: in expansion of macro 'RGB_DI_PIN' DDRx_ADDRESS(RGB_DI_PIN) |= pinmask(RGB_DI_PIN); ^~~~~~~~~~ platforms/avr/drivers/ws2812.c:42:47: error: implicit declaration of function 'PORTx_ADDRESS' [-Werror=implicit-function-declaration] uint8_t masklo = ~(pinmask(RGB_DI_PIN)) & PORTx_ADDRESS(RGB_DI_PIN); ^~~~~~~~~~~~~ In file included from /usr/local/Cellar/avr-gcc@8/8.4.0_2/avr/include/avr/io.h:99, from /usr/local/Cellar/avr-gcc@8/8.4.0_2/avr/include/avr/interrupt.h:38, from platforms/avr/drivers/ws2812.c:24: platforms/avr/drivers/ws2812.c: In function 'ws2812_sendarray_mask': ./keyboards/ws2812_test/config.h:21:20: error: 'B1' undeclared (first use in this function); did you mean 'PB1'? #define RGB_DI_PIN B1 ^~ platforms/avr/drivers/ws2812.c:167:69: note: in expansion of macro 'RGB_DI_PIN' : "r"(curbyte), "I"(_SFR_IO_ADDR(PORTx_ADDRESS(RGB_DI_PIN))), "r"(maskhi), "r"(masklo)); ^~~~~~~~~~ cc1: all warnings being treated as errors [ERRORS] | | | make[1]: *** [.build/obj_ws2812_test_default/ws2812.o] Error 1 make: *** [ws2812_test:default] Error 1 Make finished with errors ``` * change include order
2022-05-19 03:58:03 +08:00
#include "ws2812.h"
#include "pin_defs.h"
2016-01-25 05:14:50 +08:00
#define pinmask(pin) (_BV((pin)&0xF))
/*
* Forward declare internal functions
*
* The functions take a byte-array and send to the data output as WS2812 bitstream.
* The length is the number of bytes to send - three per LED.
*/
static inline void ws2812_sendarray_mask(uint8_t *data, uint16_t datlen, uint8_t masklo, uint8_t maskhi);
void ws2812_init(void) {
DDRx_ADDRESS(WS2812_DI_PIN) |= pinmask(WS2812_DI_PIN);
}
2016-11-13 09:54:37 +08:00
void ws2812_setleds(rgb_led_t *ledarray, uint16_t number_of_leds) {
uint8_t masklo = ~(pinmask(WS2812_DI_PIN)) & PORTx_ADDRESS(WS2812_DI_PIN);
uint8_t maskhi = pinmask(WS2812_DI_PIN) | PORTx_ADDRESS(WS2812_DI_PIN);
2023-09-04 08:19:59 +08:00
ws2812_sendarray_mask((uint8_t *)ledarray, number_of_leds * sizeof(rgb_led_t), masklo, maskhi);
2016-11-13 09:54:37 +08:00
_delay_us(WS2812_TRST_US);
2016-01-25 05:14:50 +08:00
}
/*
This routine writes an array of bytes with RGB values to the Dataout pin
using the fast 800kHz clockless WS2811/2812 protocol.
*/
// Fixed cycles used by the inner loop
2019-08-31 02:19:03 +08:00
#define w_fixedlow 2
#define w_fixedhigh 4
#define w_fixedtotal 8
2016-01-25 05:14:50 +08:00
// Insert NOPs to match the timing, if possible
#define w_zerocycles (((F_CPU / 1000) * WS2812_T0H) / 1000000)
#define w_onecycles (((F_CPU / 1000) * WS2812_T1H + 500000) / 1000000)
#define w_totalcycles (((F_CPU / 1000) * WS2812_TIMING + 500000) / 1000000)
2016-01-25 05:14:50 +08:00
// w1_nops - nops between rising edge and falling edge - low
#if w_zerocycles >= w_fixedlow
# define w1_nops (w_zerocycles - w_fixedlow)
2016-01-25 05:14:50 +08:00
#else
2019-08-31 02:19:03 +08:00
# define w1_nops 0
2016-01-25 05:14:50 +08:00
#endif
// w2_nops - nops between fe low and fe high
#if w_onecycles >= (w_fixedhigh + w1_nops)
# define w2_nops (w_onecycles - w_fixedhigh - w1_nops)
#else
# define w2_nops 0
#endif
// w3_nops - nops to complete loop
#if w_totalcycles >= (w_fixedtotal + w1_nops + w2_nops)
# define w3_nops (w_totalcycles - w_fixedtotal - w1_nops - w2_nops)
#else
# define w3_nops 0
#endif
2016-01-25 05:14:50 +08:00
// The only critical timing parameter is the minimum pulse length of the "0"
// Warn or throw error if this timing can not be met with current F_CPU settings.
2019-08-31 02:19:03 +08:00
#define w_lowtime ((w1_nops + w_fixedlow) * 1000000) / (F_CPU / 1000)
#if w_lowtime > 550
# error "Light_ws2812: Sorry, the clock speed is too low. Did you set F_CPU correctly?"
#elif w_lowtime > 450
# warning "Light_ws2812: The timing is critical and may only work on WS2812B, not on WS2812(S)."
# warning "Please consider a higher clockspeed, if possible"
2016-01-25 05:14:50 +08:00
#endif
2019-08-31 02:19:03 +08:00
#define w_nop1 "nop \n\t"
#define w_nop2 "rjmp .+0 \n\t"
#define w_nop4 w_nop2 w_nop2
#define w_nop8 w_nop4 w_nop4
2016-01-25 05:14:50 +08:00
#define w_nop16 w_nop8 w_nop8
static inline void ws2812_sendarray_mask(uint8_t *data, uint16_t datlen, uint8_t masklo, uint8_t maskhi) {
uint8_t curbyte, ctr, sreg_prev;
2019-08-31 02:19:03 +08:00
sreg_prev = SREG;
cli();
while (datlen--) {
curbyte = (*data++);
asm volatile(" ldi %0,8 \n\t"
"loop%=: \n\t"
" out %2,%3 \n\t" // '1' [01] '0' [01] - re
2019-08-31 02:19:03 +08:00
#if (w1_nops & 1)
w_nop1
2016-01-25 05:14:50 +08:00
#endif
2019-08-31 02:19:03 +08:00
#if (w1_nops & 2)
w_nop2
2016-01-25 05:14:50 +08:00
#endif
2019-08-31 02:19:03 +08:00
#if (w1_nops & 4)
w_nop4
2016-01-25 05:14:50 +08:00
#endif
2019-08-31 02:19:03 +08:00
#if (w1_nops & 8)
w_nop8
2016-01-25 05:14:50 +08:00
#endif
2019-08-31 02:19:03 +08:00
#if (w1_nops & 16)
w_nop16
2016-01-25 05:14:50 +08:00
#endif
" sbrs %1,7 \n\t" // '1' [03] '0' [02]
" out %2,%4 \n\t" // '1' [--] '0' [03] - fe-low
" lsl %1 \n\t" // '1' [04] '0' [04]
2019-08-31 02:19:03 +08:00
#if (w2_nops & 1)
w_nop1
2016-01-25 05:14:50 +08:00
#endif
2019-08-31 02:19:03 +08:00
#if (w2_nops & 2)
w_nop2
2016-01-25 05:14:50 +08:00
#endif
2019-08-31 02:19:03 +08:00
#if (w2_nops & 4)
w_nop4
2016-01-25 05:14:50 +08:00
#endif
2019-08-31 02:19:03 +08:00
#if (w2_nops & 8)
w_nop8
2016-01-25 05:14:50 +08:00
#endif
2019-08-31 02:19:03 +08:00
#if (w2_nops & 16)
w_nop16
2016-01-25 05:14:50 +08:00
#endif
" out %2,%4 \n\t" // '1' [+1] '0' [+1] - fe-high
2019-08-31 02:19:03 +08:00
#if (w3_nops & 1)
w_nop1
2016-01-25 05:14:50 +08:00
#endif
2019-08-31 02:19:03 +08:00
#if (w3_nops & 2)
w_nop2
2016-01-25 05:14:50 +08:00
#endif
2019-08-31 02:19:03 +08:00
#if (w3_nops & 4)
w_nop4
2016-01-25 05:14:50 +08:00
#endif
2019-08-31 02:19:03 +08:00
#if (w3_nops & 8)
w_nop8
2016-01-25 05:14:50 +08:00
#endif
2019-08-31 02:19:03 +08:00
#if (w3_nops & 16)
w_nop16
2016-01-25 05:14:50 +08:00
#endif
" dec %0 \n\t" // '1' [+2] '0' [+2]
" brne loop%=\n\t" // '1' [+3] '0' [+4]
2019-08-31 02:19:03 +08:00
: "=&d"(ctr)
: "r"(curbyte), "I"(_SFR_IO_ADDR(PORTx_ADDRESS(WS2812_DI_PIN))), "r"(maskhi), "r"(masklo));
2019-08-31 02:19:03 +08:00
}
2016-01-25 05:14:50 +08:00
2019-08-31 02:19:03 +08:00
SREG = sreg_prev;
2016-01-25 05:14:50 +08:00
}