You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 5, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: board/spi.md
+52-31
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,13 @@ When a slave device's chip select is 0 (low), then it communicates with the mast
9
9
10
10
Since the SS pins may be connected to slave chip select through a demultiplexer and thereby work as an address bus, slave devices are identified by an index in this API, rather than by SS pins. Also, since multiple SPI buses may be present on a board, these are identified by an index in this API. Implementations SHOULD encapsulate the mapping from SPI bus number and device number to the physical SPI pins.
11
11
12
-
This API uses a [`Buffer`](../README.md/#buffer) object for both read and write data.
12
+
Commmunication with SPI devices is defined in the terms of SPI transactions:
13
+
- configure SPI
14
+
- start the transaction
15
+
- data transfer (one or more times)
16
+
- close the transaction.
17
+
18
+
This API uses a [`Buffer`](../README.md/#buffer) object for both read and write data. An SPI transaction is started by invoking the [`open()`](#open) method that configures and starts SPI and returns a control object on which applications can invoke the [`transceive()`](#transceive) method multiple times, then are supposed to invoke [`close()`](#close) in order to close the SPI transaction.
13
19
14
20
<aname="apiobject"></a>
15
21
### The SPI API object
@@ -26,23 +32,7 @@ See also the [Web IDL](./webidl.md) definition.
26
32
27
33
<aname="open"></a>
28
34
#### The `SPI open(options)` method
29
-
Configures an SPI bus using data provided by the `options` argument. It runs the following steps:
30
-
- Let `spi` be an [`SPI`](#spi) object.
31
-
- If `options` is a dictionary and the `options.bus` property is a number between 0 and 127, let `spi.bus` be `options.bus`, otherwise select the platform default value, and if that is not available, set the value to 0.
32
-
- If `options.speed` is not a number, let `spi.speed` be 10. Otherwise, set `spi.speed` to the closest matching value that is lower than `options.speed` and is supported by the platform.
33
-
- If `options.msbFirst` is `false`, set `spi.msbFirst` to `false`, otherwise set it to `true`.
34
-
- If `options.bits` is in the set {1, 2, 4, 8, 16 }, then set `spi.bits` to `option.bits`, otherwise set it to the value 4.
35
-
- If `options.polarity` is 0, then set `spi.polarity` to 0, otherwise set `spi.polarity` to 2.
36
-
- If `options.phase` is 0, then set `spi.phase` to 0, otherwise set `spi.phase` to 1.
37
-
- Request the underlying platform to initialize the SPI `spi.bus` with `spi.speed` on the board. The implementation will use the board mapping from the value of `bus` to the set of physical pins used for the bus.
38
-
- In case of failure in any of the steps above, return `null`.
39
-
- Set `spi.frameGap` to 0. Request the underlying platform to provide the SPI inter-frame delay value expressed in nanoseconds and if the request successfully completes, then set `spi.frameGap` to that value.
40
-
- Set `spi.topology` to `"full-duplex"`. Request the underlying platform to provide the SPI transfer mode and if the request successfully completes, then set `spi.topology` to the corresponding value.
41
-
- Return `spi`.
42
-
43
-
<aname="spi"></a>
44
-
### The `SPI` interface
45
-
Represents the properties and methods that expose SPI functionality. The `SPI` object has the following read-only properties:
35
+
Configures an SPI bus using data provided by the `options` argument.
46
36
47
37
| Property | Type | Optional | Default value | Represents |
48
38
| --- | --- | --- | --- | --- |
@@ -53,11 +43,30 @@ Represents the properties and methods that expose SPI functionality. The `SPI` o
53
43
|`polarity`| long | yes | 0 | clock polarity, 0 or 2 |
54
44
|`phase`| long | yes | 0 | clock phase, 0 or 1 |
55
45
|`frameGap`| unsigned long | yes |`undefined`| inter-frame gap in nanoseconds |
56
-
|`topology`| string | yes |`undefined`| SPI master-slave transfer topology |
- If `options` is a dictionary and the `options.bus` property is `undefined`, then set `options.bus` to 0.
50
+
- Otherwise if `options.bus` is not a number between 0 and 127, throw `TypeError` and abort these steps.
51
+
- If `options.speed` is `undefined`, let `options.speed` be 10. Otherwise, set `options.speed` to the closest matching value that is lower than `options.speed` and is supported by the platform.
52
+
- If `options.msbFirst` is `undefined`, set it to `true`.
53
+
- If `options.bits` is not in the set {1, 2, 4, 8, 16 }, then set it to the value 4.
54
+
- If `options.polarity` is `undefined`, set it to 0. If it is not 0, set it to 2.
55
+
- If `options.phase` is `undefined`, set it to 0. If it is not 0, set it to 1.
56
+
- If `spi.frameGap` is defined, request the underlying platform to set the SPI inter-frame delay value expressed in nanoseconds. If the request fails or it is not supported, set `spi.frameGap` to `undefined`.
57
+
- If `spi.topology` is defined, use its value to initialize SPI on the underlying platform. If that fails, or it is not supported, set `spi.topology` to the default value `"full-duplex"`. This will define the behaviour of [`transceive()`](#transceive).
58
+
- Request the underlying platform to initialize SPI with `options`. The implementation will use the board mapping from the value of `options.bus` to the set of physical pins used for the bus.
59
+
- In case of failure in any of the steps above, throw `SystemError`.
60
+
- Let `spi` be an [`SPI`](#spi) object that controls the initialized SPI bus.
61
+
- Return `spi`.
62
+
63
+
<aname="spi"></a>
64
+
### The `SPI` interface
65
+
Exposes SPI functionality and represents one SPI transaction that has begun by calling [`open()`](#open). Applications can invoke the [`transceive()`](#transceive) method multiple times, then are supposed to invoke [`close()`](#close) in order to release the SPI bus.
57
66
58
-
| Method signature| Description |
67
+
| Method | Description |
59
68
| --- | --- |
60
-
|[`transceive(device, buffer)`](#transceive)| read or write device(s) |
69
+
|[`transceive()`](#transceive)| read or write device(s) |
61
70
|[`close()`](#close)| close the SPI bus |
62
71
63
72
The `bus` property denotes the SPI bus number between 0 and 127.
@@ -82,23 +91,35 @@ The sum of the `polarity` and `phase` property values provides the SPI mode (thi
82
91
83
92
The `frameGap` property denotes the inter-frame gap in milliseconds on the platforms this is supported. The default value is 0.
84
93
85
-
The `topology` property describes the SPI master-slave connection type. This value may be provided by implementations that support the feature, but it is not mandatory. Applications can check whether the property is defined. The values can be the following:
94
+
The `topology` property describes the SPI master-slave connection type. The values can be the following:
86
95
-`"full-duplex"`: the slave devices are connected to the master via separate SS (Slave Select) lines that each activate one slave device. Prior to communication, the master must activate one device by driving the corresponding SS to 0, then data transfer is bidirectional. This is the default value.
87
-
-`"single-read"`: the slaves are connected to the master as above. When a slave is activated, the data is read by the master.
88
-
-`"single-write"`: the slaves are connected to the master as above. The master can activate any number of SS lines and the data is read by all activated slaves.
89
-
-`"multiplexed"`: 4 SS lines are connected to a decoder that can activate up to 15 slave devices. This works as full-duplex.
90
-
-`"daisy-chain"`: the master uses one SS and one SCLK (clock) line for all slaves. The MOSI line from the master goes to the first slave's MOSI pin, the MISO line of that slave goes to the MOSI pin of the next slave, and so forth. The last slave's MISO line is connected to the master's MISO pin.
96
+
-`"read"`: the slaves are connected to the master as above. When a slave is activated, the data is read by the master.
97
+
-`"write"`: the slaves are connected to the master as above. The master can activate any number of SS lines and the data is written to all activated slaves.
98
+
-`"multiplexed"`: a number`n` of SS lines are connected to a decoder that can activate up to `2^n - 1` number of slave devices. This works as full-duplex.
99
+
-`"daisy-chain"`: the master uses one SS and one SCLK (clock) line for all slaves. The MOSI line from the master goes to the first slave's MOSI pin, the MISO line of that slave goes to the MOSI pin of the next slave, and so forth. The last slave's MISO line is connected to the master's MISO pin. This is also bidirectional.
91
100
92
101
<aname="transceive"></a>
93
-
#### The `transceive(device, buffer)` method
94
-
Writes a [`Buffer`](../README.md/#buffer)`buffer` using SPI to the slave identified by the `device` argument, and reads another [`Buffer`](../README.md/#buffer) from the slave device that is returned. The method runs the following steps:
95
-
- If `device` is not a number between 0 and 127, Throw `TypeError` and terminate these steps.
96
-
- Request the underlying platform to write the bytes specified in `buffer` to the specified device and read another [`Buffer`](../README.md/#buffer)`readBuffer`. The implementation maps the value of `device` to the physical SS (slave select) pins on the board. If the operation fails, throw `SystemError` and abort these steps.
102
+
#### The `transceive(target, buffer, direction)` method
103
+
Writes [`buffer`](../README.md/#buffer)`buffer` using SPI to the slave identified by the `target` argument, and reads from the slave device into a [`readBuffer`](../README.md/#buffer) that is returned. The method runs the following steps:
104
+
- If `target` is not a number between 0 and 127, throw `TypeError` and abort these steps. Depending on SPI topology, multiple slave devices may be possible to select. The value of `target` may denote one single address, or an index of a device, or a multicast address (e.g. `0b110` could enable slaves 2 and 1 and leave slave 0 off).
105
+
- Check the valid combinations of `this.topology` and `direction` values from the following table. If `direction` is `undefined` or `null`, let it take the default value.
In any other case, throw `SystemError` and abort these steps.
116
+
- If `direction` allows reading, let `readBuffer` be a new [`Buffer`](../README.md/#buffer), otherwise let `readBuffer` be `null`.
117
+
- Request the underlying platform to perform the data transfer, i.e. write the bytes specified in `writeBuffer` to the specified target if `direction` allows writing and read another [`Buffer`](../README.md/#buffer)`readBuffer` if `direction` allows reading. The implementation maps the value of `target` to the physical SS (slave select) pins on the board. If the operation fails, throw `SystemError` and abort these steps.
97
118
- Return `readBuffer`.
98
119
99
120
<aname="close"></a>
100
121
#### The `close()` method
101
-
Closes the current [`SPI`](#spi)bus and cancels all pending operations.
122
+
Closes the current [`SPI`](#spi)transaction and cancels all pending operations. The implementation SHOULD NOT explicitly reset or change the existing SPI configuration.
0 commit comments