Skip to content

Commit b91a11e

Browse files
committed
Initial progress
0 parents  commit b91a11e

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 OpenChirp
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

main.go

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
// Copyright 2017 OpenChirp. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
//
5+
// March 13, 2017
6+
// Craig Hesling <[email protected]>
7+
8+
package main
9+
10+
import (
11+
"flag"
12+
"fmt"
13+
"io/ioutil"
14+
"log"
15+
16+
"os"
17+
18+
"github.com/jacobsa/go-serial/serial"
19+
"github.com/linux4life798/ccboot"
20+
)
21+
22+
const (
23+
// timeout in ms
24+
readTimeout = 1000
25+
)
26+
27+
var commands = []string{
28+
"sync",
29+
"ping",
30+
"getstatus",
31+
"getchipid",
32+
"bankerase",
33+
"reset",
34+
}
35+
36+
var verbose bool
37+
var portSpeed uint
38+
39+
func setupFlags() {
40+
flag.Usage = func() {
41+
fmt.Printf("Usage: ccbootutil [options] <portname> <command> [parameters]\n\n")
42+
fmt.Printf("Commands:\n")
43+
for _, cmd := range commands {
44+
fmt.Printf("\t%s\n", cmd)
45+
}
46+
fmt.Printf("\n")
47+
48+
fmt.Printf("Options:\n")
49+
flag.PrintDefaults()
50+
}
51+
flag.BoolVar(&verbose, "verbose", false, "Toggles the verbose setting")
52+
flag.UintVar(&portSpeed, "speed", 115200, "The serial baud rate to use")
53+
}
54+
55+
func main() {
56+
setupFlags()
57+
flag.Parse()
58+
59+
if len(flag.Args()) < 2 {
60+
flag.Usage()
61+
os.Exit(1)
62+
}
63+
64+
portname := flag.Args()[0]
65+
cmd := flag.Args()[1]
66+
67+
// Set up options.
68+
options := serial.OpenOptions{
69+
PortName: portname,
70+
BaudRate: 115200,
71+
DataBits: 8,
72+
StopBits: 1,
73+
MinimumReadSize: 1,
74+
InterCharacterTimeout: readTimeout,
75+
}
76+
77+
log.SetOutput(ioutil.Discard)
78+
if verbose {
79+
log.SetOutput(os.Stderr)
80+
}
81+
82+
// Open the port.
83+
log.Println("Opening Serial")
84+
port, err := serial.Open(options)
85+
if err != nil {
86+
log.Fatalf("Failed to open serial port: %v", err)
87+
}
88+
// Make sure to close it later.
89+
defer port.Close()
90+
91+
d := ccboot.NewDevice(port)
92+
93+
switch cmd {
94+
case "sync":
95+
// Synchronize
96+
log.Println("Synchronizing")
97+
if err = d.Sync(); err != nil {
98+
log.Printf("Error synchronizing device: %v\n", err)
99+
os.Exit(1)
100+
}
101+
log.Println("Synchronization success")
102+
case "ping":
103+
// Ping Device Bootloader
104+
log.Println("Pinging")
105+
err = d.Ping()
106+
if err != nil {
107+
log.Printf("Error pinging device: %s\n", err.Error())
108+
os.Exit(1)
109+
}
110+
log.Println("Ping success")
111+
case "getstatus":
112+
// Get Status
113+
log.Println("Getting status")
114+
status, err := d.GetStatus()
115+
if err != nil {
116+
fmt.Println() // maintain parsibility
117+
log.Printf("# Error - %s\n", err.Error())
118+
os.Exit(1)
119+
}
120+
fmt.Println(status.GetString())
121+
case "getchipid":
122+
// Get Chip ID
123+
log.Println("Getting chip ID")
124+
id, err := d.GetChipID()
125+
if err != nil {
126+
log.Printf("Error reading chip id: %s\n", err.Error())
127+
os.Exit(1)
128+
}
129+
fmt.Printf("0x%.8X\n", id)
130+
case "bankerase":
131+
// Bank Erase
132+
log.Println("Bank erasing")
133+
err = d.BankErase()
134+
if err != nil {
135+
log.Printf("Error bank erasing device: %s\n", err.Error())
136+
os.Exit(1)
137+
}
138+
log.Println("Bank erase success")
139+
case "reset":
140+
// Reset Device
141+
log.Println("Resetting device")
142+
err = d.Reset()
143+
if err != nil {
144+
log.Printf("Error resetting chip: %s\n", err.Error())
145+
os.Exit(1)
146+
}
147+
log.Println("Device reset")
148+
}
149+
150+
log.Printf("Exiting\n")
151+
}

0 commit comments

Comments
 (0)