e22da962d611ade3e8c87e06977649c492d516da
[debian/gnuradio] / gr-radar-mono / src / fpga / lib / radar_control.v
1 // -*- verilog -*-
2 //
3 //  USRP - Universal Software Radio Peripheral
4 //
5 //  Copyright (C) 2007 Corgan Enterprises LLC
6 //
7 //  This program is free software; you can redistribute it and/or modify
8 //  it under the terms of the GNU General Public License as published by
9 //  the Free Software Foundation; either version 2 of the License, or
10 //  (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 51 Franklin Street, Boston, MA  02110-1301  USA
20 //
21
22 `include "../lib/radar_config.vh"
23
24 module radar_control(clk_i,saddr_i,sdata_i,s_strobe_i,
25                      reset_o,tx_side_o,dbg_o,
26                      tx_strobe_o,tx_ctrl_o,rx_ctrl_o,
27                      ampl_o,fstart_o,fincr_o);
28
29    // System interface
30    input         clk_i;         // Master clock @ 64 MHz
31    input  [6:0]  saddr_i;       // Configuration bus address
32    input  [31:0] sdata_i;       // Configuration bus data
33    input         s_strobe_i;    // Configuration bus write
34
35    // Control and configuration outputs
36    output        reset_o;
37    output        tx_side_o;
38    output        dbg_o;
39    output        tx_strobe_o;
40    output        tx_ctrl_o;
41    output        rx_ctrl_o;
42    output [15:0] ampl_o;
43    output [31:0] fstart_o;
44    output [31:0] fincr_o;
45    
46    // Internal configuration
47    wire          lp_ena;
48    wire          md_ena;
49    wire          dr_ena;
50    wire   [1:0]  chirps;
51    wire   [15:0] t_on;
52    wire   [15:0] t_sw;
53    wire   [15:0] t_look;
54    wire   [31:0] t_idle;
55
56    // Configuration from host
57    wire [31:0]   mode;
58    setting_reg #(`FR_RADAR_MODE)   sr_mode(.clock(clk_i),.reset(1'b0),.strobe(s_strobe_i),.addr(saddr_i),.in(sdata_i),
59                                            .out(mode));
60    assign reset_o   = mode[0];
61    assign tx_side_o = mode[1];
62    assign lp_ena    = mode[2];
63    assign md_ena    = mode[3];
64    assign dr_ena    = mode[4];
65    assign chirps    = mode[6:5];
66    assign dbg_o     = mode[7];
67    
68    setting_reg #(`FR_RADAR_TON)    sr_ton(.clock(clk_i),.reset(1'b0),.strobe(s_strobe_i),.addr(saddr_i),.in(sdata_i),
69                                           .out(t_on));
70    
71    setting_reg #(`FR_RADAR_TSW)    sr_tsw(.clock(clk_i),.reset(1'b0),.strobe(s_strobe_i),.addr(saddr_i),.in(sdata_i),
72                                           .out(t_sw));
73                                      
74    setting_reg #(`FR_RADAR_TLOOK)  sr_tlook(.clock(clk_i),.reset(1'b0),.strobe(s_strobe_i),.addr(saddr_i),.in(sdata_i),
75                                             .out(t_look));
76                                      
77    setting_reg #(`FR_RADAR_TIDLE)  sr_tidle(.clock(clk_i),.reset(1'b0),.strobe(s_strobe_i),.addr(saddr_i),.in(sdata_i),
78                                             .out(t_idle));
79                                      
80    setting_reg #(`FR_RADAR_AMPL)   sr_ampl(.clock(clk_i),.reset(1'b0),.strobe(s_strobe_i),.addr(saddr_i),.in(sdata_i),
81                                            .out(ampl_o));
82
83    setting_reg #(`FR_RADAR_FSTART) sr_fstart(.clock(clk_i),.reset(1'b0),.strobe(s_strobe_i),.addr(saddr_i),.in(sdata_i),
84                                              .out(fstart_o));
85
86    setting_reg #(`FR_RADAR_FINCR)  sr_fincr(.clock(clk_i),.reset(1'b0),.strobe(s_strobe_i),.addr(saddr_i),.in(sdata_i),
87                                             .out(fincr_o));
88
89    // Pulse state machine
90    `define ST_ON   4'b0001
91    `define ST_SW   4'b0010
92    `define ST_LOOK 4'b0100
93    `define ST_IDLE 4'b1000
94
95    reg [3:0]  state;
96    reg [31:0] count;
97
98    always @(posedge clk_i)
99      if (reset_o)
100        begin
101           state <= `ST_ON;
102           count <= 32'b0;
103        end
104      else
105        case (state)
106          `ST_ON:
107            if (count == {16'b0,t_on})
108              begin
109                 state <= `ST_SW;
110                 count <= 32'b0;
111              end
112            else
113              count <= count + 32'b1;
114          
115          `ST_SW:
116            if (count == {16'b0,t_sw})
117              begin
118                 state <= `ST_LOOK;
119                 count <= 32'b0;
120              end
121            else
122              count <= count + 32'b1;
123
124          `ST_LOOK:
125            if (count == {16'b0,t_look})
126              begin
127                 state <= `ST_IDLE;
128                 count <= 32'b0;
129              end
130            else
131              count <= count + 32'b1;
132
133          `ST_IDLE:
134            if (count == t_idle)
135              begin
136                 state <= `ST_ON;
137                 count <= 32'b0;
138              end
139            else
140              count <= count + 32'b1;
141
142          default:                 // Invalid state, reset state machine
143            begin
144               state <= `ST_ON;
145               count <= 32'b0;
146            end
147        endcase
148    
149    assign tx_strobe_o = count[0]; // Drive DAC inputs at 32 MHz
150    assign tx_ctrl_o = (state == `ST_ON);
151    assign rx_ctrl_o = (state == `ST_LOOK);
152    
153 endmodule // radar_control