2010-09-27 09:28:45 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup PIOS PIOS Core hardware abstraction layer
|
|
|
|
* @{
|
|
|
|
* @addtogroup PIOS_SERVO RC Servo Functions
|
|
|
|
* @brief Code to do set RC servo output
|
|
|
|
* @{
|
|
|
|
*
|
2011-07-16 10:25:33 +02:00
|
|
|
* @file pios_servo.c
|
2010-09-27 09:28:45 +02:00
|
|
|
* @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
|
2011-07-16 10:25:33 +02:00
|
|
|
*
|
2010-09-27 09:28:45 +02:00
|
|
|
*****************************************************************************/
|
2011-07-16 10:25:33 +02: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
|
2010-09-27 09:28:45 +02:00
|
|
|
* (at your option) any later version.
|
2011-07-16 10:25:33 +02: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
|
2010-09-27 09:28:45 +02:00
|
|
|
* for more details.
|
2011-07-16 10:25:33 +02: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.,
|
2010-09-27 09:28:45 +02:00
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pios.h"
|
2013-03-15 19:25:30 +01:00
|
|
|
|
|
|
|
#ifdef PIOS_INCLUDE_SERVO
|
|
|
|
|
2011-01-21 04:40:21 +01:00
|
|
|
#include "pios_servo_priv.h"
|
2011-08-13 05:23:16 +02:00
|
|
|
#include "pios_tim_priv.h"
|
2010-09-27 09:28:45 +02:00
|
|
|
|
|
|
|
/* Private Function Prototypes */
|
|
|
|
|
2011-08-13 05:23:16 +02:00
|
|
|
static const struct pios_servo_cfg * servo_cfg;
|
|
|
|
|
2010-09-27 09:28:45 +02:00
|
|
|
/**
|
|
|
|
* Initialise Servos
|
|
|
|
*/
|
2011-08-13 05:23:16 +02:00
|
|
|
int32_t PIOS_Servo_Init(const struct pios_servo_cfg * cfg)
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
2011-08-13 05:23:16 +02:00
|
|
|
uint32_t tim_id;
|
|
|
|
if (PIOS_TIM_InitChannels(&tim_id, cfg->channels, cfg->num_channels, NULL, 0)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2011-07-16 10:25:33 +02:00
|
|
|
|
2011-08-13 05:23:16 +02:00
|
|
|
/* Store away the requested configuration */
|
|
|
|
servo_cfg = cfg;
|
2011-07-16 10:25:33 +02:00
|
|
|
|
2011-08-13 05:23:16 +02:00
|
|
|
/* 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 = &cfg->channels[i];
|
2011-07-16 10:25:33 +02:00
|
|
|
|
2011-01-21 04:40:21 +01:00
|
|
|
/* Set up for output compare function */
|
2011-08-13 05:23:16 +02:00
|
|
|
switch(chan->timer_chan) {
|
2011-01-24 08:51:45 +01:00
|
|
|
case TIM_Channel_1:
|
2011-08-13 05:23:16 +02:00
|
|
|
TIM_OC1Init(chan->timer, &cfg->tim_oc_init);
|
|
|
|
TIM_OC1PreloadConfig(chan->timer, TIM_OCPreload_Enable);
|
2011-01-21 04:40:21 +01:00
|
|
|
break;
|
2011-01-24 08:51:45 +01:00
|
|
|
case TIM_Channel_2:
|
2011-08-13 05:23:16 +02:00
|
|
|
TIM_OC2Init(chan->timer, &cfg->tim_oc_init);
|
|
|
|
TIM_OC2PreloadConfig(chan->timer, TIM_OCPreload_Enable);
|
2011-01-21 04:40:21 +01:00
|
|
|
break;
|
2011-01-24 08:51:45 +01:00
|
|
|
case TIM_Channel_3:
|
2011-08-13 05:23:16 +02:00
|
|
|
TIM_OC3Init(chan->timer, &cfg->tim_oc_init);
|
|
|
|
TIM_OC3PreloadConfig(chan->timer, TIM_OCPreload_Enable);
|
2011-01-21 04:40:21 +01:00
|
|
|
break;
|
2011-01-24 08:51:45 +01:00
|
|
|
case TIM_Channel_4:
|
2011-08-13 05:23:16 +02:00
|
|
|
TIM_OC4Init(chan->timer, &cfg->tim_oc_init);
|
|
|
|
TIM_OC4PreloadConfig(chan->timer, TIM_OCPreload_Enable);
|
2011-01-21 04:40:21 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-07-16 10:25:33 +02:00
|
|
|
|
2011-08-13 05:23:16 +02:00
|
|
|
TIM_ARRPreloadConfig(chan->timer, ENABLE);
|
|
|
|
TIM_CtrlPWMOutputs(chan->timer, ENABLE);
|
|
|
|
TIM_Cmd(chan->timer, ENABLE);
|
2011-01-24 08:51:48 +01:00
|
|
|
}
|
2011-07-16 10:25:33 +02:00
|
|
|
|
2011-08-13 05:23:16 +02:00
|
|
|
return 0;
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the servo update rate (Max 500Hz)
|
2011-02-01 03:17:55 +01:00
|
|
|
* \param[in] array of rates in Hz
|
|
|
|
* \param[in] maximum number of banks
|
2010-09-27 09:28:45 +02:00
|
|
|
*/
|
2012-08-12 01:46:00 +02:00
|
|
|
void PIOS_Servo_SetHz(const uint16_t * speeds, uint8_t banks)
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
2011-08-13 05:23:16 +02:00
|
|
|
if (!servo_cfg) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure = servo_cfg->tim_base_init;
|
2010-09-27 09:28:45 +02:00
|
|
|
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
|
|
|
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
|
|
|
TIM_TimeBaseStructure.TIM_Prescaler = (PIOS_MASTER_CLOCK / 1000000) - 1;
|
2011-07-16 10:25:33 +02:00
|
|
|
|
2011-02-01 03:17:55 +01:00
|
|
|
uint8_t set = 0;
|
2011-07-16 10:25:33 +02:00
|
|
|
|
2011-08-13 05:23:16 +02:00
|
|
|
for(uint8_t i = 0; (i < servo_cfg->num_channels) && (set < banks); i++) {
|
2011-02-01 03:17:55 +01:00
|
|
|
bool new = true;
|
2011-08-13 05:23:16 +02:00
|
|
|
const struct pios_tim_channel * chan = &servo_cfg->channels[i];
|
2011-07-16 10:25:33 +02:00
|
|
|
|
2011-02-01 03:17:55 +01:00
|
|
|
/* See if any previous channels use that same timer */
|
2011-07-16 10:25:33 +02:00
|
|
|
for(uint8_t j = 0; (j < i) && new; j++)
|
2011-08-13 05:23:16 +02:00
|
|
|
new &= chan->timer != servo_cfg->channels[j].timer;
|
2011-07-16 10:25:33 +02:00
|
|
|
|
2011-02-01 03:17:55 +01:00
|
|
|
if(new) {
|
2011-07-16 10:25:33 +02:00
|
|
|
TIM_TimeBaseStructure.TIM_Period = ((1000000 / speeds[set]) - 1);
|
2011-08-13 05:23:16 +02:00
|
|
|
TIM_TimeBaseInit(chan->timer, &TIM_TimeBaseStructure);
|
2011-02-01 03:17:55 +01:00
|
|
|
set++;
|
2011-01-21 04:40:21 +01:00
|
|
|
}
|
2011-02-01 03:17:55 +01:00
|
|
|
}
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set servo position
|
|
|
|
* \param[in] Servo Servo number (0-7)
|
2011-07-16 10:25:33 +02:00
|
|
|
* \param[in] Position Servo position in microseconds
|
2010-09-27 09:28:45 +02:00
|
|
|
*/
|
2011-08-13 05:23:16 +02:00
|
|
|
void PIOS_Servo_Set(uint8_t servo, uint16_t position)
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
|
|
|
/* Make sure servo exists */
|
2011-08-13 05:23:16 +02:00
|
|
|
if (!servo_cfg || servo >= servo_cfg->num_channels) {
|
|
|
|
return;
|
|
|
|
}
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2011-08-13 05:23:16 +02:00
|
|
|
/* Update the position */
|
|
|
|
const struct pios_tim_channel * chan = &servo_cfg->channels[servo];
|
|
|
|
switch(chan->timer_chan) {
|
|
|
|
case TIM_Channel_1:
|
|
|
|
TIM_SetCompare1(chan->timer, position);
|
|
|
|
break;
|
|
|
|
case TIM_Channel_2:
|
|
|
|
TIM_SetCompare2(chan->timer, position);
|
|
|
|
break;
|
|
|
|
case TIM_Channel_3:
|
|
|
|
TIM_SetCompare3(chan->timer, position);
|
|
|
|
break;
|
|
|
|
case TIM_Channel_4:
|
|
|
|
TIM_SetCompare4(chan->timer, position);
|
|
|
|
break;
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
}
|
2013-03-15 19:25:30 +01:00
|
|
|
|
|
|
|
#endif /* PIOS_INCLUDE_SERVO */
|