2018-01-05 02:19:51 -04:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
* Code by Andrew Tridgell and Siddharth Bharat Purohit
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "AP_HAL_ChibiOS.h"
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
|
2018-03-01 20:46:30 -04:00
|
|
|
#if HAL_USE_PWM == TRUE
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
class ChibiOS::RCOutput : public AP_HAL::RCOutput {
|
2018-01-05 02:19:51 -04:00
|
|
|
public:
|
|
|
|
void init();
|
|
|
|
void set_freq(uint32_t chmask, uint16_t freq_hz);
|
|
|
|
uint16_t get_freq(uint8_t ch);
|
|
|
|
void enable_ch(uint8_t ch);
|
|
|
|
void disable_ch(uint8_t ch);
|
|
|
|
void write(uint8_t ch, uint16_t period_us);
|
|
|
|
uint16_t read(uint8_t ch);
|
|
|
|
void read(uint16_t* period_us, uint8_t len);
|
|
|
|
uint16_t read_last_sent(uint8_t ch) override;
|
|
|
|
void read_last_sent(uint16_t* period_us, uint8_t len) override;
|
|
|
|
void set_esc_scaling(uint16_t min_pwm, uint16_t max_pwm) override {
|
|
|
|
_esc_pwm_min = min_pwm;
|
|
|
|
_esc_pwm_max = max_pwm;
|
|
|
|
}
|
|
|
|
void set_output_mode(enum output_mode mode) override;
|
|
|
|
|
2018-02-23 04:53:56 -04:00
|
|
|
float scale_esc_to_unity(uint16_t pwm) override {
|
|
|
|
return 2.0 * ((float) pwm - _esc_pwm_min) / (_esc_pwm_max - _esc_pwm_min) - 1.0;
|
|
|
|
}
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
void cork(void) override;
|
|
|
|
void push(void) override;
|
|
|
|
|
|
|
|
/*
|
|
|
|
force the safety switch on, disabling PWM output from the IO board
|
|
|
|
*/
|
|
|
|
bool force_safety_on(void) override;
|
|
|
|
|
|
|
|
/*
|
|
|
|
force the safety switch off, enabling PWM output from the IO board
|
|
|
|
*/
|
|
|
|
void force_safety_off(void) override;
|
|
|
|
|
|
|
|
bool enable_px4io_sbus_out(uint16_t rate_hz) override;
|
2018-01-12 23:53:29 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
set default update rate
|
|
|
|
*/
|
|
|
|
void set_default_rate(uint16_t rate_hz) override;
|
2018-01-17 06:25:02 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
timer push (for oneshot min rate)
|
|
|
|
*/
|
|
|
|
void timer_tick(void) override;
|
2018-01-05 02:19:51 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
struct pwm_group {
|
2018-01-12 23:53:29 -04:00
|
|
|
// only advanced timers can do high clocks needed for more than 400Hz
|
|
|
|
bool advanced_timer;
|
2018-01-12 04:29:16 -04:00
|
|
|
uint8_t chan[4]; // chan number, zero based, 255 for disabled
|
2018-01-05 02:19:51 -04:00
|
|
|
PWMConfig pwm_cfg;
|
|
|
|
PWMDriver* pwm_drv;
|
|
|
|
};
|
|
|
|
enum output_mode _output_mode = MODE_PWM_NORMAL;
|
|
|
|
|
2018-01-12 23:53:29 -04:00
|
|
|
static pwm_group pwm_group_list[];
|
2018-01-05 02:19:51 -04:00
|
|
|
uint16_t _esc_pwm_min;
|
|
|
|
uint16_t _esc_pwm_max;
|
|
|
|
|
|
|
|
// offset of first local channel
|
|
|
|
uint8_t chan_offset;
|
|
|
|
|
2018-01-12 04:29:16 -04:00
|
|
|
// total number of channels
|
|
|
|
uint8_t total_channels;
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
// last sent values are for all channels
|
|
|
|
uint16_t last_sent[16];
|
|
|
|
|
|
|
|
// these values are for the local channels. Non-local channels are handled by IOMCU
|
|
|
|
uint32_t en_mask;
|
|
|
|
uint16_t period[16];
|
|
|
|
uint8_t num_channels;
|
|
|
|
bool corked;
|
2018-01-12 17:13:00 -04:00
|
|
|
// mask of channels that are running in high speed
|
|
|
|
uint16_t fast_channel_mask;
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-01-16 03:58:58 -04:00
|
|
|
// min time to trigger next pulse to prevent overlap
|
|
|
|
uint64_t min_pulse_trigger_us;
|
2018-01-17 06:25:02 -04:00
|
|
|
|
|
|
|
// mutex for oneshot triggering
|
|
|
|
mutex_t trigger_mutex;
|
|
|
|
|
|
|
|
// which output groups need triggering
|
|
|
|
uint8_t trigger_groups;
|
|
|
|
|
|
|
|
// widest pulse for oneshot triggering
|
|
|
|
uint16_t trigger_widest_pulse;
|
2018-01-16 03:58:58 -04:00
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
// push out values to local PWM
|
|
|
|
void push_local(void);
|
2018-01-17 06:25:02 -04:00
|
|
|
|
|
|
|
// trigger oneshot pulses
|
|
|
|
void trigger_oneshot(void);
|
2018-01-05 02:19:51 -04:00
|
|
|
};
|
2018-03-01 20:46:30 -04:00
|
|
|
|
|
|
|
#endif // HAL_USE_PWM
|