1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-18 03:52:11 +01:00

OP-256 PIOS/PWM: Converting PWM input to use standard driver structure.

Beginning of unifying the input types into PIOS_RECEIVER.

git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@2568 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
peabody124 2011-01-24 07:51:45 +00:00 committed by peabody124
parent 01b09668aa
commit da96ce15b9
12 changed files with 442 additions and 336 deletions

View File

@ -217,7 +217,6 @@ SRC += $(PIOSSTM32F10X)/pios_usb_hid_pwr.c
## PIOS Hardware (Common)
SRC += $(PIOSCOMMON)/pios_adxl345.c
SRC += $(PIOSCOMMON)/pios_sdcard.c
SRC += $(PIOSCOMMON)/pios_com.c
SRC += $(PIOSCOMMON)/pios_i2c_esc.c
SRC += $(PIOSCOMMON)/pios_iap.c

View File

@ -43,7 +43,7 @@
#define PIOS_INCLUDE_LED
//#define PIOS_INCLUDE_SPEKTRUM
//#define PIOS_INCLUDE_PPM
//#define PIOS_INCLUDE_PWM
#define PIOS_INCLUDE_PWM
#define PIOS_INCLUDE_SERVO
#define PIOS_INCLUDE_SPI
#define PIOS_INCLUDE_SYS

View File

