-
Notifications
You must be signed in to change notification settings - Fork 31
/
nuttx_PWM.c
260 lines (208 loc) · 6.04 KB
/
nuttx_PWM.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
/*
COPYRIGHT (C) 2009 Roberto Bucher (roberto.bucher@supsi.ch)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include <pyblock.h>
#include <commonFun.h>
#include <nuttx/config.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <nuttx/timers/pwm.h>
#define RANGE 65536
/****************************************************************************
* Name: init
*
* Description:
* Open the device and set characteristics of PWM.
*
****************************************************************************/
static void init(python_block *block)
{
double * realPar = block->realPar;
int * intPar = block->intPar;
struct pwm_info_s info;
int ret;
#ifdef CONFIG_PWM_MULTICHAN
int fd = intPar[block->nin];
#else
int fd = intPar[1];
#endif
if (fd == 0)
{
fd = open(block->str, O_RDONLY);
if (fd < 0)
{
fprintf(stderr, "Error opening device: %s\n", block->str);
exit(1);
}
#ifdef CONFIG_PWM_MULTICHAN
/* Set all channels initially to zero */
for (int i = 0; i < CONFIG_PWM_NCHANNELS; i++)
{
info.channels[i].channel = 0;
info.channels[i].duty = 0;
#if defined(PWM_CPOL_HIGH) && defined(PWM_DCPOL_LOW)
info.channels[i].cpol = PWM_CPOL_HIGH;
info.channels[i].dcpol = PWM_DCPOL_LOW;
#endif
}
/* Add channel numbr to first block->nin used channels */
for (int i = 0; i < block->nin; i++)
{
info.channels[i].channel = intPar[i];
}
/* End the channel list with -1 if there is a free channel */
if (block->nin < CONFIG_PWM_NCHANNELS)
{
info.channels[block->nin].channel = -1;
}
#else
info.duty = 0;
#endif
}
info.frequency = realPar[0];
/* Set PWM characteristic and start the output */
ret = ioctl(fd, PWMIOC_SETCHARACTERISTICS,
(unsigned long)((uintptr_t) &info));
if (ret < 0)
{
fprintf(stderr, "ioctl(PWMIOC_SETCHARACTERISTICS) failed\n");
close(fd);
exit(1);
}
ret = ioctl(fd, PWMIOC_START, 0);
if (ret < 0)
{
fprintf(stderr,"ioctl(PWMIOC_START) failed\n");
close(fd);
exit(1);
}
#ifdef CONFIG_PWM_MULTICHAN
intPar[block->nin] = fd;
#else
intPar[1] = fd;
#endif
}
/****************************************************************************
* Name: inout
*
* Description:
* Compute and set PWM duty cycle based on input value.
*
****************************************************************************/
static void inout(python_block *block)
{
double * realPar = block->realPar;
int * intPar = block->intPar;
struct pwm_info_s info;
double *val;
int ret;
memset(&info, 0, sizeof(info));
info.frequency = realPar[0];
#ifdef CONFIG_PWM_MULTICHAN
/* Set all channels initially to zero */
for (int i = 0; i < CONFIG_PWM_NCHANNELS; i++)
{
info.channels[i].channel = 0;
info.channels[i].duty = 0;
#if defined(PWM_CPOL_HIGH) && defined(PWM_DCPOL_LOW)
info.channels[i].cpol = PWM_CPOL_HIGH;
info.channels[i].dcpol = PWM_DCPOL_LOW;
#endif
}
if (block->nin < CONFIG_PWM_NCHANNELS)
{
info.channels[block->nin].channel = -1;
}
int fd = intPar[block->nin];
/* Set output for used channels */
for (int i = 0; i < block->nin; i++)
{
info.channels[i].channel = intPar[i];
/* Get value from the input and saturate it */
val = (double *) block->u[i];
if (val[0]>realPar[2]) val[0] = realPar[2];
if (val[0]<realPar[1]) val[0] = realPar[1];
double value = mapD2wD(val[0], realPar[1], realPar[2]);
/* Save duty cycle value */
info.channels[i].duty = (uint32_t) (value*RANGE);
}
#else
int fd = intPar[1];
/* Get value from the input and saturate it */
val = (double *) block->u[0];
if (val[0]>realPar[2]) val[0] = realPar[2];
if (val[0]<realPar[1]) val[0] = realPar[1];
double value = mapD2wD(val[0], realPar[1], realPar[2]);
/* Save duty cycle value */
info.duty = (uint32_t) (value*RANGE);
#endif
/* Update PWM characteristics */
ret = ioctl(fd, PWMIOC_SETCHARACTERISTICS, (unsigned long)((uintptr_t)&info));
if (ret < 0)
{
fprintf(stderr,"ioctl(PWMIOC_SETCHARACTERISTICS) failed: %d\n",
errno);
close(fd);
exit(1);
}
}
/****************************************************************************
* Name: end
*
* Description:
* Stop PWM pulse and close the device.
*
****************************************************************************/
static void end(python_block *block)
{
int * intPar = block->intPar;
#ifdef CONFIG_PWM_MULTICHAN
int fd = intPar[block->nin];
#else
int fd = intPar[1];
#endif
/* Stop PWM output */
int ret = ioctl(fd, PWMIOC_STOP, 0);
if (ret < 0)
{
fprintf(stderr, "ioctl(PWMIOC_STOP) failed: %d\n", errno);
close(fd);
exit(1);
}
close(fd);
}
/****************************************************************************
* Name: nuttx_PWM
*
* Description:
* Call needed function based on input flag.
*
****************************************************************************/
void nuttx_PWM(int flag, python_block *block)
{
if (flag==CG_OUT){ /* get input */
inout(block);
}
else if (flag==CG_END){ /* termination */
end(block);
}
else if (flag ==CG_INIT){ /* initialisation */
init(block);
}
}