1 |
11 |
acapola |
/*
|
2 |
|
|
Author: Sebastien Riou (acapola)
|
3 |
|
|
Creation date: 23:57:02 08/31/2010
|
4 |
|
|
|
5 |
|
|
$LastChangedDate: 2011-03-07 14:17:52 +0100 (Mon, 07 Mar 2011) $
|
6 |
|
|
$LastChangedBy: acapola $
|
7 |
|
|
$LastChangedRevision: 18 $
|
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 |
12 |
acapola |
output wire endOfTx, //one cycle pulse: 1 during last cycle of last stop bit of tx
|
62 |
2 |
acapola |
|
63 |
4 |
acapola |
input wire serialIn, //signals to merged into a inout signal according to "isTx"
|
64 |
|
|
output wire serialOut,
|
65 |
|
|
output wire comClk,
|
66 |
2 |
acapola |
|
67 |
4 |
acapola |
input wire [DIVIDER_WIDTH-1:0] clkPerCycle,
|
68 |
|
|
input wire [7:0] txData,
|
69 |
|
|
input wire [CLOCK_PER_BIT_WIDTH-1:0] clocksPerBit,
|
70 |
|
|
input wire stopBit2,//0: 1 stop bit, 1: 2 stop bits
|
71 |
|
|
input wire oddParity, //if 1, parity bit is such that data+parity have an odd number of 1
|
72 |
|
|
input wire msbFirst, //if 1, bits order is: startBit, b7, b6, b5...b0, parity
|
73 |
|
|
input wire startTx,
|
74 |
|
|
input wire ackFlags,
|
75 |
|
|
input wire clk,
|
76 |
|
|
input wire nReset
|
77 |
2 |
acapola |
);
|
78 |
|
|
|
79 |
|
|
wire rxSerialIn = isTx ? STOP_BIT1 : serialIn;
|
80 |
|
|
wire loadDataIn;
|
81 |
|
|
wire txStopBits;
|
82 |
|
|
assign isTx = txRun & ~txStopBits;
|
83 |
|
|
assign loadDataIn = startTx & ~rxStartBit & (~rxRun | endOfRx);
|
84 |
|
|
|
85 |
12 |
acapola |
reg [CLOCK_PER_BIT_WIDTH-1:0] safeClocksPerBit;
|
86 |
15 |
acapola |
reg safeStopBit2;
|
87 |
|
|
reg safeOddParity;
|
88 |
|
|
reg safeMsbFirst;
|
89 |
12 |
acapola |
always @(posedge clk, negedge nReset) begin
|
90 |
|
|
if(~nReset) begin
|
91 |
18 |
acapola |
/*safeClocksPerBit<=clocksPerBit;
|
92 |
15 |
acapola |
safeStopBit2<=stopBit2;
|
93 |
|
|
safeOddParity<=oddParity;
|
94 |
18 |
acapola |
safeMsbFirst<=msbFirst;*/
|
95 |
|
|
safeClocksPerBit<={CLOCK_PER_BIT_WIDTH{1'b0}};
|
96 |
|
|
safeStopBit2<=1'b0;
|
97 |
|
|
safeOddParity<=1'b0;
|
98 |
|
|
safeMsbFirst<=1'b0;
|
99 |
12 |
acapola |
end else if(endOfRx|endOfTx|~(rxRun|rxStartBit|txRun)) begin
|
100 |
|
|
safeClocksPerBit<=clocksPerBit;
|
101 |
15 |
acapola |
safeStopBit2<=stopBit2;
|
102 |
|
|
safeOddParity<=oddParity;
|
103 |
|
|
safeMsbFirst<=msbFirst;
|
104 |
12 |
acapola |
end
|
105 |
2 |
acapola |
end
|
106 |
12 |
acapola |
|
107 |
5 |
acapola |
wire stopBit;
|
108 |
2 |
acapola |
// Instantiate the module
|
109 |
|
|
RxCoreSelfContained #(
|
110 |
7 |
acapola |
.DIVIDER_WIDTH(DIVIDER_WIDTH),
|
111 |
|
|
.CLOCK_PER_BIT_WIDTH(CLOCK_PER_BIT_WIDTH)
|
112 |
|
|
)
|
113 |
2 |
acapola |
rxCore (
|
114 |
|
|
.dataOut(rxData),
|
115 |
|
|
.overrunErrorFlag(overrunErrorFlag),
|
116 |
|
|
.dataOutReadyFlag(dataOutReadyFlag),
|
117 |
|
|
.frameErrorFlag(frameErrorFlag),
|
118 |
|
|
.endOfRx(endOfRx),
|
119 |
|
|
.run(rxRun),
|
120 |
|
|
.startBit(rxStartBit),
|
121 |
5 |
acapola |
.stopBit(stopBit),
|
122 |
|
|
.clkPerCycle(clkPerCycle),
|
123 |
12 |
acapola |
.clocksPerBit(safeClocksPerBit),
|
124 |
15 |
acapola |
.stopBit2(safeStopBit2),
|
125 |
|
|
.oddParity(safeOddParity),
|
126 |
|
|
.msbFirst(safeMsbFirst),
|
127 |
2 |
acapola |
.ackFlags(ackFlags),
|
128 |
|
|
.serialIn(rxSerialIn),
|
129 |
|
|
.comClk(comClk),
|
130 |
|
|
.clk(clk),
|
131 |
|
|
.nReset(nReset)
|
132 |
|
|
);
|
133 |
7 |
acapola |
TxCore #(.DIVIDER_WIDTH(DIVIDER_WIDTH),
|
134 |
|
|
.CLOCK_PER_BIT_WIDTH(CLOCK_PER_BIT_WIDTH)
|
135 |
|
|
)
|
136 |
2 |
acapola |
txCore (
|
137 |
|
|
.serialOut(serialOut),
|
138 |
12 |
acapola |
.run(txRun),
|
139 |
|
|
.endOfTx(endOfTx),
|
140 |
2 |
acapola |
.full(txFull),
|
141 |
|
|
.stopBits(txStopBits),
|
142 |
|
|
.dataIn(txData),
|
143 |
|
|
.clkPerCycle(clkPerCycle),
|
144 |
12 |
acapola |
.clocksPerBit(safeClocksPerBit),
|
145 |
15 |
acapola |
.stopBit2(safeStopBit2),
|
146 |
|
|
.oddParity(safeOddParity),
|
147 |
|
|
.msbFirst(safeMsbFirst),
|
148 |
2 |
acapola |
.loadDataIn(loadDataIn),
|
149 |
|
|
.comClk(comClk),
|
150 |
|
|
.clk(clk),
|
151 |
|
|
.nReset(nReset)
|
152 |
|
|
);
|
153 |
|
|
|
154 |
|
|
endmodule
|
155 |
11 |
acapola |
`default_nettype wire
|