| 1 |
2 |
sinclairrf |
################################################################################
|
| 2 |
|
|
#
|
| 3 |
9 |
sinclairrf |
# Copyright 2012-2015, Sinclair R.F., Inc.
|
| 4 |
2 |
sinclairrf |
#
|
| 5 |
|
|
################################################################################
|
| 6 |
|
|
|
| 7 |
|
|
import math;
|
| 8 |
|
|
import re;
|
| 9 |
|
|
|
| 10 |
|
|
from ssbccPeripheral import SSBCCperipheral
|
| 11 |
6 |
sinclairrf |
from ssbccUtil import IsPowerOf2;
|
| 12 |
2 |
sinclairrf |
from ssbccUtil import SSBCCException;
|
| 13 |
|
|
|
| 14 |
|
|
class UART_Tx(SSBCCperipheral):
|
| 15 |
|
|
"""
|
| 16 |
|
|
Transmit side of a UART:
|
| 17 |
|
|
1 start bit
|
| 18 |
|
|
8 data bits
|
| 19 |
|
|
1 or 2 stop bits\n
|
| 20 |
|
|
Usage:
|
| 21 |
|
|
PERIPHERAL UART_Tx outport=O_outport_name \\
|
| 22 |
|
|
outstatus=I_outstatus_name \\
|
| 23 |
|
|
baudmethod={clk/rate|count} \\
|
| 24 |
|
|
[outsignal=o_name] \\
|
| 25 |
|
|
[noOutFIFO|outFIFO=n] \\
|
| 26 |
6 |
sinclairrf |
[{CTS|CTSn}=i_cts_name] \\
|
| 27 |
2 |
sinclairrf |
[nStop={1|2}]\n
|
| 28 |
|
|
Where:
|
| 29 |
|
|
outport=O_outport_name
|
| 30 |
|
|
specifies the symbol used by the outport instruction to write a byte to
|
| 31 |
|
|
the peripheral
|
| 32 |
|
|
Note: The name must start with "O_".
|
| 33 |
|
|
outstatus=I_outstatus_name
|
| 34 |
|
|
specifies the symbol used by the inport instruction to get the status of
|
| 35 |
|
|
the output side of the peripheral
|
| 36 |
|
|
Note: The name must start with "I_".
|
| 37 |
|
|
baudmethod
|
| 38 |
|
|
specifies the method to generate the desired bit rate:
|
| 39 |
|
|
1st method: clk/rate
|
| 40 |
|
|
clk is the frequency of "i_clk" in Hz
|
| 41 |
|
|
a number will be interpreted as the clock frequency in Hz
|
| 42 |
9 |
sinclairrf |
a symbol will be interpreted as a constant or a parameter
|
| 43 |
|
|
Note: the symbol must be declared with the CONSTANT, LOCALPARARM,
|
| 44 |
|
|
or PARAMETER configuration command.
|
| 45 |
2 |
sinclairrf |
rate is the desired baud rate
|
| 46 |
|
|
this is specified as per "clk"
|
| 47 |
|
|
2nd method:
|
| 48 |
|
|
specify the number of "i_clk" clock cycles between bit edges
|
| 49 |
|
|
Note: clk, rate, and count can be parameters or constants. For example,
|
| 50 |
|
|
the following uses the parameter G_CLK_FREQ_HZ for the clock
|
| 51 |
|
|
frequency and a hard-wired baud rate of 9600:
|
| 52 |
|
|
"baudmethod=G_CLK_FREQ_HZ/9600".
|
| 53 |
|
|
Note: The numeric values can have Verilog-style '_' separators between
|
| 54 |
|
|
the digits. For example, 100_000_000 represents 100 million.
|
| 55 |
|
|
outsignal=o_name
|
| 56 |
|
|
optionally specifies the name of the output signal
|
| 57 |
|
|
Default: o_UART_Tx
|
| 58 |
|
|
noOutFIFO
|
| 59 |
|
|
optionally state that the peripheral will not have an output FIFO
|
| 60 |
|
|
Note: This is the default.
|
| 61 |
|
|
outFIFO=n
|
| 62 |
|
|
optionally add a FIFO of depth n to the output side of the UART
|
| 63 |
|
|
Note: n must be a power of 2.
|
| 64 |
6 |
sinclairrf |
CTS=i_cts_name or CTSn=i_cts_name
|
| 65 |
|
|
optionally specify an input handshake signal to control whether or not the
|
| 66 |
|
|
peripheral transmits data
|
| 67 |
|
|
Note: If CTS is specified then the transmitter is active when i_cts_name
|
| 68 |
|
|
is high. If CTSn is specified then the transmitter is active when
|
| 69 |
|
|
i_cts_name is low.
|
| 70 |
|
|
Note: The default, i.e., neither CTS nor CTSn is specified, is to always
|
| 71 |
|
|
enable the transmitter.
|
| 72 |
|
|
Note: If there is no FIFO and the CTS/CTSn handshake indicates that the
|
| 73 |
|
|
data flow is disabled, then the busy signal will be high and the
|
| 74 |
|
|
processor code must not transmit the next byte.
|
| 75 |
2 |
sinclairrf |
nStop=n
|
| 76 |
|
|
optionally configure the peripheral for n stop bits
|
| 77 |
|
|
default: 1 stop bit
|
| 78 |
|
|
Note: n must be 1 or 2
|
| 79 |
|
|
Note: the peripheral does not accept 1.5 stop bits\n
|
| 80 |
|
|
The following ports are provided by this peripheral:
|
| 81 |
|
|
O_outport_name
|
| 82 |
|
|
output the next 8-bit value to transmit or to queue for transmission
|
| 83 |
|
|
Note: If there is no output FIFO or if there is an output FIFO and this
|
| 84 |
|
|
write would cause a FIFO overflow, then this byte will be
|
| 85 |
|
|
discarded.
|
| 86 |
|
|
I_outstatus_name
|
| 87 |
|
|
input the status of the output side of the peripheral
|
| 88 |
|
|
bit 0: output busy
|
| 89 |
|
|
this bit will be high when the output side of the peripheral cannot
|
| 90 |
|
|
accept more writes
|
| 91 |
|
|
Note: If there is no FIFO this means that the peripheral is still
|
| 92 |
|
|
transmitting the last byte. If there is an output FIFO it means
|
| 93 |
|
|
that it is full.\n
|
| 94 |
|
|
Note: "Busy" is used rather that "ready" to facilitate loops that wait
|
| 95 |
|
|
for a not-busy status to send the next byte. See the examples below.\n
|
| 96 |
|
|
WARNING: The peripheral is very simple and does not protect against writing a
|
| 97 |
|
|
new value in the middle of a transmition or writing to a full FIFO.
|
| 98 |
|
|
Adding such logic would be contrary to the design principle of
|
| 99 |
|
|
keeping the HDL small and relying on the assembly code to provide
|
| 100 |
|
|
the protection.\n
|
| 101 |
|
|
Example: Configure the UART for 115200 baud using a 100 MHz clock and
|
| 102 |
|
|
transmit the message "Hello World!"\n
|
| 103 |
|
|
Within the processor architecture file include the configuration command:\n
|
| 104 |
|
|
PERIPHERAL UART_Tx O_UART_TX I_UART_TX baudmethod=100_000_000/115200\n
|
| 105 |
|
|
Use the following assembly code to transmit the message "Hello World!".
|
| 106 |
|
|
This transmits the entire message whether or not the peripheral has a FIFO.\n
|
| 107 |
|
|
N"Hello World!\\r\\n"
|
| 108 |
|
|
:loop .outport(O_UART_TX) :wait .inport(I_UART_TX_BUSY) .jumpc(wait) .jumpc(loop,nop) drop
|
| 109 |
|
|
"""
|
| 110 |
|
|
|
| 111 |
|
|
def __init__(self,peripheralFile,config,param_list,loc):
|
| 112 |
|
|
# Use the externally provided file name for the peripheral
|
| 113 |
|
|
self.peripheralFile = peripheralFile;
|
| 114 |
|
|
# Get the parameters.
|
| 115 |
6 |
sinclairrf |
allowables = (
|
| 116 |
|
|
( 'CTS', r'i_\w+$', None, ),
|
| 117 |
|
|
( 'CTSn', r'i_\w+$', None, ),
|
| 118 |
|
|
( 'baudmethod', r'\S+$', lambda v : self.RateMethod(config,v), ),
|
| 119 |
|
|
( 'noOutFIFO', None, None, ),
|
| 120 |
|
|
( 'nStop', r'[12]$', int, ),
|
| 121 |
9 |
sinclairrf |
( 'outFIFO', r'[1-9]\d*$', lambda v : self.IntPow2Method(config,v), ),
|
| 122 |
6 |
sinclairrf |
( 'outport', r'O_\w+$', None, ),
|
| 123 |
|
|
( 'outsignal', r'o_\w+$', None, ),
|
| 124 |
|
|
( 'outstatus', r'I_\w+$', None, ),
|
| 125 |
|
|
);
|
| 126 |
|
|
names = [a[0] for a in allowables];
|
| 127 |
2 |
sinclairrf |
for param_tuple in param_list:
|
| 128 |
|
|
param = param_tuple[0];
|
| 129 |
6 |
sinclairrf |
if param not in names:
|
| 130 |
|
|
raise SSBCCException('Unrecognized parameter "%s" at %s' % (param,loc,));
|
| 131 |
|
|
param_test = allowables[names.index(param)];
|
| 132 |
|
|
self.AddAttr(config,param,param_tuple[1],param_test[1],loc,param_test[2]);
|
| 133 |
2 |
sinclairrf |
# Ensure the required parameters are provided.
|
| 134 |
|
|
for paramname in (
|
| 135 |
|
|
'baudmethod',
|
| 136 |
|
|
'outport',
|
| 137 |
|
|
'outstatus',
|
| 138 |
|
|
):
|
| 139 |
|
|
if not hasattr(self,paramname):
|
| 140 |
|
|
raise SSBCCException('Required parameter "%s" is missing at %s' % (paramname,loc,));
|
| 141 |
|
|
# Set optional parameters.
|
| 142 |
|
|
for optionalpair in (
|
| 143 |
6 |
sinclairrf |
( 'nStop', 1, ),
|
| 144 |
|
|
( 'outsignal', 'o_UART_Tx', ),
|
| 145 |
2 |
sinclairrf |
):
|
| 146 |
|
|
if not hasattr(self,optionalpair[0]):
|
| 147 |
|
|
setattr(self,optionalpair[0],optionalpair[1]);
|
| 148 |
|
|
# Ensure exclusive pair configurations are set and consistent.
|
| 149 |
|
|
for exclusivepair in (
|
| 150 |
6 |
sinclairrf |
( 'CTS', 'CTSn', None, None, ),
|
| 151 |
|
|
( 'noOutFIFO', 'outFIFO', 'noOutFIFO', True, ),
|
| 152 |
2 |
sinclairrf |
):
|
| 153 |
|
|
if hasattr(self,exclusivepair[0]) and hasattr(self,exclusivepair[1]):
|
| 154 |
|
|
raise SSBCCException('Only one of "%s" and "%s" can be specified at %s' % (exclusivepair[0],exclusivepair[1],loc,));
|
| 155 |
6 |
sinclairrf |
if not hasattr(self,exclusivepair[0]) and not hasattr(self,exclusivepair[1]) and exclusivepair[2]:
|
| 156 |
2 |
sinclairrf |
setattr(self,exclusivepair[2],exclusivepair[3]);
|
| 157 |
6 |
sinclairrf |
# Convert configurations to alternative format.
|
| 158 |
|
|
for equivalent in (
|
| 159 |
|
|
( 'noOutFIFO', 'outFIFO', 0, ),
|
| 160 |
|
|
):
|
| 161 |
|
|
if hasattr(self,equivalent[0]):
|
| 162 |
|
|
delattr(self,equivalent[0]);
|
| 163 |
|
|
setattr(self,equivalent[1],equivalent[2]);
|
| 164 |
2 |
sinclairrf |
# Set the string used to identify signals associated with this peripheral.
|
| 165 |
|
|
self.namestring = self.outsignal;
|
| 166 |
|
|
# Add the I/O port, internal signals, and the INPORT and OUTPORT symbols for this peripheral.
|
| 167 |
6 |
sinclairrf |
for ioEntry in (
|
| 168 |
|
|
( 'outsignal', 1, 'output', ),
|
| 169 |
|
|
( 'CTS', 1, 'input', ),
|
| 170 |
|
|
( 'CTSn', 1, 'input', ),
|
| 171 |
|
|
):
|
| 172 |
|
|
if hasattr(self,ioEntry[0]):
|
| 173 |
|
|
config.AddIO(getattr(self,ioEntry[0]),ioEntry[1],ioEntry[2],loc);
|
| 174 |
9 |
sinclairrf |
config.AddSignalWithInit('s__%s__Tx' % self.namestring,8,None,loc);
|
| 175 |
|
|
config.AddSignal('s__%s__Tx_busy' % self.namestring,1,loc);
|
| 176 |
|
|
config.AddSignalWithInit('s__%s__Tx_wr' % self.namestring,1,None,loc);
|
| 177 |
2 |
sinclairrf |
config.AddOutport((self.outport,False,
|
| 178 |
6 |
sinclairrf |
('s__%s__Tx' % self.namestring,8,'data',),
|
| 179 |
|
|
('s__%s__Tx_wr' % self.namestring,1,'strobe',),
|
| 180 |
|
|
),loc);
|
| 181 |
2 |
sinclairrf |
config.AddInport((self.outstatus,
|
| 182 |
6 |
sinclairrf |
('s__%s__Tx_busy' % self.namestring,1,'data',),
|
| 183 |
|
|
),loc);
|
| 184 |
2 |
sinclairrf |
# Add the 'clog2' function to the processor (if required).
|
| 185 |
|
|
config.functions['clog2'] = True;
|
| 186 |
|
|
|
| 187 |
|
|
def GenVerilog(self,fp,config):
|
| 188 |
|
|
for bodyextension in ('.v',):
|
| 189 |
|
|
body = self.LoadCore(self.peripheralFile,bodyextension);
|
| 190 |
|
|
for subpair in (
|
| 191 |
9 |
sinclairrf |
( r'\bL__', 'L__@NAME@__', ),
|
| 192 |
|
|
( r'\bgen__', 'gen__@NAME@__', ),
|
| 193 |
|
|
( r'\bs__', 's__@NAME@__', ),
|
| 194 |
|
|
( r'@BAUDMETHOD@', str(self.baudmethod), ),
|
| 195 |
|
|
( r'@ENABLED@', self.CTS if hasattr(self,'CTS') else ('!%s' % self.CTSn) if hasattr(self,'CTSn') else '1\'b1', ),
|
| 196 |
|
|
( r'@NSTOP@', str(self.nStop), ),
|
| 197 |
|
|
( r'@OUTFIFO@', str(self.outFIFO), ),
|
| 198 |
|
|
( r'@NAME@', self.namestring, ),
|
| 199 |
6 |
sinclairrf |
):
|
| 200 |
2 |
sinclairrf |
body = re.sub(subpair[0],subpair[1],body);
|
| 201 |
|
|
body = self.GenVerilogFinal(config,body);
|
| 202 |
|
|
fp.write(body);
|