PX4 Firmware
PX4 Autopilot Software http://px4.io
navio_sysfs.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2017 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 #include "navio_sysfs.h"
35 
36 #include <fcntl.h>
37 #include <errno.h>
38 #include <unistd.h>
39 #include <px4_platform_common/log.h>
40 
41 using namespace linux_pwm_out;
42 
43 NavioSysfsPWMOut::NavioSysfsPWMOut(const char *device, int max_num_outputs)
44  : _device(device)
45 {
46  if (max_num_outputs > MAX_NUM_PWM) {
47  PX4_WARN("number of outputs too large. Setting to %i", MAX_NUM_PWM);
48  max_num_outputs = MAX_NUM_PWM;
49  }
50 
51  for (int i = 0; i < MAX_NUM_PWM; ++i) {
52  _pwm_fd[i] = -1;
53  }
54 
55  _pwm_num = max_num_outputs;
56 }
57 
59 {
60  for (int i = 0; i < MAX_NUM_PWM; ++i) {
61  if (_pwm_fd[i] != -1) {
62  ::close(_pwm_fd[i]);
63  }
64  }
65 }
66 
68 {
69  int i;
70  char path[128];
71 
72  for (i = 0; i < _pwm_num; ++i) {
73  ::snprintf(path, sizeof(path), "%s/export", _device);
74 
75  if (pwm_write_sysfs(path, i) < 0) {
76  PX4_ERR("PWM export failed");
77  }
78  }
79 
80  for (i = 0; i < _pwm_num; ++i) {
81  ::snprintf(path, sizeof(path), "%s/pwm%u/enable", _device, i);
82 
83  if (pwm_write_sysfs(path, 1) < 0) {
84  PX4_ERR("PWM enable failed");
85  }
86  }
87 
88  for (i = 0; i < _pwm_num; ++i) {
89  ::snprintf(path, sizeof(path), "%s/pwm%u/period", _device, i);
90 
91  if (pwm_write_sysfs(path, (int)1e9 / FREQUENCY_PWM)) {
92  PX4_ERR("PWM period failed");
93  }
94  }
95 
96  for (i = 0; i < _pwm_num; ++i) {
97  ::snprintf(path, sizeof(path), "%s/pwm%u/duty_cycle", _device, i);
98  _pwm_fd[i] = ::open(path, O_WRONLY | O_CLOEXEC);
99 
100  if (_pwm_fd[i] == -1) {
101  PX4_ERR("PWM: Failed to open duty_cycle.");
102  return -errno;
103  }
104  }
105 
106  return 0;
107 }
108 
109 int NavioSysfsPWMOut::send_output_pwm(const uint16_t *pwm, int num_outputs)
110 {
111  char data[16];
112 
113  if (num_outputs > _pwm_num) {
114  num_outputs = _pwm_num;
115  }
116 
117  int ret = 0;
118 
119  //convert this to duty_cycle in ns
120  for (int i = 0; i < num_outputs; ++i) {
121  int n = ::snprintf(data, sizeof(data), "%u", pwm[i] * 1000);
122  int write_ret = ::write(_pwm_fd[i], data, n);
123 
124  if (n != write_ret) {
125  ret = -1;
126  }
127  }
128 
129  return ret;
130 }
131 
132 int NavioSysfsPWMOut::pwm_write_sysfs(char *path, int value)
133 {
134  int fd = ::open(path, O_WRONLY | O_CLOEXEC);
135  int n;
136  char data[16];
137 
138  if (fd == -1) {
139  return -errno;
140  }
141 
142  n = ::snprintf(data, sizeof(data), "%u", value);
143 
144  if (n > 0) {
145  n = ::write(fd, data, n); // This n is not used, but to avoid a compiler error.
146  }
147 
148  ::close(fd);
149 
150  return 0;
151 }
152 
int pwm_write_sysfs(char *path, int value)
Namespace encapsulating all device framework classes, functions and data.
Definition: CDev.cpp:47
int send_output_pwm(const uint16_t *pwm, int num_outputs) override
uint8_t * data
Definition: dataman.cpp:149
static const int FREQUENCY_PWM
Definition: navio_sysfs.h:59
static char _device[64]
int fd
Definition: dataman.cpp:146
static void write(bootloader_app_shared_t *pshared)
static const int MAX_NUM_PWM
Definition: navio_sysfs.h:58
NavioSysfsPWMOut(const char *device, int max_num_outputs)
Definition: navio_sysfs.cpp:43