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/>.
|
2018-03-14 03:06:30 -03:00
|
|
|
*
|
2018-01-05 02:19:51 -04:00
|
|
|
* Code by Andrew Tridgell and Siddharth Bharat Purohit
|
|
|
|
*/
|
|
|
|
#include "RCOutput.h"
|
|
|
|
#include <AP_Math/AP_Math.h>
|
2018-01-05 04:55:01 -04:00
|
|
|
#include <AP_BoardConfig/AP_BoardConfig.h>
|
2018-03-16 18:49:40 -03:00
|
|
|
#include <AP_HAL/utility/RingBuffer.h>
|
|
|
|
#include "GPIO.h"
|
2018-05-30 01:22:49 -03:00
|
|
|
#include "hwdef/common/stm32_util.h"
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-03-01 20:46:30 -04:00
|
|
|
#if HAL_USE_PWM == TRUE
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
using namespace ChibiOS;
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
#if HAL_WITH_IO_MCU
|
|
|
|
#include <AP_IOMCU/AP_IOMCU.h>
|
|
|
|
extern AP_IOMCU iomcu;
|
|
|
|
#endif
|
|
|
|
|
2018-04-01 03:00:52 -03:00
|
|
|
#define RCOU_SERIAL_TIMING_DEBUG 0
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
struct RCOutput::pwm_group RCOutput::pwm_group_list[] = { HAL_PWM_GROUPS };
|
2018-03-16 18:49:40 -03:00
|
|
|
struct RCOutput::irq_state RCOutput::irq;
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-01-11 21:49:33 -04:00
|
|
|
#define NUM_GROUPS ARRAY_SIZE_SIMPLE(pwm_group_list)
|
2018-01-07 19:40:23 -04:00
|
|
|
|
2018-03-16 18:49:40 -03:00
|
|
|
// marker for a disabled channel
|
2018-01-12 22:33:48 -04:00
|
|
|
#define CHAN_DISABLED 255
|
|
|
|
|
2018-03-16 18:49:40 -03:00
|
|
|
// #pragma GCC optimize("Og")
|
|
|
|
|
|
|
|
/*
|
|
|
|
initialise RC output driver
|
|
|
|
*/
|
2018-01-13 00:02:05 -04:00
|
|
|
void RCOutput::init()
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-04-06 22:31:56 -03:00
|
|
|
uint8_t pwm_count = AP_BoardConfig::get_pwm_count();
|
2018-01-07 19:40:23 -04:00
|
|
|
for (uint8_t i = 0; i < NUM_GROUPS; i++ ) {
|
2018-01-05 02:19:51 -04:00
|
|
|
//Start Pwm groups
|
2018-03-14 03:06:30 -03:00
|
|
|
pwm_group &group = pwm_group_list[i];
|
|
|
|
group.current_mode = MODE_PWM_NORMAL;
|
2018-01-12 04:29:16 -04:00
|
|
|
for (uint8_t j = 0; j < 4; j++ ) {
|
2018-04-06 22:31:56 -03:00
|
|
|
uint8_t chan = group.chan[j];
|
|
|
|
if (chan >= pwm_count) {
|
|
|
|
group.chan[j] = CHAN_DISABLED;
|
|
|
|
}
|
2018-03-14 03:06:30 -03:00
|
|
|
if (group.chan[j] != CHAN_DISABLED) {
|
2018-05-05 05:54:12 -03:00
|
|
|
num_fmu_channels = MAX(num_fmu_channels, group.chan[j]+1);
|
2018-03-14 03:06:30 -03:00
|
|
|
group.ch_mask |= (1U<<group.chan[j]);
|
2018-01-12 04:29:16 -04:00
|
|
|
}
|
|
|
|
}
|
2018-03-14 03:06:30 -03:00
|
|
|
if (group.ch_mask != 0) {
|
|
|
|
pwmStart(group.pwm_drv, &group.pwm_cfg);
|
2018-03-16 18:49:40 -03:00
|
|
|
group.pwm_started = true;
|
2018-03-14 03:06:30 -03:00
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
2018-03-14 03:06:30 -03:00
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
#if HAL_WITH_IO_MCU
|
2018-01-05 04:55:01 -04:00
|
|
|
if (AP_BoardConfig::io_enabled()) {
|
|
|
|
iomcu.init();
|
2018-03-16 18:49:40 -03:00
|
|
|
// with IOMCU the local (FMU) channels start at 8
|
2018-01-05 04:55:01 -04:00
|
|
|
chan_offset = 8;
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
#endif
|
2018-01-17 06:25:02 -04:00
|
|
|
chMtxObjectInit(&trigger_mutex);
|
2018-03-16 18:49:40 -03:00
|
|
|
|
|
|
|
// setup default output rate of 50Hz
|
|
|
|
set_freq(0xFFFF, 50);
|
2018-04-14 00:55:03 -03:00
|
|
|
|
|
|
|
#ifdef HAL_GPIO_PIN_SAFETY_IN
|
|
|
|
safety_state = AP_HAL::Util::SAFETY_DISARMED;
|
|
|
|
#endif
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
2018-03-16 18:49:40 -03:00
|
|
|
/*
|
|
|
|
setup the output frequency for a group and start pwm output
|
|
|
|
*/
|
|
|
|
void RCOutput::set_freq_group(pwm_group &group)
|
|
|
|
{
|
2018-03-25 20:48:38 -03:00
|
|
|
if (mode_requires_dma(group.current_mode)) {
|
|
|
|
// speed setup in DMA handler
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-16 18:49:40 -03:00
|
|
|
uint16_t freq_set = group.rc_frequency;
|
|
|
|
uint32_t old_clock = group.pwm_cfg.frequency;
|
2018-04-03 05:13:41 -03:00
|
|
|
uint32_t old_period = group.pwm_cfg.period;
|
2018-03-16 18:49:40 -03:00
|
|
|
|
2018-04-03 05:13:41 -03:00
|
|
|
if (freq_set > 400 || group.current_mode == MODE_PWM_ONESHOT125) {
|
|
|
|
// use a 8MHz clock for higher frequencies or for
|
|
|
|
// oneshot125. Using 8MHz for oneshot125 results in the full
|
|
|
|
// 1000 steps for smooth output
|
2018-03-16 18:49:40 -03:00
|
|
|
group.pwm_cfg.frequency = 8000000;
|
|
|
|
} else if (freq_set <= 400) {
|
|
|
|
// use a 1MHz clock
|
|
|
|
group.pwm_cfg.frequency = 1000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the frequency is possible, and keep halving
|
|
|
|
// down to 1MHz until it is OK with the hardware timer we
|
|
|
|
// are using. If we don't do this we'll hit an assert in
|
|
|
|
// the ChibiOS PWM driver on some timers
|
|
|
|
PWMDriver *pwmp = group.pwm_drv;
|
|
|
|
uint32_t psc = (pwmp->clock / pwmp->config->frequency) - 1;
|
|
|
|
while ((psc > 0xFFFF || ((psc + 1) * pwmp->config->frequency) != pwmp->clock) &&
|
|
|
|
group.pwm_cfg.frequency > 1000000) {
|
|
|
|
group.pwm_cfg.frequency /= 2;
|
|
|
|
psc = (pwmp->clock / pwmp->config->frequency) - 1;
|
|
|
|
}
|
2018-03-25 20:48:38 -03:00
|
|
|
|
2018-04-03 05:13:41 -03:00
|
|
|
if (group.current_mode == MODE_PWM_ONESHOT ||
|
|
|
|
group.current_mode == MODE_PWM_ONESHOT125) {
|
|
|
|
// force a period of 0, meaning no pulses till we trigger
|
|
|
|
group.pwm_cfg.period = 0;
|
|
|
|
} else {
|
|
|
|
group.pwm_cfg.period = group.pwm_cfg.frequency/freq_set;
|
|
|
|
}
|
2018-03-26 18:11:10 -03:00
|
|
|
|
2018-03-25 20:48:38 -03:00
|
|
|
bool force_reconfig = false;
|
|
|
|
for (uint8_t j=0; j<4; j++) {
|
2018-03-26 18:11:10 -03:00
|
|
|
if (group.pwm_cfg.channels[j].mode == PWM_OUTPUT_ACTIVE_LOW) {
|
2018-03-25 20:48:38 -03:00
|
|
|
group.pwm_cfg.channels[j].mode = PWM_OUTPUT_ACTIVE_HIGH;
|
|
|
|
force_reconfig = true;
|
|
|
|
}
|
2018-04-13 18:44:14 -03:00
|
|
|
if (group.pwm_cfg.channels[j].mode == PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW) {
|
|
|
|
group.pwm_cfg.channels[j].mode = PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH;
|
|
|
|
force_reconfig = true;
|
|
|
|
}
|
|
|
|
|
2018-03-25 20:48:38 -03:00
|
|
|
}
|
2018-03-26 18:11:10 -03:00
|
|
|
|
2018-04-03 05:13:41 -03:00
|
|
|
if (old_clock != group.pwm_cfg.frequency ||
|
|
|
|
old_period != group.pwm_cfg.period ||
|
|
|
|
!group.pwm_started ||
|
|
|
|
force_reconfig) {
|
2018-03-16 18:49:40 -03:00
|
|
|
// we need to stop and start to setup the new clock
|
|
|
|
if (group.pwm_started) {
|
|
|
|
pwmStop(group.pwm_drv);
|
|
|
|
}
|
|
|
|
pwmStart(group.pwm_drv, &group.pwm_cfg);
|
|
|
|
group.pwm_started = true;
|
|
|
|
}
|
2018-03-26 18:11:10 -03:00
|
|
|
pwmChangePeriod(group.pwm_drv, group.pwm_cfg.period);
|
2018-03-16 18:49:40 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
set output frequency in HZ for a set of channels given by a mask
|
|
|
|
*/
|
2018-01-13 00:02:05 -04:00
|
|
|
void RCOutput::set_freq(uint32_t chmask, uint16_t freq_hz)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
|
|
|
//check if the request spans accross any of the channel groups
|
|
|
|
uint8_t update_mask = 0;
|
|
|
|
|
|
|
|
#if HAL_WITH_IO_MCU
|
2018-01-05 04:55:01 -04:00
|
|
|
if (AP_BoardConfig::io_enabled()) {
|
2018-03-16 18:49:40 -03:00
|
|
|
// change frequency on IOMCU
|
2018-05-11 03:30:18 -03:00
|
|
|
if (freq_hz > 50) {
|
|
|
|
io_fast_channel_mask = chmask;
|
|
|
|
}
|
2018-01-05 04:55:01 -04:00
|
|
|
iomcu.set_freq(chmask, freq_hz);
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
#endif
|
|
|
|
|
2018-03-16 18:49:40 -03:00
|
|
|
// convert to a local (FMU) channel mask
|
2018-01-05 02:19:51 -04:00
|
|
|
chmask >>= chan_offset;
|
|
|
|
if (chmask == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-14 03:06:30 -03:00
|
|
|
|
2018-03-16 18:49:40 -03:00
|
|
|
/*
|
|
|
|
we enable the new frequency on all groups that have one
|
|
|
|
of the requested channels. This means we may enable high
|
|
|
|
speed on some channels that aren't requested, but that
|
|
|
|
is needed in order to fly a vehicle such a a hex
|
|
|
|
multicopter properly
|
|
|
|
*/
|
2018-01-07 19:40:23 -04:00
|
|
|
for (uint8_t i = 0; i < NUM_GROUPS; i++ ) {
|
2018-03-14 03:06:30 -03:00
|
|
|
// greater than 400 doesn't give enough room at higher periods for
|
|
|
|
// the down pulse. This still allows for high rate with oneshot and dshot.
|
|
|
|
pwm_group &group = pwm_group_list[i];
|
|
|
|
uint16_t group_freq = freq_hz;
|
|
|
|
if (group_freq > 400 && group.current_mode != MODE_PWM_BRUSHED) {
|
|
|
|
group_freq = 400;
|
2018-01-12 04:29:16 -04:00
|
|
|
}
|
2018-03-14 03:06:30 -03:00
|
|
|
if ((group.ch_mask & chmask) != 0) {
|
2018-03-16 18:49:40 -03:00
|
|
|
group.rc_frequency = group_freq;
|
|
|
|
set_freq_group(group);
|
2018-03-14 03:06:30 -03:00
|
|
|
update_mask |= group.ch_mask;
|
|
|
|
}
|
|
|
|
if (group_freq > 50) {
|
|
|
|
fast_channel_mask |= group.ch_mask;
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
2018-01-12 23:53:29 -04:00
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
if (chmask != update_mask) {
|
|
|
|
hal.console->printf("RCOutput: Failed to set PWM frequency req %x set %x\n", (unsigned)chmask, (unsigned)update_mask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 23:53:29 -04:00
|
|
|
/*
|
|
|
|
set default output rate
|
|
|
|
*/
|
2018-01-13 00:02:05 -04:00
|
|
|
void RCOutput::set_default_rate(uint16_t freq_hz)
|
2018-01-12 23:53:29 -04:00
|
|
|
{
|
|
|
|
#if HAL_WITH_IO_MCU
|
|
|
|
if (AP_BoardConfig::io_enabled()) {
|
|
|
|
iomcu.set_default_rate(freq_hz);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
for (uint8_t i = 0; i < NUM_GROUPS; i++ ) {
|
2018-03-14 03:06:30 -03:00
|
|
|
pwm_group &group = pwm_group_list[i];
|
|
|
|
if ((group.ch_mask & fast_channel_mask) || group.ch_mask == 0) {
|
2018-01-12 23:53:29 -04:00
|
|
|
// don't change fast channels
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-26 18:11:10 -03:00
|
|
|
group.pwm_cfg.period = group.pwm_cfg.frequency/freq_hz;
|
|
|
|
if (group.pwm_started) {
|
|
|
|
pwmChangePeriod(group.pwm_drv, group.pwm_cfg.period);
|
|
|
|
}
|
2018-01-12 23:53:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
uint16_t RCOutput::get_freq(uint8_t chan)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-05-05 05:54:12 -03:00
|
|
|
if (chan >= max_channels) {
|
2018-01-12 04:29:16 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
#if HAL_WITH_IO_MCU
|
|
|
|
if (chan < chan_offset) {
|
|
|
|
return iomcu.get_freq(chan);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
chan -= chan_offset;
|
2018-03-14 03:06:30 -03:00
|
|
|
|
2018-01-07 19:40:23 -04:00
|
|
|
for (uint8_t i = 0; i < NUM_GROUPS; i++ ) {
|
2018-03-14 03:06:30 -03:00
|
|
|
pwm_group &group = pwm_group_list[i];
|
2018-01-05 02:19:51 -04:00
|
|
|
for (uint8_t j = 0; j < 4; j++) {
|
2018-03-14 03:06:30 -03:00
|
|
|
if (group.chan[j] == chan) {
|
|
|
|
return group.pwm_drv->config->frequency / group.pwm_drv->period;
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// assume 50Hz default
|
|
|
|
return 50;
|
|
|
|
}
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
void RCOutput::enable_ch(uint8_t chan)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-05-05 05:54:12 -03:00
|
|
|
if (chan >= max_channels) {
|
2018-01-12 04:29:16 -04:00
|
|
|
return;
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
if (chan < chan_offset) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
chan -= chan_offset;
|
|
|
|
|
2018-01-07 19:40:23 -04:00
|
|
|
for (uint8_t i = 0; i < NUM_GROUPS; i++ ) {
|
2018-03-14 03:06:30 -03:00
|
|
|
pwm_group &group = pwm_group_list[i];
|
2018-01-05 02:19:51 -04:00
|
|
|
for (uint8_t j = 0; j < 4; j++) {
|
2018-03-14 03:06:30 -03:00
|
|
|
if ((group.chan[j] == chan) && !(en_mask & 1<<chan)) {
|
2018-01-05 02:19:51 -04:00
|
|
|
en_mask |= 1<<chan;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
void RCOutput::disable_ch(uint8_t chan)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-05-05 05:54:12 -03:00
|
|
|
if (chan >= max_channels) {
|
2018-01-12 04:29:16 -04:00
|
|
|
return;
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
if (chan < chan_offset) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
chan -= chan_offset;
|
|
|
|
|
2018-01-07 19:40:23 -04:00
|
|
|
for (uint8_t i = 0; i < NUM_GROUPS; i++ ) {
|
2018-03-14 03:06:30 -03:00
|
|
|
pwm_group &group = pwm_group_list[i];
|
2018-01-05 02:19:51 -04:00
|
|
|
for (uint8_t j = 0; j < 4; j++) {
|
2018-03-14 03:06:30 -03:00
|
|
|
if (group.chan[j] == chan) {
|
|
|
|
pwmDisableChannel(group.pwm_drv, j);
|
2018-01-05 02:19:51 -04:00
|
|
|
en_mask &= ~(1<<chan);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
void RCOutput::write(uint8_t chan, uint16_t period_us)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-05-05 05:54:12 -03:00
|
|
|
if (chan >= max_channels) {
|
2018-01-12 04:29:16 -04:00
|
|
|
return;
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
last_sent[chan] = period_us;
|
2018-03-14 03:06:30 -03:00
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
#if HAL_WITH_IO_MCU
|
|
|
|
// handle IO MCU channels
|
2018-01-05 04:55:01 -04:00
|
|
|
if (AP_BoardConfig::io_enabled()) {
|
2018-04-03 05:13:41 -03:00
|
|
|
uint16_t io_period_us = period_us;
|
2018-05-11 03:30:18 -03:00
|
|
|
if (iomcu_oneshot125 && ((1U<<chan) & io_fast_channel_mask)) {
|
2018-04-03 05:13:41 -03:00
|
|
|
// the iomcu only has one oneshot setting, so we need to scale by a factor
|
|
|
|
// of 8 here for oneshot125
|
|
|
|
io_period_us /= 8;
|
|
|
|
}
|
|
|
|
iomcu.write_channel(chan, io_period_us);
|
2018-01-05 04:55:01 -04:00
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
#endif
|
|
|
|
if (chan < chan_offset) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-14 00:55:03 -03:00
|
|
|
|
|
|
|
if (safety_state == AP_HAL::Util::SAFETY_DISARMED && !(safety_mask & (1U<<chan))) {
|
|
|
|
// implement safety pwm value
|
|
|
|
period_us = safe_pwm[chan];
|
|
|
|
}
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
chan -= chan_offset;
|
2018-03-14 03:06:30 -03:00
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
period[chan] = period_us;
|
2018-05-05 05:54:12 -03:00
|
|
|
|
|
|
|
if (chan < num_fmu_channels) {
|
|
|
|
active_fmu_channels = MAX(chan+1, active_fmu_channels);
|
|
|
|
if (!corked) {
|
|
|
|
push_local();
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
push values to local channels from period[] array
|
|
|
|
*/
|
2018-01-13 00:02:05 -04:00
|
|
|
void RCOutput::push_local(void)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-05-05 05:54:12 -03:00
|
|
|
if (active_fmu_channels == 0) {
|
2018-01-05 02:19:51 -04:00
|
|
|
return;
|
|
|
|
}
|
2018-05-05 05:54:12 -03:00
|
|
|
uint16_t outmask = (1U<<active_fmu_channels)-1;
|
2018-01-06 05:42:47 -04:00
|
|
|
outmask &= en_mask;
|
2018-01-16 03:58:58 -04:00
|
|
|
|
|
|
|
uint16_t widest_pulse = 0;
|
|
|
|
uint8_t need_trigger = 0;
|
2018-04-14 00:55:03 -03:00
|
|
|
bool safety_on = hal.util->safety_switch_state() == AP_HAL::Util::SAFETY_DISARMED;
|
2018-03-14 03:06:30 -03:00
|
|
|
|
2018-01-07 19:40:23 -04:00
|
|
|
for (uint8_t i = 0; i < NUM_GROUPS; i++ ) {
|
2018-03-14 03:06:30 -03:00
|
|
|
pwm_group &group = pwm_group_list[i];
|
2018-03-25 07:44:31 -03:00
|
|
|
if (serial_group == &group) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-16 18:49:40 -03:00
|
|
|
if (!group.pwm_started) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
for (uint8_t j = 0; j < 4; j++) {
|
2018-03-14 03:06:30 -03:00
|
|
|
uint8_t chan = group.chan[j];
|
2018-01-12 22:33:48 -04:00
|
|
|
if (chan == CHAN_DISABLED) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
if (outmask & (1UL<<chan)) {
|
|
|
|
uint32_t period_us = period[chan];
|
2018-04-14 00:55:03 -03:00
|
|
|
|
|
|
|
if (safety_on && !(safety_mask & (1U<<(chan+chan_offset)))) {
|
|
|
|
// safety is on, overwride pwm
|
|
|
|
period_us = safe_pwm[chan+chan_offset];
|
|
|
|
}
|
|
|
|
|
2018-03-14 03:06:30 -03:00
|
|
|
if (group.current_mode == MODE_PWM_BRUSHED) {
|
2018-01-05 02:19:51 -04:00
|
|
|
if (period_us <= _esc_pwm_min) {
|
|
|
|
period_us = 0;
|
|
|
|
} else if (period_us >= _esc_pwm_max) {
|
2018-03-14 03:06:30 -03:00
|
|
|
period_us = PWM_FRACTION_TO_WIDTH(group.pwm_drv, 1, 1);
|
2018-01-05 02:19:51 -04:00
|
|
|
} else {
|
2018-03-14 03:06:30 -03:00
|
|
|
period_us = PWM_FRACTION_TO_WIDTH(group.pwm_drv,\
|
2018-01-05 02:19:51 -04:00
|
|
|
(_esc_pwm_max - _esc_pwm_min), (period_us - _esc_pwm_min));
|
|
|
|
}
|
2018-03-14 03:06:30 -03:00
|
|
|
pwmEnableChannel(group.pwm_drv, j, period_us);
|
2018-04-03 05:13:41 -03:00
|
|
|
} else if (group.current_mode == MODE_PWM_ONESHOT125) {
|
|
|
|
// this gives us a width in 125 ns increments, giving 1000 steps over the 125 to 250 range
|
|
|
|
uint32_t width = ((group.pwm_cfg.frequency/1000000U) * period_us) / 8U;
|
|
|
|
pwmEnableChannel(group.pwm_drv, j, width);
|
2018-03-14 03:06:30 -03:00
|
|
|
} else if (group.current_mode < MODE_PWM_DSHOT150) {
|
2018-03-16 18:49:40 -03:00
|
|
|
uint32_t width = (group.pwm_cfg.frequency/1000000U) * period_us;
|
2018-03-14 03:06:30 -03:00
|
|
|
pwmEnableChannel(group.pwm_drv, j, width);
|
|
|
|
} else if (group.current_mode >= MODE_PWM_DSHOT150 && group.current_mode <= MODE_PWM_DSHOT1200) {
|
|
|
|
// set period_us to time for pulse output, to enable very fast rates
|
|
|
|
period_us = dshot_pulse_time_us;
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
2018-01-16 03:58:58 -04:00
|
|
|
if (period_us > widest_pulse) {
|
|
|
|
widest_pulse = period_us;
|
|
|
|
}
|
2018-03-14 03:06:30 -03:00
|
|
|
if (group.current_mode == MODE_PWM_ONESHOT ||
|
2018-04-03 05:13:41 -03:00
|
|
|
group.current_mode == MODE_PWM_ONESHOT125 ||
|
2018-03-14 03:06:30 -03:00
|
|
|
(group.current_mode >= MODE_PWM_DSHOT150 &&
|
|
|
|
group.current_mode <= MODE_PWM_DSHOT1200)) {
|
|
|
|
need_trigger |= (1U<<i);
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-16 03:58:58 -04:00
|
|
|
|
2018-01-17 06:25:02 -04:00
|
|
|
if (widest_pulse > 2300) {
|
|
|
|
widest_pulse = 2300;
|
|
|
|
}
|
|
|
|
trigger_widest_pulse = widest_pulse;
|
2018-03-14 03:06:30 -03:00
|
|
|
|
|
|
|
trigger_groupmask = need_trigger;
|
|
|
|
|
|
|
|
if (trigger_groupmask) {
|
|
|
|
trigger_groups();
|
2018-01-16 03:58:58 -04:00
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
uint16_t RCOutput::read(uint8_t chan)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-05-05 05:54:12 -03:00
|
|
|
if (chan >= max_channels) {
|
2018-01-12 04:29:16 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
#if HAL_WITH_IO_MCU
|
|
|
|
if (chan < chan_offset) {
|
|
|
|
return iomcu.read_channel(chan);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
chan -= chan_offset;
|
|
|
|
return period[chan];
|
|
|
|
}
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
void RCOutput::read(uint16_t* period_us, uint8_t len)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-05-05 05:54:12 -03:00
|
|
|
if (len > max_channels) {
|
|
|
|
len = max_channels;
|
2018-01-12 04:29:16 -04:00
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
#if HAL_WITH_IO_MCU
|
|
|
|
for (uint8_t i=0; i<MIN(len, chan_offset); i++) {
|
|
|
|
period_us[i] = iomcu.read_channel(i);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (len <= chan_offset) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
len -= chan_offset;
|
|
|
|
period_us += chan_offset;
|
2018-03-14 03:06:30 -03:00
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
memcpy(period_us, period, len*sizeof(uint16_t));
|
|
|
|
}
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
uint16_t RCOutput::read_last_sent(uint8_t chan)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-05-05 05:54:12 -03:00
|
|
|
if (chan >= max_channels) {
|
2018-01-12 04:29:16 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
return last_sent[chan];
|
|
|
|
}
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
void RCOutput::read_last_sent(uint16_t* period_us, uint8_t len)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-05-05 05:54:12 -03:00
|
|
|
if (len > max_channels) {
|
|
|
|
len = max_channels;
|
2018-01-12 04:29:16 -04:00
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
for (uint8_t i=0; i<len; i++) {
|
|
|
|
period_us[i] = read_last_sent(i);
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 06:25:02 -04:00
|
|
|
|
2018-03-16 18:49:40 -03:00
|
|
|
/*
|
|
|
|
does an output mode require the use of the UP DMA channel?
|
|
|
|
*/
|
|
|
|
bool RCOutput::mode_requires_dma(enum output_mode mode) const
|
|
|
|
{
|
|
|
|
switch (mode) {
|
|
|
|
case MODE_PWM_DSHOT150:
|
|
|
|
case MODE_PWM_DSHOT300:
|
|
|
|
case MODE_PWM_DSHOT600:
|
|
|
|
case MODE_PWM_DSHOT1200:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
setup a group for DMA output at a given bitrate. The bit_width is
|
|
|
|
the value for a pulse width in the DMA buffer for a full bit.
|
|
|
|
|
|
|
|
This is used for both DShot and serial output
|
|
|
|
*/
|
|
|
|
bool RCOutput::setup_group_DMA(pwm_group &group, uint32_t bitrate, uint32_t bit_width, bool active_high)
|
|
|
|
{
|
|
|
|
if (!group.dma_buffer) {
|
|
|
|
group.dma_buffer = (uint32_t *)hal.util->malloc_type(dshot_buffer_length, AP_HAL::Util::MEM_DMA_SAFE);
|
|
|
|
if (!group.dma_buffer) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// for dshot we setup for DMAR based output
|
|
|
|
if (!group.dma) {
|
|
|
|
group.dma = STM32_DMA_STREAM(group.dma_up_stream_id);
|
|
|
|
group.dma_handle = new Shared_DMA(group.dma_up_stream_id, SHARED_DMA_NONE,
|
|
|
|
FUNCTOR_BIND_MEMBER(&RCOutput::dma_allocate, void, Shared_DMA *),
|
|
|
|
FUNCTOR_BIND_MEMBER(&RCOutput::dma_deallocate, void, Shared_DMA *));
|
|
|
|
if (!group.dma_handle) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// hold the lock during setup, to ensure there isn't a DMA operation ongoing
|
|
|
|
group.dma_handle->lock();
|
|
|
|
|
|
|
|
// configure timer driver for DMAR at requested rate
|
|
|
|
if (group.pwm_started) {
|
|
|
|
pwmStop(group.pwm_drv);
|
|
|
|
group.pwm_started = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjust frequency to give an allowed value given the
|
|
|
|
// clock. There is probably a better way to do this
|
|
|
|
uint32_t clock_hz = group.pwm_drv->clock;
|
|
|
|
uint32_t target_frequency = bitrate * bit_width;
|
|
|
|
uint32_t prescaler = clock_hz / target_frequency;
|
|
|
|
while ((clock_hz / prescaler) * prescaler != clock_hz && prescaler <= 0x8000) {
|
|
|
|
prescaler++;
|
|
|
|
}
|
|
|
|
uint32_t freq = clock_hz / prescaler;
|
|
|
|
if (prescaler > 0x8000) {
|
|
|
|
group.dma_handle->unlock();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
group.pwm_cfg.frequency = freq;
|
|
|
|
group.pwm_cfg.period = bit_width;
|
|
|
|
group.pwm_cfg.dier = TIM_DIER_UDE;
|
|
|
|
group.pwm_cfg.cr2 = 0;
|
|
|
|
group.bit_width_mul = (freq + (target_frequency/2)) / target_frequency;
|
|
|
|
|
|
|
|
for (uint8_t j=0; j<4; j++) {
|
2018-04-13 18:44:14 -03:00
|
|
|
pwmmode_t mode = group.pwm_cfg.channels[j].mode;
|
|
|
|
if (mode != PWM_OUTPUT_DISABLED) {
|
|
|
|
if(mode == PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW || mode == PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH) {
|
|
|
|
group.pwm_cfg.channels[j].mode = active_high?PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH:PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW;
|
|
|
|
} else {
|
|
|
|
group.pwm_cfg.channels[j].mode = active_high?PWM_OUTPUT_ACTIVE_HIGH:PWM_OUTPUT_ACTIVE_LOW;
|
|
|
|
}
|
2018-03-16 18:49:40 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pwmStart(group.pwm_drv, &group.pwm_cfg);
|
|
|
|
group.pwm_started = true;
|
|
|
|
|
|
|
|
for (uint8_t j=0; j<4; j++) {
|
|
|
|
if (group.chan[j] != CHAN_DISABLED) {
|
|
|
|
pwmEnableChannel(group.pwm_drv, j, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
group.dma_handle->unlock();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
setup output mode for a group, using group.current_mode. Used to restore output
|
|
|
|
after serial operations
|
|
|
|
*/
|
|
|
|
void RCOutput::set_group_mode(pwm_group &group)
|
|
|
|
{
|
|
|
|
if (group.pwm_started) {
|
|
|
|
pwmStop(group.pwm_drv);
|
|
|
|
group.pwm_started = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (group.current_mode) {
|
|
|
|
case MODE_PWM_BRUSHED:
|
|
|
|
// force zero output initially
|
|
|
|
for (uint8_t i=0; i<4; i++) {
|
|
|
|
if (group.chan[i] == CHAN_DISABLED) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uint8_t chan = chan_offset + group.chan[i];
|
|
|
|
write(chan, 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MODE_PWM_DSHOT150 ... MODE_PWM_DSHOT1200: {
|
|
|
|
const uint16_t rates[(1 + MODE_PWM_DSHOT1200) - MODE_PWM_DSHOT150] = { 150, 300, 600, 1200 };
|
|
|
|
uint32_t rate = rates[uint8_t(group.current_mode - MODE_PWM_DSHOT150)] * 1000UL;
|
|
|
|
const uint32_t bit_period = 19;
|
|
|
|
|
|
|
|
// configure timer driver for DMAR at requested rate
|
|
|
|
if (!setup_group_DMA(group, rate, bit_period, true)) {
|
|
|
|
group.current_mode = MODE_PWM_NONE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate min time between pulses
|
|
|
|
dshot_pulse_time_us = 1000000UL * dshot_bit_length / rate;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MODE_PWM_ONESHOT:
|
2018-04-03 05:13:41 -03:00
|
|
|
case MODE_PWM_ONESHOT125:
|
|
|
|
// for oneshot we set a period of 0, which results in no pulses till we trigger
|
|
|
|
group.pwm_cfg.period = 0;
|
2018-03-31 18:55:01 -03:00
|
|
|
group.rc_frequency = 1;
|
2018-03-26 18:11:10 -03:00
|
|
|
if (group.pwm_started) {
|
|
|
|
pwmChangePeriod(group.pwm_drv, group.pwm_cfg.period);
|
|
|
|
}
|
2018-03-16 18:49:40 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MODE_PWM_NORMAL:
|
|
|
|
case MODE_PWM_NONE:
|
|
|
|
// nothing needed
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-04-03 05:13:41 -03:00
|
|
|
set_freq_group(group);
|
|
|
|
|
2018-03-16 18:49:40 -03:00
|
|
|
if (group.current_mode != MODE_PWM_NONE &&
|
|
|
|
!group.pwm_started) {
|
|
|
|
pwmStart(group.pwm_drv, &group.pwm_cfg);
|
|
|
|
group.pwm_started = true;
|
|
|
|
for (uint8_t j=0; j<4; j++) {
|
|
|
|
if (group.chan[j] != CHAN_DISABLED) {
|
|
|
|
pwmEnableChannel(group.pwm_drv, j, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
/*
|
|
|
|
setup output mode
|
|
|
|
*/
|
2018-03-14 03:06:30 -03:00
|
|
|
void RCOutput::set_output_mode(uint16_t mask, enum output_mode mode)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-03-14 03:06:30 -03:00
|
|
|
for (uint8_t i = 0; i < NUM_GROUPS; i++ ) {
|
|
|
|
pwm_group &group = pwm_group_list[i];
|
|
|
|
if (((group.ch_mask << chan_offset) & mask) == 0) {
|
|
|
|
// this group is not affected
|
|
|
|
continue;
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
2018-03-16 18:49:40 -03:00
|
|
|
if (mode_requires_dma(mode) && !group.have_up_dma) {
|
|
|
|
mode = MODE_PWM_NONE;
|
2018-01-16 03:58:58 -04:00
|
|
|
}
|
2018-05-20 21:20:46 -03:00
|
|
|
if (mode > MODE_PWM_NORMAL) {
|
|
|
|
fast_channel_mask |= group.ch_mask;
|
|
|
|
}
|
2018-04-03 05:13:41 -03:00
|
|
|
if (group.current_mode != mode) {
|
|
|
|
group.current_mode = mode;
|
|
|
|
set_group_mode(group);
|
|
|
|
}
|
2018-01-16 03:58:58 -04:00
|
|
|
}
|
2018-03-14 03:06:30 -03:00
|
|
|
#if HAL_WITH_IO_MCU
|
2018-04-03 05:13:41 -03:00
|
|
|
if ((mode == MODE_PWM_ONESHOT ||
|
|
|
|
mode == MODE_PWM_ONESHOT125) &&
|
2018-03-14 03:06:30 -03:00
|
|
|
(mask & ((1U<<chan_offset)-1)) &&
|
|
|
|
AP_BoardConfig::io_enabled()) {
|
2018-04-03 05:13:41 -03:00
|
|
|
iomcu_oneshot125 = (mode == MODE_PWM_ONESHOT125);
|
|
|
|
// also setup IO to use a 1Hz frequency, so we only get output
|
|
|
|
// when we trigger
|
2018-05-11 03:30:18 -03:00
|
|
|
iomcu.set_freq(io_fast_channel_mask, 1);
|
2018-03-14 03:06:30 -03:00
|
|
|
return iomcu.set_oneshot_mode();
|
|
|
|
}
|
|
|
|
#endif
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
start corking output
|
|
|
|
*/
|
2018-01-13 00:02:05 -04:00
|
|
|
void RCOutput::cork(void)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
|
|
|
corked = true;
|
|
|
|
#if HAL_WITH_IO_MCU
|
2018-01-05 04:55:01 -04:00
|
|
|
if (AP_BoardConfig::io_enabled()) {
|
|
|
|
iomcu.cork();
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
stop corking output
|
|
|
|
*/
|
2018-01-13 00:02:05 -04:00
|
|
|
void RCOutput::push(void)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
|
|
|
corked = false;
|
|
|
|
push_local();
|
|
|
|
#if HAL_WITH_IO_MCU
|
2018-01-05 04:55:01 -04:00
|
|
|
if (AP_BoardConfig::io_enabled()) {
|
|
|
|
iomcu.push();
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
enable sbus output
|
|
|
|
*/
|
2018-01-13 00:02:05 -04:00
|
|
|
bool RCOutput::enable_px4io_sbus_out(uint16_t rate_hz)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
|
|
|
#if HAL_WITH_IO_MCU
|
2018-01-05 04:55:01 -04:00
|
|
|
if (AP_BoardConfig::io_enabled()) {
|
|
|
|
return iomcu.enable_sbus_out(rate_hz);
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
#endif
|
2018-01-05 04:55:01 -04:00
|
|
|
return false;
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
2018-01-17 06:25:02 -04:00
|
|
|
|
|
|
|
/*
|
2018-03-14 03:06:30 -03:00
|
|
|
trigger output groups for oneshot or dshot modes
|
2018-01-17 06:25:02 -04:00
|
|
|
*/
|
2018-03-14 03:06:30 -03:00
|
|
|
void RCOutput::trigger_groups(void)
|
2018-01-17 06:25:02 -04:00
|
|
|
{
|
|
|
|
if (!chMtxTryLock(&trigger_mutex)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint64_t now = AP_HAL::micros64();
|
|
|
|
if (now < min_pulse_trigger_us) {
|
|
|
|
// guarantee minimum pulse separation
|
|
|
|
hal.scheduler->delay_microseconds(min_pulse_trigger_us - now);
|
|
|
|
}
|
2018-03-16 18:49:40 -03:00
|
|
|
|
2018-01-17 06:25:02 -04:00
|
|
|
osalSysLock();
|
2018-03-14 03:06:30 -03:00
|
|
|
for (uint8_t i = 0; i < NUM_GROUPS; i++) {
|
|
|
|
pwm_group &group = pwm_group_list[i];
|
2018-03-16 18:49:40 -03:00
|
|
|
if (irq.waiter) {
|
|
|
|
// doing serial output, don't send pulses
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-03 05:13:41 -03:00
|
|
|
if (group.current_mode == MODE_PWM_ONESHOT ||
|
|
|
|
group.current_mode == MODE_PWM_ONESHOT125) {
|
2018-03-14 03:06:30 -03:00
|
|
|
if (trigger_groupmask & (1U<<i)) {
|
|
|
|
// this triggers pulse output for a channel group
|
|
|
|
group.pwm_drv->tim->EGR = STM32_TIM_EGR_UG;
|
|
|
|
}
|
2018-01-17 06:25:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
osalSysUnlock();
|
2018-03-14 03:06:30 -03:00
|
|
|
|
2018-03-16 18:49:40 -03:00
|
|
|
for (uint8_t i = 0; i < NUM_GROUPS; i++) {
|
|
|
|
pwm_group &group = pwm_group_list[i];
|
2018-03-25 07:44:31 -03:00
|
|
|
if (serial_group == &group) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-16 18:49:40 -03:00
|
|
|
if (group.current_mode >= MODE_PWM_DSHOT150 && group.current_mode <= MODE_PWM_DSHOT1200) {
|
|
|
|
dshot_send(group, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 06:25:02 -04:00
|
|
|
/*
|
|
|
|
calculate time that we are allowed to trigger next pulse
|
|
|
|
to guarantee at least a 50us gap between pulses
|
|
|
|
*/
|
|
|
|
min_pulse_trigger_us = AP_HAL::micros64() + trigger_widest_pulse + 50;
|
2018-03-14 03:06:30 -03:00
|
|
|
|
2018-01-17 06:25:02 -04:00
|
|
|
chMtxUnlock(&trigger_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-04-14 00:55:03 -03:00
|
|
|
periodic timer. This is used for oneshot and dshot modes, plus for
|
|
|
|
safety switch update
|
2018-01-17 06:25:02 -04:00
|
|
|
*/
|
|
|
|
void RCOutput::timer_tick(void)
|
|
|
|
{
|
2018-04-14 00:55:03 -03:00
|
|
|
safety_update();
|
|
|
|
|
2018-04-01 21:39:17 -03:00
|
|
|
uint64_t now = AP_HAL::micros64();
|
2018-03-14 03:06:30 -03:00
|
|
|
for (uint8_t i = 0; i < NUM_GROUPS; i++ ) {
|
|
|
|
pwm_group &group = pwm_group_list[i];
|
2018-04-02 03:16:23 -03:00
|
|
|
if (serial_group != &group &&
|
|
|
|
group.current_mode >= MODE_PWM_DSHOT150 &&
|
2018-04-01 21:39:17 -03:00
|
|
|
group.current_mode <= MODE_PWM_DSHOT1200 &&
|
|
|
|
now - group.last_dshot_send_us > 900) {
|
|
|
|
// do a blocking send now, to guarantee DShot sends at
|
|
|
|
// above 1000 Hz. This makes the protocol more reliable on
|
|
|
|
// long cables, and also keeps some ESCs happy that don't
|
|
|
|
// like low rates
|
2018-03-14 03:06:30 -03:00
|
|
|
dshot_send(group, true);
|
|
|
|
}
|
|
|
|
}
|
2018-04-03 05:13:41 -03:00
|
|
|
if (min_pulse_trigger_us == 0 ||
|
2018-04-02 03:16:23 -03:00
|
|
|
serial_group != nullptr) {
|
2018-01-17 06:25:02 -04:00
|
|
|
return;
|
|
|
|
}
|
2018-03-14 03:06:30 -03:00
|
|
|
if (now > min_pulse_trigger_us &&
|
2018-04-03 05:13:41 -03:00
|
|
|
now - min_pulse_trigger_us > 4000) {
|
|
|
|
// trigger at a minimum of 250Hz
|
2018-03-14 03:06:30 -03:00
|
|
|
trigger_groups();
|
2018-04-03 05:13:41 -03:00
|
|
|
}
|
2018-03-14 03:06:30 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
allocate DMA channel
|
|
|
|
*/
|
|
|
|
void RCOutput::dma_allocate(Shared_DMA *ctx)
|
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < NUM_GROUPS; i++ ) {
|
|
|
|
pwm_group &group = pwm_group_list[i];
|
|
|
|
if (group.dma_handle == ctx) {
|
|
|
|
dmaStreamAllocate(group.dma, 10, dma_irq_callback, &group);
|
|
|
|
}
|
2018-01-17 06:25:02 -04:00
|
|
|
}
|
|
|
|
}
|
2018-03-01 20:46:30 -04:00
|
|
|
|
2018-03-14 03:06:30 -03:00
|
|
|
/*
|
|
|
|
deallocate DMA channel
|
|
|
|
*/
|
|
|
|
void RCOutput::dma_deallocate(Shared_DMA *ctx)
|
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < NUM_GROUPS; i++ ) {
|
|
|
|
pwm_group &group = pwm_group_list[i];
|
|
|
|
if (group.dma_handle == ctx) {
|
|
|
|
dmaStreamRelease(group.dma);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
create a DSHOT 16 bit packet. Based on prepareDshotPacket from betaflight
|
|
|
|
*/
|
2018-04-02 01:20:58 -03:00
|
|
|
uint16_t RCOutput::create_dshot_packet(const uint16_t value, bool telem_request)
|
2018-03-14 03:06:30 -03:00
|
|
|
{
|
2018-04-02 01:20:58 -03:00
|
|
|
uint16_t packet = (value << 1);
|
|
|
|
|
|
|
|
if (telem_request) {
|
|
|
|
packet |= 1;
|
|
|
|
}
|
2018-03-14 03:06:30 -03:00
|
|
|
|
|
|
|
// compute checksum
|
|
|
|
uint16_t csum = 0;
|
|
|
|
uint16_t csum_data = packet;
|
|
|
|
for (uint8_t i = 0; i < 3; i++) {
|
|
|
|
csum ^= csum_data;
|
|
|
|
csum_data >>= 4;
|
|
|
|
}
|
|
|
|
csum &= 0xf;
|
|
|
|
// append checksum
|
|
|
|
packet = (packet << 4) | csum;
|
|
|
|
|
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
fill in a DMA buffer for dshot
|
|
|
|
*/
|
2018-03-16 18:49:40 -03:00
|
|
|
void RCOutput::fill_DMA_buffer_dshot(uint32_t *buffer, uint8_t stride, uint16_t packet, uint16_t clockmul)
|
2018-03-14 03:06:30 -03:00
|
|
|
{
|
2018-03-16 18:49:40 -03:00
|
|
|
const uint32_t DSHOT_MOTOR_BIT_0 = 7 * clockmul;
|
|
|
|
const uint32_t DSHOT_MOTOR_BIT_1 = 14 * clockmul;
|
2018-03-14 03:06:30 -03:00
|
|
|
for (uint16_t i = 0; i < 16; i++) {
|
|
|
|
buffer[i * stride] = (packet & 0x8000) ? DSHOT_MOTOR_BIT_1 : DSHOT_MOTOR_BIT_0;
|
|
|
|
packet <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
send a set of DShot packets for a channel group
|
|
|
|
This call be called in blocking mode from the timer, in which case it waits for the DMA lock.
|
|
|
|
In normal operation it doesn't wait for the DMA lock.
|
|
|
|
*/
|
|
|
|
void RCOutput::dshot_send(pwm_group &group, bool blocking)
|
|
|
|
{
|
2018-03-16 18:49:40 -03:00
|
|
|
if (irq.waiter) {
|
|
|
|
// doing serial output, don't send DShot pulses
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-14 03:06:30 -03:00
|
|
|
if (blocking) {
|
|
|
|
group.dma_handle->lock();
|
|
|
|
} else {
|
|
|
|
if (!group.dma_handle->lock_nonblock()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-04-14 00:55:03 -03:00
|
|
|
|
|
|
|
bool safety_on = hal.util->safety_switch_state() == AP_HAL::Util::SAFETY_DISARMED;
|
|
|
|
|
2018-03-14 03:06:30 -03:00
|
|
|
for (uint8_t i=0; i<4; i++) {
|
|
|
|
uint8_t chan = group.chan[i];
|
|
|
|
if (chan != CHAN_DISABLED) {
|
|
|
|
uint16_t pwm = period[chan];
|
2018-04-14 00:55:03 -03:00
|
|
|
|
|
|
|
if (safety_on && !(safety_mask & (1U<<(chan+chan_offset)))) {
|
|
|
|
// safety is on, overwride pwm
|
|
|
|
pwm = safe_pwm[chan+chan_offset];
|
|
|
|
}
|
|
|
|
|
2018-03-14 03:06:30 -03:00
|
|
|
pwm = constrain_int16(pwm, _esc_pwm_min, _esc_pwm_max);
|
|
|
|
uint16_t value = 2000UL * uint32_t(pwm - _esc_pwm_min) / uint32_t(_esc_pwm_max - _esc_pwm_min);
|
|
|
|
if (value != 0) {
|
|
|
|
// dshot values are from 48 to 2047. Zero means off.
|
|
|
|
value += 47;
|
|
|
|
}
|
2018-04-02 01:20:58 -03:00
|
|
|
uint16_t chan_mask = (1U<<chan);
|
|
|
|
bool request_telemetry = (telem_request_mask & chan_mask)?true:false;
|
|
|
|
uint16_t packet = create_dshot_packet(value, request_telemetry);
|
|
|
|
if (request_telemetry) {
|
|
|
|
telem_request_mask &= ~chan_mask;
|
|
|
|
}
|
2018-03-16 18:49:40 -03:00
|
|
|
fill_DMA_buffer_dshot(group.dma_buffer + i, 4, packet, group.bit_width_mul);
|
2018-03-14 03:06:30 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 18:49:40 -03:00
|
|
|
// start sending the pulses out
|
|
|
|
send_pulses_DMAR(group, dshot_buffer_length);
|
2018-04-01 21:39:17 -03:00
|
|
|
|
|
|
|
group.last_dshot_send_us = AP_HAL::micros64();
|
2018-03-16 18:49:40 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
send a series of pulses for a group using DMAR. Pulses must have
|
|
|
|
been encoded into the group dma_buffer with interleaving for the 4
|
|
|
|
channels in the group
|
|
|
|
*/
|
|
|
|
void RCOutput::send_pulses_DMAR(pwm_group &group, uint32_t buffer_length)
|
|
|
|
{
|
2018-03-14 03:06:30 -03:00
|
|
|
/*
|
|
|
|
The DMA approach we are using is based on the DMAR method from
|
|
|
|
betaflight. We use the TIMn_UP DMA channel for the timer, and
|
|
|
|
setup an interleaved set of pulse durations, with a stride of 4
|
|
|
|
(for the 4 channels). We use the DMAR register to point the DMA
|
|
|
|
engine at the 4 CCR registers of the timer, so it fills in the
|
|
|
|
pulse widths for each timer in turn. This means we only use a
|
|
|
|
single DMA channel for groups of 4 timer channels. See the "DMA
|
|
|
|
address for full transfer TIMx_DMAR" section of the
|
|
|
|
datasheet. Many thanks to the betaflight developers for coming
|
|
|
|
up with this great method.
|
|
|
|
*/
|
|
|
|
dmaStreamSetPeripheral(group.dma, &(group.pwm_drv->tim->DMAR));
|
|
|
|
dmaStreamSetMemory0(group.dma, group.dma_buffer);
|
2018-03-16 18:49:40 -03:00
|
|
|
dmaStreamSetTransactionSize(group.dma, buffer_length/sizeof(uint32_t));
|
2018-03-14 03:06:30 -03:00
|
|
|
dmaStreamSetFIFO(group.dma, STM32_DMA_FCR_DMDIS | STM32_DMA_FCR_FTH_FULL);
|
|
|
|
dmaStreamSetMode(group.dma,
|
|
|
|
STM32_DMA_CR_CHSEL(group.dma_up_channel) |
|
|
|
|
STM32_DMA_CR_DIR_M2P | STM32_DMA_CR_PSIZE_WORD | STM32_DMA_CR_MSIZE_WORD |
|
|
|
|
STM32_DMA_CR_MINC | STM32_DMA_CR_PL(3) |
|
|
|
|
STM32_DMA_CR_TEIE | STM32_DMA_CR_TCIE);
|
|
|
|
|
|
|
|
// setup for 4 burst strided transfers. 0x0D is the register
|
|
|
|
// address offset of the CCR registers in the timer peripheral
|
|
|
|
group.pwm_drv->tim->DCR = 0x0D | STM32_TIM_DCR_DBL(3);
|
|
|
|
|
|
|
|
dmaStreamEnable(group.dma);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
DMA interrupt handler. Used to mark DMA completed for DShot
|
|
|
|
*/
|
|
|
|
void RCOutput::dma_irq_callback(void *p, uint32_t flags)
|
|
|
|
{
|
|
|
|
pwm_group *group = (pwm_group *)p;
|
2018-06-17 20:43:30 -03:00
|
|
|
chSysLockFromISR();
|
2018-03-14 03:06:30 -03:00
|
|
|
dmaStreamDisable(group->dma);
|
2018-03-16 18:49:40 -03:00
|
|
|
if (group->in_serial_dma && irq.waiter) {
|
|
|
|
// tell the waiting process we've done the DMA
|
|
|
|
chEvtSignalI(irq.waiter, serial_event_mask);
|
|
|
|
} else {
|
|
|
|
group->dma_handle->unlock_from_IRQ();
|
|
|
|
}
|
2018-06-17 20:43:30 -03:00
|
|
|
chSysUnlockFromISR();
|
2018-03-16 18:49:40 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
setup for serial output to an ESC using the given
|
|
|
|
baudrate. Assumes 1 start bit, 1 stop bit, LSB first and 8
|
|
|
|
databits. This is used for passthrough ESC configuration and
|
|
|
|
firmware flashing
|
|
|
|
|
|
|
|
While serial output is active normal output to the channel group is
|
|
|
|
suspended.
|
|
|
|
*/
|
|
|
|
bool RCOutput::serial_setup_output(uint8_t chan, uint32_t baudrate)
|
|
|
|
{
|
|
|
|
// account for IOMCU channels
|
|
|
|
chan -= chan_offset;
|
|
|
|
pwm_group *new_serial_group = nullptr;
|
|
|
|
|
|
|
|
// find the channel group
|
|
|
|
for (uint8_t i = 0; i < NUM_GROUPS; i++ ) {
|
|
|
|
pwm_group &group = pwm_group_list[i];
|
2018-03-25 20:48:38 -03:00
|
|
|
if (group.current_mode == MODE_PWM_BRUSHED) {
|
|
|
|
// can't do serial output with brushed motors
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-16 18:49:40 -03:00
|
|
|
if (group.ch_mask & (1U<<chan)) {
|
|
|
|
if (serial_group && serial_group != &group) {
|
|
|
|
// we're changing to a new group, end the previous output
|
|
|
|
serial_end();
|
|
|
|
}
|
|
|
|
new_serial_group = &group;
|
|
|
|
for (uint8_t j=0; j<4; j++) {
|
|
|
|
if (group.chan[j] == chan) {
|
|
|
|
group.serial.chan = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!new_serial_group) {
|
|
|
|
if (serial_group) {
|
|
|
|
// shutdown old group
|
|
|
|
serial_end();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup the group for serial output. We ask for a bit width of 1, which gets modified by the
|
|
|
|
if (!setup_group_DMA(*new_serial_group, baudrate, 10, false)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
serial_group = new_serial_group;
|
2018-04-03 07:41:24 -03:00
|
|
|
|
|
|
|
// run the thread doing serial IO at highest priority. This is needed to ensure we don't
|
|
|
|
// lose bytes when we switch between output and input
|
|
|
|
serial_thread = chThdGetSelfX();
|
|
|
|
serial_priority = chThdGetSelfX()->realprio;
|
|
|
|
chThdSetPriority(HIGHPRIO);
|
2018-03-16 18:49:40 -03:00
|
|
|
|
|
|
|
// remember the bit period for serial_read_byte()
|
|
|
|
serial_group->serial.bit_time_us = 1000000UL / baudrate;
|
|
|
|
|
|
|
|
// remember the thread that set things up. This is also used to
|
|
|
|
// mark the group as doing serial output, so normal output is
|
|
|
|
// suspended
|
|
|
|
irq.waiter = chThdGetSelfX();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
fill in a DMA buffer for a serial byte, assuming 1 start bit and 1 stop bit
|
|
|
|
*/
|
|
|
|
void RCOutput::fill_DMA_buffer_byte(uint32_t *buffer, uint8_t stride, uint8_t b, uint32_t bitval)
|
|
|
|
{
|
|
|
|
const uint32_t BIT_0 = bitval;
|
|
|
|
const uint32_t BIT_1 = 0;
|
|
|
|
|
|
|
|
// start bit
|
|
|
|
buffer[0] = BIT_0;
|
|
|
|
|
|
|
|
// stop bit
|
|
|
|
buffer[9*stride] = BIT_1;
|
|
|
|
|
|
|
|
// 8 data bits
|
|
|
|
for (uint8_t i = 0; i < 8; i++) {
|
|
|
|
buffer[(1 + i) * stride] = (b & 1) ? BIT_1 : BIT_0;
|
|
|
|
b >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
send one serial byte, blocking call, should be called with the DMA lock held
|
|
|
|
*/
|
|
|
|
bool RCOutput::serial_write_byte(uint8_t b)
|
|
|
|
{
|
|
|
|
chEvtGetAndClearEvents(serial_event_mask);
|
|
|
|
|
|
|
|
fill_DMA_buffer_byte(serial_group->dma_buffer+serial_group->serial.chan, 4, b, serial_group->bit_width_mul*10);
|
|
|
|
|
|
|
|
serial_group->in_serial_dma = true;
|
|
|
|
|
|
|
|
// start sending the pulses out
|
|
|
|
send_pulses_DMAR(*serial_group, 10*4*sizeof(uint32_t));
|
|
|
|
|
|
|
|
// wait for the event
|
|
|
|
eventmask_t mask = chEvtWaitAnyTimeout(serial_event_mask, MS2ST(2));
|
|
|
|
|
|
|
|
serial_group->in_serial_dma = false;
|
|
|
|
|
|
|
|
return (mask & serial_event_mask) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
send a set of serial bytes, blocking call
|
|
|
|
*/
|
|
|
|
bool RCOutput::serial_write_bytes(const uint8_t *bytes, uint16_t len)
|
|
|
|
{
|
|
|
|
if (!serial_group) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
serial_group->dma_handle->lock();
|
|
|
|
memset(serial_group->dma_buffer, 0, dshot_buffer_length);
|
|
|
|
while (len--) {
|
|
|
|
if (!serial_write_byte(*bytes++)) {
|
|
|
|
serial_group->dma_handle->unlock();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-05-22 03:11:45 -03:00
|
|
|
|
2018-03-16 18:49:40 -03:00
|
|
|
// add a small delay for last word of output to have completely
|
|
|
|
// finished
|
2018-05-22 03:11:45 -03:00
|
|
|
hal.scheduler->delay_microseconds(25);
|
|
|
|
|
|
|
|
serial_group->dma_handle->unlock();
|
2018-03-16 18:49:40 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
irq handler for bit transition in serial_read_byte()
|
|
|
|
This implements a one byte soft serial reader
|
|
|
|
*/
|
|
|
|
void RCOutput::serial_bit_irq(void)
|
|
|
|
{
|
2018-04-15 02:33:40 -03:00
|
|
|
systime_t now = chVTGetSystemTimeX();
|
2018-03-16 18:49:40 -03:00
|
|
|
uint8_t bit = palReadLine(irq.line);
|
|
|
|
bool send_signal = false;
|
2018-04-01 03:00:52 -03:00
|
|
|
|
|
|
|
#if RCOU_SERIAL_TIMING_DEBUG
|
|
|
|
palWriteLine(HAL_GPIO_LINE_GPIO55, bit);
|
|
|
|
#endif
|
2018-03-16 18:49:40 -03:00
|
|
|
|
|
|
|
if (irq.nbits == 0 || bit == irq.last_bit) {
|
|
|
|
// start of byte, should be low
|
|
|
|
if (bit != 0) {
|
|
|
|
irq.byteval = 0x200;
|
|
|
|
send_signal = true;
|
|
|
|
} else {
|
|
|
|
irq.nbits = 1;
|
2018-04-15 02:33:40 -03:00
|
|
|
irq.byte_start_tick = now;
|
2018-03-16 18:49:40 -03:00
|
|
|
irq.bitmask = 0;
|
|
|
|
}
|
|
|
|
} else {
|
2018-04-15 02:33:40 -03:00
|
|
|
systime_t dt = now - irq.byte_start_tick;
|
|
|
|
uint8_t bitnum = (dt+(irq.bit_time_tick/2)) / irq.bit_time_tick;
|
2018-03-16 18:49:40 -03:00
|
|
|
|
|
|
|
if (bitnum > 10) {
|
|
|
|
bitnum = 10;
|
|
|
|
}
|
|
|
|
if (!bit) {
|
|
|
|
// set the bits that we've processed
|
|
|
|
irq.bitmask |= ((1U<<bitnum)-1) & ~((1U<<irq.nbits)-1);
|
|
|
|
}
|
|
|
|
irq.nbits = bitnum;
|
|
|
|
|
|
|
|
if (irq.nbits == 10) {
|
|
|
|
send_signal = true;
|
|
|
|
irq.byteval = irq.bitmask & 0x3FF;
|
|
|
|
irq.bitmask = 0;
|
|
|
|
irq.nbits = 1;
|
2018-04-15 02:33:40 -03:00
|
|
|
irq.byte_start_tick = now;
|
2018-03-16 18:49:40 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
irq.last_bit = bit;
|
|
|
|
|
|
|
|
if (send_signal) {
|
|
|
|
chSysLockFromISR();
|
|
|
|
chEvtSignalI(irq.waiter, serial_event_mask);
|
|
|
|
chSysUnlockFromISR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
read a byte from a port, using serial parameters from serial_setup_output()
|
|
|
|
*/
|
|
|
|
bool RCOutput::serial_read_byte(uint8_t &b)
|
|
|
|
{
|
2018-03-31 18:55:44 -03:00
|
|
|
bool timed_out = ((chEvtWaitAnyTimeout(serial_event_mask, MS2ST(10)) & serial_event_mask) == 0);
|
2018-03-16 18:49:40 -03:00
|
|
|
|
|
|
|
uint16_t byteval = irq.byteval;
|
|
|
|
|
|
|
|
if (timed_out) {
|
|
|
|
// we can accept a byte with a timeout if the last bit was 1
|
|
|
|
// and the start bit is set correctly
|
|
|
|
if (irq.last_bit == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
byteval = irq.bitmask | 0x200;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((byteval & 0x201) != 0x200) {
|
|
|
|
// wrong start/stop bits
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
b = uint8_t(byteval>>1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
read a byte from a port, using serial parameters from serial_setup_output()
|
|
|
|
*/
|
|
|
|
uint16_t RCOutput::serial_read_bytes(uint8_t *buf, uint16_t len)
|
|
|
|
{
|
2018-03-25 20:48:38 -03:00
|
|
|
if (serial_group == nullptr) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-03-16 18:49:40 -03:00
|
|
|
pwm_group &group = *serial_group;
|
2018-04-06 20:58:57 -03:00
|
|
|
const ioline_t line = group.pal_lines[group.serial.chan];
|
2018-04-05 19:30:46 -03:00
|
|
|
uint32_t gpio_mode = PAL_MODE_INPUT_PULLUP;
|
2018-03-16 18:49:40 -03:00
|
|
|
uint32_t restore_mode = PAL_MODE_ALTERNATE(group.alt_functions[group.serial.chan]) | PAL_STM32_OSPEED_MID2 | PAL_STM32_OTYPE_PUSHPULL;
|
|
|
|
uint16_t i = 0;
|
|
|
|
|
2018-04-01 03:00:52 -03:00
|
|
|
#if RCOU_SERIAL_TIMING_DEBUG
|
|
|
|
hal.gpio->pinMode(54, 1);
|
|
|
|
hal.gpio->pinMode(55, 1);
|
|
|
|
#endif
|
2018-03-16 18:49:40 -03:00
|
|
|
|
|
|
|
// assume GPIO mappings for PWM outputs start at 50
|
2018-04-06 20:58:57 -03:00
|
|
|
palSetLineMode(line, gpio_mode);
|
2018-03-16 18:49:40 -03:00
|
|
|
|
|
|
|
chEvtGetAndClearEvents(serial_event_mask);
|
|
|
|
|
|
|
|
irq.line = group.pal_lines[group.serial.chan];
|
|
|
|
irq.nbits = 0;
|
|
|
|
irq.bitmask = 0;
|
|
|
|
irq.byteval = 0;
|
2018-04-15 02:33:40 -03:00
|
|
|
irq.bit_time_tick = serial_group->serial.bit_time_us;
|
2018-03-16 18:49:40 -03:00
|
|
|
irq.last_bit = 0;
|
|
|
|
irq.waiter = chThdGetSelfX();
|
2018-04-01 03:00:52 -03:00
|
|
|
|
|
|
|
#if RCOU_SERIAL_TIMING_DEBUG
|
|
|
|
palWriteLine(HAL_GPIO_LINE_GPIO54, 1);
|
|
|
|
#endif
|
2018-03-16 18:49:40 -03:00
|
|
|
|
2018-04-06 20:58:57 -03:00
|
|
|
if (!((GPIO *)hal.gpio)->_attach_interrupt(line, serial_bit_irq, HAL_GPIO_INTERRUPT_BOTH)) {
|
2018-04-01 03:00:52 -03:00
|
|
|
#if RCOU_SERIAL_TIMING_DEBUG
|
|
|
|
palWriteLine(HAL_GPIO_LINE_GPIO54, 0);
|
|
|
|
#endif
|
2018-03-16 18:49:40 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
if (!serial_read_byte(buf[i])) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-06 20:58:57 -03:00
|
|
|
((GPIO *)hal.gpio)->_attach_interrupt(line, nullptr, 0);
|
2018-03-16 18:49:40 -03:00
|
|
|
irq.waiter = nullptr;
|
|
|
|
|
2018-04-06 20:58:57 -03:00
|
|
|
palSetLineMode(line, restore_mode);
|
2018-04-01 03:00:52 -03:00
|
|
|
#if RCOU_SERIAL_TIMING_DEBUG
|
|
|
|
palWriteLine(HAL_GPIO_LINE_GPIO54, 0);
|
|
|
|
#endif
|
2018-03-16 18:49:40 -03:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
end serial output
|
|
|
|
*/
|
|
|
|
void RCOutput::serial_end(void)
|
|
|
|
{
|
|
|
|
if (serial_group) {
|
2018-03-25 20:48:38 -03:00
|
|
|
pwm_group &group = *serial_group;
|
2018-03-16 18:49:40 -03:00
|
|
|
// restore normal output
|
2018-03-25 20:48:38 -03:00
|
|
|
if (group.pwm_started) {
|
|
|
|
pwmStop(group.pwm_drv);
|
|
|
|
group.pwm_started = false;
|
|
|
|
}
|
|
|
|
set_group_mode(group);
|
|
|
|
set_freq_group(group);
|
2018-03-16 18:49:40 -03:00
|
|
|
irq.waiter = nullptr;
|
2018-04-03 07:41:24 -03:00
|
|
|
if (serial_thread == chThdGetSelfX()) {
|
|
|
|
chThdSetPriority(serial_priority);
|
|
|
|
serial_thread = nullptr;
|
|
|
|
}
|
2018-03-16 18:49:40 -03:00
|
|
|
}
|
|
|
|
serial_group = nullptr;
|
2018-03-14 03:06:30 -03:00
|
|
|
}
|
|
|
|
|
2018-04-14 00:55:03 -03:00
|
|
|
/*
|
|
|
|
get safety switch state for Util.cpp
|
|
|
|
*/
|
|
|
|
AP_HAL::Util::safety_state RCOutput::_safety_switch_state(void)
|
|
|
|
{
|
|
|
|
#if HAL_WITH_IO_MCU
|
|
|
|
if (AP_BoardConfig::io_enabled()) {
|
|
|
|
return iomcu.get_safety_switch_state();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return safety_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
force the safety switch on, disabling PWM output from the IO board
|
|
|
|
*/
|
|
|
|
bool RCOutput::force_safety_on(void)
|
|
|
|
{
|
|
|
|
#if HAL_WITH_IO_MCU
|
|
|
|
if (AP_BoardConfig::io_enabled()) {
|
|
|
|
return iomcu.force_safety_on();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
safety_state = AP_HAL::Util::SAFETY_DISARMED;
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
force the safety switch off, enabling PWM output from the IO board
|
|
|
|
*/
|
|
|
|
void RCOutput::force_safety_off(void)
|
|
|
|
{
|
|
|
|
#if HAL_WITH_IO_MCU
|
|
|
|
if (AP_BoardConfig::io_enabled()) {
|
|
|
|
iomcu.force_safety_off();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
safety_state = AP_HAL::Util::SAFETY_ARMED;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
set PWM to send to a set of channels when the safety switch is
|
|
|
|
in the safe state
|
|
|
|
*/
|
|
|
|
void RCOutput::set_safety_pwm(uint32_t chmask, uint16_t period_us)
|
|
|
|
{
|
|
|
|
#if HAL_WITH_IO_MCU
|
|
|
|
if (AP_BoardConfig::io_enabled()) {
|
|
|
|
iomcu.set_safety_pwm(chmask, period_us);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
for (uint8_t i=0; i<16; i++) {
|
|
|
|
if (chmask & (1U<<i)) {
|
|
|
|
safe_pwm[i] = period_us;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
update safety state
|
|
|
|
*/
|
|
|
|
void RCOutput::safety_update(void)
|
|
|
|
{
|
|
|
|
uint32_t now = AP_HAL::millis();
|
|
|
|
if (now - safety_update_ms < 100) {
|
|
|
|
// update safety at 10Hz
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
safety_update_ms = now;
|
|
|
|
|
|
|
|
AP_BoardConfig *boardconfig = AP_BoardConfig::get_instance();
|
|
|
|
|
|
|
|
if (boardconfig) {
|
|
|
|
// remember mask of channels to allow with safety on
|
|
|
|
safety_mask = boardconfig->get_safety_mask();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAL_GPIO_PIN_SAFETY_IN
|
|
|
|
// handle safety button
|
|
|
|
uint16_t safety_options = 0;
|
|
|
|
if (boardconfig) {
|
|
|
|
safety_options = boardconfig->get_safety_button_options();
|
|
|
|
}
|
|
|
|
bool safety_pressed = palReadLine(HAL_GPIO_PIN_SAFETY_IN);
|
|
|
|
if (!(safety_options & AP_BoardConfig::BOARD_SAFETY_OPTION_BUTTON_ACTIVE_ARMED) &&
|
|
|
|
hal.util->get_soft_armed()) {
|
|
|
|
safety_pressed = false;
|
|
|
|
}
|
|
|
|
if (safety_state==AP_HAL::Util::SAFETY_DISARMED &&
|
|
|
|
!(safety_options & AP_BoardConfig::BOARD_SAFETY_OPTION_BUTTON_ACTIVE_SAFETY_ON)) {
|
|
|
|
safety_pressed = false;
|
|
|
|
}
|
|
|
|
if (safety_state==AP_HAL::Util::SAFETY_ARMED &&
|
|
|
|
!(safety_options & AP_BoardConfig::BOARD_SAFETY_OPTION_BUTTON_ACTIVE_SAFETY_OFF)) {
|
|
|
|
safety_pressed = false;
|
|
|
|
}
|
|
|
|
if (safety_pressed) {
|
|
|
|
safety_button_counter++;
|
|
|
|
} else {
|
|
|
|
safety_button_counter = 0;
|
|
|
|
}
|
|
|
|
if (safety_button_counter == 10) {
|
|
|
|
// safety has been pressed for 1 second, change state
|
|
|
|
if (safety_state==AP_HAL::Util::SAFETY_ARMED) {
|
|
|
|
safety_state = AP_HAL::Util::SAFETY_DISARMED;
|
|
|
|
} else {
|
|
|
|
safety_state = AP_HAL::Util::SAFETY_ARMED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#elif HAL_WITH_IO_MCU
|
|
|
|
safety_state = _safety_switch_state();
|
|
|
|
iomcu.set_safety_mask(safety_mask);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAL_GPIO_PIN_LED_SAFETY
|
|
|
|
led_counter = (led_counter+1) % 16;
|
|
|
|
const uint16_t led_pattern = safety_state==AP_HAL::Util::SAFETY_DISARMED?0x5500:0xFFFF;
|
|
|
|
palWriteLine(HAL_GPIO_PIN_LED_SAFETY, (led_pattern & (1U << led_counter))?0:1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-03-01 20:46:30 -04:00
|
|
|
#endif // HAL_USE_PWM
|