-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys_nvm.go
60 lines (49 loc) · 1.55 KB
/
sys_nvm.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
package rn2483
import (
"bytes"
"fmt"
)
const (
// UserNVMStart is the address of the first byte in user NVM that can be addressed
UserNVMStart uint16 = 0x300
// UserNVMEnd is the last address in user NVM that can be addressed
UserNVMEnd uint16 = 0x3FF
)
var (
// UserNVMLength is the number of bytes of user NVM that can be addressed
UserNVMLength = (UserNVMEnd + 1) - UserNVMStart
)
// WriteNVM writes a single byte of data to user NVM
func (d *Device) WriteNVM(address uint16, value byte) error {
return d.ExecuteCommandCheckedStrict("sys set nvm %s %s", UInt16ToHex(address), ByteToHex(value))
}
// ReadNVM reads a single byte of data from user NVM
func (d *Device) ReadNVM(address uint16) (byte, error) {
line, err := d.ExecuteCommandChecked("sys get nvm %s", UInt16ToHex(address))
if err != nil {
return 0, err
}
return HexToByte(PadHexToEvenLength(line))
}
// ReadNVM reads a block of data from user NVM
func ReadNVM(d *Device, start, amount uint16) ([]byte, error) {
var buffer bytes.Buffer
for i := start; i < (start + amount); i++ {
value, err := d.ReadNVM(i)
if err != nil {
return nil, fmt.Errorf("error reading NVM at %s: %w", UInt16ToHex(i), err)
}
buffer.WriteByte(value)
}
return buffer.Bytes(), nil
}
// WriteNVM writes a block of data to user NVM
func WriteNVM(d *Device, address uint16, data []byte) error {
for i, b := range data {
writeAddress := address + uint16(i)
if err := d.WriteNVM(writeAddress, b); err != nil {
return fmt.Errorf("error writing NVM at %s: %w", UInt16ToHex(writeAddress), err)
}
}
return nil
}