ad9915.c 5.79 KB
/*
 SPI : initialisation du dds et communication SPI
 */
#include"ad9915.h"
#include "spi.h"
#include "ddsFreq.h"

#define debug
//fonction reset
void Send_Reset(int f_dds)
{
		char reset='2';
		write(f_dds,&reset,sizeof(reset)); //reset
		sleep(0.1);
}

//fonction ioupdate
void Send_IO_Update(int f_dds)
{
		char update='1';
		write(f_dds,&update,sizeof(update)); //reset
		sleep(0.1);
}

//fonction write register
void  write_register (int fd, unsigned char addr, unsigned char d3, unsigned char d2, unsigned char d1, unsigned char d0)
{
		unsigned char tx[5]={0};
		tx[0]=addr;
		tx[1]=d3;
		tx[2]=d2;
		tx[3]=d1;
		tx[4]=d0;
		//spi_put_multiple envoie le vecteur dans cet ordre
		//[tx0]-[tx1]-[tx2] -> adresse->bit poids Fort->bit poids faible
		spi_put_multiple(fd,tx,5,NULL,0);
}

//initialisation du dds
void Initialize_DDS (int fd, int f_dds)
{
	Send_Reset(f_dds);
	write_register(fd,CFRAddress[0],CFR1Start[0], CFR1Start[1], CFR1Start[2], CFR1Start[3]);
  	Send_IO_Update (f_dds);    //Send the update to set the control registers
	write_register(fd,CFRAddress[1], CFR2Start[0], CFR2Start[1], CFR2Start[2], CFR2Start[3]);
  	Send_IO_Update (f_dds);
	write_register(fd,CFRAddress[2], CFR3Start[0], CFR3Start[1], CFR3Start[2], CFR3Start[3]);
  	Send_IO_Update (f_dds);
	write_register(fd,CFRAddress[3], CFR4Start[0], CFR4Start[1], CFR4Start[2], CFR4Start[3]);
  	Send_IO_Update (f_dds);
	write_register(fd,USR0Address, 0xA2, 0x00, 0x08, 0x00);
  	Send_IO_Update (f_dds);
}

//calibration du dac
void Calibrate_DAC (int fd, int f_dds)
{		
	write_register(fd,CFRAddress[3], DACCalEnable[0], DACCalEnable[1], DACCalEnable[2], DACCalEnable[3]);
  	Send_IO_Update (f_dds);
	write_register(fd,CFRAddress[3], CFR4Start[0], CFR4Start[1], CFR4Start[2], CFR4Start[3]);
  	Send_IO_Update (f_dds);
}

void modulus_setup(int fd, int f_dds) 
{
	write_register(fd,0x00,0x00, 0x01, 0x01, 0x08); //OSK enable
  	Send_IO_Update (f_dds);
	write_register(fd,0x01,0x00, 0x89, 0x09, 0x00); //enable program modulus and digital ramp
  	Send_IO_Update (f_dds);
}

void basic_setup(int fd, int f_dds,uint16_t ampWord, uint16_t phaseWord)
{
	write_register(fd,0x00,0x00, 0x01, 0x01, 0x08); //OSK enable
  	Send_IO_Update (f_dds);
	write_register(fd,0x01,0x00, 0x89, 0x09, 0x00); //enable program modulus and digital ramp
  	Send_IO_Update (f_dds);
    write_register(fd,0x04,0x19, 0x99, 0x99, 0x99); //ftw
  	Send_IO_Update (f_dds);
    write_register(fd,0x05,0xC0, 0x00, 0x00, 0x00); //A
  	Send_IO_Update (f_dds);
    write_register(fd,0x06,0x00, 0x00, 0x00, 0x05);  //B
  	Send_IO_Update (f_dds);
    //write_register(fd,0x0c, 0x0F, 0xFF, 0x00, 0x00); // amp (12b) ph(16)
    write_register(fd,0x0c, (uint8_t)((ampWord>>8) & 0x0F), (uint8_t)(ampWord & 0xFF), (uint8_t)((phaseWord>>8) & 0xFF), (uint8_t)(phaseWord & 0xFF)); // amp (12b) ph(16)
  	Send_IO_Update (f_dds);
}

