1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-08 22:40:59 +00:00

chore(core/embed): remove unused legacy i2c driver

[no changelog]
This commit is contained in:
cepetr 2024-09-10 15:12:05 +02:00 committed by cepetr
parent 2540b91823
commit 33a94d945d
13 changed files with 0 additions and 447 deletions

View File

@ -42,9 +42,6 @@
#include "dma2d.h"
#endif
#endif
#ifdef USE_I2C
#include "i2c.h"
#endif
#ifdef USE_OPTIGA
#include "optiga_hal.h"
#endif
@ -373,10 +370,6 @@ int bootloader_main(void) {
hash_processor_init();
#endif
#ifdef USE_I2C
i2c_init();
#endif
display_init(DISPLAY_RETAIN_CONTENT);
#ifdef USE_DMA2D

View File

@ -72,9 +72,6 @@
#ifdef USE_BUTTON
#include "button.h"
#endif
#ifdef USE_I2C
#include "i2c.h"
#endif
#ifdef USE_TOUCH
#include "touch.h"
#endif
@ -207,10 +204,6 @@ int main(void) {
consumption_mask_init();
#endif
#ifdef USE_I2C
i2c_init();
#endif
#ifdef USE_TOUCH
touch_init();
#endif

View File

@ -35,7 +35,6 @@
#include "flash.h"
#include "flash_otp.h"
#include "fwutils.h"
#include "i2c.h"
#include "image.h"
#include "model.h"
#include "mpu.h"
@ -796,9 +795,6 @@ int main(void) {
#ifdef USE_BUTTON
button_init();
#endif
#ifdef USE_I2C
i2c_init();
#endif
#ifdef USE_TOUCH
touch_init();
#endif

View File

@ -1,38 +0,0 @@
/*
* This file is part of the Trezor project, https://trezor.io/
*
* Copyright (c) SatoshiLabs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TREZORHAL_I2C_H
#define TREZORHAL_I2C_H
#include STM32_HAL_H
void i2c_init(void);
void i2c_cycle(uint16_t idx);
HAL_StatusTypeDef i2c_transmit(uint16_t idx, uint8_t addr, uint8_t *data,
uint16_t len, uint32_t timeout);
HAL_StatusTypeDef i2c_receive(uint16_t idx, uint8_t addr, uint8_t *data,
uint16_t len, uint32_t timeout);
HAL_StatusTypeDef i2c_mem_write(uint16_t idx, uint8_t addr, uint16_t mem_addr,
uint16_t mem_addr_size, uint8_t *data,
uint16_t len, uint32_t timeout);
HAL_StatusTypeDef i2c_mem_read(uint16_t idx, uint8_t addr, uint16_t mem_addr,
uint16_t mem_addr_size, uint8_t *data,
uint16_t len, uint32_t timeout);
#endif // TREZORHAL_I2C_H

View File

@ -1,199 +0,0 @@
#include STM32_HAL_H
#include TREZOR_BOARD
#include "i2c.h"
#include "common.h"
static I2C_HandleTypeDef i2c_handle[I2C_COUNT];
typedef struct {
I2C_TypeDef *Instance;
GPIO_TypeDef *SclPort;
GPIO_TypeDef *SdaPort;
uint16_t SclPin;
uint16_t SdaPin;
uint8_t PinAF;
uint32_t Reset;
} i2c_instance_t;
i2c_instance_t i2c_defs[I2C_COUNT] = {
{
.Instance = I2C_INSTANCE_0,
.SclPort = I2C_INSTANCE_0_SCL_PORT,
.SdaPort = I2C_INSTANCE_0_SDA_PORT,
.SclPin = I2C_INSTANCE_0_SCL_PIN,
.SdaPin = I2C_INSTANCE_0_SDA_PIN,
.PinAF = I2C_INSTANCE_0_PIN_AF,
.Reset = I2C_INSTANCE_0_RESET_BIT,
},
#ifdef I2C_INSTANCE_1
{
.Instance = I2C_INSTANCE_1,
.SclPort = I2C_INSTANCE_1_SCL_PORT,
.SdaPort = I2C_INSTANCE_1_SDA_PORT,
.SclPin = I2C_INSTANCE_1_SCL_PIN,
.SdaPin = I2C_INSTANCE_1_SDA_PIN,
.PinAF = I2C_INSTANCE_1_PIN_AF,
.Reset = I2C_INSTANCE_1_RESET_BIT,
},
#endif
};
void i2c_init_instance(uint16_t idx, i2c_instance_t *instance) {
if (i2c_handle[idx].Instance) {
return;
}
GPIO_InitTypeDef GPIO_InitStructure = {0};
// configure CTP I2C SCL and SDA GPIO lines
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 = instance->PinAF;
GPIO_InitStructure.Pin = instance->SclPin;
HAL_GPIO_Init(instance->SclPort, &GPIO_InitStructure);
GPIO_InitStructure.Alternate = instance->PinAF;
GPIO_InitStructure.Pin = instance->SdaPin;
HAL_GPIO_Init(instance->SdaPort, &GPIO_InitStructure);
i2c_handle[idx].Instance = instance->Instance;
i2c_handle[idx].Init.ClockSpeed = 200000;
i2c_handle[idx].Init.DutyCycle = I2C_DUTYCYCLE_16_9;
i2c_handle[idx].Init.OwnAddress1 = 0xFE; // master
i2c_handle[idx].Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
i2c_handle[idx].Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
i2c_handle[idx].Init.OwnAddress2 = 0;
i2c_handle[idx].Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
i2c_handle[idx].Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_OK != HAL_I2C_Init(&i2c_handle[idx])) {
error_shutdown("I2C was not loaded properly.");
return;
}
}
void i2c_init(void) {
// enable I2C clock
I2C_INSTANCE_0_CLK_EN();
I2C_INSTANCE_0_SCL_CLK_EN();
I2C_INSTANCE_0_SDA_CLK_EN();
i2c_init_instance(0, &i2c_defs[0]);
#ifdef I2C_INSTANCE_1
I2C_INSTANCE_1_CLK_EN();
I2C_INSTANCE_1_SCL_CLK_EN();
I2C_INSTANCE_1_SDA_CLK_EN();
i2c_init_instance(1, &i2c_defs[1]);
#endif
}
void i2c_deinit(uint16_t idx) {
if (i2c_handle[idx].Instance) {
HAL_I2C_DeInit(&i2c_handle[idx]);
i2c_handle[idx].Instance = NULL;
}
}
void i2c_ensure_pin(GPIO_TypeDef *port, uint16_t GPIO_Pin,
GPIO_PinState PinState) {
HAL_GPIO_WritePin(port, GPIO_Pin, PinState);
while (HAL_GPIO_ReadPin(port, 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(uint16_t idx) {
i2c_instance_t *instance = &i2c_defs[0];
// 1. Disable I2C peripheral
i2c_deinit(idx);
// 2. Configure SCL/SDA as GPIO OUTPUT Open Drain
GPIO_InitTypeDef GPIO_InitStructure = {0};
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStructure.Pin = instance->SdaPin;
HAL_GPIO_Init(instance->SdaPort, &GPIO_InitStructure);
GPIO_InitStructure.Pin = instance->SclPin;
HAL_GPIO_Init(instance->SclPort, &GPIO_InitStructure);
HAL_Delay(50);
// 3. Check SCL and SDA High level
i2c_ensure_pin(instance->SclPort, instance->SclPin, GPIO_PIN_SET);
i2c_ensure_pin(instance->SdaPort, instance->SdaPin, GPIO_PIN_SET);
// 4+5. Check SDA Low level
i2c_ensure_pin(instance->SdaPort, instance->SdaPin, GPIO_PIN_RESET);
// 6+7. Check SCL Low level
i2c_ensure_pin(instance->SclPort, instance->SclPin, GPIO_PIN_RESET);
// 8+9. Check SCL High level
i2c_ensure_pin(instance->SclPort, instance->SclPin, GPIO_PIN_SET);
// 10+11. Check SDA High level
i2c_ensure_pin(instance->SclPort, instance->SdaPin, 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 = instance->PinAF;
GPIO_InitStructure.Pin = instance->SclPin;
HAL_GPIO_Init(instance->SclPort, &GPIO_InitStructure);
GPIO_InitStructure.Pin = instance->SdaPin;
HAL_GPIO_Init(instance->SdaPort, &GPIO_InitStructure);
HAL_Delay(50);
// 13. Force reset
RCC->APB1RSTR |= instance->Reset;
HAL_Delay(50);
// 14. Release reset
RCC->APB1RSTR &= ~instance->Reset;
// 15. Enable the I2C peripheral
i2c_init_instance(idx, instance);
HAL_Delay(10);
}
HAL_StatusTypeDef i2c_transmit(uint16_t idx, uint8_t addr, uint8_t *data,
uint16_t len, uint32_t timeout) {
return HAL_I2C_Master_Transmit(&i2c_handle[idx], addr, data, len, timeout);
}
HAL_StatusTypeDef i2c_receive(uint16_t idx, uint8_t addr, uint8_t *data,
uint16_t len, uint32_t timeout) {
HAL_StatusTypeDef ret =
HAL_I2C_Master_Receive(&i2c_handle[idx], addr, data, len, timeout);
#ifdef USE_OPTIGA
if (idx == OPTIGA_I2C_INSTANCE) {
// apply GUARD_TIME as specified by the OPTIGA datasheet
// (only applies to the I2C bus to which the OPTIGA is connected)
hal_delay_us(50);
}
#endif
return ret;
}
HAL_StatusTypeDef i2c_mem_write(uint16_t idx, uint8_t addr, uint16_t mem_addr,
uint16_t mem_addr_size, uint8_t *data,
uint16_t len, uint32_t timeout) {
return HAL_I2C_Mem_Write(&i2c_handle[idx], addr, mem_addr, mem_addr_size,
data, len, timeout);
}
HAL_StatusTypeDef i2c_mem_read(uint16_t idx, uint8_t addr, uint16_t mem_addr,
uint16_t mem_addr_size, uint8_t *data,
uint16_t len, uint32_t timeout) {
return HAL_I2C_Mem_Read(&i2c_handle[idx], addr, mem_addr, mem_addr_size, data,
len, timeout);
}

View File

@ -1,179 +0,0 @@
#include STM32_HAL_H
#include TREZOR_BOARD
#include "i2c.h"
#include "common.h"
static I2C_HandleTypeDef i2c_handle[I2C_COUNT];
typedef struct {
I2C_TypeDef *Instance;
GPIO_TypeDef *SclPort;
GPIO_TypeDef *SdaPort;
uint16_t SclPin;
uint16_t SdaPin;
uint8_t PinAF;
volatile uint32_t *ResetReg;
uint32_t ResetBit;
} i2c_instance_t;
i2c_instance_t i2c_defs[I2C_COUNT] = {
{
.Instance = I2C_INSTANCE_0,
.SclPort = I2C_INSTANCE_0_SCL_PORT,
.SdaPort = I2C_INSTANCE_0_SDA_PORT,
.SclPin = I2C_INSTANCE_0_SCL_PIN,
.SdaPin = I2C_INSTANCE_0_SDA_PIN,
.PinAF = I2C_INSTANCE_0_PIN_AF,
.ResetReg = I2C_INSTANCE_0_RESET_REG,
.ResetBit = I2C_INSTANCE_0_RESET_BIT,
},
#ifdef I2C_INSTANCE_1
{
.Instance = I2C_INSTANCE_1,
.SclPort = I2C_INSTANCE_1_SCL_PORT,
.SdaPort = I2C_INSTANCE_1_SDA_PORT,
.SclPin = I2C_INSTANCE_1_SCL_PIN,
.SdaPin = I2C_INSTANCE_1_SDA_PIN,
.PinAF = I2C_INSTANCE_1_PIN_AF,
.ResetReg = I2C_INSTANCE_1_RESET_REG,
.ResetBit = I2C_INSTANCE_1_RESET_BIT,
},
#endif
#ifdef I2C_INSTANCE_2
{
.Instance = I2C_INSTANCE_2,
.SclPort = I2C_INSTANCE_2_SCL_PORT,
.SdaPort = I2C_INSTANCE_2_SDA_PORT,
.SclPin = I2C_INSTANCE_2_SCL_PIN,
.SdaPin = I2C_INSTANCE_2_SDA_PIN,
.PinAF = I2C_INSTANCE_2_PIN_AF,
.ResetReg = I2C_INSTANCE_2_RESET_REG,
.ResetBit = I2C_INSTANCE_2_RESET_BIT,
},
#endif
};
/*
* Using calculation from STM32CubeMX
* PCLKx as source, assumed 160MHz
* Fast mode, freq = 400kHz, Rise time = 250ns, Fall time = 100ns
* Fast mode, freq = 200kHz, Rise time = 250ns, Fall time = 100ns
* SCLH and SCLL are manually modified to achieve more symmetric clock
*/
#define I2C_TIMING_400000_Hz 0x30D22728
#define I2C_TIMING_200000_Hz 0x30D2595A
#define I2C_TIMING I2C_TIMING_200000_Hz
void i2c_init_instance(uint16_t idx, i2c_instance_t *instance) {
if (i2c_handle[idx].Instance) {
return;
}
GPIO_InitTypeDef GPIO_InitStructure = {0};
// configure CTP I2C SCL and SDA GPIO lines
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 = instance->PinAF;
GPIO_InitStructure.Pin = instance->SclPin;
HAL_GPIO_Init(instance->SclPort, &GPIO_InitStructure);
GPIO_InitStructure.Alternate = instance->PinAF;
GPIO_InitStructure.Pin = instance->SdaPin;
HAL_GPIO_Init(instance->SdaPort, &GPIO_InitStructure);
i2c_handle[idx].Instance = instance->Instance;
i2c_handle[idx].Init.Timing = I2C_TIMING;
i2c_handle[idx].Init.OwnAddress1 = 0xFE; // master
i2c_handle[idx].Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
i2c_handle[idx].Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
i2c_handle[idx].Init.OwnAddress2 = 0;
i2c_handle[idx].Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
i2c_handle[idx].Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_OK != HAL_I2C_Init(&i2c_handle[idx])) {
error_shutdown("I2C was not loaded properly.");
return;
}
}
void i2c_init(void) {
// enable I2C clock
I2C_INSTANCE_0_CLK_EN();
I2C_INSTANCE_0_SCL_CLK_EN();
I2C_INSTANCE_0_SDA_CLK_EN();
i2c_init_instance(0, &i2c_defs[0]);
#ifdef I2C_INSTANCE_1
I2C_INSTANCE_1_CLK_EN();
I2C_INSTANCE_1_SCL_CLK_EN();
I2C_INSTANCE_1_SDA_CLK_EN();
i2c_init_instance(1, &i2c_defs[1]);
#endif
#ifdef I2C_INSTANCE_2
I2C_INSTANCE_2_CLK_EN();
I2C_INSTANCE_2_SCL_CLK_EN();
I2C_INSTANCE_2_SDA_CLK_EN();
i2c_init_instance(2, &i2c_defs[2]);
#endif
}
void i2c_deinit(uint16_t idx) {
if (i2c_handle[idx].Instance) {
HAL_I2C_DeInit(&i2c_handle[idx]);
i2c_handle[idx].Instance = NULL;
}
}
void i2c_ensure_pin(GPIO_TypeDef *port, uint16_t GPIO_Pin,
GPIO_PinState PinState) {
HAL_GPIO_WritePin(port, GPIO_Pin, PinState);
while (HAL_GPIO_ReadPin(port, GPIO_Pin) != PinState)
;
}
void i2c_cycle(uint16_t idx) {
SET_BIT(*i2c_defs[idx].ResetReg, i2c_defs[idx].ResetBit);
CLEAR_BIT(*i2c_defs[idx].ResetReg, i2c_defs[idx].ResetBit);
}
HAL_StatusTypeDef i2c_transmit(uint16_t idx, uint8_t addr, uint8_t *data,
uint16_t len, uint32_t timeout) {
return HAL_I2C_Master_Transmit(&i2c_handle[idx], addr, data, len, timeout);
}
HAL_StatusTypeDef i2c_receive(uint16_t idx, uint8_t addr, uint8_t *data,
uint16_t len, uint32_t timeout) {
HAL_StatusTypeDef ret =
HAL_I2C_Master_Receive(&i2c_handle[idx], addr, data, len, timeout);
#ifdef USE_OPTIGA
if (idx == OPTIGA_I2C_INSTANCE) {
// apply GUARD_TIME as specified by the OPTIGA datasheet
// (only applies to the I2C bus to which the OPTIGA is connected)
hal_delay_us(50);
}
#endif
return ret;
}
HAL_StatusTypeDef i2c_mem_write(uint16_t idx, uint8_t addr, uint16_t mem_addr,
uint16_t mem_addr_size, uint8_t *data,
uint16_t len, uint32_t timeout) {
return HAL_I2C_Mem_Write(&i2c_handle[idx], addr, mem_addr, mem_addr_size,
data, len, timeout);
}
HAL_StatusTypeDef i2c_mem_read(uint16_t idx, uint8_t addr, uint16_t mem_addr,
uint16_t mem_addr_size, uint8_t *data,
uint16_t len, uint32_t timeout) {
return HAL_I2C_Mem_Read(&i2c_handle[idx], addr, mem_addr, mem_addr_size, data,
len, timeout);
}

View File

@ -72,7 +72,6 @@ def configure(
features_available.append("display_rgb565")
if "input" in features_wanted:
sources += ["embed/trezorhal/stm32f4/i2c.c"]
sources += ["embed/trezorhal/stm32f4/i2c_bus.c"]
sources += ["embed/trezorhal/stm32f4/touch/stmpe811.c"]
features_available.append("touch")

View File

@ -57,9 +57,6 @@ def configure(
]
if "input" in features_wanted:
sources += [
"embed/trezorhal/stm32u5/i2c.c",
]
sources += ["embed/trezorhal/stm32u5/i2c_bus.c"]
sources += ["embed/trezorhal/stm32u5/touch/sitronix.c"]
features_available.append("touch")

View File

@ -48,11 +48,6 @@ def configure(
else:
sources += [f"embed/trezorhal/stm32f4/displays/{display}"]
sources += [
"embed/trezorhal/stm32f4/i2c.c",
"embed/trezorhal/stm32f4/i2c_bus.c",
]
if "input" in features_wanted:
sources += ["embed/trezorhal/stm32f4/button.c"]
features_available.append("button")

View File

@ -82,7 +82,6 @@ def configure(
features_available.append("backlight")
if "input" in features_wanted:
sources += ["embed/trezorhal/stm32f4/i2c.c"]
sources += ["embed/trezorhal/stm32f4/i2c_bus.c"]
sources += ["embed/trezorhal/stm32f4/touch/ft6x36.c"]
features_available.append("touch")

View File

@ -73,7 +73,6 @@ def configure(
if "optiga" in features_wanted:
defines += ["USE_OPTIGA=1"]
sources += ["embed/trezorhal/stm32u5/i2c.c"]
sources += ["embed/trezorhal/stm32u5/i2c_bus.c"]
sources += ["embed/trezorhal/stm32u5/optiga_hal.c"]
sources += ["embed/trezorhal/optiga/optiga.c"]

View File

@ -71,7 +71,6 @@ def configure(
features_available.append("backlight")
if "input" in features_wanted:
sources += ["embed/trezorhal/stm32u5/i2c.c"]
sources += ["embed/trezorhal/stm32u5/i2c_bus.c"]
sources += ["embed/trezorhal/stm32u5/touch/ft6x36.c"]
sources += ["embed/trezorhal/stm32u5/touch/panels/lx154a2422cpt23.c"]

View File

@ -75,7 +75,6 @@ def configure(
features_available.append("backlight")
if "input" in features_wanted:
sources += ["embed/trezorhal/stm32u5/i2c.c"]
sources += ["embed/trezorhal/stm32u5/i2c_bus.c"]
sources += ["embed/trezorhal/stm32u5/touch/ft6x36.c"]
features_available.append("touch")