aboutsummaryrefslogtreecommitdiffstats
path: root/src/ws2812.c
blob: 4140fd6597783e5ffb194b6ad4b944ae7dc77008 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "ws2812.h"
#include "ws2812.pio.h"
#include "util.h"

#include "hardware/pio.h"
#include "hardware/clocks.h"

#define CYCLES_PER_BIT (ws2812_T1 + ws2812_T2 + ws2812_T3)

void
ws2812_init(struct ws2812 *pix, PIO pio, uint pin)
{
	pio_sm_config config;
	uint offset;
	uint sm;

	pix->pio = pio;
	pix->pin = pin;

	sm = CLAIM_UNUSED_SM(pio);
	pix->sm = sm;

	pio_gpio_init(pio, pin);
	pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true);

	offset = pio_add_program(pix->pio, &ws2812_program);
	config = ws2812_program_get_default_config(offset);
	sm_config_set_sideset_pins(&config, pin);
	sm_config_set_out_shift(&config, false, true, 24);
	sm_config_set_fifo_join(&config, PIO_FIFO_JOIN_TX);
	sm_config_set_clkdiv(&config,
		(float) clock_get_hz(clk_sys) / (800000 * CYCLES_PER_BIT));

	pio_sm_init(pio, sm, offset, &config);
	pio_sm_set_enabled(pio, sm, true);

	pix->init = true;
}

void
ws2812_put(struct ws2812 *pix, uint32_t rgb) {
	pio_sm_put_blocking(pix->pio, pix->sm, rgb << 8u);
}