123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- /*
- * 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>
- #include <fcntl.h>
- #include <inttypes.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <AP_HAL/AP_HAL.h>
- #include <AP_Math/AP_Math.h>
- static const AP_HAL::HAL &hal = AP_HAL::get_HAL();
- namespace Linux {
- PWM_Sysfs_Base::PWM_Sysfs_Base(char* export_path, char* polarity_path,
- char* enable_path, char* duty_path,
- char* period_path, uint8_t channel)
- : _export_path(export_path)
- , _polarity_path(polarity_path)
- , _enable_path(enable_path)
- , _duty_path(duty_path)
- , _period_path(period_path)
- , _channel(channel)
- {
- }
- PWM_Sysfs_Base::~PWM_Sysfs_Base()
- {
- ::close(_duty_cycle_fd);
- free(_polarity_path);
- free(_enable_path);
- free(_period_path);
- }
- void PWM_Sysfs_Base::init()
- {
- if (_export_path == nullptr || _enable_path == nullptr ||
- _period_path == nullptr || _duty_path == nullptr) {
- 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);
- }
- /* Not checking the return of write_file since it will fail if
- * the pwm has already been exported
- */
- Util::from(hal.util)->write_file(_export_path, "%u", _channel);
- free(_export_path);
- _duty_cycle_fd = ::open(_duty_path, O_RDWR | O_CLOEXEC);
- if (_duty_cycle_fd < 0) {
- AP_HAL::panic("LinuxPWM_Sysfs:Unable to open file %s: %s",
- _duty_path, strerror(errno));
- }
- free(_duty_path);
- }
- void PWM_Sysfs_Base::enable(bool value)
- {
- 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");
- }
- }
- bool PWM_Sysfs_Base::is_enabled()
- {
- unsigned int enabled;
- 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);
- }
- return enabled;
- }
- void PWM_Sysfs_Base::set_period(uint32_t nsec_period)
- {
- set_duty_cycle(0);
- 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);
- }
- }
- uint32_t PWM_Sysfs_Base::get_period()
- {
- uint32_t nsec_period;
- 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;
- }
- return nsec_period;
- }
- void PWM_Sysfs_Base::set_freq(uint32_t freq)
- {
- set_period(hz_to_nsec(freq));
- }
- uint32_t PWM_Sysfs_Base::get_freq()
- {
- return nsec_to_hz(get_period());
- }
- bool PWM_Sysfs_Base::set_duty_cycle(uint32_t nsec_duty_cycle)
- {
- /* Don't log fails since this could spam the console */
- if (dprintf(_duty_cycle_fd, "%u", nsec_duty_cycle) < 0) {
- return false;
- }
- _nsec_duty_cycle_value = nsec_duty_cycle;
- return true;
- }
- uint32_t PWM_Sysfs_Base::get_duty_cycle()
- {
- return _nsec_duty_cycle_value;
- }
- void PWM_Sysfs_Base::set_polarity(PWM_Sysfs_Base::Polarity polarity)
- {
- if (Util::from(hal.util)->write_file(_polarity_path, "%s",
- polarity == NORMAL ?
- "normal" : "inversed") < 0) {
- hal.console->printf("LinuxPWM_Sysfs: %s Unable to set polarity\n",
- _polarity_path);
- }
- }
- PWM_Sysfs_Base::Polarity PWM_Sysfs_Base::get_polarity()
- {
- char polarity[16];
- 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);
- return NORMAL;
- }
- return strncmp(polarity, "normal", sizeof(polarity)) ? INVERSE : NORMAL;
- }
- /* 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;
- }
- 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(),
- nullptr,
- _generate_enable_path(channel),
- _generate_duty_path(channel),
- _generate_period_path(channel),
- channel)
- {
- }
- }
|