1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-12-01 09:24:10 +01:00

PIOS_PWM now reading 8 channels properly!

git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@269 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
gussy 2010-03-08 10:09:30 +00:00 committed by gussy
parent 96a4ea1a39
commit 4f5d7ae83a
3 changed files with 294 additions and 79 deletions

View File

@ -217,39 +217,63 @@ TIM8 | Servo 5 | Servo 6 | Servo 7 | Servo 8
//-------------------------
// Receiver PWM inputs
//-------------------------
#define RECEIVER1_GPIO_PORT GPIOB
#define RECEIVER1_PIN GPIO_Pin_0 // PB0
#define RECEIVER1_TIM_PORT TIM3
#define RECEIVER1_CH TIM_Channel_3 // TIM3_CH3
#define RECEIVER2_GPIO_PORT GPIOB
#define RECEIVER2_PIN GPIO_Pin_1 // PB1
#define RECEIVER2_TIM_PORT TIM3
#define RECEIVER2_CH TIM_Channel_4 // TIM3_CH4
#define RECEIVER1_GPIO_PORT GPIOA
#define RECEIVER1_PIN GPIO_Pin_9
#define RECEIVER1_TIM_PORT TIM1
#define RECEIVER1_CH TIM_Channel_2
#define RECEIVER1_CCR TIM_IT_CC2
#define RECEIVER2_GPIO_PORT GPIOA
#define RECEIVER2_PIN GPIO_Pin_10
#define RECEIVER2_TIM_PORT TIM1
#define RECEIVER2_CH TIM_Channel_3
#define RECEIVER2_CCR TIM_IT_CC3
#define RECEIVER3_GPIO_PORT GPIOA
#define RECEIVER3_PIN GPIO_Pin_8 // PA8
#define RECEIVER3_TIM_PORT TIM1
#define RECEIVER3_CH TIM_Channel_1 // TIM1_CH1
#define RECEIVER3_PIN GPIO_Pin_0
#define RECEIVER3_TIM_PORT TIM5
#define RECEIVER3_CH TIM_Channel_1
#define RECEIVER3_CCR TIM_IT_CC1
#define RECEIVER4_GPIO_PORT GPIOA
#define RECEIVER4_PIN GPIO_Pin_0 // PA0
#define RECEIVER4_TIM_PORT TIM5
#define RECEIVER4_CH TIM_Channel_1 // TIM5_CH1
#define RECEIVER5_GPIO_PORT GPIOA
#define RECEIVER5_PIN GPIO_Pin_10 // PA10
#define RECEIVER5_TIM_PORT TIM1
#define RECEIVER5_CH TIM_Channel_3 // TIM1_CH3
#define RECEIVER6_GPIO_PORT GPIOA
#define RECEIVER6_PIN GPIO_Pin_9 // PA9
#define RECEIVER6_TIM_PORT TIM1
#define RECEIVER6_CH TIM_Channel_2 // TIM1_CH2
#define RECEIVER4_PIN GPIO_Pin_8
#define RECEIVER4_TIM_PORT TIM1
#define RECEIVER4_CH TIM_Channel_1
#define RECEIVER4_CCR TIM_IT_CC1
#define RECEIVER5_GPIO_PORT GPIOB
#define RECEIVER5_PIN GPIO_Pin_1
#define RECEIVER5_TIM_PORT TIM3
#define RECEIVER5_CH TIM_Channel_4
#define RECEIVER5_CCR TIM_IT_CC4
#define RECEIVER6_GPIO_PORT GPIOB
#define RECEIVER6_PIN GPIO_Pin_0
#define RECEIVER6_TIM_PORT TIM3
#define RECEIVER6_CH TIM_Channel_3
#define RECEIVER6_CCR TIM_IT_CC3
#define RECEIVER7_GPIO_PORT GPIOB
#define RECEIVER7_PIN GPIO_Pin_4 // PB4
#define RECEIVER7_PIN GPIO_Pin_4
#define RECEIVER7_TIM_PORT TIM3
#define RECEIVER7_CH TIM_Channel_1 // TIM3_CH1
#define RECEIVER7_CH TIM_Channel_1
#define RECEIVER7_CCR TIM_IT_CC1
#define RECEIVER8_GPIO_PORT GPIOB
#define RECEIVER8_PIN GPIO_Pin_5 // PB5
#define RECEIVER8_PIN GPIO_Pin_5
#define RECEIVER8_TIM_PORT TIM3
#define RECEIVER8_CH TIM_Channel_2 // TIM3_CH2
#define PIOS_PWM_NUM_INPUTS 1
#define RECEIVER8_CH TIM_Channel_2
#define RECEIVER8_CCR TIM_IT_CC2
#define PIOS_PWM_GPIO_PORTS { RECEIVER1_GPIO_PORT, RECEIVER2_GPIO_PORT, RECEIVER3_GPIO_PORT, RECEIVER4_GPIO_PORT, RECEIVER5_GPIO_PORT, RECEIVER6_GPIO_PORT, RECEIVER7_GPIO_PORT, RECEIVER8_GPIO_PORT }
#define PIOS_PWM_GPIO_PINS { RECEIVER1_PIN, RECEIVER2_PIN, RECEIVER3_PIN, RECEIVER4_PIN, RECEIVER5_PIN, RECEIVER6_PIN, RECEIVER7_PIN, RECEIVER8_PIN }
#define PIOS_PWM_TIM_PORTS { RECEIVER1_TIM_PORT, RECEIVER2_TIM_PORT, RECEIVER3_TIM_PORT, RECEIVER4_TIM_PORT, RECEIVER5_TIM_PORT, RECEIVER6_TIM_PORT, RECEIVER7_TIM_PORT, RECEIVER8_TIM_PORT }
#define PIOS_PWM_TIM_CHANNELS { RECEIVER1_CH, RECEIVER2_CH, RECEIVER3_CH, RECEIVER4_CH, RECEIVER5_CH, RECEIVER6_CH, RECEIVER7_CH, RECEIVER8_CH }
#define PIOS_PWM_TIM_CCRS { RECEIVER1_CCR, RECEIVER2_CCR, RECEIVER3_CCR, RECEIVER4_CCR, RECEIVER5_CCR, RECEIVER6_CCR, RECEIVER7_CCR, RECEIVER8_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_TIMER TIM6
#define PIOS_PWM_SUPV_TIMER_RCC_FUNC RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE)

