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