dd2bcf1edd5edc39a82c5a7104276a80f83b8978
[debian/gnuradio] / usrp2 / firmware / include / usrp2_types.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008 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 #ifndef INCLUDED_USRP2_TYPES_H
19 #define INCLUDED_USRP2_TYPES_H
20
21 #include <usrp2_cdefs.h>
22 #include <stdint.h>
23
24 __U2_BEGIN_DECLS
25
26 /*!
27  * \brief Fixed point representation of a frequency in Hertz (VITA-49 compatible)
28  *
29  * 64-bit two's complement, with the radix point 20 bits up from the bottom. 
30  * Q44.20 format (20 bits to the right of the radix point)
31  *
32  * Values range from +/- 8.79 terahertz with a resolution of 0.95 microhertz.
33  */
34 typedef int64_t u2_fxpt_freq_t;
35
36 #define U2_FPF_RP       20      // location of radix point in u2_fxpt_freq_t
37
38 // macro so we can init structs at compile time
39 #define U2_DOUBLE_TO_FXPT_FREQ(f) (int64_t)((f) * (1LL << U2_FPF_RP))
40
41 static inline u2_fxpt_freq_t
42 u2_double_to_fxpt_freq(double f)
43 {
44   return U2_DOUBLE_TO_FXPT_FREQ(f);
45 }
46
47 static inline int
48 u2_fxpt_freq_round_to_int(u2_fxpt_freq_t fx)
49 {
50   return (int)((fx+(1<<(U2_FPF_RP-1)))>>U2_FPF_RP);
51 }
52
53 static inline double
54 u2_fxpt_freq_to_double(u2_fxpt_freq_t fx)
55 {
56   return ((double) fx) * 1.0/(1 << U2_FPF_RP);
57 }
58
59 static inline uint32_t
60 u2_fxpt_freq_hi(u2_fxpt_freq_t f)
61 {
62   return ((f >> 32) & 0xffffffff);
63 }
64
65 static inline uint32_t
66 u2_fxpt_freq_lo(u2_fxpt_freq_t f)
67 {
68   return (f & 0xffffffff);
69 }
70
71 static inline u2_fxpt_freq_t
72 u2_fxpt_freq_from_hilo(uint32_t hi, uint32_t lo)
73 {
74   return (((u2_fxpt_freq_t) hi) << 32) | lo;
75 }
76
77 /*!
78  * \brief Fixed point representation of a gain in dB (VITA-49 compatible)
79  *
80  * 16-bit two's complement, with the radix point 7 bits up from the bottom. 
81  * Q9.7 format (7 bits to the right of the radix point)
82  */
83 typedef int16_t u2_fxpt_gain_t;
84
85 #define U2_FPG_RP       7       // location of radix point in u2_fxpt_gain_t
86
87 // macro so we can init structs at compile time
88 #define U2_DOUBLE_TO_FXPT_GAIN(g) (int16_t)((g) * (1 << U2_FPG_RP))
89
90 static inline u2_fxpt_gain_t
91 u2_double_to_fxpt_gain(double g)
92 {
93   return U2_DOUBLE_TO_FXPT_GAIN(g);
94 }
95
96 static inline float
97 u2_fxpt_gain_to_double(u2_fxpt_gain_t fx)
98 {
99   return ((double) fx) * 1.0/(1 << U2_FPG_RP);
100 }
101
102 static inline int
103 u2_fxpt_gain_round_to_int(u2_fxpt_gain_t fx)
104
105   return (int)((fx+(1<<(U2_FPG_RP-1)))>>U2_FPG_RP);
106 }
107
108
109 __U2_END_DECLS
110
111
112 #endif /* INCLUDED_USRP2_TYPES_H */