From 782d8316da4023af53d9b60e89c95b60c215139b Mon Sep 17 00:00:00 2001 From: Hydra Date: Thu, 17 Nov 2016 18:44:21 +0100 Subject: [PATCH] Move transponder to use IO/Timer configuraton. Initialisation now matches LED strip code. --- src/main/drivers/resource.c | 1 + src/main/drivers/resource.h | 1 + src/main/drivers/timer.h | 17 +-- src/main/drivers/transponder_ir.c | 23 ++-- src/main/drivers/transponder_ir.h | 5 +- src/main/drivers/transponder_ir_stm32f30x.c | 128 ++++++++++---------- 6 files changed, 90 insertions(+), 85 deletions(-) diff --git a/src/main/drivers/resource.c b/src/main/drivers/resource.c index 1b5eb25cb..170535b1a 100644 --- a/src/main/drivers/resource.c +++ b/src/main/drivers/resource.c @@ -58,5 +58,6 @@ const char * const ownerNames[OWNER_TOTAL_COUNT] = { "RX_BIND", "INVERTER", "LED_STRIP", + "TRANSPONDER" }; diff --git a/src/main/drivers/resource.h b/src/main/drivers/resource.h index e554f9845..a8dd0f012 100644 --- a/src/main/drivers/resource.h +++ b/src/main/drivers/resource.h @@ -58,6 +58,7 @@ typedef enum { OWNER_RX_BIND, OWNER_INVERTER, OWNER_LED_STRIP, + OWNER_TRANSPONDER, OWNER_TOTAL_COUNT } resourceOwner_e; diff --git a/src/main/drivers/timer.h b/src/main/drivers/timer.h index ead0649cd..3d198f67b 100644 --- a/src/main/drivers/timer.h +++ b/src/main/drivers/timer.h @@ -55,13 +55,14 @@ typedef uint32_t timCNT_t; #endif typedef enum { - TIM_USE_ANY = 0x0, - TIM_USE_NONE = 0x0, - TIM_USE_PPM = 0x1, - TIM_USE_PWM = 0x2, - TIM_USE_MOTOR = 0x4, - TIM_USE_SERVO = 0x8, - TIM_USE_LED = 0x10 + TIM_USE_ANY = 0x0, + TIM_USE_NONE = 0x0, + TIM_USE_PPM = 0x1, + TIM_USE_PWM = 0x2, + TIM_USE_MOTOR = 0x4, + TIM_USE_SERVO = 0x8, + TIM_USE_LED = 0x10, + TIM_USE_TRANSPONDER = 0x20 } timerUsageFlag_e; // use different types from capture and overflow - multiple overflow handlers are implemented as linked list @@ -194,4 +195,4 @@ void timerOCPreloadConfig(TIM_TypeDef *tim, uint8_t channel, uint16_t preload); #endif volatile timCCR_t *timerCCR(TIM_TypeDef *tim, uint8_t channel); -uint16_t timerDmaSource(uint8_t channel); \ No newline at end of file +uint16_t timerDmaSource(uint8_t channel); diff --git a/src/main/drivers/transponder_ir.c b/src/main/drivers/transponder_ir.c index dbc6749af..e56a51d61 100644 --- a/src/main/drivers/transponder_ir.c +++ b/src/main/drivers/transponder_ir.c @@ -23,6 +23,8 @@ #include "dma.h" #include "nvic.h" +#include "io.h" +#include "timer.h" #include "transponder_ir.h" /* @@ -37,20 +39,19 @@ uint8_t transponderIrDMABuffer[TRANSPONDER_DMA_BUFFER_SIZE]; volatile uint8_t transponderIrDataTransferInProgress = 0; -void transponderDMAHandler(dmaChannelDescriptor_t* descriptor) -{ - if (DMA_GET_FLAG_STATUS(descriptor, DMA_IT_TCIF)) { - transponderIrDataTransferInProgress = 0; - DMA_Cmd(descriptor->channel, DISABLE); - DMA_CLEAR_FLAG(descriptor, DMA_IT_TCIF); - } -} - void transponderIrInit(void) { memset(&transponderIrDMABuffer, 0, TRANSPONDER_DMA_BUFFER_SIZE); - dmaSetHandler(TRANSPONDER_DMA_HANDLER_IDENTIFER, transponderDMAHandler, NVIC_PRIO_TRANSPONDER_DMA, 0); - transponderIrHardwareInit(); + + ioTag_t ioTag = IO_TAG_NONE; + for (int i = 0; i < USABLE_TIMER_CHANNEL_COUNT; i++) { + if (timerHardware[i].usageFlags & TIM_USE_TRANSPONDER) { + ioTag = timerHardware[i].tag; + break; + } + } + + transponderIrHardwareInit(ioTag); } bool isTransponderIrReady(void) diff --git a/src/main/drivers/transponder_ir.h b/src/main/drivers/transponder_ir.h index c21bdec8c..253d1d9a5 100644 --- a/src/main/drivers/transponder_ir.h +++ b/src/main/drivers/transponder_ir.h @@ -17,6 +17,7 @@ #pragma once +#include "io_types.h" #define TRANSPONDER_BITS_PER_BYTE 10 // start + 8 data + stop #define TRANSPONDER_DATA_LENGTH 6 @@ -29,10 +30,10 @@ #define BIT_TOGGLE_1 78 // (156 / 2) #define BIT_TOGGLE_0 0 -void transponderIrInit(void); +void transponderIrInit(); void transponderIrDisable(void); -void transponderIrHardwareInit(void); +void transponderIrHardwareInit(ioTag_t ioTag); void transponderIrDMAEnable(void); void transponderIrWaitForTransmitComplete(void); diff --git a/src/main/drivers/transponder_ir_stm32f30x.c b/src/main/drivers/transponder_ir_stm32f30x.c index 0bff2e112..6b2125c7c 100644 --- a/src/main/drivers/transponder_ir_stm32f30x.c +++ b/src/main/drivers/transponder_ir_stm32f30x.c @@ -20,46 +20,53 @@ #include -#include "gpio.h" -#include "transponder_ir.h" +#include "io.h" #include "nvic.h" -#ifndef TRANSPONDER_GPIO -#define USE_TRANSPONDER_ON_DMA1_CHANNEL3 -#define TRANSPONDER_GPIO GPIOB -#define TRANSPONDER_GPIO_AHB_PERIPHERAL RCC_AHBPeriph_GPIOB -#define TRANSPONDER_GPIO_AF GPIO_AF_1 -#define TRANSPONDER_PIN GPIO_Pin_8 // TIM16_CH1 -#define TRANSPONDER_PIN_SOURCE GPIO_PinSource8 -#define TRANSPONDER_TIMER TIM16 -#define TRANSPONDER_TIMER_APB2_PERIPHERAL RCC_APB2Periph_TIM16 -#define TRANSPONDER_DMA_CHANNEL DMA1_Channel3 -#define TRANSPONDER_IRQ DMA1_Channel3_IRQn -#define TRANSPONDER_DMA_TC_FLAG DMA1_FLAG_TC3 -#define TRANSPONDER_DMA_HANDLER_IDENTIFER DMA1_CH3_HANDLER -#endif +#include "dma.h" +#include "rcc.h" +#include "timer.h" -void transponderIrHardwareInit(void) +#include "transponder_ir.h" + +static IO_t transponderIO = IO_NONE; +static DMA_Channel_TypeDef *dmaChannel = NULL; +static TIM_TypeDef *timer = NULL; + +static void TRANSPONDER_DMA_IRQHandler(dmaChannelDescriptor_t* descriptor) { + if (DMA_GET_FLAG_STATUS(descriptor, DMA_IT_TCIF)) { + transponderIrDataTransferInProgress = 0; + DMA_Cmd(descriptor->channel, DISABLE); + DMA_CLEAR_FLAG(descriptor, DMA_IT_TCIF); + } +} + + +void transponderIrHardwareInit(ioTag_t ioTag) +{ + if (!ioTag) { + return; + } + TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; - GPIO_InitTypeDef GPIO_InitStructure; DMA_InitTypeDef DMA_InitStructure; - RCC_AHBPeriphClockCmd(TRANSPONDER_GPIO_AHB_PERIPHERAL, ENABLE); + const timerHardware_t *timerHardware = timerGetByTag(ioTag, TIM_USE_ANY); + timer = timerHardware->tim; - GPIO_PinAFConfig(TRANSPONDER_GPIO, TRANSPONDER_PIN_SOURCE, TRANSPONDER_GPIO_AF); + if (timerHardware->dmaChannel == NULL) { + return; + } - /* Configuration alternate function push-pull */ - GPIO_StructInit(&GPIO_InitStructure); - GPIO_InitStructure.GPIO_Pin = TRANSPONDER_PIN; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; - GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_Init(TRANSPONDER_GPIO, &GPIO_InitStructure); + transponderIO = IOGetByTag(ioTag); + IOInit(transponderIO, OWNER_TRANSPONDER, 0); + IOConfigGPIOAF(transponderIO, IO_CONFIG(GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_PP, GPIO_PuPd_DOWN), timerHardware->alternateFunction); - RCC_APB2PeriphClockCmd(TRANSPONDER_TIMER_APB2_PERIPHERAL, ENABLE); + dmaInit(timerHardware->dmaIrqHandler, OWNER_TRANSPONDER, 0); + dmaSetHandler(timerHardware->dmaIrqHandler, TRANSPONDER_DMA_IRQHandler, NVIC_PRIO_TRANSPONDER_DMA, 0); + RCC_ClockCmd(timerRCC(timer), ENABLE); /* Time base configuration */ TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); @@ -67,32 +74,31 @@ void transponderIrHardwareInit(void) TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; - TIM_TimeBaseInit(TRANSPONDER_TIMER, &TIM_TimeBaseStructure); + TIM_TimeBaseInit(timer, &TIM_TimeBaseStructure); /* PWM1 Mode configuration: Channel1 */ TIM_OCStructInit(&TIM_OCInitStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; - TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; + if (timerHardware->output & TIMER_OUTPUT_N_CHANNEL) { + TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; + TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; + } else { + TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; + TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; + } + TIM_OCInitStructure.TIM_OCPolarity = (timerHardware->output & TIMER_OUTPUT_INVERTED) ? TIM_OCPolarity_Low : TIM_OCPolarity_High; TIM_OCInitStructure.TIM_Pulse = 0; -#ifdef TRANSPONDER_INVERTED - TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; -#else - TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; -#endif - TIM_OC1Init(TRANSPONDER_TIMER, &TIM_OCInitStructure); - TIM_OC1PreloadConfig(TRANSPONDER_TIMER, TIM_OCPreload_Enable); + TIM_OC1Init(timer, &TIM_OCInitStructure); + TIM_OC1PreloadConfig(timer, TIM_OCPreload_Enable); - TIM_CtrlPWMOutputs(TRANSPONDER_TIMER, ENABLE); + TIM_CtrlPWMOutputs(timer, ENABLE); /* configure DMA */ - /* DMA clock enable */ - //RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); - - /* DMA1 Channel6 Config */ - DMA_DeInit(TRANSPONDER_DMA_CHANNEL); + dmaChannel = timerHardware->dmaChannel; + DMA_DeInit(dmaChannel); DMA_StructInit(&DMA_InitStructure); - DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&TRANSPONDER_TIMER->CCR1; + DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)timerCCR(timer, timerHardware->channel); DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)transponderIrDMABuffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = TRANSPONDER_DMA_BUFFER_SIZE; @@ -104,40 +110,34 @@ void transponderIrHardwareInit(void) DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; - DMA_Init(TRANSPONDER_DMA_CHANNEL, &DMA_InitStructure); + DMA_Init(dmaChannel, &DMA_InitStructure); - TIM_DMACmd(TRANSPONDER_TIMER, TIM_DMA_CC1, ENABLE); + TIM_DMACmd(timer, timerDmaSource(timerHardware->channel), ENABLE); - DMA_ITConfig(TRANSPONDER_DMA_CHANNEL, DMA_IT_TC, ENABLE); + DMA_ITConfig(dmaChannel, DMA_IT_TC, ENABLE); } void transponderIrDMAEnable(void) { - DMA_SetCurrDataCounter(TRANSPONDER_DMA_CHANNEL, TRANSPONDER_DMA_BUFFER_SIZE); // load number of bytes to be transferred - TIM_SetCounter(TRANSPONDER_TIMER, 0); - TIM_Cmd(TRANSPONDER_TIMER, ENABLE); - DMA_Cmd(TRANSPONDER_DMA_CHANNEL, ENABLE); + DMA_SetCurrDataCounter(dmaChannel, TRANSPONDER_DMA_BUFFER_SIZE); // load number of bytes to be transferred + TIM_SetCounter(timer, 0); + TIM_Cmd(timer, ENABLE); + DMA_Cmd(dmaChannel, ENABLE); } void transponderIrDisable(void) { - GPIO_InitTypeDef GPIO_InitStructure; + DMA_Cmd(dmaChannel, DISABLE); + TIM_Cmd(timer, DISABLE); - DMA_Cmd(TRANSPONDER_DMA_CHANNEL, DISABLE); - TIM_Cmd(TRANSPONDER_TIMER, DISABLE); + IOInit(transponderIO, OWNER_TRANSPONDER, 0); + IOConfigGPIOAF(transponderIO, IO_CONFIG(GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_PP, GPIO_PuPd_DOWN), timerHardware->alternateFunction); - GPIO_StructInit(&GPIO_InitStructure); - GPIO_InitStructure.GPIO_Pin = TRANSPONDER_PIN; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; - GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_Init(TRANSPONDER_GPIO, &GPIO_InitStructure); #ifdef TRANSPONDER_INVERTED - digitalHi(TRANSPONDER_GPIO, TRANSPONDER_PIN); + IOHi(transponderIO); #else - digitalLo(TRANSPONDER_GPIO, TRANSPONDER_PIN); + IOLo(transponderIO); #endif }