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

Subversion Repositories openrisc

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

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 462 julius
SC_HAS_PROCESS(UartSC);
34 42 julius
 
35 6 julius
//! Constructor for the Uart system C model
36
 
37
//! @param[in] name  Name of this module, passed to the parent constructor.
38
// Todo: Probably some sort of scaler parameter
39
 
40 462 julius
UartSC::UartSC(sc_core::sc_module_name name):
41
sc_module(name)
42 6 julius
{
43
 
44 462 julius
        SC_METHOD(checkTx);
45
        dont_initialize();
46
        sensitive << clk.pos();
47
        //sensitive << uarttx;
48 6 julius
 
49 462 julius
}                               // UartSC ()
50
 
51
void
52
 UartSC::initUart(int clk_freq_hz,      // Presume in NS
53
                  int uart_baud)
54 6 julius
{
55 462 julius
        // Calculate number of clocks per UART bit
56
        clocks_per_bit = (int)(clk_freq_hz / uart_baud);
57
        bits_received = 0;
58 42 julius
#ifdef UART_SC_DEBUG
59 462 julius
        printf
60
            ("UartSC Initialised: Sys. clk. freq.: %d Hz, Baud: %d, cpb: %d\n",
61
             clk_freq_hz, uart_baud, clocks_per_bit);
62 42 julius
#endif
63 462 julius
}
64 6 julius
 
65
// Maybe do this with threads instead?!
66 462 julius
void UartSC::checkTx()
67
{
68 6 julius
 
69 42 julius
#ifdef UART_SC_DEBUG
70 462 julius
        //printf("Uart TX activity: level is : 0x%x\n", uarttx.read()&1);
71 42 julius
#endif
72 462 julius
 
73
        // Check the number of bits received
74
        if (bits_received == 0) {
75
                // Check if tx is low
76
                if ((uarttx.read() & 1) == 0) {
77
                        // Line pulled low, begin receive of new char
78
                        current_char = 0;
79
                        // Start 
80
                        counter = 1;
81
                        bits_received++;        // We got the start bit
82 42 julius
#ifdef UART_SC_DEBUG
83 462 julius
                        cout << "UartSC checkTx: got start bit at time " <<
84
                            sc_time_stamp() << endl;
85 42 julius
#endif
86 462 julius
                }
87
        } else if (bits_received > 0 && bits_received < 9) {
88
                // Check the counter - see if it's time to sample the line
89
                // We do an extra half-bit delay on first bit read
90
                if (((bits_received == 1) &&
91
                     (counter == (clocks_per_bit + (clocks_per_bit / 2)))) ||
92
                    ((bits_received > 1) && (counter == clocks_per_bit))) {
93
                        //printf("UartSC checkTx: read bit %d as 0x%x at time", bits_received, uarttx.read()&1);
94
                        //cout << sc_time_stamp() << endl;
95 6 julius
 
96 462 julius
                        // Shift in the current value of the tx into our char
97
                        current_char |=
98
                            ((uarttx.read() & 1) << (bits_received - 1));
99
                        // Reset the counter
100
                        counter = 1;
101
                        // Increment bit number
102
                        bits_received++;
103
                } else
104
                        counter++;
105
        } else if (bits_received == 9) {
106
                // Now check for stop bit 1
107
                if (counter == clocks_per_bit) {
108
                        // Check that the value is 1 - this should be the stop bit
109
                        if ((uarttx.read() & 1) != 1) {
110
                                printf("UART TX framing error at time\n");
111
                                cout << sc_time_stamp() << endl;
112 6 julius
 
113 462 julius
                                // Perhaps do something else here to deal with this
114
                                bits_received = 0;
115
                                counter = 0;
116
                        } else {
117
                                // Print the char
118 42 julius
#ifdef UART_SC_DEBUG
119 462 julius
                                printf("Char received: 0x%2x time: ",
120
                                       current_char);
121
                                cout << sc_time_stamp() << endl;
122 42 julius
#endif
123 462 julius
                                // cout'ing the char didn't work for some systems - jb 090613ol
124
                                //cout << current_char;
125
                                printf("%c", current_char);
126 42 julius
 
127 462 julius
                                bits_received = 0;
128
                                counter = 0;
129
                        }
130
                } else
131
                        counter++;
132 6 julius
        }
133
}

powered by: WebSVN 2.1.0

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