1 |
11 |
acapola |
/*
|
2 |
|
|
Author: Sebastien Riou (acapola)
|
3 |
|
|
Creation date: 17:16:40 01/09/2011
|
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/Iso7816_3_Master.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 |
4 |
acapola |
`default_nettype none
|
33 |
2 |
acapola |
module Iso7816_3_Master(
|
34 |
4 |
acapola |
input wire nReset,
|
35 |
|
|
input wire clk,
|
36 |
|
|
input wire [15:0] clkPerCycle,//not supported yet
|
37 |
|
|
input wire startActivation,//Starts activation sequence
|
38 |
|
|
input wire startDeactivation,//Starts deactivation sequence
|
39 |
|
|
input wire [7:0] dataIn,
|
40 |
|
|
input wire nWeDataIn,
|
41 |
7 |
acapola |
input wire [12:0] cyclesPerEtu,
|
42 |
15 |
acapola |
output reg [7:0] dataOut,
|
43 |
4 |
acapola |
input wire nCsDataOut,
|
44 |
|
|
output wire [7:0] statusOut,
|
45 |
|
|
input wire nCsStatusOut,
|
46 |
2 |
acapola |
output reg isActivated,//set to high by activation sequence, set to low by deactivation sequence
|
47 |
4 |
acapola |
output wire useIndirectConvention,
|
48 |
|
|
output wire tsError,//high if TS character is wrong
|
49 |
|
|
output wire tsReceived,
|
50 |
|
|
output wire atrIsEarly,//high if TS received before 400 cycles after reset release
|
51 |
|
|
output wire atrIsLate,//high if TS is still not received after 40000 cycles after reset release
|
52 |
2 |
acapola |
//ISO7816 signals
|
53 |
18 |
acapola |
//inout wire isoSio,//not synthesisable on FPGA :-S
|
54 |
|
|
output wire isTx,
|
55 |
|
|
input wire isoSioIn,
|
56 |
|
|
output wire isoSioOut,
|
57 |
4 |
acapola |
output wire isoClk,
|
58 |
3 |
acapola |
output reg isoReset,
|
59 |
|
|
output reg isoVdd
|
60 |
2 |
acapola |
);
|
61 |
|
|
|
62 |
18 |
acapola |
wire txRun,txPending, rxRun, rxStartBit, overrunErrorFlag, frameErrorFlag, bufferFull;
|
63 |
3 |
acapola |
assign {txRun, txPending, rxRun, rxStartBit, isTx, overrunErrorFlag, frameErrorFlag, bufferFull} = statusOut;
|
64 |
|
|
|
65 |
18 |
acapola |
//wire serialOut;
|
66 |
|
|
//not synthesisable on FPGA :-S
|
67 |
|
|
//assign isoSio = isTx ? serialOut : 1'bz;
|
68 |
|
|
//pullup(isoSio);
|
69 |
3 |
acapola |
wire comClk;
|
70 |
2 |
acapola |
|
71 |
15 |
acapola |
wire stopBit2=1'b1;//0: 1 stop bit, 1: 2 stop bits
|
72 |
|
|
wire msbFirst = useIndirectConvention;//if 1, bits order is: startBit, b7, b6, b5...b0, parity
|
73 |
|
|
wire oddParity = 1'b0;//if 1, parity bit is such that data+parity have an odd number of 1
|
74 |
|
|
wire sioHighValue = ~useIndirectConvention;//apply only to data bits
|
75 |
|
|
|
76 |
|
|
wire [7:0] uart_dataOut;
|
77 |
|
|
wire [7:0] uart_dataIn = sioHighValue ? dataIn : ~dataIn;
|
78 |
|
|
always @(*) dataOut = sioHighValue ? uart_dataOut : ~uart_dataOut;
|
79 |
|
|
|
80 |
|
|
|
81 |
7 |
acapola |
HalfDuplexUartIf #(
|
82 |
|
|
.DIVIDER_WIDTH(1'b1),
|
83 |
|
|
.CLOCK_PER_BIT_WIDTH(4'd13)
|
84 |
|
|
)
|
85 |
|
|
uart (
|
86 |
2 |
acapola |
.nReset(nReset),
|
87 |
|
|
.clk(clk),
|
88 |
|
|
.clkPerCycle(1'b0),
|
89 |
15 |
acapola |
.dataIn(uart_dataIn),
|
90 |
2 |
acapola |
.nWeDataIn(nWeDataIn),
|
91 |
7 |
acapola |
.clocksPerBit(cyclesPerEtu),
|
92 |
15 |
acapola |
.stopBit2(stopBit2),
|
93 |
|
|
.oddParity(oddParity),
|
94 |
|
|
.msbFirst(msbFirst),
|
95 |
|
|
.dataOut(uart_dataOut),
|
96 |
2 |
acapola |
.nCsDataOut(nCsDataOut),
|
97 |
|
|
.statusOut(statusOut),
|
98 |
|
|
.nCsStatusOut(nCsStatusOut),
|
99 |
18 |
acapola |
.serialIn(isoSioIn),
|
100 |
|
|
.serialOut(isoSioOut),
|
101 |
2 |
acapola |
.comClk(comClk)
|
102 |
|
|
);
|
103 |
|
|
|
104 |
|
|
reg isoClkEn;
|
105 |
|
|
assign isoClk = isoClkEn ? comClk : 1'b0;
|
106 |
|
|
|
107 |
5 |
acapola |
reg [16:0] resetCnt;
|
108 |
|
|
reg waitTs;
|
109 |
|
|
assign tsReceived = ~waitTs;
|
110 |
|
|
reg [7:0] ts;
|
111 |
|
|
assign atrIsEarly = ~waitTs & (resetCnt<(16'h100+16'd400));
|
112 |
|
|
assign atrIsLate = resetCnt>(16'h100+16'd40000);
|
113 |
|
|
assign useIndirectConvention = ~waitTs & (ts==8'h3F);
|
114 |
17 |
acapola |
assign tsError = ~waitTs & (ts!=8'h3B) & (ts!=8'h3F);
|
115 |
5 |
acapola |
always @(posedge comClk, negedge nReset) begin
|
116 |
|
|
if(~nReset) begin
|
117 |
|
|
isoClkEn <= 1'b0;
|
118 |
|
|
resetCnt<=16'b0;
|
119 |
|
|
waitTs<=1'b1;
|
120 |
|
|
isoReset <= 1'b0;
|
121 |
|
|
isoVdd <= 1'b0;
|
122 |
|
|
isActivated <= 1'b0;
|
123 |
|
|
end else if(isActivated) begin
|
124 |
|
|
if(waitTs) begin
|
125 |
|
|
if(statusOut[0]) begin
|
126 |
|
|
waitTs<=1'b0;
|
127 |
15 |
acapola |
case(dataOut)
|
128 |
|
|
8'h3B: ts<=dataOut;
|
129 |
17 |
acapola |
8'h03: ts<=8'h3F;//03 is 3F written LSB first and complemented
|
130 |
15 |
acapola |
default: ts<=dataOut;
|
131 |
|
|
endcase
|
132 |
5 |
acapola |
end
|
133 |
18 |
acapola |
resetCnt<=resetCnt+1'b1;
|
134 |
5 |
acapola |
end
|
135 |
|
|
if(startDeactivation) begin
|
136 |
|
|
isoVdd <= 1'b0;
|
137 |
2 |
acapola |
isoClkEn <= 1'b0;
|
138 |
5 |
acapola |
isoReset <= 1'b0;
|
139 |
2 |
acapola |
resetCnt<=16'b0;
|
140 |
|
|
isActivated <= 1'b0;
|
141 |
5 |
acapola |
end
|
142 |
|
|
end else begin
|
143 |
|
|
if(startActivation) begin
|
144 |
|
|
waitTs <= 1'b1;
|
145 |
|
|
isoVdd <= 1'b1;
|
146 |
|
|
isoClkEn <= 1'b1;
|
147 |
|
|
if(16'h100 == resetCnt) begin
|
148 |
|
|
isActivated <=1'b1;
|
149 |
|
|
isoReset <=1'b1;
|
150 |
|
|
end else
|
151 |
18 |
acapola |
resetCnt<=resetCnt + 1'b1;
|
152 |
2 |
acapola |
end else begin
|
153 |
5 |
acapola |
resetCnt<=16'b0;
|
154 |
2 |
acapola |
end
|
155 |
|
|
end
|
156 |
5 |
acapola |
end
|
157 |
2 |
acapola |
endmodule
|
158 |
11 |
acapola |
`default_nettype wire
|