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

Subversion Repositories test_project

[/] [test_project/] [trunk/] [bench/] [sysc/] [src/] [UartSC.cpp] - Blame information for rev 51

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

Line No. Rev Author Line
1 51 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
// Todo: calculate clocks_per_bit properly! At the moment it is hardcoded!!
26
 
27
#include <iostream>
28
#include <iomanip>
29
#include <cmath>
30
 
31
#include "UartSC.h"
32
 
33
 
34
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
  // This doesn't work! Fix it...
59
  //clocks_per_bit = (int)(clk_freq_hz/uart_baud);
60
  //printf("initUart: clocks_per_bit: %d\n", clocks_per_bit);
61
 
62
  // debug: hardset for now
63
  clocks_per_bit= 432;
64
 
65
}
66
 
67
 
68
// Maybe do this with threads instead?!
69
void
70
UartSC::checkTx () {
71
 
72
  //printf("Uart TX activity: level is : 0x%x\n", uarttx.read()&1);
73
 
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
          //cout << "UartSC checkTx: got start bit at time " << sc_time_stamp() << endl;
86
        }
87
    }
88
  else if (bits_received > 0 && bits_received < 9)
89
    {
90
      // Check the counter - see if it's time to sample the line
91
      // We do an extra half-bit delay on first bit read
92
      if ( ((bits_received==1) &&
93
            (counter == (clocks_per_bit + (clocks_per_bit/2)))) ||
94
           ((bits_received > 1) && (counter == clocks_per_bit)) )
95
        {
96
          //printf("UartSC checkTx: read bit %d as 0x%x at time", bits_received, uarttx.read()&1);
97
          //cout << sc_time_stamp() << endl;
98
 
99
          // Shift in the current value of the tx into our char
100
          current_char |= ((uarttx.read() & 1) << (bits_received-1));
101
          // Reset the counter
102
          counter = 1;
103
          // Increment bit number
104
          bits_received++;
105
        }
106
      else
107
        counter++;
108
    }
109
  else if (bits_received == 9)
110
    {
111
      // Now check for stop bit 1
112
      if (counter == clocks_per_bit)
113
        {
114
          // Check that the value is 1 - this should be the stop bit
115
          if ((uarttx.read() & 1) != 1)
116
            {
117
              printf("UART TX framing error at time\n");
118
              cout << sc_time_stamp() << endl;
119
 
120
              // Perhaps do something else here to deal with this
121
              bits_received = 0;
122
              counter = 0;
123
            }
124
          else
125
            {
126
              // Print the char
127
              //printf("Char received: 0x%2x time: ", current_char);
128
              //cout << sc_time_stamp() << endl;
129
              cout << current_char;
130
              bits_received = 0;
131
              counter = 0;
132
            }
133
        }
134
      else
135
        counter++;
136
    }
137
}
138
 
139
 
140
 

powered by: WebSVN 2.1.0

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