aboutsummaryrefslogtreecommitdiffstats
path: root/mpm/lib/mykonos/adi_sample/headless.cpp
blob: 93f079cc2bf47239d85702d1c39fcc5feeef3857 (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
 * \file headless.c
 *
 * \brief Contains example code for user integration with their application
 *
 * All data structures required for operation have been initialized with values which reflect
 * these settings:
 *
 * Device Clock:
 * 125MHz
 *
 * Profiles:
 * Rx 100MHz, IQrate 125MSPS, Dec5
 * Tx 20/100MHz, IQrate 125MSPS, Dec5
 * ORX 100MHz, IQrate 125MSPS, Dec5
 * SRx 20MHz, IQrate 31.25MSPS, Dec5
 *
 */

#include <stdlib.h>
#include <iostream>
#include "headless.h"
#include "../adi/t_mykonos.h"
#include "../adi/mykonos.h"
#include "../adi/mykonos_gpio.h"
#include "../config/mykonos_default_config.h"
//#include "mykonos_static_config.h"

#include <functional>

// dumb function to make the error reporting reusable within this file
void call_mykonos_api(std::function<mykonosErr_t()> f)
{
    auto error = f();
    if (error != MYKONOS_ERR_OK)
    {
        // TODO: make this an exception and stop normal processing
        // in the mean time, print an error and continue happily on
        std::cout << getMykonosErrorMessage(error) << std::endl;
    }
}

int headlessinit(mykonosDevice_t *mykDevice)
{
	uint32_t initCalMask = TX_BB_FILTER | ADC_TUNER | TIA_3DB_CORNER | DC_OFFSET |
	TX_ATTENUATION_DELAY | RX_GAIN_DELAY | FLASH_CAL |
	PATH_DELAY | TX_LO_LEAKAGE_INTERNAL | TX_QEC_INIT |
	LOOPBACK_RX_LO_DELAY | LOOPBACK_RX_RX_QEC_INIT |
	RX_LO_DELAY | RX_QEC_INIT;

	uint32_t trackingCalMask = 	TRACK_RX1_QEC |
	TRACK_RX2_QEC |
	TRACK_TX1_QEC |
	TRACK_TX2_QEC |
	TRACK_ORX1_QEC|
	TRACK_ORX2_QEC;

	/*** < Action: Insert System Clock(s) Initialization Code Here          > ***/

	/*** < Action: Insert BBIC Initialization Code Here                     > ***/

	/*************************************************************************/
	/*****                Mykonos Initialization Sequence                *****/
	/*************************************************************************/

    /*** < Action: Toggle RESETB pin on Mykonos device > ***/
    call_mykonos_api(std::bind(MYKONOS_resetDevice, mykDevice));
    call_mykonos_api(std::bind(MYKONOS_initialize, mykDevice));

	/*************************************************************************/
	/*****                Mykonos CLKPLL Status Check                    *****/
	/*************************************************************************/

    // change logic to timeout/wait for PLL lock
    {
        uint8_t pllLockStatus = 0;
        call_mykonos_api(std::bind(MYKONOS_checkPllsLockStatus, mykDevice, &pllLockStatus));

        if (pllLockStatus & 0x01)
        {
            /*** < User: code here for actions once CLKPLL locked  > ***/
        }
        else
        {
            /*** < User: code here here for actions since CLKPLL not locked
             * ensure lock before proceeding - > ***/
        }
    }

    // Multichip sync was here

	/*************************************************************************/
	/*****                Mykonos Load ARM file                          *****/
	/*************************************************************************/

    // deleted check for PLL lock here, do not advance until PLL is locked

    call_mykonos_api(std::bind(MYKONOS_initArm, mykDevice));
    {
        // TODO: Add code for loading ARM binary here
        uint8_t binary[98304] = { 0 };
        uint32_t count = sizeof(binary);
        call_mykonos_api(std::bind(MYKONOS_loadArmFromBinary, mykDevice, &binary[0], count));
    }

	/*************************************************************************/
	/*****                Mykonos Set RF PLL Frequencies                 *****/
	/*************************************************************************/

    call_mykonos_api(std::bind(MYKONOS_setRfPllFrequency, mykDevice, RX_PLL, mykDevice->rx->rxPllLoFrequency_Hz));
    call_mykonos_api(std::bind(MYKONOS_setRfPllFrequency, mykDevice, TX_PLL, mykDevice->tx->txPllLoFrequency_Hz));
    call_mykonos_api(std::bind(MYKONOS_setRfPllFrequency, mykDevice, SNIFFER_PLL, mykDevice->obsRx->snifferPllLoFrequency_Hz));

	/*** < Action: wait 200ms for PLLs to lock > ***/

    // change logic to wait for rest of PLLs to lock
    {
        uint8_t pllLockStatus = 0;
        call_mykonos_api(std::bind(MYKONOS_checkPllsLockStatus, mykDevice, &pllLockStatus));

        if ((pllLockStatus & 0x0F) == 0x0F)
        {
            /*** < Info: All PLLs locked > ***/
        }
        else
        {
            /*** < Info: PLLs not locked  > ***/
            /*** < Action: Ensure lock before proceeding - User code here > ***/
        }
    }

    // GPIO Ctrl set up was here

	/*************************************************************************/
	/*****                Mykonos Set manual gains values                *****/
	/*************************************************************************/

    call_mykonos_api(std::bind(MYKONOS_setRx1ManualGain, mykDevice, 255));
    call_mykonos_api(std::bind(MYKONOS_setRx2ManualGain, mykDevice, 255));

    // setting gain of obs and sniffer channels was here

	/*************************************************************************/
	/*****                Mykonos Initialize attenuations                *****/
	/*************************************************************************/

    call_mykonos_api(std::bind(MYKONOS_setTx1Attenuation, mykDevice, 0));
    call_mykonos_api(std::bind(MYKONOS_setTx2Attenuation, mykDevice, 0));

	/*************************************************************************/
	/*****           Mykonos ARM Initialization Calibrations             *****/
	/*************************************************************************/

    call_mykonos_api(std::bind(MYKONOS_runInitCals, mykDevice, (initCalMask & ~TX_LO_LEAKAGE_EXTERNAL)));

    {
        uint8_t errorFlag = 0;
        uint8_t errorCode = 0;
        call_mykonos_api(std::bind(MYKONOS_waitInitCals, mykDevice, 60000, &errorFlag, &errorCode));

        if ((errorFlag != 0) || (errorCode != 0))
        {
            mykonosInitCalStatus_t initCalStatus = { 0 };
            call_mykonos_api(std::bind(MYKONOS_getInitCalStatus, mykDevice, &initCalStatus));

            /*** < Info: abort init cals > ***/
            uint32_t initCalsCompleted = 0;
            call_mykonos_api(std::bind(MYKONOS_abortInitCals, mykDevice, &initCalsCompleted));
            if (initCalsCompleted)
            {
                /*** < Info: which calls had completed, per the mask > ***/
            }

            uint16_t errorWord = 0;
            uint16_t statusWord = 0;
            call_mykonos_api(std::bind(MYKONOS_readArmCmdStatus, mykDevice, &errorWord, &statusWord));

            uint8_t status = 0;
            call_mykonos_api(std::bind(MYKONOS_readArmCmdStatusByte, mykDevice, 2, &status));

            if (status != 0)
            {
                /*** < Info: Arm Mailbox Status Error errorWord > ***/
                /*** < Info: Pending Flag per opcode statusWord, this follows the mask > ***/
            }
        }
        else
        {
            /*** < Info: Calibrations completed successfully  > ***/
        }
    }

	/*************************************************************************/
	/*****  Mykonos ARM Initialization External LOL Calibrations with PA *****/
	/*************************************************************************/
	/*** < Action: Please ensure PA is enabled operational at this time > ***/
	if (initCalMask & TX_LO_LEAKAGE_EXTERNAL)
	{
        call_mykonos_api(std::bind(MYKONOS_runInitCals, mykDevice, TX_LO_LEAKAGE_EXTERNAL));

        uint8_t errorFlag = 0;
        uint8_t errorCode = 0;
        call_mykonos_api(std::bind(MYKONOS_waitInitCals, mykDevice, 60000, &errorFlag, &errorCode));

		if ((errorFlag != 0) || (errorCode != 0))
		{
            mykonosInitCalStatus_t initCalStatus = { 0 };
            call_mykonos_api(std::bind(MYKONOS_getInitCalStatus, mykDevice, &initCalStatus));

			/*** < Info: abort init cals > ***/
            uint32_t initCalsCompleted = 0;
            call_mykonos_api(std::bind(MYKONOS_abortInitCals, mykDevice, &initCalsCompleted));
			if (initCalsCompleted)
			{
				/*** < Info: which calls had completed, per the mask > ***/
			}

            uint16_t errorWord = 0;
            uint16_t statusWord = 0;
            call_mykonos_api(std::bind(MYKONOS_readArmCmdStatus, mykDevice, &errorWord, &statusWord));

            uint8_t status = 0;
            call_mykonos_api(std::bind(MYKONOS_readArmCmdStatusByte, mykDevice, 2, &status));
			if (status != 0)
			{
				/*** < Info: Arm Mailbox Status Error errorWord > ***/
				/*** < Info: Pending Flag per opcode statusWord, this follows the mask > ***/
			}
		}
		else
		{
			/*** < Info: Calibrations completed successfully  > ***/
		}
	}

	/*************************************************************************/
	/*****             SYSTEM JESD bring up procedure                    *****/
	/*************************************************************************/
	/*** < Action: Make sure SYSREF is stopped/disabled > ***/
	/*** < Action: Make sure BBIC JESD is reset and ready to recieve CGS chars> ***/

    call_mykonos_api(std::bind(MYKONOS_enableSysrefToRxFramer, mykDevice, 1));

	/*** < Info: Mykonos is waiting for sysref in order to start
	 * transmitting CGS from the RxFramer> ***/

    call_mykonos_api(std::bind(MYKONOS_enableSysrefToObsRxFramer, mykDevice, 1));

	/*** < Info: Mykonos is waiting for sysref in order to start
	 * transmitting CGS from the ObsRxFramer> ***/

	/*** < User: Make sure SYSREF is stopped/disabled > ***/
    call_mykonos_api(std::bind(MYKONOS_enableSysrefToDeframer, mykDevice, 0));

    call_mykonos_api(std::bind(MYKONOS_resetDeframer, mykDevice));
    call_mykonos_api(std::bind(MYKONOS_enableSysrefToDeframer, mykDevice, 1));

	/*** < User: make sure BBIC JESD framer is actively transmitting CGS> ***/

	/*************************************************************************/
	/*****            Enable SYSREF to Mykonos and BBIC                  *****/
	/*************************************************************************/
	/*** < Action: Sends SYSREF Here > ***/

	/*** < Info: Mykonos is actively transmitting CGS from the RxFramer> ***/

	/*** < Info: Mykonos is actively transmitting CGS from the ObsRxFramer> ***/

	/*** < Action: Insert User: BBIC JESD Sync Verification Code Here > ***/

	/*************************************************************************/
	/*****               Check Mykonos Framer Status                     *****/
	/*************************************************************************/
    {
        uint8_t framerStatus = 0;
        call_mykonos_api(std::bind(MYKONOS_readRxFramerStatus, mykDevice, &framerStatus));
    }

    {
        uint8_t obsFramerStatus = 0;
        call_mykonos_api(std::bind(MYKONOS_readOrxFramerStatus, mykDevice, &obsFramerStatus));
    }

	/*************************************************************************/
	/*****               Check Mykonos Deframer Status                   *****/
	/*************************************************************************/

    {
        uint8_t deframerStatus = 0;
        call_mykonos_api(std::bind(MYKONOS_readDeframerStatus, mykDevice, &deframerStatus));
    }
	/*** < Action: When links have been verified, proceed > ***/

	/*************************************************************************/
	/*****           Mykonos enable tracking calibrations                *****/
	/*************************************************************************/

    call_mykonos_api(std::bind(MYKONOS_enableTrackingCals, mykDevice, trackingCalMask));

	/*** < Info: Allow Rx1/2 QEC tracking and Tx1/2 QEC tracking to run when in the radioOn state
	     *  Tx calibrations will only run if radioOn and the obsRx path is set to OBS_INTERNAL_CALS > ***/

	/*** < Info: Function to turn radio on, Enables transmitters and receivers
	 * that were setup during MYKONOS_initialize() > ***/

    call_mykonos_api(std::bind(MYKONOS_radioOn, mykDevice));
    /*** < Info: Allow TxQEC to run when User: is not actively using ORx receive path > ***/
    call_mykonos_api(std::bind(MYKONOS_setObsRxPathSource, mykDevice, OBS_RXOFF));
    call_mykonos_api(std::bind(MYKONOS_setObsRxPathSource, mykDevice, OBS_INTERNALCALS));

	return 0;
}