1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-22 22:38:08 +00:00

refactor(core/embed): use new i2c driver in stmpe811 touch driver

[no changelog]
This commit is contained in:
cepetr 2024-08-28 16:59:31 +02:00 committed by cepetr
parent fdd6a2a40e
commit 2540b91823

View File

@ -25,7 +25,7 @@
#include "common.h"
#include "secbool.h"
#include "i2c.h"
#include "i2c_bus.h"
#include "stmpe811.h"
#include "touch.h"
@ -157,8 +157,6 @@
#define STMPE811_TS_CTRL_ENABLE 0x01
#define STMPE811_TS_CTRL_STATUS 0x80
#define TOUCH_ADDRESS \
(0x38U << 1) // the HAL requires the 7-bit address to be shifted by one bit
#define TOUCH_PACKET_SIZE 7U
#define EVENT_PRESS_DOWN 0x00U
#define EVENT_CONTACT 0x80U
@ -173,17 +171,14 @@
#define EVENT_OLD_TIMEOUT_MS 50
#define EVENT_MISSING_TIMEOUT_MS 50
#define TS_I2C_ADDRESS 0x82
#define TS_I2C_ADDRESS (0x82 >> 1)
#define I2Cx_TIMEOUT_MAX \
0x3000 /*<! The value of the maximal timeout for I2C waiting loops */
uint32_t I2cxTimeout =
I2Cx_TIMEOUT_MAX; /*<! Value of Timeout when I2C communication fails */
/**
* @brief I2Cx error treatment function
*/
static void I2Cx_Error(void) { i2c_cycle(TOUCH_I2C_INSTANCE); }
static i2c_bus_t *i2c_bus = NULL;
/**
* @brief Writes a value in a register of the device through BUS.
@ -192,16 +187,24 @@ static void I2Cx_Error(void) { i2c_cycle(TOUCH_I2C_INSTANCE); }
* @param Value: The target register value to be written
*/
static void I2Cx_WriteData(uint8_t Addr, uint8_t Reg, uint8_t Value) {
HAL_StatusTypeDef status = HAL_OK;
i2c_op_t ops[] = {
{
.flags = I2C_FLAG_TX | I2C_FLAG_EMBED,
.size = 2,
.data = {Reg, Value},
},
};
status = i2c_mem_write(TOUCH_I2C_INSTANCE, Addr, (uint16_t)Reg,
I2C_MEMADD_SIZE_8BIT, &Value, 1, I2cxTimeout);
i2c_packet_t pkt = {
.address = TS_I2C_ADDRESS,
.timeout = I2cxTimeout,
.op_count = ARRAY_LENGTH(ops),
.ops = ops,
};
/* Check the communication status */
if (status != HAL_OK) {
/* Re-Initialize the BUS */
I2Cx_Error();
}
i2c_status_t status = i2c_bus_submit_and_wait(i2c_bus, &pkt);
(void)status;
}
/**
@ -213,16 +216,29 @@ static void I2Cx_WriteData(uint8_t Addr, uint8_t Reg, uint8_t Value) {
*/
static void I2Cx_WriteBuffer(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer,
uint16_t Length) {
HAL_StatusTypeDef status = HAL_OK;
i2c_op_t ops[] = {
{
.flags = I2C_FLAG_TX | I2C_FLAG_EMBED,
.size = 1,
.data = {Reg},
},
{
.flags = I2C_FLAG_TX,
.size = Length,
.ptr = pBuffer,
},
};
status = i2c_mem_write(TOUCH_I2C_INSTANCE, Addr, (uint16_t)Reg,
I2C_MEMADD_SIZE_8BIT, pBuffer, Length, I2cxTimeout);
i2c_packet_t pkt = {
.address = TS_I2C_ADDRESS,
.timeout = I2cxTimeout,
.op_count = ARRAY_LENGTH(ops),
.ops = ops,
};
/* Check the communication status */
if (status != HAL_OK) {
/* Re-Initialize the BUS */
I2Cx_Error();
}
i2c_status_t status = i2c_bus_submit_and_wait(i2c_bus, &pkt);
(void)status;
}
/**
@ -232,17 +248,32 @@ static void I2Cx_WriteBuffer(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer,
* @retval Data read at register address
*/
static uint8_t I2Cx_ReadData(uint8_t Addr, uint8_t Reg) {
HAL_StatusTypeDef status = HAL_OK;
uint8_t value = 0;
status = i2c_mem_read(TOUCH_I2C_INSTANCE, Addr, Reg, I2C_MEMADD_SIZE_8BIT,
&value, 1, I2cxTimeout);
i2c_op_t ops[] = {
{
.flags = I2C_FLAG_TX | I2C_FLAG_EMBED,
.size = 1,
.data = {Reg},
},
{
.flags = I2C_FLAG_RX,
.size = 1,
.ptr = &value,
},
};
i2c_packet_t pkt = {
.address = TS_I2C_ADDRESS,
.timeout = I2cxTimeout,
.op_count = ARRAY_LENGTH(ops),
.ops = ops,
};
i2c_status_t status = i2c_bus_submit_and_wait(i2c_bus, &pkt);
(void)status;
/* Check the communication status */
if (status != HAL_OK) {
/* Re-Initialize the BUS */
I2Cx_Error();
}
return value;
}
@ -256,21 +287,31 @@ static uint8_t I2Cx_ReadData(uint8_t Addr, uint8_t Reg) {
*/
static uint8_t I2Cx_ReadBuffer(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer,
uint16_t Length) {
HAL_StatusTypeDef status = HAL_OK;
i2c_op_t ops[] = {
{
.flags = I2C_FLAG_TX | I2C_FLAG_EMBED,
.size = 1,
.data = {Reg},
},
{
.flags = I2C_FLAG_RX,
.size = Length,
.ptr = pBuffer,
},
};
status = i2c_mem_read(TOUCH_I2C_INSTANCE, Addr, (uint16_t)Reg,
I2C_MEMADD_SIZE_8BIT, pBuffer, Length, I2cxTimeout);
i2c_packet_t pkt = {
.address = TS_I2C_ADDRESS,
.timeout = I2cxTimeout,
.op_count = ARRAY_LENGTH(ops),
.ops = ops,
};
/* Check the communication status */
if (status == HAL_OK) {
return 0;
} else {
/* Re-Initialize the BUS */
I2Cx_Error();
i2c_status_t status = i2c_bus_submit_and_wait(i2c_bus, &pkt);
return 1;
}
return status == I2C_STATUS_OK ? 0 : 1;
}
/**
* @brief IOE Writes single data operation.
* @param Addr: I2C Address
@ -574,6 +615,11 @@ secbool touch_init(void) {
touch_driver_t *driver = &g_touch_driver;
if (driver->initialized != sectrue) {
i2c_bus = i2c_bus_open(TOUCH_I2C_INSTANCE);
if (i2c_bus == NULL) {
return secfalse;
}
stmpe811_Reset();
touch_set_mode();
@ -589,6 +635,7 @@ void touch_deinit(void) {
if (driver->initialized == sectrue) {
// Not implemented properly
i2c_bus_close(i2c_bus);
memset(driver, 0, sizeof(touch_driver_t));
}
}