-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathduart_tx.pio
62 lines (47 loc) · 1.92 KB
/
duart_tx.pio
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
61
62
.program duart_tx
; An 8n1 UART transmit program.
; SET & OUT pins should be set to TX+/TX-
set PINS, 0b10
set PINDIRS, 0b11
.wrap_target
out PINS, 2 [7] ; Shift 2 bits from OSR to the OUT pins, delay 7 cycles
.wrap
% c-sdk {
#include "hardware/clocks.h"
uint32_t txtab[256];
static inline int __time_critical_func(duart_tx_program_init)(PIO pio, uint sm, uint offset, uint txbase, uint baud) {
pio_sm_claim(pio, sm);
pio_sm_set_pins_with_mask(pio, sm, 3u << txbase, 3u << txbase);
pio_sm_set_pindirs_with_mask(pio, sm, 3u << txbase, 3u << txbase);
pio_gpio_init(pio, txbase);
pio_gpio_init(pio, txbase+1);
pio_sm_config c = duart_tx_program_get_default_config(offset);
sm_config_set_out_shift(&c, true, true, 24);
sm_config_set_set_pins(&c, txbase, 2);
sm_config_set_out_pins(&c, txbase, 2);
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
float div = (float)clock_get_hz(clk_sys) / (8 * baud);
sm_config_set_clkdiv(&c, div);
for(uint i = 0; i < 256; i++) {
txtab[i] = 0x55540002 |
((i & 0x01) ? (0b01 << 2) : (0b10 << 2)) |
((i & 0x02) ? (0b01 << 4) : (0b10 << 4)) |
((i & 0x04) ? (0b01 << 6) : (0b10 << 6)) |
((i & 0x08) ? (0b01 << 8) : (0b10 << 8)) |
((i & 0x10) ? (0b01 << 10) : (0b10 << 10)) |
((i & 0x20) ? (0b01 << 12) : (0b10 << 12)) |
((i & 0x40) ? (0b01 << 14) : (0b10 << 14)) |
((i & 0x80) ? (0b01 << 16) : (0b10 << 16));
}
pio_sm_init(pio, sm, offset, &c);
pio_sm_set_enabled(pio, sm, true);
return clock_get_hz(clk_sys) / div;
}
static inline void __time_critical_func(duart_tx_program_put)(PIO pio, uint sm, char c) {
pio_sm_put_blocking(pio, sm, txtab[(uint8_t)c]);
}
static inline void __time_critical_func(duart_tx_program_puts)(PIO pio, uint sm, const char *s) {
while (*s)
duart_tx_program_put(pio, sm, *s++);
}
%}