-
Notifications
You must be signed in to change notification settings - Fork 17
/
tspi.nim
58 lines (42 loc) · 1.3 KB
/
tspi.nim
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
import nesper/spis
# define PIN_NUM_MISO 37
# define PIN_NUM_MOSI 35
# define PIN_NUM_CLK 36
# define PIN_NUM_CS 34
let
bus = HSPI.newSpiBus(miso=gpio_num_t(9), mosi=gpio_num_t(10), sclk=gpio_num_t(12), dma_channel=2, flags={MASTER})
var
dev: SpiDev =
bus.addDevice(commandlen = bits(3),
addresslen = bits(4),
mode=1, cs_io=gpio_num_t(23),
clock_speed_hz = 1_000_000,
queue_size = 10,
flags={HALFDUPLEX})
let tdata1 = [byte 1, 2]
let trn1 = dev.fullTrans(tdata1)
# read non-byte number of bits
var tdata2 = [byte 1, 2, 3]
var trn2 = dev.fullTrans(tdata2, rxlength=bits(20))
var trn3 = dev.fullTrans([byte 1, 2, 3, 4, 5])
var tdata4 = @[1'u8, 2, 3]
var trn4 = dev.fullTrans(tdata4)
var trn5 = dev.readTrans(bytes(3))
# this is an error: let trn5 = dev.readTrans(@[1'u8, 2, 3, 4, 5])
var trn6 = dev.writeTrans(@[1'u8, 2, 3, 4, 5])
echo "trn1: " & repr(trn1)
echo "trn2: " & repr(trn2)
echo "trn3: " & repr(trn3)
echo "trn4: " & repr(trn4)
echo "trn5: " & repr(trn5)
echo "trn5: " & repr(trn6)
# Example: spi poll fullTransmission
trn1.poll()
# Example spi queued fullTransaction
trn2.queue()
# Example aquire bus
withSpiBus(dev):
trn3.poll()
trn4.poll()
# Regular spi transmit
trn5.transmit()