-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpwm_lut.c
270 lines (252 loc) · 6.61 KB
/
pwm_lut.c
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* pwm_lut.c
*
* Created on: 15 May 2014
* Author: Jonathan
*
* Generates a N x M lookup table for a CMOS
* totem-pole output driver operating as a PWM DAC.
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <stdint.h>
#include <getopt.h>
#include <string.h>
#include <sys/types.h>
const char usage[] = {
"pwm_lut: usage\n"
"Creates a CSV table of computed output voltages of a CMOS n-bit PWM DAC\n"
"--an \t N-MOSFET vds^2 curve-fit term\n"
"--bn \t N-MOSFET vds curve-fit term\n"
"--ap \t P-MOSFET vds^2 curve-fit term\n"
"--bp \t P-MOSFET vds curve-fit term\n"
"--ra \t R1 resistor value\n"
"--rb \t R2 resistor value\n"
"--vdac \t DAC driving voltage\n"
"-x n \t Number of quantization levels of the PWM output duty\n"
"-y n \t Number of quantization levels of the output voltage Vc\n"
"--file \t Output filename\n"
"--csv \t Generate CSV data (default to generate C fragment)\n",
};
static const struct option longopts[] = {
{ "an", required_argument, 0, 1 },
{ "bn", required_argument, 0, 2 },
{ "ap", required_argument, 0, 3 },
{ "bp", required_argument, 0, 4 },
{ "ra", required_argument, 0, 5 },
{ "rb", required_argument, 0, 6 },
{ "vdac", required_argument, 0, 7 },
{ "file", required_argument, 0, 8 },
{ "csv", no_argument, 0, 9 },
{ "fudge", required_argument, 0, 10 },
{ 0, 0, 0, 0 },
};
struct dac_params {
float an;
float bn;
float ap;
float bp;
float ra;
float rb;
float vdac;
float max_vc;
float fudge;
int x;
int y;
int csv_out;
} dac_params;
float converge (float vc, float vdac, float r1, float a, float b, int leg)
{
int i = 0;
float vds = 0.0f;
float vds1, vds2;
float ids = 0.0f;
/* Iteratively solve the intersection of the VDS/IDS load-line with the
* -1/R1 slope load-line. */
for (i = 0; i < 10; i++) {
/* Find IDS through R1 */
if (leg)
ids = (vc - vds) / r1;
else
ids = (vdac - vc - vds) / r1;
if (ids < 0.0f)
break;
/* Solve quadratic fit curve to find VDS */
vds1 = (0.0f - b) + sqrt(b*b - 4.0f * a * (0.0f - ids));
vds1 = vds1 / (2.0f * a);
vds2 = (0.0f - b) - sqrt(b*b - 4.0f * a * (0.0f - ids));
vds2 = vds1 / (2.0f * a);
if (vds1 > 0) {
vds = vds1;
} else if (vds2 > 0) {
vds = vds2;
}
}
return vds;
}
float ** make_array (int x, int y)
{
int i = 0;
float ** ret = malloc(x * sizeof(float *));
if (!ret)
return NULL;
for (i = 0; i < x; i++) {
ret[i] = malloc(y * sizeof(float));
if (!ret[i])
return NULL;
}
return ret;
}
void write_csv (int x, int y, float **array, FILE *fp)
{
int i = 0, j = 0;
for (i = 0; i < x; i++) {
for (j = 0; j < y; j ++) {
fprintf(fp, "%.6e, ", array[i][j]);
}
fprintf(fp, "\n");
}
}
void write_c (int x, int y, float **array, FILE *fp)
{
int i = 0, j = 0;
fprintf(fp, "/**\n"
" * CMOS DAC duty-cycle linearisation LUT\n"
" * Computed with the following parameters:\n"
" * PWM range=%d Vc nlevels=%d\n"
" * PMOS k'n*(Vgs-Vt)=%f -0.5*k'n=%f\n"
" * NMOS k'n*(Vgs-Vt)=%f -0.5*k'n=%f\n"
" * Vdac=%f R1=%f R2=%f\n"
" **/\n", dac_params.x, dac_params.y,
dac_params.ap, dac_params.bp, dac_params.an, dac_params.bn,
dac_params.vdac, dac_params.ra, dac_params.rb);
fprintf(fp, "#define LUT_V0_SIZE_POW2 %d\n#define LUT_VC_SIZE_POW2 %d\n", (unsigned int) log2(x), (unsigned int) log2(y));
fprintf(fp, "const int16_t dac_lut[%d][%d] = {\n", x, y);
for (i = 0; i < x; i++) {
fprintf(fp, "\t{");
for (j = 0; j < y; j++) {
/* Convert to duty-referenced */
float out_scaled = 4294967294.0f * (array[i][j] / dac_params.vdac);
/* 16-bit precision is enough for anybody */
uint16_t out_quantised = ((((uint32_t) out_scaled) & 0xFFFF0000) + ((((uint32_t) out_scaled) & 0x00008000) << 1)) >> 16;
fprintf(fp, " 0x%04x, ", out_quantised);
}
fprintf(fp, "},\n");
}
fprintf(fp, "};\n");
}
float calc_max_vc (float ra, float rb, float vdac)
{
return (rb / (ra + rb)) * vdac;
}
int main (int argc, char **argv) {
int index = 0;
int i, j;
float **array;
char *filename = "out.csv";
FILE *outfile = NULL;
dac_params.csv_out = 0;
opterr = 0;
i = getopt_long(argc, argv, "x:y:", longopts, &index);
while (i != -1) {
switch(i) {
case 'x':
dac_params.x = atoi(optarg);
break;
case 'y':
dac_params.y = atoi(optarg);
break;
case 0:
break;
case 1:
dac_params.an = atof(optarg);
break;
case 2:
dac_params.bn = atof(optarg);
break;
case 3:
dac_params.ap = atof(optarg);
break;
case 4:
dac_params.bp = atof(optarg);
break;
case 5:
dac_params.ra = atof(optarg);
break;
case 6:
dac_params.rb = atof(optarg);
break;
case 7:
dac_params.vdac = atof(optarg);
break;
case 8:
filename = optarg;
break;
case 9:
dac_params.csv_out = 1;
break;
case 10:
dac_params.fudge = atof(optarg);
break;
case '?':
printf("option=%d\n", optopt);
printf(usage);
exit(-1);
break;
case ':':
printf("Error: Option %s requires an argument.\n", argv[optopt]);
printf(usage);
exit(-1);
break;
default:
printf("wat\n");
break;
}
i = getopt_long(argc, argv, "x:y:", longopts, &index);
}
if(opterr) {
printf("Invalid argument\n");
printf(usage);
exit(-1);
}
printf("Parms:\n"
"an=%f bn=%f ap=%f bp=%f\n"
"vdac=%f ra=%f rb=%f\n", dac_params.an, dac_params.bn, dac_params.ap, dac_params.bp,
dac_params.vdac, dac_params.ra, dac_params.rb);
array = make_array(dac_params.x, dac_params.y);
if(!array) {
printf("Error: failed to allocate memory for LUT\n");
exit(-1);
}
outfile = fopen(filename, "w");
if(!outfile) {
printf("Error: Cannot open output file '%s' for writing.\n", filename);
free(array);
exit(-1);
}
/* For each duty, calculate Vo given Vc. Note: Vc is the voltage across Rb. */
dac_params.max_vc = calc_max_vc(dac_params.ra, dac_params.rb, dac_params.vdac);
for (i = 0; i < dac_params.x; i++) {
for (j = 0; j < dac_params.y; j++) {
float vn, vp, vc;
vc = dac_params.max_vc * ((float) j / ((float) dac_params.y - 1.0f));
vn = converge (vc, dac_params.vdac, dac_params.ra, dac_params.an, dac_params.bn, 1);
vp = converge (vc, dac_params.vdac, dac_params.ra, dac_params.ap, dac_params.bp, 0);
//printf ("vn=%f vp=%f i=%d j=%d\n", vn, vp, i, j);
float d = ((float) i / ((float) dac_params.x - 1.0f));
array[i][j] = (d * (dac_params.vdac - vp) + (1.0f - d) * vn) + dac_params.fudge;
//array[i][j] += array[i][j] * dac_params.fudge
}
}
if (dac_params.csv_out) {
write_csv(dac_params.x, dac_params.y, array, outfile);
} else {
/* Write LUT as a set of hex digits suitable for array definition */
write_c(dac_params.x, dac_params.y, array, outfile);
}
fprintf(outfile, "\n");
fclose(outfile);
return 0;
}