view DDS4xAD9912.c @ 45:b47b97cfd050

Fix output frequency read back in DDS Fox driver The FTW registry value was read back wrong due to an integer overflow in the conversion from bytes string to double. This should fix the "jumps" seen in the application of the frequency dedrifting.
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Wed, 10 Oct 2012 12:14:01 +0200
parents d9aae7d7f2c6
children 0e9f95525a8f
line wrap: on
line source

#include <utility.h>

//==============================================================================
//
// Title:       DDS4xAD9912.c
// Purpose:     
//
// Created on:  by Volodimer Laurent, modified by YLC
// Copyright:   syrte. All Rights Reserved.
//
//==============================================================================

//==============================================================================
// Include files

#include "DDS4xAD9912.h"
#include <userint.h>
#include <tcpsupp.h>
#include <ansi_c.h>

//==============================================================================
// Global functions

static int OnTCPEvent(unsigned handle, int xType, int errCode, void *callbackData)
	{
	switch(xType)
		{
		case TCP_DISCONNECT :
			break;
		case TCP_DATAREADY :
			ClientTCPRead (handle, Response, sizeof(Response), 0);
			break;
		}
		return 0;
	}


//==============================================================================
// DDS4xAD9912_SendCmd
//
// FUNC   : envoie une chaine de caracteres sur un port du serveur TCP/IP de la foxboard
// PARAM  : DDSNum contient le numero de la DDS (1,2,3 ou 4)
// PARAM  : Buffer contenant la chaîne de caractères à envoyer
// RETURN : 0 si la connexion et l'ecriture sur le serveur TCP/IP sont OK
//==============================================================================


static int DDS4xAD9912_SendCmd(int DDSNum, char * Buffer)  // essentially a private function
	{
	int res_1,res_2;

	switch(DDSNum) 
		{
		case 1:
			res_1=ConnectToTCPServer (&hConv, Port1, Adress, OnTCPEvent, 0, 0);
			if(res_1 <0) MessagePopup("DDS4xAD9912_1 Serveur", "Connection to server failed !");	
			break;
		case 2:
			res_1=ConnectToTCPServer (&hConv, Port2, Adress, OnTCPEvent, 0, 0); 
			if(res_1 <0) MessagePopup("DDS4xAD9912_2 Serveur", "Connection to server failed !");
			break;
		
		 case 3:
			res_1=ConnectToTCPServer (&hConv, Port3, Adress, OnTCPEvent, 0, 0); 
			if(res_1 <0) MessagePopup("DDS4xAD9912_3 Serveur", "Connection to server failed !");
			break;
		
		 case 4:
			res_1=ConnectToTCPServer (&hConv, Port4, Adress, OnTCPEvent, 0, 0); 
			if(res_1 <0) MessagePopup("DDS4xAD9912_4 Serveur", "Connection to server failed !");
			break;
		
		}
	
	res_2=ClientTCPWrite (hConv, Buffer, strlen(Buffer)+1, 0);
	if(res_2 <0) MessagePopup("DDS Serveur", "Write to server failed !");
	DisconnectFromTCPServer (hConv);
	return (res_1+res_2) ;
	}


//==============================================================================
// DDS4xAD9912_Reset
//
// FUNC   : reset des 4 DDS
// PARAM  :
// RETURN : 0 si le reset de la DDS 1 , DDS 2, DDS3 et DDS4 est OK
//==============================================================================

 int DDS4xAD9912_Reset(DDS4xAD9912_Data * Instance)
 	{
	int res_1,res_2;
	
	sprintf(Request,"set;%i;%i",1,17);
	res_1 = DDS4xAD9912_SendCmd(1,Request) ;
	if(res_1 <0) {
		MessagePopup("DDS Serveur", "DDS4xAD9912_1 Reset failed !") ;
		}
	else Instance->Frequency1 = 0.0 ;
	
	sprintf(Request,"set;%i;%i",1,17);
	res_2 = DDS4xAD9912_SendCmd(2,Request) ; 	 
	if(res_2 <0) {
		MessagePopup("DDS Serveur", "DDS4xAD9912_2 Reset failed !") ;
		}
	else Instance->Frequency2 = 0.0;

	 sprintf(Request,"set;%i;%i",1,17);
	res_2 = DDS4xAD9912_SendCmd(3,Request) ; 	 
	if(res_2 <0) {
		MessagePopup("DDS Serveur", "DDS4xAD9912_3 Reset failed !") ;
		}
	else Instance->Frequency3 = 0.0;
	
	 sprintf(Request,"set;%i;%i",1,17);
	res_2 = DDS4xAD9912_SendCmd(4,Request) ; 	 
	if(res_2 <0) {
		MessagePopup("DDS Serveur", "DDS4xAD9912_4 Reset failed !") ;
		}
	else Instance->Frequency4 = 0.0;
	
	
	return (res_1+res_2);	 
 	}	 


