Update incorrectly checked in Makefile.am
[debian/gnuradio] / usrp2 / firmware / apps / mimo_app_common_v2.c
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008,2009 Free Software Foundation, Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include "mimo_app_common_v2.h"
24 #include "buffer_pool.h"
25 #include "memcpy_wa.h"
26 #include "ethernet.h"
27 #include "nonstdio.h"
28 #include "print_rmon_regs.h"
29 #include "db.h"
30 #include "db_base.h"
31 #include "clocks.h"
32 #include "u2_init.h"
33 #include <string.h>
34
35 volatile bool link_is_up = false;       // eth handler sets this
36 int cpu_tx_buf_dest_port = PORT_ETH;
37
38 // If this is non-zero, this dbsm could be writing to the ethernet
39 dbsm_t *ac_could_be_sending_to_eth;
40
41 static unsigned char exp_seqno __attribute__((unused)) = 0;
42
43 void abort(void);
44
45 static bool
46 burn_mac_addr(const op_burn_mac_addr_t *p)
47 {
48   return ethernet_set_mac_addr(&p->addr);
49 }
50
51 static bool
52 sync_to_pps(const op_generic_t *p)
53 {
54   timesync_regs->sync_on_next_pps = 1;
55   putstr("SYNC to PPS\n");
56   return true;
57 }
58
59 static bool
60 config_mimo_cmd(const op_config_mimo_t *p)
61 {
62   clocks_mimo_config(p->flags);
63   return true;
64 }
65
66 void
67 set_reply_hdr(u2_eth_packet_t *reply_pkt, u2_eth_packet_t const *cmd_pkt)
68 {
69   reply_pkt->ehdr.dst = cmd_pkt->ehdr.src;
70   reply_pkt->ehdr.src = *ethernet_mac_addr();
71   reply_pkt->ehdr.ethertype = U2_ETHERTYPE;
72   reply_pkt->thdr.flags = 0;
73   reply_pkt->thdr.fifo_status = 0;      // written by protocol engine
74   reply_pkt->thdr.seqno = 0;            // written by protocol engine
75   reply_pkt->thdr.ack = 0;              // written by protocol engine
76   u2p_set_word0(&reply_pkt->fixed, 0, CONTROL_CHAN);
77   reply_pkt->fixed.timestamp = timer_regs->time;
78 }
79
80 static void
81 send_reply(unsigned char *reply, size_t reply_len)
82 {
83   if (reply_len < 64)
84     reply_len = 64;
85
86   // wait for buffer to become idle
87   hal_set_leds(0x4, 0x4);
88   while((buffer_pool_status->status & BPS_IDLE(CPU_TX_BUF)) == 0)
89     ;
90   hal_set_leds(0x0, 0x4);
91
92   // copy reply into CPU_TX_BUF
93   memcpy_wa(buffer_ram(CPU_TX_BUF), reply, reply_len);
94
95   // wait until nobody else is sending to the ethernet
96   if (ac_could_be_sending_to_eth){
97     hal_set_leds(0x8, 0x8);
98     dbsm_wait_for_opening(ac_could_be_sending_to_eth);
99     hal_set_leds(0x0, 0x8);
100   }
101
102   if (0){
103     printf("sending_reply to port %d, len = %d\n", cpu_tx_buf_dest_port, (int)reply_len);
104     print_buffer(buffer_ram(CPU_TX_BUF), reply_len/4);
105   }
106
107   // fire it off
108   bp_send_from_buf(CPU_TX_BUF, cpu_tx_buf_dest_port, 1, 0, reply_len/4);
109
110   // wait for it to complete (not long, it's a small pkt)
111   while((buffer_pool_status->status & (BPS_DONE(CPU_TX_BUF) | BPS_ERROR(CPU_TX_BUF))) == 0)
112     ;
113
114   bp_clear_buf(CPU_TX_BUF);
115 }
116
117
118 static size_t
119 op_id_cmd(const op_generic_t *p,
120           void *reply_payload, size_t reply_payload_space)
121 {
122   op_id_reply_t *r = (op_id_reply_t *) reply_payload;
123   if (reply_payload_space < sizeof(*r)) // no room
124     return 0;
125
126   // Build reply subpacket
127
128   r->opcode = OP_ID_REPLY;
129   r->len = sizeof(op_id_reply_t);
130   r->rid = p->rid;
131   r->addr = *ethernet_mac_addr();
132   r->hw_rev = (u2_hw_rev_major << 8) | u2_hw_rev_minor;
133   // r->fpga_md5sum = ; // FIXME
134   // r->sw_md5sum = ;   // FIXME
135
136   return r->len;
137 }
138
139
140 static size_t
141 config_tx_v2_cmd(const op_config_tx_v2_t *p,
142                  void *reply_payload, size_t reply_payload_space)
143 {
144   op_config_tx_reply_v2_t *r = (op_config_tx_reply_v2_t *) reply_payload;
145   if (reply_payload_space < sizeof(*r))
146     return 0;                                   // no room
147
148   struct tune_result    tune_result;
149   memset(&tune_result, 0, sizeof(tune_result));
150
151   bool ok = true;
152   
153 #if 0
154   if (p->valid & CFGV_GAIN){
155     ok &= db_set_gain(tx_dboard, p->gain);
156   }
157
158   if (p->valid & CFGV_FREQ){
159     bool was_streaming = is_streaming();
160     if (was_streaming)
161       stop_rx_cmd();
162     
163     u2_fxpt_freq_t f = u2_fxpt_freq_from_hilo(p->freq_hi, p->freq_lo);
164     bool tune_ok = db_tune(tx_dboard, f, &tune_result);
165     ok &= tune_ok;
166     print_tune_result("Tx", tune_ok, f, &tune_result);
167
168     if (was_streaming)
169       restart_streaming();
170   }
171
172   if (p->valid & CFGV_INTERP_DECIM){
173     int interp = p->interp;
174     int hb1 = 0;
175     int hb2 = 0;
176
177     if (!(interp & 1)){
178       hb2 = 1;
179       interp = interp >> 1;
180     }
181
182     if (!(interp & 1)){
183       hb1 = 1;
184       interp = interp >> 1;
185     }
186     
187     if (interp < MIN_CIC_INTERP || interp > MAX_CIC_INTERP)
188       ok = false;
189     else {
190       dsp_tx_regs->interp_rate = (hb1<<9) | (hb2<<8) | interp;
191       // printf("Interp: %d, register %d\n", p->interp, (hb1<<9) | (hb2<<8) | interp);
192     }
193   }
194
195   if (p->valid & CFGV_SCALE_IQ){
196     dsp_tx_regs->scale_iq = p->scale_iq;
197   }
198 #endif
199
200   // Build reply subpacket
201
202   r->opcode = OP_CONFIG_TX_REPLY_V2;
203   r->len = sizeof(*r);
204   r->rid = p->rid;
205   r->ok = ok;
206   r->inverted = tune_result.inverted;
207   r->baseband_freq_hi = u2_fxpt_freq_hi(tune_result.baseband_freq);
208   r->baseband_freq_lo = u2_fxpt_freq_lo(tune_result.baseband_freq);
209   r->duc_freq_hi = u2_fxpt_freq_hi(tune_result.dxc_freq);
210   r->duc_freq_lo = u2_fxpt_freq_lo(tune_result.dxc_freq);
211   r->residual_freq_hi = u2_fxpt_freq_hi(tune_result.residual_freq);
212   r->residual_freq_lo = u2_fxpt_freq_lo(tune_result.residual_freq);
213   return r->len;
214 }
215
216 static size_t
217 config_rx_v2_cmd(const op_config_rx_v2_t *p, 
218                  void *reply_payload, size_t reply_payload_space)
219 {
220   op_config_rx_reply_v2_t *r = (op_config_rx_reply_v2_t *) reply_payload;
221   if (reply_payload_space < sizeof(*r))
222     return 0;                           // no room
223
224   struct tune_result    tune_result;
225   memset(&tune_result, 0, sizeof(tune_result));
226
227   bool ok = true;
228   
229   if (p->valid & CFGV_GAIN){
230     ok &= db_set_gain(rx_dboard, p->gain);
231   }
232
233   if (p->valid & CFGV_FREQ){
234     bool was_streaming = is_streaming();
235     if (was_streaming)
236       stop_rx_cmd();
237     
238     u2_fxpt_freq_t f = u2_fxpt_freq_from_hilo(p->freq_hi, p->freq_lo);
239     bool tune_ok = db_tune(rx_dboard, f, &tune_result);
240     ok &= tune_ok;
241     print_tune_result("Rx", tune_ok, f, &tune_result);
242
243     if (was_streaming)
244       restart_streaming();
245   }
246
247   if (p->valid & CFGV_INTERP_DECIM){
248     int decim = p->decim;
249     int hb1 = 0;
250     int hb2 = 0;
251     
252     if(!(decim & 1)) {
253       hb2 = 1;
254       decim = decim >> 1;
255     }
256     
257     if(!(decim & 1)) {
258       hb1 = 1;
259       decim = decim >> 1;
260     }
261     
262     if (decim < MIN_CIC_DECIM || decim > MAX_CIC_DECIM)
263       ok = false;
264     else {
265       dsp_rx_regs->decim_rate = (hb1<<9) | (hb2<<8) | decim;
266       // printf("Decim: %d, register %d\n", p->decim, (hb1<<9) | (hb2<<8) | decim);
267     }
268   }
269
270   if (p->valid & CFGV_SCALE_IQ){
271     dsp_rx_regs->scale_iq = p->scale_iq;
272   }
273
274   // Build reply subpacket
275
276   r->opcode = OP_CONFIG_RX_REPLY_V2;
277   r->len = sizeof(*r);
278   r->rid = p->rid;
279   r->ok = ok;
280   r->inverted = tune_result.inverted;
281   r->baseband_freq_hi = u2_fxpt_freq_hi(tune_result.baseband_freq);
282   r->baseband_freq_lo = u2_fxpt_freq_lo(tune_result.baseband_freq);
283   r->ddc_freq_hi = u2_fxpt_freq_hi(tune_result.dxc_freq);
284   r->ddc_freq_lo = u2_fxpt_freq_lo(tune_result.dxc_freq);
285   r->residual_freq_hi = u2_fxpt_freq_hi(tune_result.residual_freq);
286   r->residual_freq_lo = u2_fxpt_freq_lo(tune_result.residual_freq);
287
288   return r->len;
289 }
290
291 static size_t
292 read_time_cmd(const op_generic_t *p,
293               void *reply_payload, size_t reply_payload_space)
294 {
295   op_read_time_reply_t *r = (op_read_time_reply_t *) reply_payload;
296   if (reply_payload_space < sizeof(*r))         
297     return 0;                                   // no room
298
299   r->opcode = OP_READ_TIME_REPLY;
300   r->len = sizeof(*r);
301   r->rid = p->rid;
302   r->time = timer_regs->time;
303
304   return r->len;
305 }
306
307 static void
308 fill_db_info(u2_db_info_t *p, const struct db_base *db)
309 {
310   p->dbid = db->dbid;
311   p->freq_min_hi = u2_fxpt_freq_hi(db->freq_min);
312   p->freq_min_lo = u2_fxpt_freq_lo(db->freq_min);
313   p->freq_max_hi = u2_fxpt_freq_hi(db->freq_max);
314   p->freq_max_lo = u2_fxpt_freq_lo(db->freq_max);
315   p->gain_min = db->gain_min;
316   p->gain_max = db->gain_max;
317   p->gain_step_size = db->gain_step_size;
318 }
319
320 static size_t
321 dboard_info_cmd(const op_generic_t *p,
322                 void *reply_payload, size_t reply_payload_space)
323 {
324   op_dboard_info_reply_t *r = (op_dboard_info_reply_t *) reply_payload;
325   if (reply_payload_space < sizeof(*r))         
326     return 0;                                   // no room
327
328   r->opcode = OP_DBOARD_INFO_REPLY;
329   r->len = sizeof(*r);
330   r->rid = p->rid;
331   r->ok = true;
332
333   fill_db_info(&r->tx_db_info, tx_dboard);
334   fill_db_info(&r->rx_db_info, rx_dboard);
335
336   return r->len;
337 }
338
339 static size_t
340 peek_cmd(const op_peek_t *p,
341          void *reply_payload, size_t reply_payload_space)
342 {
343   op_generic_t *r = (op_generic_t *) reply_payload;
344
345   putstr("peek: addr="); puthex32(p->addr);
346   printf(" bytes=%u\n", p->bytes);
347
348   if ((reply_payload_space < (sizeof(*r) + p->bytes)) ||
349       p->bytes > MAX_SUBPKT_LEN - sizeof(op_generic_t)) {
350     putstr("peek: insufficient reply packet space\n");
351     return 0;                   // FIXME do partial read?
352   }
353
354   r->opcode = OP_PEEK_REPLY;
355   r->len = sizeof(*r)+p->bytes;
356   r->rid = p->rid;
357   r->ok = true;
358
359   memcpy_wa(reply_payload+sizeof(*r), (void *)p->addr, p->bytes);
360
361   return r->len;
362 }
363
364 static bool
365 poke_cmd(const op_poke_t *p)
366 {
367   int bytes = p->len - sizeof(*p);
368   putstr("poke: addr="); puthex32(p->addr);
369   printf(" bytes=%u\n", bytes);
370
371   uint8_t *src = (uint8_t *)p + sizeof(*p);
372   memcpy_wa((void *)p->addr, src, bytes);
373
374   return true;
375 }
376
377 static size_t
378 generic_reply(const op_generic_t *p,
379               void *reply_payload, size_t reply_payload_space,
380               bool ok)
381 {
382   op_generic_t *r = (op_generic_t *) reply_payload;
383   if (reply_payload_space < sizeof(*r))         
384     return 0;                                   // no room
385
386   r->opcode = p->opcode | OP_REPLY_BIT;
387   r->len = sizeof(*r);
388   r->rid = p->rid;
389   r->ok = ok;
390
391   return r->len;
392 }
393
394 static size_t
395 add_eop(void *reply_payload, size_t reply_payload_space)
396 {
397   op_generic_t *r = (op_generic_t *) reply_payload;
398   if (reply_payload_space < sizeof(*r))         
399     return 0;                                   // no room
400
401   r->opcode = OP_EOP;
402   r->len = sizeof(*r);
403   r->rid = 0;
404   r->ok =  0;
405
406   return r->len;
407 }
408
409 void
410 handle_control_chan_frame(u2_eth_packet_t *pkt, size_t len)
411 {
412   unsigned char reply[sizeof(u2_eth_packet_t) + 4 * sizeof(u2_subpkt_t)] _AL4;
413   unsigned char *reply_payload = &reply[sizeof(u2_eth_packet_t)];
414   int reply_payload_space = sizeof(reply) - sizeof(u2_eth_packet_t);
415
416   // initialize reply
417   memset(reply, 0, sizeof(reply));
418   set_reply_hdr((u2_eth_packet_t *) reply, pkt);
419
420   // point to beginning of payload (subpackets)
421   unsigned char *payload = ((unsigned char *) pkt) + sizeof(u2_eth_packet_t);
422   int payload_len = len - sizeof(u2_eth_packet_t);
423
424   size_t subpktlen = 0;
425
426   while (payload_len >= sizeof(op_generic_t)){
427     const op_generic_t *gp = (const op_generic_t *) payload;
428     subpktlen = 0;
429
430     // printf("\nopcode = %d\n", gp->opcode);
431
432     switch(gp->opcode){
433     case OP_EOP:                // end of subpackets
434       goto end_of_subpackets;
435
436     case OP_ID:
437       subpktlen = op_id_cmd(gp, reply_payload, reply_payload_space);
438       break;
439     
440     case OP_CONFIG_TX_V2:
441       subpktlen = config_tx_v2_cmd((op_config_tx_v2_t *) payload,
442                                    reply_payload, reply_payload_space);
443       break;
444
445     case OP_CONFIG_RX_V2:
446       subpktlen = config_rx_v2_cmd((op_config_rx_v2_t *) payload,
447                                    reply_payload, reply_payload_space);
448       break;
449
450     case OP_START_RX_STREAMING:
451       start_rx_streaming_cmd(&pkt->ehdr.src, (op_start_rx_streaming_t *) payload);
452       subpktlen = generic_reply(gp, reply_payload, reply_payload_space, true);
453       break;
454     
455     case OP_STOP_RX:
456       stop_rx_cmd();
457       subpktlen = generic_reply(gp, reply_payload, reply_payload_space, true);
458       break;
459     
460     case OP_BURN_MAC_ADDR:
461       subpktlen = generic_reply(gp, reply_payload, reply_payload_space,
462                                 burn_mac_addr((op_burn_mac_addr_t *) payload));
463       break;
464
465     case OP_CONFIG_MIMO:
466       subpktlen = generic_reply(gp, reply_payload, reply_payload_space,
467                                 config_mimo_cmd((op_config_mimo_t *) payload));
468       break;
469
470     case OP_READ_TIME:
471       subpktlen = read_time_cmd(gp, reply_payload, reply_payload_space);
472       break;
473
474     case OP_DBOARD_INFO:
475       subpktlen = dboard_info_cmd(gp, reply_payload, reply_payload_space);
476       break;
477
478     case OP_SYNC_TO_PPS:
479       subpktlen = generic_reply(gp, reply_payload, reply_payload_space,
480                                 sync_to_pps((op_generic_t *) payload));
481       break;
482
483     case OP_PEEK:
484       subpktlen = peek_cmd((op_peek_t *)payload, reply_payload, reply_payload_space);
485       break;
486
487     case OP_POKE:
488       subpktlen = generic_reply(gp, reply_payload, reply_payload_space,
489                                 poke_cmd((op_poke_t *)payload));
490       break;
491
492     default:
493       printf("app_common_v2: unhandled opcode = %d\n", gp->opcode);
494       break;
495     }
496
497     int t = (gp->len + 3) & ~3;         // bump to a multiple of 4
498     payload += t;
499     payload_len -= t;
500
501     subpktlen = (subpktlen + 3) & ~3;   // bump to a multiple of 4
502     reply_payload += subpktlen;
503     reply_payload_space -= subpktlen;
504   }
505
506  end_of_subpackets:
507
508   // add the EOP marker
509   subpktlen = add_eop(reply_payload, reply_payload_space);
510   subpktlen = (subpktlen + 3) & ~3;     // bump to a multiple of 4
511   reply_payload += subpktlen;
512   reply_payload_space -= subpktlen;
513
514   send_reply(reply, reply_payload - reply);
515 }
516
517
518 /*
519  * Called when an ethernet packet is received.
520  * Return true if we handled it here, otherwise
521  * it'll be passed on to the DSP Tx pipe
522  */
523 int
524 eth_pkt_inspector(bsm12_t *sm, int bufno)
525 {
526   u2_eth_packet_t *pkt = (u2_eth_packet_t *) buffer_ram(bufno);
527   size_t byte_len = (buffer_pool_status->last_line[bufno] - 3) * 4;
528
529   //static size_t last_len = 0;
530
531   // hal_toggle_leds(0x1);
532
533   // inspect rcvd frame and figure out what do do.
534
535   if (pkt->ehdr.ethertype != U2_ETHERTYPE)
536     return true;        // ignore, probably bogus PAUSE frame from MAC
537
538   int chan = u2p_chan(&pkt->fixed);
539
540   switch (chan){
541   case CONTROL_CHAN:
542     handle_control_chan_frame(pkt, byte_len);
543     return -1;
544     break;
545
546   case 0:
547     return 0;   // pass it off to DSP TX
548
549   case 1:
550     return 1;   // pass it off to SERDES TX
551
552   default:
553     abort();
554     break;
555   }
556 }
557
558 /*
559  * Called when eth phy state changes (w/ interrupts disabled)
560  */
561 void
562 link_changed_callback(int speed)
563 {
564   link_is_up = speed != 0;
565   hal_set_leds(link_is_up ? LED_RJ45 : 0x0, LED_RJ45);
566   printf("\neth link changed: speed = %d\n", speed);
567 }
568
569
570 void
571 print_tune_result(char *msg, bool tune_ok,
572                   u2_fxpt_freq_t target_freq, struct tune_result *r)
573 {
574 #if 0
575   printf("db_tune %s %s\n", msg, tune_ok ? "true" : "false");
576   putstr("  target_freq   "); print_fxpt_freq(target_freq); newline();
577   putstr("  baseband_freq "); print_fxpt_freq(r->baseband_freq); newline();
578   putstr("  dxc_freq      "); print_fxpt_freq(r->dxc_freq); newline();
579   putstr("  residual_freq "); print_fxpt_freq(r->residual_freq); newline();
580   printf("  inverted      %s\n", r->inverted ? "true" : "false");
581 #endif
582 }