Guest User

Untitled

a guest
Jan 31st, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module RS232(
  2.     input CLOCK_50, RESET, RX, SEND_DATA, DATA_READ,
  3.     input [7:0] DATA_IN,
  4.    
  5.     output TX, DATA_READY, DATA_SENT, SAMPLE,
  6.     output [7:0] DATA_OUT  
  7.     );
  8.    
  9.     // This module is a basic RS-232 transmitter and receiver.  The TX and RX functions are completely separte and do not communicate.
  10.     //
  11.     // Parameters:
  12.     //   Baud = 3000000
  13.     //   One start bit
  14.     //   One stop bit
  15.     //   Inverted
  16.     //
  17.     // Operation:
  18.     //    RX
  19.     //    This module will watch the RX line for the start bit.  If it receives a start bit it will record the data and push it on a FIFO
  20.     //    When data is ready it will assert the DATA_READY output.  It will hold this high until the DATA_READ input is asserted.  Nothing
  21.     //    here is guaranteed to happen in one cycle.  The reading of data takes precedence over handshaking output data.  Data will be held
  22.     //    until new data over writes it.
  23.    
  24.     // Define internal registers
  25.     reg[3:0] data_count, data_count_next, state, state_next;
  26.     reg tx_int, tx_int_next, flag, flag_next, data_ready_int, data_ready_int_next, data_sent_int, data_sent_int_next, sample_int, sample_int_next;
  27.     reg[5:0] counter, counter_next, read_counter, read_counter_next;
  28.     reg[7:0] data_out_int, data_out_int_next;
  29.    
  30.     // Define connections
  31.     //assign TX = tx_int_next;
  32.     assign DATA_READY = data_ready_int;
  33.     assign DATA_SENT = data_sent_int;
  34.     assign DATA_OUT = data_out_int;
  35.     assign SAMPLE = sample_int;
  36.    
  37.     // Define states
  38.     localparam [3:0]
  39.         Init            = 0,
  40.         Idle            = 1,
  41.         Start_Bit   = 2,
  42.         Read            = 3,
  43.         Stop_Bit        = 4,
  44.         Wait            = 5;
  45.    
  46.     // Define sequential logic
  47.     always @(posedge CLOCK_50)
  48.     begin
  49.         if(!RESET)
  50.         begin
  51.             data_count      <= 0;
  52.             state           <= Init;
  53.             tx_int          <= 0;  // Check
  54.             flag                <= 0;
  55.             data_ready_int <= 0;
  56.             data_sent_int   <= 0;
  57.             counter             <= 0;
  58.             data_out_int    <= 0;
  59.             read_counter    <= 0;
  60.             sample_int      <= 0;
  61.         end
  62.         else
  63.         begin
  64.             data_count      <= data_count_next;
  65.             state               <= state_next;
  66.             tx_int          <= tx_int_next;
  67.             flag                <= flag_next;
  68.             data_ready_int <= data_ready_int_next;
  69.             data_sent_int   <= data_sent_int_next; 
  70.             counter         <= counter_next;
  71.             data_out_int    <= data_out_int_next;
  72.             read_counter    <= read_counter_next;
  73.             sample_int      <= sample_int_next;
  74.         end
  75.     end
  76.    
  77.     // Define combintation logic
  78.     always @*
  79.     begin
  80.         // Define default transitions
  81.         data_count_next = data_count;
  82.         data_out_int_next = data_out_int;
  83.         state_next = state;
  84.         tx_int_next = tx_int;
  85.         flag_next = flag;
  86.         data_ready_int_next = data_ready_int;
  87.         data_sent_int_next = data_sent_int;
  88.         counter_next = counter;
  89.         read_counter_next = read_counter;
  90.         sample_int_next = sample_int;
  91.        
  92.         // Define state machine
  93.         case(state)
  94.             Init:   // Intialize all registers
  95.             begin
  96.                 data_count_next = 0;
  97.                 data_out_int_next = 0;
  98.                 tx_int_next = 0;  // Check
  99.                 flag_next = 0;
  100.                 data_ready_int_next = 0;
  101.                 data_sent_int_next = 0;
  102.                 counter_next = 0;  
  103.                 read_counter_next = 0;
  104.                 sample_int_next = 0;
  105.                 state_next = Idle;     
  106.             end
  107.            
  108.             Idle:  // Main Idle process.  Each cycle in the Idle process the RX line is checked first for the start bit.
  109.             begin
  110.                 data_out_int_next = data_out_int;  // Hold the previous data.
  111.                 sample_int_next = 1;
  112.                
  113.                 if(!RX)   // Check if the start bit has come(low)
  114.                 begin
  115.                     counter_next = 0;
  116.                     state_next = Start_Bit;
  117.                 end
  118.                 else     // If the start bit is not here continue
  119.                 begin
  120.                     if(data_count == 0)  // If data count is zero, then no data is available, stay in idle and keep data_ready low
  121.                     begin
  122.                         data_ready_int_next = 0;
  123.                         state_next = Idle;
  124.                     end
  125.                     else   // If data count is not zero, then data needs to be given to controller
  126.                     begin
  127.                         if(flag)  // The purpose of this flag is to determine if the data has been read already by the controller.  
  128.                         begin     //   If flag is high, then the data has been read by the controller.  If so, we pull data ready low, then wait for DATA_READ to go low.  Finishing the hand shake
  129.                             if(DATA_READ)  // Wait for DATA_READ to go low
  130.                             begin
  131.                                 state_next = Idle;
  132.                             end
  133.                             else  // Read complete, reset
  134.                             begin
  135.                                 data_count_next = data_count - 1;
  136.                                 flag_next = 0;
  137.                                 data_ready_int_next = 0;
  138.                                
  139.                                 state_next = Idle;
  140.                             end
  141.                         end  
  142.                         else  // If flag is low, then data has not been read.  Assert DATA_READY then wait for DATA_READ to go high.
  143.                         begin
  144.                             if(DATA_READ)  // If DATA_READ goes high, then assert flag and pull low DATA_READY
  145.                             begin
  146.                                 flag_next = 1;
  147.                                 data_ready_int_next = 0;
  148.                                 state_next = Idle;
  149.                             end
  150.                             else   // Wait for DATA_READ to go high.  
  151.                             begin
  152.                                 flag_next = 0;
  153.                                 data_ready_int_next = 1;
  154.                                 state_next = Idle;
  155.                             end
  156.                         end
  157.                     end
  158.                 end
  159.             end
  160.            
  161.             Start_Bit:  // Wait 20 cycles to get past start bit
  162.             begin
  163.                 sample_int_next = 0;
  164.                 counter_next = counter + 1;
  165.                 if(counter == 20)
  166.                 begin
  167.                     counter_next = 16;  // This is needed for convenience, tells it to read immediately in the next state.  
  168.                     read_counter_next = 0;
  169.                     state_next = Read;
  170.                 end
  171.                 else
  172.                 begin
  173.                     state_next = Start_Bit;
  174.                 end
  175.             end
  176.            
  177.             Read:  // Read 8 bits of data.  
  178.             begin
  179.                 counter_next = counter + 1;
  180.                 sample_int_next = 0;
  181.                
  182.                 if(read_counter == 8)  // Count 8 reads, then go to wait state to avoid stop bit.
  183.                 begin
  184.                     sample_int_next = 0;
  185.                     data_count_next = data_count + 1;
  186.                     read_counter_next = 0;
  187.                     counter_next = 0;
  188.                     state_next = Wait;
  189.                 end
  190.                 else
  191.                 begin
  192.                     if(counter == 16)  // Wait 16 cycles between reads.  
  193.                     begin
  194.                         data_out_int_next = {RX, data_out_int[7:1]};  // Shift data in.  
  195.                         counter_next = 0;
  196.                         read_counter_next = read_counter + 1;
  197.                         sample_int_next = 1;
  198.                         state_next = Read;
  199.                     end
  200.                     else
  201.                     begin
  202.                         state_next = Read;
  203.                     end
  204.                 end
  205.             end
  206.            
  207.             Wait:  // Wait for stop bit to pass before entering Idle state again.  
  208.             begin
  209.                 sample_int_next = 0;
  210.                 if(RX)
  211.                 begin
  212.                     sample_int_next = 0;
  213.                     state_next = Idle;
  214.                 end
  215.                 else
  216.                 begin
  217.                     state_next = Wait;
  218.                 end
  219.             end
  220.            
  221.             default: state_next = Init;
  222.         endcase
  223.    
  224.     end
  225.    
  226.    
  227. endmodule
Add Comment
Please, Sign In to add comment