-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
80 lines (68 loc) · 2.08 KB
/
setup.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
// 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 uartcon
import (
"embedded/rtos"
"os"
"syscall"
"github.com/embeddedgo/fs/termfs"
"github.com/embeddedgo/imxrt/hal/iomux"
"github.com/embeddedgo/imxrt/hal/lpuart"
)
var uart *lpuart.Driver
func write(_ int, p []byte) int {
n, _ := uart.Write(p)
return n
}
func panicErr(err error) {
if err != nil {
panic(err.Error())
}
}
// Setup setpus an LPUART peripheral to work as the system console.
func Setup(d *lpuart.Driver, rx, tx iomux.Pin, conf lpuart.Config, baudrate int, name string) {
// Setup and enable the LPUART driver.
d.UsePin(rx, lpuart.RXD)
d.UsePin(tx, lpuart.TXD)
d.Setup(conf, baudrate)
d.EnableTx()
d.EnableRx(64)
// Set a system writer for print, println, panic, etc.
uart = d
rtos.SetSystemWriter(write)
// Setup a serial console (standard input and output).
con := termfs.New(name, d, d)
con.SetCharMap(termfs.InCRLF | termfs.OutLFCRLF)
con.SetEcho(true)
con.SetLineMode(true, 256)
rtos.Mount(con, "/dev/console")
var err error
os.Stdin, err = os.OpenFile("/dev/console", syscall.O_RDONLY, 0)
panicErr(err)
os.Stdout, err = os.OpenFile("/dev/console", syscall.O_WRONLY, 0)
panicErr(err)
os.Stderr = os.Stdout
}
// SetupLight setpus an LPUART to work as the system console.
// It usese termfs.LightFS instead of termfs.FS.
func SetupLight(d *lpuart.Driver, rx, tx iomux.Pin, conf lpuart.Config, baudrate int, name string) {
// Setup and enable the LPUART driver.
d.UsePin(rx, lpuart.RXD)
d.UsePin(tx, lpuart.TXD)
d.Setup(conf, baudrate)
d.EnableTx()
d.EnableRx(64)
// Set a system writer for print, println, panic, etc.
uart = d
rtos.SetSystemWriter(write)
// Setup a serial console (standard input and output).
con := termfs.NewLight(name, d, d)
rtos.Mount(con, "/dev/console")
var err error
os.Stdin, err = os.OpenFile("/dev/console", syscall.O_RDONLY, 0)
panicErr(err)
os.Stdout, err = os.OpenFile("/dev/console", syscall.O_WRONLY, 0)
panicErr(err)
os.Stderr = os.Stdout
}