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

Subversion Repositories wbfmtx

[/] [wbfmtx/] [trunk/] [rtl/] [wbfmtxhack.v] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 dgisselq
////////////////////////////////////////////////////////////////////////////////
2 2 dgisselq
//
3
// Filename:    wbfmtxhack.v
4
//              
5
// Project:     A Wishbone Controlled FM Transmitter Hack
6
//
7
// Purpose:     This Hack is based off of two things: 1) the interface spec
8
//              of the WB controlled PWM audio device, and 2) a Raspberry Pi
9
//      Hack I was shown that converted the RPi PWM device into an FM
10
//      transmitter.  So, the question is, can a GPIO pin be turned into an
11
//      FM transmitter that can be heard throughout the house?
12
//
13
//      We'll try and do this properly: We'll use a Numerically Controlled
14
//      Oscillator to generate our signal, but only grab the top bit out of
15
//      that oscillator.  We'll then send this bit to the GPIO pin (a.k.a.
16
//      antenna) to see if it can accomplish our goals.
17
//
18
//      WB Control/Registers:
19
//      1'b0:   Next Sample
20
//
21
//              The top bits of this 'next sample' will indicate the number
22
//              of clock ticks before we generate a need next sample interrupt.
23
//              If these top bits are zero, the sample rate will not be
24
//              adjusted.  The value to set here is the value of the clock
25
//              rate divided by the desired sample rate.  Hence, if the clock
26
//              rate is 80MHz, setting this to 10e3 (unsigned) would set us up
27
//              for an 8kHz sample rate, whereas setting these upper 16 bits to
28
//              1814 would specify a sample rate closer to 44.1kHz.
29
//
30
//              The lower 16 bits specify the value of the next sample.
31
//
32
//              Since we'll be dealing with FM modulation, we'll try to arrange
33
//              that this sixteen bit sample will correspond to a maximum
34
//              FM deviation of about 75 kHz.
35
//
36
//
37
//      1'b1:   The Oscillator "Frequency" (really stepsize).  This should be
38
//              used to control/determine the "RF frequency" this device can
39
//              transmit on.  
40
//
41
//              To transmit at 0Hz, set this to zero.  To transmit at
42
//              CLKSPEED/2 Hz, set this to 32'h8000_0000.  Hence for a 
43
//              transmit frequency of X, set this value to 
44
//
45
//              OSXFREQ = 2^32 * X / CLKSPEED
46
//
47
//              Where X and CLKSPEED share the same units.  But how shall we
48
//              transmit at speeds of anything higher than CLKSPEED/2?  By
49
//              aliasing up.  Hence, set X to your actual frequency value,
50
//              divide by the clockspeed and multiply by 2^32.  Remove any
51
//              bits that don't fit in the top 32 and you are there.
52
//
53
//              This also gives us about 20 mHz resolution for our Carrier
54
//              frequency--overkill perhaps, but it should work.
55
//
56
//      So ... how do we create our 75 kHz deviation?  We want:
57
//
58
//      MAX_STEPSIZE = 2^32 * (X + 75kHz * sample / 2^15) / CLKSPEED
59
//      = OSXFREQ = (2^32 * sample / 2^15 / CLKSPEED * 75 kHz)
60
//      = 123 * sample ~= 128 * sample = sample << 7.
61
//
62
//      Thus, by shifting our input sample value a touch, we can multiply by
63
//      nearly the exact constant we want.
64
//
65 5 dgisselq
// OSERDES:
66
//      Okay, the first version was fun and worked ... okay, but ... can we do
67
//      better?  I mean, we lost over 10dB by undersampling, and most(many?)
68
//      FPGA's have OSERDES components that will allow bits to toggle faster
69
//      than the FPGA clock rate.  In other words, if we have an 80MHz clock,
70
//      we should be able to output samples at 320MHz, no?
71
//
72
//      Let's do even one better than that: suppose we create outputs for a 
73
//      2-bit DAC.  Of course, our chip doesn't have a 2-bit DAC, much less a
74
//      DAC at all, but could we create one from our I/O pins?  For example,
75
//      if two I/O pins both produced a 1, the resulting field would be
76
//      stronger, right?  What if they both produced a zero, same thing, right?
77
//      Now, what if one produced a 1 and one produced a 0?  Would the fields
78
//      interfere with each other?  You know, would they produce a sum field
79
//      that was better than just the one-bit produced field?  Perhaps, with
80
//      only a 1-bit output, we get +/- 3.  With a two bit output, we should
81
//      be able to get { -3, 0, 3 }, right?  (Ignore scaling ...)
82
//
83
//      A better two-bit output would probably be something like
84
//      { -3, -1, 1, 3 }.  How can we produce something like that?  Without a 
85
//      proper ADC?  Can we connect a majority of the pins to the high order
86
//      bit output, and some fewer number to the low order bit output?
87
//      Would this create a better field?
88
//
89
//      The component created with `define OSERDES is designed to allow such
90
//      hypotheses to be tested.
91
//
92 2 dgisselq
// Creator:     Dan Gisselquist, Ph.D.
93
//              Gisselquist Technology, LLC
94
//
95 4 dgisselq
////////////////////////////////////////////////////////////////////////////////
96 2 dgisselq
//
97 4 dgisselq
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
98 2 dgisselq
//
99
// This program is free software (firmware): you can redistribute it and/or
100
// modify it under the terms of  the GNU General Public License as published
101
// by the Free Software Foundation, either version 3 of the License, or (at
102
// your option) any later version.
103
//
104
// This program is distributed in the hope that it will be useful, but WITHOUT
105
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
106
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
107
// for more details.
108
//
109
// You should have received a copy of the GNU General Public License along
110
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
111
// target there if the PDF file isn't present.)  If not, see
112
// <http://www.gnu.org/licenses/> for a copy.
113
//
114
// License:     GPL, v3, as defined and found on www.gnu.org,
115
//              http://www.gnu.org/licenses/gpl.html
116
//
117
//
118 4 dgisselq
////////////////////////////////////////////////////////////////////////////////
119
//
120
//
121 2 dgisselq
module  wbfmtxhack(i_clk,
122
                // Wishbone interface
123
                i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data,
124
                        o_wb_ack, o_wb_stall, o_wb_data,
125
                o_tx, o_int);
