|
| 1 | +/* |
| 2 | + FirmwareUploader |
| 3 | + Copyright (c) 2021 Arduino LLC. All right reserved. |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | +package flasher |
| 21 | + |
| 22 | +import ( |
| 23 | + "fmt" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/arduino/go-paths-helper" |
| 27 | + "go.bug.st/serial" |
| 28 | +) |
| 29 | + |
| 30 | +type CommandData struct { |
| 31 | + Command byte |
| 32 | + Address uint32 |
| 33 | + Value uint32 |
| 34 | + Payload []byte |
| 35 | +} |
| 36 | + |
| 37 | +type FlasherError struct { |
| 38 | + err string |
| 39 | +} |
| 40 | + |
| 41 | +func (e FlasherError) Error() string { |
| 42 | + return e.err |
| 43 | +} |
| 44 | + |
| 45 | +type Flasher interface { |
| 46 | + FlashFirmware(firmwareFile *paths.Path) error |
| 47 | + FlashCertificates(certificatePaths *paths.PathList) error |
| 48 | + Close() error |
| 49 | + |
| 50 | + hello() error |
| 51 | + write(address uint32, buffer []byte) error |
| 52 | + flashChunk(offset int, buffer []byte) error |
| 53 | + getMaximumPayloadSize() (uint16, error) |
| 54 | + serialFillBuffer(buffer []byte) error |
| 55 | + sendCommand(data CommandData) error |
| 56 | +} |
| 57 | + |
| 58 | +// http://www.ni.com/product-documentation/54548/en/ |
| 59 | +// Standard baud rates supported by most serial ports |
| 60 | +var baudRates = []int{ |
| 61 | + 115200, |
| 62 | + 57600, |
| 63 | + 56000, |
| 64 | + 38400, |
| 65 | +} |
| 66 | + |
| 67 | +func openSerial(portAddress string) (serial.Port, error) { |
| 68 | + var lastError error |
| 69 | + |
| 70 | + for _, baudRate := range baudRates { |
| 71 | + port, err := serial.Open(portAddress, &serial.Mode{BaudRate: baudRate}) |
| 72 | + if err != nil { |
| 73 | + lastError = err |
| 74 | + // Try another baudrate |
| 75 | + continue |
| 76 | + } |
| 77 | + |
| 78 | + if err := port.SetReadTimeout(30 * time.Second); err != nil { |
| 79 | + return nil, fmt.Errorf("Could not set timeout on serial port: %s", err) |
| 80 | + } |
| 81 | + |
| 82 | + return port, nil |
| 83 | + } |
| 84 | + |
| 85 | + return nil, lastError |
| 86 | +} |
0 commit comments