/*
|
/*
|
Author: Sebastien Riou (acapola)
|
Author: Sebastien Riou (acapola)
|
Creation date: 19:57:35 10/31/2010
|
Creation date: 19:57:35 10/31/2010
|
|
|
$LastChangedDate: 2011-01-29 13:16:17 +0100 (Sat, 29 Jan 2011) $
|
$LastChangedDate: 2011-02-13 16:20:10 +0100 (Sun, 13 Feb 2011) $
|
$LastChangedBy: acapola $
|
$LastChangedBy: acapola $
|
$LastChangedRevision: 11 $
|
$LastChangedRevision: 15 $
|
$HeadURL: file:///svn/iso7816_3_master/iso7816_3_master/trunk/sources/HalfDuplexUartIf.v $
|
$HeadURL: file:///svn/iso7816_3_master/iso7816_3_master/trunk/sources/HalfDuplexUartIf.v $
|
|
|
This file is under the BSD licence:
|
This file is under the BSD licence:
|
Copyright (c) 2011, Sebastien Riou
|
Copyright (c) 2011, Sebastien Riou
|
|
|
All rights reserved.
|
All rights reserved.
|
|
|
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
|
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
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.
|
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.
|
The names of contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
|
The names of contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
*/
|
*/
|
`default_nettype none
|
`default_nettype none
|
|
|
module HalfDuplexUartIf
|
module HalfDuplexUartIf
|
#(//parameters to override
|
#(//parameters to override
|
parameter DIVIDER_WIDTH = 1,
|
parameter DIVIDER_WIDTH = 1,
|
parameter CLOCK_PER_BIT_WIDTH = 13 //allow to support default speed of ISO7816
|
parameter CLOCK_PER_BIT_WIDTH = 13 //allow to support default speed of ISO7816
|
)
|
)
|
(
|
(
|
input wire nReset,
|
input wire nReset,
|
input wire clk,
|
input wire clk,
|
input wire [DIVIDER_WIDTH-1:0] clkPerCycle,
|
input wire [DIVIDER_WIDTH-1:0] clkPerCycle,
|
input wire [7:0] dataIn,
|
input wire [7:0] dataIn,
|
input wire nWeDataIn,
|
input wire nWeDataIn,
|
input wire [CLOCK_PER_BIT_WIDTH-1:0] clocksPerBit,
|
input wire [CLOCK_PER_BIT_WIDTH-1:0] clocksPerBit,
|
|
input wire stopBit2,//0: 1 stop bit, 1: 2 stop bits
|
|
input wire oddParity, //if 1, parity bit is such that data+parity have an odd number of 1
|
|
input wire msbFirst, //if 1, bits order is: startBit, b7, b6, b5...b0, parity
|
output wire [7:0] dataOut,
|
output wire [7:0] dataOut,
|
input wire nCsDataOut,
|
input wire nCsDataOut,
|
output wire [7:0] statusOut,
|
output wire [7:0] statusOut,
|
input wire nCsStatusOut,
|
input wire nCsStatusOut,
|
input wire serialIn,
|
input wire serialIn,
|
output wire serialOut,
|
output wire serialOut,
|
output wire comClk
|
output wire comClk
|
);
|
);
|
|
|
|
|
reg [7:0] dataReg;
|
reg [7:0] dataReg;
|
|
|
// Inputs
|
// Inputs
|
wire [7:0] txData;
|
wire [7:0] txData;
|
//wire [12:0] clocksPerBit;
|
|
wire stopBit2=1;
|
|
wire oddParity=0; //if 1, parity bit is such that data+parity have an odd number of 1
|
|
wire msbFirst=0; //if 1, bits will be send in the order startBit, b7, b6, b5...b0, parity
|
|
reg txPending;
|
reg txPending;
|
wire ackFlags;
|
wire ackFlags;
|
|
|
// Outputs
|
// Outputs
|
wire [7:0] rxData;
|
wire [7:0] rxData;
|
wire overrunErrorFlag;
|
wire overrunErrorFlag;
|
wire dataOutReadyFlag;
|
wire dataOutReadyFlag;
|
wire frameErrorFlag;
|
wire frameErrorFlag;
|
wire txRun;
|
wire txRun;
|
wire endOfRx;
|
wire endOfRx;
|
wire rxRun;
|
wire rxRun;
|
wire rxStartBit;
|
wire rxStartBit;
|
wire txFull;
|
wire txFull;
|
wire isTx;
|
wire isTx;
|
|
|
wire rxFlagsSet = dataOutReadyFlag | overrunErrorFlag | frameErrorFlag;
|
wire rxFlagsSet = dataOutReadyFlag | overrunErrorFlag | frameErrorFlag;
|
reg bufferFull;
|
reg bufferFull;
|
reg [1:0] flagsReg;
|
reg [1:0] flagsReg;
|
|
|
assign txData = dataReg;
|
assign txData = dataReg;
|
//assign clocksPerBit = 7;
|
|
|
|
assign dataOut=dataReg;
|
assign dataOut=dataReg;
|
assign statusOut[7:0]={txRun, txPending, rxRun, rxStartBit, isTx, flagsReg, bufferFull};
|
assign statusOut[7:0]={txRun, txPending, rxRun, rxStartBit, isTx, flagsReg, bufferFull};
|
|
|
reg waitTxFull0;//internal reg for managing bufferFull bit in Tx
|
reg waitTxFull0;//internal reg for managing bufferFull bit in Tx
|
|
|
assign ackFlags=~txPending & ~txRun & rxFlagsSet & ((bufferFull & ~nCsDataOut)| ~bufferFull);
|
assign ackFlags=~txPending & ~txRun & rxFlagsSet & ((bufferFull & ~nCsDataOut)| ~bufferFull);
|
|
|
always @(posedge clk, negedge nReset) begin
|
always @(posedge clk, negedge nReset) begin
|
if(~nReset) begin
|
if(~nReset) begin
|
bufferFull <= 1'b0;
|
bufferFull <= 1'b0;
|
flagsReg <= 1'b0;
|
flagsReg <= 1'b0;
|
txPending <= 1'b0;
|
txPending <= 1'b0;
|
end else begin
|
end else begin
|
if(ackFlags) begin
|
if(ackFlags) begin
|
dataReg <= rxData;
|
dataReg <= rxData;
|
flagsReg <= {overrunErrorFlag, frameErrorFlag};
|
flagsReg <= {overrunErrorFlag, frameErrorFlag};
|
if(rxFlagsSet)
|
if(rxFlagsSet)
|
bufferFull <= 1'b1;
|
bufferFull <= 1'b1;
|
else
|
else
|
bufferFull <= 1'b0;
|
bufferFull <= 1'b0;
|
end else if(txPending) begin
|
end else if(txPending) begin
|
if(waitTxFull0) begin
|
if(waitTxFull0) begin
|
if(~txFull)
|
if(~txFull)
|
waitTxFull0 <= 1'b0;
|
waitTxFull0 <= 1'b0;
|
end else if(txFull) begin//tx actually started, clear txPending and free buffer
|
end else if(txFull) begin//tx actually started, clear txPending and free buffer
|
txPending <= 1'b0;
|
txPending <= 1'b0;
|
bufferFull <= 1'b0; //buffer is empty
|
bufferFull <= 1'b0; //buffer is empty
|
end
|
end
|
end else if(~nCsDataOut) begin
|
end else if(~nCsDataOut) begin
|
bufferFull <= 1'b0;
|
bufferFull <= 1'b0;
|
end else if(~nWeDataIn) begin
|
end else if(~nWeDataIn) begin
|
dataReg <= dataIn;
|
dataReg <= dataIn;
|
bufferFull <= 1'b1;
|
bufferFull <= 1'b1;
|
txPending <= 1'b1;
|
txPending <= 1'b1;
|
waitTxFull0 <= txFull;
|
waitTxFull0 <= txFull;
|
end
|
end
|
end
|
end
|
end
|
end
|
|
|
BasicHalfDuplexUart #(
|
BasicHalfDuplexUart #(
|
.DIVIDER_WIDTH(DIVIDER_WIDTH),
|
.DIVIDER_WIDTH(DIVIDER_WIDTH),
|
.CLOCK_PER_BIT_WIDTH(CLOCK_PER_BIT_WIDTH)
|
.CLOCK_PER_BIT_WIDTH(CLOCK_PER_BIT_WIDTH)
|
)
|
)
|
uart (
|
uart (
|
.rxData(rxData),
|
.rxData(rxData),
|
.overrunErrorFlag(overrunErrorFlag),
|
.overrunErrorFlag(overrunErrorFlag),
|
.dataOutReadyFlag(dataOutReadyFlag),
|
.dataOutReadyFlag(dataOutReadyFlag),
|
.frameErrorFlag(frameErrorFlag),
|
.frameErrorFlag(frameErrorFlag),
|
.txRun(txRun),
|
.txRun(txRun),
|
.endOfRx(endOfRx),
|
.endOfRx(endOfRx),
|
.rxRun(rxRun),
|
.rxRun(rxRun),
|
.rxStartBit(rxStartBit),
|
.rxStartBit(rxStartBit),
|
.txFull(txFull),
|
.txFull(txFull),
|
.isTx(isTx),
|
.isTx(isTx),
|
.serialIn(serialIn),
|
.serialIn(serialIn),
|
.serialOut(serialOut),
|
.serialOut(serialOut),
|
.comClk(comClk),
|
.comClk(comClk),
|
.txData(txData),
|
.txData(txData),
|
.clocksPerBit(clocksPerBit),
|
.clocksPerBit(clocksPerBit),
|
.stopBit2(stopBit2),
|
.stopBit2(stopBit2),
|
.oddParity(oddParity),
|
.oddParity(oddParity),
|
.msbFirst(msbFirst),
|
.msbFirst(msbFirst),
|
.startTx(txPending),
|
.startTx(txPending),
|
.ackFlags(ackFlags),
|
.ackFlags(ackFlags),
|
.clkPerCycle(clkPerCycle),
|
.clkPerCycle(clkPerCycle),
|
.clk(clk),
|
.clk(clk),
|
.nReset(nReset)
|
.nReset(nReset)
|
);
|
);
|
|
|
endmodule
|
endmodule
|
`default_nettype wire
|
`default_nettype wire
|
|
|