mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2024-12-01 09:24:10 +01:00
Merged in mindnever/librepilot/LP-415_pios_servo_Add_functions_to_support_esc_passthrough (pull request #334)
LP-415 pios servo add functions to support esc passthrough
This commit is contained in:
commit
c2cf44f8d8
@ -96,6 +96,7 @@ SRC += $(PIOSCOMMON)/pios_exbus.c
|
||||
SRC += $(PIOSCOMMON)/pios_ibus.c
|
||||
SRC += $(PIOSCOMMON)/pios_sdcard.c
|
||||
SRC += $(PIOSCOMMON)/pios_sensors.c
|
||||
SRC += $(PIOSCOMMON)/pios_servo.c
|
||||
SRC += $(PIOSCOMMON)/pios_openlrs.c
|
||||
SRC += $(PIOSCOMMON)/pios_openlrs_rcvr.c
|
||||
|
||||
|
@ -7,7 +7,8 @@
|
||||
* @{
|
||||
*
|
||||
* @file pios_servo.c
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
|
||||
* @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016.
|
||||
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
|
||||
* @brief RC Servo routines (STM32 dependent)
|
||||
* @see The GNU Public License (GPL) Version 3
|
||||
*
|
||||
@ -50,19 +51,98 @@ static TIM_TypeDef *pios_servo_bank_timer[PIOS_SERVO_BANKS] = { 0 };
|
||||
// index of bank used for each pin
|
||||
static uint8_t *pios_servo_pin_bank;
|
||||
|
||||
static bool pios_servo_enabled = true;
|
||||
|
||||
#define PIOS_SERVO_TIMER_CLOCK 1000000
|
||||
#define PIOS_SERVO_SAFE_MARGIN 50
|
||||
|
||||
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;
|
||||
|
||||
#if defined(STM32F40_41xxx) || defined(STM32F446xx) || defined(STM32F411xE)
|
||||
init.GPIO_Mode = GPIO_Mode_OUT;
|
||||
#elif defined(STM32F10X_MD)
|
||||
init.GPIO_Mode = GPIO_Mode_Out_PP;
|
||||
#else
|
||||
#error Unsupported MCU
|
||||
#endif
|
||||
GPIO_Init(chan->pin.gpio, &init);
|
||||
|
||||
GPIO_ResetBits(chan->pin.gpio, chan->pin.init.GPIO_Pin);
|
||||
}
|
||||
}
|
||||
|
||||
extern void PIOS_Servo_Enable()
|
||||
{
|
||||
if (!servo_cfg) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (uint8_t i = 0; (i < servo_cfg->num_channels); i++) {
|
||||
const struct pios_tim_channel *chan = &servo_cfg->channels[i];
|
||||
|
||||
GPIO_Init(chan->pin.gpio, &chan->pin.init);
|
||||
#if defined(STM32F40_41xxx) || defined(STM32F446xx) || defined(STM32F411xE)
|
||||
GPIO_PinAFConfig(chan->pin.gpio, chan->pin.pin_source, chan->remap);
|
||||
#elif defined(STM32F10X_MD)
|
||||
if (chan->remap) {
|
||||
GPIO_PinRemapConfig(chan->remap, ENABLE);
|
||||
}
|
||||
#else
|
||||
#error Unsupported MCU
|
||||
#endif
|
||||
|
||||
/* Set up for output compare function */
|
||||
switch (chan->timer_chan) {
|
||||
case TIM_Channel_1:
|
||||
TIM_OC1Init(chan->timer, &servo_cfg->tim_oc_init);
|
||||
TIM_OC1PreloadConfig(chan->timer, TIM_OCPreload_Enable);
|
||||
break;
|
||||
case TIM_Channel_2:
|
||||
TIM_OC2Init(chan->timer, &servo_cfg->tim_oc_init);
|
||||
TIM_OC2PreloadConfig(chan->timer, TIM_OCPreload_Enable);
|
||||
break;
|
||||
case TIM_Channel_3:
|
||||
TIM_OC3Init(chan->timer, &servo_cfg->tim_oc_init);
|
||||
TIM_OC3PreloadConfig(chan->timer, TIM_OCPreload_Enable);
|
||||
break;
|
||||
case TIM_Channel_4:
|
||||
TIM_OC4Init(chan->timer, &servo_cfg->tim_oc_init);
|
||||
TIM_OC4PreloadConfig(chan->timer, TIM_OCPreload_Enable);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (uint8_t i = 0; (i < PIOS_SERVO_BANKS); i++) {
|
||||
TIM_TypeDef *timer = pios_servo_bank_timer[i];
|
||||
|
||||
if (timer && (pios_servo_bank_mode[i] != PIOS_SERVO_BANK_MODE_NONE)) {
|
||||
TIM_SelectOnePulseMode(timer, TIM_OPMode_Repetitive);
|
||||
TIM_Cmd(timer, ENABLE);
|
||||
}
|
||||
}
|
||||
|
||||
pios_servo_enabled = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise Servos
|
||||
*/
|
||||
int32_t PIOS_Servo_Init(const struct pios_servo_cfg *cfg)
|
||||
{
|
||||
uint32_t tim_id;
|
||||
|
||||
if (PIOS_TIM_InitChannels(&tim_id, cfg->channels, cfg->num_channels, NULL, 0)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Store away the requested configuration */
|
||||
servo_cfg = cfg;
|
||||
pios_servo_pin_bank = pios_malloc(sizeof(uint8_t) * cfg->num_channels);
|
||||
@ -91,28 +171,10 @@ int32_t PIOS_Servo_Init(const struct pios_servo_cfg *cfg)
|
||||
|
||||
bank++;
|
||||
}
|
||||
|
||||
/* Set up for output compare function */
|
||||
switch (chan->timer_chan) {
|
||||
case TIM_Channel_1:
|
||||
TIM_OC1Init(chan->timer, &servo_cfg->tim_oc_init);
|
||||
TIM_OC1PreloadConfig(chan->timer, TIM_OCPreload_Enable);
|
||||
break;
|
||||
case TIM_Channel_2:
|
||||
TIM_OC2Init(chan->timer, &servo_cfg->tim_oc_init);
|
||||
TIM_OC2PreloadConfig(chan->timer, TIM_OCPreload_Enable);
|
||||
break;
|
||||
case TIM_Channel_3:
|
||||
TIM_OC3Init(chan->timer, &servo_cfg->tim_oc_init);
|
||||
TIM_OC3PreloadConfig(chan->timer, TIM_OCPreload_Enable);
|
||||
break;
|
||||
case TIM_Channel_4:
|
||||
TIM_OC4Init(chan->timer, &servo_cfg->tim_oc_init);
|
||||
TIM_OC4PreloadConfig(chan->timer, TIM_OCPreload_Enable);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
PIOS_Servo_Enable();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -121,28 +183,11 @@ void PIOS_Servo_SetBankMode(uint8_t bank, uint8_t mode)
|
||||
PIOS_Assert(bank < PIOS_SERVO_BANKS);
|
||||
pios_servo_bank_mode[bank] = mode;
|
||||
|
||||
if (pios_servo_bank_timer[bank]) {
|
||||
for (uint8_t i = 0; (i < servo_cfg->num_channels); i++) {
|
||||
if (pios_servo_pin_bank[i] == bank) {
|
||||
const struct pios_tim_channel *chan = &servo_cfg->channels[i];
|
||||
/* Set up for output compare function */
|
||||
switch (chan->timer_chan) {
|
||||
case TIM_Channel_1:
|
||||
TIM_OC1PolarityConfig(chan->timer, TIM_OCPolarity_High);
|
||||
break;
|
||||
case TIM_Channel_2:
|
||||
TIM_OC2PolarityConfig(chan->timer, TIM_OCPolarity_High);
|
||||
break;
|
||||
case TIM_Channel_3:
|
||||
TIM_OC3PolarityConfig(chan->timer, TIM_OCPolarity_High);
|
||||
break;
|
||||
case TIM_Channel_4:
|
||||
TIM_OC4PolarityConfig(chan->timer, TIM_OCPolarity_High);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!pios_servo_enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (pios_servo_bank_timer[bank]) {
|
||||
// Setup the timer accordingly
|
||||
TIM_SelectOnePulseMode(pios_servo_bank_timer[bank], TIM_OPMode_Repetitive);
|
||||
TIM_Cmd(pios_servo_bank_timer[bank], ENABLE);
|
||||
@ -152,6 +197,10 @@ void PIOS_Servo_SetBankMode(uint8_t bank, uint8_t mode)
|
||||
|
||||
void PIOS_Servo_Update()
|
||||
{
|
||||
if (!pios_servo_enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (uint8_t i = 0; (i < PIOS_SERVO_BANKS); i++) {
|
||||
const TIM_TypeDef *timer = pios_servo_bank_timer[i];
|
||||
if (timer && pios_servo_bank_mode[i] == PIOS_SERVO_BANK_MODE_SINGLE_PULSE) {
|
||||
@ -211,13 +260,28 @@ void PIOS_Servo_SetHz(const uint16_t *speeds, const uint32_t *clock, uint8_t ban
|
||||
if (clock[i]) {
|
||||
new_clock = clock[i];
|
||||
}
|
||||
|
||||
uint32_t apb_clock;
|
||||
|
||||
// Choose the correct prescaler value for the APB the timer is attached
|
||||
if (timer == TIM1 || timer == TIM8 || timer == TIM9 || timer == TIM10 || timer == TIM11) {
|
||||
TIM_TimeBaseStructure.TIM_Prescaler = (PIOS_PERIPHERAL_APB2_CLOCK / new_clock) - 1;
|
||||
|
||||
#if defined(STM32F10X_MD)
|
||||
if (timer == TIM1 || timer == TIM8) {
|
||||
apb_clock = PIOS_PERIPHERAL_APB2_CLOCK;
|
||||
} else {
|
||||
TIM_TimeBaseStructure.TIM_Prescaler = (PIOS_PERIPHERAL_APB1_CLOCK / new_clock) - 1;
|
||||
apb_clock = PIOS_PERIPHERAL_APB1_CLOCK;
|
||||
}
|
||||
TIM_TimeBaseStructure.TIM_Period = ((new_clock / speeds[i]) - 1);
|
||||
#elif defined(STM32F40_41xxx) || defined(STM32F446xx) || defined(STM32F411xE)
|
||||
if (timer == TIM1 || timer == TIM8 || timer == TIM9 || timer == TIM10 || timer == TIM11) {
|
||||
apb_clock = PIOS_PERIPHERAL_APB2_CLOCK;
|
||||
} else {
|
||||
apb_clock = PIOS_PERIPHERAL_APB1_CLOCK;
|
||||
}
|
||||
#else
|
||||
#error Unsupported MCU
|
||||
#endif
|
||||
TIM_TimeBaseStructure.TIM_Prescaler = (apb_clock / new_clock) - 1;
|
||||
TIM_TimeBaseStructure.TIM_Period = ((new_clock / speeds[i]) - 1);
|
||||
TIM_TimeBaseInit((TIM_TypeDef *)timer, &TIM_TimeBaseStructure);
|
||||
}
|
||||
}
|
||||
@ -231,7 +295,7 @@ void PIOS_Servo_SetHz(const uint16_t *speeds, const uint32_t *clock, uint8_t ban
|
||||
void PIOS_Servo_Set(uint8_t servo, uint16_t position)
|
||||
{
|
||||
/* Make sure servo exists */
|
||||
if (!servo_cfg || servo >= servo_cfg->num_channels) {
|
||||
if (!pios_servo_enabled || !servo_cfg || servo >= servo_cfg->num_channels) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -273,4 +337,9 @@ uint8_t PIOS_Servo_GetPinBank(uint8_t pin)
|
||||
}
|
||||
}
|
||||
|
||||
const struct pios_servo_cfg *PIOS_Servo_GetConfig()
|
||||
{
|
||||
return servo_cfg;
|
||||
}
|
||||
|
||||
#endif /* PIOS_INCLUDE_SERVO */
|
@ -6,7 +6,8 @@
|
||||
* @{
|
||||
*
|
||||
* @file pios_servo.h
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
||||
* @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016.
|
||||
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
||||
* @brief RC Servo functions header.
|
||||
* @see The GNU Public License (GPL) Version 3
|
||||
*
|
||||
@ -32,8 +33,9 @@
|
||||
|
||||
/* Global types */
|
||||
enum pios_servo_bank_mode {
|
||||
PIOS_SERVO_BANK_MODE_PWM = 0,
|
||||
PIOS_SERVO_BANK_MODE_SINGLE_PULSE = 1
|
||||
PIOS_SERVO_BANK_MODE_NONE = 0,
|
||||
PIOS_SERVO_BANK_MODE_PWM = 1,
|
||||
PIOS_SERVO_BANK_MODE_SINGLE_PULSE = 2
|
||||
};
|
||||
/* Public Functions */
|
||||
extern void PIOS_Servo_SetHz(const uint16_t *speeds, const uint32_t *clock, uint8_t banks);
|
||||
@ -42,6 +44,10 @@ extern void PIOS_Servo_Update();
|
||||
extern void PIOS_Servo_SetBankMode(uint8_t bank, uint8_t mode);
|
||||
extern uint8_t PIOS_Servo_GetPinBank(uint8_t pin);
|
||||
|
||||
/* ESC Bridge support */
|
||||
extern void PIOS_Servo_Disable();
|
||||
extern void PIOS_Servo_Enable();
|
||||
|
||||
#endif /* PIOS_SERVO_H */
|
||||
|
||||
/**
|
||||
|
@ -7,7 +7,8 @@
|
||||
* @{
|
||||
*
|
||||
* @file pios_servo_priv.h
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
||||
* @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016.
|
||||
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
||||
* @brief Servo private structures.
|
||||
* @see The GNU Public License (GPL) Version 3
|
||||
*
|
||||
@ -45,6 +46,7 @@ struct pios_servo_cfg {
|
||||
};
|
||||
|
||||
extern int32_t PIOS_Servo_Init(const struct pios_servo_cfg *cfg);
|
||||
const struct pios_servo_cfg *PIOS_Servo_GetConfig();
|
||||
|
||||
#endif /* PIOS_SERVO_PRIV_H */
|
||||
|
||||
|
@ -1,283 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @addtogroup PIOS PIOS Core hardware abstraction layer
|
||||
* @{
|
||||
* @addtogroup PIOS_SERVO RC Servo Functions
|
||||
* @brief Code to do set RC servo output
|
||||
* @{
|
||||
*
|
||||
* @file pios_servo.c
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
||||
* @brief RC Servo routines (STM32 dependent)
|
||||
* @see The GNU Public License (GPL) Version 3
|
||||
*
|
||||
*****************************************************************************/
|
||||
/*
|
||||
* 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
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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
|
||||
* for more details.
|
||||
*
|
||||
* 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.,
|
||||
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include "pios.h"
|
||||
|
||||
#ifdef PIOS_INCLUDE_SERVO
|
||||
|
||||
#include "pios_servo_priv.h"
|
||||
#include "pios_tim_priv.h"
|
||||
|
||||
/* Private Function Prototypes */
|
||||
|
||||
static const struct pios_servo_cfg *servo_cfg;
|
||||
|
||||
// determine if the related timer will work in synchronous (or OneShot/OneShot125) One Pulse mode.
|
||||
static uint8_t pios_servo_bank_mode[PIOS_SERVO_BANKS] = { 0 };
|
||||
// used to skip updates when pulse length is higher than update cycle
|
||||
static uint16_t pios_servo_bank_next_update[PIOS_SERVO_BANKS] = { 0 };
|
||||
static uint16_t pios_servo_bank_max_pulse[PIOS_SERVO_BANKS] = { 0 };
|
||||
// timer associated to each bank
|
||||
static TIM_TypeDef *pios_servo_bank_timer[PIOS_SERVO_BANKS] = { 0 };
|
||||
|
||||
// index of bank used for each pin
|
||||
static uint8_t *pios_servo_pin_bank;
|
||||
|
||||
#define PIOS_SERVO_TIMER_CLOCK 1000000
|
||||
#define PIOS_SERVO_SAFE_MARGIN 50
|
||||
/**
|
||||
* Initialise Servos
|
||||
*/
|
||||
int32_t PIOS_Servo_Init(const struct pios_servo_cfg *cfg)
|
||||
{
|
||||
uint32_t tim_id;
|
||||
|
||||
if (PIOS_TIM_InitChannels(&tim_id, cfg->channels, cfg->num_channels, NULL, 0)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Store away the requested configuration */
|
||||
servo_cfg = cfg;
|
||||
pios_servo_pin_bank = pios_malloc(sizeof(uint8_t) * cfg->num_channels);
|
||||
|
||||
uint8_t bank = 0;
|
||||
/* Configure the channels to be in output compare mode */
|
||||
for (uint8_t i = 0; i < 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) {
|
||||
PIOS_Assert(bank < PIOS_SERVO_BANKS);
|
||||
for (uint8_t j = i; j < servo_cfg->num_channels; j++) {
|
||||
if (servo_cfg->channels[j].timer == chan->timer) {
|
||||
pios_servo_pin_bank[j] = bank;
|
||||
}
|
||||
}
|
||||
pios_servo_bank_timer[bank] = chan->timer;
|
||||
|
||||
PIOS_Assert(bank < PIOS_SERVO_BANKS);
|
||||
|
||||
for (uint8_t j = i; j < servo_cfg->num_channels; j++) {
|
||||
if (servo_cfg->channels[j].timer == chan->timer) {
|
||||
pios_servo_pin_bank[j] = bank;
|
||||
}
|
||||
}
|
||||
pios_servo_bank_timer[bank] = chan->timer;
|
||||
|
||||
TIM_ARRPreloadConfig(chan->timer, ENABLE);
|
||||
TIM_CtrlPWMOutputs(chan->timer, ENABLE);
|
||||
TIM_Cmd(chan->timer, ENABLE);
|
||||
|
||||
bank++;
|
||||
}
|
||||
|
||||
/* Set up for output compare function */
|
||||
switch (chan->timer_chan) {
|
||||
case TIM_Channel_1:
|
||||
TIM_OC1Init(chan->timer, &cfg->tim_oc_init);
|
||||
TIM_OC1PreloadConfig(chan->timer, TIM_OCPreload_Enable);
|
||||
break;
|
||||
case TIM_Channel_2:
|
||||
TIM_OC2Init(chan->timer, &cfg->tim_oc_init);
|
||||
TIM_OC2PreloadConfig(chan->timer, TIM_OCPreload_Enable);
|
||||
break;
|
||||
case TIM_Channel_3:
|
||||
TIM_OC3Init(chan->timer, &cfg->tim_oc_init);
|
||||
TIM_OC3PreloadConfig(chan->timer, TIM_OCPreload_Enable);
|
||||
break;
|
||||
case TIM_Channel_4:
|
||||
TIM_OC4Init(chan->timer, &cfg->tim_oc_init);
|
||||
TIM_OC4PreloadConfig(chan->timer, TIM_OCPreload_Enable);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the servo update rate (Max 500Hz)
|
||||
* \param[in] array of rates in Hz
|
||||
* \param[in] array of timer clocks in Hz
|
||||
* \param[in] maximum number of banks
|
||||
*/
|
||||
void PIOS_Servo_SetHz(const uint16_t *speeds, const uint32_t *clock, uint8_t banks)
|
||||
{
|
||||
PIOS_Assert(banks <= PIOS_SERVO_BANKS);
|
||||
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;
|
||||
|
||||
for (uint8_t i = 0; i < banks && i < PIOS_SERVO_BANKS; i++) {
|
||||
const TIM_TypeDef *timer = pios_servo_bank_timer[i];
|
||||
if (timer) {
|
||||
uint32_t new_clock = PIOS_SERVO_TIMER_CLOCK;
|
||||
if (clock[i]) {
|
||||
new_clock = clock[i];
|
||||
}
|
||||
TIM_TimeBaseStructure.TIM_Prescaler = (PIOS_MASTER_CLOCK / new_clock) - 1;
|
||||
TIM_TimeBaseStructure.TIM_Period = ((new_clock / speeds[i]) - 1);
|
||||
|
||||
TIM_TimeBaseInit((TIM_TypeDef *)timer, &TIM_TimeBaseStructure);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set servo position
|
||||
* \param[in] Servo Servo number (0-7)
|
||||
* \param[in] Position Servo position in microseconds
|
||||
*/
|
||||
void PIOS_Servo_Set(uint8_t servo, uint16_t position)
|
||||
{
|
||||
/* Make sure servo exists */
|
||||
if (!servo_cfg || servo >= servo_cfg->num_channels) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/* Update the position */
|
||||
const struct pios_tim_channel *chan = &servo_cfg->channels[servo];
|
||||
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;
|
||||
}
|
||||
|
||||
uint8_t bank = pios_servo_pin_bank[servo];
|
||||
if (pios_servo_bank_max_pulse[bank] < val) {
|
||||
pios_servo_bank_max_pulse[bank] = 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;
|
||||
}
|
||||
}
|
||||
|
||||
void PIOS_Servo_Update()
|
||||
{
|
||||
for (uint8_t i = 0; (i < PIOS_SERVO_BANKS); i++) {
|
||||
const TIM_TypeDef *timer = pios_servo_bank_timer[i];
|
||||
if (timer && pios_servo_bank_mode[i] == PIOS_SERVO_BANK_MODE_SINGLE_PULSE) {
|
||||
// a pulse to be generated is longer than cycle period. skip this update.
|
||||
if (TIM_GetCounter((TIM_TypeDef *)timer) > (uint32_t)(pios_servo_bank_next_update[i] + PIOS_SERVO_SAFE_MARGIN)) {
|
||||
TIM_GenerateEvent((TIM_TypeDef *)timer, TIM_EventSource_Update);
|
||||
pios_servo_bank_next_update[i] = pios_servo_bank_max_pulse[i];
|
||||
}
|
||||
}
|
||||
pios_servo_bank_max_pulse[i] = 0;
|
||||
}
|
||||
for (uint8_t i = 0; (i < servo_cfg->num_channels); i++) {
|
||||
uint8_t bank = pios_servo_pin_bank[i];
|
||||
uint8_t mode = pios_servo_bank_mode[bank];
|
||||
if (mode == PIOS_SERVO_BANK_MODE_SINGLE_PULSE) {
|
||||
/* 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PIOS_Servo_SetBankMode(uint8_t bank, uint8_t mode)
|
||||
{
|
||||
PIOS_Assert(bank < PIOS_SERVO_BANKS);
|
||||
pios_servo_bank_mode[bank] = mode;
|
||||
|
||||
if (pios_servo_bank_timer[bank]) {
|
||||
for (uint8_t i = 0; (i < servo_cfg->num_channels); i++) {
|
||||
if (pios_servo_pin_bank[i] == bank) {
|
||||
const struct pios_tim_channel *chan = &servo_cfg->channels[i];
|
||||
/* Set up for output compare function */
|
||||
switch (chan->timer_chan) {
|
||||
case TIM_Channel_1:
|
||||
TIM_OC1PolarityConfig(chan->timer, TIM_OCPolarity_High);
|
||||
break;
|
||||
case TIM_Channel_2:
|
||||
TIM_OC2PolarityConfig(chan->timer, TIM_OCPolarity_High);
|
||||
break;
|
||||
case TIM_Channel_3:
|
||||
TIM_OC3PolarityConfig(chan->timer, TIM_OCPolarity_High);
|
||||
break;
|
||||
case TIM_Channel_4:
|
||||
TIM_OC4PolarityConfig(chan->timer, TIM_OCPolarity_High);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Setup the timer accordingly
|
||||
TIM_SelectOnePulseMode(pios_servo_bank_timer[bank], TIM_OPMode_Repetitive);
|
||||
TIM_Cmd(pios_servo_bank_timer[bank], ENABLE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
uint8_t PIOS_Servo_GetPinBank(uint8_t pin)
|
||||
{
|
||||
if (pin < servo_cfg->num_channels) {
|
||||
return pios_servo_pin_bank[pin];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* PIOS_INCLUDE_SERVO */
|
@ -59,58 +59,59 @@
|
||||
// ------------------------
|
||||
// BOOTLOADER_SETTINGS
|
||||
// ------------------------
|
||||
#define BOARD_READABLE TRUE
|
||||
#define BOARD_WRITABLE TRUE
|
||||
#define MAX_DEL_RETRYS 3
|
||||
#define BOARD_READABLE TRUE
|
||||
#define BOARD_WRITABLE TRUE
|
||||
#define MAX_DEL_RETRYS 3
|
||||
|
||||
// ------------------------
|
||||
// WATCHDOG_SETTINGS
|
||||
// ------------------------
|
||||
#define PIOS_WATCHDOG_TIMEOUT 250
|
||||
#define PIOS_WDG_REGISTER BKP_DR4
|
||||
#define PIOS_WDG_ACTUATOR 0x0001
|
||||
#define PIOS_WDG_STABILIZATION 0x0002
|
||||
#define PIOS_WDG_ATTITUDE 0x0004
|
||||
#define PIOS_WDG_MANUAL 0x0008
|
||||
#define PIOS_WDG_AUTOTUNE 0x0010
|
||||
#define PIOS_WATCHDOG_TIMEOUT 250
|
||||
#define PIOS_WDG_REGISTER BKP_DR4
|
||||
#define PIOS_WDG_ACTUATOR 0x0001
|
||||
#define PIOS_WDG_STABILIZATION 0x0002
|
||||
#define PIOS_WDG_ATTITUDE 0x0004
|
||||
#define PIOS_WDG_MANUAL 0x0008
|
||||
#define PIOS_WDG_AUTOTUNE 0x0010
|
||||
|
||||
// ------------------------
|
||||
// TELEMETRY
|
||||
// ------------------------
|
||||
#define TELEM_QUEUE_SIZE 10
|
||||
#define TELEM_QUEUE_SIZE 10
|
||||
|
||||
// ------------------------
|
||||
// PIOS_LED
|
||||
// ------------------------
|
||||
#define PIOS_LED_HEARTBEAT 0
|
||||
#define PIOS_LED_HEARTBEAT 0
|
||||
|
||||
// -------------------------
|
||||
// System Settings
|
||||
// -------------------------
|
||||
#define PIOS_MASTER_CLOCK 72000000
|
||||
#define PIOS_PERIPHERAL_CLOCK (PIOS_MASTER_CLOCK / 2)
|
||||
#define PIOS_MASTER_CLOCK 72000000
|
||||
#define PIOS_PERIPHERAL_APB1_CLOCK (PIOS_MASTER_CLOCK / 2)
|
||||
#define PIOS_PERIPHERAL_APB2_CLOCK (PIOS_MASTER_CLOCK / 1)
|
||||
|
||||
// -------------------------
|
||||
// Interrupt Priorities
|
||||
// -------------------------
|
||||
#define PIOS_IRQ_PRIO_LOW 12 // lower than RTOS
|
||||
#define PIOS_IRQ_PRIO_MID 8 // higher than RTOS
|
||||
#define PIOS_IRQ_PRIO_HIGH 5 // for SPI, ADC, I2C etc...
|
||||
#define PIOS_IRQ_PRIO_HIGHEST 4 // for USART etc...
|
||||
#define PIOS_IRQ_PRIO_LOW 12 // lower than RTOS
|
||||
#define PIOS_IRQ_PRIO_MID 8 // higher than RTOS
|
||||
#define PIOS_IRQ_PRIO_HIGH 5 // for SPI, ADC, I2C etc...
|
||||
#define PIOS_IRQ_PRIO_HIGHEST 4 // for USART etc...
|
||||
// ------------------------
|
||||
// PIOS_I2C
|
||||
// See also pios_board.c
|
||||
// ------------------------
|
||||
#define PIOS_I2C_MAX_DEVS 1
|
||||
#define PIOS_I2C_MAX_DEVS 1
|
||||
extern uint32_t pios_i2c_flexi_adapter_id;
|
||||
#define PIOS_I2C_MAIN_ADAPTER (pios_i2c_flexi_adapter_id)
|
||||
#define PIOS_I2C_ESC_ADAPTER (pios_i2c_flexi_adapter_id)
|
||||
#define PIOS_I2C_BMP085_ADAPTER (pios_i2c_flexi_adapter_id)
|
||||
#define PIOS_I2C_MAIN_ADAPTER (pios_i2c_flexi_adapter_id)
|
||||
#define PIOS_I2C_ESC_ADAPTER (pios_i2c_flexi_adapter_id)
|
||||
#define PIOS_I2C_BMP085_ADAPTER (pios_i2c_flexi_adapter_id)
|
||||
|
||||
// ------------------------
|
||||
// PIOS_BMP085
|
||||
// ------------------------
|
||||
#define PIOS_BMP085_OVERSAMPLING 3
|
||||
#define PIOS_BMP085_OVERSAMPLING 3
|
||||
|
||||
// -------------------------
|
||||
// SPI
|
||||
|
@ -2,7 +2,8 @@
|
||||
******************************************************************************
|
||||
*
|
||||
* @file pios_board.h
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2013.
|
||||
* @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016.
|
||||
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2013.
|
||||
* @brief Defines PiOS board hardware for the OpenPilot OPLink Mini board.
|
||||
* @see The GNU Public License (GPL) Version 3
|
||||
*
|
||||
@ -137,16 +138,17 @@
|
||||
// -------------------------
|
||||
// System Settings
|
||||
// -------------------------
|
||||
#define PIOS_MASTER_CLOCK 72000000
|
||||
#define PIOS_PERIPHERAL_CLOCK (PIOS_MASTER_CLOCK / 2)
|
||||
#define PIOS_MASTER_CLOCK 72000000
|
||||
#define PIOS_PERIPHERAL_APB1_CLOCK (PIOS_MASTER_CLOCK / 2)
|
||||
#define PIOS_PERIPHERAL_APB2_CLOCK (PIOS_MASTER_CLOCK / 1)
|
||||
|
||||
// -------------------------
|
||||
// Interrupt Priorities
|
||||
// -------------------------
|
||||
#define PIOS_IRQ_PRIO_LOW 12 // lower than RTOS
|
||||
#define PIOS_IRQ_PRIO_MID 8 // higher than RTOS
|
||||
#define PIOS_IRQ_PRIO_HIGH 5 // for SPI, ADC, I2C etc...
|
||||
#define PIOS_IRQ_PRIO_HIGHEST 4 // for USART etc...
|
||||
#define PIOS_IRQ_PRIO_LOW 12 // lower than RTOS
|
||||
#define PIOS_IRQ_PRIO_MID 8 // higher than RTOS
|
||||
#define PIOS_IRQ_PRIO_HIGH 5 // for SPI, ADC, I2C etc...
|
||||
#define PIOS_IRQ_PRIO_HIGHEST 4 // for USART etc...
|
||||
|
||||
// ------------------------
|
||||
// PIOS_I2C
|
||||
|
Loading…
Reference in New Issue
Block a user