File tree Expand file tree Collapse file tree
Open diff view settings
website/docs/developer/servers Expand file tree Collapse file tree
Open diff view settings
Original file line number Diff line number Diff line change 11import * as ds from "@devicescript/core"
22
3+ /**
4+ * SPI configuration options
5+ */
36export interface SPIConfig {
7+ /**
8+ * MISO pin
9+ */
410 miso ?: ds . InputPin
11+ /**
12+ * MOSI pin
13+ */
514 mosi ?: ds . OutputPin
15+ /**
16+ * SCK pin
17+ */
618 sck ?: ds . OutputPin
719 mode ?: number
20+ /**
21+ * Clock speed in Hz
22+ */
823 hz ?: number
924}
1025
@@ -23,6 +38,10 @@ function pinNum(p: ds.Pin) {
2338 return p ? p . gpio : - 1
2439}
2540
41+ /**
42+ * Configure the SPI bus
43+ * @param cfg a set of configuration options
44+ */
2645export function spiConfigure ( cfg : SPIConfig ) {
2746 ; ( ds as DsSpi ) . spiConfigure (
2847 pinNum ( cfg . miso ) ,
@@ -33,17 +52,28 @@ export function spiConfigure(cfg: SPIConfig) {
3352 )
3453}
3554
55+ /**
56+ * Write a buffer to the SPI bus
57+ */
3658export async function spiWrite ( buf : Buffer ) {
3759 await ( ds as DsSpi ) . spiXfer ( buf , null )
3860}
3961
62+ /**
63+ * Reads a buffer from the SPI bus
64+ */
4065export async function spiRead ( numbytes : number ) {
4166 const r = Buffer . alloc ( numbytes )
4267 await ( ds as DsSpi ) . spiXfer ( null , r )
4368 return r
4469}
4570
46- export async function spiXfer ( buf : Buffer ) {
71+ /**
72+ * Transfers a buffer to and from the SPI bus
73+ * @param buf buffer to send
74+ * @returns buffer received of the same size
75+ */
76+ export async function spiTransfer ( buf : Buffer ) {
4777 const r = Buffer . alloc ( buf . length )
4878 await ( ds as DsSpi ) . spiXfer ( buf , r )
4979 return r
Original file line number Diff line number Diff line change @@ -5,8 +5,45 @@ sidebar_position: 101
55
66# SPI
77
8- :::info
8+ SPI [ builtin package ] ( /developer/packages ) that exposes functions to read, write, transfer buffers over SPI.
99
10- TODO
10+ ## ` spiConfigure `
1111
12- :::
12+ The ` spiConfigure ` function is used to configure the SPI bus. It takes the pin configuration, frequency and mode.
13+
14+ ``` ts
15+ import { spiConfigure } from " @devicescript/spi"
16+
17+ await spiConfigure ({})
18+ ```
19+
20+ ## ` spiRead `
21+
22+ The ` spiRead ` function is used to read a buffer from the SPI bus.
23+
24+ ``` ts
25+ import { spiRead } from " @devicescript/spi"
26+
27+ const res = await spiRead (8 ) // read 8 bytes
28+ ```
29+
30+ ## ` spiWrite `
31+
32+ The ` spiWrite ` function is used to write a buffer to the SPI bus.
33+
34+ ``` ts
35+ import { spiWrite } from " @devicescript/spi"
36+
37+ await spiWrite (hex ` abcd ` )
38+ ```
39+
40+ ## ` spiTransfer `
41+
42+ The ` spiTransfter ` function is used to write and read buffers from and to the SPI bus.
43+ The read buffer has the same length as the write buffer.
44+
45+ ``` ts
46+ import { spiTransfer } from " @devicescript/spi"
47+
48+ const res = await spiTransfer (hex ` abcd ` )
49+ ```
You can’t perform that action at this time.
0 commit comments