//==============================================================================
// DDS4xAD9912_SetFreq
//
// FUNC   : sort une fréquence sur la DDS1 ,DDS2, DDS3 ou DDS4
// PARAM  : DDSNum contient le numero de la DDS (1, 2 , 3 ou 4)
// RETURN : 0 si la connexion et l'ecriture sur le serveur TCP/IP sont OK
//============================================================================== 
int DDS4xAD9912_SetFrequency(DDS4xAD9912_Data * Instance, int DDSNum, double Freq)
	{
		
	double Word=WORD(Freq,Clk) ;
	double reste ;
	int res_0 ;
	unsigned char b5,b4,b3,b2,b1,b0 ; 

	b5=(unsigned char)(Word/E40);		
	b4=(unsigned char)((Word - E40*b5)/E32);
	b3=(unsigned char)((Word - E40*b5 - E32*b4)/E24);
	b2=(unsigned char)((Word - E40*b5 - E32*b4 - E24*b3)/E16); 
	b1=(unsigned char)((Word - E40*b5 - E32*b4 - E24*b3 - E16*b2)/E8);
	b0=(unsigned char)((Word - E40*b5 - E32*b4 - E24*b3 - E16*b2 - E8*b1)/1.0);
	reste = (Word - E40*b5 -E32*b4 -E24*b3 -E16*b2 -E8*b1-1.0*b0) ;

	sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",0,10,b5,b4,b3,b2,b1,b0); 
	res_0 = DDS4xAD9912_SendCmd(DDSNum, Request) ;
	if (res_0 <0) {
		MessagePopup("DDS Serveur", "SetFrequency function failed !") ;
		}
	else {
		if (DDSNum==1) Instance->Frequency1 = Freq - reste*Clk/E48 ;
		if (DDSNum==2) Instance->Frequency2 = Freq - reste*Clk/E48 ;
		if (DDSNum==3) Instance->Frequency3 = Freq - reste*Clk/E48 ;
		if (DDSNum==4) Instance->Frequency4 = Freq - reste*Clk/E48 ;
		} ;
	  	
	sprintf(Request,"set;%i;%i",0,18); // IO_Update logiciel 
	res_0 = DDS4xAD9912_SendCmd(DDSNum, Request) ;
	if (res_0 <0) {
		MessagePopup("DDS Serveur", "IO_Update function failed !") ;
		}
	return(res_0) ;
	
	} 

 

int DDS4xAD9912_FrequencyRampe ( DDS4xAD9912_Data * Instance, int DDSNum, double Frequin,double Frequout, double Step )  
{
	int i=0,imax=0;
	double Frequactu=0;
	time_t Time ; 
	double delaysecond=0.01;
	
	
	 if ( (Frequout - Frequin)>0)
	 {
		 if ((Frequout-Frequin) > Step)
			 {
				 imax= (Frequout-Frequin)/Step ;
				 for (i=0; i<imax;i++)
				 {
					Delay(delaysecond);
					Frequactu= Frequin+i*Step;
					DDS4xAD9912_SetFrequency(Instance, DDSNum, Frequactu);  

				 }
			 }
	 }
	 
	 else
	 {
		if ((Frequin-Frequout) > Step)
			 {
				 imax= (Frequin-Frequout)/Step ;
				 for (i=0; i<imax;i++)
				 {
					  Delay(delaysecond); 
					  Frequactu= Frequin-i*Step;
					  DDS4xAD9912_SetFrequency(Instance, DDSNum, Frequactu); 
				 }
			 }
		 
	 }
	 
	 DDS4xAD9912_SetFrequency(Instance, DDSNum, Frequout);    
	 return(0);
	
}

/*Word=WORD(Frequactu,Clk);
					b5=(unsigned char)(Word/E48);
					b4=(unsigned char)((Word - E48*b5)/E32);
					b3=(unsigned char)((Word - E48*b5 -E32*b4)/E24);
					b2=(unsigned char)((Word - E48*b5 -E32*b4 -E24*b3)/E16); 
					b1=(unsigned char)((Word - E48*b5 -E32*b4 -E24*b3 -E16*b2)/E8);
					b0=(unsigned char)((Word - E48*b5 -E32*b4 -E24*b3 -E16*b2 -E8*b1)/1.0);
					reste = (Word - E48*b5 -E32*b4 -E24*b3 -E16*b2 -E8*b1-1.0*b0) ;
					sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",0,10,b5,b4,b3,b2,b1,b0);
					res_0 = DDS4xAD9912_SendCmd(DDSNum, Request) ; 
					if (DDSNum==1) Instance->Frequency1 = Frequactu - reste*Clk/E48 ;
					if (DDSNum==2) Instance->Frequency2 = Frequactu - reste*Clk/E48 ;
					if (DDSNum==3) Instance->Frequency3 = Frequactu - reste*Clk/E48 ;
					if (DDSNum==4) Instance->Frequency4 = Frequactu - reste*Clk/E48 ;
					sprintf(Request,"set;%i;%i",0,18);
					res_0 = DDS4xAD9912_SendCmd(DDSNum, Request) ; */