refactor(core): decouple i2c driver and touch driver

[no changelog]
pull/2773/head
tychovrahe 1 year ago committed by TychoVrahe
parent 83500487b8
commit 33fb590f44

@ -37,6 +37,9 @@
#ifdef USE_DMA2D
#include "dma2d.h"
#endif
#ifdef USE_I2C
#include "i2c.h"
#endif
#ifdef USE_TOUCH
#include "touch/touch.h"
#endif
@ -325,6 +328,10 @@ int bootloader_main(void) {
ui_screen_boot_empty(false);
#ifdef USE_I2C
i2c_init();
#endif
#ifdef USE_TOUCH
touch_power_on();
touch_init();

@ -53,6 +53,9 @@
#ifdef USE_BUTTON
#include "button.h"
#endif
#ifdef USE_I2C
#include "i2c.h"
#endif
#ifdef USE_TOUCH
#include "touch/touch.h"
#endif
@ -127,6 +130,10 @@ int main(void) {
rgb_led_init();
#endif
#ifdef USE_I2C
i2c_init();
#endif
#ifdef USE_TOUCH
touch_init();
#endif

@ -26,6 +26,7 @@
#include "common.h"
#include "display.h"
#include "flash.h"
#include "i2c.h"
#include "mini_printf.h"
#include "random_delays.h"
#include "rng.h"
@ -374,6 +375,7 @@ int main(void) {
display_orientation(0);
random_delays_init();
sdcard_init();
i2c_init();
touch_init();
sbu_init();
usb_init_all();
@ -438,3 +440,5 @@ int main(void) {
return 0;
}
void HardFault_Handler(void) { error_shutdown("INTERNAL ERROR!", "(HF)"); }

@ -2,6 +2,7 @@
#define _TREZOR_T_H
#define USE_SD_CARD 1
#define USE_I2C 1
#define USE_TOUCH 1
#define USE_SBU 1

@ -0,0 +1,124 @@
#include STM32_HAL_H
#include "i2c.h"
#include "common.h"
static I2C_HandleTypeDef i2c_handle;
void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c) {
// enable I2C clock
__HAL_RCC_I2C1_CLK_ENABLE();
// GPIO have already been initialised by touch_init
}
void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c) {
__HAL_RCC_I2C1_CLK_DISABLE();
}
void i2c_init(void) {
if (i2c_handle.Instance) {
return;
}
GPIO_InitTypeDef GPIO_InitStructure;
// configure CTP I2C SCL and SDA GPIO lines (PB6 & PB7)
GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed =
GPIO_SPEED_FREQ_LOW; // I2C is a KHz bus and low speed is still good into
// the low MHz
GPIO_InitStructure.Alternate = GPIO_AF4_I2C1;
GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
i2c_handle.Instance = I2C1;
i2c_handle.Init.ClockSpeed = 200000;
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;
if (HAL_OK != HAL_I2C_Init(&i2c_handle)) {
ensure(secfalse, "I2C was not loaded properly.");
return;
}
}
void _i2c_deinit(void) {
if (i2c_handle.Instance) {
HAL_I2C_DeInit(&i2c_handle);
i2c_handle.Instance = NULL;
}
}
void _i2c_ensure_pin(uint16_t GPIO_Pin, GPIO_PinState PinState) {
HAL_GPIO_WritePin(GPIOB, GPIO_Pin, PinState);
while (HAL_GPIO_ReadPin(GPIOB, GPIO_Pin) != PinState)
;
}
// I2C cycle described in section 2.9.7 of STM CD00288116 Errata sheet
//
// https://www.st.com/content/ccc/resource/technical/document/errata_sheet/7f/05/b0/bc/34/2f/4c/21/CD00288116.pdf/files/CD00288116.pdf/jcr:content/translations/en.CD00288116.pdf
void i2c_cycle(void) {
// PIN6 is SCL, PIN7 is SDA
// 1. Disable I2C peripheral
_i2c_deinit();
// 2. Configure SCL/SDA as GPIO OUTPUT Open Drain
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
HAL_Delay(50);
// 3. Check SCL and SDA High level
_i2c_ensure_pin(GPIO_PIN_6, GPIO_PIN_SET);
_i2c_ensure_pin(GPIO_PIN_7, GPIO_PIN_SET);
// 4+5. Check SDA Low level
_i2c_ensure_pin(GPIO_PIN_7, GPIO_PIN_RESET);
// 6+7. Check SCL Low level
_i2c_ensure_pin(GPIO_PIN_6, GPIO_PIN_RESET);
// 8+9. Check SCL High level
_i2c_ensure_pin(GPIO_PIN_6, GPIO_PIN_SET);
// 10+11. Check SDA High level
_i2c_ensure_pin(GPIO_PIN_7, GPIO_PIN_SET);
// 12. Configure SCL/SDA as Alternate function Open-Drain
GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStructure.Alternate = GPIO_AF4_I2C1;
GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
HAL_Delay(50);
// 13. Set SWRST bit in I2Cx_CR1 register
__HAL_RCC_I2C1_FORCE_RESET();
HAL_Delay(50);
// 14. Clear SWRST bit in I2Cx_CR1 register
__HAL_RCC_I2C1_RELEASE_RESET();
// 15. Enable the I2C peripheral
i2c_init();
HAL_Delay(10);
}
HAL_StatusTypeDef i2c_transmit(uint8_t addr, uint8_t *data, uint16_t len,
uint32_t timeout) {
return HAL_I2C_Master_Transmit(&i2c_handle, addr, data, len, timeout);
}
HAL_StatusTypeDef i2c_receive(uint8_t addr, uint8_t *data, uint16_t len,
uint32_t timeout) {
return HAL_I2C_Master_Receive(&i2c_handle, addr, data, len, timeout);
}

@ -0,0 +1,9 @@
#include STM32_HAL_H
void i2c_init(void);
void i2c_cycle(void);
HAL_StatusTypeDef i2c_transmit(uint8_t addr, uint8_t *data, uint16_t len,
uint32_t timeout);
HAL_StatusTypeDef i2c_receive(uint8_t addr, uint8_t *data, uint16_t len,
uint32_t timeout);

@ -25,6 +25,7 @@
#include "secbool.h"
#include "ft6x36.h"
#include "i2c.h"
#include "touch.h"
#define TOUCH_ADDRESS \
@ -43,15 +44,11 @@
#define EVENT_OLD_TIMEOUT_MS 50
#define EVENT_MISSING_TIMEOUT_MS 50
static I2C_HandleTypeDef i2c_handle;
static void touch_default_pin_state(void) {
// set power off and other pins as per section 3.5 of FT6236 datasheet
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10,
GPIO_PIN_SET); // CTP_ON/PB10 (active low) i.e.- CTPM power
// off when set/high/log 1
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET); // CTP_I2C_SCL/PB6
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET); // CTP_I2C_SDA/PB7
HAL_GPIO_WritePin(
GPIOC, GPIO_PIN_4,
GPIO_PIN_RESET); // CTP_INT/PC4 normally an input, but drive low as an
@ -66,7 +63,7 @@ static void touch_default_pin_state(void) {
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStructure.Pin = GPIO_PIN_10 | GPIO_PIN_6 | GPIO_PIN_7;
GPIO_InitStructure.Pin = GPIO_PIN_10;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_4 | GPIO_PIN_5;
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
@ -84,16 +81,6 @@ static void touch_active_pin_state(void) {
GPIO_InitTypeDef GPIO_InitStructure;
// configure CTP I2C SCL and SDA GPIO lines (PB6 & PB7)
GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed =
GPIO_SPEED_FREQ_LOW; // I2C is a KHz bus and low speed is still good into
// the low MHz
GPIO_InitStructure.Alternate = GPIO_AF4_I2C1;
GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
// PC4 capacitive touch panel module (CTPM) interrupt (INT) input
GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStructure.Pull = GPIO_PULLUP;
@ -107,118 +94,16 @@ static void touch_active_pin_state(void) {
// 300ms, giving an extra 10ms
}
void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c) {
// enable I2C clock
__HAL_RCC_I2C1_CLK_ENABLE();
// GPIO have already been initialised by touch_init
}
void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c) {
__HAL_RCC_I2C1_CLK_DISABLE();
}
static void _i2c_init(void) {
if (i2c_handle.Instance) {
return;
}
i2c_handle.Instance = I2C1;
i2c_handle.Init.ClockSpeed = 200000;
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;
if (HAL_OK != HAL_I2C_Init(&i2c_handle)) {
ensure(secfalse, "Touch screen panel was not loaded properly.");
return;
}
}
static void _i2c_deinit(void) {
if (i2c_handle.Instance) {
HAL_I2C_DeInit(&i2c_handle);
i2c_handle.Instance = NULL;
}
}
static void _i2c_ensure_pin(uint16_t GPIO_Pin, GPIO_PinState PinState) {
HAL_GPIO_WritePin(GPIOB, GPIO_Pin, PinState);
while (HAL_GPIO_ReadPin(GPIOB, GPIO_Pin) != PinState)
;
}
// I2C cycle described in section 2.9.7 of STM CD00288116 Errata sheet
//
// https://www.st.com/content/ccc/resource/technical/document/errata_sheet/7f/05/b0/bc/34/2f/4c/21/CD00288116.pdf/files/CD00288116.pdf/jcr:content/translations/en.CD00288116.pdf
static void _i2c_cycle(void) {
// PIN6 is SCL, PIN7 is SDA
// 1. Disable I2C peripheral
_i2c_deinit();
// 2. Configure SCL/SDA as GPIO OUTPUT Open Drain
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
HAL_Delay(50);
// 3. Check SCL and SDA High level
_i2c_ensure_pin(GPIO_PIN_6, GPIO_PIN_SET);
_i2c_ensure_pin(GPIO_PIN_7, GPIO_PIN_SET);
// 4+5. Check SDA Low level
_i2c_ensure_pin(GPIO_PIN_7, GPIO_PIN_RESET);
// 6+7. Check SCL Low level
_i2c_ensure_pin(GPIO_PIN_6, GPIO_PIN_RESET);
// 8+9. Check SCL High level
_i2c_ensure_pin(GPIO_PIN_6, GPIO_PIN_SET);
// 10+11. Check SDA High level
_i2c_ensure_pin(GPIO_PIN_7, GPIO_PIN_SET);
// 12. Configure SCL/SDA as Alternate function Open-Drain
GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStructure.Alternate = GPIO_AF4_I2C1;
GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
HAL_Delay(50);
// 13. Set SWRST bit in I2Cx_CR1 register
__HAL_RCC_I2C1_FORCE_RESET();
HAL_Delay(50);
// 14. Clear SWRST bit in I2Cx_CR1 register
__HAL_RCC_I2C1_RELEASE_RESET();
// 15. Enable the I2C peripheral
_i2c_init();
HAL_Delay(10);
}
void touch_set_mode(void) {
// set register 0xA4 G_MODE to interrupt trigger mode (0x01). basically, CTPM
// generates a pulse when new data is available
uint8_t touch_panel_config[] = {0xA4, 0x01};
ensure(
sectrue * (HAL_OK == HAL_I2C_Master_Transmit(
&i2c_handle, TOUCH_ADDRESS, touch_panel_config,
sizeof(touch_panel_config), 10)),
"Touch screen panel was not loaded properly.");
ensure(sectrue * (HAL_OK == i2c_transmit(TOUCH_ADDRESS, touch_panel_config,
sizeof(touch_panel_config), 10)),
"Touch screen panel was not loaded properly.");
}
void touch_power_on(void) {
if (i2c_handle.Instance) {
return;
}
touch_default_pin_state();
// turn on CTP circuitry
@ -227,7 +112,6 @@ void touch_power_on(void) {
}
void touch_power_off(void) {
_i2c_deinit();
// turn off CTP circuitry
HAL_Delay(50);
touch_default_pin_state();
@ -236,9 +120,6 @@ void touch_power_off(void) {
void touch_init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
// I2C device interface configuration
_i2c_init();
// PC4 capacitive touch panel module (CTPM) interrupt (INT) input
GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStructure.Pull = GPIO_PULLUP;
@ -254,10 +135,8 @@ void touch_init(void) {
void touch_sensitivity(uint8_t value) {
// set panel threshold (TH_GROUP) - default value is 0x12
uint8_t touch_panel_threshold[] = {0x80, value};
ensure(sectrue *
(HAL_OK == HAL_I2C_Master_Transmit(
&i2c_handle, TOUCH_ADDRESS, touch_panel_threshold,
sizeof(touch_panel_threshold), 10)),
ensure(sectrue * (HAL_OK == i2c_transmit(TOUCH_ADDRESS, touch_panel_threshold,
sizeof(touch_panel_threshold), 10)),
NULL);
}
@ -321,15 +200,13 @@ uint32_t touch_read(void) {
last_check_time = hal_ticks_ms();
uint8_t outgoing[] = {0x00}; // start reading from address 0x00
int result = HAL_I2C_Master_Transmit(&i2c_handle, TOUCH_ADDRESS, outgoing,
sizeof(outgoing), 1);
int result = i2c_transmit(TOUCH_ADDRESS, outgoing, sizeof(outgoing), 1);
if (result != HAL_OK) {
if (result == HAL_BUSY) _i2c_cycle();
if (result == HAL_BUSY) i2c_cycle();
return 0;
}
if (HAL_OK != HAL_I2C_Master_Receive(&i2c_handle, TOUCH_ADDRESS, touch_data,
TOUCH_PACKET_SIZE, 1)) {
if (HAL_OK != i2c_receive(TOUCH_ADDRESS, touch_data, TOUCH_PACKET_SIZE, 1)) {
return 0; // read failure
}

@ -14,6 +14,7 @@ def configure(env, features_wanted, defines, sources):
sources += [f'embed/trezorhal/displays/{display}', ]
if "input" in features_wanted:
sources += ['embed/trezorhal/i2c.c', ]
sources += ['embed/trezorhal/touch/touch.c', ]
sources += ['embed/trezorhal/touch/ft6x36.c', ]
features_available.append("touch")

@ -9,6 +9,7 @@ EXCEPTIONS+=( "cinema" "dash" "enemy" "float" "flock" "flower" "floor" "floral"
EXCEPTIONS+=( "mnemonic" ) # has NEM in it
EXCEPTIONS+=( "workflow" "overflow" ) # has Flo in it
EXCEPTIONS+=( "SyntaxError" ) # has Axe in it
EXCEPTIONS+=( "DKDNEM" ) # has NEM in it, some sort of weird coincidence
GREP_ARGS=()
for exception in "${EXCEPTIONS[@]}"; do

Loading…
Cancel
Save