Skip to content

Commit 82d2093

Browse files
aykevldeadprogram
authored andcommitted
reference/machine: add UART documentation
1 parent b08bcf2 commit 82d2093

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

content/docs/reference/machine.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,100 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error
121121
```
122122

123123
The `Tx` call performs the actual I2C transaction. It first writes the bytes in `w` to the peripheral device indicated in `addr` and then reads `r` bytes from the peripheral and stores the read bytes in the `r` slice. It returns an error if the transaction failed. Both `w` and `r` can be `nil`.
124+
125+
126+
## UART
127+
128+
```go
129+
type UARTConfig struct {
130+
BaudRate uint32
131+
TX Pin
132+
RX Pin
133+
}
134+
```
135+
136+
The `UARTConfig` struct contains configuration for the UART peripheral.
137+
138+
* `BaudRate` is the baud rate of the UART. Common values are 9600 and 115200. Other than that, most chips support multiples of 1200 (2400, 4800, 9600, etc). Support for other baud rates varies by chip, some chips even support high baudrates like 1MHz.
139+
* `TX` and `RX` are the transmit and receive pins of the UART. Many chips impose restrictions on which pins can be used, some only support a particular TX and RX pin.
140+
141+
```go
142+
type UARTParity uint8
143+
144+
const (
145+
ParityNone UARTParity = iota
146+
ParityEven
147+
ParityOdd
148+
)
149+
```
150+
151+
Parity is a rarely used checksum feature of UART.
152+
153+
* `ParityNone` is the default, meaning no parity. It is the most commonly used setting.
154+
* `ParityEven` means to expect that the total number of 1 bits sent should be an even number.
155+
* `ParityOdd` means to expect that the total number of 1 bits sent should be an odd number.
156+
157+
```go
158+
type UART struct {
159+
// values are unexported or vary by chip
160+
}
161+
162+
var (
163+
UART0 = &UART{...}
164+
UART1 = &UART{...}
165+
)
166+
```
167+
168+
The `UART` object refers to a single (hardware) UART instance. Depending on chip capabilities, various objects such as `UART0` and perhaps others are defined.
169+
170+
```go
171+
func (uart UART) Configure(config UARTConfig) error
172+
```
173+
174+
The `Configure` call enables and configures the hardware UART for use, setting the pins and baud rate. It will return an error when an incorrect configuration is provided (for example, using pins not usable with this UART instance). See `UARTConfig` for details.
175+
176+
Depending on the target configuration, a UART may already be configured if it is the stdout output for the given target. In that case, it is normally configured with a baud rate of 115200.
177+
178+
```go
179+
func (uart *UART) SetBaudRate(br uint32)
180+
```
181+
182+
Set the baud rate for the UART. This method is not available on all chips. See `UARTConfig` above for permissible baud rate values.
183+
184+
```go
185+
func (uart *UART) SetFormat(dataBits, stopBits int, parity UARTParity) error
186+
```
187+
188+
Set the UART format. The default format (8N1 meaning 8 bits, no parity, and 1 stop bit) is used for almost all external devices, but if you need it this method can be used to override the default.
189+
190+
This method is only available on a limited number of chips.
191+
192+
```go
193+
func (uart *UART) Buffered() int
194+
```
195+
196+
Return the number of bytes stored in the receive buffer.
197+
198+
```go
199+
func (uart *UART) Read(data []byte) (n int, err error)
200+
```
201+
202+
Read from the receive buffer. This method implements the `io.Reader` interface. It is non-blocking: it will return immediately with `n` set to 0 and `err` set to nil if no data is available.
203+
204+
```go
205+
func (uart *UART) ReadByte() (byte, error)
206+
```
207+
208+
ReadByte reads a single byte from the UART receive buffer. If there is no data available in the buffer, it returns an error. You can use `Buffered` to know whether there is data available.
209+
210+
```go
211+
func (uart *UART) Write(data []byte) (n int, err error)
212+
```
213+
214+
Write data to the UART. This method implements the `io.Writer` interface. It blocks until all data is written.
215+
216+
```go
217+
func (uart *UART) WriteByte(c byte) error
218+
```
219+
220+
Write a single byte to the UART output.

0 commit comments

Comments
 (0)