2021-04-03 19:00:38 -03: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/>.
|
|
|
|
*/
|
2022-02-20 23:44:56 -04:00
|
|
|
|
|
|
|
#include <hal.h>
|
2021-04-03 19:00:38 -03:00
|
|
|
#include "RCOutput.h"
|
|
|
|
#include <AP_Math/AP_Math.h>
|
2022-08-25 02:36:10 -03:00
|
|
|
#include <AP_BoardConfig/AP_BoardConfig.h>
|
2021-04-03 19:00:38 -03:00
|
|
|
#include "hwdef/common/stm32_util.h"
|
|
|
|
#include <AP_InternalError/AP_InternalError.h>
|
|
|
|
#include <AP_Vehicle/AP_Vehicle_Type.h>
|
|
|
|
|
|
|
|
#if HAL_USE_PWM == TRUE
|
2022-08-25 02:36:10 -03:00
|
|
|
#if HAL_DSHOT_ENABLED
|
|
|
|
|
|
|
|
#if HAL_WITH_IO_MCU
|
|
|
|
#include <AP_IOMCU/AP_IOMCU.h>
|
|
|
|
extern AP_IOMCU iomcu;
|
|
|
|
#endif
|
2021-04-03 19:00:38 -03:00
|
|
|
|
|
|
|
using namespace ChibiOS;
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2024-08-20 14:11:06 -03:00
|
|
|
// send a dshot command to group
|
|
|
|
// chan is the FMU channel to send the command to
|
2021-05-02 14:35:19 -03:00
|
|
|
bool RCOutput::dshot_send_command(pwm_group& group, uint8_t command, uint8_t chan)
|
2021-04-03 19:00:38 -03:00
|
|
|
{
|
|
|
|
if (!group.can_send_dshot_pulse()) {
|
2021-05-02 14:35:19 -03:00
|
|
|
return false;
|
2021-04-03 19:00:38 -03:00
|
|
|
}
|
|
|
|
|
2023-09-24 10:09:23 -03:00
|
|
|
if (soft_serial_waiting() || !is_dshot_send_allowed(group.dshot_state)) {
|
2021-04-03 19:00:38 -03:00
|
|
|
// doing serial output or DMAR input, don't send DShot pulses
|
2021-05-02 14:35:19 -03:00
|
|
|
return false;
|
2021-04-03 19:00:38 -03:00
|
|
|
}
|
|
|
|
|
2022-03-25 05:09:20 -03:00
|
|
|
#ifdef HAL_GPIO_LINE_GPIO81
|
2021-04-03 19:00:38 -03:00
|
|
|
TOGGLE_PIN_DEBUG(81);
|
2022-03-25 05:09:20 -03:00
|
|
|
#endif
|
2021-04-03 19:00:38 -03:00
|
|
|
// first make sure we have the DMA channel before anything else
|
2022-08-25 02:36:10 -03:00
|
|
|
#if AP_HAL_SHARED_DMA_ENABLED
|
2021-04-03 19:00:38 -03:00
|
|
|
osalDbgAssert(!group.dma_handle->is_locked(), "DMA handle is already locked");
|
|
|
|
group.dma_handle->lock();
|
2022-08-25 02:36:10 -03:00
|
|
|
#endif
|
2021-04-03 19:00:38 -03:00
|
|
|
|
|
|
|
// only the timer thread releases the locks
|
|
|
|
group.dshot_waiter = rcout_thread_ctx;
|
|
|
|
bool bdshot_telem = false;
|
|
|
|
#ifdef HAL_WITH_BIDIR_DSHOT
|
2023-09-24 10:09:23 -03:00
|
|
|
bdshot_prepare_for_next_pulse(group);
|
|
|
|
bdshot_telem = group.bdshot.enabled;
|
2021-04-03 19:00:38 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
memset((uint8_t *)group.dma_buffer, 0, DSHOT_BUFFER_LENGTH);
|
|
|
|
|
|
|
|
// keep the other ESCs armed rather than sending nothing
|
|
|
|
const uint16_t zero_packet = create_dshot_packet(0, false, bdshot_telem);
|
|
|
|
const uint16_t packet = create_dshot_packet(command, true, bdshot_telem);
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < 4; i++) {
|
2022-03-25 05:09:20 -03:00
|
|
|
if (!group.is_chan_enabled(i)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (group.chan[i] == chan || chan == RCOutput::ALL_CHANNELS) {
|
2021-04-03 19:00:38 -03:00
|
|
|
fill_DMA_buffer_dshot(group.dma_buffer + i, 4, packet, group.bit_width_mul);
|
2022-03-25 05:09:20 -03:00
|
|
|
} else {
|
2021-04-03 19:00:38 -03:00
|
|
|
fill_DMA_buffer_dshot(group.dma_buffer + i, 4, zero_packet, group.bit_width_mul);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chEvtGetAndClearEvents(group.dshot_event_mask);
|
|
|
|
// start sending the pulses out
|
|
|
|
send_pulses_DMAR(group, DSHOT_BUFFER_LENGTH);
|
2022-03-25 05:09:20 -03:00
|
|
|
#ifdef HAL_GPIO_LINE_GPIO81
|
2021-04-03 19:00:38 -03:00
|
|
|
TOGGLE_PIN_DEBUG(81);
|
2022-03-25 05:09:20 -03:00
|
|
|
#endif
|
2021-05-02 14:35:19 -03:00
|
|
|
|
|
|
|
return true;
|
2021-04-03 19:00:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send a dshot command, if command timout is 0 then 10 commands are sent
|
|
|
|
// chan is the servo channel to send the command to
|
|
|
|
void RCOutput::send_dshot_command(uint8_t command, uint8_t chan, uint32_t command_timeout_ms, uint16_t repeat_count, bool priority)
|
|
|
|
{
|
2021-07-29 17:46:15 -03:00
|
|
|
// once armed only priority commands will be accepted
|
|
|
|
if (hal.util->get_soft_armed() && !priority) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// not an FMU channel
|
2023-05-31 18:29:58 -03:00
|
|
|
if (chan < chan_offset || chan == ALL_CHANNELS) {
|
2022-08-25 02:36:10 -03:00
|
|
|
#if HAL_WITH_IO_MCU
|
2023-10-19 12:22:08 -03:00
|
|
|
if (iomcu_dshot) {
|
2022-08-25 02:36:10 -03:00
|
|
|
iomcu.send_dshot_command(command, chan, command_timeout_ms, repeat_count, priority);
|
|
|
|
}
|
|
|
|
#endif
|
2023-05-31 18:29:58 -03:00
|
|
|
if (chan != ALL_CHANNELS) {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-29 17:46:15 -03:00
|
|
|
}
|
|
|
|
|
2021-04-03 19:00:38 -03:00
|
|
|
DshotCommandPacket pkt;
|
|
|
|
pkt.command = command;
|
2023-09-24 10:09:23 -03:00
|
|
|
if (chan != ALL_CHANNELS) {
|
2024-08-20 14:11:06 -03:00
|
|
|
pkt.chan = chan - chan_offset; // normalize to FMU channel
|
2023-09-24 10:09:23 -03:00
|
|
|
} else {
|
|
|
|
pkt.chan = ALL_CHANNELS;
|
|
|
|
}
|
2024-08-20 14:11:06 -03:00
|
|
|
|
2021-05-02 14:35:19 -03:00
|
|
|
if (command_timeout_ms == 0) {
|
2021-04-03 19:00:38 -03:00
|
|
|
pkt.cycle = MAX(10, repeat_count);
|
|
|
|
} else {
|
|
|
|
pkt.cycle = MAX(command_timeout_ms * 1000 / _dshot_period_us, repeat_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
// prioritize anything that is not an LED or BEEP command
|
|
|
|
if (!_dshot_command_queue.push(pkt) && priority) {
|
|
|
|
_dshot_command_queue.push_force(pkt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the dshot outputs that should be reversed (as opposed to 3D)
|
|
|
|
// The chanmask passed is added (ORed) into any existing mask.
|
2021-07-29 17:46:15 -03:00
|
|
|
// The mask uses servo channel numbering
|
2021-01-11 22:01:48 -04:00
|
|
|
void RCOutput::set_reversed_mask(uint32_t chanmask) {
|
2023-07-05 12:48:50 -03:00
|
|
|
_reversed_mask |= chanmask;
|
2021-04-03 19:00:38 -03:00
|
|
|
}
|
|
|
|
|
2021-05-25 18:13:09 -03:00
|
|
|
// Set the dshot outputs that should be reversible/3D
|
2021-04-03 19:00:38 -03:00
|
|
|
// The chanmask passed is added (ORed) into any existing mask.
|
2021-07-29 17:46:15 -03:00
|
|
|
// The mask uses servo channel numbering
|
2021-01-11 22:01:48 -04:00
|
|
|
void RCOutput::set_reversible_mask(uint32_t chanmask) {
|
2023-07-05 12:48:50 -03:00
|
|
|
_reversible_mask |= chanmask;
|
2024-08-13 09:55:57 -03:00
|
|
|
#if HAL_WITH_IO_MCU
|
|
|
|
const uint32_t iomcu_mask = ((1U<<chan_offset)-1);
|
|
|
|
if (iomcu_dshot && (chanmask & iomcu_mask)) {
|
|
|
|
iomcu.set_reversible_mask(chanmask & iomcu_mask);
|
|
|
|
}
|
|
|
|
#endif
|
2021-06-16 06:27:23 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the dshot outputs that should be reversible/3D at 1Hz
|
|
|
|
void RCOutput::update_channel_masks() {
|
|
|
|
|
|
|
|
// post arming dshot commands will not be accepted
|
2021-07-29 17:46:15 -03:00
|
|
|
if (hal.util->get_soft_armed() || _disable_channel_mask_updates) {
|
2021-06-16 06:27:23 -03:00
|
|
|
return;
|
|
|
|
}
|
2021-04-03 19:00:38 -03:00
|
|
|
|
2024-08-20 14:11:06 -03:00
|
|
|
// The masks use servo channel numbering
|
|
|
|
#if NUM_SERVO_CHANNELS > 0
|
|
|
|
for (uint8_t i=0; i<NUM_SERVO_CHANNELS; i++) {
|
2021-06-16 06:27:23 -03:00
|
|
|
switch (_dshot_esc_type) {
|
|
|
|
case DSHOT_ESC_BLHELI:
|
2022-05-23 07:42:48 -03:00
|
|
|
case DSHOT_ESC_BLHELI_S:
|
2023-05-29 16:45:47 -03:00
|
|
|
case DSHOT_ESC_BLHELI_EDT:
|
|
|
|
case DSHOT_ESC_BLHELI_EDT_S:
|
2021-06-16 06:27:23 -03:00
|
|
|
if (_reversible_mask & (1U<<i)) {
|
2023-07-05 12:48:50 -03:00
|
|
|
send_dshot_command(DSHOT_3D_ON, i, 0, 10, true);
|
2021-06-16 06:27:23 -03:00
|
|
|
}
|
|
|
|
if (_reversed_mask & (1U<<i)) {
|
2023-07-05 12:48:50 -03:00
|
|
|
send_dshot_command(DSHOT_REVERSE, i, 0, 10, true);
|
2021-06-16 06:27:23 -03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2021-04-03 19:00:38 -03:00
|
|
|
}
|
|
|
|
}
|
2023-05-29 16:45:47 -03:00
|
|
|
|
|
|
|
if (_dshot_esc_type == DSHOT_ESC_BLHELI_EDT || _dshot_esc_type == DSHOT_ESC_BLHELI_EDT_S) {
|
|
|
|
send_dshot_command(DSHOT_EXTENDED_TELEMETRY_ENABLE, ALL_CHANNELS, 0, 10, true);
|
|
|
|
}
|
2021-08-27 23:58:53 -03:00
|
|
|
#endif
|
2021-04-03 19:00:38 -03:00
|
|
|
}
|
|
|
|
|
2022-08-25 02:36:10 -03:00
|
|
|
#endif // HAL_DSHOT_ENABLED
|
2021-04-03 19:00:38 -03:00
|
|
|
#endif // HAL_USE_PWM
|