summaryrefslogtreecommitdiff
path: root/Radio/HW/BladeRF/common/thirdparty/ad936x/util.h
blob: 2b3f25e719afb9b9507523b187edc060519c4135 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/***************************************************************************//**
 *   @file   util.h
 *   @brief  Header file of Util driver.
 *   @author DBogdan (dragos.bogdan@analog.com)
********************************************************************************
 * Copyright 2013(c) Analog Devices, Inc.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *  - Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  - Neither the name of Analog Devices, Inc. nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *  - The use of this software may or may not infringe the patent rights
 *    of one or more patent holders.  This license does not release you
 *    from the requirement that you obtain separate licenses from these
 *    patent holders to use this software.
 *  - Use of the software either in source or binary form, must be run
 *    on or directly connected to an Analog Devices Inc. component.
 *
 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/
#ifndef __NO_OS_PORT_H__
#define __NO_OS_PORT_H__

/******************************************************************************/
/***************************** Include Files **********************************/
/******************************************************************************/
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "ad9361.h"
#include "common.h"
#include "config.h"

#ifdef NUAND_MODIFICATIONS
// include adc/dac core headers
#include "adc_core.h"
#include "dac_core.h"
#endif // NUAND_MODIFICATIONS

/******************************************************************************/
/********************** Macros and Constants Definitions **********************/
/******************************************************************************/
#define SUCCESS									0
#ifdef NUAND_MODIFICATIONS
// guard on ARRAY_SIZE (already defined in libbladeRF)
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(arr)							(sizeof(arr) / sizeof((arr)[0]))
#endif
#else
#define ARRAY_SIZE(arr)							(sizeof(arr) / sizeof((arr)[0]))
#endif // NUAND_MODIFICATIONS
#define min(x, y)								(((x) < (y)) ? (x) : (y))
#define min_t(type, x, y)						(type)min((type)(x), (type)(y))
#define max(x, y)								(((x) > (y)) ? (x) : (y))
#define max_t(type, x, y)						(type)max((type)(x), (type)(y))
#define clamp(val, min_val, max_val)			(max(min((val), (max_val)), (min_val)))
#define clamp_t(type, val, min_val, max_val)	(type)clamp((type)(val), (type)(min_val), (type)(max_val))
#define DIV_ROUND_UP(x, y)						(((x) + (y) - 1) / (y))
#define DIV_ROUND_CLOSEST(x, divisor)			(((x) + (divisor) / 2) / (divisor))
#define BIT(x)									(1 << (x))
#define CLK_IGNORE_UNUSED						BIT(3)
#define CLK_GET_RATE_NOCACHE					BIT(6)

#ifdef NUAND_MODIFICATIONS
// Fix a whole slew of MSVC compiler errors...
#if defined(HAVE_VERBOSE_MESSAGES)
#define dev_err(dev, format, ...)		{printf(format, ## __VA_ARGS__);printf("\n"); }
#define dev_warn(dev, format, ...)		{printf(format, ## __VA_ARGS__);printf("\n"); }
#if defined(HAVE_DEBUG_MESSAGES)
#define dev_dbg(dev, format, ...)		{printf(format, ## __VA_ARGS__);printf("\n"); }
#else
#define dev_dbg(dev, format, ...)	{ if (0) printf(format, ## __VA_ARGS__); }
#endif
#define printk(format, ...)			printf(format, ## __VA_ARGS__)
#else
#define dev_err(dev, format, ...)	{ if (0) printf(format, ## __VA_ARGS__); }
#define dev_warn(dev, format, ...)	{ if (0) printf(format, ## __VA_ARGS__); }
#define dev_dbg(dev, format, ...)	{ if (0) printf(format, ## __VA_ARGS__); }
#define printk(format, ...)			{ if (0) printf(format, ## __VA_ARGS__); }
#endif
#else
#if defined(HAVE_VERBOSE_MESSAGES)
#define dev_err(dev, format, ...)		({printf(format, ## __VA_ARGS__);printf("\n"); })
#define dev_warn(dev, format, ...)		({printf(format, ## __VA_ARGS__);printf("\n"); })
#if defined(HAVE_DEBUG_MESSAGES)
#define dev_dbg(dev, format, ...)		({printf(format, ## __VA_ARGS__);printf("\n"); })
#else
#define dev_dbg(dev, format, ...)	({ if (0) printf(format, ## __VA_ARGS__); })
#endif
#define printk(format, ...)			printf(format, ## __VA_ARGS__)
#else
#define dev_err(dev, format, ...)	({ if (0) printf(format, ## __VA_ARGS__); })
#define dev_warn(dev, format, ...)	({ if (0) printf(format, ## __VA_ARGS__); })
#define dev_dbg(dev, format, ...)	({ if (0) printf(format, ## __VA_ARGS__); })
#define printk(format, ...)			({ if (0) printf(format, ## __VA_ARGS__); })
#endif
#endif // NUAND_MODIFICATIONS

struct device {
#ifdef NUAND_MODIFICATIONS
	// fix MSVC code C2016
	void *noop;
#endif // NUAND_MODIFICATIONS
};

struct spi_device {
	struct device	dev;
	uint8_t 		id_no;
#ifdef NUAND_MODIFICATIONS
	// add userdata field
	void			*userdata;
#endif // NUAND_MODIFICATIONS
};

#ifdef NUAND_MODIFICATIONS
// gpio_device struct
struct gpio_device {
	void *userdata;
};
#endif // NUAND_MODIFICATIONS

struct axiadc_state {
	struct ad9361_rf_phy	*phy;
	uint32_t				pcore_version;
#ifdef NUAND_MODIFICATIONS
	// add DAC DDS state, userdata
	struct dds_state		dac_dds_state;
	void					*userdata;
#endif // NUAND_MODIFICATIONS
};

struct axiadc_chip_info {
	char		*name;
	int32_t		num_channels;
};

struct axiadc_converter {
	struct axiadc_chip_info	*chip_info;
	uint32_t				scratch_reg[16];
};

#ifdef WIN32
#include "basetsd.h"
typedef SSIZE_T ssize_t;
#define strsep(s, ct)				0
#define snprintf(s, n, format, ...)	0
#define __func__ __FUNCTION__
#endif

/******************************************************************************/
/************************ Functions Declarations ******************************/
/******************************************************************************/
int32_t clk_prepare_enable(struct clk *clk);
uint32_t clk_get_rate(struct ad9361_rf_phy *phy,
					  struct refclk_scale *clk_priv);
int32_t clk_set_rate(struct ad9361_rf_phy *phy,
					 struct refclk_scale *clk_priv,
					 uint32_t rate);
uint32_t int_sqrt(uint32_t x);
int32_t ilog2(int32_t x);
uint64_t do_div(uint64_t* n,
				uint64_t base);
uint32_t find_first_bit(uint32_t word);
void * ERR_PTR(long error);
void *zmalloc(size_t size);

#endif