Applied 'start streaming at' patch from Douglas Geiger
[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 #include <clocks.h>
41 #include "sd.h"
42
43 #define FW_SETS_SEQNO   1       // define to 0 or 1 (FIXME must be 1 for now)
44
45 #if (FW_SETS_SEQNO)
46 static int fw_seqno;    // used when f/w is filling in sequence numbers
47 #endif
48
49
50 /*
51  * Full duplex Tx and Rx between ethernet and DSP pipelines
52  *
53  * Buffer 1 is used by the cpu to send frames to the host.
54  * Buffers 2 and 3 are used to double-buffer the DSP Rx to eth flow
55  * Buffers 4 and 5 are used to double-buffer the eth to DSP Tx  eth flow
56  */
57 //#define CPU_RX_BUF    0       // eth -> cpu
58
59 #define DSP_RX_BUF_0    2       // dsp rx -> eth (double buffer)
60 #define DSP_RX_BUF_1    3       // dsp rx -> eth
61 #define DSP_TX_BUF_0    4       // eth -> dsp tx (double buffer)
62 #define DSP_TX_BUF_1    5       // eth -> dsp tx
63
64 /*
65  * ================================================================
66  *   configure DSP TX double buffering state machine (eth -> dsp)
67  * ================================================================
68  */
69
70 // 4 lines of ethernet hdr + 1 line transport hdr + 2 lines (word0 + timestamp)
71 // DSP Tx reads word0 (flags) + timestamp followed by samples
72
73 #define DSP_TX_FIRST_LINE ((sizeof(u2_eth_hdr_t) + sizeof(u2_transport_hdr_t))/4)
74
75 // Receive from ethernet
76 buf_cmd_args_t dsp_tx_recv_args = {
77   PORT_ETH,
78   0,
79   BP_LAST_LINE
80 };
81
82 // send to DSP Tx
83 buf_cmd_args_t dsp_tx_send_args = {
84   PORT_DSP,
85   DSP_TX_FIRST_LINE,    // starts just past transport header
86   0                     // filled in from last_line register
87 };
88
89 dbsm_t dsp_tx_sm;       // the state machine
90
91 /*
92  * ================================================================
93  *   configure DSP RX double buffering state machine (dsp -> eth)
94  * ================================================================
95  */
96
97 // 4 lines of ethernet hdr + 1 line transport hdr + 1 line (word0)
98 // DSP Rx writes timestamp followed by nlines_per_frame of samples
99 #define DSP_RX_FIRST_LINE ((sizeof(u2_eth_hdr_t) + sizeof(u2_transport_hdr_t))/4 + 1)
100
101 // receive from DSP
102 buf_cmd_args_t dsp_rx_recv_args = {
103   PORT_DSP,
104   DSP_RX_FIRST_LINE,
105   BP_LAST_LINE
106 };
107
108 // send to ETH
109 buf_cmd_args_t dsp_rx_send_args = {
110   PORT_ETH,
111   0,            // starts with ethernet header in line 0
112   0,            // filled in from list_line register
113 };
114
115 dbsm_t dsp_rx_sm;       // the state machine
116
117
118 // The mac address of the host we're sending to.
119 u2_mac_addr_t host_mac_addr;
120
121
122 // variables for streaming mode
123
124 static bool         streaming_p = false;
125 static unsigned int streaming_items_per_frame = 0;
126 static int          streaming_frame_count = 0;
127 #define FRAMES_PER_CMD  1000
128
129 bool is_streaming(void){ return streaming_p; }
130
131 // ----------------------------------------------------------------
132
133
134 void
135 restart_streaming(void)
136 {
137   // setup RX DSP regs
138   dsp_rx_regs->clear_state = 1;                 // reset
139
140   streaming_p = true;
141   streaming_frame_count = FRAMES_PER_CMD;
142
143   dsp_rx_regs->rx_command =
144     MK_RX_CMD(FRAMES_PER_CMD * streaming_items_per_frame,
145               streaming_items_per_frame,
146               1, 1);                    // set "chain" bit
147
148   // kick off the state machine
149   dbsm_start(&dsp_rx_sm);
150
151   dsp_rx_regs->rx_time = 0;             // enqueue first of two commands
152
153   // make sure this one and the rest have the "now" and "chain" bits set.
154   dsp_rx_regs->rx_command =
155     MK_RX_CMD(FRAMES_PER_CMD * streaming_items_per_frame,
156               streaming_items_per_frame,
157               1, 1);                            
158
159   dsp_rx_regs->rx_time = 0;             // enqueue second command
160 }
161
162 void
163 start_rx_streaming_cmd(const u2_mac_addr_t *host, op_start_rx_streaming_t *p)
164 {
165   host_mac_addr = *host;        // remember who we're sending to
166
167   /*
168    * Construct  ethernet header and word0 and preload into two buffers
169    */
170   u2_eth_packet_t       pkt;
171   memset(&pkt, 0, sizeof(pkt));
172   pkt.ehdr.dst = *host;
173   pkt.ehdr.src = *ethernet_mac_addr();
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 start_rx_streaming_at_cmd(const u2_mac_addr_t *host, op_start_rx_streaming_t *p, uint32_t time)
191 {}
192 void restart_streaming_at(uint32_t time)
193 {}
194
195 void
196 stop_rx_cmd(void)
197 {
198   streaming_p = false;
199   dsp_rx_regs->clear_state = 1; // flush cmd queue
200   bp_clear_buf(DSP_RX_BUF_0);
201   bp_clear_buf(DSP_RX_BUF_1);
202 }
203
204
205 static void
206 setup_tx()
207 {
208   dsp_tx_regs->clear_state = 1;
209   bp_clear_buf(DSP_TX_BUF_0);
210   bp_clear_buf(DSP_TX_BUF_1);
211
212   int tx_scale = 256;
213   int interp = 32;
214
215   // setup some defaults
216
217   dsp_tx_regs->freq = 0;
218   dsp_tx_regs->scale_iq = (tx_scale << 16) | tx_scale;
219   dsp_tx_regs->interp_rate = interp;
220 }
221
222
223 #if (FW_SETS_SEQNO)
224 /*
225  * Debugging ONLY.  This will be handled by the tx_protocol_engine.
226  *
227  * This is called when the DSP Rx chain has filled in a packet.
228  * We set and increment the seqno, then return false, indicating
229  * that we didn't handle the packet.  A bit of a kludge
230  * but it should work.
231  */
232 bool 
233 fw_sets_seqno_inspector(dbsm_t *sm, int buf_this)       // returns false
234 {
235   uint32_t *p = buffer_ram(buf_this);
236   uint32_t seqno = fw_seqno++;
237
238   // KLUDGE all kinds of nasty magic numbers and embedded knowledge
239   uint32_t t = p[4];
240   t = (t & 0xffff00ff) | ((seqno & 0xff) << 8);
241   p[4] = t;
242
243   // queue up another rx command when required
244   if (streaming_p && --streaming_frame_count == 0){
245     streaming_frame_count = FRAMES_PER_CMD;
246     dsp_rx_regs->rx_time = 0;
247   }
248
249   return false;         // we didn't handle the packet
250 }
251 #endif
252
253
254 inline static void
255 buffer_irq_handler(unsigned irq)
256 {
257   uint32_t  status = buffer_pool_status->status;
258
259   dbsm_process_status(&dsp_tx_sm, status);
260   dbsm_process_status(&dsp_rx_sm, status);
261 }
262
263 int test_ram()
264 {
265   int i,j,k;
266   output_regs->ram_page = 1<<10;
267   
268   extram[0] = 0xDEADBEEF;
269   extram[1] = 0xF00D1234;
270   extram[7] = 0x76543210;
271   
272   output_regs->ram_page = 2<<10;
273   extram[7] = 0x55555555;
274   extram[1] = 0xaaaaaaaa;
275   extram[0] = 0xeeeeeeee;
276   
277   output_regs->ram_page = 1<<10;
278   
279   i = extram[0];
280   k = extram[1];
281   j = extram[7];
282   
283   if((i != 0xDEADBEEF)||(j!=0x76543210)||(k!=0xF00D1234)) {
284     puts("RAM FAIL1!\n");
285     puthex32_nl(i);
286     puthex32_nl(j);
287     puthex32_nl(k);
288     return 0;
289   }
290   
291   output_regs->ram_page = 2<<10;
292
293   j = extram[7];
294   k = extram[1];
295   i = extram[0];
296
297   if((i != 0xeeeeeeee)||(j!=0x55555555)||(k!=0xaaaaaaaa)) {
298     puts("RAM FAIL2!\n");
299     puthex32_nl(i);
300     puthex32_nl(j);
301     puthex32_nl(k);
302     return 0;
303   }
304   return 1;
305 }
306
307 int test_sd()
308 {
309   int i = sd_init();
310   if(i==0) {
311     puts("FAILED INIT of Card\n");
312     return 0;
313   }
314   
315   unsigned char buf[512];
316   i = sd_read_block(2048,buf);
317   if(i == 0) {
318     puts("READ Command Rejected\n");
319     return 0;
320   }
321   if((buf[0]==0xb8)&&(buf[1]==0x08)&&(buf[2]==0x00)&&(buf[3]==0x50))
322     ;
323   else {
324     puts("Read bad data from SD Card\n");
325     return 0;
326   }
327   return 1;
328 }
329
330 int
331 main(void)
332 {
333   u2_init();
334
335   putstr("\nFactory Test\n");
336
337   print_mac_addr(ethernet_mac_addr()->addr);
338   newline();
339
340   if(test_sd())
341     puts("SD OK\n");
342   else {
343     puts("SD FAIL\n");
344     //    hal_finish();
345     //return 0;
346   }
347   if(test_ram())
348     puts("RAM OK\n");
349   else {
350     puts("RAM FAIL\n");
351     hal_finish();
352     return 0;
353   }
354
355   print_mac_addr(ethernet_mac_addr()->addr);
356   newline();
357
358   output_regs->led_src = 0x7;  // make bottom 3 controlled by HW
359
360   ethernet_register_link_changed_callback(link_changed_callback);
361   ethernet_init();
362
363   clocks_enable_tx_dboard(true,1);
364   clocks_mimo_config(MC_WE_LOCK_TO_SMA);
365 #if 0
366   // make bit 15 of Tx gpio's be a s/w output
367   hal_gpio_set_sel(GPIO_TX_BANK, 15, 's');
368   hal_gpio_set_ddr(GPIO_TX_BANK, 0x8000, 0x8000);
369 #endif
370
371   output_regs->debug_mux_ctrl = 1;
372 #if 0
373   hal_gpio_set_sels(GPIO_TX_BANK, "1111111111111111");
374   hal_gpio_set_sels(GPIO_RX_BANK, "1111111111111111");
375   hal_gpio_set_ddr(GPIO_TX_BANK, 0xffff, 0xffff);
376   hal_gpio_set_ddr(GPIO_RX_BANK, 0xffff, 0xffff);
377 #endif
378
379
380   // initialize double buffering state machine for ethernet -> DSP Tx
381
382   dbsm_init(&dsp_tx_sm, DSP_TX_BUF_0,
383             &dsp_tx_recv_args, &dsp_tx_send_args,
384             eth_pkt_inspector);
385
386
387   // initialize double buffering state machine for DSP RX -> Ethernet
388
389   if (FW_SETS_SEQNO){
390     dbsm_init(&dsp_rx_sm, DSP_RX_BUF_0,
391               &dsp_rx_recv_args, &dsp_rx_send_args,
392               fw_sets_seqno_inspector);
393   }
394   else {
395     dbsm_init(&dsp_rx_sm, DSP_RX_BUF_0,
396               &dsp_rx_recv_args, &dsp_rx_send_args,
397               dbsm_nop_inspector);
398   }
399
400   // tell app_common that this dbsm could be sending to the ethernet
401   ac_could_be_sending_to_eth = &dsp_rx_sm;
402
403
404   // program tx registers
405   setup_tx();
406
407   // kick off the state machine
408   dbsm_start(&dsp_tx_sm);
409
410   //int which = 0;
411
412   while(1){
413     // hal_gpio_write(GPIO_TX_BANK, which, 0x8000);
414     // which ^= 0x8000;
415
416     buffer_irq_handler(0);
417
418     int pending = pic_regs->pending;            // poll for under or overrun
419
420     if (pending & PIC_UNDERRUN_INT){
421       dbsm_handle_tx_underrun(&dsp_tx_sm);
422       pic_regs->pending = PIC_UNDERRUN_INT;     // clear interrupt
423       putchar('U');
424     }
425
426     if (pending & PIC_OVERRUN_INT){
427       dbsm_handle_rx_overrun(&dsp_rx_sm);
428       pic_regs->pending = PIC_OVERRUN_INT;      // clear pending interrupt
429
430       // FIXME Figure out how to handle this robustly.
431       // Any buffers that are emptying should be allowed to drain...
432
433       if (streaming_p){
434         // restart_streaming();
435         // FIXME report error
436       }
437       else {
438         // FIXME report error
439       }
440       putchar('O');
441     }
442   }
443 }