View File

@ -101,12 +101,12 @@ int main()
PIOS_BMP085_Init();
//PIOS_Servo_SetHz(50, 500);
PIOS_Servo_SetHz(50, 450);
/* Create a FreeRTOS task */
xTaskCreate(TaskTick, (signed portCHAR *)"Test", configMINIMAL_STACK_SIZE , NULL, 1, NULL);
xTaskCreate(TaskTesting, (signed portCHAR *)"TaskTesting", configMINIMAL_STACK_SIZE , NULL, 4, NULL);
xTaskCreate(TaskServos, (signed portCHAR *)"Servos", configMINIMAL_STACK_SIZE , NULL, 3, NULL);
//xTaskCreate(TaskServos, (signed portCHAR *)"Servos", configMINIMAL_STACK_SIZE , NULL, 3, NULL);
//xTaskCreate(TaskSDCard, (signed portCHAR *)"SDCard", configMINIMAL_STACK_SIZE, NULL, (tskIDLE_PRIORITY + 2), NULL);
/* Start the FreeRTOS scheduler */
@ -142,7 +142,7 @@ static void TaskTick(void *pvParameters)
static void TaskTesting(void *pvParameters)
{
portTickType xDelay = 1000 / portTICK_RATE_MS;
portTickType xDelay = 250 / portTICK_RATE_MS;
portTickType xTimeout = 10 / portTICK_RATE_MS;
for(;;)
@ -160,7 +160,7 @@ static void TaskTesting(void *pvParameters)
PIOS_COM_SendFormattedStringNonBlocking(COM_DEBUG_USART, "%u\r", PIOS_BMP085_GetPressure());
*/
PIOS_COM_SendFormattedStringNonBlocking(COM_DEBUG_USART, "%u uS\r", PIOS_PWM_Get(0));
PIOS_COM_SendFormattedStringNonBlocking(COM_DEBUG_USART, "%u,%u,%u,%u,%u,%u,%u,%u uS\r", PIOS_PWM_Get(0), PIOS_PWM_Get(1), PIOS_PWM_Get(2), PIOS_PWM_Get(3), PIOS_PWM_Get(4), PIOS_PWM_Get(5), PIOS_PWM_Get(6), PIOS_PWM_Get(7));
/* This blocks the task until there is something on the buffer */
/*xSemaphoreTake(PIOS_USART1_Buffer, portMAX_DELAY);
@ -213,7 +213,7 @@ static void TaskServos(void *pvParameters)
/* Used to test servos, cycles all servos from one side to the other */
for(;;) {
xDelay = 250 / portTICK_RATE_MS;
/*xDelay = 250 / portTICK_RATE_MS;
PIOS_Servo_Set(1, 2000);
vTaskDelay(xDelay);
PIOS_Servo_Set(2, 2000);
@ -246,7 +246,7 @@ static void TaskServos(void *pvParameters)
PIOS_Servo_Set(2, 1000);
vTaskDelay(xDelay);
PIOS_Servo_Set(1, 1000);
vTaskDelay(xDelay);
vTaskDelay(xDelay);*/
xDelay = 1 / portTICK_RATE_MS;
for(int i = 1000; i < 2000; i++) {

View File

@ -32,6 +32,15 @@
#if !defined(PIOS_DONT_USE_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 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];
@ -63,53 +72,61 @@ void PIOS_PWM_Init(void)
}
/* Setup RCC */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
/* Enable timer interrupts */
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
for(i = 0; i < PIOS_PWM_NUM_TIMS; i++) {
NVIC_InitStructure.NVIC_IRQChannel = PIOS_PWM_TIM_IRQ[i];
NVIC_Init(&NVIC_InitStructure);
}
/* Partial pin remap for PB5 */
/* 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;
GPIO_InitStructure.GPIO_Pin = RECEIVER8_PIN;
GPIO_Init(RECEIVER8_GPIO_PORT, &GPIO_InitStructure);
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);
}
/* Configure timer for input capture */
TIM_ICInitTypeDef TIM_ICInitStructure;
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;
TIM_ICInitStructure.TIM_Channel = RECEIVER8_CH;
TIM_ICInit(RECEIVER8_TIM_PORT, &TIM_ICInitStructure);
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);
}
/* Configure timer clocks */
TIM_InternalClockConfig(RECEIVER8_TIM_PORT);
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;
TIM_TimeBaseInit(RECEIVER8_TIM_PORT, &TIM_TimeBaseStructure);
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);
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(RECEIVER8_TIM_PORT, TIM_IT_CC2, ENABLE);
/* Enable the Capture Compare Interrupt Request */
TIM_ITConfig(PIOS_PWM_TIM_PORT[i], PIOS_PWM_TIM_CCR[i], ENABLE);
}
/* Enable timers */
TIM_Cmd(RECEIVER8_TIM_PORT, ENABLE);
for(i = 0; i < PIOS_PWM_NUM_TIMS; i++) {
TIM_Cmd(PIOS_PWM_TIM[i], ENABLE);
}
/* Supervisor Setup */
#if (PIOS_PWM_SUPV_ENABLED)
@ -148,6 +165,12 @@ void PIOS_PWM_Init(void)
/* Enable counter */
TIM_Cmd(PIOS_PWM_SUPV_TIMER, ENABLE);
#endif
/* Setup local variable which stays in this scope */
/* Doing this here and using a local variable saves doing it in the ISR */
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
}
/**
@ -166,58 +189,226 @@ int32_t PIOS_PWM_Get(int8_t Channel)
}
/**
* This function handles TIM3 global interrupt request.
* Handle TIM3 global interrupt request
*/
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(RECEIVER8_TIM_PORT, TIM_IT_CC2) == SET) {
int32_t i;
/* For now we skip the generic non-application stuff for speed reasons */
//for(i = 0; i < PIOS_PWM_NUM_INPUTS; i++) {
// if(TIM_GetITStatus(PIOS_PWM_TIM_PORT[i], PIOS_PWM_TIM_CCR[i]) == SET) {
// /* This identifies which interrupt we are processing */
// break;
// }
//}
/* 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]);
}
}
/* 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) {
/* Get the Input Capture value */
//switch(PIOS_PWM_TIM_CHANNEL[i]) {
// case TIM_Channel_1:
// RiseValue[i] = TIM_GetCapture1(PIOS_PWM_TIM_PORT[i]);
// break;
// case TIM_Channel_2:
// RiseValue[i] = TIM_GetCapture2(PIOS_PWM_TIM_PORT[i]);
// break;
// case TIM_Channel_3:
// RiseValue[i] = TIM_GetCapture3(PIOS_PWM_TIM_PORT[i]);
// break;
// case TIM_Channel_4:
// RiseValue[i] = TIM_GetCapture4(PIOS_PWM_TIM_PORT[i]);
// break;
//}
/* 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 {
/* Get the Input Capture value */
//switch(PIOS_PWM_TIM_CHANNEL[i]) {
// case TIM_Channel_1:
// FallValue[i] = TIM_GetCapture1(PIOS_PWM_TIM_PORT[i]);
// break;
// case TIM_Channel_2:
// FallValue[i] = TIM_GetCapture2(PIOS_PWM_TIM_PORT[i]);
// break;
// case TIM_Channel_3:
// FallValue[i] = TIM_GetCapture3(PIOS_PWM_TIM_PORT[i]);
// break;
// case TIM_Channel_4:
// FallValue[i] = TIM_GetCapture4(PIOS_PWM_TIM_PORT[i]);
// break;
//}
/* 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);
}
}
/**
* Handle TIM1 global interrupt request
*/
void TIM1_CC_IRQHandler(void)
{
int32_t i;
/* 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);
}
}
/**
* Handle TIM5 global interrupt request
*/
void TIM5_IRQHandler(void)
{
/* Do this as it's more efficient */
if(TIM_GetITStatus(PIOS_PWM_TIM_PORT[2], PIOS_PWM_TIM_CCR[2]) == SET) {
if(CaptureState[2] == 0) {
RiseValue[2] = TIM_GetCapture1(PIOS_PWM_TIM_PORT[2]);
} else {
FallValue[2] = TIM_GetCapture1(PIOS_PWM_TIM_PORT[2]);
}
/* Clear TIM3 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(RECEIVER8_TIM_PORT, TIM_IT_CC2);
TIM_ClearITPendingBit(PIOS_PWM_TIM_PORT[2], PIOS_PWM_TIM_CCR[2]);
/* Simple rise or fall state machine */
if(CaptureState[0] == 0) {
/* Get the Input Capture value */
RiseValue[0] = TIM_GetCapture2(RECEIVER8_TIM_PORT);
if(CaptureState[2] == 0) {
/* Switch states */
CaptureState[0] = 1;
CaptureState[2] = 1;
/* Switch polarity of input capture */
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_ICInitStructure.TIM_Channel = RECEIVER8_CH;
TIM_ICInit(RECEIVER8_TIM_PORT, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = PIOS_PWM_TIM_CHANNEL[2];
TIM_ICInit(PIOS_PWM_TIM_PORT[2], &TIM_ICInitStructure);
} else {
/* Get the Input Capture value */
FallValue[0] = TIM_GetCapture2(RECEIVER8_TIM_PORT);
/* Capture computation */
if (FallValue[0] > RiseValue[0]) {
CaptureValue[0] = (FallValue[0] - RiseValue[0]);
if (FallValue[2] > RiseValue[2]) {
CaptureValue[2] = (FallValue[2] - RiseValue[2]);
} else {
CaptureValue[0] = ((0xFFFF - RiseValue[0]) + FallValue[0]);
CaptureValue[2] = ((0xFFFF - RiseValue[2]) + FallValue[2]);
}
/* Switch states */
CaptureState[0] = 0;
CaptureState[2] = 0;
/* Increase supervisor counter */
CapCounter[0]++;
CapCounter[2]++;
/* Switch polarity of input capture */
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_Channel = RECEIVER8_CH;
TIM_ICInit(RECEIVER8_TIM_PORT, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = PIOS_PWM_TIM_CHANNEL[2];
TIM_ICInit(PIOS_PWM_TIM_PORT[2], &TIM_ICInitStructure);
}
}
}