2015-10-01 18:19:15 -03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* This file is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This file 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "PWM_Sysfs.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
2015-11-05 23:17:32 -04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <inttypes.h>
|
2015-10-01 18:19:15 -03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2015-11-05 23:17:32 -04:00
|
|
|
#include <AP_Math/AP_Math.h>
|
2015-10-01 18:19:15 -03:00
|
|
|
|
2020-03-01 19:50:34 -04:00
|
|
|
extern const AP_HAL::HAL& hal;
|
2015-10-01 18:19:15 -03:00
|
|
|
|
2015-11-05 23:17:32 -04:00
|
|
|
namespace Linux {
|
2015-10-01 18:19:15 -03:00
|
|
|
|
2015-11-26 09:19:11 -04:00
|
|
|
PWM_Sysfs_Base::PWM_Sysfs_Base(char* export_path, char* polarity_path,
|
|
|
|
char* enable_path, char* duty_path,
|
|
|
|
char* period_path, uint8_t channel)
|
2024-09-21 21:37:45 -03:00
|
|
|
: _channel(channel)
|
|
|
|
, _export_path(export_path)
|
2015-11-26 09:19:11 -04:00
|
|
|
, _polarity_path(polarity_path)
|
|
|
|
, _enable_path(enable_path)
|
|
|
|
, _duty_path(duty_path)
|
|
|
|
, _period_path(period_path)
|
2016-10-27 04:46:08 -03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PWM_Sysfs_Base::~PWM_Sysfs_Base()
|
|
|
|
{
|
|
|
|
::close(_duty_cycle_fd);
|
|
|
|
|
|
|
|
free(_polarity_path);
|
|
|
|
free(_enable_path);
|
|
|
|
free(_period_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PWM_Sysfs_Base::init()
|
2015-11-26 09:19:11 -04:00
|
|
|
{
|
2016-10-30 02:24:21 -03:00
|
|
|
if (_export_path == nullptr || _enable_path == nullptr ||
|
|
|
|
_period_path == nullptr || _duty_path == nullptr) {
|
2015-11-30 08:54:34 -04:00
|
|
|
AP_HAL::panic("PWM_Sysfs: export=%p enable=%p period=%p duty=%p"
|
|
|
|
" required path is NULL", _export_path, _enable_path,
|
|
|
|
_period_path, _duty_path);
|
2015-11-26 09:19:11 -04:00
|
|
|
}
|
|
|
|
/* Not checking the return of write_file since it will fail if
|
|
|
|
* the pwm has already been exported
|
2015-10-01 18:19:15 -03:00
|
|
|
*/
|
2016-10-27 04:46:08 -03:00
|
|
|
Util::from(hal.util)->write_file(_export_path, "%u", _channel);
|
2015-11-26 09:19:11 -04:00
|
|
|
free(_export_path);
|
|
|
|
|
|
|
|
_duty_cycle_fd = ::open(_duty_path, O_RDWR | O_CLOEXEC);
|
2015-10-01 18:19:15 -03:00
|
|
|
if (_duty_cycle_fd < 0) {
|
2015-11-26 09:19:11 -04:00
|
|
|
AP_HAL::panic("LinuxPWM_Sysfs:Unable to open file %s: %s",
|
|
|
|
_duty_path, strerror(errno));
|
2015-10-01 18:19:15 -03:00
|
|
|
}
|
2015-11-26 09:19:11 -04:00
|
|
|
free(_duty_path);
|
2015-10-01 18:19:15 -03:00
|
|
|
}
|
|
|
|
|
2015-11-26 09:19:11 -04:00
|
|
|
void PWM_Sysfs_Base::enable(bool value)
|
2015-10-01 18:19:15 -03:00
|
|
|
{
|
2015-11-26 09:19:11 -04:00
|
|
|
if (Util::from(hal.util)->write_file(_enable_path, "%u", value) < 0) {
|
|
|
|
hal.console->printf("LinuxPWM_Sysfs: %s Unable to %s\n",
|
|
|
|
_enable_path, value ? "enable" : "disable");
|
2015-10-01 18:19:15 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-26 09:19:11 -04:00
|
|
|
bool PWM_Sysfs_Base::is_enabled()
|
2015-10-01 18:19:15 -03:00
|
|
|
{
|
2015-11-05 23:17:32 -04:00
|
|
|
unsigned int enabled;
|
|
|
|
|
2015-11-26 09:19:11 -04:00
|
|
|
if (Util::from(hal.util)->read_file(_enable_path, "%u", &enabled) < 0) {
|
|
|
|
hal.console->printf("LinuxPWM_Sysfs: %s Unable to get status\n",
|
|
|
|
_enable_path);
|
2015-10-01 18:19:15 -03:00
|
|
|
}
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
2015-11-26 09:19:11 -04:00
|
|
|
void PWM_Sysfs_Base::set_period(uint32_t nsec_period)
|
2015-10-01 18:19:15 -03:00
|
|
|
{
|
2017-10-13 18:04:05 -03:00
|
|
|
set_duty_cycle(0);
|
|
|
|
|
2015-11-26 09:19:11 -04:00
|
|
|
if (Util::from(hal.util)->write_file(_period_path, "%u", nsec_period) < 0) {
|
|
|
|
hal.console->printf("LinuxPWM_Sysfs: %s Unable to set period\n",
|
|
|
|
_period_path);
|
2015-10-01 18:19:15 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-26 09:19:11 -04:00
|
|
|
uint32_t PWM_Sysfs_Base::get_period()
|
2015-10-01 18:19:15 -03:00
|
|
|
{
|
|
|
|
uint32_t nsec_period;
|
|
|
|
|
2015-11-26 09:19:11 -04:00
|
|
|
if (Util::from(hal.util)->read_file(_period_path, "%u", &nsec_period) < 0) {
|
|
|
|
hal.console->printf("LinuxPWM_Sysfs: %s Unable to get period\n",
|
|
|
|
_period_path);
|
|
|
|
nsec_period = 0;
|
2015-10-01 18:19:15 -03:00
|
|
|
}
|
|
|
|
return nsec_period;
|
|
|
|
}
|
|
|
|
|
2015-11-26 09:19:11 -04:00
|
|
|
void PWM_Sysfs_Base::set_freq(uint32_t freq)
|
2015-10-01 18:19:15 -03:00
|
|
|
{
|
|
|
|
set_period(hz_to_nsec(freq));
|
|
|
|
}
|
|
|
|
|
2015-11-26 09:19:11 -04:00
|
|
|
uint32_t PWM_Sysfs_Base::get_freq()
|
2015-10-01 18:19:15 -03:00
|
|
|
{
|
|
|
|
return nsec_to_hz(get_period());
|
|
|
|
}
|
|
|
|
|
2015-11-26 09:19:11 -04:00
|
|
|
bool PWM_Sysfs_Base::set_duty_cycle(uint32_t nsec_duty_cycle)
|
2015-10-01 18:19:15 -03:00
|
|
|
{
|
2015-11-05 23:17:32 -04:00
|
|
|
/* Don't log fails since this could spam the console */
|
2015-10-01 18:19:15 -03:00
|
|
|
if (dprintf(_duty_cycle_fd, "%u", nsec_duty_cycle) < 0) {
|
2015-11-05 23:17:32 -04:00
|
|
|
return false;
|
2015-10-01 18:19:15 -03:00
|
|
|
}
|
2015-11-05 23:17:32 -04:00
|
|
|
|
|
|
|
_nsec_duty_cycle_value = nsec_duty_cycle;
|
|
|
|
return true;
|
2015-10-01 18:19:15 -03:00
|
|
|
}
|
|
|
|
|
2015-11-26 09:19:11 -04:00
|
|
|
uint32_t PWM_Sysfs_Base::get_duty_cycle()
|
2015-10-01 18:19:15 -03:00
|
|
|
{
|
|
|
|
return _nsec_duty_cycle_value;
|
|
|
|
}
|
|
|
|
|
2015-11-26 09:19:11 -04:00
|
|
|
void PWM_Sysfs_Base::set_polarity(PWM_Sysfs_Base::Polarity polarity)
|
2015-10-01 18:19:15 -03:00
|
|
|
{
|
2015-11-30 08:54:34 -04:00
|
|
|
if (Util::from(hal.util)->write_file(_polarity_path, "%s",
|
2015-11-26 09:19:11 -04:00
|
|
|
polarity == NORMAL ?
|
|
|
|
"normal" : "inversed") < 0) {
|
|
|
|
hal.console->printf("LinuxPWM_Sysfs: %s Unable to set polarity\n",
|
|
|
|
_polarity_path);
|
2015-10-01 18:19:15 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-26 09:19:11 -04:00
|
|
|
PWM_Sysfs_Base::Polarity PWM_Sysfs_Base::get_polarity()
|
2015-10-01 18:19:15 -03:00
|
|
|
{
|
|
|
|
char polarity[16];
|
|
|
|
|
2015-11-26 09:19:11 -04:00
|
|
|
if (Util::from(hal.util)->read_file(_polarity_path, "%s", polarity) < 0) {
|
|
|
|
hal.console->printf("LinuxPWM_Sysfs: %s Unable to get polarity\n",
|
|
|
|
_polarity_path);
|
2015-10-01 18:19:15 -03:00
|
|
|
return NORMAL;
|
|
|
|
}
|
2015-11-05 23:17:32 -04:00
|
|
|
return strncmp(polarity, "normal", sizeof(polarity)) ? INVERSE : NORMAL;
|
|
|
|
}
|
2015-11-26 09:19:11 -04:00
|
|
|
|
|
|
|
/* PWM Sysfs api for mainline kernel */
|
|
|
|
char *PWM_Sysfs::_generate_export_path(uint8_t chip)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
int r = asprintf(&path, "/sys/class/pwm/pwmchip%u/export", chip);
|
|
|
|
if (r == -1) {
|
|
|
|
AP_HAL::panic("LinuxPWM_Sysfs :"
|
|
|
|
"couldn't allocate export path\n");
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *PWM_Sysfs::_generate_polarity_path(uint8_t chip, uint8_t channel)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
int r = asprintf(&path, "/sys/class/pwm/pwmchip%u/pwm%u/polarity",
|
|
|
|
chip, channel);
|
|
|
|
if (r == -1) {
|
|
|
|
AP_HAL::panic("LinuxPWM_Sysfs :"
|
|
|
|
"couldn't allocate polarity path\n");
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *PWM_Sysfs::_generate_enable_path(uint8_t chip, uint8_t channel)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
int r = asprintf(&path, "/sys/class/pwm/pwmchip%u/pwm%u/enable",
|
|
|
|
chip, channel);
|
|
|
|
if (r == -1) {
|
|
|
|
AP_HAL::panic("LinuxPWM_Sysfs :"
|
|
|
|
"couldn't allocate enable path\n");
|
|
|
|
}
|
|
|
|
return path;
|
2015-10-01 18:19:15 -03:00
|
|
|
}
|
|
|
|
|
2015-11-26 09:19:11 -04:00
|
|
|
char *PWM_Sysfs::_generate_duty_path(uint8_t chip, uint8_t channel)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
int r = asprintf(&path, "/sys/class/pwm/pwmchip%u/pwm%u/duty_cycle",
|
|
|
|
chip, channel);
|
|
|
|
if (r == -1) {
|
|
|
|
AP_HAL::panic("LinuxPWM_Sysfs :"
|
|
|
|
"couldn't allocate duty path\n");
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *PWM_Sysfs::_generate_period_path(uint8_t chip, uint8_t channel)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
int r = asprintf(&path, "/sys/class/pwm/pwmchip%u/pwm%u/period",
|
|
|
|
chip, channel);
|
|
|
|
if (r == -1) {
|
|
|
|
AP_HAL::panic("LinuxPWM_Sysfs :"
|
|
|
|
"couldn't allocate period path\n");
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
PWM_Sysfs::PWM_Sysfs(uint8_t chip, uint8_t channel) :
|
|
|
|
PWM_Sysfs_Base(_generate_export_path(chip),
|
|
|
|
_generate_polarity_path(chip, channel),
|
|
|
|
_generate_enable_path(chip, channel),
|
|
|
|
_generate_duty_path(chip, channel),
|
|
|
|
_generate_period_path(chip, channel),
|
|
|
|
channel)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PWM Sysfs api for bebop kernel */
|
|
|
|
char *PWM_Sysfs_Bebop::_generate_export_path()
|
|
|
|
{
|
|
|
|
return strdup("/sys/class/pwm/export");
|
|
|
|
}
|
|
|
|
|
|
|
|
char *PWM_Sysfs_Bebop::_generate_enable_path(uint8_t channel)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
int r = asprintf(&path, "/sys/class/pwm/pwm_%u/run",
|
|
|
|
channel);
|
|
|
|
if (r == -1) {
|
|
|
|
AP_HAL::panic("LinuxPWM_Sysfs :"
|
|
|
|
"couldn't allocate enable path\n");
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *PWM_Sysfs_Bebop::_generate_duty_path(uint8_t channel)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
int r = asprintf(&path, "/sys/class/pwm/pwm_%u/duty_ns",
|
|
|
|
channel);
|
|
|
|
if (r == -1) {
|
|
|
|
AP_HAL::panic("LinuxPWM_Sysfs :"
|
|
|
|
"couldn't allocate duty path\n");
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *PWM_Sysfs_Bebop::_generate_period_path(uint8_t channel)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
int r = asprintf(&path, "/sys/class/pwm/pwm_%u/period_ns",
|
|
|
|
channel);
|
|
|
|
if (r == -1) {
|
|
|
|
AP_HAL::panic("LinuxPWM_Sysfs :"
|
|
|
|
"couldn't allocate period path\n");
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
PWM_Sysfs_Bebop::PWM_Sysfs_Bebop(uint8_t channel) :
|
|
|
|
PWM_Sysfs_Base(_generate_export_path(),
|
2016-10-30 02:24:21 -03:00
|
|
|
nullptr,
|
2015-11-26 09:19:11 -04:00
|
|
|
_generate_enable_path(channel),
|
|
|
|
_generate_duty_path(channel),
|
|
|
|
_generate_period_path(channel),
|
|
|
|
channel)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|