Project

General

Profile

RE: SPI interface for ADC5560 (MityDSP L138F) » ADS5560.cpp

Vivek Ponnani, 07/24/2017 03:40 PM

 
1
/**
2
 * @file 		ADS5560.cpp
3
 * @author		James Thesing <james.thesing@criticallink.com>
4
 * @version		1.0
5
 *
6
 * @section LICENSE
7
 *
8
 *
9
 *    o  0
10
 *    | /       Copyright (c) 2005-2011
11
 *  (CL)---o   Critical Link, LLC
12
 *    \
13
 *     O
14
 *
15
 *
16
 * @section DESCRIPTION
17
 *
18
 * This class handles exercising the ADS5560 on the DSP. Input data is
19
 * acquired through an analog source, and capture data is sent back
20
 * to the ARM.
21
 */
22

    
23

    
24

    
25
#include "ADS5560.h"
26
#include "core/DspUpp.h"
27
#include "core/DspSyscfg.h"
28
#include "core/chipconfig.h"
29
#include "core/cl_ipc_inbound.h"
30
#include "core/cl_ipc_outbound.h"
31

    
32

    
33
#include <std.h>
34
#include <tsk.h>
35
#include <clk.h>
36
#include <vector>
37
#include <string>
38
#include <cmath>
39
#include <stdio.h>
40

    
41

    
42
using namespace std;
43
using namespace MityDSP;
44

    
45
const int BUFF_SIZE = 32000; //size of buffers used in upp link
46
const int SAMPLE_SIZE = 200000; //amount of data to collect
47

    
48
//set up buffers
49
#pragma DATA_ALIGN( 64 );
50
int16_t buff1[ BUFF_SIZE ];
51
int16_t buff2[ BUFF_SIZE ];
52
int16_t buff3[ BUFF_SIZE ];
53

    
54

    
55
// Forward declarations
56
string convertInt(int number);
57
void init();
58
void debugPrint( char *buffer );
59
int handleInboundMessage( void *apBuffer, uint32_t anLength, void *apUserArg ) ;
60

    
61

    
62
// Object for sending debug messages (these are received and printed to stdout by tcDspApp)
63
tcCL_IPCOutbound* 	gpDebug;
64
// Object for sending GPPMSGQ1 messages that the ARM will receive
65
tcCL_IPCOutbound* 	gpOutbound;
66
// Object for receiving DSPMSGQ0 messages that the DSP will receive
67
tcCL_IPCInbound* 	gpInbound;
68

    
69
// Initialize static member variables
70
ADS5560* ADS5560::mpUppSignal = NULL;
71

    
72
// uPP configuration record
73
static const tcDspUpp::tsDspUppConfig gsUppConfig =
74
	{
75
	4, // Interrupt level. Must be between 4 and 15.
76
	9, // Chan A DMA thread handling priority
77
	9, // Chan B DMA thread handling priority
78
	tcDspUpp::eeTransmit, // Directionality of Channel A.
79
	tcDspUpp::eeReceive, // Directionality of Channel B.
80
	tcDspUpp::ee16Bit, // Chan A data bit width.
81
	tcDspUpp::ee16Bit, // Chan B data bit width.
82
	false, // Use XData[7:0] for ChanA[15:8] even if ChanB is
83
	// disabled. See Table 3 in uPP User's Guide for details.
84
	4, // Size of MBX for Channel A.
85
	4, // Size of MBX for Channel B.
86
	1, // Clock divider for Channel A (37.5 MHz output clock)
87
	// (only in transmit mode). See Section 2.1.1 in uPP User's
88
	// Guide for details. Value must be between 1 and 16.
89
	0, // Clock divider for Channel B
90
	// (only in transmit mode). See Section 2.1.1 in uPP User's
91
	// Guide for details. Value must be between 1 and 16.
92
	tcDspUpp::ee256Bytes, // Chan A Transmit Thresh
93
	tcDspUpp::ee256Bytes, // Chan A Receive Thresh
94
	tcDspUpp::ee256Bytes, // Chan B Transmit Thresh
95
	tcDspUpp::ee256Bytes, // Chan B Receive Thresh
96
	false, // Do not use Chan A start signal
97
	false, // Do not use Chan B start signal
98
	//tcDspUpp::eeUPP_2xTXCLK, // Using external 2xTxClk for Transmit
99
	tcDspUpp::eePLL0_SYSCLK2,
100
	0 // uPP DMA Master Priority
101
	};
102

    
103
/**
104
 * Private Constructor - initializes the digital output logic
105
 */
