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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [orpsocv2/] [bench/] [sysc/] [src/] [UartSC.cpp] - Blame information for rev 178

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

Line No. Rev Author Line
1 6 julius
// ----------------------------------------------------------------------------
2
 
3
// SystemC Uart: implementation
4
 
5
// This file is part of the cycle accurate model of the OpenRISC 1000 based
6
// system-on-chip, ORPSoC, built using Verilator.
7
 
8
// This program is free software: you can redistribute it and/or modify it
9
// under the terms of the GNU Lesser General Public License as published by
10
// the Free Software Foundation, either version 3 of the License, or (at your
11
// option) any later version.
12
 
13
// This program is distributed in the hope that it will be useful, but WITHOUT
14
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16
// License for more details.
17
 
18
// You should have received a copy of the GNU Lesser General Public License
19
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 
21
// ----------------------------------------------------------------------------
22
 
23
// $Id: $
24
 
25
#include <iostream>
26
#include <iomanip>
27
#include <cmath>
28
 
29
#include "UartSC.h"
30
 
31 42 julius
//#define UART_SC_DEBUG
32 6 julius
 
33 42 julius
 
34 6 julius
SC_HAS_PROCESS( UartSC );
35
 
36
//! Constructor for the Uart system C model
37
 
38
//! @param[in] name  Name of this module, passed to the parent constructor.
39
// Todo: Probably some sort of scaler parameter
40
 
41
UartSC::UartSC (sc_core::sc_module_name   name):
42
  sc_module (name)
43
{
44
 
45
  SC_METHOD (checkTx);
46
  dont_initialize();
47
  sensitive << clk.pos();
48
  //sensitive << uarttx;
49
 
50
}       // UartSC ()
51
 
52
void
53
UartSC::initUart (int clk_freq_hz, // Presume in NS
54
                  int uart_baud
55
                  )
56
{
57
  // Calculate number of clocks per UART bit
58
  clocks_per_bit = (int)(clk_freq_hz/uart_baud);
59 42 julius
  bits_received=0;
60
#ifdef UART_SC_DEBUG
61
  printf("UartSC Initialised: Sys. clk. freq.: %d Hz, Baud: %d, cpb: %d\n", clk_freq_hz, uart_baud, clocks_per_bit);
62
#endif
63 6 julius
}
64
 
65
 
66
// Maybe do this with threads instead?!
67
void
68
UartSC::checkTx () {
69
 
70 42 julius
#ifdef UART_SC_DEBUG
71 6 julius
  //printf("Uart TX activity: level is : 0x%x\n", uarttx.read()&1);
72 42 julius
#endif
73 6 julius
 
74
  // Check the number of bits received
75
  if (bits_received==0)
76
    {
77
      // Check if tx is low
78
      if ((uarttx.read()&1) == 0)
79
        {
80
          // Line pulled low, begin receive of new char
81
          current_char = 0;
82
          // Start 
83
          counter = 1;
84
          bits_received++; // We got the start bit
85 42 julius
#ifdef UART_SC_DEBUG
86
          cout << "UartSC checkTx: got start bit at time " << sc_time_stamp() << endl;
87
#endif
88 6 julius
        }
89
    }
90
  else if (bits_received > 0 && bits_received < 9)
91
    {
92
      // Check the counter - see if it's time to sample the line
93
      // We do an extra half-bit delay on first bit read
94
      if ( ((bits_received==1) &&
95
            (counter == (clocks_per_bit + (clocks_per_bit/2)))) ||
96
           ((bits_received > 1) && (counter == clocks_per_bit)) )
97
        {
98
          //printf("UartSC checkTx: read bit %d as 0x%x at time", bits_received, uarttx.read()&1);
99
          //cout << sc_time_stamp() << endl;
100
 
101
          // Shift in the current value of the tx into our char
102
          current_char |= ((uarttx.read() & 1) << (bits_received-1));
103
          // Reset the counter
104
          counter = 1;
105
          // Increment bit number
106
          bits_received++;
107
        }
108
      else
109
        counter++;
110
    }
111
  else if (bits_received == 9)
112
    {
113
      // Now check for stop bit 1
114
      if (counter == clocks_per_bit)
115
        {
116
          // Check that the value is 1 - this should be the stop bit
117
          if ((uarttx.read() & 1) != 1)
118
            {
119
              printf("UART TX framing error at time\n");
120
              cout << sc_time_stamp() << endl;
121
 
122
              // Perhaps do something else here to deal with this
123
              bits_received = 0;
124
              counter = 0;
125
            }
126
          else
127
            {
128
              // Print the char
129 42 julius
#ifdef UART_SC_DEBUG
130
              printf("Char received: 0x%2x time: ", current_char);
131
              cout << sc_time_stamp() << endl;
132
#endif
133
              // cout'ing the char didn't work for some systems - jb 090613ol
134
              //cout << current_char;
135
              printf("%c",current_char);
136
 
137 6 julius
              bits_received = 0;
138
              counter = 0;
139
            }
140
        }
141
      else
142
        counter++;
143
    }
144
}
145
 
146
 
147
 

powered by: WebSVN 2.1.0

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