Move transponder to use IO/Timer configuraton.

Initialisation now matches LED strip code.
This commit is contained in:
Hydra 2016-11-17 18:44:21 +01:00
parent 135231f289
commit 782d8316da
6 changed files with 90 additions and 85 deletions

View File

@ -58,5 +58,6 @@ const char * const ownerNames[OWNER_TOTAL_COUNT] = {
"RX_BIND", "RX_BIND",
"INVERTER", "INVERTER",
"LED_STRIP", "LED_STRIP",
"TRANSPONDER"
}; };

View File

@ -58,6 +58,7 @@ typedef enum {
OWNER_RX_BIND, OWNER_RX_BIND,
OWNER_INVERTER, OWNER_INVERTER,
OWNER_LED_STRIP, OWNER_LED_STRIP,
OWNER_TRANSPONDER,
OWNER_TOTAL_COUNT OWNER_TOTAL_COUNT
} resourceOwner_e; } resourceOwner_e;

View File

@ -55,13 +55,14 @@ typedef uint32_t timCNT_t;
#endif #endif
typedef enum { typedef enum {
TIM_USE_ANY = 0x0, TIM_USE_ANY = 0x0,
TIM_USE_NONE = 0x0, TIM_USE_NONE = 0x0,
TIM_USE_PPM = 0x1, TIM_USE_PPM = 0x1,
TIM_USE_PWM = 0x2, TIM_USE_PWM = 0x2,
TIM_USE_MOTOR = 0x4, TIM_USE_MOTOR = 0x4,
TIM_USE_SERVO = 0x8, TIM_USE_SERVO = 0x8,
TIM_USE_LED = 0x10 TIM_USE_LED = 0x10,
TIM_USE_TRANSPONDER = 0x20
} timerUsageFlag_e; } timerUsageFlag_e;
// use different types from capture and overflow - multiple overflow handlers are implemented as linked list // 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 #endif
volatile timCCR_t *timerCCR(TIM_TypeDef *tim, uint8_t channel); volatile timCCR_t *timerCCR(TIM_TypeDef *tim, uint8_t channel);
uint16_t timerDmaSource(uint8_t channel); uint16_t timerDmaSource(uint8_t channel);

View File

@ -23,6 +23,8 @@
#include "dma.h" #include "dma.h"
#include "nvic.h" #include "nvic.h"
#include "io.h"
#include "timer.h"
#include "transponder_ir.h" #include "transponder_ir.h"
/* /*
@ -37,20 +39,19 @@ uint8_t transponderIrDMABuffer[TRANSPONDER_DMA_BUFFER_SIZE];
volatile uint8_t transponderIrDataTransferInProgress = 0; 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) void transponderIrInit(void)
{ {
memset(&transponderIrDMABuffer, 0, TRANSPONDER_DMA_BUFFER_SIZE); 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) bool isTransponderIrReady(void)

View File

@ -17,6 +17,7 @@
#pragma once #pragma once
#include "io_types.h"
#define TRANSPONDER_BITS_PER_BYTE 10 // start + 8 data + stop #define TRANSPONDER_BITS_PER_BYTE 10 // start + 8 data + stop
#define TRANSPONDER_DATA_LENGTH 6 #define TRANSPONDER_DATA_LENGTH 6
@ -29,10 +30,10 @@
#define BIT_TOGGLE_1 78 // (156 / 2) #define BIT_TOGGLE_1 78 // (156 / 2)
#define BIT_TOGGLE_0 0 #define BIT_TOGGLE_0 0
void transponderIrInit(void); void transponderIrInit();
void transponderIrDisable(void); void transponderIrDisable(void);
void transponderIrHardwareInit(void); void transponderIrHardwareInit(ioTag_t ioTag);
void transponderIrDMAEnable(void); void transponderIrDMAEnable(void);
void transponderIrWaitForTransmitComplete(void); void transponderIrWaitForTransmitComplete(void);

View File

@ -20,46 +20,53 @@
#include <platform.h> #include <platform.h>
#include "gpio.h" #include "io.h"
#include "transponder_ir.h"
#include "nvic.h" #include "nvic.h"
#ifndef TRANSPONDER_GPIO #include "dma.h"
#define USE_TRANSPONDER_ON_DMA1_CHANNEL3 #include "rcc.h"
#define TRANSPONDER_GPIO GPIOB #include "timer.h"
#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
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_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_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 */ transponderIO = IOGetByTag(ioTag);
GPIO_StructInit(&GPIO_InitStructure); IOInit(transponderIO, OWNER_TRANSPONDER, 0);
GPIO_InitStructure.GPIO_Pin = TRANSPONDER_PIN; IOConfigGPIOAF(transponderIO, IO_CONFIG(GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_PP, GPIO_PuPd_DOWN), timerHardware->alternateFunction);
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);
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 */ /* Time base configuration */
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
@ -67,32 +74,31 @@ void transponderIrHardwareInit(void)
TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TRANSPONDER_TIMER, &TIM_TimeBaseStructure); TIM_TimeBaseInit(timer, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */ /* PWM1 Mode configuration: Channel1 */
TIM_OCStructInit(&TIM_OCInitStructure); TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 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; TIM_OCInitStructure.TIM_Pulse = 0;
#ifdef TRANSPONDER_INVERTED TIM_OC1Init(timer, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC1PreloadConfig(timer, TIM_OCPreload_Enable);
#else
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
#endif
TIM_OC1Init(TRANSPONDER_TIMER, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TRANSPONDER_TIMER, TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs(TRANSPONDER_TIMER, ENABLE); TIM_CtrlPWMOutputs(timer, ENABLE);
/* configure DMA */ /* configure DMA */
/* DMA clock enable */ dmaChannel = timerHardware->dmaChannel;
//RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); DMA_DeInit(dmaChannel);
/* DMA1 Channel6 Config */
DMA_DeInit(TRANSPONDER_DMA_CHANNEL);
DMA_StructInit(&DMA_InitStructure); 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_MemoryBaseAddr = (uint32_t)transponderIrDMABuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = TRANSPONDER_DMA_BUFFER_SIZE; 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_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; 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) void transponderIrDMAEnable(void)
{ {
DMA_SetCurrDataCounter(TRANSPONDER_DMA_CHANNEL, TRANSPONDER_DMA_BUFFER_SIZE); // load number of bytes to be transferred DMA_SetCurrDataCounter(dmaChannel, TRANSPONDER_DMA_BUFFER_SIZE); // load number of bytes to be transferred
TIM_SetCounter(TRANSPONDER_TIMER, 0); TIM_SetCounter(timer, 0);
TIM_Cmd(TRANSPONDER_TIMER, ENABLE); TIM_Cmd(timer, ENABLE);
DMA_Cmd(TRANSPONDER_DMA_CHANNEL, ENABLE); DMA_Cmd(dmaChannel, ENABLE);
} }
void transponderIrDisable(void) void transponderIrDisable(void)
{ {
GPIO_InitTypeDef GPIO_InitStructure; DMA_Cmd(dmaChannel, DISABLE);
TIM_Cmd(timer, DISABLE);
DMA_Cmd(TRANSPONDER_DMA_CHANNEL, DISABLE); IOInit(transponderIO, OWNER_TRANSPONDER, 0);
TIM_Cmd(TRANSPONDER_TIMER, DISABLE); 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 #ifdef TRANSPONDER_INVERTED
digitalHi(TRANSPONDER_GPIO, TRANSPONDER_PIN); IOHi(transponderIO);
#else #else
digitalLo(TRANSPONDER_GPIO, TRANSPONDER_PIN); IOLo(transponderIO);
#endif #endif
} }