Skip to content

Commit 30608fd

Browse files
committed
improved flasher.sendCommand()
1 parent 33739b0 commit 30608fd

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/flasher/flasher.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ type Flasher struct {
4141
// Also check if the version of the programmer protocol match the uploader
4242
func (flasher *Flasher) Hello() error {
4343
// "HELLO" command
44-
flasher.sendCommand(0x99, 0x11223344, 0x55667788, 0)
44+
if err := flasher.sendCommand(0x99, 0x11223344, 0x55667788, nil); err != nil {
45+
return err
46+
}
4547

4648
// Wait a bit
4749
time.Sleep(100 * time.Millisecond)
@@ -71,7 +73,9 @@ func (flasher *Flasher) Close() error {
7173
// Get maximum payload size for upload.
7274
func (flasher *Flasher) GetMaximumPayloadSize() (uint16, error) {
7375
// "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+
}
7579

7680
// Receive response
7781
res := make([]byte, 2)
@@ -84,7 +88,9 @@ func (flasher *Flasher) GetMaximumPayloadSize() (uint16, error) {
8488
// Read a block of flash memory
8589
func (flasher *Flasher) Read(address uint32, length uint32) ([]byte, error) {
8690
// "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+
}
8894

8995
// Receive response
9096
result := make([]byte, length)
@@ -104,10 +110,7 @@ func (flasher *Flasher) Read(address uint32, length uint32) ([]byte, error) {
104110
// Write a block of flash memory
105111
func (flasher *Flasher) Write(address uint32, buffer []byte) error {
106112
// "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 {
111114
return err
112115
}
113116

@@ -125,7 +128,9 @@ func (flasher *Flasher) Write(address uint32, buffer []byte) error {
125128
// Erase a block of flash memory
126129
func (flasher *Flasher) Erase(address uint32, length uint32) error {
127130
// "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+
}
129134

130135
// wait acknowledge
131136
ack := make([]byte, 2)
@@ -155,7 +160,7 @@ func (flasher *Flasher) serialFillBuffer(buffer []byte) error {
155160
return nil
156161
}
157162

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 {
159164
if err := binary.Write(flasher.port, binary.BigEndian, command); err != nil {
160165
return err
161166
}
@@ -165,9 +170,20 @@ func (flasher *Flasher) sendCommand(command byte, address uint32, val uint32, le
165170
if err := binary.Write(flasher.port, binary.BigEndian, val); err != nil {
166171
return err
167172
}
173+
var length uint16
174+
if payload == nil {
175+
length = 0
176+
} else {
177+
length = uint16(len(payload))
178+
}
168179
if err := binary.Write(flasher.port, binary.BigEndian, length); err != nil {
169180
return err
170181
}
182+
if payload != nil {
183+
if _, err := flasher.port.Write(payload); err != nil {
184+
return err
185+
}
186+
}
171187
return nil
172188
}
173189

0 commit comments

Comments
 (0)