@ -75,7 +75,7 @@ void PIOS_Board_Init(void) {
PIOS_GPIO_Init();
#if defined(PIOS_INCLUDE_PWM)
//PIOS_PWM_Init();
PIOS_PWM_Init();
#endif
#if defined(PIOS_INCLUDE_PPM)
//PIOS_PPM_Init();
@ -547,6 +547,105 @@ const struct pios_servo_cfg pios_servo_cfg = {
.num_channels = NELEMENTS(pios_servo_channels),
};
/*
* PWM Inputs
*/
#include <pios_pwm_priv.h>
const struct pios_pwm_channel pios_pwm_channels[] = {
{
.timer = TIM4,
.port = GPIOB,
.ccr = TIM_IT_CC1,
.channel = TIM_Channel_1,
.pin = GPIO_Pin_6,
},
{
.timer = TIM3,
.port = GPIOB,
.ccr = TIM_IT_CC2,
.channel = TIM_Channel_2,
.pin = GPIO_Pin_5,
},
{
.timer = TIM3,
.port = GPIOB,
.ccr = TIM_IT_CC3,
.channel = TIM_Channel_3,
.pin = GPIO_Pin_0
},
{
.timer = TIM3,
.port = GPIOB,
.ccr = TIM_IT_CC4,
.channel = TIM_Channel_4,
.pin = GPIO_Pin_1,
},
{
.timer = TIM2,
.port = GPIOA,
.ccr = TIM_IT_CC1,
.channel = TIM_Channel_1,
.pin = GPIO_Pin_0,
},
{
.timer = TIM2,
.port = GPIOA,
.ccr = TIM_IT_CC2,
.channel = TIM_Channel_2,
.pin = GPIO_Pin_1,
},
};
void TIM2_IRQHandler();
void TIM3_IRQHandler();
void TIM4_IRQHandler();
void TIM2_IRQHandler() __attribute__ ((alias ("PIOS_TIM2_irq_handler")));
void TIM3_IRQHandler() __attribute__ ((alias ("PIOS_TIM3_irq_handler")));
void TIM4_IRQHandler() __attribute__ ((alias ("PIOS_TIM4_irq_handler")));
const struct pios_pwm_cfg pios_pwm_cfg = {
.tim_base_init = {
.TIM_Prescaler = (PIOS_MASTER_CLOCK / 1000000) - 1,
.TIM_ClockDivision = TIM_CKD_DIV1,
.TIM_CounterMode = TIM_CounterMode_Up,
.TIM_Period = 0xFFFF,
.TIM_RepetitionCounter = 0x0000,
},
.tim_ic_init = {
.TIM_ICPolarity = TIM_ICPolarity_Rising,
.TIM_ICSelection = TIM_ICSelection_DirectTI,
.TIM_ICPrescaler = TIM_ICPSC_DIV1,
.TIM_ICFilter = 0x0,
},
.gpio_init = {
.GPIO_Mode = GPIO_Mode_IPD,
.GPIO_Speed = GPIO_Speed_2MHz,
},
.remap = 0,
.irq = {
.handler = TIM2_IRQHandler,
.init = {
.NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_MID,
.NVIC_IRQChannelSubPriority = 0,
.NVIC_IRQChannelCmd = ENABLE,
},
},
.channels = pios_pwm_channels,
.num_channels = NELEMENTS(pios_pwm_channels),
};
void PIOS_TIM2_irq_handler()
{
PIOS_PWM_irq_handler(TIM2);
}
void PIOS_TIM3_irq_handler()
{
PIOS_PWM_irq_handler(TIM3);
}
void PIOS_TIM4_irq_handler()
{
PIOS_PWM_irq_handler(TIM4);
}
/*
* I2C Adapters
*/

View File

@ -130,6 +130,11 @@ void updateInput()
{
ManualControlCommandData manual;
ManualControlCommandGet(&manual);
manual.Throttle = (PIOS_PWM_Get(0) - 1100) / 900;
manual.Roll = (PIOS_PWM_Get(1) - 1500) / 500;
manual.Pitch = (PIOS_PWM_Get(2) - 1500) / 500;
manual.Yaw = (PIOS_PWM_Get(3) - 1500) / 500;
manual.FlightMode = (PIOS_PWM_Get(4) - 1500) / 500;
ManualControlCommandSet(&manual);
}

View File

@ -613,49 +613,49 @@ const struct pios_servo_channel pios_servo_channels[] = {
{
.timer = TIM4,
.port = GPIOB,
.channel = 1,
.channel = TIM_Channel_1,
.pin = GPIO_Pin_6,
},
{
.timer = TIM4,
.port = GPIOB,
.channel = 2,
.channel = TIM_Channel_2,
.pin = GPIO_Pin_7,
},
{
.timer = TIM4,
.port = GPIOB,
.channel = 3,
.channel = TIM_Channel_3,
.pin = GPIO_Pin_8,
},
{
.timer = TIM4,
.port = GPIOB,
.channel = 4,
.channel = TIM_Channel_4,
.pin = GPIO_Pin_9,
},
{
.timer = TIM8,
.port = GPIOC,
.channel = 1,
.channel = TIM_Channel_1,
.pin = GPIO_Pin_6,
},
{
.timer = TIM8,
.port = GPIOC,
.channel = 2,
.channel = TIM_Channel_2,
.pin = GPIO_Pin_7,
},
{
.timer = TIM8,
.port = GPIOC,
.channel = 3,
.channel = TIM_Channel_3,
.pin = GPIO_Pin_8,
},
{
.timer = TIM8,
.port = GPIOC,
.channel = 4,
.channel = TIM_Channel_4,
.pin = GPIO_Pin_9,
},
};
@ -687,6 +687,118 @@ const struct pios_servo_cfg pios_servo_cfg = {
};
/*
* PWM Inputs
*/
#include <pios_pwm_priv.h>
const struct pios_pwm_channel pios_pwm_channels[] = {
{
.timer = TIM1,
.port = GPIOA,
.ccr = TIM_IT_CC2,
.channel = TIM_Channel_2,
.pin = GPIO_Pin_9,
},
{
.timer = TIM1,
.port = GPIOA,
.ccr = TIM_IT_CC3,
.channel = TIM_Channel_3,
.pin = GPIO_Pin_10,
},
{
.timer = TIM5,
.port = GPIOA,
.ccr = TIM_IT_CC1,
.channel = TIM_Channel_1,
.pin = GPIO_Pin_0
},
{
.timer = TIM1,
.port = GPIOA,
.ccr = TIM_IT_CC1,
.channel = TIM_Channel_1,
.pin = GPIO_Pin_8,
},
{
.timer = TIM3,
.port = GPIOB,
.ccr = TIM_IT_CC4,
.channel = TIM_Channel_4,
.pin = GPIO_Pin_1,
},
{
.timer = TIM3,
.port = GPIOB,
.ccr = TIM_IT_CC3,
.channel = TIM_Channel_3,
.pin = GPIO_Pin_0,
},
{
.timer = TIM3,
.port = GPIOB,
.ccr = TIM_IT_CC1,
.channel = TIM_Channel_1,
.pin = GPIO_Pin_4,
},
{
.timer = TIM3,
.port = GPIOB,
.ccr = TIM_IT_CC2,
.channel = TIM_Channel_2,
.pin = GPIO_Pin_5,
},
};
void TIM2_IRQHandler();
void TIM3_IRQHandler();
void TIM4_IRQHandler();
void TIM2_IRQHandler() __attribute__ ((alias ("PIOS_TIM2_irq_handler")));
void TIM3_IRQHandler() __attribute__ ((alias ("PIOS_TIM3_irq_handler")));
void TIM4_IRQHandler() __attribute__ ((alias ("PIOS_TIM4_irq_handler")));
const struct pios_pwm_cfg pios_pwm_cfg = {
.tim_base_init = {
.TIM_Prescaler = (PIOS_MASTER_CLOCK / 1000000) - 1,
.TIM_ClockDivision = TIM_CKD_DIV1,
.TIM_CounterMode = TIM_CounterMode_Up,
.TIM_Period = 0xFFFF,
.TIM_RepetitionCounter = 0x0000,
},
.tim_ic_init = {
.TIM_ICPolarity = TIM_ICPolarity_Rising,
.TIM_ICSelection = TIM_ICSelection_DirectTI,
.TIM_ICPrescaler = TIM_ICPSC_DIV1,
.TIM_ICFilter = 0x0,
},
.gpio_init = {
.GPIO_Mode = GPIO_Mode_IPD,
.GPIO_Speed = GPIO_Speed_2MHz,
},
.remap = GPIO_PartialRemap_TIM3,
.irq = {
.handler = TIM2_IRQHandler,
.init = {
.NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_MID,
.NVIC_IRQChannelSubPriority = 0,
.NVIC_IRQChannelCmd = ENABLE,
},
},
.channels = pios_pwm_channels,
.num_channels = NELEMENTS(pios_pwm_channels),
};
void PIOS_TIM2_irq_handler()
{
PIOS_PWM_irq_handler(TIM2);
}
void PIOS_TIM3_irq_handler()
{
PIOS_PWM_irq_handler(TIM3);
}
void PIOS_TIM4_irq_handler()
{
PIOS_PWM_irq_handler(TIM4);
}
/*
* COM devices
*/

View File

@ -216,62 +216,7 @@ TIM4 | RC In 1 | Servo 3 | Servo 2 | Servo 1
//-------------------------
// Receiver PWM inputs
//-------------------------
/* #define PIOS_PWM_CH1_GPIO_PORT GPIOA
#define PIOS_PWM_CH1_PIN GPIO_Pin_9
#define PIOS_PWM_CH1_TIM_PORT TIM1
#define PIOS_PWM_CH1_CH TIM_Channel_2
#define PIOS_PWM_CH1_CCR TIM_IT_CC2
#define PIOS_PWM_CH2_GPIO_PORT GPIOA
#define PIOS_PWM_CH2_PIN GPIO_Pin_10
#define PIOS_PWM_CH2_TIM_PORT TIM1
#define PIOS_PWM_CH2_CH TIM_Channel_3
#define PIOS_PWM_CH2_CCR TIM_IT_CC3
#define PIOS_PWM_CH3_GPIO_PORT GPIOA
#define PIOS_PWM_CH3_PIN GPIO_Pin_0
#define PIOS_PWM_CH3_TIM_PORT TIM5
#define PIOS_PWM_CH3_CH TIM_Channel_1
#define PIOS_PWM_CH3_CCR TIM_IT_CC1
#define PIOS_PWM_CH4_GPIO_PORT GPIOA
#define PIOS_PWM_CH4_PIN GPIO_Pin_8
#define PIOS_PWM_CH4_TIM_PORT TIM1
#define PIOS_PWM_CH4_CH TIM_Channel_1
#define PIOS_PWM_CH4_CCR TIM_IT_CC1
#define PIOS_PWM_CH5_GPIO_PORT GPIOB
#define PIOS_PWM_CH5_PIN GPIO_Pin_1
#define PIOS_PWM_CH5_TIM_PORT TIM3
#define PIOS_PWM_CH5_CH TIM_Channel_4
#define PIOS_PWM_CH5_CCR TIM_IT_CC4
#define PIOS_PWM_CH6_GPIO_PORT GPIOB
#define PIOS_PWM_CH6_PIN GPIO_Pin_0
#define PIOS_PWM_CH6_TIM_PORT TIM3
#define PIOS_PWM_CH6_CH TIM_Channel_3
#define PIOS_PWM_CH6_CCR TIM_IT_CC3
#define PIOS_PWM_CH7_GPIO_PORT GPIOB
#define PIOS_PWM_CH7_PIN GPIO_Pin_4
#define PIOS_PWM_CH7_TIM_PORT TIM3
#define PIOS_PWM_CH7_CH TIM_Channel_1
#define PIOS_PWM_CH7_CCR TIM_IT_CC1
#define PIOS_PWM_CH8_GPIO_PORT GPIOB
#define PIOS_PWM_CH8_PIN GPIO_Pin_5
#define PIOS_PWM_CH8_TIM_PORT TIM3
#define PIOS_PWM_CH8_CH TIM_Channel_2
#define PIOS_PWM_CH8_CCR TIM_IT_CC2
#define PIOS_PWM_GPIO_PORTS { PIOS_PWM_CH1_GPIO_PORT, PIOS_PWM_CH2_GPIO_PORT, PIOS_PWM_CH3_GPIO_PORT, PIOS_PWM_CH4_GPIO_PORT, PIOS_PWM_CH5_GPIO_PORT, PIOS_PWM_CH6_GPIO_PORT, PIOS_PWM_CH7_GPIO_PORT, PIOS_PWM_CH8_GPIO_PORT }
#define PIOS_PWM_GPIO_PINS { PIOS_PWM_CH1_PIN, PIOS_PWM_CH2_PIN, PIOS_PWM_CH3_PIN, PIOS_PWM_CH4_PIN, PIOS_PWM_CH5_PIN, PIOS_PWM_CH6_PIN, PIOS_PWM_CH7_PIN, PIOS_PWM_CH8_PIN }
#define PIOS_PWM_TIM_PORTS { PIOS_PWM_CH1_TIM_PORT, PIOS_PWM_CH2_TIM_PORT, PIOS_PWM_CH3_TIM_PORT, PIOS_PWM_CH4_TIM_PORT, PIOS_PWM_CH5_TIM_PORT, PIOS_PWM_CH6_TIM_PORT, PIOS_PWM_CH7_TIM_PORT, PIOS_PWM_CH8_TIM_PORT }
#define PIOS_PWM_TIM_CHANNELS { PIOS_PWM_CH1_CH, PIOS_PWM_CH2_CH, PIOS_PWM_CH3_CH, PIOS_PWM_CH4_CH, PIOS_PWM_CH5_CH, PIOS_PWM_CH6_CH, PIOS_PWM_CH7_CH, PIOS_PWM_CH8_CH }
#define PIOS_PWM_TIM_CCRS { PIOS_PWM_CH1_CCR, PIOS_PWM_CH2_CCR, PIOS_PWM_CH3_CCR, PIOS_PWM_CH4_CCR, PIOS_PWM_CH5_CCR, PIOS_PWM_CH6_CCR, PIOS_PWM_CH7_CCR, PIOS_PWM_CH8_CCR }
#define PIOS_PWM_TIMS { TIM1, TIM3, TIM5 }
#define PIOS_PWM_TIM_IRQS { TIM1_CC_IRQn, TIM3_IRQn, TIM5_IRQn }
#define PIOS_PWM_NUM_INPUTS 8
#define PIOS_PWM_NUM_TIMS 3
#define PIOS_PWM_SUPV_ENABLED 0
#define PIOS_PWM_SUPV_TIMER TIM6
#define PIOS_PWM_SUPV_TIMER_RCC_FUNC RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE)
#define PIOS_PWM_SUPV_HZ 25
#define PIOS_PWM_SUPV_IRQ_CHANNEL TIM6_IRQn
#define PIOS_PWM_SUPV_IRQ_FUNC void TIM6_IRQHandler(void)
*/
#define PIOS_PWM_MAX_INPUTS 6
//-------------------------
// Servo outputs

View File

@ -202,61 +202,12 @@ TIM8 | Servo 5 | Servo 6 | Servo 7 | Servo 8
//-------------------------
// Receiver PWM inputs
//-------------------------
#define PIOS_PWM_CH1_GPIO_PORT GPIOA
#define PIOS_PWM_CH1_PIN GPIO_Pin_9
#define PIOS_PWM_CH1_TIM_PORT TIM1
#define PIOS_PWM_CH1_CH TIM_Channel_2
#define PIOS_PWM_CH1_CCR TIM_IT_CC2
#define PIOS_PWM_CH2_GPIO_PORT GPIOA
#define PIOS_PWM_CH2_PIN GPIO_Pin_10
#define PIOS_PWM_CH2_TIM_PORT TIM1
#define PIOS_PWM_CH2_CH TIM_Channel_3
#define PIOS_PWM_CH2_CCR TIM_IT_CC3
#define PIOS_PWM_CH3_GPIO_PORT GPIOA
#define PIOS_PWM_CH3_PIN GPIO_Pin_0
#define PIOS_PWM_CH3_TIM_PORT TIM5
#define PIOS_PWM_CH3_CH TIM_Channel_1
#define PIOS_PWM_CH3_CCR TIM_IT_CC1
#define PIOS_PWM_CH4_GPIO_PORT GPIOA
#define PIOS_PWM_CH4_PIN GPIO_Pin_8
#define PIOS_PWM_CH4_TIM_PORT TIM1
#define PIOS_PWM_CH4_CH TIM_Channel_1
#define PIOS_PWM_CH4_CCR TIM_IT_CC1
#define PIOS_PWM_CH5_GPIO_PORT GPIOB
#define PIOS_PWM_CH5_PIN GPIO_Pin_1
#define PIOS_PWM_CH5_TIM_PORT TIM3
#define PIOS_PWM_CH5_CH TIM_Channel_4
#define PIOS_PWM_CH5_CCR TIM_IT_CC4
#define PIOS_PWM_CH6_GPIO_PORT GPIOB
#define PIOS_PWM_CH6_PIN GPIO_Pin_0
#define PIOS_PWM_CH6_TIM_PORT TIM3
#define PIOS_PWM_CH6_CH TIM_Channel_3
#define PIOS_PWM_CH6_CCR TIM_IT_CC3
#define PIOS_PWM_CH7_GPIO_PORT GPIOB
#define PIOS_PWM_CH7_PIN GPIO_Pin_4
#define PIOS_PWM_CH7_TIM_PORT TIM3
#define PIOS_PWM_CH7_CH TIM_Channel_1
#define PIOS_PWM_CH7_CCR TIM_IT_CC1
#define PIOS_PWM_CH8_GPIO_PORT GPIOB
#define PIOS_PWM_CH8_PIN GPIO_Pin_5
#define PIOS_PWM_CH8_TIM_PORT TIM3
#define PIOS_PWM_CH8_CH TIM_Channel_2
#define PIOS_PWM_CH8_CCR TIM_IT_CC2
#define PIOS_PWM_GPIO_PORTS { PIOS_PWM_CH1_GPIO_PORT, PIOS_PWM_CH2_GPIO_PORT, PIOS_PWM_CH3_GPIO_PORT, PIOS_PWM_CH4_GPIO_PORT, PIOS_PWM_CH5_GPIO_PORT, PIOS_PWM_CH6_GPIO_PORT, PIOS_PWM_CH7_GPIO_PORT, PIOS_PWM_CH8_GPIO_PORT }
#define PIOS_PWM_GPIO_PINS { PIOS_PWM_CH1_PIN, PIOS_PWM_CH2_PIN, PIOS_PWM_CH3_PIN, PIOS_PWM_CH4_PIN, PIOS_PWM_CH5_PIN, PIOS_PWM_CH6_PIN, PIOS_PWM_CH7_PIN, PIOS_PWM_CH8_PIN }
#define PIOS_PWM_TIM_PORTS { PIOS_PWM_CH1_TIM_PORT, PIOS_PWM_CH2_TIM_PORT, PIOS_PWM_CH3_TIM_PORT, PIOS_PWM_CH4_TIM_PORT, PIOS_PWM_CH5_TIM_PORT, PIOS_PWM_CH6_TIM_PORT, PIOS_PWM_CH7_TIM_PORT, PIOS_PWM_CH8_TIM_PORT }
#define PIOS_PWM_TIM_CHANNELS { PIOS_PWM_CH1_CH, PIOS_PWM_CH2_CH, PIOS_PWM_CH3_CH, PIOS_PWM_CH4_CH, PIOS_PWM_CH5_CH, PIOS_PWM_CH6_CH, PIOS_PWM_CH7_CH, PIOS_PWM_CH8_CH }
#define PIOS_PWM_TIM_CCRS { PIOS_PWM_CH1_CCR, PIOS_PWM_CH2_CCR, PIOS_PWM_CH3_CCR, PIOS_PWM_CH4_CCR, PIOS_PWM_CH5_CCR, PIOS_PWM_CH6_CCR, PIOS_PWM_CH7_CCR, PIOS_PWM_CH8_CCR }
#define PIOS_PWM_TIMS { TIM1, TIM3, TIM5 }
#define PIOS_PWM_TIM_IRQS { TIM1_CC_IRQn, TIM3_IRQn, TIM5_IRQn }
#define PIOS_PWM_NUM_INPUTS 8
#define PIOS_PWM_NUM_TIMS 3
#define PIOS_PWM_SUPV_ENABLED 1
/*#define PIOS_PWM_SUPV_ENABLED 1
#define PIOS_PWM_SUPV_TIMER TIM6
#define PIOS_PWM_SUPV_TIMER_RCC_FUNC RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE)
#define PIOS_PWM_SUPV_HZ 25
#define PIOS_PWM_SUPV_IRQ_CHANNEL TIM6_IRQn
#define PIOS_PWM_SUPV_IRQ_FUNC void TIM6_IRQHandler(void)
#define PIOS_PWM_SUPV_IRQ_FUNC void TIM6_IRQHandler(void)*/
//-------------------------
// Receiver PPM input
@ -291,6 +242,7 @@ TIM8 | Servo 5 | Servo 6 | Servo 7 | Servo 8
//-------------------------
#define PIOS_SERVO_UPDATE_HZ 50
#define PIOS_SERVOS_INITIAL_POSITION 0 /* dont want to start motors, have no pulse till settings loaded */
#define PIOS_PWM_MAX_INPUTS 8
//-------------------------
// ADC

View File

@ -30,105 +30,105 @@
/* Project Includes */
#include "pios.h"
#include "pios_pwm_priv.h"
#if defined(PIOS_INCLUDE_PWM)
/* Local Variables */
static GPIO_TypeDef *PIOS_PWM_GPIO_PORT[PIOS_PWM_NUM_INPUTS] = PIOS_PWM_GPIO_PORTS;
static const uint32_t PIOS_PWM_GPIO_PIN[PIOS_PWM_NUM_INPUTS] = PIOS_PWM_GPIO_PINS;
static TIM_TypeDef *PIOS_PWM_TIM_PORT[PIOS_PWM_NUM_INPUTS] = PIOS_PWM_TIM_PORTS;
static const uint32_t PIOS_PWM_TIM_CHANNEL[PIOS_PWM_NUM_INPUTS] = PIOS_PWM_TIM_CHANNELS;
static const uint32_t PIOS_PWM_TIM_CCR[PIOS_PWM_NUM_INPUTS] = PIOS_PWM_TIM_CCRS;
static TIM_TypeDef *PIOS_PWM_TIM[PIOS_PWM_NUM_TIMS] = PIOS_PWM_TIMS;
static const uint32_t PIOS_PWM_TIM_IRQ[PIOS_PWM_NUM_TIMS] = PIOS_PWM_TIM_IRQS;
static uint8_t CaptureState[PIOS_PWM_MAX_INPUTS];
static uint16_t RiseValue[PIOS_PWM_MAX_INPUTS];
static uint16_t FallValue[PIOS_PWM_MAX_INPUTS];
static uint32_t CaptureValue[PIOS_PWM_MAX_INPUTS];
static TIM_ICInitTypeDef TIM_ICInitStructure;
static uint8_t CaptureState[PIOS_PWM_NUM_INPUTS];
static uint16_t RiseValue[PIOS_PWM_NUM_INPUTS];
static uint16_t FallValue[PIOS_PWM_NUM_INPUTS];
static uint32_t CaptureValue[PIOS_PWM_NUM_INPUTS];
static uint8_t SupervisorState = 0;
static uint32_t CapCounter[PIOS_PWM_NUM_INPUTS];
static uint32_t CapCounterPrev[PIOS_PWM_NUM_INPUTS];
//static uint8_t SupervisorState = 0;
static uint32_t CapCounter[PIOS_PWM_MAX_INPUTS];
//static uint32_t CapCounterPrev[MAX_CHANNELS];
/**
* Initialises all the pins
*/
void PIOS_PWM_Init(void)
{
for (uint8_t i = 0; i < pios_pwm_cfg.num_channels; i++) {
/* Flush counter variables */
int32_t i;
for (i = 0; i < PIOS_PWM_NUM_INPUTS; i++) {
CaptureState[i] = 0;
}
for (i = 0; i < PIOS_PWM_NUM_INPUTS; i++) {
RiseValue[i] = 0;
}
for (i = 0; i < PIOS_PWM_NUM_INPUTS; i++) {
FallValue[i] = 0;
}
for (i = 0; i < PIOS_PWM_NUM_INPUTS; i++) {
CaptureValue[i] = 0;
}
/* Setup RCC */
NVIC_InitTypeDef NVIC_InitStructure = pios_pwm_cfg.irq.init;
GPIO_InitTypeDef GPIO_InitStructure = pios_pwm_cfg.gpio_init;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure = pios_pwm_cfg.tim_base_init;
TIM_ICInitTypeDef TIM_ICInitStructure = pios_pwm_cfg.tim_ic_init;
struct pios_pwm_channel channel = pios_pwm_cfg.channels[i];
/* Enable appropriate clock to timer module */
switch((int32_t) channel.timer) {
case (int32_t)TIM1:
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
break;
case (int32_t)TIM2:
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
break;
case (int32_t)TIM3:
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
break;
case (int32_t)TIM4:
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
break;
#ifdef STM32F10X_HD
case (int32_t)TIM5:
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
/* Enable timer interrupts */
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_MID;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
for (i = 0; i < PIOS_PWM_NUM_TIMS; i++) {
NVIC_InitStructure.NVIC_IRQChannel = PIOS_PWM_TIM_IRQ[i];
break;
case (int32_t)TIM6:
NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
break;
case (int32_t)TIM7:
NVIC_InitStructure.NVIC_IRQChannel = TIM7_IRQn;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);
break;
case (int32_t)TIM8:
NVIC_InitStructure.NVIC_IRQChannel = TIM8_CC_IRQn;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
break;
#endif
}
NVIC_Init(&NVIC_InitStructure);
}
/* Partial pin remap for TIM3 (PB5) */
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
/* Configure input pins */
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
for (i = 0; i < PIOS_PWM_NUM_INPUTS; i++) {
GPIO_InitStructure.GPIO_Pin = PIOS_PWM_GPIO_PIN[i];
GPIO_Init(PIOS_PWM_GPIO_PORT[i], &GPIO_InitStructure);
}
/* Enable GPIO */
GPIO_InitStructure.GPIO_Pin = channel.pin;
GPIO_Init(channel.port, &GPIO_InitStructure);
/* Configure timer for input capture */
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
for (i = 0; i < PIOS_PWM_NUM_INPUTS; i++) {
TIM_ICInitStructure.TIM_Channel = PIOS_PWM_TIM_CHANNEL[i];
TIM_ICInit(PIOS_PWM_TIM_PORT[i], &TIM_ICInitStructure);
}
TIM_ICInitStructure.TIM_Channel = channel.channel;
TIM_ICInit(channel.timer, &TIM_ICInitStructure);
/* Configure timer clocks */
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = (PIOS_MASTER_CLOCK / 1000000) - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
for (i = 0; i < PIOS_PWM_NUM_INPUTS; i++) {
TIM_InternalClockConfig(PIOS_PWM_TIM_PORT[i]);
TIM_TimeBaseInit(PIOS_PWM_TIM_PORT[i], &TIM_TimeBaseStructure);
TIM_InternalClockConfig(channel.timer);
if(channel.timer->PSC != ((PIOS_MASTER_CLOCK / 1000000) - 1))
TIM_TimeBaseInit(channel.timer, &TIM_TimeBaseStructure);
/* Enable the Capture Compare Interrupt Request */
TIM_ITConfig(PIOS_PWM_TIM_PORT[i], PIOS_PWM_TIM_CCR[i], ENABLE);
}
TIM_ITConfig(channel.timer, channel.ccr, ENABLE);
/* Enable timers */
for (i = 0; i < PIOS_PWM_NUM_TIMS; i++) {
TIM_Cmd(PIOS_PWM_TIM[i], ENABLE);
TIM_Cmd(channel.timer, ENABLE);
}
if(pios_pwm_cfg.remap) {
/* Warning, I don't think this will work for multiple remaps at once */
GPIO_PinRemapConfig(pios_pwm_cfg.remap, ENABLE);
}
#if 0
/* Supervisor Setup */
#if (PIOS_PWM_SUPV_ENABLED)
/* Flush counter variables */
@ -172,6 +172,7 @@ void PIOS_PWM_Init(void)
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
#endif
}
/**
@ -183,70 +184,59 @@ void PIOS_PWM_Init(void)
int32_t PIOS_PWM_Get(int8_t Channel)
{
/* Return error if channel not available */
if (Channel >= PIOS_PWM_NUM_INPUTS) {
if (Channel >= pios_pwm_cfg.num_channels) {
return -1;
}
return CaptureValue[Channel];
}
/**
* Handle TIM3 global interrupt request
*/
void TIM3_IRQHandler(void)
void PIOS_PWM_irq_handler(TIM_TypeDef * timer)
{
/* Zero value always will be changed but this prevents compiler warning */
int32_t i = 0;
uint16_t val = 0;
for(uint8_t i = 0; i < pios_pwm_cfg.num_channels; i++) {
struct pios_pwm_channel channel = pios_pwm_cfg.channels[i];
if ((channel.timer == timer) && (TIM_GetITStatus(channel.timer, channel.ccr) == SET)) {
/* Do this as it's more efficient */
if (TIM_GetITStatus(PIOS_PWM_TIM_PORT[4], PIOS_PWM_TIM_CCR[4]) == SET) {
i = 4;
if (CaptureState[i] == 0) {
RiseValue[i] = TIM_GetCapture4(PIOS_PWM_TIM_PORT[i]);
} else {
FallValue[i] = TIM_GetCapture4(PIOS_PWM_TIM_PORT[i]);
}
} else if (TIM_GetITStatus(PIOS_PWM_TIM_PORT[5], PIOS_PWM_TIM_CCR[5]) == SET) {
i = 5;
if (CaptureState[i] == 0) {
RiseValue[i] = TIM_GetCapture3(PIOS_PWM_TIM_PORT[i]);
} else {
FallValue[i] = TIM_GetCapture3(PIOS_PWM_TIM_PORT[i]);
}
} else if (TIM_GetITStatus(PIOS_PWM_TIM_PORT[6], PIOS_PWM_TIM_CCR[6]) == SET) {
i = 6;
if (CaptureState[i] == 0) {
RiseValue[i] = TIM_GetCapture1(PIOS_PWM_TIM_PORT[i]);
} else {
FallValue[i] = TIM_GetCapture1(PIOS_PWM_TIM_PORT[i]);
}
} else if (TIM_GetITStatus(PIOS_PWM_TIM_PORT[7], PIOS_PWM_TIM_CCR[7]) == SET) {
i = 7;
if (CaptureState[i] == 0) {
RiseValue[i] = TIM_GetCapture2(PIOS_PWM_TIM_PORT[i]);
} else {
FallValue[i] = TIM_GetCapture2(PIOS_PWM_TIM_PORT[i]);
}
TIM_ClearITPendingBit(channel.timer, channel.ccr);
switch(channel.channel) {
case TIM_Channel_1:
val = TIM_GetCapture1(channel.timer);
break;
case TIM_Channel_2:
val = TIM_GetCapture2(channel.timer);
break;
case TIM_Channel_3:
val = TIM_GetCapture3(channel.timer);
break;
case TIM_Channel_4:
val = TIM_GetCapture4(channel.timer);
break;
}
/* Clear TIM3 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(PIOS_PWM_TIM_PORT[i], PIOS_PWM_TIM_CCR[i]);
if (CaptureState[i] == 0) {
RiseValue[i] = val;
} else {
FallValue[i] = val;
}
// flip state machine and capture value here
/* Simple rise or fall state machine */
TIM_ICInitTypeDef TIM_ICInitStructure = pios_pwm_cfg.tim_ic_init;
if (CaptureState[i] == 0) {
/* Switch states */
CaptureState[i] = 1;
/* Switch polarity of input capture */
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_ICInitStructure.TIM_Channel = PIOS_PWM_TIM_CHANNEL[i];
TIM_ICInit(PIOS_PWM_TIM_PORT[i], &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = channel.channel;
TIM_ICInit(channel.timer, &TIM_ICInitStructure);
} else {
/* Capture computation */
if (FallValue[i] > RiseValue[i]) {
CaptureValue[i] = (FallValue[i] - RiseValue[i]);
} else {
CaptureValue[i] = ((0xFFFF - RiseValue[i]) + FallValue[i]);
CaptureValue[i] = ((channel.timer->ARR - RiseValue[i]) + FallValue[i]);
}
/* Switch states */
@ -257,77 +247,14 @@ void TIM3_IRQHandler(void)
/* Switch polarity of input capture */
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_Channel = PIOS_PWM_TIM_CHANNEL[i];
TIM_ICInit(PIOS_PWM_TIM_PORT[i], &TIM_ICInitStructure);
}
}
/**
* Handle TIM1 global interrupt request
*/
void TIM1_CC_IRQHandler(void)
{
/* Zero value always will be changed but this prevents compiler warning */
int32_t i = 0;
/* Do this as it's more efficient */
if (TIM_GetITStatus(PIOS_PWM_TIM_PORT[0], PIOS_PWM_TIM_CCR[0]) == SET) {
i = 0;
if (CaptureState[i] == 0) {
RiseValue[i] = TIM_GetCapture2(PIOS_PWM_TIM_PORT[i]);
} else {
FallValue[i] = TIM_GetCapture2(PIOS_PWM_TIM_PORT[i]);
}
} else if (TIM_GetITStatus(PIOS_PWM_TIM_PORT[1], PIOS_PWM_TIM_CCR[1]) == SET) {
i = 1;
if (CaptureState[i] == 0) {
RiseValue[i] = TIM_GetCapture3(PIOS_PWM_TIM_PORT[i]);
} else {
FallValue[i] = TIM_GetCapture3(PIOS_PWM_TIM_PORT[i]);
}
} else if (TIM_GetITStatus(PIOS_PWM_TIM_PORT[3], PIOS_PWM_TIM_CCR[3]) == SET) {
i = 3;
if (CaptureState[i] == 0) {
RiseValue[i] = TIM_GetCapture1(PIOS_PWM_TIM_PORT[i]);
} else {
FallValue[i] = TIM_GetCapture1(PIOS_PWM_TIM_PORT[i]);
}
}
/* Clear TIM3 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(PIOS_PWM_TIM_PORT[i], PIOS_PWM_TIM_CCR[i]);
/* Simple rise or fall state machine */
if (CaptureState[i] == 0) {
/* Switch states */
CaptureState[i] = 1;
/* Switch polarity of input capture */
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_ICInitStructure.TIM_Channel = PIOS_PWM_TIM_CHANNEL[i];
TIM_ICInit(PIOS_PWM_TIM_PORT[i], &TIM_ICInitStructure);
} else {
/* Capture computation */
if (FallValue[i] > RiseValue[i]) {
CaptureValue[i] = (FallValue[i] - RiseValue[i]);
} else {
CaptureValue[i] = ((0xFFFF - RiseValue[i]) + FallValue[i]);
}
/* Switch states */
CaptureState[i] = 0;
/* Increase supervisor counter */
CapCounter[i]++;
/* Switch polarity of input capture */
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_Channel = PIOS_PWM_TIM_CHANNEL[i];
TIM_ICInit(PIOS_PWM_TIM_PORT[i], &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = channel.channel;
TIM_ICInit(channel.timer, &TIM_ICInitStructure);
}
}
}
}
#if 0
/**
* Handle TIM5 global interrupt request
*/
@ -404,7 +331,7 @@ PIOS_PWM_SUPV_IRQ_FUNC {
SupervisorState = 0;
}
}
#endif
#endif
/**

View File

@ -89,19 +89,19 @@ void PIOS_Servo_Init(void)
/* Set up for output compare function */
switch(channel.channel) {
case 1:
case TIM_Channel_1:
TIM_OC1Init(channel.timer, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(channel.timer, TIM_OCPreload_Enable);
break;
case 2:
case TIM_Channel_2:
TIM_OC2Init(channel.timer, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(channel.timer, TIM_OCPreload_Enable);
break;
case 3:
case TIM_Channel_3:
TIM_OC3Init(channel.timer, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(channel.timer, TIM_OCPreload_Enable);
break;
case 4:
case TIM_Channel_4:
TIM_OC4Init(channel.timer, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(channel.timer, TIM_OCPreload_Enable);
break;

View File

@ -33,5 +33,6 @@
/* Public Functions */
extern void PIOS_PWM_Init(void);
extern int32_t PIOS_PWM_Get(int8_t Channel);
//extern void PIOS_PWM_irq_handler(TIM_TypeDef * timer);
#endif /* PIOS_PWM_H */

View File

@ -0,0 +1,65 @@
/**
******************************************************************************
* @addtogroup PIOS PIOS Core hardware abstraction layer
* @{
* @addtogroup PIOS_SERVO Servo Functions
* @brief PIOS interface to read and write from servo PWM ports
* @{
*
* @file pios_servo_priv.h
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @brief Servo private structures.
* @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
*/
#ifndef PIOS_PWM_PRIV_H
#define PIOS_PWM_PRIV_H
#include <pios.h>
#include <pios_stm32.h>
struct pios_pwm_channel {
TIM_TypeDef * timer;
GPIO_TypeDef * port;
uint16_t ccr;
uint8_t channel;
uint16_t pin;
};
struct pios_pwm_cfg {
TIM_TimeBaseInitTypeDef tim_base_init;
TIM_ICInitTypeDef tim_ic_init;
GPIO_InitTypeDef gpio_init;
uint32_t remap; /* GPIO_Remap_* */
struct stm32_irq irq;
const struct pios_pwm_channel *const channels;
uint8_t num_channels;
};
extern void PIOS_PWM_irq_handler(TIM_TypeDef * timer);
extern uint8_t pios_pwm_num_channels;
extern const struct pios_pwm_cfg pios_pwm_cfg;
#endif /* PIOS_PWM_PRIV_H */
/**
* @}
* @}
*/

View File

@ -186,6 +186,7 @@
657CEEBA121DBC63007A1FBE /* WorldMagModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WorldMagModel.h; sourceTree = "<group>"; };
657CEEBB121DBC63007A1FBE /* WorldMagModel.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = WorldMagModel.c; sourceTree = "<group>"; };
657CF024121F49CD007A1FBE /* WMMInternal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WMMInternal.h; sourceTree = "<group>"; };
657FF86A12EA8BFB00801617 /* pios_pwm_priv.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = pios_pwm_priv.h; sourceTree = "<group>"; };
659ED317122226B60011010E /* ahrssettings.xml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = ahrssettings.xml; sourceTree = "<group>"; };
65B35D7F121C261E003EAD18 /* bin.pro */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = bin.pro; sourceTree = "<group>"; };
65B35D80121C261E003EAD18 /* openpilotgcs */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = openpilotgcs; sourceTree = "<group>"; };