Есть у меня «умные» RGB-светодиоды — WS2812. Сейчас тыкаю STM32 с libopencm3.
Проблема в том, что светодиоды светят только белым (255, 255, 255). Полагаю, что все нули, которые я им посылаю, интерпретируются как единицы. Вероятно, у меня какие-то проблемы с таймингами.
Светодиоды запитаны от блока питания 5V, микроконтроллер от 3.3V, земля общая.
Код:
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
int t0h = 3; // 0.375us (0.35±0.15us datasheet)
int t1h = 6; // 0.750us (0.70±0.15us datasheet)
int t0l = 7; // 0.875us (0.80±0.15us datasheet)
int t1l = 5; // 0.625us (0.60±0.15us datasheet)
void clock_setup() {
rcc_clock_setup_in_hse_8mhz_out_72mhz();
/* Enable GPIOA clock. */
rcc_periph_clock_enable(RCC_GPIOA);
}
void gpio_setup() {
/* Set GPIO3 (in GPIO port A) to 'output push-pull'. */
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL, GPIO3);
}
void sleep(int cycles) {
int i;
for (i = 0; i < cycles; i++)
__asm__("nop");
}
void send_h() {
gpio_set(GPIOA, GPIO3);
sleep(t1h);
gpio_clear(GPIOA, GPIO3);
sleep(t1l);
}
void send_l() {
gpio_set(GPIOA, GPIO3);
sleep(t0h);
gpio_clear(GPIOA, GPIO3);
sleep(t0l);
}
void main() {
clock_setup();
gpio_setup();
while (1) {
int led, bit;
for (led = 0; led < 5; led++) {
// green
for (bit = 0; bit < 8; bit++) {
send_l();
}
// red
for (bit = 0; bit < 8; bit++) {
send_h();
}
// blue
for (bit = 0; bit < 8; bit++) {
send_h();
}
}
sleep(400); // tres
}
}
