-
Notifications
You must be signed in to change notification settings - Fork 6
/
def.go
406 lines (352 loc) · 9.56 KB
/
def.go
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package cyw43439
import (
"errors"
"strings"
"github.com/soypat/cyw43439/whd"
"golang.org/x/exp/constraints"
)
var ErrDataNotAvailable = errors.New("requested data not available")
type Function uint32
const (
// All SPI-specific registers.
FuncBus Function = 0b00
// Registers and memories belonging to other blocks in the chip (64 bytes max).
FuncBackplane Function = 0b01
// DMA channel 1. WLAN packets up to 2048 bytes.
FuncDMA1 Function = 0b10
FuncWLAN = FuncDMA1
// DMA channel 2 (optional). Packets up to 2048 bytes.
FuncDMA2 Function = 0b11
)
func (f Function) String() (s string) {
switch f {
case FuncBus:
s = "bus"
case FuncBackplane:
s = "backplane"
case FuncWLAN: // same as FuncDMA1
s = "wlan"
case FuncDMA2:
s = "dma2"
default:
s = "unknown"
}
return s
}
// Status supports status notification to the host after a read/write
// transaction over gSPI. This status notification provides information
// about packet errors, protocol errors, available packets in the RX queue, etc.
// The status information helps reduce the number of interrupts to the host.
// The status-reporting feature can be switched off using a register bit,
// without any timing overhead.
type Status uint32
func (s Status) String() (str string) {
if s == 0 {
return "no status"
}
if s.HostCommandDataError() {
str += "hostcmderr "
}
if s.DataUnavailable() {
str += "dataunavailable "
}
if s.IsOverflow() {
str += "overflow "
}
if s.IsUnderflow() {
str += "underflow "
}
if s.F2PacketAvailable() || s.F3PacketAvailable() {
str += "packetavail "
}
if s.F2RxReady() || s.F3RxReady() {
str += "rxready "
}
return str
}
// DataUnavailable returns true if requested read data is unavailable.
func (s Status) DataUnavailable() bool { return s&1 != 0 }
// IsUnderflow returns true if FIFO underflow occurred due to current (F2, F3) read command.
func (s Status) IsUnderflow() bool { return s&(1<<1) != 0 }
// IsOverflow returns true if FIFO overflow occurred due to current (F1, F2, F3) write command.
func (s Status) IsOverflow() bool { return s&(1<<2) != 0 }
// F2Interrupt returns true if F2 channel interrupt set.
func (s Status) F2Interrupt() bool { return s&(1<<3) != 0 }
// F2RxReady returns true if F2 FIFO is ready to receive data (FIFO empty).
func (s Status) F2RxReady() bool { return s&(1<<5) != 0 }
// F3RxReady returns true if F3 FIFO is ready to receive data (FIFO empty).
func (s Status) F3RxReady() bool { return s&0x40 != 0 }
// HostCommandDataError TODO document.
func (s Status) HostCommandDataError() bool { return s&0x80 != 0 }
// GSPIPacketAvailable notifies there is a packet available over gSPI.
func (s Status) GSPIPacketAvailable() bool { return s&0x0100 != 0 }
// F2PacketAvailable returns true if Packet is available/ready in F2 TX FIFO.
func (s Status) F2PacketAvailable() bool { return s&(1<<8) != 0 }
// F3PacketAvailable returns true if Packet is available/ready in F3 TX FIFO.
func (s Status) F3PacketAvailable() bool { return s&0x00100000 != 0 }
// F2PacketAvailable returns F2 packet length.
func (s Status) F2PacketLength() uint16 {
const mask = 1<<11 - 1
return uint16(s>>9) & mask
}
// F3PacketAvailable returns F3 packet length.
func (s Status) F3PacketLength() uint16 {
const mask = 1<<11 - 1
return uint16(s>>21) & mask
}
type Interrupts uint16
func (Int Interrupts) IsBusOverflowedOrUnderflowed() bool {
return Int&(whd.F2_F3_FIFO_RD_UNDERFLOW|whd.F2_F3_FIFO_WR_OVERFLOW|whd.F1_OVERFLOW) != 0
}
func (Int Interrupts) IsF2Available() bool {
return Int&(whd.F2_PACKET_AVAILABLE) != 0
}
func (Int Interrupts) IsDataUnavailable() bool {
return Int&(whd.DATA_UNAVAILABLE) != 0
}
func (Int Interrupts) String() (s string) {
if Int == 0 {
return "no interrupts"
}
for i := 0; Int != 0; i++ {
if Int&1 != 0 {
s += irqmask(1<<i).String() + " "
}
Int >>= 1
}
return s
}
func GetCLM(firmware []byte) []byte {
clmAddr := alignup(uint32(len(firmware)), 512)
if uint32(cap(firmware)) < clmAddr+clmLen {
panic("firmware slice too small for CLM")
}
return firmware[clmAddr : clmAddr+clmLen]
}
var errFirmwareValidationFailed = errors.New("firmware validation failed")
func getFWVersion(src string) (string, error) {
begin := strings.LastIndex(src, "Version: ")
if begin == -1 {
return "", errors.New("FW version not found")
}
end := strings.Index(src[begin:], "\x00")
if end == -1 {
return "", errors.New("FW version not found")
}
fwVersion := src[begin : begin+end]
// if verbose_debug {
// println("got version", fwVersion)
// }
return fwVersion, nil
}
// errjoion returns an error that wraps the given errors.
// Any nil error values are discarded.
// errjoion returns nil if every value in errs is nil.
// The error formats as the concatenation of the strings obtained
// by calling the Error method of each element of errs, with a newline
// between each string.
//
// A non-nil error returned by errjoion implements the Unwrap() []error method.
func errjoin(errs ...error) error {
n := 0
for _, err := range errs {
if err != nil {
n++
}
}
if n == 0 {
return nil
}
e := &joinError{
errs: make([]error, 0, n),
}
for _, err := range errs {
if err != nil {
e.errs = append(e.errs, err)
}
}
return e
}
//go:generate stringer -type=irqmask -output=interrupts_string.go -trimprefix=irq
type irqmask uint16
const (
irqDATA_UNAVAILABLE irqmask = 0x0001 // Requested data not available; Clear by writing a "1"
irqF2_F3_FIFO_RD_UNDERFLOW irqmask = 0x0002
irqF2_F3_FIFO_WR_OVERFLOW irqmask = 0x0004
irqCOMMAND_ERROR irqmask = 0x0008 // Cleared by writing 1.
irqDATA_ERROR irqmask = 0x0010 // Cleared by writing 1.
irqF2_PACKET_AVAILABLE irqmask = 0x0020
irqF3_PACKET_AVAILABLE irqmask = 0x0040
irqF1_OVERFLOW irqmask = 0x0080 // Due to last write. Bkplane has pending write requests.
irqMISC_INTR0 irqmask = 0x0100
irqMISC_INTR1 irqmask = 0x0200
irqMISC_INTR2 irqmask = 0x0400
irqMISC_INTR3 irqmask = 0x0800
irqMISC_INTR4 irqmask = 0x1000
irqF1_INTR irqmask = 0x2000
irqF2_INTR irqmask = 0x4000
irqF3_INTR irqmask = 0x8000
)
// https://github.com/embassy-rs/embassy/blob/26870082427b64d3ca42691c55a2cded5eadc548/cyw43/src/lib.rs#L153
type powerManagementMode uint8
const (
// Custom, officially unsupported mode. Use at your own risk.
// All power-saving features set to their max at only a marginal decrease in power consumption
// as oppposed to `Aggressive`.
pmSuperSave powerManagementMode = iota
// pmAggressive power saving mode.
pmAggressive
// The default mode.
pmPowerSave
// pmPerformance is prefered over power consumption but still some power is conserved as opposed to
// `None`.
pmPerformance
// Unlike all the other PM modes, this lowers the power consumption at all times at the cost of
// a much lower throughput.
pmThroughputThrottling
// No power management is configured. This consumes the most power.
pmNone
)
func (pm powerManagementMode) IsValid() bool {
return pm <= pmNone
}
func (pm powerManagementMode) String() string {
switch pm {
case pmSuperSave:
return "SuperSave"
case pmAggressive:
return "Aggressive"
case pmPowerSave:
return "PowerSave"
case pmPerformance:
return "Performance"
case pmThroughputThrottling:
return "ThroughputThrottling"
case pmNone:
return "None"
default:
return "unknown"
}
}
func (pm powerManagementMode) sleep_ret_ms() uint16 {
switch pm {
case pmSuperSave:
return 2000
case pmAggressive:
return 2000
case pmPowerSave:
return 200
case pmPerformance:
return 20
default: // ThroughputThrottling, None
return 0 // value doesn't matter
}
}
func (pm powerManagementMode) beacon_period() uint8 {
switch pm {
case pmSuperSave:
return 255
case pmAggressive:
return 1
case pmPowerSave:
return 1
case pmPerformance:
return 1
default: // ThroughputThrottling, None
return 0 // value doesn't matter
}
}
func (pm powerManagementMode) dtim_period() uint8 {
switch pm {
case pmSuperSave:
return 255
case pmAggressive:
return 1
case pmPowerSave:
return 1
case pmPerformance:
return 1
default: // ThroughputThrottling, None
return 0 // value doesn't matter
}
}
func (pm powerManagementMode) assoc() uint8 {
switch pm {
case pmSuperSave:
return 255
case pmAggressive:
return 10
case pmPowerSave:
return 10
case pmPerformance:
return 1
default: // ThroughputThrottling, None
return 0 // value doesn't matter
}
}
// mode returns the WHD's internal mode number.
func (pm powerManagementMode) mode() uint8 {
switch pm {
case pmThroughputThrottling:
return 1
case pmNone:
return 0
default:
return 2
}
}
type joinError struct {
errs []error
}
func (e *joinError) Error() string {
var b []byte
for i, err := range e.errs {
if i > 0 {
b = append(b, '\n')
}
b = append(b, err.Error()...)
}
return string(b)
}
func (e *joinError) Unwrap() []error {
return e.errs
}
type _uinteger = interface {
~uint8 | ~uint16 | ~uint32 | ~uint64 | uintptr
}
type _integer = interface {
~int | _uinteger
}
func max[T constraints.Integer](a, b T) T {
if a > b {
return a
}
return b
}
func min[T constraints.Integer](a, b T) T {
if a < b {
return a
}
return b
}
//go:inline
func b2u32(b bool) uint32 {
if b {
return 1
}
return 0
}
// swap16 swaps lowest 16 bits with highest 16 bits of a uint32.
//
//go:inline
func swap16(b uint32) uint32 {
return (b >> 16) | (b << 16)
}
func swap16be(b uint32) uint32 {
b = swap16(b)
b0 := b & 0xff
b1 := (b >> 8) & 0xff
b2 := (b >> 16) & 0xff
b3 := (b >> 24) & 0xff
return b0<<24 | b1<<16 | b2<<8 | b3
}