From 72a36b065ed656cd12c671078e5905549354025f Mon Sep 17 00:00:00 2001 From: bsongis Date: Sun, 11 May 2014 15:49:18 +0200 Subject: [PATCH] PWM Haptic --- radio/src/targets/taranis/hal.h | 1 + radio/src/targets/taranis/haptic_driver.cpp | 31 ++++++++++++++------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/radio/src/targets/taranis/hal.h b/radio/src/targets/taranis/hal.h index 4f84843a9..082e725c5 100644 --- a/radio/src/targets/taranis/hal.h +++ b/radio/src/targets/taranis/hal.h @@ -380,6 +380,7 @@ // Haptic #if defined(REVPLUS) #define RCC_AHB1Periph_GPIOHAPTIC RCC_AHB1Periph_GPIOB + #define GPIO_PinSource_HAPTIC GPIO_PinSource8 #define GPIO_HAPTIC GPIOB #define PIN_HAPTIC GPIO_Pin_8 #else diff --git a/radio/src/targets/taranis/haptic_driver.cpp b/radio/src/targets/taranis/haptic_driver.cpp index b459405c1..c6df245cd 100644 --- a/radio/src/targets/taranis/haptic_driver.cpp +++ b/radio/src/targets/taranis/haptic_driver.cpp @@ -39,8 +39,7 @@ void hapticOff(void) { #if defined(REVPLUS) - GPIO_ResetBits(GPIO_HAPTIC, PIN_HAPTIC); // Temp, TODO PWM support - return; + TIM10->CCR1 = 0; #else GPIO_ResetBits(GPIO_HAPTIC, PIN_HAPTIC); #endif @@ -48,9 +47,11 @@ void hapticOff(void) #if defined(REVPLUS) void hapticOn(uint32_t pwmPercent) -{ // pwmPercent 0-100 - GPIO_SetBits(GPIO_HAPTIC, PIN_HAPTIC); // Temp, TODO PWM support - return; +{ + if (pwmPercent > 100) { + pwmPercent = 100; + } + TIM10->CCR1 = pwmPercent; } #else void hapticOn() @@ -62,14 +63,24 @@ void hapticOn() void hapticInit(void) { - GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOHAPTIC, ENABLE); - - /* GPIO Configuration*/ + GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = PIN_HAPTIC; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIO_HAPTIC, &GPIO_InitStructure); + + GPIO_PinAFConfig(GPIO_HAPTIC, GPIO_PinSource_HAPTIC, GPIO_AF_TIM10); + + RCC->APB2ENR |= RCC_APB2ENR_TIM10EN ; // Enable clock + TIM10->ARR = 100 ; + TIM10->PSC = (PERI2_FREQUENCY * TIMER_MULT_APB2) / 10000 - 1; + TIM10->CCMR1 = TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2 ; // PWM + TIM10->CCER = TIM_CCER_CC1E ; + + TIM10->CCR1 = 0 ; + TIM10->EGR = 0 ; + TIM10->CR1 = TIM_CR1_CEN ; // Counter enable }