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

Subversion Repositories wbfmtx

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

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 dgisselq
///////////////////////////////////////////////////////////////////////////
2
//
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
// Creator:     Dan Gisselquist, Ph.D.
66
//              Gisselquist Technology, LLC
67
//
68
///////////////////////////////////////////////////////////////////////////
69
//
70
// Copyright (C) 2015, Gisselquist Technology, LLC
71
//
72
// This program is free software (firmware): you can redistribute it and/or
73
// modify it under the terms of  the GNU General Public License as published
74
// by the Free Software Foundation, either version 3 of the License, or (at
75
// your option) any later version.
76
//
77
// This program is distributed in the hope that it will be useful, but WITHOUT
78
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
79
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
80
// for more details.
81
//
82
// You should have received a copy of the GNU General Public License along
83
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
84
// target there if the PDF file isn't present.)  If not, see
85
// <http://www.gnu.org/licenses/> for a copy.
86
//
87
// License:     GPL, v3, as defined and found on www.gnu.org,
88
//              http://www.gnu.org/licenses/gpl.html
89
//
90
//
91
///////////////////////////////////////////////////////////////////////////
92
module  wbfmtxhack(i_clk,
93
                // Wishbone interface
94
                i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data,
95
                        o_wb_ack, o_wb_stall, o_wb_data,
96
                o_tx, o_int);
97
        parameter       DEFAULT_RELOAD = 16'd1814; // 44.1kHz at a 80MHz clock
98
        input   i_clk;
99
        input   i_wb_cyc, i_wb_stb, i_wb_we;
100
        input           i_wb_addr;
101
        input   [31:0]   i_wb_data;
102
        output  reg             o_wb_ack;
103
        output  wire            o_wb_stall;
104
        output  reg     [31:0]   o_wb_data;
105
        output  wire            o_tx;
106
        output  reg             o_int;
107
 
108
        reg     [31:0]   nco_step, nco_phase;
109
 
110
        // How often shall we create an interrupt?  Every reload_value clocks!
111
        // If VARIABLE_RATE==0, this value will never change and will be kept
112
        // at the default reload rate (44.1 kHz, for a 100 MHz clock)
113
        reg     [15:0]   reload_value;
114
        initial reload_value = DEFAULT_RELOAD;
115
        always @(posedge i_clk) // Data write
116
                if ((i_wb_cyc)&&(i_wb_stb)&&(~i_wb_addr)&&(i_wb_we)
117
                                &&(|i_wb_data[31:16]))
118
                        reload_value <= i_wb_data[31:16];
119
        always @(posedge i_clk) // Data write
120
                if ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_addr)&&(i_wb_we))
121
                        nco_step <= i_wb_data[31:0];
122
 
123
        reg     [15:0]   timer;
124
        always @(posedge i_clk)
125
                if (timer == 0)
126
                        timer <= reload_value;
127
                else
128
                        timer <= timer - 16'h1;
129
 
130
        reg     [15:0]   next_sample, sample_out;
131
        always @(posedge i_clk)
132
                if (timer == 0)
133
                        sample_out <= next_sample;
134
 
135
        reg             next_valid;
136
        initial next_valid = 1'b1;
137
        initial next_sample = 16'h8000;
138
        always @(posedge i_clk) // Data write
139
                if ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_we)&&(~i_wb_addr))
140
                begin
141
                        // Write with two's complement data, convert it
142
                        // internally to binary offset
143
                        next_sample <= i_wb_data[15:0];
144
                        next_valid <= 1'b1;
145
                end else if (timer == 0)
146
                        next_valid <= 1'b0;
147
 
148
        initial o_int = 1'b0;
149
        always @(posedge i_clk)
150
                o_int <= (~next_valid);
151
 
152
        initial nco_phase = 32'h00;
153
        always @(posedge i_clk)
154
                nco_phase <= nco_phase + nco_step
155
                        + { {(32-16-7){sample_out[15]}}, sample_out, 7'h00 };
156
        assign  o_tx = nco_phase[31];
157
 
158
        always @(posedge i_clk)
159
                if (i_wb_addr)
160
                        o_wb_data <= nco_step;
161
                else
162
                        o_wb_data <= { reload_value, sample_out[15:1], o_int };
163
 
164
        initial o_wb_ack = 1'b0;
165
        always @(posedge i_clk)
166
                o_wb_ack <= (i_wb_cyc)&&(i_wb_stb);
167
        assign  o_wb_stall = 1'b0;
168
 
169
endmodule

powered by: WebSVN 2.1.0

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