-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from hasheddan/uart-rx
Add UART receiving module
- Loading branch information
Showing
7 changed files
with
266 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Connecting Over Serial Port | ||
|
||
The `moss` UART can be used to send information between the system and the host | ||
machine. There are a variety of utilities that can be used to connect over | ||
serial port. | ||
|
||
## Screen | ||
|
||
``` | ||
screen /dev/ttyUSB1 9600 | ||
``` | ||
|
||
Kill screen: | ||
``` | ||
CTRL+a k | ||
``` | ||
|
||
## Minicom | ||
|
||
Minicom can be configured using `minirc` files. The following configuration file | ||
supports communicating with `moss`: | ||
|
||
`minirc.moss` | ||
``` | ||
# Machine-generated file - use "minicom -s" to change parameters. | ||
pu port /dev/ttyUSB1 | ||
pu baudrate 9600 | ||
pu rtscts No | ||
``` | ||
|
||
## xxd | ||
|
||
``` | ||
xxd -b < /dev/ttyUSB1 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Verilator | ||
|
||
## Resources | ||
|
||
- [Verilator Website](https://www.veripool.org/verilator/) | ||
- [It's Embedded: Introduction to | ||
Verilator](https://www.itsembedded.com/dhd/verilator_1/) | ||
|
||
## Overview | ||
|
||
Verilator is a tool that translates Verilog into a C++ model that can be invoked | ||
to simulate the RTL design. | ||
|
||
## Configuration | ||
|
||
Verilator supports a large number of flags and configuration values. Those used | ||
in `moss` are detailed below. | ||
|
||
- `-Wall`: turns on all Verilator linter warnings | ||
- `--cc`: specifies that we want C++ output | ||
- `--exe`: specifies that we want a executable rather than just a library | ||
- `--build`: instructs Verilator to build library or executable (calls `make` on | ||
the Makefiles it generates) | ||
- `--trace`: enables waveform creation | ||
- `-I<dir>`: add directory to the list that Verilator searches for RTL | ||
|
||
## Data Types | ||
|
||
Verilator defines packed data types to represent signals in a module. | ||
|
||
`verilated.h` | ||
```c++ | ||
// Basic types | ||
|
||
// P // Packed data of bit type (C/S/I/Q/W) | ||
typedef vluint8_t CData; ///< Verilated pack data, 1-8 bits | ||
typedef vluint16_t SData; ///< Verilated pack data, 9-16 bits | ||
typedef vluint32_t IData; ///< Verilated pack data, 17-32 bits | ||
typedef vluint64_t QData; ///< Verilated pack data, 33-64 bits | ||
typedef vluint32_t EData; ///< Verilated pack element of WData array | ||
typedef EData WData; ///< Verilated pack data, >64 bits, as an array | ||
// float F // No typedef needed; Verilator uses float | ||
// double D // No typedef needed; Verilator uses double | ||
// string N // No typedef needed; Verilator uses string | ||
|
||
typedef const WData* WDataInP; ///< Array input to a function | ||
typedef WData* WDataOutP; ///< Array output from a function | ||
|
||
typedef void (*VerilatedVoidCb)(void); | ||
``` | ||
```c++ | ||
# define VL_OUT8(name, msb,lsb) CData name ///< Declare output signal, 1-8 bits | ||
# define VL_OUT16(name, msb,lsb) SData name ///< Declare output signal, 9-16 bits | ||
# define VL_OUT64(name, msb,lsb) QData name ///< Declare output signal, 33-64bits | ||
# define VL_OUT(name, msb,lsb) IData name ///< Declare output signal, 17-32 bits | ||
# define VL_OUTW(name, msb,lsb, words) WData name[words] ///< Declare output signal, 65+ bits | ||
``` | ||
|
||
For example, `moss` controls LEDs on the Arty A7 using one signal per LED. These | ||
are defined as a `wire` with width 4 (i.e. `[3:0]led`). Verilator represents | ||
these signals using the `CData` type, with bits in each position corresponding | ||
to the index in the `wire`. | ||
|
||
The variable is declared using the `VL_OUT8(name, mdb,lsb)` macro, which | ||
instantiates a variable with name `name` and type `CData`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
Copyright 2020 The Moss Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
`timescale 1ns / 1ps | ||
|
||
module uart_rx( | ||
input clk, | ||
input in, | ||
// NOTE(hasheddan): the outputs below are declared as registers, which is | ||
// essentially shorthand for instaniating a register and assigning the wire | ||
// to it on every clock cycle. | ||
// See https://stackoverflow.com/a/5360623 | ||
output reg notif, | ||
output reg [7:0] data, | ||
output reg send | ||
); | ||
|
||
reg [2:0] state; | ||
|
||
// states | ||
localparam s_idle = 3'b000; | ||
localparam s_start_bit = 3'b001; | ||
localparam s_data_bit = 3'b010; | ||
localparam s_stop_bit = 3'b011; | ||
localparam s_cleanup = 3'b100; | ||
|
||
// clock cycles per bit transmitted | ||
// 100000000 / 9600 ~= 10416 | ||
parameter cycles = 10416; | ||
|
||
integer clock_count = 0; | ||
integer bit_index = 7; | ||
|
||
always @(posedge clk) begin | ||
case (state) | ||
s_idle: | ||
begin | ||
notif <= 1'b0; | ||
send <= 1'b0; | ||
if (in == 1'b0) | ||
begin | ||
state <= s_start_bit; | ||
end | ||
else | ||
begin | ||
state <= s_idle; | ||
end | ||
end | ||
s_start_bit: | ||
begin | ||
notif <= 1'b1; | ||
send <= 1'b0; | ||
if (clock_count < (cycles-1 / 2)) | ||
begin | ||
clock_count <= clock_count+1; | ||
state <= s_start_bit; | ||
end | ||
else | ||
begin | ||
clock_count <= 0; | ||
state <= s_data_bit; | ||
end | ||
end | ||
s_data_bit: | ||
begin | ||
notif <= 1'b1; | ||
send <= 1'b0; | ||
data[bit_index] <= in; | ||
if (clock_count < cycles-1) | ||
begin | ||
clock_count <= clock_count+1; | ||
state <= s_data_bit; | ||
end | ||
else | ||
begin | ||
clock_count <= 0; | ||
if (bit_index > 0) | ||
begin | ||
bit_index <= bit_index-1; | ||
state <= s_data_bit; | ||
end | ||
else | ||
begin | ||
bit_index <= 7; | ||
state <= s_stop_bit; | ||
end | ||
end | ||
end | ||
s_stop_bit: | ||
begin | ||
notif <= 1'b1; | ||
send <= 1'b0; | ||
if (clock_count < cycles-1) | ||
begin | ||
clock_count <= clock_count+1; | ||
state <= s_stop_bit; | ||
end | ||
else | ||
begin | ||
clock_count <= 0; | ||
state <= s_cleanup; | ||
end | ||
end | ||
s_cleanup: | ||
begin | ||
notif <= 1'b1; | ||
// Assert send after data has been read. | ||
send <= 1'b1; | ||
state <= s_idle; | ||
end | ||
default: | ||
state <= s_idle; | ||
endcase | ||
end | ||
endmodule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.