106
ADS5560::ADS5560()
107
: mpDspUpp(NULL)
108
, meXmitChan(tcDspUpp::eeChanA)
109
, mhXmitMbx(NULL)
110
, meRecvChan(tcDspUpp::eeChanB)
111
, mhRecvMbx(NULL)
112
, filled(false)
113
{
114
	char debug2[] = "getting instance of DSPuPP\n";
115
	char debug3[] = "configuring UPP\n";
116
	char debug4[] = "upp configured\n";
117
	// Get instance of DspUpp
118
	debugPrint(debug2);
119
	mpDspUpp = tcDspUpp::getInstance();
120

    
121
	// Configure uPP
122
	debugPrint(debug3);
123
	if (0 != mpDspUpp->initialize(&gsUppConfig))
124
	{
125
	char debug1[] = "failed configuring upp";
126
	debugPrint(debug1);
127
	}
128
	else
129
	{
130
	debugPrint(debug4);
131
	// Get the receive and transmit mailboxes
132
	mhXmitMbx = mpDspUpp->getMBX(meXmitChan);
133
	mhRecvMbx = mpDspUpp->getMBX(meRecvChan);
134
	}
135
	//while(lbWaitForMe == true){};
136
}
137

    
138
/**
139
 * Initializes the DSP link and registers message handler
140
 */
141
void init()
142
{
143
	// Message to ARM core.
144
	char lpReturnMessage[] = "DSP Initialization finished.";
145
	// Buffer for return message
146
	char* lpMessageBuffer = NULL;
147

    
148
	// Create the outbound debug link
149
	gpDebug = new tcCL_IPCOutbound("debug");
150

    
151
	// Create the inbound link for messages to the DSP
152
   	gpInbound = new tcCL_IPCInbound();
153

    
154
   	gpInbound->Open("DSPMSGQ0", 8);
155

    
156
	// Create the outbound controller for sending messages to the ARM
157
   	gpOutbound = new tcCL_IPCOutbound("GPPMSGQ1");
158

    
159
	if (NULL != gpInbound)
160
	{
161
		// Register a callback function to handle messages from the ARM
162
   		gpInbound->RegisterCallback(handleInboundMessage, (void*)NULL);
163
	}
164

    
165
	// Now that initialization is complete, let the ARM know with a message
166

    
167
	// Obtain a dsplink buffer for the return message
168
	lpMessageBuffer = (char*)gpOutbound->GetBuffer(strlen(lpReturnMessage) + 1);
169

    
170
	// Make sure we received a valid buffer
171
	if (NULL != lpMessageBuffer)
172
	{
173
		// Copy our message to the buffer
174
		strcpy(lpMessageBuffer, lpReturnMessage);
175

    
176
		// Send the message back to the ARM
177
		gpOutbound->SendMessage(lpMessageBuffer);
178
	}
179
}
180

    
181
/**
182
 * Gets data from the upp interface
183
 * @param SAMPLE_SIZE amount of samples to collect
184
 */
185
void ADS5560::getData( int SAMPLE_SIZE ){
186
	char debug1[] = "failed to send received buffer to upp\n";
187
	char debug7[] = "failed to send data\n ";
188

    
189
	int dataR = 0; //keep track of amount of data collected
190
	bool finished = false; //do not calculate if already done
191

    
192
	//get a receive mailbox
193
	tcDspUpp::tsMbxMsg lsRecvMbxMsg;
194
	int16_t* lpMessageBuffer = NULL;
195

    
196
	//turn on ADC by writing to register 0x66000202
197
	unsigned short *ptr = ( unsigned short* )0x66000202;
198
	*ptr =  1;
199

    
200
	//send buffers to upp to fill
201
	if (0 != mpDspUpp->receive(meRecvChan, (uint8_t*)buff1,
202
			(uint16_t)(BUFF_SIZE * 2) ) )
203
	{
204
			debugPrint(debug1);
205
	}
206

    
207
	if (0 != mpDspUpp->receive(meRecvChan, (uint8_t*)buff2,
208
				(uint16_t)(BUFF_SIZE * 2) ) )
209
	{
210
			debugPrint(debug1);
211
	}
212

    
213
	if (0 != mpDspUpp->receive(meRecvChan, (uint8_t*)buff3,
214
				(uint16_t)(BUFF_SIZE * 2) ) )
215
	{
216
			debugPrint(debug1);
217
	}
218

    
219
	// Wait for receive to finish, send data to ARM and give
220
	// upp buffer back
221
	while( !finished )
222
	{
223

    
224
		MBX_pend(mhRecvMbx, &lsRecvMbxMsg, SYS_FOREVER);
225
		//get finished buffer
226
		lpMessageBuffer = (int16_t*)gpOutbound->GetBuffer(sizeof(int16_t)*BUFF_SIZE);
227
		if (NULL != lpMessageBuffer)
228
		{
229

    
230
			// Copy uppData to the send buffer
231
			memmove(lpMessageBuffer, lsRecvMbxMsg.pBufPtr ,sizeof(int16_t)*BUFF_SIZE );
232

    
233
			// Send the message back to the ARM
234
			gpOutbound->SendMessage(lpMessageBuffer);
235
			//send buffer to upp to fill
236
				if (0 != mpDspUpp->receive(meRecvChan, (uint8_t*)lsRecvMbxMsg.pBufPtr,
237
						(uint16_t)(BUFF_SIZE * 2) ) )
238
				{
239
						debugPrint(debug1);
240
				}
241
			//check to see if enough samples have been collected
242
			if (dataR < (SAMPLE_SIZE - BUFF_SIZE))
243
			{
244
				dataR += BUFF_SIZE;
245
			}
246
			else
247
			{
248
				//loop break when enough data collected
249
				finished = true;
250
			}
251
		}
252
		else
253
		{
254
			debugPrint(debug7);
255
		}
256

    
257
	}
258
	//turn off ADC by writing to register 0x66000202
259
	*ptr =  0;
260
}
261

    
262

    
263

    
264
/**
265
 * Return instance of object or create one to enforce
266
 * singleton use
267
 */
