Skip to content

Commit

Permalink
chore: Update NV14/EL18 touch driver to generic EXTI driver (#4159)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardclli authored Oct 10, 2023
1 parent f4f04c1 commit b050440
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 113 deletions.
2 changes: 1 addition & 1 deletion radio/src/targets/nv14/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ set(FIRMWARE_TARGET_SRC
set(FIRMWARE_SRC
${FIRMWARE_SRC}
hal/adc_driver.cpp
targets/common/arm/stm32/stm32_adc.cpp
targets/common/arm/stm32/stm32_exti_driver.cpp
targets/common/arm/stm32/timers_driver.cpp
targets/common/arm/stm32/stm32_pulse_driver.cpp
targets/common/arm/stm32/stm32_usart_driver.cpp
Expand Down
11 changes: 8 additions & 3 deletions radio/src/targets/nv14/hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,9 @@
#define I2C_B1_GPIO GPIOB
#define I2C_B1_SCL_GPIO_PIN LL_GPIO_PIN_8 // PB.08
#define I2C_B1_SDA_GPIO_PIN LL_GPIO_PIN_7 // PB.09
#define I2C_B1_GPIO_AF GPIO_AF4_I2C1
#define I2C_B1_GPIO_AF LL_GPIO_AF_4

// Touch
#define TOUCH_I2C_BUS I2C_Bus_1
#define TOUCH_I2C_CLK_RATE 400000

Expand All @@ -373,8 +374,12 @@
#define TOUCH_INT_EXTI_Line LL_EXTI_LINE_9
#define TOUCH_INT_EXTI_Port LL_SYSCFG_EXTI_PORTB
#define TOUCH_INT_EXTI_SysCfgLine LL_SYSCFG_EXTI_LINE9
#define TOUCH_INT_EXTI_IRQn EXTI9_5_IRQn
#define TOUCH_INT_EXTI_IRQHandler EXTI9_5_IRQHandler

// TOUCH_INT_EXTI IRQ
#if !defined(USE_EXTI9_5_IRQ)
#define USE_EXTI9_5_IRQ
#define EXTI9_5_IRQ_Priority 9
#endif

// Haptic: TIM1_CH1
#define HAPTIC_PWM
Expand Down
153 changes: 44 additions & 109 deletions radio/src/targets/nv14/touch_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@

#include "stm32_hal_ll.h"
#include "stm32_hal.h"
#include "hal.h"

#include "stm32_i2c_driver.h"
#include "stm32_gpio_driver.h"
#include "stm32_exti_driver.h"

#include "hal.h"
#include "timers_driver.h"
#include "delays_driver.h"
#include "touch_driver.h"
Expand All @@ -36,12 +36,6 @@ volatile static bool touchEventOccured;

#define FT6x06_MAX_INSTANCE 1

#define TOUCH_RCC_AHB1Periph RCC_AHB1Periph_GPIOB
#define TOUCH_RCC_APB1Periph RCC_APB1Periph_I2C1
#define TOUCH_GPIO I2C_B1_GPIO
#define TOUCH_SCL_GPIO_PIN I2C_B1_SCL_GPIO_PIN // PB.08
#define TOUCH_SDA_GPIO_PIN I2C_B1_SDA_GPIO_PIN // PB.09

#define TOUCH_FT6236_I2C_ADDRESS (0x70>>1)
#define TOUCH_CST836U_I2C_ADDRESS (0x15)

Expand All @@ -67,119 +61,69 @@ static const TouchControllerDescriptor *tc = nullptr;

static TouchState internalTouchState = {};

void I2C_FreeBus()
static void _cst836u_exti_isr(void)
{
LL_GPIO_InitTypeDef gpioInit;
LL_GPIO_StructInit(&gpioInit);

// reset i2c bus by setting clk as output and sending manual clock pulses
gpioInit.Pin = TOUCH_SCL_GPIO_PIN;
gpioInit.Mode = LL_GPIO_MODE_OUTPUT;
gpioInit.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
gpioInit.Pull = LL_GPIO_PULL_NO;
gpioInit.Speed = LL_GPIO_SPEED_FREQ_LOW;
LL_GPIO_Init(TOUCH_GPIO, &gpioInit);

gpioInit.Pin = TOUCH_SDA_GPIO_PIN;
gpioInit.Mode = LL_GPIO_MODE_INPUT;
gpioInit.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
gpioInit.Pull = LL_GPIO_PULL_UP;
gpioInit.Speed = LL_GPIO_SPEED_FREQ_LOW;
LL_GPIO_Init(TOUCH_GPIO, &gpioInit);

//send 100khz clock train for some 100ms
tmr10ms_t until = get_tmr10ms() + 11;
while (get_tmr10ms() < until) {
if (LL_GPIO_IsInputPinSet(TOUCH_GPIO, TOUCH_SDA_GPIO_PIN)) {
TRACE("touch: i2c free again\n");
break;
}
TRACE("FREEEEE");
LL_GPIO_ResetOutputPin(TOUCH_GPIO, TOUCH_SCL_GPIO_PIN);
delay_us(10);
LL_GPIO_SetOutputPin(TOUCH_GPIO, TOUCH_SCL_GPIO_PIN);
delay_us(10);
}

//send stop condition:
gpioInit.Pin = TOUCH_SDA_GPIO_PIN;
gpioInit.Mode = LL_GPIO_MODE_OUTPUT;
gpioInit.Speed = LL_GPIO_SPEED_FREQ_LOW;
LL_GPIO_Init(TOUCH_GPIO, &gpioInit);

//clock is low
LL_GPIO_ResetOutputPin(TOUCH_GPIO, TOUCH_SCL_GPIO_PIN);
delay_us(10);
//sda = lo
LL_GPIO_SetOutputPin(TOUCH_GPIO, TOUCH_SDA_GPIO_PIN);
delay_us(10);
//clock goes high
LL_GPIO_ResetOutputPin(TOUCH_GPIO, TOUCH_SCL_GPIO_PIN);
delay_us(10);
//sda = hi
LL_GPIO_SetOutputPin(TOUCH_GPIO, TOUCH_SDA_GPIO_PIN);
delay_us(10);
TRACE("FREE BUS");
touchEventOccured = true;
}

// void Touch_DeInit()
// {
// I2C_DeInit(I2C_B1);
// __HAL_RCC_I2C1_FORCE_RESET();
// delay_ms(150);
// __HAL_RCC_I2C1_RELEASE_RESET();
// }

void I2C_Init()
static void TOUCH_AF_ExtiStop(void)
{
stm32_i2c_deinit(TOUCH_I2C_BUS);
stm32_exti_disable(TOUCH_INT_EXTI_Line);
}

stm32_gpio_enable_clock(TOUCH_GPIO);
static void TOUCH_AF_ExtiConfig(void)
{
__HAL_RCC_SYSCFG_CLK_ENABLE();
LL_SYSCFG_SetEXTISource(TOUCH_INT_EXTI_Port, TOUCH_INT_EXTI_SysCfgLine);

__HAL_RCC_I2C1_CLK_ENABLE();
__HAL_RCC_I2C1_CLK_DISABLE();

I2C_FreeBus();
stm32_exti_enable(TOUCH_INT_EXTI_Line,
LL_EXTI_TRIGGER_FALLING,
_cst836u_exti_isr);
}

stm32_i2c_init(TOUCH_I2C_BUS, TOUCH_I2C_CLK_RATE);
static void TOUCH_AF_GPIOConfig(void)
{
LL_GPIO_InitTypeDef gpioInit;
LL_GPIO_StructInit(&gpioInit);

stm32_gpio_enable_clock(TOUCH_RST_GPIO);
stm32_gpio_enable_clock(TOUCH_INT_GPIO);

gpioInit.Pin = TOUCH_RST_GPIO_PIN;

gpioInit.Mode = LL_GPIO_MODE_OUTPUT;
gpioInit.Speed = LL_GPIO_SPEED_FREQ_LOW;
gpioInit.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
gpioInit.Pull = LL_GPIO_PULL_UP;
gpioInit.Pull = LL_GPIO_PULL_NO;

gpioInit.Pin = TOUCH_RST_GPIO_PIN;
LL_GPIO_Init(TOUCH_RST_GPIO, &gpioInit);
LL_GPIO_SetOutputPin(TOUCH_RST_GPIO, TOUCH_RST_GPIO_PIN);

//ext interupt
gpioInit.Pin = TOUCH_INT_GPIO_PIN;
gpioInit.Mode = LL_GPIO_MODE_INPUT;
gpioInit.Pull = LL_GPIO_PULL_UP;
gpioInit.Speed = LL_GPIO_SPEED_FREQ_HIGH;
gpioInit.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
LL_GPIO_Init(TOUCH_INT_GPIO, &gpioInit);
LL_GPIO_SetOutputPin(TOUCH_INT_GPIO, TOUCH_INT_GPIO_PIN);
}

LL_SYSCFG_SetEXTISource(TOUCH_INT_EXTI_Port, TOUCH_INT_EXTI_SysCfgLine);

LL_EXTI_InitTypeDef extiInit;
LL_EXTI_StructInit(&extiInit);

extiInit.Line_0_31 = TOUCH_INT_EXTI_Line;
extiInit.Mode = LL_EXTI_MODE_IT;
extiInit.Trigger = LL_EXTI_TRIGGER_FALLING;
extiInit.LineCommand = ENABLE;
LL_EXTI_Init(&extiInit);
void I2C_Init_Radio(void)
{
TRACE("CST836U I2C Init");

NVIC_SetPriority(TOUCH_INT_EXTI_IRQn, 8);
NVIC_EnableIRQ(TOUCH_INT_EXTI_IRQn);
if (stm32_i2c_init(TOUCH_I2C_BUS, TOUCH_I2C_CLK_RATE) < 0) {
TRACE("CST836U ERROR: stm32_i2c_init failed");
return;
}
}

// void Touch_DeInit()
// {
// I2C_DeInit(I2C_B1);
// __HAL_RCC_I2C1_FORCE_RESET();
// delay_ms(150);
// __HAL_RCC_I2C1_RELEASE_RESET();
// }

#define I2C_TIMEOUT_MAX 5 // 5 ms

bool touch_i2c_read(uint8_t addr, uint8_t reg, uint8_t * data, uint8_t len)
Expand Down Expand Up @@ -224,7 +168,7 @@ static uint8_t TS_IO_Read(uint8_t addr, uint8_t reg)
uint8_t tryCount = 3;
while (!touch_i2c_read(addr, reg, &retult, 1)) {
if (--tryCount == 0) break;
I2C_Init();
I2C_Init_Radio();
}
return retult;
}
Expand All @@ -234,7 +178,7 @@ static uint16_t TS_IO_ReadMultiple(uint8_t addr, uint8_t reg, uint8_t * buffer,
uint8_t tryCount = 3;
while (!touch_i2c_read(addr, reg, buffer, length)) {
if (--tryCount == 0) break;
I2C_Init();
I2C_Init_Radio();
}
return 1;
}
Expand Down Expand Up @@ -489,9 +433,12 @@ void detectTouchController()

void TouchInit(void)
{
I2C_Init();
TOUCH_AF_GPIOConfig();
I2C_Init_Radio();
TouchReset();
detectTouchController();
TOUCH_AF_ExtiConfig();

tc->printDebugInfo();
}

Expand Down Expand Up @@ -543,18 +490,6 @@ void handleTouch()
}
}

#if !defined(TOUCH_INT_EXTI_IRQHandler)
#error "TOUCH_INT_EXTI_IRQHandler is not defined"
#endif
extern "C" void TOUCH_INT_EXTI_IRQHandler(void)
{
if (LL_EXTI_IsEnabledIT_0_31(TOUCH_INT_EXTI_Line) &&
LL_EXTI_IsActiveFlag_0_31(TOUCH_INT_EXTI_Line)) {
touchEventOccured = true;
LL_EXTI_ClearFlag_0_31(TOUCH_INT_EXTI_Line);
}
}

bool touchPanelEventOccured()
{
return touchEventOccured;
Expand Down

0 comments on commit b050440

Please sign in to comment.