1 |
4 |
acapola |
`timescale 1ns / 1ps
|
2 |
|
|
`default_nettype none
|
3 |
2 |
acapola |
//////////////////////////////////////////////////////////////////////////////////
|
4 |
|
|
// Company:
|
5 |
|
|
// Engineer: Sebastien Riou
|
6 |
|
|
//
|
7 |
|
|
// Create Date: 23:57:02 08/31/2010
|
8 |
|
|
// Design Name:
|
9 |
|
|
// Module Name: Uart
|
10 |
|
|
// Project Name:
|
11 |
|
|
// Target Devices:
|
12 |
|
|
// Tool versions:
|
13 |
|
|
// Description: Half duplex UART with 1 byte buffer
|
14 |
|
|
//
|
15 |
|
|
// Dependencies:
|
16 |
|
|
//
|
17 |
|
|
// Revision:
|
18 |
|
|
// Revision 0.01 - File Created
|
19 |
|
|
// Additional Comments:
|
20 |
|
|
//
|
21 |
|
|
//////////////////////////////////////////////////////////////////////////////////
|
22 |
|
|
module BasicHalfDuplexUart(
|
23 |
4 |
acapola |
output wire [7:0] rxData,
|
24 |
|
|
output wire overrunErrorFlag, //new data has been received before dataOut was read
|
25 |
|
|
output wire dataOutReadyFlag, //new data available
|
26 |
|
|
output wire frameErrorFlag, //bad parity or bad stop bits
|
27 |
|
|
output wire txRun, //tx is started
|
28 |
|
|
output wire endOfRx, //one cycle pulse: 1 during last cycle of last stop bit of rx
|
29 |
|
|
output wire rxRun, //rx is definitely started, one of the three flag will be set
|
30 |
|
|
output wire rxStartBit, //rx is started, but we don't know yet if real rx or just a glitch
|
31 |
|
|
output wire txFull,
|
32 |
|
|
output wire isTx, //1 only when tx is ongoing. Indicates the direction of the com line.
|
33 |
2 |
acapola |
|
34 |
4 |
acapola |
input wire serialIn, //signals to merged into a inout signal according to "isTx"
|
35 |
|
|
output wire serialOut,
|
36 |
|
|
output wire comClk,
|
37 |
2 |
acapola |
|
38 |
4 |
acapola |
input wire [DIVIDER_WIDTH-1:0] clkPerCycle,
|
39 |
|
|
input wire [7:0] txData,
|
40 |
|
|
input wire [CLOCK_PER_BIT_WIDTH-1:0] clocksPerBit,
|
41 |
|
|
input wire stopBit2,//0: 1 stop bit, 1: 2 stop bits
|
42 |
|
|
input wire oddParity, //if 1, parity bit is such that data+parity have an odd number of 1
|
43 |
|
|
input wire msbFirst, //if 1, bits order is: startBit, b7, b6, b5...b0, parity
|
44 |
|
|
input wire startTx,
|
45 |
|
|
input wire ackFlags,
|
46 |
|
|
input wire clk,
|
47 |
|
|
input wire nReset
|
48 |
2 |
acapola |
);
|
49 |
|
|
|
50 |
|
|
//parameters to override
|
51 |
|
|
parameter DIVIDER_WIDTH = 1;
|
52 |
|
|
parameter CLOCK_PER_BIT_WIDTH = 13; //allow to support default speed of ISO7816
|
53 |
|
|
//invert the polarity of the output or not
|
54 |
|
|
parameter IN_POLARITY = 1'b0;
|
55 |
|
|
parameter PARITY_POLARITY = 1'b1;
|
56 |
|
|
//default conventions
|
57 |
|
|
parameter START_BIT = 1'b0;
|
58 |
|
|
parameter STOP_BIT1 = 1'b1;
|
59 |
|
|
parameter STOP_BIT2 = 1'b1;
|
60 |
|
|
|
61 |
|
|
//constant definition for states
|
62 |
|
|
localparam IDLE_STATE = 3'b000;
|
63 |
|
|
localparam RX_STATE = 3'b001;
|
64 |
|
|
localparam TX_STATE = 3'b011;
|
65 |
|
|
|
66 |
|
|
wire rxSerialIn = isTx ? STOP_BIT1 : serialIn;
|
67 |
|
|
//wire serialOut;
|
68 |
|
|
wire loadDataIn;
|
69 |
|
|
|
70 |
|
|
wire txStopBits;
|
71 |
|
|
|
72 |
|
|
assign isTx = txRun & ~txStopBits;
|
73 |
|
|
//let this to top level to avoid inout signal
|
74 |
|
|
//assign serialLine = isTx ? serialOut : 1'bz;
|
75 |
|
|
|
76 |
|
|
assign loadDataIn = startTx & ~rxStartBit & (~rxRun | endOfRx);
|
77 |
|
|
|
78 |
|
|
/*//complicated approach... instead we can simply divide the clock at lower levels
|
79 |
|
|
wire useEarlyComClk = |clkPerCycle ? 1'b1:1'b0;
|
80 |
|
|
reg dividedClk;
|
81 |
|
|
wire earlyComClk;//earlier than comClk by 1 cycle of clk (use to make 1 cycle pulse signals)
|
82 |
|
|
always @(posedge clk)begin
|
83 |
|
|
if(useEarlyComClk)
|
84 |
|
|
dividedClk <= earlyComClk;
|
85 |
|
|
end
|
86 |
|
|
assign comClk=useEarlyComClk ? dividedClk : clk;//clock for communication
|
87 |
|
|
wire endOfRxComClk;//pulse of 1 cycle of comClk
|
88 |
|
|
assign endOfRx = useEarlyComClk ? endOfRxComClk & earlyComClk & ~comClk : endOfRxComClk;//pulse of 1 cycle of clk
|
89 |
|
|
ClkDivider #(.DIVIDER_WIDTH(DIVIDER_WIDTH))
|
90 |
|
|
clkDivider(
|
91 |
|
|
.nReset(nReset),
|
92 |
|
|
.clk(clk),
|
93 |
|
|
.divider(clkPerCycle),
|
94 |
|
|
.dividedClk(earlyComClk)
|
95 |
|
|
);
|
96 |
|
|
*/
|
97 |
|
|
|
98 |
|
|
// Instantiate the module
|
99 |
|
|
RxCoreSelfContained #(
|
100 |
|
|
.DIVIDER_WIDTH(DIVIDER_WIDTH),
|
101 |
|
|
.PARITY_POLARITY(PARITY_POLARITY))
|
102 |
|
|
rxCore (
|
103 |
|
|
.dataOut(rxData),
|
104 |
|
|
.overrunErrorFlag(overrunErrorFlag),
|
105 |
|
|
.dataOutReadyFlag(dataOutReadyFlag),
|
106 |
|
|
.frameErrorFlag(frameErrorFlag),
|
107 |
|
|
.endOfRx(endOfRx),
|
108 |
|
|
.run(rxRun),
|
109 |
|
|
.startBit(rxStartBit),
|
110 |
|
|
.clkPerCycle(clkPerCycle),
|
111 |
|
|
.clocksPerBit(clocksPerBit),
|
112 |
|
|
.stopBit2(stopBit2),
|
113 |
|
|
.oddParity(oddParity),
|
114 |
|
|
.msbFirst(msbFirst),
|
115 |
|
|
.ackFlags(ackFlags),
|
116 |
|
|
.serialIn(rxSerialIn),
|
117 |
|
|
.comClk(comClk),
|
118 |
|
|
.clk(clk),
|
119 |
|
|
.nReset(nReset)
|
120 |
|
|
);
|
121 |
|
|
TxCore #(.DIVIDER_WIDTH(DIVIDER_WIDTH))
|
122 |
|
|
txCore (
|
123 |
|
|
.serialOut(serialOut),
|
124 |
|
|
.run(txRun),
|
125 |
|
|
.full(txFull),
|
126 |
|
|
.stopBits(txStopBits),
|
127 |
|
|
.dataIn(txData),
|
128 |
|
|
.clkPerCycle(clkPerCycle),
|
129 |
|
|
.clocksPerBit(clocksPerBit),
|
130 |
|
|
.stopBit2(stopBit2),
|
131 |
|
|
.oddParity(oddParity),
|
132 |
|
|
.msbFirst(msbFirst),
|
133 |
|
|
.loadDataIn(loadDataIn),
|
134 |
|
|
.comClk(comClk),
|
135 |
|
|
.clk(clk),
|
136 |
|
|
.nReset(nReset)
|
137 |
|
|
);
|
138 |
|
|
|
139 |
|
|
endmodule
|