126
        parameter       DEFAULT_RELOAD = 16'd1814; // 44.1kHz at a 80MHz clock
127
        input   i_clk;
128
        input   i_wb_cyc, i_wb_stb, i_wb_we;
129
        input           i_wb_addr;
130
        input   [31:0]   i_wb_data;
131
        output  reg             o_wb_ack;
132
        output  wire            o_wb_stall;
133
        output  reg     [31:0]   o_wb_data;
134 5 dgisselq
`ifdef  USE_OSERDES
135
        output  wire    [7:0]    o_tx;
136
`else
137 2 dgisselq
        output  wire            o_tx;
138 5 dgisselq
`endif
139 2 dgisselq
        output  reg             o_int;
140
 
141 5 dgisselq
        reg     [31:0]   nco_step;
142 2 dgisselq
 
143
        // How often shall we create an interrupt?  Every reload_value clocks!
144
        // If VARIABLE_RATE==0, this value will never change and will be kept
145
        // at the default reload rate (44.1 kHz, for a 100 MHz clock)
146
        reg     [15:0]   reload_value;
147
        initial reload_value = DEFAULT_RELOAD;
148 3 dgisselq
 
149
        // Data write, but we use the upper 16 bits to set our sample rate.
150
        // If these bits are zero, we ignore the write--allowing users to
151
        // write samples without adjusting the sample rate.
152
        always @(posedge i_clk) // Set sample rate
153 2 dgisselq
                if ((i_wb_cyc)&&(i_wb_stb)&&(~i_wb_addr)&&(i_wb_we)
154
                                &&(|i_wb_data[31:16]))
155
                        reload_value <= i_wb_data[31:16];
156 3 dgisselq
 
157
        // Set the NCO transmit frequency
158
        initial nco_step = 32'h00;
159
        always @(posedge i_clk)
160 2 dgisselq
                if ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_addr)&&(i_wb_we))
161
                        nco_step <= i_wb_data[31:0];
162
 
163 3 dgisselq
        reg             ztimer;
164 2 dgisselq
        reg     [15:0]   timer;
165 3 dgisselq
        initial ztimer = 1'b0;
166
        always @(posedge i_clk) // Be true when the timer is zero
167
                ztimer <= (timer[15:0] == 16'h1);
168
        initial timer = reload_value;
169 2 dgisselq
        always @(posedge i_clk)
170 3 dgisselq
                if (ztimer)
171 2 dgisselq
                        timer <= reload_value;
172
                else
173
                        timer <= timer - 16'h1;
174
 
175
        reg     [15:0]   next_sample, sample_out;
176 3 dgisselq
        initial sample_out  = 16'h00;
177
        initial next_sample = 16'h00;
178 2 dgisselq
        always @(posedge i_clk)
179 3 dgisselq
                if (ztimer)
180 2 dgisselq
                        sample_out <= next_sample;
181
 
182
        reg             next_valid;
183
        initial next_valid = 1'b1;
184
        initial next_sample = 16'h8000;
185
        always @(posedge i_clk) // Data write
186
                if ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_we)&&(~i_wb_addr))
187
                begin
188 3 dgisselq
                        // Write with two's complement data
189 2 dgisselq
                        next_sample <= i_wb_data[15:0];
190
                        next_valid <= 1'b1;
191 3 dgisselq
                end else if (ztimer)
192 2 dgisselq
                        next_valid <= 1'b0;
193
 
194 3 dgisselq
        // The interrupt line will remain high until writing a new data value
195
        // clears it.  This design does not permit turning off this interrupt.
196
        // If the interrupt needs to be turned off, then ignore it in the 
197
        // interrupt controller.
198 2 dgisselq
        initial o_int = 1'b0;
199
        always @(posedge i_clk)
200
                o_int <= (~next_valid);
201
 
202 5 dgisselq
`ifdef  USE_OSERDES
203
        // If we use an OSERDES on our final output, we should be able to 
