ad9915.c
5.79 KB
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
/*
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;
}