Commit 09ecc607dab6561abeb414705310f22a061517de

Authored by bachi
1 parent dfa91a3bc3
Exists in master

mise a jour avec la lib dans paulo bachir/lib

Showing 3 changed files with 1268 additions and 3 deletions Inline Diff

/* 1 1 /*
dds initialisation et SPI 2 2 dds initialisation et SPI
*/ 3 3 */
4 4
#include <stdint.h> 5 5 #include <stdint.h>
#include <unistd.h> 6 6 #include <unistd.h>
#include <stdio.h> 7 7 #include <stdio.h>
#include <stdlib.h> 8 8 #include <stdlib.h>
#include <getopt.h> 9 9 #include <getopt.h>
#include <fcntl.h> 10 10 #include <fcntl.h>
#include <sys/ioctl.h> 11 11 #include <sys/ioctl.h>
#include <linux/types.h> 12 12 #include <linux/types.h>
#include <linux/spi/spidev.h> 13 13 #include <linux/spi/spidev.h>
/* memory management */ 14 14 /* memory management */
#include <sys/mman.h> 15 15 #include <sys/mman.h>
#include "spi.h" 16 16 #include "spi.h"
#include <string.h> 17 17 #include <string.h>
#include <math.h> 18 18 #include <math.h>
#include <time.h> 19 19 #include <time.h>
20 20
21 21
static const char CFRAddress[] = {0x00,0x01,0x02,0x03}; 22 22 static const char CFRAddress[] = {0x00,0x01,0x02,0x03};
23 23
static const char CFR1Start[] ={0x00, 0x01, 0x00, 0x0a}; 24 24 static const char CFR1Start[] ={0x00, 0x01, 0x00, 0x0a};
//static const char CFR1Start[] ={0x00, 0x01, 0x01, 0x08}; //with osk enable 25 25 //static const char CFR1Start[] ={0x00, 0x01, 0x01, 0x08}; //with osk enable
static const char CFR2Start[] ={0x00, 0x80, 0x09, 0x00}; //0x80 enable prof mode 26 26 static const char CFR2Start[] ={0x00, 0x80, 0x09, 0x00}; //0x80 enable prof mode
static const char CFR3Start[] = {0x00, 0x00, 0x19, 0x1C}; 27 27 static const char CFR3Start[] = {0x00, 0x00, 0x19, 0x1C};
static const char CFR4Start[] = {0x00, 0x05, 0x21, 0x20}; 28 28 static const char CFR4Start[] = {0x00, 0x05, 0x21, 0x20};
29 //static const char CFR4Start[] = {0x20, 0x21, 0x05, 0x00};
29 30
31
static const char USR0Address = 0x1B; 30 32 static const char USR0Address = 0x1B;
static const char FTW0Address = 0x0b; //profile 0 ftw 31 33 static const char FTW0Address = 0x0b; //profile 0 ftw
static const char PA0Address = 0x0c; //profile 0 32 34 static const char PA0Address = 0x0c; //profile 0
33 35
static const char DACCalEnable[] = {0x01, 0x05, 0x21, 0x20}; //Command to enable the DAC Cal, should be 0x01XXXXXX, where X is the last 6 digits of CFR4Start 34 36 static const char DACCalEnable[] = {0x01, 0x05, 0x21, 0x20}; //Command to enable the DAC Cal, should be 0x01XXXXXX, where X is the last 6 digits of CFR4Start
35 37
36 38
void Send_Reset(int f_dds); 37 39 void Send_Reset(int f_dds);
void Send_IO_Update (int f_dds); //Send the update to set the control registers 38 40 void Send_IO_Update (int f_dds); //Send the update to set the control registers
void write_register (int fd, unsigned char addr, unsigned char d3, unsigned char d2, unsigned char d1, unsigned char d0); 39 41 void write_register (int fd, unsigned char addr, unsigned char d3, unsigned char d2, unsigned char d1, unsigned char d0);
42 void read_register (int fd, unsigned char addr, uint32_t *readword);
43 void read_register_ini (int fd,unsigned char addr);
void Initialize_DDS (int fd, int f_dds); 40 44 void Initialize_DDS (int fd, int f_dds);
void Calibrate_DAC(int fd, int f_dds); 41 45 void Calibrate_DAC(int fd, int f_dds);
void basic_setup(int fd, int f_dds, uint16_t ampWord, uint16_t phaseWord); 42 46 void basic_setup(int fd, int f_dds, uint16_t ampWord, uint16_t phaseWord);
void modulus_setup(int fd, int f_dds); 43 47 void modulus_setup(int fd, int f_dds);
void setFreqMM(int fd, int f_dds, unsigned int ftw, unsigned int A, unsigned int B); 44 48 void setFreqMM(int fd, int f_dds, unsigned int ftw, unsigned int A, unsigned int B);
void setAmpPhaseWord(int fd, int f_dds,unsigned int phaseAmpWord); 45 49 void setAmpPhaseWord(int fd, int f_dds,unsigned int phaseAmpWord);
50 void checkSize(void);
int openAndSetDdsFreq( char * device, char * gpio_update, double f_clk, double f_out, uint16_t ampWord, uint16_t phaseWord); 46 51 int openAndSetDdsFreq( char * device, char * gpio_update, double f_clk, double f_out, uint16_t ampWord, uint16_t phaseWord);
int receiveParameterFromPythonServer(char * device, double f_clk, double f_out); 47 52 int receiveParameterFromPythonServer(char * device, double f_clk, double f_out);
int setDdsFreqFull ( int fd, int f_dds, double f_clk, double f_out, uint16_t ampWord, uint16_t phaseWord); 48 53 int setDdsFreqFull ( int fd, int f_dds, double f_clk, double f_out, uint16_t ampWord, uint16_t phaseWord);
54
55 //------------ DRCTL
56
57 void Send_CTRL_UP(int fp);
58 void Send_CTRL_DOWN(int fp);
59 void Send_HOLD(int fp);
60 void Send_UNHOLD(int fp);
61 int testRampFreq(int fd, int f_dds, int fp);
62
63 //-------Profile register mode
64
65 int InitialisationProfile (int fd,int f_dds);
66 int PutFrequencyAmpPhaseWord(int fd, int f_dds, double fout, double fclk,double amp, double phase);
67
68
69 int PutFrequencyWord(int fd, int f_dds, double freq);//Attention uniquement valable pour fclk = 1 GHz
70 int PutPhaseWord(int fd, int f_dds, double phase);
71 int PutAmpWord(int fd, int f_dds, double amplitude);
72
73 //fonctions rampes hardwres
74
75 int FrequencySweep (int fd, int f_dds,double fclk,double dfUp, double dfDown, double FreqMax,double FreqMin, double DeltaTimeUp, double DeltaTimeDown);
76 int ContinueFrequencySweep (int fd, int f_dds,double fclk,double dfUp, double dfDown, double FreqMax,double FreqMin, double DeltaTimeUp, double DeltaTimeDown);
77 int AmplitudeSweep (int fd, int f_dds,double fclk,double dAUp, double dADown, double AMax,double AMin, double DeltaTimeUp, double DeltaTimeDown);
78 int ContinueAmplitudeSweep (int fd, int f_dds,double fclk,double dAUp, double dADown, double AMax,double AMin, double DeltaTimeUp, double DeltaTimeDown);
79 int PhaseSweep (int fd, int f_dds,double fclk,double dphiUp, double dphiDown, double PhiMax,double PhiMin, double DeltaTimeUp, double DeltaTimeDown);
80 int ContinuePhaseSweep (int fd, int f_dds,double fclk,double dphiUp, double dphiDown, double PhiMax,double PhiMin, double DeltaTimeUp, double DeltaTimeDown);
81
82 //fin fonctions rampes hardwares
83
84 // fonctions rampes softwares
85
86 int RampAmpFromSoft(int fd, int f_dds,double DeltaTimeUp,double AIni, double AFin);
/* 1 1 /*
SPI : initialisation du dds et communication SPI 2 2 SPI : initialisation du dds et communication SPI
*/ 3 3 */
#include"ad9915.h" 4 4 #include"ad9915.h"
#include "spi.h" 5 5 #include "spi.h"
#include "ddsFreq.h" 6 6 #include "ddsFreq.h"
7 7
#define debug 8 8 #define debug
//fonction reset 9 9 //fonction reset
void Send_Reset(int f_dds) 10 10 void Send_Reset(int f_dds)
{ 11 11 {
char reset='2'; 12 12 char reset='2';
write(f_dds,&reset,sizeof(reset)); //reset 13 13 write(f_dds,&reset,sizeof(reset)); //reset
sleep(0.1); 14 14 sleep(0.1);
} 15 15 }
16 16
//fonction ioupdate 17 17 //fonction ioupdate
void Send_IO_Update(int f_dds) 18 18 void Send_IO_Update(int f_dds)
{ 19 19 {
char update='1'; 20 20 char update='1';
write(f_dds,&update,sizeof(update)); //reset 21 21 write(f_dds,&update,sizeof(update)); //reset
sleep(0.1); 22 22 sleep(0.1);
} 23 23 }
24 24
//fonction write register 25 25 //fonction write register
void write_register (int fd, unsigned char addr, unsigned char d3, unsigned char d2, unsigned char d1, unsigned char d0) 26 26 void write_register (int fd, unsigned char addr, unsigned char d3, unsigned char d2, unsigned char d1, unsigned char d0)
{ 27 27 {
unsigned char tx[5]={0}; 28 28 unsigned char tx[5]={0};
tx[0]=addr; 29 29 tx[0]=addr;
tx[1]=d3; 30 30 tx[1]=d3;
tx[2]=d2; 31 31 tx[2]=d2;
tx[3]=d1; 32 32 tx[3]=d1;
tx[4]=d0; 33 33 tx[4]=d0;
//spi_put_multiple envoie le vecteur dans cet ordre 34 34 //spi_put_multiple envoie le vecteur dans cet ordre
//[tx0]-[tx1]-[tx2] -> adresse->bit poids Fort->bit poids faible 35 35 //[tx0]-[tx1]-[tx2] -> adresse->bit poids Fort->bit poids faible
spi_put_multiple(fd,tx,5,NULL,0); 36 36 spi_put_multiple(fd,tx,5,NULL,0);
} 37 37 }
38 38
39 //fonction read register
40
41 void read_register (int fd,unsigned char addr, uint32_t *readword)
42 {
43 unsigned char tx[1] = {0};
44 unsigned char rx[4] = {0};
45 addr = addr|0x80;//instruction byte for the read : 1000 0000 | byte_adress
46 tx[0]=addr;
47 spi_put_multiple(fd,tx,1,rx,4);
48 //printf("TX:%x RX:%x %x %x %x\n",tx[0],rx[0],rx[1],rx[2],rx[3]);
49 *readword = 0x00000000;
50 *readword = *readword|(rx[0]<<24);
51 *readword = *readword|(rx[1]<<16);
52 *readword = *readword|(rx[2]<<8);
53 *readword = *readword|rx[3];
54 }
55
56 //fonction read register
57 void read_register_ini (int fd, unsigned char addr)
58 {
59 unsigned char tx[1] = {0};
60 unsigned char rx[4] = {0};
61 addr = addr|0x80;
62 tx[0]=addr;
63 spi_put_multiple(fd,tx,1,rx,4);
64 printf("TX:%x RX:%x %x %x %x\n",tx[0],rx[0],rx[1],rx[2],rx[3]);
65 // Send_IO_Update (f_dds); //Send the update to set the control registers
66 }
67
//initialisation du dds 39 68 //initialisation du dds
void Initialize_DDS (int fd, int f_dds) 40 69 void Initialize_DDS (int fd, int f_dds)
{ 41 70 {
Send_Reset(f_dds); 42 71 Send_Reset(f_dds);
write_register(fd,CFRAddress[0],CFR1Start[0], CFR1Start[1], CFR1Start[2], CFR1Start[3]); 43 72 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 44 73 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]); 45 74 write_register(fd,CFRAddress[1], CFR2Start[0], CFR2Start[1], CFR2Start[2], CFR2Start[3]);
Send_IO_Update (f_dds); 46 75 Send_IO_Update (f_dds);
write_register(fd,CFRAddress[2], CFR3Start[0], CFR3Start[1], CFR3Start[2], CFR3Start[3]); 47 76 write_register(fd,CFRAddress[2], CFR3Start[0], CFR3Start[1], CFR3Start[2], CFR3Start[3]);
Send_IO_Update (f_dds); 48 77 Send_IO_Update (f_dds);
write_register(fd,CFRAddress[3], CFR4Start[0], CFR4Start[1], CFR4Start[2], CFR4Start[3]); 49 78 write_register(fd,CFRAddress[3], CFR4Start[0], CFR4Start[1], CFR4Start[2], CFR4Start[3]);
Send_IO_Update (f_dds); 50 79 Send_IO_Update (f_dds);
write_register(fd,USR0Address, 0xA2, 0x00, 0x08, 0x00); 51 80 write_register(fd,USR0Address, 0xA2, 0x00, 0x08, 0x00);
Send_IO_Update (f_dds); 52 81 Send_IO_Update (f_dds);
} 53 82 }
54 83
//calibration du dac 55 84 //calibration du dac
void Calibrate_DAC (int fd, int f_dds) 56 85 void Calibrate_DAC (int fd, int f_dds)
{ 57 86 {
write_register(fd,CFRAddress[3], DACCalEnable[0], DACCalEnable[1], DACCalEnable[2], DACCalEnable[3]); 58 87 write_register(fd,CFRAddress[3], DACCalEnable[0], DACCalEnable[1], DACCalEnable[2], DACCalEnable[3]);
Send_IO_Update (f_dds); 59 88 Send_IO_Update (f_dds);
write_register(fd,CFRAddress[3], CFR4Start[0], CFR4Start[1], CFR4Start[2], CFR4Start[3]); 60 89 write_register(fd,CFRAddress[3], CFR4Start[0], CFR4Start[1], CFR4Start[2], CFR4Start[3]);
Send_IO_Update (f_dds); 61 90 Send_IO_Update (f_dds);
} 62 91 }
63 92
void modulus_setup(int fd, int f_dds) 64 93 void modulus_setup(int fd, int f_dds)
{ 65 94 {
write_register(fd,0x00,0x00, 0x01, 0x01, 0x08); //OSK enable 66 95 write_register(fd,0x00,0x00, 0x01, 0x01, 0x0a); //OSK enable //0x08
96 //write_register(fd,0x00,0x00, 0x01, 0x01, 0x08); //OSK enable de base
Send_IO_Update (f_dds); 67 97 Send_IO_Update (f_dds);
write_register(fd,0x01,0x00, 0x89, 0x09, 0x00); //enable program modulus and digital ramp 68 98 write_register(fd,0x01,0x00, 0x89, 0x09, 0x00); //enable program modulus and digital ramp
Send_IO_Update (f_dds); 69 99 Send_IO_Update (f_dds);
} 70 100 }
71 101
void basic_setup(int fd, int f_dds,uint16_t ampWord, uint16_t phaseWord) 72 102 void basic_setup(int fd, int f_dds,uint16_t ampWord, uint16_t phaseWord)
{ 73 103 {
write_register(fd,0x00,0x00, 0x01, 0x01, 0x08); //OSK enable 74 104 write_register(fd,0x00,0x00, 0x01, 0x01, 0x08); //OSK enable
Send_IO_Update (f_dds); 75 105 Send_IO_Update (f_dds);
write_register(fd,0x01,0x00, 0x89, 0x09, 0x00); //enable program modulus and digital ramp 76 106 write_register(fd,0x01,0x00, 0x89, 0x09, 0x00); //enable program modulus and digital ramp
Send_IO_Update (f_dds); 77 107 Send_IO_Update (f_dds);
write_register(fd,0x04,0x19, 0x99, 0x99, 0x99); //ftw 78 108 write_register(fd,0x04,0x19, 0x99, 0x99, 0x99); //ftw
Send_IO_Update (f_dds); 79 109 Send_IO_Update (f_dds);
write_register(fd,0x05,0xC0, 0x00, 0x00, 0x00); //A 80 110 write_register(fd,0x05,0xC0, 0x00, 0x00, 0x00); //A
Send_IO_Update (f_dds); 81 111 Send_IO_Update (f_dds);
write_register(fd,0x06,0x00, 0x00, 0x00, 0x05); //B 82 112 write_register(fd,0x06,0x00, 0x00, 0x00, 0x05); //B
Send_IO_Update (f_dds); 83 113 Send_IO_Update (f_dds);
//write_register(fd,0x0c, 0x0F, 0xFF, 0x00, 0x00); // amp (12b) ph(16) 84 114 //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) 85 115 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); 86 116 Send_IO_Update (f_dds);
} 87 117 }
88 118
void setFreqMM(int fd, int f_dds, unsigned int ftw, unsigned int A, unsigned int B) 89 119 void setFreqMM(int fd, int f_dds, unsigned int ftw, unsigned int A, unsigned int B)
{ 90 120 {
//uint16_t phaseWord = 0x7400; 91 121 //uint16_t phaseWord = 0x7400;
//uint16_t ampWord = (uint16_t)strtol(argv[4],NULL,0) & 0x0FFF ; //masque en 2 92 122 //uint16_t ampWord = (uint16_t)strtol(argv[4],NULL,0) & 0x0FFF ; //masque en 2
//uint16_t ampWord = 0x00000FFF; 93 123 //uint16_t ampWord = 0x00000FFF;
//uint32_t phaseAmpWord = phaseWord | ampWord<<16; 94 124 //uint32_t phaseAmpWord = phaseWord | ampWord<<16;
95 125
//write_register(fd,0x00,0x00, 0x01, 0x01, 0x08); //OSK enable 96 126 //write_register(fd,0x00,0x00, 0x01, 0x01, 0x08); //OSK enable
//Send_IO_Update(f_dds); 97 127 //Send_IO_Update(f_dds);
//write_register(fd,0x01,0x00, 0x89, 0x09, 0x00); //enable program modulus and digital ramp 98 128 //write_register(fd,0x01,0x00, 0x89, 0x09, 0x00); //enable program modulus and digital ramp
//Send_IO_Update(f_dds); 99 129 //Send_IO_Update(f_dds);
write_register(fd,0x04,ftw>>24&0xFF, ftw>>16&0xFF, ftw>>8&0xFF, ftw&0xFF); //ftw 100 130 write_register(fd,0x04,ftw>>24&0xFF, ftw>>16&0xFF, ftw>>8&0xFF, ftw&0xFF); //ftw
Send_IO_Update(f_dds); 101 131 Send_IO_Update(f_dds);
write_register(fd,0x05,A>>24&0xFF,A>>16&0xFF, A>>8&0xFF, A&0xFF); //A 102 132 write_register(fd,0x05,A>>24&0xFF,A>>16&0xFF, A>>8&0xFF, A&0xFF); //A
Send_IO_Update(f_dds); 103 133 Send_IO_Update(f_dds);
write_register(fd,0x06,B>>24&0xFF,B>>16&0xFF, B>>8&0xFF, B&0xFF); //B 104 134 write_register(fd,0x06,B>>24&0xFF,B>>16&0xFF, B>>8&0xFF, B&0xFF); //B
Send_IO_Update(f_dds); 105 135 Send_IO_Update(f_dds);
// write_register(fd,0x0c, phaseAmpWord>>24&0xFF, phaseAmpWord>>16&0xFF,phaseAmpWord>>8&0xFF,phaseAmpWord&0xFF); // amp (12b) ph(16) 106 136 // write_register(fd,0x0c, phaseAmpWord>>24&0xFF, phaseAmpWord>>16&0xFF,phaseAmpWord>>8&0xFF,phaseAmpWord&0xFF); // amp (12b) ph(16)
//Send_IO_Update(f_dds); 107 137 //Send_IO_Update(f_dds);
} 108 138 }
109 139
void setAmpPhaseWord(int fd, int f_dds,unsigned int phaseAmpWord) 110 140 void setAmpPhaseWord(int fd, int f_dds,unsigned int phaseAmpWord)
{ 111 141 {
write_register(fd,0x0c, phaseAmpWord>>24&0xFF, phaseAmpWord>>16&0xFF,phaseAmpWord>>8&0xFF,phaseAmpWord&0xFF); // amp (12b) ph(16) 112 142 write_register(fd,0x0c, phaseAmpWord>>24&0xFF, phaseAmpWord>>16&0xFF,phaseAmpWord>>8&0xFF,phaseAmpWord&0xFF); // amp (12b) ph(16)
Send_IO_Update(f_dds); 113 143 Send_IO_Update(f_dds);
} 114 144 }
115 145
146 /*
147 void getAmpPhaseWord(int fd)
148 {
149 // unsigned char rx[5]={0};
150 unsigned char* pt;
151 read_register(fd,0x0c,rx); // amp (12b) ph(16)
152 rx[0]=*pt;
153 rx[1]=*(pt+1);
154 rx[2]=*(pt+2);
155 rx[3]=*(pt+3);
156 rx[4]=*(pt+4);
157 printf("%c %c %c %c %c", rx[0], rx[1], rx[2], rx[3], rx[4]);
158 }
159 */
160 void checkSize(void)
161 {
162 printf("int : %d\n",sizeof(unsigned int));
163 }
116 164
int openAndSetDdsFreq( char * device, char * gpio_update, double f_clk, double f_out, uint16_t ampWord, uint16_t phaseWord) 117 165 int openAndSetDdsFreq( char * device, char * gpio_update, double f_clk, double f_out, uint16_t ampWord, uint16_t phaseWord)
{ 118 166 {
#ifdef debug 119 167 #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); 120 168 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 121 169 #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); 122 170 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" 123 171 int fd=configureSpi(device); //ex #define FILENAME2 "/dev/spidev0.0"
printf("fd(funct)=%d\n",fd); 124 172 printf("fd(funct)=%d\n",fd);
setMode(fd,SPI_MODE_0); 125 173 setMode(fd,SPI_MODE_0);
if (fd <= 0){ 126 174 if (fd <= 0){
printf("ERREUR : ouverture périphérique SPI %s\n",device); 127 175 printf("ERREUR : ouverture périphérique SPI %s\n",device);
return EXIT_FAILURE; 128 176 return EXIT_FAILURE;
} 129 177 }
int f_dds=open(gpio_update,O_RDWR); //ex #define UPDATE2 "/sys/dds/gpio24/updateOn" 130 178 int f_dds=open(gpio_update,O_RDWR); //ex #define UPDATE2 "/sys/dds/gpio24/updateOn"
printf("f_dds(funct)=%d\n",f_dds); 131 179 printf("f_dds(funct)=%d\n",f_dds);
if (f_dds <= 0){ 132 180 if (f_dds <= 0){
printf("ERREUR : ouverture ");printf(gpio_update);printf("\n"); 133 181 printf("ERREUR : ouverture ");printf(gpio_update);printf("\n");
printf("Chargez le module ddsIOupdateX\n"); 134 182 printf("Chargez le module ddsIOupdateX\n");
return EXIT_FAILURE; 135 183 return EXIT_FAILURE;
} 136 184 }
setddsFreq(fd,f_dds,f_out,f_clk); 137 185 setddsFreq(fd,f_dds,f_out,f_clk);
//debug 138 186 //debug
phaseWord = 0x0000; 139 187 phaseWord = 0x0000;
ampWord=0x0FFF; 140 188 ampWord=0x0FFF;
uint32_t phaseAmpWord = phaseWord | ampWord<<16; 141 189 uint32_t phaseAmpWord = phaseWord | ampWord<<16;
setAmpPhaseWord(fd, f_dds, phaseAmpWord); 142 190 setAmpPhaseWord(fd, f_dds, phaseAmpWord);
#endif 143 191 #endif
return EXIT_SUCCESS; 144 192 return EXIT_SUCCESS;
} 145 193 }
146 194
int setDdsFreqFull ( int fd, int f_dds, double f_clk, double f_out, uint16_t ampWord, uint16_t phaseWord) 147 195 int setDdsFreqFull ( int fd, int f_dds, double f_clk, double f_out, uint16_t ampWord, uint16_t phaseWord)
{ 148 196 {
setddsFreq(fd,f_dds,f_out,f_clk); 149 197 setddsFreq(fd,f_dds,f_out,f_clk);
#ifdef debug 150 198 #ifdef debug
phaseWord = 0x0000; 151 199 phaseWord = 0x0000;
ampWord=0x0FFF; 152 200 ampWord=0x0FFF;
#endif 153 201 #endif
uint32_t phaseAmpWord = phaseWord | ampWord<<16; 154 202 uint32_t phaseAmpWord = phaseWord | ampWord<<16;
setAmpPhaseWord(fd, f_dds, phaseAmpWord); 155 203 setAmpPhaseWord(fd, f_dds, phaseAmpWord);
156 204
return EXIT_SUCCESS; 157 205 return EXIT_SUCCESS;
} 158 206 }
207
208 void Send_CTRL_UP(int fp)
209 {
210 char DRCTLOn='0';//pente positive
211 write(fp,&DRCTLOn,sizeof(DRCTLOn)); //
212 sleep(0.1);
213 }
214
215 void Send_CTRL_DOWN(int fp)
216 {
217 char DRCTLOn='2';//pente negative
218 write(fp,&DRCTLOn,sizeof(DRCTLOn)); //reset
219 sleep(0.1);
220 }
221
222 void Send_HOLD(int fp)
223 {
224 char DRHOLDOn ='1';//rampe bloquée
225 write(fp,&DRHOLDOn,sizeof(DRHOLDOn)); //reset
226 sleep(0.1);
227 }
228
229 void Send_UNHOLD(int fp)
230 {
231 char DRHOLDOn ='3';//rampe debloquée
232 write(fp,&DRHOLDOn,sizeof(DRHOLDOn)); //reset
233 sleep(0.1);
234 }
235
236
237 int testRampFreq(int fd, int f_dds, int fp)
238 {
239
240
241 //3-Rampe de pente positive + debloquage
242 Send_CTRL_DOWN(fp);//rampe up
243 Send_UNHOLD(fp);
244
245 Send_Reset(f_dds);
246 //Initialize_DDS (fd, f_dds);
247
248 //0- paramètre de bases
249 write_register(fd,0x00,0x00,0x01,0x01,0x0a);//OSKenable+Read&write
250 Send_IO_Update(f_dds);
251 read_register_ini(fd,0x00);
252
253 write_register(fd,0x01,0x00, 0x00, 0x09, 0x00); // enable amp ramp
254 Send_IO_Update(f_dds);
255 read_register_ini(fd,0x00);
256
257 //1- Donner une frequence f_dds de 1.1e7 Hz sachant que f_clk=1e9 Hz;
258 // => Le mot a donner est (p.19) : FTW=(f_dds/f_clk)*2³²
259 // FTW = 42949672.96 = 42 949 673. = 0x028f5c29
260 // 1.1e7 =0x02d0e560
261
262 // 2d0e560
263
264 //Calibrate_DAC (fd,f_dds);
265
266
267
268 write_register(fd,0x0b,0x02,0xd0,0xe5,0x60);//mot de frequence
269 Send_IO_Update(f_dds);
270 write_register(fd,0x0c,0x0F,0xFF,0x00,0x00); //mot de amp et de phase : amp max, phase nulle
271 Send_IO_Update(f_dds);
272
273 printf("Les mots de frequences, d'amplitudes et de phases sont envoyes\n");
274 printf("Lecture registre de frequence: 0x0b\n");
275 read_register_ini(fd,0x0b);
276 printf("Lecture registre de amp et phase: 0x0c\n");
277 read_register_ini(fd,0x0c);
278
279 Calibrate_DAC (fd,f_dds);
280 printf("\n\n ----------------------- \n\n");
281
282
283 /*
284 //2- Taille des dt
285
286 write_register(fd,0x08,0xFF,0xFF,0xFF,0xFF); //+dt max, -dt max (+dt = -dt)
287 Send_IO_Update(f_dds);
288
289 printf("Les mots de dt sont envoyes\n");
290 printf("Lecture dans le registre 0x08 : taille dt\n");
291 read_register_ini (fd,0x08);
292
293
294
295
296 printf("\n\n ----------------------- \n\n");
297
298
299 //3- STEP SIZE :dphi
300
301 // en theorie dphi =2.25e-28 degrees
302 write_register(fd,0x06,0x00,0x00,0x00,0x01);
303 Send_IO_Update(f_dds);
304
305 write_register(fd,0x07,0x00,0x00,0x00,0x01);
306 Send_IO_Update(f_dds);
307
308 //printf("\n\n ----------------------- \n\n");
309 printf("Les mots de dphi sont envoyes\n");
310 printf("Lecture dans les registres 0x06 et 0x07 : taille dt\n");
311 read_register_ini (fd,0x06);
312 read_register_ini (fd,0x07);
313
314 printf("\n\n ----------------------- \n\n");
315 //4- Ecrire les extremums de la rampe
316
317 write_register(fd,0x04,0x00,0x00,0x00,0x01);//0 rad
318 Send_IO_Update(f_dds);
319 write_register(fd,0x05,0xFF,0xFF,0xFF,0xFF);//2*pi rad
320 Send_IO_Update(f_dds);
321
322 printf("Les mots des frontieres sont envoyes\n");
323 printf("Lecture dans les registres 0x04 et 0x05 : taille dt\n");
324 read_register_ini (fd,0x04);
325 read_register_ini (fd,0x05);
326
327
328 //3-Rampe de pente positive ou négative
329 // Send_CTRL_UP(fp);//rampe up
330
331
332 //4-ecriture dans le registre 0x01
333 write_register(fd,0x01,0x00, 0xa8, 0x09, 0x00); // enable amp ramp
334 Send_IO_Update(f_dds);
335
336 printf("Lecture dans le registre 0x01 : \n");
337 read_register_ini (fd,0x01);
338 // printf("Les mots de dt sont envoyes\n");
339 printf("Lecture dans le registre 0x08 : taille dt\n");
340 read_register_ini (fd,0x08);
341
342
343 */
344 return EXIT_SUCCESS;
345 }
346
347 //---------------rajout 8/1
348
349 int PutAmpWord(int fd, int f_dds, double amplitude)
350 {
351 uint16_t PhaseWord = 0x0000;
352 uint16_t AmpWord = 0x0000;
353 uint32_t ReadPhaseAmpWord=0x00000000;
354
355 AmpWord = rint(amplitude*(pow(2.0,12.0)-1)/100);
356
357 read_register(fd,0x0c,&ReadPhaseAmpWord);
358
359 PhaseWord = ReadPhaseAmpWord&0xFFFF;
360
361 write_register(fd,0x0c,AmpWord>>8&0xFF,AmpWord&0xFF,PhaseWord>>8&0xFF,PhaseWord&0xFF);
362 Send_IO_Update(f_dds);
363
364 return 0;
365 }
366
367
368 int PutPhaseWord(int fd, int f_dds, double phase)
369 {
370 uint16_t PhaseWord = 0x0000;
371 uint16_t AmpWord = 0x0000;
372 uint32_t ReadPhaseAmpWord=0x00000000;
373
374 PhaseWord = rint(phase*(pow(2.0,16.0)-1)/360);
375
376 read_register(fd,0x0c,&ReadPhaseAmpWord);
377
378 AmpWord = ReadPhaseAmpWord>>16&0xFFFF;
379
380 write_register(fd,0x0c,AmpWord>>8&0xFF,AmpWord&0xFF,PhaseWord>>8&0xFF,PhaseWord&0xFF);
381 Send_IO_Update(f_dds);
382
383 return 0;
384 }
385
386 int PutFrequencyWord(int fd, int f_dds, double fout)//Attention uniquement valable pour fclk = 1 GHz
387 {
388
389 uint32_t FTWR = 0x00000000;
390 FTWR = rint((fout/1e9)*pow(2.0,32.0));
391 write_register(fd,0x0b,FTWR>>24&0xFF,FTWR>>16&0xFF,FTWR>>8&0xFF,FTWR&0xFF);//mot de frequence
392 Send_IO_Update(f_dds);
393
394 return 0;
395 }
396
397
398 int PutFrequencyAmpPhaseWord(int fd, int f_dds, double fout, double fclk,double amp, double phase)//n'existe pas dans le mode profile enable
399 {
400
401 uint32_t FTWR = 0x00000000;
402
403 FTWR = rint((fout/fclk)*pow(2.0,32.0));
404
405 write_register(fd,0x0b,FTWR>>24&0xFF,FTWR>>16&0xFF,FTWR>>8&0xFF,FTWR&0xFF);//mot de frequence
406
407 Send_IO_Update(f_dds);
408
409 // write_register(fd,0x0c,0x0F,0xFF,0x00,0x00); //mot de amp et de phase : amp max, phase nulle
410 // Send_IO_Update(f_dds);
411
412 PutPhaseWord(fd,f_dds,phase);
413 PutAmpWord(fd,f_dds,amp);
414
415 return 0;
416 }
417
418
419 int InitialisationProfile (int fd,int f_dds)
420 {
421
422 Send_Reset(f_dds); //les registres sont maintenant par defaut
423 write_register(fd,0x00,0x00,0x01,0x01,0x0a); //OSK+SDIO input only//ok
424 Send_IO_Update(f_dds);
425 write_register(fd,0x01,0x00,0x80,0x09,0x00); // profile mode enable very important
426 Send_IO_Update(f_dds);
427 write_register(fd,0x02,0x00,0x00,0x19,0x1c); // default
428 Send_IO_Update(f_dds);
429 write_register(fd,0x03,0x01,0x05,0x21,0x20); // calibration 1/2 :enable
430 Send_IO_Update(f_dds);
431 write_register(fd,0x03,0x00,0x05,0x21,0x20); // calibration 2/2 :desenable
432 Send_IO_Update(f_dds);
433
434 return 0;
435 }
436
437
438
439 //------------------- FIN RAJOUT 8/1
440
441
442 //-------------------------FONCTIONS RAMPES HARDWARE
443
444 //1-rampe de frequence
445
446 //rampe simple de fréquence
447 int FrequencySweep (int fd, int f_dds,double fclk,double dfUp, double dfDown, double FreqMax,double FreqMin, double DeltaTimeUp, double DeltaTimeDown)
448 {
449 //calcul de N => N=DeltaF/df = DeltaT/dt
450
451 //1-fmax > fmin
452 double DeltaFreq = FreqMax-FreqMin;
453 if(FreqMax<FreqMin)
454 {
455 printf("FreqMax should be > FreqMin\n");
456 printf("Can't Start the ramp\n");
457 return -1;
458 }
459
460 //2- Calcul du nombre de points de la rampe
461 int NumberPointsUp = DeltaFreq/dfUp;
462 int NumberPointsDown = DeltaFreq/dfDown;
463
464 //3- En déduire la valeur de dtUp et dtDown
465 double dtUp = DeltaTimeUp/NumberPointsUp;
466 double dtDown = DeltaTimeDown/NumberPointsDown;
467
468 double dtmin = 24/fclk;
469 double dtmax = dtmin*(pow(2.0,16.0)-1);
470
471 //Check Up
472 if(dtUp<dtmin)
473 {
474 printf("During of ramp UP should be > %f s\n",dtmin*NumberPointsUp);
475 printf("Can't start the ramp\n");
476 return -1;
477 }
478
479 if(dtUp>dtmax)
480 {
481 printf("During of ramp UP should be < %f\n",dtmax*NumberPointsUp);
482 printf("Can't start the ramp\n");
483 return -1;
484 }
485
486 //Check Down
487 if(dtDown<dtmin)
488 {
489 printf("During of ramp DOWN should be > %f s\n",dtmin*NumberPointsDown);
490 printf("Can't start the ramp\n");
491 return -1;
492 }
493
494 if(dtDown>dtmax)
495 {
496 printf("During of ramp DOWN should be < %f\n",dtmax*NumberPointsDown);
497 printf("Can't start the ramp\n");
498 return -1;
499 }
500
501
502 //4 - Put word in register
503
504
505 //4.1 Limit register
506 uint32_t FTWRmin = rint((FreqMin/fclk)*pow(2.0,32.0));
507 write_register(fd,0x04,FTWRmin>>24&0xFF,FTWRmin>>16&0xFF,FTWRmin>>8&0xFF,FTWRmin&0xFF);//mot de frequence
508 Send_IO_Update(f_dds);
509
510 uint32_t FTWRmax = rint((FreqMax/fclk)*pow(2.0,32.0));
511 write_register(fd,0x05,FTWRmax>>24&0xFF,FTWRmax>>16&0xFF,FTWRmax>>8&0xFF,FTWRmax&0xFF);//mot de frequence
512 Send_IO_Update(f_dds);
513
514
515 //4.2 dfUP et dfDown
516 double df_up = DeltaFreq/NumberPointsUp;
517 double df_down = DeltaFreq/NumberPointsDown;
518
519 uint32_t Word_df_up = rint(df_up*pow(2.0,32.0)/fclk);
520 uint32_t Word_df_down = rint(df_down*pow(2.0,32.0)/fclk);
521
522 write_register(fd,0x06,Word_df_up>>24&0xFF,Word_df_up>>16&0xFF,Word_df_up>>8&0xFF,Word_df_up&0xFF); //df_up
523 Send_IO_Update(f_dds);
524 write_register(fd,0x07,Word_df_down>>24&0xFF,Word_df_down>>16&0xFF,Word_df_down>>8&0xFF,Word_df_down&0xFF);//df_down
525 Send_IO_Update(f_dds);
526
527
528 //4.3 dtUp et dtDown
529
530 uint16_t Word_dt_up = 0x0000;
531 uint16_t Word_dt_down = 0x0000;
532
533 Word_dt_up = rint(dtUp*fclk/24);
534 Word_dt_down = rint(dtDown*fclk/24);
535
536 write_register(fd,0x08,Word_dt_down>>8&0xFF,Word_dt_down&0xFF,Word_dt_up>>8&0xFF,Word_dt_up&0xFF); // il y a 2 dt
537 Send_IO_Update(f_dds);
538
539 //5 Start of the ramp
540
541 write_register(fd,0x01,0x00,0x08,0x09,0x00); //rampe simple
542 Send_IO_Update(f_dds);
543 return 0;
544
545
546
547 return EXIT_SUCCESS;
548
549 }
550
551
552 //rampe continue de fréquence
553 int ContinueFrequencySweep (int fd, int f_dds,double fclk,double dfUp, double dfDown, double FreqMax,double FreqMin, double DeltaTimeUp, double DeltaTimeDown)
554 {
555 //calcul de N => N=DeltaF/df = DeltaT/dt
556
557 //1-fmax > fmin
558 double DeltaFreq = FreqMax-FreqMin;
559 if(FreqMax<FreqMin)
560 {
561 printf("FreqMax should be > FreqMin\n");
562 printf("Can't Start the ramp\n");
563 return -1;
564 }
565
566 //2- Calcul du nombre de points de la rampe
567 int NumberPointsUp = DeltaFreq/dfUp;
568 int NumberPointsDown = DeltaFreq/dfDown;
569
570 //3- En déduire la valeur de dtUp et dtDown
571 double dtUp = DeltaTimeUp/NumberPointsUp;
572 double dtDown = DeltaTimeDown/NumberPointsDown;
573
574 double dtmin = 24/fclk;
575 double dtmax = dtmin*(pow(2.0,16.0)-1);
576
577 //Check Up
578 if(dtUp<dtmin)
579 {
580 printf("During of ramp UP should be > %f s\n",dtmin*NumberPointsUp);
581 printf("Can't start the ramp\n");
582 return -1;
583 }
584
585 if(dtUp>dtmax)
586 {
587 printf("During of ramp UP should be < %f\n",dtmax*NumberPointsUp);
588 printf("Can't start the ramp\n");
589 return -1;
590 }
591
592 //Check Down
593 if(dtDown<dtmin)
594 {
595 printf("During of ramp DOWN should be > %f s\n",dtmin*NumberPointsDown);
596 printf("Can't start the ramp\n");
597 return -1;
598 }
599
600 if(dtDown>dtmax)
601 {
602 printf("During of ramp DOWN should be < %f\n",dtmax*NumberPointsDown);
603 printf("Can't start the ramp\n");
604 return -1;
605 }
606
607
608 //4 - Put word in register
609
610
611 //4.1 Limit register
612 uint32_t FTWRmin = rint((FreqMin/fclk)*pow(2.0,32.0));
613 write_register(fd,0x04,FTWRmin>>24&0xFF,FTWRmin>>16&0xFF,FTWRmin>>8&0xFF,FTWRmin&0xFF);//mot de frequence
614 Send_IO_Update(f_dds);
615
616 uint32_t FTWRmax = rint((FreqMax/fclk)*pow(2.0,32.0));
617 write_register(fd,0x05,FTWRmax>>24&0xFF,FTWRmax>>16&0xFF,FTWRmax>>8&0xFF,FTWRmax&0xFF);//mot de frequence
618 Send_IO_Update(f_dds);
619
620
621 //4.2 dfUP et dfDown
622 double df_up = DeltaFreq/NumberPointsUp;
623 double df_down = DeltaFreq/NumberPointsDown;
624
625 uint32_t Word_df_up = rint(df_up*pow(2.0,32.0)/fclk);
626 uint32_t Word_df_down = rint(df_down*pow(2.0,32.0)/fclk);
627
628 write_register(fd,0x06,Word_df_up>>24&0xFF,Word_df_up>>16&0xFF,Word_df_up>>8&0xFF,Word_df_up&0xFF); //df_up
629 Send_IO_Update(f_dds);
630 write_register(fd,0x07,Word_df_down>>24&0xFF,Word_df_down>>16&0xFF,Word_df_down>>8&0xFF,Word_df_down&0xFF);//df_down
631 Send_IO_Update(f_dds);
632
633
634 //4.3 dtUp et dtDown
635
636 uint16_t Word_dt_up = 0x0000;
637 uint16_t Word_dt_down = 0x0000;
638
639 Word_dt_up = rint(dtUp*fclk/24);
640 Word_dt_down = rint(dtDown*fclk/24);
641
642 write_register(fd,0x08,Word_dt_down>>8&0xFF,Word_dt_down&0xFF,Word_dt_up>>8&0xFF,Word_dt_up&0xFF); // il y a 2 dt
643 Send_IO_Update(f_dds);
644
645 //5 Start of the ramp
646
647 write_register(fd,0x01,0x00,0x8e,0x29,0x00); //rampe triangulaire
648 Send_IO_Update(f_dds);
649 return 0;
650
651
652
653 return EXIT_SUCCESS;
654
655 }
656
657 //2-rampe d'amplitude
658
659 int AmplitudeSweep (int fd, int f_dds,double fclk,double dAUp, double dADown, double AMax,double AMin, double DeltaTimeUp, double DeltaTimeDown)
660 {
661
662
663 //calcul de N => N=DeltaF/df = DeltaT/dt
664
665 //1-Amax > Amin
666 double DeltaAmp = AMax-AMin;
667 if(AMax<AMin)
668 {
669 printf("AMax should be > AMin\n");
670 printf("Can't Start the ramp\n");
671 return -1;
672 }
673
674
675 //2- Calcul du nombre de points de la rampe
676 int NumberPointsUp = DeltaAmp/dAUp;
677 int NumberPointsDown = DeltaAmp/dADown;
678
679 //3- En déduire la valeur de dtUp et dtDown
680 double dtUp = DeltaTimeUp/NumberPointsUp;
681 double dtDown = DeltaTimeDown/NumberPointsDown;
682
683 double dtmin = 24/fclk;
684 double dtmax = dtmin*(pow(2.0,16.0)-1);
685
686 //Check Up
687 if(dtUp<dtmin)
688 {
689 printf("During of ramp UP should be > %f s\n",dtmin*NumberPointsUp);
690 printf("Can't start the ramp\n");
691 return -1;
692 }
693
694 if(dtUp>dtmax)
695 {
696 printf("During of ramp UP should be < %f\n",dtmax*NumberPointsUp);
697 printf("Can't start the ramp\n");
698 return -1;
699 }
700
701 //Check Down
702 if(dtDown<dtmin)
703 {
704 printf("During of ramp DOWN should be > %f s\n",dtmin*NumberPointsDown);
705 printf("Can't start the ramp\n");
706 return -1;
707 }
708
709 if(dtDown>dtmax)
710 {
711 printf("During of ramp DOWN should be < %f\n",dtmax*NumberPointsDown);
712 printf("Can't start the ramp\n");
713 return -1;
714 }
715
716
717
718 //check if DeltaAmp is enouth
719 double enouthUP=0.5*NumberPointsUp*100/4095;
720 double enouthDOWN=0.5*NumberPointsDown*100/4095;
721
722 int borneUP = rint(enouthUP);
723 int borneDown = rint(enouthDOWN);
724
725 if(DeltaAmp<=enouthUP)
726 {
727 printf("The amplitude difference should be > %d\n, decrease Number Points for positive slope",borneUP);
728 //return -1;
729 }
730
731 if(DeltaAmp<=enouthDOWN)
732 {
733 printf("The amplitude difference should be > %d\n, decrease Number Points for negative slope",borneDown);
734 //return -1;
735 }
736
737
738 //4 - Put word in register
739
740
741 //4.1 Limit register
742 uint32_t WordAmin = rint(AMin*4095/100);
743 uint32_t WordAmax= rint(AMax*4095/100);
744
745 write_register(fd,0x04,WordAmin>>24&0xFF,WordAmin>>16&0xFF,WordAmin>>8&0xFF,WordAmin&0xFF); //min => lim-(Amp ) = 0x800 = Amax/2
746 Send_IO_Update(f_dds);
747 write_register(fd,0x05,WordAmax>>24&0xFF,WordAmax>>16&0xFF,WordAmax>>8&0xFF,WordAmax&0xFF);//max => lim+(Amp) = 0xFFF= Amax0
748 Send_IO_Update(f_dds);
749
750
751 //4.2 dAUP et dADown
752
753 uint32_t Word_dA_up = rint(dAUp*4095/100);
754 uint32_t Word_dA_down = rint(dADown*4095/100);
755
756 write_register(fd,0x06,Word_dA_up>>24&0xFF,Word_dA_up>>16&0xFF,Word_dA_up>>8&0xFF,Word_dA_up&0xFF); //dA_up
757 Send_IO_Update(f_dds);
758 write_register(fd,0x07,Word_dA_down>>24&0xFF,Word_dA_down>>16&0xFF,Word_dA_down>>8&0xFF,Word_dA_down&0xFF);//dA_down
759 Send_IO_Update(f_dds);
760
761
762 //4.3 dtUp et dtDown
763
764 uint16_t Word_dt_up = rint(dtUp*fclk/24);
765 uint16_t Word_dt_down = rint(dtDown*fclk/24);
766
767 write_register(fd,0x08,Word_dt_down>>8&0xFF,Word_dt_down&0xFF,Word_dt_up>>8&0xFF,Word_dt_up&0xFF); // il y a 2 dt
768 Send_IO_Update(f_dds);
769
770 //5 Start of the ramp
771
772 write_register(fd,0x01,0x00,0x28,0x09,0x00); //rampe simple montante
773 //write_register(fd,0x01,0x00,0xAE,0x29,0x00); //rampe continue
774 Send_IO_Update(f_dds);
775
776 return EXIT_SUCCESS;
777 }
778
779 int ContinueAmplitudeSweep (int fd, int f_dds,double fclk,double dAUp, double dADown, double AMax,double AMin, double DeltaTimeUp, double DeltaTimeDown)
780 {
781
782
783 //calcul de N => N=DeltaF/df = DeltaT/dt
784
785 //1-Amax > Amin
786 double DeltaAmp = AMax-AMin;
787 if(AMax<AMin)
788 {
789 printf("AMax should be > AMin\n");
790 printf("Can't Start the ramp\n");
791 return -1;
792 }
793
794
795 //2- Calcul du nombre de points de la rampe
796 int NumberPointsUp = DeltaAmp/dAUp;
797 int NumberPointsDown = DeltaAmp/dADown;
798
799 //3- En déduire la valeur de dtUp et dtDown
800 double dtUp = DeltaTimeUp/NumberPointsUp;
801 double dtDown = DeltaTimeDown/NumberPointsDown;
802
803 double dtmin = 24/fclk;
804 double dtmax = dtmin*(pow(2.0,16.0)-1);
805
806 //Check Up
807 if(dtUp<dtmin)
808 {
809 printf("During of ramp UP should be > %f s\n",dtmin*NumberPointsUp);
810 printf("Can't start the ramp\n");
811 return -1;
812 }
813
814 if(dtUp>dtmax)
815 {
816 printf("During of ramp UP should be < %f\n",dtmax*NumberPointsUp);
817 printf("Can't start the ramp\n");
818 return -1;
819 }
820
821 //Check Down
822 if(dtDown<dtmin)
823 {
824 printf("During of ramp DOWN should be > %f s\n",dtmin*NumberPointsDown);
825 printf("Can't start the ramp\n");
826 return -1;
827 }
828
829 if(dtDown>dtmax)
830 {
831 printf("During of ramp DOWN should be < %f\n",dtmax*NumberPointsDown);
832 printf("Can't start the ramp\n");
833 return -1;
834 }
835
836
837
838 //check if DeltaAmp is enouth
839 double enouthUP=0.5*NumberPointsUp*100/4095;
840 double enouthDOWN=0.5*NumberPointsDown*100/4095;
841
842 int borneUP = rint(enouthUP);
843 int borneDown = rint(enouthDOWN);
844
845 if(DeltaAmp<=enouthUP)
846 {
847 printf("The amplitude difference should be > %d\n, decrease Number Points for positive slope",borneUP);
848 //return -1;
849 }
850
851 if(DeltaAmp<=enouthDOWN)
852 {
853 printf("The amplitude difference should be > %d\n, decrease Number Points for negative slope",borneDown);
854 //return -1;
855 }
856
857
858 //4 - Put word in register
859
860
861 //4.1 Limit register
862 uint32_t WordAmin = rint(AMin*4095/100);
863 uint32_t WordAmax= rint(AMax*4095/100);
864
865 write_register(fd,0x04,WordAmin>>24&0xFF,WordAmin>>16&0xFF,WordAmin>>8&0xFF,WordAmin&0xFF); //min => lim-(Amp ) = 0x800 = Amax/2
866 Send_IO_Update(f_dds);
867 write_register(fd,0x05,WordAmax>>24&0xFF,WordAmax>>16&0xFF,WordAmax>>8&0xFF,WordAmax&0xFF);//max => lim+(Amp) = 0xFFF= Amax0
868 Send_IO_Update(f_dds);
869
870
871 //4.2 dAUP et dADown
872
873 uint32_t Word_dA_up = rint(dAUp*4095/100);
874 uint32_t Word_dA_down = rint(dADown*4095/100);
875
876 write_register(fd,0x06,Word_dA_up>>24&0xFF,Word_dA_up>>16&0xFF,Word_dA_up>>8&0xFF,Word_dA_up&0xFF); //dA_up
877 Send_IO_Update(f_dds);
878 write_register(fd,0x07,Word_dA_down>>24&0xFF,Word_dA_down>>16&0xFF,Word_dA_down>>8&0xFF,Word_dA_down&0xFF);//dA_down
879 Send_IO_Update(f_dds);
880
881
882 //4.3 dtUp et dtDown
883
884 uint16_t Word_dt_up = rint(dtUp*fclk/24);
885 uint16_t Word_dt_down = rint(dtDown*fclk/24);
886
887 write_register(fd,0x08,Word_dt_down>>8&0xFF,Word_dt_down&0xFF,Word_dt_up>>8&0xFF,Word_dt_up&0xFF); // il y a 2 dt
888 Send_IO_Update(f_dds);
889
890 //5 Start of the ramp
891
892 //write_register(fd,0x01,0x00,0x28,0x09,0x00); //rampe simple montante
893 write_register(fd,0x01,0x00,0xAE,0x29,0x00); //rampe continue
894 Send_IO_Update(f_dds);
895
896 return EXIT_SUCCESS;
897 }
898
899
900 //3-rampe de ĥase
901
902 int PhaseSweep (int fd, int f_dds,double fclk,double dphiUp, double dphiDown, double PhiMax,double PhiMin, double DeltaTimeUp, double DeltaTimeDown)
903 {
904
905
906 //calcul de N => N=Deltaphi/dphi = DeltaT/dt
907
908 //1-phimax > phimin
909 double DeltaPhi = PhiMax-PhiMin;
910 if(PhiMax<PhiMin)
911 {
912 printf("PhiMax should be > PhiMin\n");
913 printf("Can't Start the ramp\n");
914 return -1;
915 }
916
917
918 //2- Calcul du nombre de points de la rampe
919 int NumberPointsUp = DeltaPhi/dphiUp;
920 int NumberPointsDown = DeltaPhi/dphiDown;
921
922 printf("NumberPointsUp = %d\n",NumberPointsUp);
923 printf("NumberPointsDown = %d\n",NumberPointsDown);
924
925 //3- En déduire la valeur de dtUp et dtDown
926 double dtUp = DeltaTimeUp/NumberPointsUp;
927 double dtDown = DeltaTimeDown/NumberPointsDown;
928
929 double dtmin = 24/fclk;
930 double dtmax = dtmin*(pow(2.0,16.0)-1);
931
932 //Check Up
933 if(dtUp<dtmin)
934 {
935 printf("During of ramp UP should be > %f s\n",dtmin*NumberPointsUp);
936 printf("Can't start the ramp\n");
937 return -1;
938 }
939
940 if(dtUp>dtmax)
941 {
942 printf("During of ramp UP should be < %f\n",dtmax*NumberPointsUp);
943 printf("Can't start the ramp\n");
944 return -1;
945 }
946
947 //Check Down
948 if(dtDown<dtmin)
949 {
950 printf("During of ramp DOWN should be > %f s\n",dtmin*NumberPointsDown);
951 printf("Can't start the ramp\n");
952 return -1;
953 }
954
955 if(dtDown>dtmax)
956 {
957 printf("During of ramp DOWN should be < %f\n",dtmax*NumberPointsDown);
958 printf("Can't start the ramp\n");
959 return -1;
960 }
961
962
963
964 //check if DeltaAmp is enouth
965 /*
966 double enouthUP=0.5*NumberPointsUp*100/4095;
967 double enouthDOWN=0.5*NumberPointsDown*100/4095;
968
969 int borneUP = rint(enouthUP);
970 int borneDown = rint(enouthDOWN);
971
972 if(DeltaAmp<=enouthUP)
973 {
974 printf("The amplitude difference should be > %d\n, decrease Number Points for positive slope",borneUP);
975 //return -1;
976 }
977
978 if(DeltaAmp<=enouthDOWN)
979 {
980 printf("The amplitude difference should be > %d\n, decrease Number Points for negative slope",borneDown);
981 //return -1;
982 }
983
984 */
985
986 //4 - Put word in register
987
988
989 //4.1 Limit register
990 uint32_t WordPhimin = rint(PhiMin*65535/360);
991 uint32_t WordPhimax= rint(PhiMax*65535/360);
992
993
994 printf("WordPhimin = %.32x\n",WordPhimin);
995 printf("WordPhimax = %.32x\n",WordPhimax);
996
997
998 write_register(fd,0x04,WordPhimin>>24&0xFF,WordPhimin>>16&0xFF,WordPhimin>>8&0xFF,WordPhimin&0xFF); //min => lim-(Amp ) = 0x800 = Amax/2
999 Send_IO_Update(f_dds);
1000 write_register(fd,0x05,WordPhimax>>24&0xFF,WordPhimax>>16&0xFF,WordPhimax>>8&0xFF,WordPhimax&0xFF);//max => lim+(Amp) = 0xFFF= Amax0
1001 Send_IO_Update(f_dds);
1002
1003
1004 //4.2 dphiUP et dphiDown
1005
1006 // uint32_t Word_dphi_up = rint(dphiUp*(pow(2.0,29.0)-1)/45);
1007 // uint32_t Word_dphi_down = rint(dphiDown*(pow(2.0,29.0)-1)/45);
1008 uint32_t Word_dphi_up = rint(dphiUp*(65535/360));
1009 uint32_t Word_dphi_down = rint(dphiDown*(65535/360));
1010
1011 printf("Word_dphi_up = %.32x\n",Word_dphi_up);
1012 printf("Word_dphi_down = %.32x\n",Word_dphi_down);
1013
1014 /*
1015 write_register(fd,0x06,Word_dphi_up>>16&0xFF,Word_dphi_up>>8&0xFF,Word_dphi_up>>0&0xFF,Word_dphi_up&0xFF); //dphi_up
1016 Send_IO_Update(f_dds);
1017 write_register(fd,0x07,Word_dphi_down>>16&0xFF,Word_dphi_down>>8&0xFF,Word_dphi_down>>0&0xFF,Word_dphi_down&0xFF);//dphi_down
1018 Send_IO_Update(f_dds);
1019
1020 */
1021
1022
1023 write_register(fd,0x06,Word_dphi_up>>24&0xFF,Word_dphi_up>>16&0xFF,Word_dphi_up>>8&0xFF,Word_dphi_up&0xFF); //dA_up
1024 Send_IO_Update(f_dds);
1025 write_register(fd,0x07,Word_dphi_down>>24&0xFF,Word_dphi_down>>16&0xFF,Word_dphi_down>>8&0xFF,Word_dphi_down&0xFF);//dA_down
1026 Send_IO_Update(f_dds);
1027
1028
1029 //4.3 dtUp et dtDown
1030
1031 uint16_t Word_dt_up = rint(dtUp*fclk/24);
1032 uint16_t Word_dt_down = rint(dtDown*fclk/24);
1033
1034 write_register(fd,0x08,Word_dt_down>>8&0xFF,Word_dt_down&0xFF,Word_dt_up>>8&0xFF,Word_dt_up&0xFF); // il y a 2 dt
1035 Send_IO_Update(f_dds);
1036
1037 //5 Start of the ramp
1038
1039 write_register(fd,0x01,0x00,0x18,0x09,0x00); //rampe simple montante
1040 Send_IO_Update(f_dds);
1041
1042 return EXIT_SUCCESS;
1043 }
1044
1045 int ContinuePhaseSweep (int fd, int f_dds,double fclk,double dphiUp, double dphiDown, double PhiMax,double PhiMin, double DeltaTimeUp, double DeltaTimeDown)
1046 {
1047
1048
1049 //calcul de N => N=Deltaphi/dphi = DeltaT/dt
1050
1051 //1-phimax > phimin
1052 double DeltaPhi = PhiMax-PhiMin;
1053 if(PhiMax<PhiMin)
1054 {
1055 printf("PhiMax should be > PhiMin\n");
1056 printf("Can't Start the ramp\n");
1057 return -1;
1058 }
1059
1060
1061 //2- Calcul du nombre de points de la rampe
1062 int NumberPointsUp = DeltaPhi/dphiUp;
1063 int NumberPointsDown = DeltaPhi/dphiDown;
1064
1065 printf("NumberPointsUp = %d\n",NumberPointsUp);
1066 printf("NumberPointsDown = %d\n",NumberPointsDown);
1067
1068 //3- En déduire la valeur de dtUp et dtDown
1069 double dtUp = DeltaTimeUp/NumberPointsUp;
1070 double dtDown = DeltaTimeDown/NumberPointsDown;
1071
1072 double dtmin = 24/fclk;
1073 double dtmax = dtmin*(pow(2.0,16.0)-1);
1074
1075 //Check Up
1076 if(dtUp<dtmin)
1077 {
1078 printf("During of ramp UP should be > %f s\n",dtmin*NumberPointsUp);
1079 printf("Can't start the ramp\n");
1080 return -1;
1081 }
1082
1083 if(dtUp>dtmax)
1084 {
1085 printf("During of ramp UP should be < %f\n",dtmax*NumberPointsUp);
1086 printf("Can't start the ramp\n");
1087 return -1;
1088 }
1089
1090 //Check Down
1091 if(dtDown<dtmin)
1092 {
1093 printf("During of ramp DOWN should be > %f s\n",dtmin*NumberPointsDown);
1094 printf("Can't start the ramp\n");
1095 return -1;
1096 }
1097
1098 if(dtDown>dtmax)
1099 {
1100 printf("During of ramp DOWN should be < %f\n",dtmax*NumberPointsDown);
1101 printf("Can't start the ramp\n");
1102 return -1;
1103 }
1104
1105
1106
1107 //check if DeltaAmp is enouth
1108 /*
1109 double enouthUP=0.5*NumberPointsUp*100/4095;
1110 double enouthDOWN=0.5*NumberPointsDown*100/4095;
1111
1112 int borneUP = rint(enouthUP);
1113 int borneDown = rint(enouthDOWN);
1114
1115 if(DeltaAmp<=enouthUP)
1116 {
1117 printf("The amplitude difference should be > %d\n, decrease Number Points for positive slope",borneUP);
1118 //return -1;
1119 }
1120
1121 if(DeltaAmp<=enouthDOWN)
1122 {
1123 printf("The amplitude difference should be > %d\n, decrease Number Points for negative slope",borneDown);
1124 //return -1;
1125 }
1126
1127 */
1128
1129 //4 - Put word in register
1130
1131
1132 //4.1 Limit register
1133 uint32_t WordPhimin = rint(PhiMin*65535/360);
1134 uint32_t WordPhimax= rint(PhiMax*65535/360);
1135
1136
1137 printf("WordPhimin = %.32x\n",WordPhimin);
1138 printf("WordPhimax = %.32x\n",WordPhimax);
1139
1140
1141 write_register(fd,0x04,WordPhimin>>24&0xFF,WordPhimin>>16&0xFF,WordPhimin>>8&0xFF,WordPhimin&0xFF); //min => lim-(Amp ) = 0x800 = Amax/2
1142 Send_IO_Update(f_dds);
1143 write_register(fd,0x05,WordPhimax>>24&0xFF,WordPhimax>>16&0xFF,WordPhimax>>8&0xFF,WordPhimax&0xFF);//max => lim+(Amp) = 0xFFF= Amax0
1144 Send_IO_Update(f_dds);
1145
1146
1147 //4.2 dphiUP et dphiDown
1148
1149 // uint32_t Word_dphi_up = rint(dphiUp*(pow(2.0,29.0)-1)/45);
1150 // uint32_t Word_dphi_down = rint(dphiDown*(pow(2.0,29.0)-1)/45);
1151 uint32_t Word_dphi_up = rint(dphiUp*(65535/360));
1152 uint32_t Word_dphi_down = rint(dphiDown*(65535/360));
1153
1154 printf("Word_dphi_up = %.32x\n",Word_dphi_up);
1155 printf("Word_dphi_down = %.32x\n",Word_dphi_down);
1156
1157 /*
1158 write_register(fd,0x06,Word_dphi_up>>16&0xFF,Word_dphi_up>>8&0xFF,Word_dphi_up>>0&0xFF,Word_dphi_up&0xFF); //dphi_up
1159 Send_IO_Update(f_dds);
1160 write_register(fd,0x07,Word_dphi_down>>16&0xFF,Word_dphi_down>>8&0xFF,Word_dphi_down>>0&0xFF,Word_dphi_down&0xFF);//dphi_down
1161 Send_IO_Update(f_dds);
1162
1163 */
1164
1165
1166 write_register(fd,0x06,Word_dphi_up>>24&0xFF,Word_dphi_up>>16&0xFF,Word_dphi_up>>8&0xFF,Word_dphi_up&0xFF); //dA_up
1167 Send_IO_Update(f_dds);
1168 write_register(fd,0x07,Word_dphi_down>>24&0xFF,Word_dphi_down>>16&0xFF,Word_dphi_down>>8&0xFF,Word_dphi_down&0xFF);//dA_down
1169 Send_IO_Update(f_dds);
1170
1171
1172 //4.3 dtUp et dtDown
1173
1174 uint16_t Word_dt_up = rint(dtUp*fclk/24);
1175 uint16_t Word_dt_down = rint(dtDown*fclk/24);
1176
1177 write_register(fd,0x08,Word_dt_down>>8&0xFF,Word_dt_down&0xFF,Word_dt_up>>8&0xFF,Word_dt_up&0xFF); // il y a 2 dt
1178 Send_IO_Update(f_dds);
1179
1180 //5 Start of the ramp
1181
1182 write_register(fd,0x01,0x00,0x9E,0x29,0x00); //rampe simple montante
1183 Send_IO_Update(f_dds);
1184
1185 return EXIT_SUCCESS;
1186 }
1187
1188
1189 //-------------------------FIN FONCTIONS RAMPES HARDWARES
1190
1191 //--------------------------FONCTIONS RAMPES SOFTWARES
1192
1193 int RampAmpFromSoft(int fd, int f_dds,double DeltaTimeUp,double AIni, double AFin)
1194 {
1195
1196 //On va plutôt fixé dA sinon trop de contrainte : => Besoin d'allocation dynamique
1197 double dA=0.024;//en %
1198 double DeltaAmp = AIni-AFin;
1199 double AbsDeltaAmp = 0;
1200 if(DeltaAmp>=0)
1201 {
1202 AbsDeltaAmp = DeltaAmp;
1203 }
1204 else
1205 {
1206 AbsDeltaAmp = -1*DeltaAmp;
1207 }
1208
1209 int NumberPoints = AbsDeltaAmp/dA;
1210
1211 double dt=DeltaTimeUp/NumberPoints;
1212
1213 printf("Taille du tableau = %d\n", NumberPoints);
1214 printf("dt = %f s\n",dt);
1215
1216
1217 uint16_t* ArrayWordAmp= NULL;
1218 ArrayWordAmp = malloc(NumberPoints*sizeof(uint16_t));
1219
1220 //extraction de la phase initiale et de l'amplitude
1221
1222 uint32_t ReadPhaseAmpWord = 0x00000000;
1223 read_register(fd, 0x0c,&ReadPhaseAmpWord);
1224 printf("ReadPhaseAmpWord = %.8x \n", ReadPhaseAmpWord);
1225
1226 uint16_t WordPhaseInitiale = ReadPhaseAmpWord&0xFFFF;
1227 printf("Phase = %.4x \n", WordPhaseInitiale);
1228
1229
1230 //
1231 for(int i=0;i<NumberPoints;i++)
1232 {
1233 ArrayWordAmp[i]=rint((AIni+i*dA)*4096/100);
1234 // printf("ArrayWordAmp[%d] = %.16x\n", i, ArrayWordAmp[i]);
1235 }
1236 free(ArrayWordAmp);
1237
1238 printf("Array calculated\n");
1239
1240 printf("Start of the ramp\n");
1241 for(int i=0;i<NumberPoints;i++)
1242 {
1243 write_register(fd,0x0c,ArrayWordAmp[i]>>8&0xFF,ArrayWordAmp[i]&0xFF,WordPhaseInitiale>>8&0xFF,WordPhaseInitiale&0xFF);
1244 Send_IO_Update(f_dds);
1245 usleep(1000000*dt);
1246 }
1247
1248
1249
1250 /*
1251 uint16_t PhaseWord = 0x0000;
1252 uint16_t AmpWord = 0x0000;
1253 uint32_t ReadPhaseAmpWord=0x00000000;
1254
1255 AmpWord = rint(amplitude*(pow(2.0,12.0)-1)/100);
1256
1257 read_register(fd,0x0c,&ReadPhaseAmpWord);
1258
1259 PhaseWord = ReadPhaseAmpWord&0xFFFF;
1260
1261 write_register(fd,0x0c,AmpWord>>8&0xFF,AmpWord&0xFF,PhaseWord>>8&0xFF,PhaseWord&0xFF);
1262
1263 */
1264
1265
1266
1267 /*
1268
1269 //pas de durée dtUp et dtDown fixée au minimum à 1 ms
1270 if(DeltaTimeUp < 4.095)
1271 {
1272 printf("Use hardware fonctions for the ramp\n");
1273 }
1274
1275
1276 //
1277 double DeltaAmp = AMax-AMin;
1278 double dA = DeltaAmp/TAILLETAB;
1279 printf("dA=%f\n",dA);
1280
1281
1282 if(dA<0.024)
1283 {
1284 printf("Amplitude step should be > 0.024 pourcent\n");
1285 printf("Impossible to start a ramp of amplitude\n");
1286 return -1;
1287 }
1288
1289 //extraction de la phase initiale et de l'amplitude
1290
1291 uint32_t ReadPhaseAmpWord = 0x00000000;
1292 read_register(fd, 0x0c,&ReadPhaseAmpWord);
1293 printf("ReadPhaseAmpWord = %.8x \n", ReadPhaseAmpWord);
1294
1295 uint16_t WordPhaseInitiale = ReadPhaseAmpWord&0xFFFF;
1296 printf("Phase = %.4x \n", WordPhaseInitiale);
1297
1298 uint16_t WordAmpInitiale = ReadPhaseAmpWord>>16&0xFFFF;
1299 printf("Amp = %.4x \n", WordAmpInitiale);
1300
1301 uint16_t Word_dA = rint(dA*4095/100);
1302
1303 uint32_t ArrayWordAmp[TAILLETAB];
1304
1305 for(int i=0;i<TAILLETAB;i++)
1306 {
1307 ArrayWordAmp[i]=WordAmpInitiale+i*Word_dA;
1308 // printf("ArrayWordAmp[%d] = %.16x\n", i, ArrayWordAmp[i]);
1309 }
1310
1311 */
1312
1313 return EXIT_SUCCESS;
1314 }
1315
1316 int RampPhaseFromSoft(int fd, int f_dds,double DeltaTimeUp,double PhiIni, double PhiFin)
1317 {
1318
1319 //On va plutôt fixé dphi : => Besoin d'allocation dynamique
1320 double dphi=0.06;//en °
1321 double DeltaPhi = PhiIni-PhiFin;
1322 double AbsDeltaPhi = 0;
1323 if(DeltaPhi>=0)
1324 {
1325 AbsDeltaPhi = DeltaPhi;
1326 }
1327 else
1328 {
1329 AbsDeltaPhi = -1*DeltaPhi;
1330 }
1331
1332 int NumberPoints = AbsDeltaPhi/dphi;
1333
1334 double dt=DeltaTimeUp/NumberPoints;
1335
1336 printf("Taille du tableau = %d\n", NumberPoints);
1337 printf("dt = %f s\n",dt);
1338
1339
1340 uint16_t* ArrayWordPhi= NULL;
1341 ArrayWordPhi = malloc(NumberPoints*sizeof(uint16_t));
1342
1343 //extraction de l'amplitude
1344
1345 uint32_t ReadPhaseAmpWord = 0x00000000;
1346 read_register(fd, 0x0c,&ReadPhaseAmpWord);
1347 printf("ReadPhaseAmpWord = %.8x \n", ReadPhaseAmpWord);
1348
1349 uint16_t WordAmpInitiale = ReadPhaseAmpWord>>16&0xFFFF;
1350 printf("Amp = %.4x \n", WordAmpInitiale);
1351
1352
1353 for(int i=0;i<NumberPoints;i++)
1354 {
1355 ArrayWordPhi[i]=rint((PhiIni+i*dphi)*65536/360);
1356 }
1357 free(ArrayWordPhi);
1358
1359 printf("Array calculated\n");
1360
1361 printf("Start of the ramp\n");
1362 for(int i=0;i<NumberPoints;i++)
1363 {
1364 write_register(fd,0x0c,WordAmpInitiale>>8&0xFF,WordAmpInitiale&0xFF,ArrayWordPhi[i]>>8&0xFF,ArrayWordPhi[i]&0xFF);
1365 Send_IO_Update(f_dds);
1366 usleep(1000000*dt);
1367 }
1368
1369
#include <stdlib.h> 1 1 #include <stdlib.h>
#include <stdio.h> 2 2 #include <stdio.h>
#include <unistd.h> 3 3 #include <unistd.h>
#include <math.h> 4 4 #include <math.h>
#include "ddsFreq.h" 5 5 #include "ddsFreq.h"
#include "ad9915.h" 6 6 #include "ad9915.h"
7 7
//#define debug 8 8 //#define debug
long double pgcd ( long double m , long double n) 9 9 long double pgcd ( long double m , long double n)
{ 10 10 {
if (fmod(m,n) ==0) 11 11 if (fmod(m,n) ==0)
return n; 12 12 return n;
return pgcd(n,fmod(m,n)); 13 13 return pgcd(n,fmod(m,n));
} 14 14 }
15 15
int setddsFreq(int fd, int f_dds, long double f0, long double fs) 16 16 int setddsFreq(int fd, int f_dds, long double f0, long double fs)
{ 17 17 {
18 18
#ifdef debug 19 19 #ifdef debug
printf("f0=%Le\tfs=%Le\n",f0,fs); 20 20 printf("f0=%Le\tfs=%Le\n",f0,fs);
#endif 21 21 #endif
22 22
if (fs<0 || f0<0) return EXIT_FAILURE; 23 23 if (fs<0 || f0<0) return EXIT_FAILURE;
24 24
struct frac f1; 25 25 struct frac f1;
26 26
f1.numerator = f0; 27 27 f1.numerator = f0;
f1.denominator = fs; 28 28 f1.denominator = fs;
29 29
long double d1 = pgcd (f1.numerator , f1.denominator ); 30 30 long double d1 = pgcd (f1.numerator , f1.denominator );
//printf(" d1 = %24.16Lf\n",d1); 31 31 //printf(" d1 = %24.16Lf\n",d1);
32 32
long double M = f0 / d1 ; 33 33 long double M = f0 / d1 ;
long double N = fs / d1 ; 34 34 long double N = fs / d1 ;
//printf(" M = f0 / d1 = %24.16Lf\n",M); 35 35 //printf(" M = f0 / d1 = %24.16Lf\n",M);
//printf(" N = fs / d1 = %24.16Lf\n",N); 36 36 //printf(" N = fs / d1 = %24.16Lf\n",N);
37 37
38 38
long double ftw = M * scalbn(1.0,32); 39 39 long double ftw = M * scalbn(1.0,32);
ftw /= N; 40 40 ftw /= N;
//printf("ftw = %24.16Lf\n",ftw); 41 41 //printf("ftw = %24.16Lf\n",ftw);
42 42
43 43
//printf("X=floor(ftw) = %24.16Lf\n",(long double)floor(ftw)); 44 44 //printf("X=floor(ftw) = %24.16Lf\n",(long double)floor(ftw));
45 45
long double Y = M * scalbn(1.0,32); 46 46 long double Y = M * scalbn(1.0,32);
Y -= N * floor(ftw); 47 47 Y -= N * floor(ftw);
//printf("Y=M*2**32-X*N = %24.16Lf\n",Y); 48 48 //printf("Y=M*2**32-X*N = %24.16Lf\n",Y);
49 49
long double d2 = pgcd (Y,N); 50 50 long double d2 = pgcd (Y,N);
//printf(" d2 = %24.16Lf\n",d2); 51 51 //printf(" d2 = %24.16Lf\n",d2);
52 52
long double A = Y / d2; 53 53 long double A = Y / d2;
long double B = N / d2; 54 54 long double B = N / d2;
//printf("A = Y/d2 = %24.16Lf\n",A); 55 55 //printf("A = Y/d2 = %24.16Lf\n",A);
//printf("B = N/d2 = %24.16Lf\n",B); 56 56 //printf("B = N/d2 = %24.16Lf\n",B);
57 57
//printf("f0 = (M/N) * fclk= %24.32Lf\n",fs*M/N); 58 58 //printf("f0 = (M/N) * fclk= %24.32Lf\n",fs*M/N);
//printf("f0 = (X+A/B)2**32= %24.32Lf\n",fs*(floor(ftw)+A/B)/scalbn(1.0,32)); 59 59 //printf("f0 = (X+A/B)2**32= %24.32Lf\n",fs*(floor(ftw)+A/B)/scalbn(1.0,32));
60 60
#ifdef debug 61 61 #ifdef debug
printf("\n so finally the programmed registers are :\n" 62 62 printf("\n so finally the programmed registers are :\n"
"ftw= 0x%08x A= 0x%08x B= 0x%08x\n" 63 63 "ftw= 0x%08x A= 0x%08x B= 0x%08x\n"
"",(unsigned int)ftw,(unsigned int)A,(unsigned int)B); 64 64 "",(unsigned int)ftw,(unsigned int)A,(unsigned int)B);
printf("uint ftw = %d\n",(unsigned int)ftw); 65 65 printf("uint ftw = %d\n",(unsigned int)ftw);
66 66
printf("\n 0x%08x 0x%08x 0x%08x \n\n",(unsigned int)ftw,(unsigned int)A,(unsigned int)B); 67 67 printf("\n 0x%08x 0x%08x 0x%08x \n\n",(unsigned int)ftw,(unsigned int)A,(unsigned int)B);
//setFreqMM(fd,f_dds,(unsigned int)ftw, (unsigned int)A, (unsigned int)B); 68 68 //setFreqMM(fd,f_dds,(unsigned int)ftw, (unsigned int)A, (unsigned int)B);
#endif 69 69 #endif
setFreqMM(fd,f_dds,(unsigned int)ftw, 2*(unsigned int)B, 2*(unsigned int)A); //??why *2?? 70 70 setFreqMM(fd,f_dds,(unsigned int)ftw, 2*(unsigned int)B, 2*(unsigned int)A); //??why *2??
return EXIT_SUCCESS; 71 71 return EXIT_SUCCESS;
} 72 72 }
int ddsFreq(int fd, int f_dds) 73 73 int ddsFreq(int fd, int f_dds)
{ 74 74 {
//checkSize(); 75 75 checkSize();
long double f0,fs; 76 76 long double f0,fs;
//printf("long = %lu\n",sizeof (long)); 77 77 //printf("long = %lu\n",sizeof (long));
//printf("float = %lu\n",sizeof (float)); 78 78 //printf("float = %lu\n",sizeof (float));
//printf("double = %lu\n",sizeof (double)); 79 79 //printf("double = %lu\n",sizeof (double));
//printf("long double = %lu\n",sizeof (long double)); 80 80 //printf("long double = %lu\n",sizeof (long double));
81 81
printf("\nDesired output Frequency (MHz) : "); 82 82 printf("\nDesired output Frequency (MHz) : ");
scanf("%Lf",&f0); 83 83 scanf("%Lf",&f0);
printf("DDS frequency Clock (MHz) : "); 84 84 printf("DDS frequency Clock (MHz) : ");
scanf("%Lf",&fs); 85 85 scanf("%Lf",&fs);
86 86
//long double f0=atof(argv[1]); //desired output freq 87 87 //long double f0=atof(argv[1]); //desired output freq
//long double fs=atof(argv[2]); //dds_clk 88 88 //long double fs=atof(argv[2]); //dds_clk
//int toto = (int)strtol(argv[3],NULL,0); 89 89 //int toto = (int)strtol(argv[3],NULL,0);
//printf("toto=%d\t%x\n",toto,toto); 90 90 //printf("toto=%d\t%x\n",toto,toto);
91 91
if (fs<0 || f0<0) return EXIT_FAILURE; 92 92 if (fs<0 || f0<0) return EXIT_FAILURE;
93 93
struct frac f1; 94 94 struct frac f1;
95 95
f1.numerator = f0; 96 96 f1.numerator = f0;
f1.denominator = fs; 97 97 f1.denominator = fs;
98 98
long double d1 = pgcd (f1.numerator , f1.denominator ); 99 99 long double d1 = pgcd (f1.numerator , f1.denominator );
printf(" d1 = %24.16Lf\n",d1); 100 100 printf(" d1 = %24.16Lf\n",d1);
101 101
long double M = f0 / d1 ; 102 102 long double M = f0 / d1 ;
long double N = fs / d1 ; 103 103 long double N = fs / d1 ;
printf(" M = f0 / d1 = %24.16Lf\n",M); 104 104 printf(" M = f0 / d1 = %24.16Lf\n",M);
printf(" N = fs / d1 = %24.16Lf\n",N); 105 105 printf(" N = fs / d1 = %24.16Lf\n",N);