-
Notifications
You must be signed in to change notification settings - Fork 17
/
serial.v
93 lines (81 loc) · 1.91 KB
/
serial.v
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
/** \file
* Test the serial output to the FTDI cable.
*
* The schematic disagrees with the PCF, but the PCF works...
*
* The SPI flash chip select *MUST* be pulled high to disable the
* flash chip, otherwise they will both be driving the bus.
*
* This may interfere with programming; `iceprog -e 128` should erase enough
* to make it compliant for re-programming
*
* The USB port will have to be cycled to get the FTDI to renumerate as
* /dev/ttyUSB0. Not sure what is going on with iceprog.
*/
`include "util.v"
`include "uart.v"
module top(
output led_r,
output led_g,
output led_b,
output serial_txd,
input serial_rxd,
output spi_cs,
output gpio_2
);
assign spi_cs = 1; // it is necessary to turn off the SPI flash chip
wire debug0 = gpio_2;
wire clk_48;
wire reset = 0;
SB_HFOSC u_hfosc (
.CLKHFPU(1'b1),
.CLKHFEN(1'b1),
.CLKHF(clk_48)
);
reg [31:0] counter;
always @(posedge clk_48)
if (reset)
counter <= 0;
else
counter <= counter + 1;
assign led_g = 1;
assign led_b = serial_rxd; // idles high
// generate a 1 MHz serial clock from the 48 MHz clock
wire clk_1;
divide_by_n #(.N(48)) div(clk_48, reset, clk_1);
reg [7:0] uart_txd;
reg uart_txd_strobe;
wire uart_txd_ready;
uart_tx txd(
.mclk(clk_48),
.reset(reset),
.baud_x1(clk_1),
.serial(serial_txd),
.ready(uart_txd_ready),
.data(uart_txd),
.data_strobe(uart_txd_strobe)
);
assign debug0 = serial_txd;
reg [3:0] byte_counter;
always @(posedge clk_48) begin
led_r <= 1;
uart_txd_strobe <= 0;
if (reset) begin
// nothing
byte_counter <= 0;
end else
if (uart_txd_ready && !uart_txd_strobe && counter[14:0] == 0) begin
// ready to send a new byte
uart_txd_strobe <= 1;
if (byte_counter == 0)
uart_txd <= "\r";
else
if (byte_counter == 1)
uart_txd <= "\n";
else
uart_txd <= "A" + byte_counter - 2;
byte_counter <= byte_counter + 1;
led_r <= 0;
end
end
endmodule