OpenCores
URL https://opencores.org/ocsvn/ssbcc/ssbcc/trunk

Subversion Repositories ssbcc

[/] [ssbcc/] [trunk/] [core/] [9x8/] [peripherals/] [open_drain.py] - Blame information for rev 7

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 sinclairrf
################################################################################
2
#
3 6 sinclairrf
# Copyright 2012-2014, Sinclair R.F., Inc.
4 2 sinclairrf
#
5
################################################################################
6
 
7
from ssbccPeripheral import SSBCCperipheral
8
from ssbccUtil import SSBCCException;
9
 
10
class open_drain(SSBCCperipheral):
11
  """
12
  Implement an open-drain I/O suitable for direct connection to a pin.  This
13
  can, for example, be used as an I/O port for an I2C device.\n
14
  Usage:
15
    PERIPHERAL open_drain inport=I_name \\
16
                          outport=O_name \\
17
                          iosignal=io_name \\
18
                          [width=n]\n
19
  Where:
20
    inport=I_name
21
      is the inport symbol to read the pin
22
    outport=O_name
23
      is the outport symbol to write to the pin
24
      Note:  A "0" value activates the open drain while a "1" value releases the
25
             open drain.
26
    iosignal=io_name
27
      is the tri-state pin for the open-drain I/O buffer
28
      Note:  The initial value of the pin is "open."
29
    width=n
30
      is the optional width of the port
31
      Note:  The default is one bit\n
32
  The following OUTPORTs are provided by this peripheral:
33
    O_name
34
      this is the new output for the open drain I/O\n
35
  The following INPORTs are provided by this peripheral:
36
    I_name
37
      this reads the current value of the open drain I/O\n
38
  Example:  Configure two 1-bit ports implementing an I2C bus:\n
39
    Add the following to the architecture file:\n
40
    PORTCOMMENT I2C bus
41
    PERIPHERAL open_drain inport=I_SCL outport=O_SCL iosignal=io_scl
42
    PERIPHERAL open_drain inport=I_SDA outport=O_SDA iosignal=io_sda\n
43
    The following assembly will transmit the start condition for an I2C bus by
44
    pulling SDA low and then pulling SCL low.\n
45
    ; Set SDA low
46
 
47
    ; delay one fourth of a 400 kHz cycle (based on a 100 MHz clock)
48
    ${int(100.e6/400.e3/3)-1} :delay .jumpc(delay,1-) drop
49
    ; Set SCL low
50
 
51
    See the I2C examples for a complete demonstration of using the open_drain
52
    peripheral.
53
  """
54
 
55
  def __init__(self,peripheralFile,config,param_list,loc):
56
    # Use the externally provided file name for the peripheral
57
    self.peripheralFile = peripheralFile;
58 6 sinclairrf
    # Get the parameters.
59
    allowables = (
60
      ( 'inport',       r'I_\w+$',      None,   ),
61
      ( 'iosignal',     r'io_\w+$',     None,   ),
62
      ( 'outport',      r'O_\w+$',      None,   ),
63
      ( 'width',        r'[1-9]\d*$',   lambda v : self.PosInt(v,maxValue=config.Get('data_width')), ),
64
    );
65
    names = [a[0] for a in allowables];
66 2 sinclairrf
    for param_tuple in param_list:
67
      param = param_tuple[0];
68 6 sinclairrf
      if param not in names:
69
        raise SSBCCException('Unrecognized parameter "%s" at %s' % (param,loc,));
70
      param_test = allowables[names.index(param)];
71
      self.AddAttr(config,param,param_tuple[1],param_test[1],loc,param_test[2]);
72 2 sinclairrf
    # Ensure the required parameters are set.
73 6 sinclairrf
    for paramname in ('inport','iosignal','outport',):
74
      if not hasattr(self,paramname):
75
        raise SSBCCException('Required parameter "%s" is missing at %s' % (paramname,loc,));
76 2 sinclairrf
    # Set defaults for non-specified values.
77
    if not hasattr(self,'width'):
78
      self.width = 1;
79
    # Create the internal signal name and initialization.
80
    self.sname = 's__%s' % self.iosignal;
81
    sname_init = '%d\'b%s' % (self.width, '1'*self.width, );
82
    # Add the I/O port, internal signals, and the INPORT and OUTPORT symbols for this peripheral.
83
    config.AddIO(self.iosignal,self.width,'inout',loc);
84
    config.AddSignalWithInit(self.sname,self.width,None,loc);
85
    config.AddInport((self.inport,
86
                     (self.iosignal,self.width,'data',),
87
                    ),
88
                    loc);
89
    config.AddOutport((self.outport,False,
90
                      (self.sname,self.width,'data',sname_init,),
91
                     ),
92
                     loc);
93
 
94
  def GenVerilog(self,fp,config):
95
    body_1 = """//
96
// PERIPHERAL open_drain:  @NAME@
97
//
98
assign @IO_NAME@ = (@S_NAME@ == 1'b0) ? 1'b0 : 1'bz;
99
"""
100
    body_big = """//
101
// PERIPHERAL open_drain:  @NAME@
102
//
103
generate
104
genvar ix;
105
for (ix=0; ix<@WIDTH@; ix = ix+1) begin : gen_@NAME@
106
  assign @IO_NAME@[ix] = (@S_NAME@[ix] == 1'b0) ? 1'b0 : 1'bz;
107
end
108
endgenerate
109
"""
110
    if self.width == 1:
111
      body = body_1;
112
    else:
113
      body = body_big;
114 6 sinclairrf
    for subpair in (
115
        ( r'\bix\b',    'ix__@NAME@',           ),
116
        ( r'@IO_NAME@', self.iosignal,          ),
117
        ( r'@NAME@',    self.iosignal,          ),
118
        ( r'@S_NAME@',  self.sname,             ),
119
        ( r'@WIDTH@',   str(self.width),        ),
120
      ):
121
      body = re.sub(subpair[0],subpair[1],body);
122 2 sinclairrf
    body = self.GenVerilogFinal(config,body);
123
    fp.write(body);

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.