Dawn Framework 1.0
Universal data acquisition framework for embedded systems
pwm.cxx
1// dawn/src/porting/nuttx/pwm.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/porting/pwm.hxx"
7
8#include <climits>
9#include <errno.h>
10#include <fcntl.h>
11#include <nuttx/timers/pwm.h>
12#include <sys/ioctl.h>
13#include <unistd.h>
14
15#include "dawn/debug.hxx"
16#include "dawn/porting/config.hxx"
17
18//***************************************************************************
19// Public Functions
20//***************************************************************************
21
22//***************************************************************************
23// Name: pwm_open
24//***************************************************************************
25
26int pwm_open(const char *path)
27{
28 int fd;
29
30 fd = open(path, O_WRONLY | O_NONBLOCK);
31 DAWNINFO("PWM: open %s %d\n", path, fd);
32 if (fd < 0)
33 {
34 DAWNERR("Failed to open PWM file %s (error %d)\n", path, fd);
35 return -EIO;
36 }
37
38 return fd;
39}
40
41//***************************************************************************
42// Name: pwm_close
43//***************************************************************************
44
45void pwm_close(int fd)
46{
47 if (fd)
48 {
49 close(fd);
50 }
51}
52
53//***************************************************************************
54// Name: pwm_init
55//***************************************************************************
56
57void pwm_init(int fd)
58{
59 // Channel validation is the IO layer's responsibility.
60
61 (void)fd;
62}
63
64//***************************************************************************
65// Name: pwm_start
66//***************************************************************************
67
68int pwm_start(int fd)
69{
70 return ioctl(fd, PWMIOC_START, 0);
71}
72
73//***************************************************************************
74// Name: pwm_stop
75//***************************************************************************
76
77int pwm_stop(int fd)
78{
79 return ioctl(fd, PWMIOC_STOP, 0);
80}
81
82//***************************************************************************
83// Name: pwm_write
84//***************************************************************************
85
86int pwm_write(int fd, dawn::porting::pwm_write_s *pwm)
87{
88 struct pwm_info_s info = {0};
89 int ret;
90
91 // Fill data
92
93 info.frequency = pwm->freq;
94
95 for (int i = 0; i < CONFIG_PWM_NCHANNELS; i++)
96 {
97 info.channels[i].duty = b16idiv(pwm->channels[i].duty, 1000);
98 if (pwm->channels[i].channel > SCHAR_MAX)
99 {
100 return -ERANGE;
101 }
102
103 info.channels[i].channel = static_cast<int8_t>(pwm->channels[i].channel);
104 }
105
106 // Set characteristic
107
108 ret = ioctl(fd, PWMIOC_SETCHARACTERISTICS, reinterpret_cast<unsigned long>(&info));
109 if (ret < 0)
110 {
111 DAWNERR("PWMIOC_SETCHARACTERISTICS failed %d\n", -errno);
112 return -errno;
113 }
114
115 return ret;
116}
PWM write data.
Definition pwm.hxx:22