1 |
11 |
acapola |
/*
|
2 |
|
|
Author: Sebastien Riou (acapola)
|
3 |
|
|
Creation date: 23:57:02 08/31/2010
|
4 |
|
|
|
5 |
|
|
$LastChangedDate: 2011-01-29 13:16:17 +0100 (Sat, 29 Jan 2011) $
|
6 |
|
|
$LastChangedBy: acapola $
|
7 |
|
|
$LastChangedRevision: 11 $
|
8 |
|
|
$HeadURL: file:///svn/iso7816_3_master/iso7816_3_master/trunk/sources/Uart.v $
|
9 |
|
|
|
10 |
|
|
This file is under the BSD licence:
|
11 |
|
|
Copyright (c) 2011, Sebastien Riou
|
12 |
|
|
|
13 |
|
|
All rights reserved.
|
14 |
|
|
|
15 |
|
|
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
16 |
|
|
|
17 |
|
|
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
18 |
|
|
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
19 |
|
|
The names of contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
|
20 |
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
21 |
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
22 |
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
23 |
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
24 |
|
|
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
25 |
|
|
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
26 |
|
|
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
27 |
|
|
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
28 |
|
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
29 |
|
|
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
30 |
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31 |
|
|
*/
|
32 |
|
|
`default_nettype none
|
33 |
4 |
acapola |
`timescale 1ns / 1ps
|
34 |
11 |
acapola |
|
35 |
|
|
/*
|
36 |
|
|
Half duplex UART with 1 byte buffer
|
37 |
|
|
*/
|
38 |
|
|
module BasicHalfDuplexUart
|
39 |
|
|
#(//parameters to override
|
40 |
|
|
parameter DIVIDER_WIDTH = 1,
|
41 |
|
|
parameter CLOCK_PER_BIT_WIDTH = 13, //allow to support default speed of ISO7816
|
42 |
|
|
//invert the polarity of the output or not
|
43 |
|
|
parameter IN_POLARITY = 1'b0,
|
44 |
|
|
parameter PARITY_POLARITY = 1'b1,
|
45 |
|
|
//default conventions
|
46 |
|
|
parameter START_BIT = 1'b0,
|
47 |
|
|
parameter STOP_BIT1 = 1'b1,
|
48 |
|
|
parameter STOP_BIT2 = 1'b1
|
49 |
|
|
)
|
50 |
|
|
(
|
51 |
4 |
acapola |
output wire [7:0] rxData,
|
52 |
|
|
output wire overrunErrorFlag, //new data has been received before dataOut was read
|
53 |
|
|
output wire dataOutReadyFlag, //new data available
|
54 |
|
|
output wire frameErrorFlag, //bad parity or bad stop bits
|
55 |
|
|
output wire txRun, //tx is started
|
56 |
|
|
output wire endOfRx, //one cycle pulse: 1 during last cycle of last stop bit of rx
|
57 |
|
|
output wire rxRun, //rx is definitely started, one of the three flag will be set
|
58 |
|
|
output wire rxStartBit, //rx is started, but we don't know yet if real rx or just a glitch
|
59 |
|
|
output wire txFull,
|
60 |
|
|
output wire isTx, //1 only when tx is ongoing. Indicates the direction of the com line.
|
61 |
2 |
acapola |
|
62 |
4 |
acapola |
input wire serialIn, //signals to merged into a inout signal according to "isTx"
|
63 |
|
|
output wire serialOut,
|
64 |
|
|
output wire comClk,
|
65 |
2 |
acapola |
|
66 |
4 |
acapola |
input wire [DIVIDER_WIDTH-1:0] clkPerCycle,
|
67 |
|
|
input wire [7:0] txData,
|
68 |
|
|
input wire [CLOCK_PER_BIT_WIDTH-1:0] clocksPerBit,
|
69 |
|
|
input wire stopBit2,//0: 1 stop bit, 1: 2 stop bits
|
70 |
|
|
input wire oddParity, //if 1, parity bit is such that data+parity have an odd number of 1
|
71 |
|
|
input wire msbFirst, //if 1, bits order is: startBit, b7, b6, b5...b0, parity
|
72 |
|
|
input wire startTx,
|
73 |
|
|
input wire ackFlags,
|
74 |
|
|
input wire clk,
|
75 |
|
|
input wire nReset
|
76 |
2 |
acapola |
);
|
77 |
|
|
|
78 |
|
|
//constant definition for states
|
79 |
|
|
localparam IDLE_STATE = 3'b000;
|
80 |
|
|
localparam RX_STATE = 3'b001;
|
81 |
|
|
localparam TX_STATE = 3'b011;
|
82 |
|
|
|
83 |
|
|
wire rxSerialIn = isTx ? STOP_BIT1 : serialIn;
|
84 |
|
|
//wire serialOut;
|
85 |
|
|
wire loadDataIn;
|
86 |
|
|
|
87 |
|
|
wire txStopBits;
|
88 |
|
|
|
89 |
|
|
assign isTx = txRun & ~txStopBits;
|
90 |
|
|
//let this to top level to avoid inout signal
|
91 |
|
|
//assign serialLine = isTx ? serialOut : 1'bz;
|
92 |
|
|
|
93 |
|
|
assign loadDataIn = startTx & ~rxStartBit & (~rxRun | endOfRx);
|
94 |
|
|
|
95 |
|
|
/*//complicated approach... instead we can simply divide the clock at lower levels
|
96 |
|
|
wire useEarlyComClk = |clkPerCycle ? 1'b1:1'b0;
|
97 |
|
|
reg dividedClk;
|
98 |
|
|
wire earlyComClk;//earlier than comClk by 1 cycle of clk (use to make 1 cycle pulse signals)
|
99 |
|
|
always @(posedge clk)begin
|
100 |
|
|
if(useEarlyComClk)
|
101 |
|
|
dividedClk <= earlyComClk;
|
102 |
|
|
end
|
103 |
|
|
assign comClk=useEarlyComClk ? dividedClk : clk;//clock for communication
|
104 |
|
|
wire endOfRxComClk;//pulse of 1 cycle of comClk
|
105 |
|
|
assign endOfRx = useEarlyComClk ? endOfRxComClk & earlyComClk & ~comClk : endOfRxComClk;//pulse of 1 cycle of clk
|
106 |
|
|
ClkDivider #(.DIVIDER_WIDTH(DIVIDER_WIDTH))
|
107 |
|
|
clkDivider(
|
108 |
|
|
.nReset(nReset),
|
109 |
|
|
.clk(clk),
|
110 |
|
|
.divider(clkPerCycle),
|
111 |
|
|
.dividedClk(earlyComClk)
|
112 |
|
|
);
|
113 |
|
|
*/
|
114 |
5 |
acapola |
wire stopBit;
|
115 |
2 |
acapola |
// Instantiate the module
|
116 |
|
|
RxCoreSelfContained #(
|
117 |
7 |
acapola |
.DIVIDER_WIDTH(DIVIDER_WIDTH),
|
118 |
|
|
.CLOCK_PER_BIT_WIDTH(CLOCK_PER_BIT_WIDTH)
|
119 |
|
|
)
|
120 |
2 |
acapola |
rxCore (
|
121 |
|
|
.dataOut(rxData),
|
122 |
|
|
.overrunErrorFlag(overrunErrorFlag),
|
123 |
|
|
.dataOutReadyFlag(dataOutReadyFlag),
|
124 |
|
|
.frameErrorFlag(frameErrorFlag),
|
125 |
|
|
.endOfRx(endOfRx),
|
126 |
|
|
.run(rxRun),
|
127 |
|
|
.startBit(rxStartBit),
|
128 |
5 |
acapola |
.stopBit(stopBit),
|
129 |
|
|
.clkPerCycle(clkPerCycle),
|
130 |
2 |
acapola |
.clocksPerBit(clocksPerBit),
|
131 |
|
|
.stopBit2(stopBit2),
|
132 |
|
|
.oddParity(oddParity),
|
133 |
|
|
.msbFirst(msbFirst),
|
134 |
|
|
.ackFlags(ackFlags),
|
135 |
|
|
.serialIn(rxSerialIn),
|
136 |
|
|
.comClk(comClk),
|
137 |
|
|
.clk(clk),
|
138 |
|
|
.nReset(nReset)
|
139 |
|
|
);
|
140 |
7 |
acapola |
TxCore #(.DIVIDER_WIDTH(DIVIDER_WIDTH),
|
141 |
|
|
.CLOCK_PER_BIT_WIDTH(CLOCK_PER_BIT_WIDTH)
|
142 |
|
|
)
|
143 |
2 |
acapola |
txCore (
|
144 |
|
|
.serialOut(serialOut),
|
145 |
|
|
.run(txRun),
|
146 |
|
|
.full(txFull),
|
147 |
|
|
.stopBits(txStopBits),
|
148 |
|
|
.dataIn(txData),
|
149 |
|
|
.clkPerCycle(clkPerCycle),
|
150 |
|
|
.clocksPerBit(clocksPerBit),
|
151 |
|
|
.stopBit2(stopBit2),
|
152 |
|
|
.oddParity(oddParity),
|
153 |
|
|
.msbFirst(msbFirst),
|
154 |
|
|
.loadDataIn(loadDataIn),
|
155 |
|
|
.comClk(comClk),
|
156 |
|
|
.clk(clk),
|
157 |
|
|
.nReset(nReset)
|
158 |
|
|
);
|
159 |
|
|
|
160 |
|
|
endmodule
|
161 |
11 |
acapola |
`default_nettype wire
|