268
ADS5560*
269
ADS5560::getInstance()
270
{
271
	if (NULL == mpUppSignal)
272
		mpUppSignal = new ADS5560();
273
	return mpUppSignal;
274
}
275

    
276
/**
277
 * Destructor
278
 */
279
ADS5560::~ADS5560()
280
{
281

    
282
}
283

    
284
/**
285
 * Main routine for DSPAPP
286
 */
287
int main(int argc, char* argv[])
288
{
289

    
290
	// initialize the DSPLink system
291
	tcCL_IPCInit::GetInstance();
292

    
293
	// Launch an initialization task
294
	TSK_Attrs* lpAttrs = new TSK_Attrs;
295
	*lpAttrs           = TSK_ATTRS;
296
	lpAttrs->name      = "Initialize";
297
	lpAttrs->stacksize = 8192*2;
298
	lpAttrs->priority  = 5;
299
	TSK_create((Fxn)init,lpAttrs);
300
    return 0;
301
}
302

    
303
/**
304
 * handles messages received from the arm
305
 * @param apBuffer buffer location of message
306
 * @param anLength length of buffer used
307
 * @param apUserArg user given arguments
308
 * @return 0;
309
 */
310
int handleInboundMessage(void* apBuffer, uint32_t anLength, void* apUserArg)
311
{
312
	char debug1[] = "Failure to communicate with arm\n";
313
	char returnMsg[] = "DSP App finished\n";
314
	char* lpMessageBuffer = NULL;
315

    
316
	lpMessageBuffer = (char*)gpOutbound->GetBuffer(strlen(returnMsg) + 1);
317
	if (NULL != lpMessageBuffer)
318
	{
319
		// Copy message to the buffer
320
		strcpy(lpMessageBuffer, returnMsg);
321
	}
322
	else
323
	{
324
		debugPrint( debug1 );
325
	}
326
	ADS5560* mhUppSignal = ADS5560::getInstance();
327
	mhUppSignal->getData( SAMPLE_SIZE  );
328
	gpOutbound->SendMessage( lpMessageBuffer );
329
	return 0;
330
}
331

    
332
/**
333
 * prints a character array through a debug IPC object
334
 * output is: "[DSP] <msg>"
335
 * @param pMsg - character array of message to print
336
 */
337
void debugPrint(char* pMsg)
338
{
339
	// The length of the message to be sent
340
	int len = strlen(pMsg);
341
   	// Pointer to dsplink buffer where to write the message
342
	char* pBuffer;
343

    
344
	// Make sure the debug IPC outbound object has been initialized
345
	if (gpDebug == NULL)
346
		return;
347

    
348
	// Get a buffer for the message
349
	pBuffer = (char *)gpDebug->GetBuffer(len+1);
350

    
351
	// Check that the buffer is valid
352
	if (pBuffer)
353
	{
354
		// Copy the message to the buffer
355
		strcpy(pBuffer, pMsg);
356
		// Send the message
357
		gpDebug->SendMessage(pBuffer);
358
	}
359
}
360

    
(2-2/4) Go to top
Add picture from clipboard (Maximum size: 1 GB)