-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
71 lines (59 loc) · 1.74 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
// Copyright 2023 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 usbcon
import (
"embedded/rtos"
"os"
"syscall"
"github.com/embeddedgo/fs/termfs"
"github.com/embeddedgo/imxrt/hal/usb/usbserial"
)
var us *usbserial.Driver
func write(_ int, p []byte) int {
n, _ := us.Write(p)
return n
}
func panicErr(err error) {
if err != nil {
panic(err.Error())
}
}
// Setup setpus an USB serial to work as the system console.
func Setup(d *usbserial.Driver, name string) {
d.SetWriteSink(true)
d.SetAutoFlush(true)
// Set a system writer for print, println, panic, etc.
us = 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 USB serial to work as the system console.
// It usese termfs.LightFS instead of termfs.FS.
func SetupLight(d *usbserial.Driver, name string) {
d.SetWriteSink(true)
d.SetAutoFlush(true)
// Set a system writer for print, println, panic, etc.
us = 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
}