/
hal_flash.go
157 lines (124 loc) · 3.05 KB
/
hal_flash.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
package mshal
import (
"encoding/binary"
)
func (h *HAL) ms2130enableSPI(enable bool) error {
value := byte(0x00)
if enable {
if h.ms2130spiEnabled == 1 {
return nil
}
/* Configure GPIO */
output := byte(1<<2 | 1<<3 | 1<<4)
input := byte(1 << 5)
if _, _, err := h.GPIOUpdate(output, 0, output, input); err != nil {
return err
}
value = byte(0x10)
} else {
if h.ms2130spiEnabled == 0 {
return nil
}
}
/* Configure pin mux */
_, err := h.MemoryRegionGet(MemoryRegionRAM).Access(true, 0xf01f, []byte{value})
if enable {
h.ms2130spiEnabled = 1
} else {
h.ms2130spiEnabled = 0
}
return err
}
type romFlashMemoryRegion struct {
hal *HAL
flashReadBufferValid bool
flashReadBufferPage uint16
}
func (h *HAL) memoryRegionFlash() MemoryRegion {
return regionWrapCompleteIO(&romFlashMemoryRegion{
hal: h,
})
}
func (r romFlashMemoryRegion) GetLength() int {
return 0x10000
}
func (r *romFlashMemoryRegion) Access(write bool, addr int, buf []byte) (int, error) {
if err := r.hal.ms2130enableSPI(true); err != nil {
return 0, err
}
if write {
r.flashReadBufferValid = false
if addr == 0 {
/* Erase the flash first */
var out [8]byte
out[0] = 0xfe
if _, err := r.hal.ROMExchangeReport(out[:]); err != nil {
return 0, err
}
}
/* Setup flash write: f801aaaaaabbbb00 (aaaaaa=address, bbbb=blocksize) *
* Write to the buffer: f800cccccccccc (cccccccccccc=data) */
if len(buf) > 256 {
buf = buf[:256]
}
var out [8]byte
out[0] = 0xf8
out[1] = 0x01
out[2] = byte(addr >> 16)
out[3] = byte(addr >> 8)
out[4] = byte(addr >> 0)
binary.BigEndian.PutUint16(out[5:], uint16(len(buf)))
if _, err := r.hal.ROMExchangeReport(out[:]); err != nil {
return 0, err
}
written := 0
for len(buf) > 0 {
out[0] = 0xf8
out[1] = 0x00
n := copy(out[2:], buf)
if _, err := r.hal.ROMExchangeReport(out[:]); err != nil {
return written, err
}
written += n
buf = buf[n:]
}
return written, nil
}
flashPage := uint16(addr >> 8)
flashOffset := uint8(addr & 0xff)
/* Read from flash to buffer: f701aaaaaabbbb00 (aaaaaa=addr, bbbb=len to read)
* Read from buffer to hostt: f700000000aaaa00 (aaaa=offset) */
if !r.flashReadBufferValid || r.flashReadBufferPage != flashPage {
var out [8]byte
out[0] = 0xf7
out[1] = 0x01
binary.BigEndian.PutUint16(out[2:], flashPage)
binary.BigEndian.PutUint16(out[5:], 256)
if _, err := r.hal.ROMExchangeReport(out[:]); err != nil {
return 0, err
}
r.flashReadBufferValid = true
r.flashReadBufferPage = flashPage
}
var out [8]byte
out[0] = 0xf7
binary.BigEndian.PutUint16(out[5:], uint16(flashOffset))
in, err := r.hal.ROMExchangeReport(out[:])
if err != nil {
return 0, err
}
maxLen := 0x100 - int(flashOffset)
if len(buf) > maxLen {
buf = buf[:maxLen]
}
return copy(buf, in), nil
}
func (r romFlashMemoryRegion) GetParent() (MemoryRegion, int) {
return nil, 0
}
func (r romFlashMemoryRegion) GetName() MemoryRegionNameType {
return MemoryRegionFLASH
}
func (r romFlashMemoryRegion) GetAlignment() int {
return 1
}