1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-04 21:48:17 +00:00
trezor-firmware/embed/trezorhal/touch.c

122 lines
4.5 KiB
C
Raw Normal View History

2017-08-14 09:05:19 +00:00
/*
* Copyright (c) Pavol Rusnak, SatoshiLabs
*
* Licensed under TREZOR License
* see LICENSE file for details
*/
2017-03-07 13:50:09 +00:00
#include STM32_HAL_H
2017-03-07 14:52:19 +00:00
#include <string.h>
#include "common.h"
#include "secbool.h"
#include "touch.h"
2017-03-07 13:50:09 +00:00
static I2C_HandleTypeDef i2c_handle;
2017-03-07 13:50:09 +00:00
void touch_init(void)
{
2017-11-04 00:57:57 +00:00
GPIO_InitTypeDef GPIO_InitStructure;
2017-03-07 13:50:09 +00:00
// Enable I2C clock
__HAL_RCC_I2C1_CLK_ENABLE();
// Init SCL and SDA GPIO lines (PB6 & PB7)
2017-11-04 00:57:57 +00:00
GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7;
GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
GPIO_InitStructure.Pull = GPIO_NOPULL;
2017-11-11 16:16:17 +00:00
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW; // I2C is a KHz bus and low speed is still good into the low MHz
2017-11-04 00:57:57 +00:00
GPIO_InitStructure.Alternate = GPIO_AF4_I2C1;
2017-03-07 13:50:09 +00:00
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
i2c_handle.Instance = I2C1;
i2c_handle.Init.ClockSpeed = 400000;
i2c_handle.Init.DutyCycle = I2C_DUTYCYCLE_16_9;
i2c_handle.Init.OwnAddress1 = 0xFE; // master
i2c_handle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
i2c_handle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
i2c_handle.Init.OwnAddress2 = 0;
i2c_handle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
i2c_handle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
2017-03-07 14:11:21 +00:00
ensure(sectrue * (HAL_OK == HAL_I2C_Init(&i2c_handle)), NULL);
2017-11-04 00:57:57 +00:00
2017-11-11 16:16:17 +00:00
// PC4 capacitive touch panel module (CTPM) interrupt (INT) input
//GPIO_InitStructure.Pin = GPIO_PIN_4;
//GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
//GPIO_InitStructure.Pull = GPIO_PULLUP;
//GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
//GPIO_InitStructure.Alternate = 0;
//HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
// PC5 capacitive touch panel module (CTPM) reset (RSTN)
2017-11-04 00:57:57 +00:00
GPIO_InitStructure.Pin = GPIO_PIN_5;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
2017-11-11 16:16:17 +00:00
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
2017-11-04 00:57:57 +00:00
GPIO_InitStructure.Alternate = 0;
2017-11-11 16:16:17 +00:00
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_RESET); // set the pin value before driving it out
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); // switch the pin to be an output
// reset the touch panel by keeping its reset line low (active low) low for a minimum of 5ms
2017-11-04 00:57:57 +00:00
HAL_Delay(10); // being conservative, min is 5ms
2017-11-11 16:16:17 +00:00
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_SET); // release CTPM reset
HAL_Delay(310); // "Time of starting to report point after resetting" min is 300ms, giving an extra 10ms
2017-03-07 14:52:19 +00:00
2017-11-11 16:16:17 +00:00
// set register 0xA4 G_MODE to interrupt polling mode (0x00). basically, CTPM keeps this input line (to PC4) low while a finger is on the screen.
//uint8_t touch_panel_config[] = {0xA4, 0x00};
//ensure(sectrue * (HAL_OK == HAL_I2C_Master_Transmit(&i2c_handle, TOUCH_ADDRESS, touch_panel_config, sizeof(touch_panel_config), 10)), NULL);
}
uint32_t touch_read(void)
{
static uint8_t touch_data[TOUCH_PACKET_SIZE], previous_touch_data[TOUCH_PACKET_SIZE];
2017-11-11 16:16:17 +00:00
//const GPIO_PinState ctpm_interrupt_line = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_4);
uint8_t outgoing[] = {0x00}; // start reading from address 0x00
if (HAL_OK != HAL_I2C_Master_Transmit(&i2c_handle, TOUCH_ADDRESS, outgoing, sizeof(outgoing), 1)) {
return 0;
}
if (HAL_OK != HAL_I2C_Master_Receive(&i2c_handle, TOUCH_ADDRESS, touch_data, TOUCH_PACKET_SIZE, 1)) {
2017-03-07 14:52:19 +00:00
return 0; // read failure
}
if (0 == memcmp(previous_touch_data, touch_data, TOUCH_PACKET_SIZE)) {
return 0; // polled and got the same event again
} else {
memcpy(previous_touch_data, touch_data, TOUCH_PACKET_SIZE);
2017-03-07 14:52:19 +00:00
}
const uint32_t number_of_touch_points = touch_data[2] & 0x0F; // valid values are 0, 1, 2 (invalid 0xF before first touch) (tested with FT6206)
const uint32_t event_flag = touch_data[3] & 0xC0;
if (touch_data[1] == GESTURE_NO_GESTURE) {
if ((number_of_touch_points == 1) && (event_flag == EVENT_PRESS_DOWN)) {
return TOUCH_START | X_Y_POS;
} else if ((number_of_touch_points == 1) && (event_flag == EVENT_CONTACT)) {
return TOUCH_MOVE | X_Y_POS;
} else if ((number_of_touch_points == 0) && (event_flag == EVENT_LIFT_UP)) {
return TOUCH_END | X_Y_POS;
}
2017-03-07 14:52:19 +00:00
}
return 0;
2017-03-07 13:50:09 +00:00
}
uint32_t touch_click(void)
{
uint32_t r = 0;
// flush touch events if any
while (touch_read()) { }
// wait for TOUCH_START
while ((touch_read() & TOUCH_START) == 0) { }
// wait for TOUCH_END
while (((r = touch_read()) & TOUCH_END) == 0) { }
// flush touch events if any
while (touch_read()) { }
// return last touch coordinate
return r;
}