Merged r11377:11390 from jcorgan/usrp-headers in to trunk.
[debian/gnuradio] / usrp / limbo / inband / usrp_inband_usb_packet.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifndef INCLUDED_USRP_INBAND_USB_PACKET_H_
23 #define INCLUDED_USRP_INBAND_USB_PACKET_H_
24
25 #include <usrp_bytesex.h>
26 #include <mblock/mblock.h>
27 #include <pmt.h>
28 #include <iostream>
29
30 #include <symbols_usrp_low_level_cs.h>
31
32 static const int USB_PKT_SIZE = 512;   // bytes
33 static const int MAX_PAYLOAD = USB_PKT_SIZE-2*sizeof(uint32_t);
34 static const int CONTROL_CHAN = 0x1f;
35
36 class usrp_inband_usb_packet {
37   //
38   // keep raw packet in USRP-endian order
39   //
40   uint32_t            d_word0;
41   uint32_t            d_timestamp;
42   unsigned char   d_payload[MAX_PAYLOAD];
43
44 public:
45
46   enum opcodes {
47     OP_PING_FIXED         = 0x00,
48     OP_PING_FIXED_REPLY   = 0x01,
49     OP_WRITE_REG          = 0x02,
50     OP_WRITE_REG_MASKED   = 0x03,
51     OP_READ_REG           = 0x04,
52     OP_READ_REG_REPLY     = 0x05,
53     OP_I2C_WRITE          = 0x06,
54     OP_I2C_READ           = 0x07,
55     OP_I2C_READ_REPLY     = 0x08,
56     OP_SPI_WRITE          = 0x09,
57     OP_SPI_READ           = 0x0a,
58     OP_SPI_READ_REPLY     = 0x0b,
59     OP_DELAY              = 0x0c
60   };
61
62   enum flags {
63     FL_OVERRUN        = 0x80000000,
64     FL_UNDERRUN       = 0x40000000,
65     FL_DROPPED        = 0x20000000,
66     FL_START_OF_BURST = 0x10000000,
67     FL_END_OF_BURST   = 0x08000000,
68     FL_CARRIER_SENSE  = 0x04000000,
69
70     FL_ALL_FLAGS      = 0xfc000000
71   };
72
73   static const int FL_OVERRUN_SHIFT = 31;
74   static const int FL_UNDERRUN_SHIFT = 30;
75   static const int FL_DROPPED_SHIFT = 29;
76   static const int FL_END_OF_BURST_SHIFT = 27;
77   static const int FL_START_OF_BURST_SHIFT = 28;
78   
79   static const int RSSI_MASK = 0x3f;
80   static const int RSSI_SHIFT = 21;
81
82   static const int CHAN_MASK = 0x1f;
83   static const int CHAN_SHIFT = 16;
84
85   static const int TAG_MASK = 0xf;
86   static const int TAG_SHIFT = 9;
87
88   static const int PAYLOAD_LEN_MASK = 0x1ff;
89   static const int PAYLOAD_LEN_SHIFT = 0;
90
91   // Fixed size for opcode and length fields
92   static const int CS_FIXED_LEN = 2;
93
94   static const int CS_OPCODE_MASK = 0xff;
95   static const int CS_OPCODE_SHIFT = 24;
96
97   static const int CS_LEN_MASK = 0xff;
98   static const int CS_LEN_SHIFT = 16;
99
100   static const int CS_RID_MASK = 0x3f;
101   static const int CS_RID_SHIFT = 10;
102
103   static const int CS_PING_LEN = 2;
104   static const int CS_PINGVAL_MASK = 0x3ff;
105   static const int CS_PINGVAL_SHIFT = 0;
106
107   static const int CS_WRITEREG_LEN = 6;
108   static const int CS_WRITEREGMASKED_LEN = 10;
109   static const int CS_READREG_LEN = 2;
110   static const int CS_READREGREPLY_LEN = 6;
111   static const int CS_REGNUM_MASK = 0x3ff;
112   static const int CS_REGNUM_SHIFT = 0;
113
114   static const int CS_DELAY_LEN = 2;
115   static const int CS_DELAY_MASK = 0xffff;
116   static const int CS_DELAY_SHIFT = 0;
117
118   static const int CS_I2CADDR_MASK = 0x7f;
119   static const int CS_I2CADDR_SHIFT = 0;
120
121   static const int CS_I2CREAD_LEN = 3;
122   static const int CS_I2CREADBYTES_MASK = 0x7f;
123   static const int CS_I2CREADBYTES_SHIFT = 24;
124
125   static const int CS_SPIOPT_MASK = 0xffff;
126   static const int CS_SPIOPT_SHIFT = 0;
127   static const int CS_SPIFORMAT_MASK = 0xff;
128   static const int CS_SPIFORMAT_SHIFT = 16;
129   static const int CS_SPIENABLES_MASK = 0xff;
130   static const int CS_SPIENABLES_SHIFT = 24;
131   static const int CS_SPIREAD_LEN = 7;
132   static const int CS_SPINBYTES_MASK = 0xff;
133   static const int CS_SPINBYTES_SHIFT = 24;
134
135 public:
136   
137   void set_timestamp(uint32_t timestamp){
138     d_timestamp = host_to_usrp_u32(timestamp);
139   }
140
141   void set_end_of_burst() {
142     uint32_t word0 = usrp_to_host_u32(d_word0);
143     word0 |= 1<<FL_END_OF_BURST_SHIFT;
144     d_word0 = host_to_usrp_u32(word0);
145   }
146   
147   void set_header(int flags, int chan, int tag, int payload_len){
148     uint32_t word0 =  ((flags & FL_ALL_FLAGS)
149                        | ((chan & CHAN_MASK) << CHAN_SHIFT)
150                        | ((tag & TAG_MASK) << TAG_SHIFT)
151                        | ((payload_len & PAYLOAD_LEN_MASK) << PAYLOAD_LEN_SHIFT));
152     d_word0 = host_to_usrp_u32(word0);
153   }
154
155   void incr_header_len(int val) {
156     set_header(flags(), chan(), tag(), payload_len() + val);
157   }
158   
159   uint32_t timestamp() const {
160     return usrp_to_host_u32(d_timestamp);
161   }
162
163   int rssi() const {
164     uint32_t word0 = usrp_to_host_u32(d_word0);
165     return (word0 >> RSSI_SHIFT) & RSSI_MASK;
166   }
167
168   int chan() const {
169     uint32_t word0 = usrp_to_host_u32(d_word0);
170     return (word0 >> CHAN_SHIFT) & CHAN_MASK;
171   }
172
173   int tag() const {
174     uint32_t word0 = usrp_to_host_u32(d_word0);
175     return (word0 >> TAG_SHIFT) & TAG_MASK;
176   }
177
178   int payload_len() const {
179     uint32_t word0 = usrp_to_host_u32(d_word0);
180     return (word0 >> PAYLOAD_LEN_SHIFT) & PAYLOAD_LEN_MASK;
181   }
182   
183   int flags() const {
184     return usrp_to_host_u32(d_word0) & FL_ALL_FLAGS;
185   }
186
187   int overrun() const {
188     return (usrp_to_host_u32(d_word0) & FL_OVERRUN) >> FL_OVERRUN_SHIFT;
189   }
190   
191
192   int underrun() const {
193     return (usrp_to_host_u32(d_word0) & FL_UNDERRUN) >> FL_UNDERRUN_SHIFT;
194   }
195
196
197   int start_of_burst() const {
198     return (usrp_to_host_u32(d_word0) & FL_START_OF_BURST) >> FL_START_OF_BURST_SHIFT;
199   }
200
201   int end_of_burst() const {
202     return (usrp_to_host_u32(d_word0) & FL_END_OF_BURST) >> FL_END_OF_BURST_SHIFT;
203   }
204
205   int dropped() const {
206     return (usrp_to_host_u32(d_word0) & FL_DROPPED) >> FL_DROPPED_SHIFT;
207   }
208
209   unsigned char *payload() { 
210     return d_payload; 
211   }
212
213   static int max_payload() {
214     return MAX_PAYLOAD;
215   }
216
217   static int max_pkt_size() {
218     return USB_PKT_SIZE;
219   }
220
221   // C/S methods
222   bool align32();
223   bool cs_ping(long rid, long ping_val);
224   bool cs_ping_reply(long rid, long ping_val);
225   bool cs_write_reg(long reg_num, long val);
226   bool cs_write_reg_masked(long reg_num, long val, long mask);
227   bool cs_read_reg(long rid, long reg_num);
228   bool cs_read_reg_reply(long rid, long reg_num, long reg_val);
229   bool cs_delay(long ticks);
230   bool cs_i2c_write(long i2c_addr, uint8_t *i2c_data, size_t data_len);
231   bool cs_i2c_read(long rid, long i2c_addr, long n_bytes);
232   bool cs_i2c_read_reply(long rid, long i2c_addr, uint8_t *i2c_data, long i2c_data_len);
233   bool cs_spi_write(long enables, long format, long opt_header_bytes, uint8_t *spi_data, long spi_data_len);
234   bool cs_spi_read(long rid, long enables, long format, long opt_header_bytes, long n_bytes);
235   bool cs_spi_read_reply(long rid, uint8_t *spi_data, long spi_data_len);
236   int cs_len(int payload_offset);
237   pmt_t read_subpacket(int payload_offset);
238 };
239
240 #endif