2011-11-01 07:09:55 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup PIOS PIOS Core hardware abstraction layer
|
|
|
|
* @{
|
|
|
|
* @addtogroup PIOS_SERVO RC Servo Functions
|
|
|
|
* @brief Code to do set RC servo output
|
|
|
|
* @{
|
|
|
|
*
|
2011-11-01 10:39:51 +01:00
|
|
|
* @file pios_servo.c
|
2016-09-26 23:17:04 +02:00
|
|
|
* @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016.
|
|
|
|
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
|
2011-11-01 07:09:55 +01:00
|
|
|
* @brief RC Servo routines (STM32 dependent)
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
2011-11-01 10:39:51 +01:00
|
|
|
*
|
2011-11-01 07:09:55 +01:00
|
|
|
*****************************************************************************/
|
2011-11-01 10:39:51 +01:00
|
|
|
/*
|
|
|
|
* This program 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
|
2011-11-01 07:09:55 +01:00
|
|
|
* (at your option) any later version.
|
2011-11-01 10:39:51 +01:00
|
|
|
*
|
|
|
|
* This program 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
|
2011-11-01 07:09:55 +01:00
|
|
|
* for more details.
|
2011-11-01 10:39:51 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2011-11-01 07:09:55 +01:00
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2017-04-21 14:54:21 +02:00
|
|
|
/*
|
|
|
|
* DShot: Tribute belongs to dRonin, http://dRonin.org/ for sparking the idea of
|
|
|
|
* using gpio bitbang as more general solution over using timer dma.
|
|
|
|
*/
|
2011-11-01 07:09:55 +01:00
|
|
|
|
|
|
|
#include "pios.h"
|
2013-03-15 19:42:54 +01:00
|
|
|
|
|
|
|
#ifdef PIOS_INCLUDE_SERVO
|
|
|
|
|
2011-11-01 07:09:55 +01:00
|
|
|
#include "pios_servo_priv.h"
|
2011-11-01 10:39:51 +01:00
|
|
|
#include "pios_tim_priv.h"
|
2011-11-01 07:09:55 +01:00
|
|
|
|
|
|
|
/* Private Function Prototypes */
|
|
|
|
|
2017-03-27 15:51:43 +02:00
|
|
|
#define PIOS_SERVO_GPIO_BANKS 3
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
static const struct pios_servo_cfg *servo_cfg;
|
2011-11-01 10:39:51 +01:00
|
|
|
|
2017-03-27 15:51:43 +02:00
|
|
|
static volatile uint32_t *pios_servo_bsrr[PIOS_SERVO_GPIO_BANKS]; // GPIO banks
|
|
|
|
|
|
|
|
struct pios_servo_bank {
|
|
|
|
enum pios_servo_bank_mode mode;
|
|
|
|
uint16_t next_update;
|
|
|
|
uint16_t max_pulse;
|
|
|
|
TIM_TypeDef *timer;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct pios_servo_pin {
|
|
|
|
struct pios_servo_bank *bank;
|
|
|
|
uint8_t bank_nr;
|
|
|
|
uint8_t gpio_bank;
|
|
|
|
uint16_t value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static struct pios_servo_bank pios_servo_banks[PIOS_SERVO_BANKS];
|
|
|
|
static struct pios_servo_pin *pios_servo_pins;
|
|
|
|
|
2015-01-23 23:50:51 +01:00
|
|
|
|
2017-03-27 15:51:43 +02:00
|
|
|
// Dshot timing
|
|
|
|
static uint32_t pios_dshot_t0h_raw;
|
|
|
|
static uint32_t pios_dshot_t1h_raw;
|
|
|
|
static uint32_t pios_dshot_t_raw;
|
2015-01-23 23:50:51 +01:00
|
|
|
|
2016-09-24 02:28:45 +02:00
|
|
|
static bool pios_servo_enabled = true;
|
|
|
|
|
2015-01-24 13:35:41 +01:00
|
|
|
#define PIOS_SERVO_TIMER_CLOCK 1000000
|
2015-02-02 02:58:22 +01:00
|
|
|
#define PIOS_SERVO_SAFE_MARGIN 50
|
2016-09-24 02:28:45 +02:00
|
|
|
|
2017-03-27 15:51:43 +02:00
|
|
|
|
|
|
|
#define DSHOT_TIMING_ADJUST 8
|
|
|
|
#define DSHOT_T0H_DIV 2666
|
|
|
|
#define DSHOT_T1H_DIV 1333
|
|
|
|
#define DSHOT_NUM_BITS 16
|
|
|
|
|
|
|
|
|
2016-09-24 02:28:45 +02:00
|
|
|
extern void PIOS_Servo_Disable()
|
|
|
|
{
|
|
|
|
if (!servo_cfg) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pios_servo_enabled = false;
|
|
|
|
|
|
|
|
/* NOTE: Following will stop pulses and force low level on output pins.
|
|
|
|
* this is ok with ESC and servos, but brushed motors could be in trouble
|
|
|
|
* if using inverted setup */
|
|
|
|
|
|
|
|
for (uint8_t i = 0; (i < servo_cfg->num_channels); i++) {
|
|
|
|
const struct pios_tim_channel *chan = &servo_cfg->channels[i];
|
|
|
|
|
|
|
|
GPIO_InitTypeDef init = chan->pin.init;
|
|
|
|
|
2017-05-22 15:08:04 +02:00
|
|
|
#if defined(STM32F40_41xxx) || defined(STM32F446xx) || defined(STM32F411xE) || defined(STM32F3)
|
2016-09-24 02:28:45 +02:00
|
|
|
init.GPIO_Mode = GPIO_Mode_OUT;
|
2016-09-24 02:39:22 +02:00
|
|
|
#elif defined(STM32F10X_MD)
|
|
|
|
init.GPIO_Mode = GPIO_Mode_Out_PP;
|
|
|
|
#else
|
|
|
|
#error Unsupported MCU
|
|
|
|
#endif
|
2016-09-24 02:28:45 +02:00
|
|
|
GPIO_ResetBits(chan->pin.gpio, chan->pin.init.GPIO_Pin);
|
2017-03-27 15:51:43 +02:00
|
|
|
|
|
|
|
GPIO_Init(chan->pin.gpio, &init);
|
2016-09-24 02:28:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 15:51:43 +02:00
|
|
|
static void PIOS_Servo_SetupBank(uint8_t bank_nr)
|
2016-09-24 02:28:45 +02:00
|
|
|
{
|
2017-03-27 15:51:43 +02:00
|
|
|
struct pios_servo_bank *bank = &pios_servo_banks[bank_nr];
|
|
|
|
|
|
|
|
if (!bank->timer) {
|
2016-09-24 02:28:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-27 15:51:43 +02:00
|
|
|
// Setup the timer accordingly
|
|
|
|
switch (bank->mode) {
|
|
|
|
case PIOS_SERVO_BANK_MODE_PWM:
|
|
|
|
case PIOS_SERVO_BANK_MODE_SINGLE_PULSE:
|
|
|
|
TIM_ARRPreloadConfig(bank->timer, ENABLE);
|
|
|
|
TIM_CtrlPWMOutputs(bank->timer, ENABLE);
|
|
|
|
TIM_SelectOnePulseMode(bank->timer, TIM_OPMode_Repetitive);
|
|
|
|
TIM_Cmd(bank->timer, ENABLE);
|
|
|
|
break;
|
|
|
|
default:;
|
|
|
|
// do not manage timers otherwise
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup GPIO/AF
|
2016-09-24 02:28:45 +02:00
|
|
|
for (uint8_t i = 0; (i < servo_cfg->num_channels); i++) {
|
|
|
|
const struct pios_tim_channel *chan = &servo_cfg->channels[i];
|
|
|
|
|
2017-03-27 15:51:43 +02:00
|
|
|
if (chan->timer != bank->timer) { // Not interested in this bank
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-05-18 12:40:53 +02:00
|
|
|
GPIO_InitTypeDef init = chan->pin.init;
|
|
|
|
|
2017-03-27 15:51:43 +02:00
|
|
|
switch (bank->mode) {
|
|
|
|
case PIOS_SERVO_BANK_MODE_PWM:
|
|
|
|
case PIOS_SERVO_BANK_MODE_SINGLE_PULSE:
|
2017-05-22 15:08:04 +02:00
|
|
|
#if defined(STM32F40_41xxx) || defined(STM32F446xx) || defined(STM32F411xE) || defined(STM32F3)
|
2017-03-27 15:51:43 +02:00
|
|
|
GPIO_PinAFConfig(chan->pin.gpio, chan->pin.pin_source, chan->remap);
|
2016-09-28 16:26:38 +02:00
|
|
|
#elif defined(STM32F10X_MD)
|
2017-05-18 12:40:53 +02:00
|
|
|
init.GPIO_Mode = GPIO_Mode_AF_PP;
|
2017-03-27 15:51:43 +02:00
|
|
|
if (chan->remap) {
|
|
|
|
GPIO_PinRemapConfig(chan->remap, ENABLE);
|
|
|
|
}
|
2016-09-28 16:26:38 +02:00
|
|
|
#else
|
|
|
|
#error Unsupported MCU
|
|
|
|
#endif
|
2017-05-18 12:40:53 +02:00
|
|
|
GPIO_Init(chan->pin.gpio, &init);
|
2016-09-24 02:28:45 +02:00
|
|
|
|
2017-03-27 15:51:43 +02:00
|
|
|
/* Set up for output compare function */
|
|
|
|
switch (chan->timer_chan) {
|
|
|
|
case TIM_Channel_1:
|
2017-05-22 15:08:04 +02:00
|
|
|
TIM_OC1Init(chan->timer, (TIM_OCInitTypeDef *)&servo_cfg->tim_oc_init);
|
2017-03-27 15:51:43 +02:00
|
|
|
TIM_OC1PreloadConfig(chan->timer, TIM_OCPreload_Enable);
|
|
|
|
break;
|
|
|
|
case TIM_Channel_2:
|
2017-05-22 15:08:04 +02:00
|
|
|
TIM_OC2Init(chan->timer, (TIM_OCInitTypeDef *)&servo_cfg->tim_oc_init);
|
2017-03-27 15:51:43 +02:00
|
|
|
TIM_OC2PreloadConfig(chan->timer, TIM_OCPreload_Enable);
|
|
|
|
break;
|
|
|
|
case TIM_Channel_3:
|
2017-05-22 15:08:04 +02:00
|
|
|
TIM_OC3Init(chan->timer, (TIM_OCInitTypeDef *)&servo_cfg->tim_oc_init);
|
2017-03-27 15:51:43 +02:00
|
|
|
TIM_OC3PreloadConfig(chan->timer, TIM_OCPreload_Enable);
|
|
|
|
break;
|
|
|
|
case TIM_Channel_4:
|
2017-05-22 15:08:04 +02:00
|
|
|
TIM_OC4Init(chan->timer, (TIM_OCInitTypeDef *)&servo_cfg->tim_oc_init);
|
2017-03-27 15:51:43 +02:00
|
|
|
TIM_OC4PreloadConfig(chan->timer, TIM_OCPreload_Enable);
|
|
|
|
break;
|
|
|
|
}
|
2016-09-24 02:28:45 +02:00
|
|
|
break;
|
2017-03-27 15:51:43 +02:00
|
|
|
|
|
|
|
case PIOS_SERVO_BANK_MODE_DSHOT:
|
|
|
|
{
|
2017-05-22 15:08:04 +02:00
|
|
|
#if defined(STM32F40_41xxx) || defined(STM32F446xx) || defined(STM32F411xE) || defined(STM32F3)
|
2017-03-27 15:51:43 +02:00
|
|
|
init.GPIO_Mode = GPIO_Mode_OUT;
|
|
|
|
#elif defined(STM32F10X_MD)
|
|
|
|
init.GPIO_Mode = GPIO_Mode_Out_PP;
|
|
|
|
#else
|
|
|
|
#error Unsupported MCU
|
|
|
|
#endif
|
|
|
|
GPIO_ResetBits(chan->pin.gpio, chan->pin.init.GPIO_Pin);
|
|
|
|
|
|
|
|
GPIO_Init(chan->pin.gpio, &init);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:;
|
2016-09-24 02:28:45 +02:00
|
|
|
}
|
|
|
|
}
|
2017-03-27 15:51:43 +02:00
|
|
|
}
|
2016-09-24 02:28:45 +02:00
|
|
|
|
2017-03-27 15:51:43 +02:00
|
|
|
extern void PIOS_Servo_Enable()
|
|
|
|
{
|
|
|
|
if (!servo_cfg) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-24 02:28:45 +02:00
|
|
|
|
2017-03-27 15:51:43 +02:00
|
|
|
for (uint8_t i = 0; (i < PIOS_SERVO_BANKS); i++) {
|
|
|
|
PIOS_Servo_SetupBank(i);
|
2016-09-24 02:28:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pios_servo_enabled = true;
|
|
|
|
}
|
|
|
|
|
2017-03-27 15:51:43 +02:00
|
|
|
void PIOS_Servo_DSHot_Rate(uint32_t rate_in_khz)
|
|
|
|
{
|
2017-04-26 16:57:38 +02:00
|
|
|
if (rate_in_khz < 150) {
|
|
|
|
rate_in_khz = 150;
|
|
|
|
}
|
|
|
|
|
2017-03-27 15:51:43 +02:00
|
|
|
uint32_t raw_hz = PIOS_DELAY_GetRawHz();
|
|
|
|
|
|
|
|
uint32_t tmp = raw_hz / rate_in_khz;
|
|
|
|
|
|
|
|
pios_dshot_t0h_raw = (tmp / DSHOT_T0H_DIV) - DSHOT_TIMING_ADJUST;
|
|
|
|
pios_dshot_t1h_raw = (tmp / DSHOT_T1H_DIV) - DSHOT_TIMING_ADJUST;
|
|
|
|
pios_dshot_t_raw = (tmp / 1000) - DSHOT_TIMING_ADJUST;
|
|
|
|
}
|
|
|
|
|
2011-11-01 07:09:55 +01:00
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* Initialise Servos
|
|
|
|
*/
|
|
|
|
int32_t PIOS_Servo_Init(const struct pios_servo_cfg *cfg)
|
2011-11-01 07:09:55 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Store away the requested configuration */
|
|
|
|
servo_cfg = cfg;
|
2015-01-23 23:50:51 +01:00
|
|
|
|
2017-03-27 15:51:43 +02:00
|
|
|
pios_servo_pins = pios_malloc(sizeof(*pios_servo_pins) * cfg->num_channels);
|
|
|
|
PIOS_Assert(pios_servo_pins);
|
|
|
|
|
|
|
|
memset(pios_servo_pins, 0, sizeof(*pios_servo_pins) * cfg->num_channels);
|
|
|
|
|
2017-04-21 14:54:21 +02:00
|
|
|
/* set default dshot timing */
|
2017-03-27 15:51:43 +02:00
|
|
|
PIOS_Servo_DSHot_Rate(300);
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t timer_bank = 0;
|
|
|
|
uint8_t gpio_bank = 0;
|
|
|
|
|
2015-01-23 23:50:51 +01:00
|
|
|
for (uint8_t i = 0; (i < servo_cfg->num_channels); i++) {
|
|
|
|
const struct pios_tim_channel *chan = &servo_cfg->channels[i];
|
|
|
|
bool new = true;
|
|
|
|
/* See if any previous channels use that same timer */
|
|
|
|
for (uint8_t j = 0; (j < i) && new; j++) {
|
|
|
|
new &= chan->timer != servo_cfg->channels[j].timer;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new) {
|
2017-03-27 15:51:43 +02:00
|
|
|
PIOS_Assert(timer_bank < PIOS_SERVO_BANKS);
|
|
|
|
struct pios_servo_bank *bank = &pios_servo_banks[timer_bank];
|
|
|
|
|
|
|
|
|
2015-01-23 23:50:51 +01:00
|
|
|
for (uint8_t j = i; j < servo_cfg->num_channels; j++) {
|
|
|
|
if (servo_cfg->channels[j].timer == chan->timer) {
|
2017-03-27 15:51:43 +02:00
|
|
|
pios_servo_pins[j].bank = bank;
|
|
|
|
pios_servo_pins[j].bank_nr = timer_bank;
|
2015-01-23 23:50:51 +01:00
|
|
|
}
|
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
|
2017-03-27 15:51:43 +02:00
|
|
|
bank->timer = chan->timer;
|
|
|
|
bank->mode = PIOS_SERVO_BANK_MODE_NONE;
|
|
|
|
|
2015-01-23 23:50:51 +01:00
|
|
|
TIM_Cmd(chan->timer, DISABLE);
|
|
|
|
|
2017-03-27 15:51:43 +02:00
|
|
|
timer_bank++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now map gpio banks
|
|
|
|
new = true;
|
|
|
|
for (uint8_t j = 0; (j < i) && new; j++) {
|
|
|
|
new &= chan->pin.gpio != servo_cfg->channels[j].pin.gpio;
|
2015-01-23 23:50:51 +01:00
|
|
|
}
|
2017-03-27 15:51:43 +02:00
|
|
|
|
|
|
|
if (new) {
|
|
|
|
PIOS_Assert(gpio_bank < PIOS_SERVO_GPIO_BANKS);
|
|
|
|
|
|
|
|
for (uint8_t j = i; j < servo_cfg->num_channels; j++) {
|
|
|
|
if (servo_cfg->channels[j].pin.gpio == chan->pin.gpio) {
|
|
|
|
pios_servo_pins[j].gpio_bank = gpio_bank;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if defined(STM32F40_41xxx) || defined(STM32F446xx) || defined(STM32F411xE)
|
|
|
|
pios_servo_bsrr[gpio_bank] = (uint32_t *)&chan->pin.gpio->BSRRL;
|
|
|
|
#else
|
|
|
|
pios_servo_bsrr[gpio_bank] = &chan->pin.gpio->BSRR;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
++gpio_bank;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t dummy_bsrr;
|
|
|
|
|
|
|
|
for (int i = gpio_bank; i < PIOS_SERVO_GPIO_BANKS; ++i) {
|
|
|
|
pios_servo_bsrr[gpio_bank] = &dummy_bsrr;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
2016-09-24 02:28:45 +02:00
|
|
|
PIOS_Servo_Enable();
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return 0;
|
2011-11-01 07:09:55 +01:00
|
|
|
}
|
|
|
|
|
2015-01-23 23:50:51 +01:00
|
|
|
void PIOS_Servo_SetBankMode(uint8_t bank, uint8_t mode)
|
|
|
|
{
|
|
|
|
PIOS_Assert(bank < PIOS_SERVO_BANKS);
|
2017-03-27 15:51:43 +02:00
|
|
|
pios_servo_banks[bank].mode = mode;
|
2015-01-23 23:50:51 +01:00
|
|
|
|
2016-09-24 02:28:45 +02:00
|
|
|
if (!pios_servo_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-09 00:54:01 +01:00
|
|
|
|
2017-03-27 15:51:43 +02:00
|
|
|
PIOS_Servo_SetupBank(bank);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PIOS_Servo_DShot_Update()
|
|
|
|
{
|
|
|
|
uint32_t next;
|
|
|
|
uint32_t data[PIOS_SERVO_GPIO_BANKS];
|
|
|
|
uint16_t pins[PIOS_SERVO_GPIO_BANKS];
|
|
|
|
uint16_t buffer[DSHOT_NUM_BITS][PIOS_SERVO_GPIO_BANKS];
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < PIOS_SERVO_GPIO_BANKS; ++i) {
|
|
|
|
pins[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_dshot = false;
|
|
|
|
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
|
|
|
|
for (uint8_t i = 0; (i < servo_cfg->num_channels); i++) {
|
|
|
|
struct pios_servo_pin *pin = &pios_servo_pins[i];
|
|
|
|
|
|
|
|
if (pin->bank->mode != PIOS_SERVO_BANK_MODE_DSHOT) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
has_dshot = true;
|
|
|
|
|
|
|
|
uint16_t payload = pin->value;
|
|
|
|
if (payload > 2047) {
|
|
|
|
payload = 2047;
|
|
|
|
}
|
|
|
|
|
|
|
|
payload <<= 5;
|
|
|
|
|
|
|
|
payload |= ((payload >> 4) & 0xf) ^
|
|
|
|
((payload >> 8) & 0xf) ^
|
|
|
|
((payload >> 12) & 0xf);
|
|
|
|
|
|
|
|
uint16_t gpio_pin = servo_cfg->channels[i].pin.init.GPIO_Pin;
|
|
|
|
|
|
|
|
for (int j = 0; j < DSHOT_NUM_BITS; ++j) {
|
|
|
|
if (!(payload & 0x8000)) {
|
|
|
|
buffer[j][pin->gpio_bank] |= gpio_pin;
|
|
|
|
}
|
|
|
|
payload <<= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pins[pin->gpio_bank] |= gpio_pin;
|
|
|
|
|
|
|
|
pin->value = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_dshot) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PIOS_IRQ_Disable();
|
|
|
|
|
|
|
|
uint32_t start = PIOS_DELAY_GetRaw();
|
|
|
|
|
|
|
|
for (int i = 0; i < DSHOT_NUM_BITS; ++i) {
|
|
|
|
// single bit:
|
|
|
|
|
|
|
|
COMPILER_BARRIER();
|
|
|
|
// 1. write 3x BSRR to set gpio high
|
|
|
|
for (int j = 0; j < PIOS_SERVO_GPIO_BANKS; ++j) {
|
|
|
|
*(pios_servo_bsrr[j]) = (uint32_t)pins[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prep data
|
|
|
|
for (int j = 0; j < PIOS_SERVO_GPIO_BANKS; ++j) {
|
|
|
|
data[j] = buffer[i][j] << 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. wait until T0H, write 3x BSRR to clear whatever bits are set to 0
|
|
|
|
next = start + pios_dshot_t0h_raw;
|
|
|
|
while ((next - PIOS_DELAY_GetRaw()) < pios_dshot_t0h_raw) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
COMPILER_BARRIER();
|
|
|
|
for (int j = 0; j < PIOS_SERVO_GPIO_BANKS; ++j) {
|
|
|
|
*(pios_servo_bsrr[j]) = data[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prep data
|
|
|
|
for (int j = 0; j < PIOS_SERVO_GPIO_BANKS; ++j) {
|
|
|
|
data[j] = (uint32_t)pins[j] << 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. wait until T1H, then write 3x BSRR to set all to low
|
|
|
|
next = start + pios_dshot_t1h_raw;
|
|
|
|
while ((next - PIOS_DELAY_GetRaw()) < pios_dshot_t1h_raw) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
COMPILER_BARRIER();
|
|
|
|
for (int j = 0; j < PIOS_SERVO_GPIO_BANKS; ++j) {
|
|
|
|
*(pios_servo_bsrr[j]) = data[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4. wait until Tend
|
|
|
|
start += pios_dshot_t_raw;
|
|
|
|
while ((start - PIOS_DELAY_GetRaw()) < pios_dshot_t_raw) {
|
|
|
|
;
|
|
|
|
}
|
2015-01-23 23:50:51 +01:00
|
|
|
}
|
2017-03-27 15:51:43 +02:00
|
|
|
|
|
|
|
PIOS_IRQ_Enable();
|
2015-01-23 23:50:51 +01:00
|
|
|
}
|
|
|
|
|
2015-01-25 21:25:14 +01:00
|
|
|
|
2015-01-23 23:50:51 +01:00
|
|
|
void PIOS_Servo_Update()
|
|
|
|
{
|
2016-09-24 02:28:45 +02:00
|
|
|
if (!pios_servo_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-23 23:50:51 +01:00
|
|
|
for (uint8_t i = 0; (i < PIOS_SERVO_BANKS); i++) {
|
2017-03-27 15:51:43 +02:00
|
|
|
struct pios_servo_bank *bank = &pios_servo_banks[i];
|
|
|
|
if (bank->timer && (bank->mode == PIOS_SERVO_BANK_MODE_SINGLE_PULSE)) {
|
2015-02-02 02:58:22 +01:00
|
|
|
// a pulse to be generated is longer than cycle period. skip this update.
|
2017-03-27 15:51:43 +02:00
|
|
|
if (TIM_GetCounter((TIM_TypeDef *)bank->timer) > (uint32_t)(bank->next_update + PIOS_SERVO_SAFE_MARGIN)) {
|
|
|
|
TIM_GenerateEvent((TIM_TypeDef *)bank->timer, TIM_EventSource_Update);
|
|
|
|
bank->next_update = bank->max_pulse;
|
2015-02-02 02:58:22 +01:00
|
|
|
}
|
2015-01-29 21:20:29 +01:00
|
|
|
}
|
2017-03-27 15:51:43 +02:00
|
|
|
bank->max_pulse = 0;
|
2015-01-29 21:20:29 +01:00
|
|
|
}
|
2017-03-27 15:51:43 +02:00
|
|
|
|
2015-01-29 21:20:29 +01:00
|
|
|
for (uint8_t i = 0; (i < servo_cfg->num_channels); i++) {
|
2017-03-27 15:51:43 +02:00
|
|
|
if (pios_servo_pins[i].bank->mode == PIOS_SERVO_BANK_MODE_SINGLE_PULSE) {
|
2015-01-29 21:20:29 +01:00
|
|
|
/* Update the position */
|
|
|
|
const struct pios_tim_channel *chan = &servo_cfg->channels[i];
|
|
|
|
|
|
|
|
switch (chan->timer_chan) {
|
|
|
|
case TIM_Channel_1:
|
|
|
|
TIM_SetCompare1(chan->timer, 0);
|
|
|
|
break;
|
|
|
|
case TIM_Channel_2:
|
|
|
|
TIM_SetCompare2(chan->timer, 0);
|
|
|
|
break;
|
|
|
|
case TIM_Channel_3:
|
|
|
|
TIM_SetCompare3(chan->timer, 0);
|
|
|
|
break;
|
|
|
|
case TIM_Channel_4:
|
|
|
|
TIM_SetCompare4(chan->timer, 0);
|
|
|
|
break;
|
|
|
|
}
|
2015-01-23 23:50:51 +01:00
|
|
|
}
|
|
|
|
}
|
2017-03-27 15:51:43 +02:00
|
|
|
|
|
|
|
PIOS_Servo_DShot_Update();
|
2015-01-23 23:50:51 +01:00
|
|
|
}
|
2011-11-01 07:09:55 +01:00
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* Set the servo update rate (Max 500Hz)
|
|
|
|
* \param[in] array of rates in Hz
|
2015-01-29 21:07:21 +01:00
|
|
|
* \param[in] array of timer clocks in Hz
|
2013-05-19 16:37:30 +02:00
|
|
|
* \param[in] maximum number of banks
|
|
|
|
*/
|
2015-01-29 21:07:21 +01:00
|
|
|
void PIOS_Servo_SetHz(const uint16_t *speeds, const uint32_t *clock, uint8_t banks)
|
2011-11-01 07:09:55 +01:00
|
|
|
{
|
2015-01-23 23:50:51 +01:00
|
|
|
PIOS_Assert(banks <= PIOS_SERVO_BANKS);
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!servo_cfg) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure = servo_cfg->tim_base_init;
|
|
|
|
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
|
|
|
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
|
|
|
|
2015-01-29 21:07:21 +01:00
|
|
|
for (uint8_t i = 0; i < banks && i < PIOS_SERVO_BANKS; i++) {
|
2017-03-27 15:51:43 +02:00
|
|
|
const TIM_TypeDef *timer = pios_servo_banks[i].timer;
|
2015-01-23 23:50:51 +01:00
|
|
|
if (timer) {
|
2015-01-29 21:07:21 +01:00
|
|
|
uint32_t new_clock = PIOS_SERVO_TIMER_CLOCK;
|
|
|
|
if (clock[i]) {
|
|
|
|
new_clock = clock[i];
|
|
|
|
}
|
2016-09-24 02:39:22 +02:00
|
|
|
|
2017-01-01 16:53:06 +01:00
|
|
|
uint32_t timer_clock;
|
2016-09-24 02:39:22 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Choose the correct prescaler value for the APB the timer is attached
|
2016-09-24 02:39:22 +02:00
|
|
|
|
2017-05-22 15:08:04 +02:00
|
|
|
#if defined(STM32F10X_MD) || defined(STM32F3)
|
|
|
|
// F1 & F3 have both timer clock domains running at master clock speed
|
2017-01-01 16:53:06 +01:00
|
|
|
timer_clock = PIOS_MASTER_CLOCK;
|
2016-09-27 01:36:00 +02:00
|
|
|
#elif defined(STM32F40_41xxx) || defined(STM32F446xx) || defined(STM32F411xE)
|
2015-01-23 23:50:51 +01:00
|
|
|
if (timer == TIM1 || timer == TIM8 || timer == TIM9 || timer == TIM10 || timer == TIM11) {
|
2017-01-01 16:53:06 +01:00
|
|
|
timer_clock = PIOS_PERIPHERAL_APB2_CLOCK;
|
2013-05-19 16:37:30 +02:00
|
|
|
} else {
|
2017-01-01 16:53:06 +01:00
|
|
|
timer_clock = PIOS_PERIPHERAL_APB1_CLOCK;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2016-09-24 02:39:22 +02:00
|
|
|
#else
|
|
|
|
#error Unsupported MCU
|
|
|
|
#endif
|
2017-01-01 16:53:06 +01:00
|
|
|
TIM_TimeBaseStructure.TIM_Prescaler = (timer_clock / new_clock) - 1;
|
2016-09-24 02:39:22 +02:00
|
|
|
TIM_TimeBaseStructure.TIM_Period = ((new_clock / speeds[i]) - 1);
|
2015-01-23 23:50:51 +01:00
|
|
|
TIM_TimeBaseInit((TIM_TypeDef *)timer, &TIM_TimeBaseStructure);
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
}
|
2011-11-01 07:09:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* Set servo position
|
|
|
|
* \param[in] Servo Servo number (0-7)
|
|
|
|
* \param[in] Position Servo position in microseconds
|
|
|
|
*/
|
2011-11-01 10:39:51 +01:00
|
|
|
void PIOS_Servo_Set(uint8_t servo, uint16_t position)
|
2011-11-01 07:09:55 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Make sure servo exists */
|
2016-09-24 02:28:45 +02:00
|
|
|
if (!pios_servo_enabled || !servo_cfg || servo >= servo_cfg->num_channels) {
|
2013-05-19 16:37:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-02 02:58:22 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Update the position */
|
2017-03-27 15:51:43 +02:00
|
|
|
|
|
|
|
pios_servo_pins[servo].value = position;
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
const struct pios_tim_channel *chan = &servo_cfg->channels[servo];
|
2017-03-27 15:51:43 +02:00
|
|
|
struct pios_servo_bank *bank = pios_servo_pins[servo].bank;
|
2015-02-02 02:58:22 +01:00
|
|
|
|
2017-03-27 15:51:43 +02:00
|
|
|
if ((bank->mode == PIOS_SERVO_BANK_MODE_SINGLE_PULSE) || (bank->mode == PIOS_SERVO_BANK_MODE_PWM)) {
|
|
|
|
uint16_t val = position;
|
|
|
|
uint16_t margin = chan->timer->ARR / 50; // Leave 2% of period as margin to prevent overlaps
|
|
|
|
if (val > (chan->timer->ARR - margin)) {
|
|
|
|
val = chan->timer->ARR - margin;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bank->max_pulse < val) {
|
|
|
|
bank->max_pulse = val;
|
|
|
|
}
|
|
|
|
switch (chan->timer_chan) {
|
|
|
|
case TIM_Channel_1:
|
|
|
|
TIM_SetCompare1(chan->timer, val);
|
|
|
|
break;
|
|
|
|
case TIM_Channel_2:
|
|
|
|
TIM_SetCompare2(chan->timer, val);
|
|
|
|
break;
|
|
|
|
case TIM_Channel_3:
|
|
|
|
TIM_SetCompare3(chan->timer, val);
|
|
|
|
break;
|
|
|
|
case TIM_Channel_4:
|
|
|
|
TIM_SetCompare4(chan->timer, val);
|
|
|
|
break;
|
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2011-11-01 07:09:55 +01:00
|
|
|
}
|
2013-03-15 19:42:54 +01:00
|
|
|
|
2015-01-23 23:50:51 +01:00
|
|
|
uint8_t PIOS_Servo_GetPinBank(uint8_t pin)
|
|
|
|
{
|
|
|
|
if (pin < servo_cfg->num_channels) {
|
2017-03-27 15:51:43 +02:00
|
|
|
return pios_servo_pins[pin].bank_nr;
|
2015-01-23 23:50:51 +01:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 23:17:04 +02:00
|
|
|
const struct pios_servo_cfg *PIOS_Servo_GetConfig()
|
2016-09-24 02:28:45 +02:00
|
|
|
{
|
|
|
|
return servo_cfg;
|
|
|
|
}
|
|
|
|
|
2013-03-15 19:42:54 +01:00
|
|
|
#endif /* PIOS_INCLUDE_SERVO */
|