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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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 */
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
static const struct pios_servo_cfg *servo_cfg;
|
2011-11-01 10:39:51 +01:00
|
|
|
|
2015-01-23 23:50:51 +01:00
|
|
|
// 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 };
|
2015-02-02 02:58:22 +01:00
|
|
|
// 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 };
|
2015-01-23 23:50:51 +01:00
|
|
|
// 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;
|
|
|
|
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-09-24 02:39:22 +02:00
|
|
|
#if defined(STM32F40_41xxx) || defined(STM32F446xx)
|
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_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);
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
pios_servo_pin_bank = pios_malloc(sizeof(uint8_t) * cfg->num_channels);
|
|
|
|
|
|
|
|
uint8_t bank = 0;
|
|
|
|
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) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2015-01-24 13:35:41 +01:00
|
|
|
pios_servo_bank_timer[bank] = chan->timer;
|
2013-05-19 16:37:30 +02:00
|
|
|
|
2015-01-23 23:50:51 +01:00
|
|
|
TIM_ARRPreloadConfig(chan->timer, ENABLE);
|
|
|
|
TIM_CtrlPWMOutputs(chan->timer, ENABLE);
|
|
|
|
TIM_Cmd(chan->timer, DISABLE);
|
|
|
|
|
|
|
|
bank++;
|
|
|
|
}
|
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);
|
|
|
|
pios_servo_bank_mode[bank] = mode;
|
|
|
|
|
2016-09-24 02:28:45 +02:00
|
|
|
if (!pios_servo_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-09 00:54:01 +01:00
|
|
|
|
2016-09-24 02:28:45 +02:00
|
|
|
if (pios_servo_bank_timer[bank]) {
|
2015-01-25 21:25:14 +01:00
|
|
|
// Setup the timer accordingly
|
2015-01-29 21:20:29 +01:00
|
|
|
TIM_SelectOnePulseMode(pios_servo_bank_timer[bank], TIM_OPMode_Repetitive);
|
|
|
|
TIM_Cmd(pios_servo_bank_timer[bank], 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++) {
|
|
|
|
const TIM_TypeDef *timer = pios_servo_bank_timer[i];
|
2015-01-29 21:20:29 +01:00
|
|
|
if (timer && pios_servo_bank_mode[i] == 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.
|
|
|
|
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];
|
|
|
|
}
|
2015-01-29 21:20:29 +01:00
|
|
|
}
|
2015-02-02 19:58:04 +01:00
|
|
|
pios_servo_bank_max_pulse[i] = 0;
|
2015-01-29 21:20:29 +01:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
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++) {
|
2015-01-23 23:50:51 +01:00
|
|
|
const TIM_TypeDef *timer = pios_servo_bank_timer[i];
|
|
|
|
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
|
|
|
|
|
|
|
uint32_t apb_clock;
|
|
|
|
|
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
|
|
|
|
|
|
|
#if defined(STM32F10X_MD)
|
|
|
|
if (timer == TIM1 || timer == TIM8) {
|
|
|
|
apb_clock = PIOS_PERIPHERAL_APB2_CLOCK;
|
|
|
|
} else {
|
|
|
|
apb_clock = PIOS_PERIPHERAL_APB1_CLOCK;
|
|
|
|
}
|
|
|
|
#elif defined(STM32F40_41xxx) || defined(STM32F446xx)
|
2015-01-23 23:50:51 +01:00
|
|
|
if (timer == TIM1 || timer == TIM8 || timer == TIM9 || timer == TIM10 || timer == TIM11) {
|
2016-09-24 02:39:22 +02:00
|
|
|
apb_clock = PIOS_PERIPHERAL_APB2_CLOCK;
|
2013-05-19 16:37:30 +02:00
|
|
|
} else {
|
2016-09-24 02:39:22 +02:00
|
|
|
apb_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
|
|
|
|
TIM_TimeBaseStructure.TIM_Prescaler = (apb_clock / new_clock) - 1;
|
|
|
|
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 */
|
|
|
|
const struct pios_tim_channel *chan = &servo_cfg->channels[servo];
|
2015-01-31 15:04:02 +01:00
|
|
|
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)) {
|
2015-01-29 21:20:29 +01:00
|
|
|
val = chan->timer->ARR - margin;
|
2015-01-31 15:04:02 +01:00
|
|
|
}
|
2015-02-02 02:58:22 +01:00
|
|
|
|
|
|
|
uint8_t bank = pios_servo_pin_bank[servo];
|
|
|
|
if (pios_servo_bank_max_pulse[bank] < val) {
|
|
|
|
pios_servo_bank_max_pulse[bank] = val;
|
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
switch (chan->timer_chan) {
|
|
|
|
case TIM_Channel_1:
|
2015-01-23 23:50:51 +01:00
|
|
|
TIM_SetCompare1(chan->timer, val);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TIM_Channel_2:
|
2015-01-23 23:50:51 +01:00
|
|
|
TIM_SetCompare2(chan->timer, val);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TIM_Channel_3:
|
2015-01-23 23:50:51 +01:00
|
|
|
TIM_SetCompare3(chan->timer, val);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case TIM_Channel_4:
|
2015-01-23 23:50:51 +01:00
|
|
|
TIM_SetCompare4(chan->timer, val);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
}
|
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) {
|
|
|
|
return pios_servo_pin_bank[pin];
|
|
|
|
} 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 */
|