-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
187 lines (171 loc) · 4.03 KB
/
main.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"encoding/hex"
"github.com/matiasinsaurralde/esp8266tool/fluepke-common"
"github.com/mikepb/go-serial"
"github.com/urfave/cli/v2"
"log"
"os"
"time"
)
const (
defaultBaudRate = 115200
logPrefix = "esp8266"
mac0reg = 0x3ff00050
mac1reg = 0x3ff00054
mac3reg = 0x3ff0005c
)
var (
portDevice string
)
// ESP8266 is the main data structure
type ESP8266 struct {
portDevice string
baudRate int
serialOpts serial.Options
serialPort *serial.Port
slipRW *common.SlipReadWriter
logger *log.Logger
}
// NewESP8266 creates a new ESP8266 object
func NewESP8266(portDevice string, baudRate int) *ESP8266 {
return &ESP8266{portDevice: portDevice, baudRate: baudRate}
}
// Connect sets the serial options and opens the port
func (e *ESP8266) Connect() (err error) {
// Set default options:
e.serialOpts = serial.RawOptions
e.serialOpts.BitRate = e.baudRate
e.serialOpts.Mode = serial.MODE_READ_WRITE
e.serialPort, err = e.serialOpts.Open(e.portDevice)
e.logger = log.New(os.Stdout, logPrefix, log.Ltime)
e.slipRW = common.NewSlipReadWriter(e.serialPort, e.logger)
return err
}
// Exec executes a command
func (e *ESP8266) Exec(cmd *common.Command) (*common.Response, error) {
var done bool
go func(cmd *common.Command) {
for !done {
e.slipRW.Write(cmd.ToBytes())
time.Sleep(100 * time.Millisecond)
}
}(cmd)
var rawData []byte
var err error
for !done {
rawData, err = e.slipRW.Read(3 * time.Second)
if err != nil {
continue
}
if len(rawData) == 0 {
continue
}
if rawData[1] != byte(cmd.Opcode) {
continue
}
done = true
}
res, err := common.NewResponse(rawData)
return res, err
}
// ReadRegister reads a register
func (e *ESP8266) ReadRegister(n uint32) (*common.Response, error) {
cmd := common.NewReadRegisterCommand(n)
return e.Exec(cmd)
}
// Sync executes the sync command as specified here: https://github.com/espressif/esptool/wiki/Serial-Protocol#initial-synchronisation
func (e *ESP8266) Sync() (*common.Response, error) {
e.SetDTR(false)
e.SetRTS(true)
time.Sleep(100 * time.Millisecond)
e.SetDTR(true)
e.SetRTS(false)
cmd := common.NewSyncCommand()
time.Sleep(100 * time.Millisecond)
return e.Exec(cmd)
}
// SetDTR sets DTR (Data Terminal Ready)
func (e *ESP8266) SetDTR(v bool) {
defer e.serialPort.Apply(&e.serialOpts)
if v {
e.serialOpts.DTR = serial.DTR_ON
return
}
e.serialOpts.DTR = serial.DTR_OFF
}
// SetRTS sets RTS (Ready To Send)
func (e *ESP8266) SetRTS(v bool) {
defer e.serialPort.Apply(&e.serialOpts)
if v {
e.serialOpts.RTS = serial.RTS_ON
return
}
e.serialOpts.RTS = serial.RTS_OFF
}
// ComputeMacAddr returns the device MAC address
func (e *ESP8266) ComputeMacAddr(mac0 [4]byte, mac1 [4]byte, mac3 [4]byte) (s string) {
macAddr := []byte{
mac3[2],
mac3[1],
mac3[0],
mac1[1],
mac1[0],
mac0[3],
}
for i, b := range macAddr {
if i >= 1 {
s += ":"
}
s += hex.EncodeToString([]byte{b})
}
return
}
func main() {
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "port",
Usage: "Serial port device",
Destination: &portDevice,
Required: true,
},
},
Name: "read_mac",
Usage: "Read MAC address from OTP ROM",
Action: func(c *cli.Context) error {
log.Println("Trying to establish connection with the device")
esp := NewESP8266(portDevice, defaultBaudRate)
err := esp.Connect()
if err != nil {
return err
}
log.Printf("Connection established with %s, baud rate is %d\n", esp.portDevice, esp.serialOpts.BitRate)
_, err = esp.Sync()
if err != nil {
panic(err)
}
log.Println("SYNC ok")
// Read registers:
mac0, err := esp.ReadRegister(mac0reg)
if err != nil {
panic(err)
}
mac1, err := esp.ReadRegister(mac1reg)
if err != nil {
panic(err)
}
mac3, err := esp.ReadRegister(mac3reg)
if err != nil {
panic(err)
}
macAddr := esp.ComputeMacAddr(mac0.Value, mac1.Value, mac3.Value)
log.Printf("MAC address is: %s\n", macAddr)
return nil
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}