Fix (actually a bit of a kludge) for the frequency setting while
[debian/gnuradio] / usrp2 / firmware / apps / factory_test.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 "memcpy_wa.h"
35 #include <stddef.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <i2c.h>
39 #include <usrp2_i2c_addr.h>
40
41 #define HW_REV_MAJOR 3
42 #define HW_REV_MINOR 0
43
44 #define FW_SETS_SEQNO   1       // define to 0 or 1 (FIXME must be 1 for now)
45
46 #if (FW_SETS_SEQNO)
47 static int fw_seqno;    // used when f/w is filling in sequence numbers
48 #endif
49
50
51 /*
52  * Full duplex Tx and Rx between ethernet and DSP pipelines
53  *
54  * Buffer 1 is used by the cpu to send frames to the host.
55  * Buffers 2 and 3 are used to double-buffer the DSP Rx to eth flow
56  * Buffers 4 and 5 are used to double-buffer the eth to DSP Tx  eth flow
57  */
58 //#define CPU_RX_BUF    0       // eth -> cpu
59
60 #define DSP_RX_BUF_0    2       // dsp rx -> eth (double buffer)
61 #define DSP_RX_BUF_1    3       // dsp rx -> eth
62 #define DSP_TX_BUF_0    4       // eth -> dsp tx (double buffer)
63 #define DSP_TX_BUF_1    5       // eth -> dsp tx
64
65 /*
66  * ================================================================
67  *   configure DSP TX double buffering state machine (eth -> dsp)
68  * ================================================================
69  */
70
71 // 4 lines of ethernet hdr + 1 line transport hdr + 2 lines (word0 + timestamp)
72 // DSP Tx reads word0 (flags) + timestamp followed by samples
73
74 #define DSP_TX_FIRST_LINE ((sizeof(u2_eth_hdr_t) + sizeof(u2_transport_hdr_t))/4)
75
76 // Receive from ethernet
77 buf_cmd_args_t dsp_tx_recv_args = {
78   PORT_ETH,
79   0,
80   BP_LAST_LINE
81 };
82
83 // send to DSP Tx
84 buf_cmd_args_t dsp_tx_send_args = {
85   PORT_DSP,
86   DSP_TX_FIRST_LINE,    // starts just past transport header
87   0                     // filled in from last_line register
88 };
89
90 dbsm_t dsp_tx_sm;       // the state machine
91
92 /*
93  * ================================================================
94  *   configure DSP RX double buffering state machine (dsp -> eth)
95  * ================================================================
96  */
97
98 // 4 lines of ethernet hdr + 1 line transport hdr + 1 line (word0)
99 // DSP Rx writes timestamp followed by nlines_per_frame of samples
100 #define DSP_RX_FIRST_LINE ((sizeof(u2_eth_hdr_t) + sizeof(u2_transport_hdr_t))/4 + 1)
101
102 // receive from DSP
103 buf_cmd_args_t dsp_rx_recv_args = {
104   PORT_DSP,
105   DSP_RX_FIRST_LINE,
106   BP_LAST_LINE
107 };
108
109 // send to ETH
110 buf_cmd_args_t dsp_rx_send_args = {
111   PORT_ETH,
112   0,            // starts with ethernet header in line 0
113   0,            // filled in from list_line register
114 };
115
116 dbsm_t dsp_rx_sm;       // the state machine
117
118
119 // The mac address of the host we're sending to.
120 u2_mac_addr_t host_mac_addr;
121
122
123 // variables for streaming mode
124
125 static bool         streaming_p = false;
126 static unsigned int streaming_items_per_frame = 0;
127 static int          streaming_frame_count = 0;
128 #define FRAMES_PER_CMD  1000
129
130 bool is_streaming(void){ return streaming_p; }
131
132 // ----------------------------------------------------------------
133
134
135 void
136 restart_streaming(void)
137 {
138   // setup RX DSP regs
139   dsp_rx_regs->clear_state = 1;                 // reset
140
141   streaming_p = true;
142   streaming_frame_count = FRAMES_PER_CMD;
143
144   dsp_rx_regs->rx_command =
145     MK_RX_CMD(FRAMES_PER_CMD * streaming_items_per_frame,
146               streaming_items_per_frame,
147               1, 1);                    // set "chain" bit
148
149   // kick off the state machine
150   dbsm_start(&dsp_rx_sm);
151
152   dsp_rx_regs->rx_time = 0;             // enqueue first of two commands
153
154   // make sure this one and the rest have the "now" and "chain" bits set.
155   dsp_rx_regs->rx_command =
156     MK_RX_CMD(FRAMES_PER_CMD * streaming_items_per_frame,
157               streaming_items_per_frame,
158               1, 1);                            
159
160   dsp_rx_regs->rx_time = 0;             // enqueue second command
161 }
162
163 void
164 start_rx_streaming_cmd(const u2_mac_addr_t *host, op_start_rx_streaming_t *p)
165 {
166   host_mac_addr = *host;        // remember who we're sending to
167
168   /*
169    * Construct  ethernet header and word0 and preload into two buffers
170    */
171   u2_eth_packet_t       pkt;
172   memset(&pkt, 0, sizeof(pkt));
173   pkt.ehdr.dst = *host;
174   pkt.ehdr.ethertype = U2_ETHERTYPE;
175   u2p_set_word0(&pkt.fixed, 0, 0);
176   // DSP RX will fill in timestamp
177
178   memcpy_wa(buffer_ram(DSP_RX_BUF_0), &pkt, sizeof(pkt));
179   memcpy_wa(buffer_ram(DSP_RX_BUF_1), &pkt, sizeof(pkt));
180
181
182   if (FW_SETS_SEQNO)
183     fw_seqno = 0;
184
185   streaming_items_per_frame = p->items_per_frame;
186   restart_streaming();
187 }
188
189
190 void
191 stop_rx_cmd(void)
192 {
193   streaming_p = false;
194   dsp_rx_regs->clear_state = 1; // flush cmd queue
195   bp_clear_buf(DSP_RX_BUF_0);
196   bp_clear_buf(DSP_RX_BUF_1);
197 }
198
199
200 static void
201 setup_tx()
202 {
203   dsp_tx_regs->clear_state = 1;
204   bp_clear_buf(DSP_TX_BUF_0);
205   bp_clear_buf(DSP_TX_BUF_1);
206
207   int tx_scale = 256;
208   int interp = 32;
209
210   // setup some defaults
211
212   dsp_tx_regs->freq = 0;
213   dsp_tx_regs->scale_iq = (tx_scale << 16) | tx_scale;
214   dsp_tx_regs->interp_rate = interp;
215 }
216
217
218 #if (FW_SETS_SEQNO)
219 /*
220  * Debugging ONLY.  This will be handled by the tx_protocol_engine.
221  *
222  * This is called when the DSP Rx chain has filled in a packet.
223  * We set and increment the seqno, then return false, indicating
224  * that we didn't handle the packet.  A bit of a kludge
225  * but it should work.
226  */
227 bool 
228 fw_sets_seqno_inspector(dbsm_t *sm, int buf_this)       // returns false
229 {
230   uint32_t *p = buffer_ram(buf_this);
231   uint32_t seqno = fw_seqno++;
232
233   // KLUDGE all kinds of nasty magic numbers and embedded knowledge
234   uint32_t t = p[4];
235   t = (t & 0xffff00ff) | ((seqno & 0xff) << 8);
236   p[4] = t;
237
238   // queue up another rx command when required
239   if (streaming_p && --streaming_frame_count == 0){
240     streaming_frame_count = FRAMES_PER_CMD;
241     dsp_rx_regs->rx_time = 0;
242   }
243
244   return false;         // we didn't handle the packet
245 }
246 #endif
247
248
249 inline static void
250 buffer_irq_handler(unsigned irq)
251 {
252   uint32_t  status = buffer_pool_status->status;
253
254   dbsm_process_status(&dsp_tx_sm, status);
255   dbsm_process_status(&dsp_rx_sm, status);
256 }
257
258 int
259 main(void)
260 {
261   u2_init();
262
263   putstr("\nset_hw_rev\n");
264
265   bool ok = true;
266   unsigned char maj = HW_REV_MAJOR;
267   unsigned char min = HW_REV_MINOR;
268   ok = eeprom_write(I2C_ADDR_MBOARD, MBOARD_REV_MSB, &maj, 1);
269   ok &= eeprom_write(I2C_ADDR_MBOARD, MBOARD_REV_LSB, &min, 1);
270
271   if (ok)
272     printf("OK: set h/w rev to %d.%d\n", HW_REV_MAJOR, HW_REV_MINOR);
273   else
274     printf("FAILED to set h/w rev to %d.%d\n", HW_REV_MAJOR, HW_REV_MINOR);
275
276   putstr("\nFactory Test TXRX\n");
277   print_mac_addr(ethernet_mac_addr()->addr);
278   newline();
279
280   ethernet_register_link_changed_callback(link_changed_callback);
281   ethernet_init();
282
283
284 #if 0
285   // make bit 15 of Tx gpio's be a s/w output
286   hal_gpio_set_sel(GPIO_TX_BANK, 15, 's');
287   hal_gpio_set_ddr(GPIO_TX_BANK, 0x8000, 0x8000);
288 #endif
289
290   output_regs->debug_mux_ctrl = 1;
291 #if 0
292   hal_gpio_set_sels(GPIO_TX_BANK, "1111111111111111");
293   hal_gpio_set_sels(GPIO_RX_BANK, "1111111111111111");
294   hal_gpio_set_ddr(GPIO_TX_BANK, 0xffff, 0xffff);
295   hal_gpio_set_ddr(GPIO_RX_BANK, 0xffff, 0xffff);
296 #endif
297
298
299   // initialize double buffering state machine for ethernet -> DSP Tx
300
301   dbsm_init(&dsp_tx_sm, DSP_TX_BUF_0,
302             &dsp_tx_recv_args, &dsp_tx_send_args,
303             eth_pkt_inspector);
304
305
306   // initialize double buffering state machine for DSP RX -> Ethernet
307
308   if (FW_SETS_SEQNO){
309     dbsm_init(&dsp_rx_sm, DSP_RX_BUF_0,
310               &dsp_rx_recv_args, &dsp_rx_send_args,
311               fw_sets_seqno_inspector);
312   }
313   else {
314     dbsm_init(&dsp_rx_sm, DSP_RX_BUF_0,
315               &dsp_rx_recv_args, &dsp_rx_send_args,
316               dbsm_nop_inspector);
317   }
318
319   // tell app_common that this dbsm could be sending to the ethernet
320   ac_could_be_sending_to_eth = &dsp_rx_sm;
321
322
323   // program tx registers
324   setup_tx();
325
326   // kick off the state machine
327   dbsm_start(&dsp_tx_sm);
328
329   //int which = 0;
330
331   while(1){
332     // hal_gpio_write(GPIO_TX_BANK, which, 0x8000);
333     // which ^= 0x8000;
334
335     buffer_irq_handler(0);
336
337     int pending = pic_regs->pending;            // poll for under or overrun
338
339     if (pending & PIC_UNDERRUN_INT){
340       dbsm_handle_tx_underrun(&dsp_tx_sm);
341       pic_regs->pending = PIC_UNDERRUN_INT;     // clear interrupt
342       putchar('U');
343     }
344
345     if (pending & PIC_OVERRUN_INT){
346       dbsm_handle_rx_overrun(&dsp_rx_sm);
347       pic_regs->pending = PIC_OVERRUN_INT;      // clear pending interrupt
348
349       // FIXME Figure out how to handle this robustly.
350       // Any buffers that are emptying should be allowed to drain...
351
352       if (streaming_p){
353         // restart_streaming();
354         // FIXME report error
355       }
356       else {
357         // FIXME report error
358       }
359       putchar('O');
360     }
361   }
362 }