204
        // oversample by a factor of 4x (or perhaps more, but this works the
205
        // 4x number).  Here is an example of figuring out what both of those
206
        // 4x oversamples are--first the primary, calculated as before, but then
207
        // also the alternate.
208
        reg     [31:0]   nco_phase, nco_phase_a, nco_phase_b, nco_phase_c,
209
                        sample_step, tripl_step, tripl_nco_step;
210
        initial nco_base   = 32'h00;
211
 
212
        always @(posedge i_clk)
213
                if (ztimer)
214
                        sample_step <= nco_step
215
                        + { {(32-16-5){next_sample[15]}}, next_sample, 5'h00 };
216
 
217
        // Multiply by three ... never that easy
218
        always @(posedge i_clk)
219
                tripl_nco_step <= nco_step + { nco_step[30:0], 1'b0 };
220
        always @(posedge i_clk)
221
                if (ztimer)
222
                        tripl_step <= tripl_nco_step
223
                        + { {(32-16-5){next_sample[15]}}, next_sample, 5'h00 };
224
                        + { {(32-16-6){next_sample[15]}}, next_sample, 6'h00 };
225
 
226
        wire    [31:0]   base_step;
227
        assign  nco_base_step = sample_step;
228
        always @(posedge i_clk)
229
                nco_phase_a <= nco_phase + sample_step;
230
        always @(posedge i_clk)
231
                nco_phase_b <= nco_phase + { sample_step[30:0], 1'b0 };
232
        always @(posedge i_clk)
233
                nco_phase_c <= nco_phase + tripl_step;
234
        always @(posedge i_clk)
235
                nco_phase <= nco_phase + { sample_step[29:0], 2'b00 };
236
 
237
        // Output a two-bit waveform.  Send each bit to GPIO port(s), with
238
        // roughly the same number of ports per bit.
239
        assign  o_tx = { nco_phase_a[31:30], nco_base_b[31:30],
240
                                nco_phase_c[31:30], nco_phase[31:30] };
241
`else
242 3 dgisselq
        // Adjust the gain for a maximum frequency offset just greater than
243
        // 75 kHz.  (We would've done 75kHz exactly, but it required a multiply
244
        // and this doesn't.)
245 5 dgisselq
        reg     [31:0]   nco_phase;
246 2 dgisselq
        initial nco_phase = 32'h00;
247
        always @(posedge i_clk)
248
                nco_phase <= nco_phase + nco_step
249
                        + { {(32-16-7){sample_out[15]}}, sample_out, 7'h00 };
250
        assign  o_tx = nco_phase[31];
251 5 dgisselq
`endif
252 2 dgisselq
 
253
        always @(posedge i_clk)
254
                if (i_wb_addr)
255
                        o_wb_data <= nco_step;
256
                else
257
                        o_wb_data <= { reload_value, sample_out[15:1], o_int };
258
 
259
        initial o_wb_ack = 1'b0;
260
        always @(posedge i_clk)
261
                o_wb_ack <= (i_wb_cyc)&&(i_wb_stb);
262
        assign  o_wb_stall = 1'b0;
263
 
264
endmodule

powered by: WebSVN 2.1.0

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