Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0f568ac

Browse filesBrowse files
committed
barebone docs on spi
1 parent b61d2f4 commit 0f568ac
Copy full SHA for 0f568ac

2 files changed

+71-4Lines changed: 71 additions & 4 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/spi/src/spi.ts‎

Copy file name to clipboardExpand all lines: packages/spi/src/spi.ts
+31-1Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
import * as ds from "@devicescript/core"
22

3+
/**
4+
* SPI configuration options
5+
*/
36
export 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+
*/
2645
export 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+
*/
3658
export 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+
*/
4065
export 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
Collapse file

‎website/docs/developer/servers/spi.mdx‎

Copy file name to clipboardExpand all lines: website/docs/developer/servers/spi.mdx
+40-3Lines changed: 40 additions & 3 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff 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+
```

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.