-
Notifications
You must be signed in to change notification settings - Fork 0
/
dshot.pio.h
61 lines (50 loc) · 1.77 KB
/
dshot.pio.h
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// -------------------------------------------------- //
// This file is autogenerated by pioasm; do not edit! //
// -------------------------------------------------- //
#pragma once
#if !PICO_NO_HARDWARE
#include "hardware/pio.h"
#endif
// ----- //
// dshot //
// ----- //
#define dshot_wrap_target 0
#define dshot_wrap 3
#define dshot_T1 3
#define dshot_T2 3
#define dshot_T3 2
static const uint16_t dshot_program_instructions[] = {
// .wrap_target
0x6121, // 0: out x, 1 side 0 [1]
0x1223, // 1: jmp !x, 3 side 1 [2]
0x1200, // 2: jmp 0 side 1 [2]
0xa242, // 3: nop side 0 [2]
// .wrap
};
#if !PICO_NO_HARDWARE
static const struct pio_program dshot_program = {
.instructions = dshot_program_instructions,
.length = 4,
.origin = -1,
};
static inline pio_sm_config dshot_program_get_default_config(uint offset) {
pio_sm_config c = pio_get_default_sm_config();
sm_config_set_wrap(&c, offset + dshot_wrap_target, offset + dshot_wrap);
sm_config_set_sideset(&c, 1, false, false);
return c;
}
#include "hardware/clocks.h"
static inline void dshot_program_init(PIO pio, uint sm, uint offset, uint pin, float freq) {
pio_gpio_init(pio, pin);
pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true);
pio_sm_config c = dshot_program_get_default_config(offset);
sm_config_set_sideset_pins(&c, pin);
sm_config_set_out_shift(&c, false, true, 16);
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
int cycles_per_bit = dshot_T1 + dshot_T2 + dshot_T3;
float div = clock_get_hz(clk_sys) / (freq * cycles_per_bit);
sm_config_set_clkdiv(&c, div);
pio_sm_init(pio, sm, offset, &c);
pio_sm_set_enabled(pio, sm, true);
}
#endif