backed out 9874
[debian/gnuradio] / usrp2 / firmware / apps / tx_only_v2.c
1 /*
2  * Copyright 2007,2008 Free Software Foundation, Inc.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include "u2_init.h"
23 #include "memory_map.h"
24 #include "spi.h"
25 #include "hal_io.h"
26 #include "buffer_pool.h"
27 #include "pic.h"
28 #include "bool.h"
29 #include "ethernet.h"
30 #include "nonstdio.h"
31 #include "usrp2_eth_packet.h"
32 #include "dbsm.h"
33 #include "app_common_v2.h"
34 #include <stddef.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <db.h>
38 #include <db_base.h>
39
40
41 /*
42  * This program can respond to queries from the host
43  * and stream rx samples.
44  *
45  * Buffer 1 is used by the cpu to send frames to the host.
46  * Buffers 2 and 3 are used to double-buffer the DSP Rx to eth flow
47  * Buffers 4 and 5 are used to double-buffer the eth to DSP Tx  eth flow
48  */
49 //#define CPU_RX_BUF    0       // eth -> cpu
50 //#define CPU_TX_BUF    1       // cpu -> eth
51
52 #define DSP_RX_BUF_0    2       // dsp rx -> eth (double buffer)
53 #define DSP_RX_BUF_1    3       // dsp rx -> eth
54 #define DSP_TX_BUF_0    4       // eth -> dsp tx (double buffer)
55 #define DSP_TX_BUF_1    5       // eth -> dsp tx
56
57 /*
58  * ================================================================
59  *      configure DSP TX double buffering state machine
60  * ================================================================
61  */
62
63 // 4 lines of ethernet hdr + 1 line transport hdr + 2 lines (word0 + timestamp)
64 // DSP Tx reads word0 (flags) + timestamp followed by samples
65
66 #define DSP_TX_FIRST_LINE ((sizeof(u2_eth_hdr_t) + sizeof(u2_transport_hdr_t))/4)
67
68 // Receive from ethernet
69 buf_cmd_args_t dsp_tx_recv_args = {
70   PORT_ETH,
71   0,
72   BP_LAST_LINE
73 };
74
75 // send to DSP Tx
76 buf_cmd_args_t dsp_tx_send_args = {
77   PORT_DSP,
78   DSP_TX_FIRST_LINE,    // starts just past transport header
79   0                     // filled in from last_line register
80 };
81
82 dbsm_t dsp_tx_sm;       // the state machine
83
84
85 // ----------------------------------------------------------------
86
87
88 // The mac address of the host we're sending to.
89 u2_mac_addr_t host_mac_addr;
90
91
92 void
93 start_rx_streaming_cmd(const u2_mac_addr_t *host, op_start_rx_streaming_t *p)
94 {
95   // FIXME nop
96 }
97
98
99 void
100 stop_rx_cmd(void)
101 {
102   // FIXME nop
103 }
104
105
106 static void
107 setup_tx()
108 {
109   dsp_tx_regs->clear_state = 1;
110   bp_clear_buf(DSP_TX_BUF_0);
111   bp_clear_buf(DSP_TX_BUF_1);
112
113   int tx_scale = 256;
114   int interp = 32;
115
116   // setup some defaults
117
118   dsp_tx_regs->freq = 0;
119   dsp_tx_regs->scale_iq = (tx_scale << 16) | tx_scale;
120   dsp_tx_regs->interp_rate = interp;
121 }
122
123
124 inline static void
125 buffer_irq_handler(unsigned irq)
126 {
127   //hal_toggle_leds(0x2);
128
129   uint32_t  status = buffer_pool_status->status;
130
131   dbsm_process_status(&dsp_tx_sm, status);
132
133   if (status & BPS_DONE(CPU_TX_BUF)){
134     bp_clear_buf(CPU_TX_BUF);
135   }
136 }
137
138 int
139 main(void)
140 {
141   u2_init();
142
143   putstr("\ntx_only_v2\n");
144
145   ethernet_register_link_changed_callback(link_changed_callback);
146   ethernet_init();
147
148   // initialize double buffering state machine for ethernet -> DSP Tx
149
150   dbsm_init(&dsp_tx_sm, DSP_TX_BUF_0,
151             &dsp_tx_recv_args, &dsp_tx_send_args,
152             eth_pkt_inspector);
153
154   // program tx registers
155   setup_tx();
156
157   // kick off the state machine
158   dbsm_start(&dsp_tx_sm);
159
160   while(1){
161     buffer_irq_handler(0);
162
163     int pending = pic_regs->pending;            // poll for under or overrun
164
165     if (pending & PIC_UNDERRUN_INT){
166       dbsm_handle_tx_underrun(&dsp_tx_sm);
167       pic_regs->pending = PIC_UNDERRUN_INT;     // clear interrupt
168       putchar('U');
169     }
170   }
171 }