@@ -41,7 +41,9 @@ type Flasher struct {
41
41
// Also check if the version of the programmer protocol match the uploader
42
42
func (flasher * Flasher ) Hello () error {
43
43
// "HELLO" command
44
- flasher .sendCommand (0x99 , 0x11223344 , 0x55667788 , 0 )
44
+ if err := flasher .sendCommand (0x99 , 0x11223344 , 0x55667788 , nil ); err != nil {
45
+ return err
46
+ }
45
47
46
48
// Wait a bit
47
49
time .Sleep (100 * time .Millisecond )
@@ -71,7 +73,9 @@ func (flasher *Flasher) Close() error {
71
73
// Get maximum payload size for upload.
72
74
func (flasher * Flasher ) GetMaximumPayloadSize () (uint16 , error ) {
73
75
// "MAX_PAYLOAD_SIZE" command
74
- flasher .sendCommand (0x50 , 0 , 0 , 0 )
76
+ if err := flasher .sendCommand (0x50 , 0 , 0 , nil ); err != nil {
77
+ return 0 , err
78
+ }
75
79
76
80
// Receive response
77
81
res := make ([]byte , 2 )
@@ -84,7 +88,9 @@ func (flasher *Flasher) GetMaximumPayloadSize() (uint16, error) {
84
88
// Read a block of flash memory
85
89
func (flasher * Flasher ) Read (address uint32 , length uint32 ) ([]byte , error ) {
86
90
// "FLASH_READ" command
87
- flasher .sendCommand (0x01 , address , length , 0 )
91
+ if err := flasher .sendCommand (0x01 , address , length , nil ); err != nil {
92
+ return nil , err
93
+ }
88
94
89
95
// Receive response
90
96
result := make ([]byte , length )
@@ -104,10 +110,7 @@ func (flasher *Flasher) Read(address uint32, length uint32) ([]byte, error) {
104
110
// Write a block of flash memory
105
111
func (flasher * Flasher ) Write (address uint32 , buffer []byte ) error {
106
112
// "FLASH_WRITE" command
107
- flasher .sendCommand (0x02 , address , 0 , uint16 (len (buffer )))
108
-
109
- // send payload
110
- if _ , err := flasher .port .Write (buffer ); err != nil {
113
+ if err := flasher .sendCommand (0x02 , address , 0 , buffer ); err != nil {
111
114
return err
112
115
}
113
116
@@ -125,7 +128,9 @@ func (flasher *Flasher) Write(address uint32, buffer []byte) error {
125
128
// Erase a block of flash memory
126
129
func (flasher * Flasher ) Erase (address uint32 , length uint32 ) error {
127
130
// "FLASH_ERASE" command
128
- flasher .sendCommand (0x03 , address , length , 0 )
131
+ if err := flasher .sendCommand (0x03 , address , length , nil ); err != nil {
132
+ return err
133
+ }
129
134
130
135
// wait acknowledge
131
136
ack := make ([]byte , 2 )
@@ -155,7 +160,7 @@ func (flasher *Flasher) serialFillBuffer(buffer []byte) error {
155
160
return nil
156
161
}
157
162
158
- func (flasher * Flasher ) sendCommand (command byte , address uint32 , val uint32 , length uint16 ) error {
163
+ func (flasher * Flasher ) sendCommand (command byte , address uint32 , val uint32 , payload [] byte ) error {
159
164
if err := binary .Write (flasher .port , binary .BigEndian , command ); err != nil {
160
165
return err
161
166
}
@@ -165,9 +170,20 @@ func (flasher *Flasher) sendCommand(command byte, address uint32, val uint32, le
165
170
if err := binary .Write (flasher .port , binary .BigEndian , val ); err != nil {
166
171
return err
167
172
}
173
+ var length uint16
174
+ if payload == nil {
175
+ length = 0
176
+ } else {
177
+ length = uint16 (len (payload ))
178
+ }
168
179
if err := binary .Write (flasher .port , binary .BigEndian , length ); err != nil {
169
180
return err
170
181
}
182
+ if payload != nil {
183
+ if _ , err := flasher .port .Write (payload ); err != nil {
184
+ return err
185
+ }
186
+ }
171
187
return nil
172
188
}
173
189
0 commit comments