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>
|
2017-10-20 15:18:55 +00:00
|
|
|
|
|
|
|
#include "common.h"
|
2017-10-26 21:51:39 +00:00
|
|
|
#include "secbool.h"
|
2017-04-07 00:34:53 +00:00
|
|
|
#include "touch.h"
|
2017-03-07 13:50:09 +00:00
|
|
|
|
2017-10-29 12:04:40 +00:00
|
|
|
static I2C_HandleTypeDef i2c_handle;
|
2017-03-07 13:50:09 +00:00
|
|
|
|
2017-10-20 15:18:55 +00:00
|
|
|
void touch_init(void)
|
2017-10-18 14:54:03 +00:00
|
|
|
{
|
2017-03-07 13:50:09 +00:00
|
|
|
// Enable I2C clock
|
|
|
|
__HAL_RCC_I2C1_CLK_ENABLE();
|
|
|
|
|
|
|
|
// Init SCL and SDA GPIO lines (PB6 & PB7)
|
|
|
|
GPIO_InitTypeDef GPIO_InitStructure = {
|
|
|
|
.Pin = GPIO_PIN_6 | GPIO_PIN_7,
|
|
|
|
.Mode = GPIO_MODE_AF_OD,
|
|
|
|
.Pull = GPIO_NOPULL,
|
|
|
|
.Speed = GPIO_SPEED_FREQ_VERY_HIGH,
|
|
|
|
.Alternate = GPIO_AF4_I2C1,
|
|
|
|
};
|
|
|
|
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
|
|
|
2017-10-29 12:04:40 +00:00
|
|
|
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
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
ensure(sectrue * (HAL_OK == HAL_I2C_Init(&i2c_handle)), NULL);
|
2017-03-07 14:52:19 +00:00
|
|
|
}
|
|
|
|
|
2017-10-29 12:09:59 +00:00
|
|
|
#define TOUCH_ADDRESS (0x38 << 1) // the HAL requires the 7-bit address to be shifted by one bit
|
2017-06-07 15:12:07 +00:00
|
|
|
#define TOUCH_PACKET_SIZE 16
|
|
|
|
|
2017-10-18 14:54:03 +00:00
|
|
|
uint32_t touch_read(void)
|
|
|
|
{
|
2017-06-07 15:12:07 +00:00
|
|
|
static uint8_t data[TOUCH_PACKET_SIZE], old_data[TOUCH_PACKET_SIZE];
|
2017-10-29 12:09:59 +00:00
|
|
|
if (HAL_OK != HAL_I2C_Master_Receive(&i2c_handle, TOUCH_ADDRESS, data, TOUCH_PACKET_SIZE, 1)) {
|
2017-03-07 14:52:19 +00:00
|
|
|
return 0; // read failure
|
|
|
|
}
|
2017-06-07 15:12:07 +00:00
|
|
|
if (0 == memcmp(data, old_data, TOUCH_PACKET_SIZE)) {
|
2017-03-07 14:52:19 +00:00
|
|
|
return 0; // no new event
|
|
|
|
}
|
2017-06-07 15:12:07 +00:00
|
|
|
memcpy(old_data, data, TOUCH_PACKET_SIZE);
|
|
|
|
|
|
|
|
if (data[0] == 0xff && data[1] == 0x00) {
|
|
|
|
if (data[2] == 0x01 && data[3] == 0x00) {
|
2017-10-26 14:23:04 +00:00
|
|
|
return TOUCH_START | ((data[3] & 0xF) << 20) | (data[4] << 12) | ((data[5] & 0xF) << 8) | data[6];
|
2017-06-07 15:12:07 +00:00
|
|
|
} else
|
|
|
|
if (data[2] == 0x01 && data[3] == 0x80) {
|
2017-10-26 14:23:04 +00:00
|
|
|
return TOUCH_MOVE | ((data[3] & 0xF) << 20) | (data[4] << 12) | ((data[5] & 0xF) << 8) | data[6];
|
2017-06-07 15:12:07 +00:00
|
|
|
} else
|
|
|
|
if (data[2] == 0x00 && data[3] == 0x40) {
|
2017-10-26 14:23:04 +00:00
|
|
|
return TOUCH_END | ((data[3] & 0xF) << 20) | (data[4] << 12) | ((data[5] & 0xF) << 8) | data[6];
|
2017-06-07 15:12:07 +00:00
|
|
|
}
|
2017-03-07 14:52:19 +00:00
|
|
|
}
|
2017-06-07 15:12:07 +00:00
|
|
|
|
|
|
|
return 0;
|
2017-03-07 13:50:09 +00:00
|
|
|
}
|
2017-03-07 16:33:39 +00:00
|
|
|
|
2017-10-24 15:59:25 +00:00
|
|
|
uint32_t touch_click(void)
|
2017-09-29 08:11:12 +00:00
|
|
|
{
|
2017-10-24 15:59:25 +00:00
|
|
|
uint32_t r;
|
2017-09-29 08:11:12 +00:00
|
|
|
// flush touch events if any
|
|
|
|
while (touch_read()) { }
|
|
|
|
// wait for TOUCH_START
|
|
|
|
while ((touch_read() & TOUCH_START) == 0) { }
|
|
|
|
// wait for TOUCH_END
|
2017-10-24 15:59:25 +00:00
|
|
|
while (((r = touch_read()) & TOUCH_END) == 0) { }
|
2017-09-29 08:11:12 +00:00
|
|
|
// flush touch events if any
|
|
|
|
while (touch_read()) { }
|
2017-10-24 15:59:25 +00:00
|
|
|
// return last touch coordinate
|
|
|
|
return r;
|
2017-09-29 08:11:12 +00:00
|
|
|
}
|