-
Notifications
You must be signed in to change notification settings - Fork 4
/
usart.go
138 lines (124 loc) · 2.69 KB
/
usart.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
// Copyright 2022 The Embedded Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package owdci provides implementation of onewire.DCI interface.
package owdci
import (
"io"
"time"
"github.com/embeddedgo/onewire"
"github.com/embeddedgo/stm32/hal/usart"
)
// USART wraps an usart.Driver to implements the onewire.DCI.
type USART usart.Driver
func usartDrv(dci *USART) *usart.Driver { return (*usart.Driver)(dci) }
// SetupUSART configures d to be used as onewire.DCI.
func SetupUSART(d *usart.Driver) *USART {
d.Setup(usart.Word8b|usart.HalfDuplex, 115200)
p := d.Periph()
p.SetConf3(p.Conf3() | usart.OneBit) // disables noise detection
d.EnableRx(32)
d.EnableTx()
return (*USART)(d)
}
func checkErr(d *usart.Driver, err error) error {
if err == nil {
return nil
}
d.DiscardRx()
return err
}
func (dci *USART) Reset() error {
d := usartDrv(dci)
d.Periph().SetBaudrate(9600)
err := d.WriteByte(0xf0)
if err != nil {
return err
}
d.SetReadTimeout(time.Second)
r, err := d.ReadByte()
if err != nil {
return err
}
if r == 0xf0 {
return onewire.ErrNoResponse
}
d.SetBaudrate(115200)
return nil
}
func sendRecvSlot(d *usart.Driver, slot byte) (byte, error) {
if err := d.WriteByte(slot); err != nil {
d.DiscardRx()
return 0, err
}
d.SetReadTimeout(time.Second)
b, err := d.ReadByte()
return b, checkErr(d, err)
}
func sendRecv(d *usart.Driver, slots *[8]byte) error {
if _, err := d.Write(slots[:]); err != nil {
d.DiscardRx()
return err
}
d.SetReadTimeout(time.Second)
_, err := io.ReadFull(d, slots[:])
return checkErr(d, err)
}
func (dci *USART) ReadBit() (int, error) {
slot, err := sendRecvSlot(usartDrv(dci), 0xff)
if err != nil {
return 0, err
}
return int(slot) & 1, nil
}
func (dci *USART) WriteBit(bit int) error {
var b byte
if bit&1 != 0 {
b = 0xff
}
d := usartDrv(dci)
slot, err := sendRecvSlot(d, b)
if err != nil {
return err
}
if slot != b {
d.DiscardRx()
return onewire.ErrBusFault
}
return nil
}
func (dci *USART) ReadByte() (byte, error) {
slots := [8]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
err := sendRecv(usartDrv(dci), &slots)
var v int
for i, slot := range slots {
v += int(slot&1) << uint(i)
}
return byte(v), err
}
func (dci *USART) WriteByte(b byte) error {
var slots [8]byte
v := int(b)
for i := range slots {
if v&1 != 0 {
slots[i] = 0xff
}
v >>= 1
}
d := usartDrv(dci)
if err := sendRecv(d, &slots); err != nil {
return err
}
v = int(b)
for i, slot := range slots {
r := v & (1 << uint(i))
if r != 0 {
r = 0xff
}
if int(slot) != r {
d.DiscardRx()
return onewire.ErrBusFault
}
}
return nil
}