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

Subversion Repositories openriscdevboard

[/] [openriscdevboard/] [trunk/] [cyc2-openrisc/] [model/] [uart_rx.v] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 sfielding
//-----------------------------------------------------
2
// Design Name : uart_rx 
3
// File Name   : uart_rx.v
4
// Function    : Simple UART
5
// Coder       : Deepak Kumar Tala, modified by Steve Fielding
6
//-----------------------------------------------------
7
module uart_rx (
8
reset          ,
9
rxclk          ,
10
rx_in
11
 
12
);
13
// Port declarations
14
input        reset          ;
15
input        rxclk          ;
16
input        rx_in          ;
17
 
18
// Internal Variables 
19
reg [7:0]    rx_reg         ;
20
reg          uld_rx_data    ;
21
reg          rx_enable      ;
22
reg [7:0]    rx_data        ;
23
reg [3:0]    rx_sample_cnt  ;
24
reg [3:0]    rx_cnt         ;
25
reg          rx_frame_err   ;
26
reg          rx_over_run    ;
27
reg          rx_empty       ;
28
reg          rx_empty_reg   ;
29
reg          rx_d1          ;
30
reg          rx_d2          ;
31
reg          rx_busy        ;
32
integer      uart_log_file_desc;
33
 
34
initial begin
35
  uart_log_file_desc = $fopen("uart.log") ;
36
  if ( uart_log_file_desc < 2 )
37
  begin
38
    $write("Could not open UART log file!\n") ;
39
    $finish ;
40
  end
41
  $fdisplay(uart_log_file_desc, "********* Start UART Monitor log file *************\n") ;
42
end
43
 
44
 
45
 
46
 
47
// UART RX Logic
48
always @ (posedge rxclk or posedge reset)
49
if (reset) begin
50
  rx_reg        <= 0;
51
  rx_data       <= 0;
52
  rx_sample_cnt <= 0;
53
  rx_cnt        <= 0;
54
  rx_frame_err  <= 0;
55
  rx_over_run   <= 0;
56
  rx_empty      <= 1;
57
  rx_d1         <= 1;
58
  rx_d2         <= 1;
59
  rx_busy       <= 0;
60
end else begin
61
  // Synchronize the asynch signal
62
  rx_d1 <= rx_in;
63
  rx_d2 <= rx_d1;
64
  // Uload the rx data
65
  if (uld_rx_data) begin
66
    rx_data  <= rx_reg;
67
    rx_empty <= 1;
68
  end
69
  // Receive data only when rx is enabled
70
  if (rx_enable) begin
71
    // Check if just received start of frame
72
    if (!rx_busy && !rx_d2) begin
73
      rx_busy       <= 1;
74
      rx_sample_cnt <= 1;
75
      rx_cnt        <= 0;
76
    end
77
    // Start of frame detected, Proceed with rest of data
78
    if (rx_busy) begin
79
       rx_sample_cnt <= rx_sample_cnt + 1;
80
       // Logic to sample at middle of data
81
       if (rx_sample_cnt == 7) begin
82
          if ((rx_d2 == 1) && (rx_cnt == 0)) begin
83
            rx_busy <= 0;
84
          end else begin
85
            rx_cnt <= rx_cnt + 1;
86
            // Start storing the rx data
87
            if (rx_cnt > 0 && rx_cnt < 9) begin
88
              rx_reg[rx_cnt - 1] <= rx_d2;
89
            end
90
            if (rx_cnt == 9) begin
91
               rx_busy <= 0;
92
               // Check if End of frame received correctly
93
               if (rx_d2 == 0) begin
94
                 rx_frame_err <= 1;
95
               end else begin
96
                 rx_empty     <= 0;
97
                 rx_frame_err <= 0;
98
                 // Check if last rx data was not unloaded,
99
                 rx_over_run  <= (rx_empty) ? 0 : 1;
100
               end
101
            end
102
          end
103
       end
104
    end
105
  end
106
  if (!rx_enable) begin
107
    rx_busy <= 0;
108
  end
109
end
110
 
111
// print rx data
112
always @(posedge rxclk or posedge reset) begin
113
  if (reset == 1'b1) begin
114
    uld_rx_data <= 1'b0;
115
    rx_enable <= 1'b1;
116
    rx_empty_reg <= 1'b1;
117
  end
118
  else begin
119
    rx_empty_reg <= rx_empty;
120
    if (rx_empty == 1'b0 && rx_empty_reg == 1'b1) begin
121
      uld_rx_data <= 1'b1;
122
      $fwrite(uart_log_file_desc,"%c",rx_reg);
123
    end
124
    else
125
      uld_rx_data <= 1'b0;
126
  end
127
end
128
 
129
endmodule
130
 

powered by: WebSVN 2.1.0

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