-
Notifications
You must be signed in to change notification settings - Fork 0
/
embedded.wit
364 lines (302 loc) · 11.5 KB
/
embedded.wit
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/// Embedded APIs.
///
/// These APIs are based on [embedded-hal].
///
/// TODO?
/// - embedded-hal-bus: Sharing SPI and I2C buses
/// - embedded-can: Controller Area Network (CAN)
///
/// [embedded-hal]: https://crates.io/crates/embedded-hal
package sketch:embedded@0.0.0;
/// Inter-Integrated Circuit (I²C).
interface i2c {
/// An address value, in either 7-bit or 10-bit form, depending on the device.
type address = u16;
/// Operation errors.
variant error-code {
/// Bus error occurred. e.g. A START or a STOP condition is detected and
/// is not located after a multiple of 9 SCL clock pulses.
bus,
/// The arbitration was lost, e.g. electrical problems with the clock signal.
arbitration-loss,
/// A bus operation was not acknowledged, e.g. due to the addressed
/// device not being available on the bus or the device not being ready
/// to process requests at the moment.
no-acknowledge(no-acknowledge-source),
/// The peripheral receive buffer was overrun.
overrun,
/// A different error occurred.
other,
}
/// No-acknowledge error source.
///
/// In cases where it is possible, a device should indicate if a no
/// acknowledge response was received to an address versus a no acknowledge
/// to a data byte. Where it is not possible to differentiate, Unknown
/// should be indicated.
enum no-acknowledge-source {
/// The device did not acknowledge its address. The device may be
/// missing.
address,
/// The device did not acknowledge the data. It may not be ready to
/// process requests at the moment.
data,
/// Either the device did not acknowledge its address or the data, but
/// it is unknown which.
unknown,
}
/// An operation used by the `transaction` method.
variant operation {
/// Read the give number of bytes.
read(u64),
/// Write the given bytes.
write(list<u8>)
}
resource i2c {
/// Execute the provided `operation`s on the I²C bus.
transaction: func(
address: address,
operations: list<operation>
) -> result<list<list<u8>>, error-code>;
/// Reads `len` bytes from address `address`.
read: func(address: address, len: u64) -> result<list<u8>, error-code>;
/// Writes bytes to target with address `address`.
write: func(address: address, data: list<u8>) -> result<_, error-code>;
/// Writes bytes to address `address` and then reads `read-len` bytes
/// in a single transaction.
write-read: func(
address: address,
write: list<u8>,
read-len: u64,
) -> result<_, error-code>;
}
}
/// Digital I/O, for example GPIO pins.
interface digital {
/// Operation errors.
enum error-code {
/// An error occurred.
other,
}
/// Digital output pin state.
enum pin-state {
low,
high,
}
/// Single digital input pin.
resource input-pin {
/// Is the input pin low?
is-low: func() -> result<bool, error-code>;
/// Is the input pin high?
is-high: func() -> result<bool, error-code>;
/// Wait until the pin is high. If it is already high, resolve
/// immediately.
wait-for-high: func() -> result<_, error-code>;
/// Wait until the pin is low. If it is already low, resolve
/// immediately.
wait-for-low: func() -> result<_, error-code>;
/// Wait for the pin to undergo a transition from low to high.
///
/// If the pin is already high, this does *not* resolve immediately,
/// it’ll wait for the pin to go low and then high again.
wait-for-rising-edge: func() -> result<_, error-code>;
/// Wait for the pin to undergo a transition from high to low.
///
/// If the pin is already low, this does *not* return immediately,
/// it’ll wait for the pin to go high and then low again.
wait-for-falling-edge: func() -> result<_, error-code>;
/// Wait for the pin to undergo any transition, i.e low to high OR high
/// to low.
wait-for-any-edge: func() -> result<_, error-code>;
}
/// Single digital input pin.
resource output-pin {
/// Drives the pin low.
set-low: func() -> result<_, error-code>;
/// Drives the pin high.
set-high: func() -> result<_, error-code>;
/// Drives the pin high or low depending on the provided value.
set-state: func(state: pin-state) -> result<_, error-code>;
}
/// Push-pull output pin that can read its output state.
resource stateful-output-pin {
/// Is the pin in drive high mode?
is-set-high: func() -> result<bool, error-code>;
/// Is the pin in drive low mode?
is-set-low: func() -> result<bool, error-code>;
/// Toggle pin output.
toggle: func() -> result<_, error-code>;
}
}
/// Delays.
interface delay {
/// Delay with up to nanosecond precision.
resource delay {
/// Pauses execution for at minimum `ns` nanoseconds. Pause can be
/// longer if the implementation requires it due to precision/timing
/// issues.
delay-ns: func(ns: u32);
}
}
/// Pulse Width Modulation (PWM).
interface pwm {
/// Operation errors.
enum error-code {
/// An error occurred.
other,
}
/// Single PWM channel / pin.
resource set-duty-cycle {
/// Get the maximum duty cycle value.
///
/// This value corresponds to a 100% duty cycle.
max-duty-cycle: func() -> u16;
/// Set the duty cycle to `duty / max_duty`.
///
/// Traps if the duty cycle value is greater than the maximum duty
/// cycle value, as reported by `max-duty-cycle`.
///
/// Passing the value 0 turns the duty cycle to always inactive.
/// Passing the value returned by `max-duty-cycle` sets the duty cycle
/// to always acctive.
set-duty-cycle: func(duty: u16) -> result<_, error-code>;
}
}
/// Serial Peripheral Interface (SPI) controller mode.
///
/// This specifiation follows [OSHWA's recommended terminology].
///
/// [OSHWA's recommended terminology]: https://www.oshwa.org/a-resolution-to-redefine-spi-signal-names/
interface spi {
/// SPI mode.
record mode {
/// Clock polarity.
polarity: polarity,
// Clock phase.
phase: phase,
}
/// Clock polarity.
enum polarity {
/// Clock signal low when idle.
idle-low,
/// Clock signal high when idle.
idle-high,
}
/// Clock phase.
enum phase {
/// Data in “captured” on the first clock transition.
capture-on-first-transition,
/// Data in “captured” on the second clock transition.
capture-on-second-transition,
}
/// SPI error kind.
enum error-code {
/// The peripheral receive buffer was overrun.
overrun,
/// Multiple devices on the SPI bus are trying to drive the chip
/// select pin.
mode-fault,
/// Received data does not conform to the peripheral configuration.
frame-format,
/// An error occurred while asserting or deasserting the
/// Chip Select pin.
chip-select-fault,
/// A different error occurred.
other,
}
/// Word size.
///
/// TODO: Support up to `u16` word sizes?
type word = u8;
/// SPI transaction operation.
///
/// This allows composition of SPI operations into a single bus transaction.
variant operation {
/// Read data.
read(u64),
/// Write data from the provided list, discarding read data.
write(list<word>),
/// Read data, while writing data from the buffer.
transfer(tuple<u64, list<word>>),
/// Delay for at least the specified number of nanoseconds.
delay-ns(u32),
}
/// Helper for CPOL = 0, CPHA = 0.
mode0: func() -> mode;
/// Helper for CPOL = 0, CPHA = 1.
mode1: func() -> mode;
/// Helper for CPOL = 1, CPHA = 0.
mode2: func() -> mode;
/// Helper for CPOL = 1, CPHA = 1.
mode3: func() -> mode;
/// SPI bus.
///
/// `bus` represents exclusive ownership over the whole SPI bus, with
/// serial clock (SCK), peripheral in/controller out (PICO), and
/// peripheral out/controller in (POCI) pins.
resource bus {
/// Read words from the peripheral.
///
/// The word value sent on PICO during reading is
/// implementation-defined, typically 0x00, 0xFF, or configurable.
///
/// Implementations are allowed to return before the operation is complete.
read: func(len: u64) -> result<list<word>, error-code>;
/// Write `words` to the peripheral, ignoring all the incoming words.
///
/// Implementations are allowed to return before the operation is complete.
write: func(words: list<word>) -> result<_, error-code>;
/// Write and read simultaneously. `write` is written to the peripheral
/// on PICO and words received on POCI are returned.
///
/// It is allowed for `read-len` and `write`'s length to be different,
/// even zero length. The transfer runs for `max(read-len, write.len())`
/// words. If `read-len` is shorter, incoming words after `read-len` has
/// been filled will be discarded. If `write` is shorter, the value of
/// words sent in PICO after all `write` has been sent is
/// implementation-defined, typically `0x00`, `0xFF`, or configurable.
///
/// Implementations are allowed to return before the operation is complete.
transfer: func(
read-len: u64,
write: list<word>
) -> result<list<word>, error-code>;
/// Wait until all operations have completed and the bus is idle.
flush: func() -> result<_, error-code>;
}
/// SPI device.
///
/// `device` represents ownership over a single SPI device on a (possibly
/// shared) bus, selected with a CS (Chip Select) pin.
resource device {
/// Perform a transaction against the device.
///
/// - Locks the bus
/// - Asserts the CS (Chip Select) pin.
/// - Performs all the operations.
/// - Flushes the bus.
/// - Deasserts the CS pin.
/// - Unlocks the bus.
///
/// The locking mechanism is implementation-defined. The only
/// requirement is it must prevent two transactions from executing
/// concurrently against the same bus. Examples of implementations are:
/// critical sections, blocking mutexes, returning an error or
/// panicking if the bus is already busy. On bus errors the
/// implementation should try to deassert CS. If an error occurs while
/// deasserting CS the bus error should take priority as the return
/// value.
transaction: func(
operations: list<operation>
) -> result<list<list<word>>, error-code>;
/// Do a read within a transaction.
read: func(len: u64) -> result<list<word>, error-code>;
/// Do a write within a transaction.
write: func(buf: list<word>) -> result<_, error-code>;
/// Do a transfer within a transaction.
transfer: func(
read-len: u64,
write: list<word>
) -> result<list<word>, error-code>;
}
}