Imported Upstream version 3.2.2
[debian/gnuradio] / usrp2 / firmware / apps / sd_bounce.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 /* 
19  * Loopback SERDES to SERDES
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include "u2_init.h"
26 #include "memory_map.h"
27 #include "spi.h"
28 #include "hal_io.h"
29 #include "buffer_pool.h"
30 #include "pic.h"
31 #include "bool.h"
32 #include "nonstdio.h"
33 #include "memset_wa.h"
34 #include <dbsm.h>
35 #include <stddef.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <clocks.h>
39
40
41
42 // ----------------------------------------------------------------
43
44 #define SERDES_RX_BUF_0         0
45 #define SERDES_RX_BUF_1         1
46
47 /*
48  * ================================================================
49  *      configure SD RX double buffering state machine
50  * ================================================================
51  */
52
53 // receive from SERDES
54 buf_cmd_args_t sd_recv_args = {
55   PORT_SERDES,
56   0,
57   BP_LAST_LINE
58 };
59
60 // send to SERDES
61 buf_cmd_args_t sd_send_args = {
62   PORT_SERDES,
63   0,            // starts with packet in line 0
64   0,            // filled in from list_line register
65 };
66
67 dbsm_t sd_sm;   // the state machine
68
69
70
71
72 // ----------------------------------------------------------------
73
74 #if 0
75 static bool
76 check_packet(int *buf, int nlines)
77 {
78   bool ok = true;
79   int i = 0;
80   for (i = 0; i < nlines; i++){
81     int expected = ((2*i + 0) << 16) | (2*i+1);
82     if (buf[i] != expected){
83       ok = false;
84       printf("buf[%d] = 0x%x  expected = 0x%x\n", i, buf[i], expected);
85     }
86   }
87   return ok;
88 }
89
90 static void
91 zero_buffer(int bufno)
92 {
93   memset_wa(buffer_ram(bufno), 0, BP_NLINES * 4);
94 }
95 #endif
96
97
98 bool
99 sd_rx_inspector(dbsm_t *sm, int buf_this)
100 {
101   hal_toggle_leds(0x2);
102
103 #if 0
104   int last_line = buffer_pool_status->last_line[buf_this];
105   bool ok = check_packet(buffer_ram(buf_this), last_line);
106   static int good = 0;
107   static int bad = 0;
108
109   if (ok)
110     good++;
111   else
112     bad++;
113
114   if(good+bad == 10000) {
115     printf("Good %d\tBad %d\n",good,bad);
116     good = 0;
117     bad = 0;
118   }
119 #endif
120
121   return false;
122 }
123
124
125 inline static void
126 buffer_irq_handler(void)
127 {
128   uint32_t  status = buffer_pool_status->status;
129   dbsm_process_status(&sd_sm, status);
130 }
131
132
133 int
134 main(void)
135 {
136   u2_init();
137
138   putstr("\nsd_bounce\n");
139
140   // Get our clock from the mimo interface
141   clocks_mimo_config(MC_WE_LOCK_TO_MIMO);
142
143   dbsm_init(&sd_sm, SERDES_RX_BUF_0,
144             &sd_recv_args, &sd_send_args,
145             sd_rx_inspector);
146
147   // kick off the state machine
148   dbsm_start(&sd_sm);
149
150   while(1){
151     buffer_irq_handler();
152   }
153 }