Rotary encoder refactoring

This commit is contained in:
Bertrand Songis
2019-04-30 14:55:21 +02:00
parent 4f6828aa1c
commit c28210e3e4
60 changed files with 363 additions and 652 deletions
@@ -8,7 +8,9 @@ if(PCB STREQUAL X12S OR PCB STREQUAL X10)
../f4/system_stm32f4xx.c
../../../../../targets/${TARGET_DIR}/startup_stm32f42_43xxx.s
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_rcc.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_syscfg.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_gpio.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_exti.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_spi.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmc.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_ltdc.c
@@ -23,7 +25,9 @@ elseif(PCB STREQUAL X9E)
../f4/system_stm32f4xx.c
../../../../../targets/${TARGET_DIR}/startup_stm32f40_41xxx.s
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_rcc.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_syscfg.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_gpio.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_exti.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_spi.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_i2c.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/misc.c
@@ -34,7 +38,9 @@ else()
../f2/system_stm32f2xx.c
../../../../../${STM32LIB_DIR}/CMSIS/Device/ST/STM32F2xx/Source/Templates/gcc_ride7/startup_stm32f2xx.s
../../../../../${STM32LIB_DIR}/STM32F2xx_StdPeriph_Driver/src/stm32f2xx_rcc.c
../../../../../${STM32LIB_DIR}/STM32F2xx_StdPeriph_Driver/src/stm32f2xx_syscfg.c
../../../../../${STM32LIB_DIR}/STM32F2xx_StdPeriph_Driver/src/stm32f2xx_gpio.c
../../../../../${STM32LIB_DIR}/STM32F2xx_StdPeriph_Driver/src/stm32f2xx_exti.c
../../../../../${STM32LIB_DIR}/STM32F2xx_StdPeriph_Driver/src/stm32f2xx_spi.c
../../../../../${STM32LIB_DIR}/STM32F2xx_StdPeriph_Driver/src/stm32f2xx_i2c.c
../../../../../${STM32LIB_DIR}/STM32F2xx_StdPeriph_Driver/src/misc.c
@@ -78,6 +84,13 @@ set(BOOTLOADER_SRC
bin_files.cpp
)
if(PCB STREQUAL X12S OR PCB STREQUAL X10 OR PCB STREQUAL X9E OR PCB STREQUAL X7 OR PCB STREQUAL X3)
set(BOOTLOADER_SRC
${BOOTLOADER_SRC}
../../../../../targets/common/arm/stm32/rotary_encoder_driver.cpp
)
endif()
if(NOT (PCB STREQUAL X10 OR PCB STREQUAL X12S))
set(BOOTLOADER_SRC
${BOOTLOADER_SRC}
@@ -50,7 +50,7 @@ const uint8_t bootloaderVersion[] __attribute__ ((section(".version"), used)) =
{ 'B', 'O', 'O', 'T', '1', '0' };
#if defined(ROTARY_ENCODER_NAVIGATION)
volatile rotenc_t rotencValue[1] = {0};
volatile rotenc_t rotencValue = 0;
#endif
/*----------------------------------------------------------------------------
@@ -86,18 +86,12 @@ void interrupt10ms(void)
}
#if defined(ROTARY_ENCODER_NAVIGATION)
checkRotaryEncoder();
static rotenc_t rePreviousValue;
rotenc_t reNewValue = (rotencValue[0] / ROTARY_ENCODER_GRANULARITY);
rotenc_t reNewValue = (rotencValue / ROTARY_ENCODER_GRANULARITY);
int8_t scrollRE = reNewValue - rePreviousValue;
if (scrollRE) {
rePreviousValue = reNewValue;
if (scrollRE < 0) {
putEvent(EVT_KEY_FIRST(KEY_UP)); //EVT_ROTARY_LEFT
}
else {
putEvent(EVT_KEY_FIRST(KEY_DOWN)); //EVT_ROTARY_RIGHT
}
putEvent(scrollRE < 0 ? EVT_KEY_FIRST(KEY_UP) : EVT_KEY_FIRST(KEY_DOWN));
}
#endif
}
@@ -221,6 +215,10 @@ int main()
keysInit();
#if defined(ROTARY_ENCODER_NAVIGATION)
rotaryEncoderInit();
#endif
// wait for inputs to stabilize
for (uint32_t i = 0; i < 50000; i += 1) {
wdt_reset();
@@ -11,8 +11,10 @@ set(STM32LIB_SRC
CMSIS/Device/ST/STM32F2xx/Source/Templates/gcc_ride7/startup_stm32f2xx.s
STM32F2xx_StdPeriph_Driver/src/misc.c
STM32F2xx_StdPeriph_Driver/src/stm32f2xx_gpio.c
STM32F2xx_StdPeriph_Driver/src/stm32f2xx_exti.c
STM32F2xx_StdPeriph_Driver/src/stm32f2xx_dbgmcu.c
STM32F2xx_StdPeriph_Driver/src/stm32f2xx_rcc.c
STM32F2xx_StdPeriph_Driver/src/stm32f2xx_syscfg.c
STM32F2xx_StdPeriph_Driver/src/stm32f2xx_spi.c
STM32F2xx_StdPeriph_Driver/src/stm32f2xx_i2c.c
STM32F2xx_StdPeriph_Driver/src/stm32f2xx_dma.c
@@ -10,8 +10,10 @@ set(STM32LIB_SRC
${STM32LIB_SRC}
STM32F4xx_StdPeriph_Driver/src/misc.c
STM32F4xx_StdPeriph_Driver/src/stm32f4xx_gpio.c
STM32F4xx_StdPeriph_Driver/src/stm32f4xx_exti.c
STM32F4xx_StdPeriph_Driver/src/stm32f4xx_dbgmcu.c
STM32F4xx_StdPeriph_Driver/src/stm32f4xx_rcc.c
STM32F4xx_StdPeriph_Driver/src/stm32f4xx_syscfg.c
STM32F4xx_StdPeriph_Driver/src/stm32f4xx_spi.c
STM32F4xx_StdPeriph_Driver/src/stm32f4xx_i2c.c
STM32F4xx_StdPeriph_Driver/src/stm32f4xx_dma.c
@@ -0,0 +1,103 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*/
#include "opentx.h"
uint32_t rotencPosition;
void rotaryEncoderInit()
{
SYSCFG_EXTILineConfig(ROTARY_ENCODER_EXTI_PortSource, ROTARY_ENCODER_EXTI_PinSource1);
#if defined(ROTARY_ENCODER_EXTI_LINE2)
SYSCFG_EXTILineConfig(ROTARY_ENCODER_EXTI_PortSource, ROTARY_ENCODER_EXTI_PinSource2);
#endif
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_StructInit(&EXTI_InitStructure);
EXTI_InitStructure.EXTI_Line = ROTARY_ENCODER_EXTI_LINE1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
#if defined(ROTARY_ENCODER_EXTI_LINE2)
EXTI_InitStructure.EXTI_Line = ROTARY_ENCODER_EXTI_LINE2;
EXTI_Init(&EXTI_InitStructure);
#endif
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = ROTARY_ENCODER_EXTI_IRQn1;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 8;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; /* Not used as 4 bits are used for the pre-emption priority. */;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#if defined(ROTARY_ENCODER_EXTI_IRQn2)
NVIC_InitStructure.NVIC_IRQChannel = ROTARY_ENCODER_EXTI_IRQn2;
NVIC_Init(&NVIC_InitStructure);
#endif
rotencPosition = ROTARY_ENCODER_POSITION();
}
void rotaryEncoderCheck()
{
uint32_t newpos = ROTARY_ENCODER_POSITION();
if (newpos != rotencPosition && !keyState(KEY_ENTER)) {
if ((rotencPosition & 0x01) ^ ((newpos & 0x02) >> 1)) {
--rotencValue;
}
else {
++rotencValue;
}
rotencPosition = newpos;
#if !defined(BOOT)
if (g_eeGeneral.backlightMode & e_backlight_mode_keys) {
backlightOn();
}
#endif
}
}
extern "C" void ROTARY_ENCODER_EXTI_IRQHandler1(void)
{
if (EXTI_GetITStatus(ROTARY_ENCODER_EXTI_LINE1) != RESET) {
rotaryEncoderCheck();
EXTI_ClearITPendingBit(ROTARY_ENCODER_EXTI_LINE1);
}
#if !defined(ROTARY_ENCODER_EXTI_IRQn2)
if (EXTI_GetITStatus(ROTARY_ENCODER_EXTI_LINE2) != RESET) {
rotaryEncoderCheck();
EXTI_ClearITPendingBit(ROTARY_ENCODER_EXTI_LINE2);
}
#endif
}
#if defined(ROTARY_ENCODER_EXTI_IRQn2)
extern "C" void ROTARY_ENCODER_EXTI_IRQHandler2(void)
{
if (EXTI_GetITStatus(ROTARY_ENCODER_EXTI_LINE2) != RESET) {
rotaryEncoderCheck();
EXTI_ClearITPendingBit(ROTARY_ENCODER_EXTI_LINE2);
}
}
#endif
+1 -1
View File
@@ -36,7 +36,6 @@ if (PCB STREQUAL X10)
../common/arm/stm32/adc_driver.cpp
../common/arm/stm32/sticks_pwm_driver.cpp
)
set(BITMAPS_TARGET x10_bitmaps)
set(FONTS_TARGET x10_fonts)
set(LCD_DRIVER lcd_driver.cpp)
@@ -133,6 +132,7 @@ set(TARGET_SRC
trainer_driver.cpp
../common/arm/stm32/intmodule_serial_driver.cpp
../common/arm/stm32/bluetooth_driver.cpp
../common/arm/stm32/rotary_encoder_driver.cpp
)
set(FIRMWARE_TARGET_SRC
+3 -5
View File
@@ -86,10 +86,6 @@ void interrupt1ms()
per10ms();
DEBUG_TIMER_STOP(debugTimerPer10ms);
}
DEBUG_TIMER_START(debugTimerRotEnc);
checkRotaryEncoder();
DEBUG_TIMER_STOP(debugTimerRotEnc);
}
extern "C" void INTERRUPT_xMS_IRQHandler()
@@ -168,7 +164,8 @@ void boardInit()
BACKLIGHT_RCC_APB1Periph,
ENABLE);
RCC_APB2PeriphClockCmd(LCD_RCC_APB2Periph |
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG |
LCD_RCC_APB2Periph |
ADC_RCC_APB2Periph |
HAPTIC_RCC_APB2Periph |
INTMODULE_RCC_APB2Periph |
@@ -196,6 +193,7 @@ void boardInit()
memset(&g_FATFS_Obj, 0, sizeof(g_FATFS_Obj));
keysInit();
rotaryEncoderInit();
#if NUM_PWMSTICKS > 0
sticksPwmInit();
+4 -1
View File
@@ -37,7 +37,9 @@ extern "C" {
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_rcc.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_syscfg.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_gpio.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_exti.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_spi.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_i2c.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_rtc.h"
@@ -344,7 +346,8 @@ uint32_t readTrims(void);
// Rotary encoder driver
#define ROTARY_ENCODER_NAVIGATION
void checkRotaryEncoder(void);
void rotaryEncoderInit(void);
void rotaryEncoderCheck(void);
// WDT driver
#define WDTO_500MS 500
+7
View File
@@ -62,6 +62,13 @@
#define ROTARY_ENCODER_GPIO_PIN_A GPIO_Pin_11 // PH.11
#define ROTARY_ENCODER_GPIO_PIN_B GPIO_Pin_10 // PH.10
#define ROTARY_ENCODER_POSITION() ((ROTARY_ENCODER_GPIO->IDR >> 10) & 0x03)
#define ROTARY_ENCODER_EXTI_LINE1 EXTI_Line11
#define ROTARY_ENCODER_EXTI_LINE2 EXTI_Line10
#define ROTARY_ENCODER_EXTI_IRQn1 EXTI15_10_IRQn
#define ROTARY_ENCODER_EXTI_IRQHandler1 EXTI15_10_IRQHandler
#define ROTARY_ENCODER_EXTI_PortSource EXTI_PortSourceGPIOH
#define ROTARY_ENCODER_EXTI_PinSource1 EXTI_PinSource11
#define ROTARY_ENCODER_EXTI_PinSource2 EXTI_PinSource10
// Switches
#define SWITCHES_GPIO_REG_A_H GPIOH->IDR
-62
View File
@@ -20,68 +20,6 @@
#include "opentx.h"
/*
* Rotary encoder handling based on state table:
* Copyright (C) by Ben Buxton
*
* This includes the state table definition bellow
* as well as the implementation of checkRotaryEncoder().
*
* The below state table has, for each state (row), the new state
* to set based on the next encoder output. From left to right in,
* the table, the encoder outputs are 00, 01, 10, 11, and the value
* in that position is the new state to set.
*/
#define R_START 0x0
#define DIR_CW 0x10
#define DIR_CCW 0x20
// Use the half-step state table (emits a code at 00 and 11)
#define R_CCW_BEGIN 0x1
#define R_CW_BEGIN 0x2
#define R_START_M 0x3
#define R_CW_BEGIN_M 0x4
#define R_CCW_BEGIN_M 0x5
const unsigned char rotenc_table[6][4] = {
// R_START (00)
{R_START_M, R_CW_BEGIN, R_CCW_BEGIN, R_START},
// R_CCW_BEGIN
{R_START_M | DIR_CCW, R_START, R_CCW_BEGIN, R_START},
// R_CW_BEGIN
{R_START_M | DIR_CW, R_CW_BEGIN, R_START, R_START},
// R_START_M (11)
{R_START_M, R_CCW_BEGIN_M, R_CW_BEGIN_M, R_START},
// R_CW_BEGIN_M
{R_START_M, R_START_M, R_CW_BEGIN_M, R_START | DIR_CW},
// R_CCW_BEGIN_M
{R_START_M, R_CCW_BEGIN_M, R_START_M, R_START | DIR_CCW},
};
void checkRotaryEncoder()
{
static uint8_t state = 0;
uint32_t pins = ROTARY_ENCODER_POSITION();
state = rotenc_table[state & 0x0F][pins];
if ((state & 0x30) && !keyState(KEY_ENTER)) {
if ((state & 0x30) == DIR_CW) {
--rotencValue[0];
}
else {
++rotencValue[0];
}
#if !defined(BOOT)
if (g_eeGeneral.backlightMode & e_backlight_mode_keys)
backlightOn();
#endif
}
}
/*
* End of the rotary encoder handler code
*/
uint32_t readKeys()
{
uint32_t result = 0;
@@ -362,9 +362,6 @@ const int OpenTxSimulator::getCapability(Capability cap)
break;
case CAP_ROTARY_ENC :
#ifdef ROTARY_ENCODERS
ret = ROTARY_ENCODERS;
#endif
break;
case CAP_ROTARY_ENC_NAV :
+9 -6
View File
@@ -150,9 +150,8 @@ void simuInit()
for (int i = 0; i < 2*NUM_TRIMS; i++)
simuSetTrim(i, false);
#if defined(ROTARY_ENCODERS) || defined(ROTARY_ENCODER_NAVIGATION)
for (uint8_t i=0; i < DIM(rotencValue); i++)
rotencValue[i] = 0;
#if defined(ROTARY_ENCODER_NAVIGATION)
rotencValue = 0;
#endif
}
@@ -225,9 +224,6 @@ void simuSetKey(uint8_t key, bool state)
KEY_CASE(KEY_LEFT, KEYS_GPIO_REG_LEFT, KEYS_GPIO_PIN_LEFT)
KEY_CASE(KEY_UP, KEYS_GPIO_REG_UP, KEYS_GPIO_PIN_UP)
KEY_CASE(KEY_DOWN, KEYS_GPIO_REG_DOWN, KEYS_GPIO_PIN_DOWN)
#endif
#if defined(PCBSKY9X) && !defined(REVX) && !defined(AR9X) && defined(ROTARY_ENCODERS)
KEY_CASE(BTN_REa, PIOB->PIO_PDSR, 0x40)
#endif
}
}
@@ -692,6 +688,13 @@ void RCC_LSEConfig(uint8_t RCC_LSE) { }
void RCC_GetClocksFreq(RCC_ClocksTypeDef* RCC_Clocks) { };
FlagStatus RCC_GetFlagStatus(uint8_t RCC_FLAG) { return SET; }
// EXTI fake functions
void SYSCFG_EXTILineConfig(uint8_t EXTI_PortSourceGPIOx, uint8_t EXTI_PinSourcex) { }
void EXTI_StructInit(EXTI_InitTypeDef* EXTI_InitStruct) { }
ITStatus EXTI_GetITStatus(uint32_t EXTI_Line) { return RESET; }
void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct) { }
void EXTI_ClearITPendingBit(uint32_t EXTI_Line) { }
// RTC fake functions
ErrorStatus RTC_Init(RTC_InitTypeDef* RTC_InitStruct) { return SUCCESS; }
void RTC_TimeStructInit(RTC_TimeTypeDef* RTC_TimeStruct) { }
+1 -4
View File
@@ -475,10 +475,7 @@ void boardInit()
eepromInit();
#if defined(ROTARY_ENCODERS)
rotencInit();
#endif
rotaryEncoderInit();
init_SDcard();
}
#else
+2 -7
View File
@@ -38,11 +38,10 @@ void boardInit(void);
#define boardOff() pwrOff()
// Rotary Encoder driver
void rotencInit();
void rotencEnd();
void rotaryEncoderInit();
void rotaryEncoderEnd();
#if !defined(REVX) && !defined(AR9X)
#define ROTARY_ENCODERS 1
#define ROTARY_ENCODER_NAVIGATION
#define REA_DOWN() (!(PIOB->PIO_PDSR & 0x40))
#else
@@ -74,10 +73,6 @@ enum EnumKeys
TRM_RH_UP,
TRM_LAST = TRM_RH_UP,
#if defined(ROTARY_ENCODERS)
BTN_REa,
#endif
NUM_KEYS
};
-4
View File
@@ -109,10 +109,6 @@ void readKeysAndTrims()
{
uint32_t i;
#if ROTARY_ENCODERS > 0
keys[BTN_REa].input(REA_DOWN());
#endif
uint8_t index = 0;
uint8_t keys_input = readKeys();
for (i = 1; i < 7; i++) {
+20 -20
View File
@@ -20,38 +20,38 @@
#include "opentx.h"
void rotencInit()
void rotaryEncoderInit()
{
configure_pins( PIO_PC19 | PIO_PC21, PIN_ENABLE | PIN_INPUT | PIN_PORTC | PIN_PULLUP ) ; // 19 and 21 are rotary encoder
configure_pins( PIO_PB6, PIN_ENABLE | PIN_INPUT | PIN_PORTB | PIN_PULLUP ) ; // rotary encoder switch
PIOC->PIO_IER = PIO_PC19 | PIO_PC21 ;
NVIC_EnableIRQ(PIOC_IRQn) ;
configure_pins(PIO_PC19 | PIO_PC21, PIN_ENABLE | PIN_INPUT | PIN_PORTC | PIN_PULLUP); // 19 and 21 are rotary encoder
configure_pins(PIO_PB6, PIN_ENABLE | PIN_INPUT | PIN_PORTB | PIN_PULLUP); // rotary encoder switch
PIOC->PIO_IER = PIO_PC19 | PIO_PC21;
NVIC_EnableIRQ(PIOC_IRQn);
}
void rotencEnd()
void rotaryEncoderEnd()
{
NVIC_DisableIRQ(PIOC_IRQn) ;
PIOC->PIO_IDR = PIO_PC19 | PIO_PC21 ;
NVIC_DisableIRQ(PIOC_IRQn);
PIOC->PIO_IDR = PIO_PC19 | PIO_PC21;
}
volatile uint32_t Rotary_position ;
volatile uint32_t Rotary_position;
extern "C" void PIOC_IRQHandler()
{
uint32_t dummy;
dummy = PIOC->PIO_ISR ; // Read and clear status register
(void) dummy ; // Discard value - prevents compiler warning
dummy = PIOC->PIO_ISR; // Read and clear status register
(void) dummy; // Discard value - prevents compiler warning
dummy = PIOC->PIO_PDSR ; // Read Rotary encoder (PC19, PC21)
dummy >>= 19 ;
dummy &= 0x05 ; // pick out the three bits
if ( dummy != ( Rotary_position & 0x05 ) ) {
if ( ( Rotary_position & 0x01 ) ^ ( ( dummy & 0x04) >> 2 ) )
incRotaryEncoder(0, -1);
dummy = PIOC->PIO_PDSR; // Read Rotary encoder (PC19, PC21)
dummy >>= 19;
dummy &= 0x05; // pick out the three bits
if (dummy != (Rotary_position & 0x05)) {
if ((Rotary_position & 0x01) ^ ((dummy & 0x04) >> 2))
rotencValue -= 1;
else
incRotaryEncoder(0, +1);
rotencValue += 1;
Rotary_position &= ~0x45 ;
Rotary_position |= dummy ;
Rotary_position &= ~0x45;
Rotary_position |= dummy;
}
}
+7
View File
@@ -188,6 +188,13 @@ if(NOT PCB STREQUAL XLITE)
)
endif()
if(PCB STREQUAL X9E OR PCB STREQUAL X7 OR PCB STREQUAL X3)
set(TARGET_SRC
${TARGET_SRC}
../common/arm/stm32/rotary_encoder_driver.cpp
)
endif()
if(PCB STREQUAL X9E OR PCB STREQUAL X7 OR PCB STREQUAL XLITE OR PCB STREQUAL XLITES)
add_definitions(-DBLUETOOTH)
set(TARGET_SRC
+49 -23
View File
@@ -88,10 +88,6 @@ void interrupt5ms()
per10ms();
DEBUG_TIMER_STOP(debugTimerPer10ms);
}
#if defined(ROTARY_ENCODER_NAVIGATION)
checkRotaryEncoder();
#endif
}
#if !defined(SIMU)
@@ -143,28 +139,53 @@ void sportUpdatePowerOff()
void boardInit()
{
#if !defined(SIMU)
RCC_AHB1PeriphClockCmd(PWR_RCC_AHB1Periph | PCBREV_RCC_AHB1Periph |
KEYS_RCC_AHB1Periph | LCD_RCC_AHB1Periph |
AUDIO_RCC_AHB1Periph | BACKLIGHT_RCC_AHB1Periph |
ADC_RCC_AHB1Periph | I2C_RCC_AHB1Periph |
SD_RCC_AHB1Periph | HAPTIC_RCC_AHB1Periph |
INTMODULE_RCC_AHB1Periph | EXTMODULE_RCC_AHB1Periph |
TELEMETRY_RCC_AHB1Periph | SPORT_UPDATE_RCC_AHB1Periph |
SERIAL_RCC_AHB1Periph | TRAINER_RCC_AHB1Periph |
HEARTBEAT_RCC_AHB1Periph | BT_RCC_AHB1Periph | GYRO_RCC_AHB1Periph,
RCC_AHB1PeriphClockCmd(PWR_RCC_AHB1Periph |
PCBREV_RCC_AHB1Periph |
KEYS_RCC_AHB1Periph |
LCD_RCC_AHB1Periph |
AUDIO_RCC_AHB1Periph |
BACKLIGHT_RCC_AHB1Periph |
ADC_RCC_AHB1Periph |
I2C_RCC_AHB1Periph |
SD_RCC_AHB1Periph |
HAPTIC_RCC_AHB1Periph |
INTMODULE_RCC_AHB1Periph |
EXTMODULE_RCC_AHB1Periph |
TELEMETRY_RCC_AHB1Periph |
SPORT_UPDATE_RCC_AHB1Periph |
SERIAL_RCC_AHB1Periph |
TRAINER_RCC_AHB1Periph |
HEARTBEAT_RCC_AHB1Periph |
BT_RCC_AHB1Periph |
GYRO_RCC_AHB1Periph,
ENABLE);
RCC_APB1PeriphClockCmd(LCD_RCC_APB1Periph | AUDIO_RCC_APB1Periph | ADC_RCC_APB1Periph |
BACKLIGHT_RCC_APB1Periph | HAPTIC_RCC_APB1Periph | INTERRUPT_xMS_RCC_APB1Periph |
TIMER_2MHz_RCC_APB1Periph | I2C_RCC_APB1Periph |
SD_RCC_APB1Periph | TRAINER_RCC_APB1Periph |
TELEMETRY_RCC_APB1Periph | SERIAL_RCC_APB1Periph |
INTMODULE_RCC_APB1Periph | BT_RCC_APB1Periph | GYRO_RCC_APB1Periph, ENABLE);
RCC_APB1PeriphClockCmd(LCD_RCC_APB1Periph |
AUDIO_RCC_APB1Periph |
ADC_RCC_APB1Periph |
BACKLIGHT_RCC_APB1Periph |
HAPTIC_RCC_APB1Periph |
INTERRUPT_xMS_RCC_APB1Periph |
TIMER_2MHz_RCC_APB1Periph |
I2C_RCC_APB1Periph |
SD_RCC_APB1Periph |
TRAINER_RCC_APB1Periph |
TELEMETRY_RCC_APB1Periph |
SERIAL_RCC_APB1Periph |
INTMODULE_RCC_APB1Periph |
BT_RCC_APB1Periph |
GYRO_RCC_APB1Periph,
ENABLE);
RCC_APB2PeriphClockCmd(BACKLIGHT_RCC_APB2Periph | ADC_RCC_APB2Periph |
HAPTIC_RCC_APB2Periph | INTMODULE_RCC_APB2Periph |
EXTMODULE_RCC_APB2Periph | HEARTBEAT_RCC_APB2Periph |
BT_RCC_APB2Periph, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG |
BACKLIGHT_RCC_APB2Periph |
ADC_RCC_APB2Periph |
HAPTIC_RCC_APB2Periph |
INTMODULE_RCC_APB2Periph |
EXTMODULE_RCC_APB2Periph |
HEARTBEAT_RCC_APB2Periph |
BT_RCC_APB2Periph,
ENABLE);
#if defined(BLUETOOTH)
bluetoothInit(BLUETOOTH_DEFAULT_BAUDRATE, true);
@@ -181,6 +202,11 @@ void boardInit()
#endif
keysInit();
#if defined(ROTARY_ENCODER_NAVIGATION)
rotaryEncoderInit();
#endif
delaysInit();
#if NUM_PWMSTICKS > 0
+6 -1
View File
@@ -39,7 +39,9 @@ extern "C" {
#if defined(STM32F4)
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_rcc.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_syscfg.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_gpio.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_exti.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_tim.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_adc.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_spi.h"
@@ -54,7 +56,9 @@ extern "C" {
#else
#include "STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/CMSIS/Device/ST/STM32F2xx/Include/stm32f2xx.h"
#include "STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/inc/stm32f2xx_rcc.h"
#include "STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/inc/stm32f2xx_syscfg.h"
#include "STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/inc/stm32f2xx_gpio.h"
#include "STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/inc/stm32f2xx_exti.h"
#include "STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/inc/stm32f2xx_tim.h"
#include "STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/inc/stm32f2xx_adc.h"
#include "STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/inc/stm32f2xx_spi.h"
@@ -420,7 +424,8 @@ uint32_t readTrims(void);
#if defined(PCBX9E) || defined(PCBX7) || defined(PCBX3)
// Rotary Encoder driver
#define ROTARY_ENCODER_NAVIGATION
void checkRotaryEncoder(void);
void rotaryEncoderInit(void);
void rotaryEncoderCheck(void);
#endif
// WDT driver
+41 -18
View File
@@ -81,28 +81,51 @@
// Rotary Encoder
#if defined(PCBX9E)
#define ENC_GPIO GPIOD
#define ENC_GPIO_PIN_A GPIO_Pin_12 // PD.12
#define ENC_GPIO_PIN_B GPIO_Pin_13 // PD.13
#define ROTARY_ENCODER_POSITION() (ENC_GPIO->IDR >> 12) & 0x03
#define ROTARY_ENCODER_GPIO GPIOD
#define ROTARY_ENCODER_GPIO_PIN_A GPIO_Pin_12 // PD.12
#define ROTARY_ENCODER_GPIO_PIN_B GPIO_Pin_13 // PD.13
#define ROTARY_ENCODER_POSITION() (ROTARY_ENCODER_GPIO->IDR >> 12) & 0x03
#define ROTARY_ENCODER_EXTI_LINE1 EXTI_Line12
#define ROTARY_ENCODER_EXTI_LINE2 EXTI_Line13
#define ROTARY_ENCODER_EXTI_IRQn1 EXTI15_10_IRQn
#define ROTARY_ENCODER_EXTI_IRQHandler1 EXTI15_10_IRQHandler
#define ROTARY_ENCODER_EXTI_PortSource EXTI_PortSourceGPIOD
#define ROTARY_ENCODER_EXTI_PinSource1 EXTI_PinSource12
#define ROTARY_ENCODER_EXTI_PinSource2 EXTI_PinSource13
#elif defined(PCBX7)
#define ENC_GPIO GPIOE
#define ENC_GPIO_PIN_A GPIO_Pin_9 // PE.09
#define ENC_GPIO_PIN_B GPIO_Pin_11 // PE.11
#define ROTARY_ENCODER_POSITION() (((ENC_GPIO->IDR >> 10) & 0x02) + ((ENC_GPIO->IDR >> 9) & 0x01))
#define ROTARY_ENCODER_GPIO GPIOE
#define ROTARY_ENCODER_GPIO_PIN_A GPIO_Pin_9 // PE.09
#define ROTARY_ENCODER_GPIO_PIN_B GPIO_Pin_11 // PE.11
#define ROTARY_ENCODER_POSITION() (((ROTARY_ENCODER_GPIO->IDR >> 10) & 0x02) + ((ROTARY_ENCODER_GPIO->IDR >> 9) & 0x01))
#define ROTARY_ENCODER_EXTI_LINE1 EXTI_Line9
#define ROTARY_ENCODER_EXTI_LINE2 EXTI_Line11
#define ROTARY_ENCODER_EXTI_IRQn1 EXTI9_5_IRQn
#define ROTARY_ENCODER_EXTI_IRQHandler1 EXTI9_5_IRQHandler
#define ROTARY_ENCODER_EXTI_IRQn2 EXTI15_10_IRQn
#define ROTARY_ENCODER_EXTI_IRQHandler2 EXTI15_10_IRQHandler
#define ROTARY_ENCODER_EXTI_PortSource EXTI_PortSourceGPIOE
#define ROTARY_ENCODER_EXTI_PinSource1 EXTI_PinSource9
#define ROTARY_ENCODER_EXTI_PinSource2 EXTI_PinSource11
#elif defined(PCBX3)
#define ENC_GPIO GPIOE
#define ENC_GPIO_PIN_A GPIO_Pin_10 // PE.10
#define ENC_GPIO_PIN_B GPIO_Pin_12 // PE.12
#define ROTARY_ENCODER_POSITION() (((ENC_GPIO->IDR >> 12) & 0x01) + ((ENC_GPIO->IDR >> 9) & 0x02))
#define ROTARY_ENCODER_GPIO GPIOE
#define ROTARY_ENCODER_GPIO_PIN_A GPIO_Pin_10 // PE.10
#define ROTARY_ENCODER_GPIO_PIN_B GPIO_Pin_12 // PE.12
#define ROTARY_ENCODER_POSITION() (((ROTARY_ENCODER_GPIO->IDR >> 12) & 0x01) + ((ROTARY_ENCODER_GPIO->IDR >> 9) & 0x02))
#define ROTARY_ENCODER_EXTI_LINE1 EXTI_Line10
#define ROTARY_ENCODER_EXTI_LINE2 EXTI_Line12
#define ROTARY_ENCODER_EXTI_IRQn1 EXTI15_10_IRQn
#define ROTARY_ENCODER_EXTI_IRQHandler1 EXTI15_10_IRQHandler
#define ROTARY_ENCODER_EXTI_PortSource EXTI_PortSourceGPIOE
#define ROTARY_ENCODER_EXTI_PinSource1 EXTI_PinSource10
#define ROTARY_ENCODER_EXTI_PinSource2 EXTI_PinSource12
#endif
// This is for SIMU: reuse rotary encoder pins to map UP and DOWN keyboard keys
#if defined(SIMU) && (defined(PCBX9E) || defined(PCBX7) || defined(PCBX3))
#define KEYS_GPIO_REG_PLUS ENC_GPIO->IDR
#define KEYS_GPIO_PIN_PLUS ENC_GPIO_PIN_A
#define KEYS_GPIO_REG_MINUS ENC_GPIO->IDR
#define KEYS_GPIO_PIN_MINUS ENC_GPIO_PIN_B
#define KEYS_GPIO_REG_PLUS ROTARY_ENCODER_GPIO->IDR
#define KEYS_GPIO_PIN_PLUS ROTARY_ENCODER_GPIO_PIN_A
#define KEYS_GPIO_REG_MINUS ROTARY_ENCODER_GPIO->IDR
#define KEYS_GPIO_PIN_MINUS ROTARY_ENCODER_GPIO_PIN_B
#endif
// Trims
@@ -403,7 +426,7 @@
#define KEYS_GPIOA_PINS (SWITCHES_GPIO_PIN_M_L)
#define KEYS_GPIOB_PINS (SWITCHES_GPIO_PIN_N_H | SWITCHES_GPIO_PIN_N_L | SWITCHES_GPIO_PIN_R_H)
#define KEYS_GPIOC_PINS (TRIMS_GPIO_PIN_RVD | TRIMS_GPIO_PIN_RVU | TRIMS_GPIO_PIN_RHL | TRIMS_GPIO_PIN_RHR)
#define KEYS_GPIOD_PINS (KEYS_GPIO_PIN_MENU | KEYS_GPIO_PIN_EXIT | KEYS_GPIO_PIN_PAGE | SWITCHES_GPIO_PIN_A_H | SWITCHES_GPIO_PIN_A_L | ENC_GPIO_PIN_A | ENC_GPIO_PIN_B)
#define KEYS_GPIOD_PINS (KEYS_GPIO_PIN_MENU | KEYS_GPIO_PIN_EXIT | KEYS_GPIO_PIN_PAGE | SWITCHES_GPIO_PIN_A_H | SWITCHES_GPIO_PIN_A_L | ROTARY_ENCODER_GPIO_PIN_A | ROTARY_ENCODER_GPIO_PIN_B)
#define KEYS_GPIOE_PINS (TRIMS_GPIO_PIN_LVU | TRIMS_GPIO_PIN_LVD | SWITCHES_GPIO_PIN_D_H | SWITCHES_GPIO_PIN_D_L| SWITCHES_GPIO_PIN_E_H | SWITCHES_GPIO_PIN_E_L | SWITCHES_GPIO_PIN_F | SWITCHES_GPIO_PIN_I_L | SWITCHES_GPIO_PIN_L_H | SWITCHES_GPIO_PIN_L_L | SWITCHES_GPIO_PIN_M_H | SWITCHES_GPIO_PIN_O_L | SWITCHES_GPIO_PIN_R_L)
#define KEYS_GPIOF_PINS (KEYS_GPIO_PIN_ENTER | SWITCHES_GPIO_PIN_C_H | SWITCHES_GPIO_PIN_C_L | SWITCHES_GPIO_PIN_G_H | SWITCHES_GPIO_PIN_G_L | SWITCHES_GPIO_PIN_H | SWITCHES_GPIO_PIN_I_H | SWITCHES_GPIO_PIN_O_H | SWITCHES_GPIO_PIN_P_H | SWITCHES_GPIO_PIN_P_L | SWITCHES_GPIO_PIN_Q_H | SWITCHES_GPIO_PIN_Q_L)
#define KEYS_GPIOG_PINS (TRIMS_GPIO_PIN_LHL | TRIMS_GPIO_PIN_LHR| SWITCHES_GPIO_PIN_B_H | SWITCHES_GPIO_PIN_B_L | SWITCHES_GPIO_PIN_J_H | SWITCHES_GPIO_PIN_J_L | SWITCHES_GPIO_PIN_K_H | SWITCHES_GPIO_PIN_K_L)
@@ -431,7 +454,7 @@
#define KEYS_GPIOA_PINS SWITCHES_GPIO_PIN_B_H
#define KEYS_GPIOC_PINS (TRIMS_GPIO_PIN_LHR | TRIMS_GPIO_PIN_RVD | TRIMS_GPIO_PIN_RVU)
#define KEYS_GPIOD_PINS (TRIMS_GPIO_PIN_LHL | KEYS_GPIO_PIN_MENU | KEYS_GPIO_PIN_EXIT | KEYS_GPIO_PIN_PAGE | SWITCHES_GPIO_PIN_C_L | SWITCHES_GPIO_PIN_H)
#define KEYS_GPIOE_PINS (ENC_GPIO_PIN_A | ENC_GPIO_PIN_B | KEYS_GPIO_PIN_ENTER | TRIMS_GPIO_PIN_RHR | TRIMS_GPIO_PIN_RHL | TRIMS_GPIO_PIN_LVD | TRIMS_GPIO_PIN_LVU | SWITCHES_GPIO_PIN_C_H | SWITCHES_GPIO_PIN_D_L | SWITCHES_GPIO_PIN_D_H | SWITCHES_GPIO_PIN_B_L | SWITCHES_GPIO_PIN_A_L | SWITCHES_GPIO_PIN_A_H | SWITCHES_GPIO_PIN_F)
#define KEYS_GPIOE_PINS (ROTARY_ENCODER_GPIO_PIN_A | ROTARY_ENCODER_GPIO_PIN_B | KEYS_GPIO_PIN_ENTER | TRIMS_GPIO_PIN_RHR | TRIMS_GPIO_PIN_RHL | TRIMS_GPIO_PIN_LVD | TRIMS_GPIO_PIN_LVU | SWITCHES_GPIO_PIN_C_H | SWITCHES_GPIO_PIN_D_L | SWITCHES_GPIO_PIN_D_H | SWITCHES_GPIO_PIN_B_L | SWITCHES_GPIO_PIN_A_L | SWITCHES_GPIO_PIN_A_H | SWITCHES_GPIO_PIN_F)
#elif defined(PCBX3)
#define KEYS_RCC_AHB1Periph (RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE)
#define KEYS_GPIOA_PINS (GPIO_Pin_5)
-29
View File
@@ -20,10 +20,6 @@
#include "opentx.h"
#if defined(ROTARY_ENCODER_NAVIGATION)
uint32_t rotencPosition;
#endif
uint32_t readKeys()
{
uint32_t result = 0;
@@ -114,27 +110,6 @@ bool keyDown()
return readKeys() || readTrims();
}
#if defined(ROTARY_ENCODER_NAVIGATION)
void checkRotaryEncoder()
{
uint32_t newpos = ROTARY_ENCODER_POSITION();
if (newpos != rotencPosition && !keyState(KEY_ENTER)) {
if ((rotencPosition & 0x01) ^ ((newpos & 0x02) >> 1)) {
--rotencValue[0];
}
else {
++rotencValue[0];
}
rotencPosition = newpos;
#if !defined(BOOT)
if (g_eeGeneral.backlightMode & e_backlight_mode_keys) {
backlightOn();
}
#endif
}
}
#endif
/* TODO common to ARM */
void readKeysAndTrims()
{
@@ -289,8 +264,4 @@ void keysInit()
GPIO_InitStructure.GPIO_Pin = KEYS_GPIOG_PINS;
GPIO_Init(GPIOG, &GPIO_InitStructure);
#endif
#if defined(ROTARY_ENCODER_NAVIGATION)
rotencPosition = ROTARY_ENCODER_POSITION();
#endif
}