void setFreqMM(int fd, int f_dds, unsigned int ftw, unsigned int A, unsigned int B)
{
    //uint16_t phaseWord = 0x7400;
    //uint16_t ampWord = (uint16_t)strtol(argv[4],NULL,0) & 0x0FFF ; //masque en 2
    //uint16_t ampWord = 0x00000FFF;
    //uint32_t phaseAmpWord = phaseWord | ampWord<<16;
		
    //write_register(fd,0x00,0x00, 0x01, 0x01, 0x08); //OSK enable
    //Send_IO_Update(f_dds);
    //write_register(fd,0x01,0x00, 0x89, 0x09, 0x00); //enable program modulus and digital ramp
    //Send_IO_Update(f_dds);
    write_register(fd,0x04,ftw>>24&0xFF, ftw>>16&0xFF, ftw>>8&0xFF, ftw&0xFF); //ftw
    Send_IO_Update(f_dds);
    write_register(fd,0x05,A>>24&0xFF,A>>16&0xFF, A>>8&0xFF, A&0xFF); //A
    Send_IO_Update(f_dds);
    write_register(fd,0x06,B>>24&0xFF,B>>16&0xFF, B>>8&0xFF, B&0xFF); //B
    Send_IO_Update(f_dds);
   // write_register(fd,0x0c, phaseAmpWord>>24&0xFF, phaseAmpWord>>16&0xFF,phaseAmpWord>>8&0xFF,phaseAmpWord&0xFF); // amp (12b) ph(16)
    //Send_IO_Update(f_dds);
}

void setAmpPhaseWord(int fd, int f_dds,unsigned int phaseAmpWord)
{
   write_register(fd,0x0c, phaseAmpWord>>24&0xFF, phaseAmpWord>>16&0xFF,phaseAmpWord>>8&0xFF,phaseAmpWord&0xFF); // amp (12b) ph(16)
   Send_IO_Update(f_dds);
}


int openAndSetDdsFreq( char * device, char * gpio_update, double f_clk, double f_out, uint16_t ampWord, uint16_t phaseWord)
{
	#ifdef debug
	printf("device=%s\tgpio_update=%s\tf_clk=%e\tf_out=%e\tampWord=%d\tphaseWord=%d\n\n",device,gpio_update,f_clk,f_out,ampWord,phaseWord);
	#else
	printf("device=%s\tgpio_update=%s\tf_clk=%e\tf_out=%e\tampWord=%d\tphaseWord=%d\n\n",device,gpio_update,f_clk,f_out,ampWord,phaseWord);
	int fd=configureSpi(device); //ex #define FILENAME2 "/dev/spidev0.0"
	printf("fd(funct)=%d\n",fd);
	setMode(fd,SPI_MODE_0);	
	if (fd <= 0){
		printf("ERREUR : ouverture périphérique SPI %s\n",device);
		return EXIT_FAILURE;	
	}
	int f_dds=open(gpio_update,O_RDWR); //ex #define UPDATE2 "/sys/dds/gpio24/updateOn"
	printf("f_dds(funct)=%d\n",f_dds);
	if (f_dds <= 0){
		printf("ERREUR : ouverture ");printf(gpio_update);printf("\n");
		printf("Chargez le module ddsIOupdateX\n");
		return EXIT_FAILURE;
	}
	setddsFreq(fd,f_dds,f_out,f_clk);
	//debug
	phaseWord = 0x0000;
	ampWord=0x0FFF;
	uint32_t phaseAmpWord = phaseWord | ampWord<<16;
	setAmpPhaseWord(fd, f_dds, phaseAmpWord);
	#endif
	return EXIT_SUCCESS;	
}

int setDdsFreqFull ( int fd, int f_dds, double f_clk, double f_out, uint16_t ampWord, uint16_t phaseWord)
{
	setddsFreq(fd,f_dds,f_out,f_clk);
	#ifdef debug
	phaseWord = 0x0000;
	ampWord=0x0FFF;
	#endif
	uint32_t phaseAmpWord = phaseWord | ampWord<<16;
	setAmpPhaseWord(fd, f_dds, phaseAmpWord);
	
	return EXIT_SUCCESS;
}

int receiveParameterFromPythonServer(char * device, double f_clk, double f_out){

        printf("receiveParameterFromPythonServer::device=%s\tf_clk=%e\tf_out=%e\n",device,f_clk,f_out);
        return 0;
}