1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-18 12:28:09 +00:00

refactor(core): improve flexibility of sd card and touch driver pin selection, display rotation and touch coords transformation

[no changelog]
This commit is contained in:
tychovrahe 2023-11-09 10:15:22 +01:00 committed by TychoVrahe
parent d15ee71279
commit fd5bfbf115
11 changed files with 233 additions and 113 deletions

View File

@ -14,6 +14,13 @@
#define USE_BACKLIGHT 1
#define USE_DISP_I8080_8BIT_DW 1
#include "displays/panels/lx154a2422.h"
#include "displays/st7789v.h"
#define DISPLAY_IDENTIFY 1
#define DISPLAY_TE_PORT GPIOD
#define DISPLAY_TE_PIN GPIO_PIN_12
#define TRANSFORM_TOUCH_COORDS lx154a2422_transform_touch_coords
#define BACKLIGHT_PWM_FREQ 10000
#define BACKLIGHT_PWM_TIM TIM1
#define BACKLIGHT_PWM_TIM_CLK_EN __HAL_RCC_TIM1_CLK_ENABLE
@ -38,8 +45,18 @@
#define I2C_INSTANCE_1_SCL_CLK_EN __HAL_RCC_GPIOB_CLK_ENABLE
#define I2C_INSTANCE_1_RESET_FLG RCC_APB1RSTR_I2C1RST
#define TOUCH_SENSITIVITY 0x06
#define TOUCH_I2C_NUM 0
#define TOUCH_RST_PORT GPIOC
#define TOUCH_RST_PIN GPIO_PIN_5
#define TOUCH_INT_PORT GPIOC
#define TOUCH_INT_PIN GPIO_PIN_4
#define TOUCH_ON_PORT GPIOB
#define TOUCH_ON_PIN GPIO_PIN_10
#include "displays/st7789v.h"
#define SD_DETECT_PORT GPIOC
#define SD_DETECT_PIN GPIO_PIN_13
#define SD_ENABLE_PORT GPIOC
#define SD_ENABLE_PIN GPIO_PIN_0
#endif //_TREZOR_T_H

View File

@ -1,5 +1,5 @@
#ifndef TT_OLD2_H_
#define TT_OLD2_H_
#ifndef _154A_H_
#define _154A_H_
// ILI9341 IC controller

View File

@ -1,5 +1,5 @@
#ifndef TT_OLD3_H_
#define TT_OLD3_H_
#ifndef LX154A2411_H_
#define LX154A2411_H_
// ST7789_V IC controller
void lx154a2411_gamma(void);

View File

@ -1,5 +1,7 @@
#include "display_interface.h"
#include "displays/st7789v.h"
#include "touch.h"
void lx154a2422_gamma(void) {
// positive voltage correction
@ -80,3 +82,67 @@ void lx154a2422_init_seq(void) {
lx154a2422_gamma();
}
void lx154a2422_rotate(int degrees, display_padding_t* padding) {
uint16_t shift = 0;
char BX = 0, BY = 0;
#define RGB (1 << 3)
#define ML (1 << 4) // vertical refresh order
#define MH (1 << 2) // horizontal refresh order
#define MV (1 << 5)
#define MX (1 << 6)
#define MY (1 << 7)
// MADCTL: Memory Data Access Control - reference:
// section 8.12 in the ST7789V manual
uint8_t display_command_parameter = 0;
switch (degrees) {
case 0:
display_command_parameter = 0;
BY = 0;
break;
case 90:
display_command_parameter = MV | MX | MH | ML;
BX = 1;
shift = 1;
break;
case 180:
display_command_parameter = MX | MY | MH | ML;
BY = 0;
shift = 1;
break;
case 270:
display_command_parameter = MV | MY;
BX = 1;
break;
}
CMD(0x36);
DATA(display_command_parameter);
if (shift) {
// GATECTRL: Gate Control; NL = 240 gate lines, first scan line is
// gate 80.; gate scan direction 319 -> 0
CMD(0xE4);
DATA(0x1D);
DATA(0x00);
DATA(0x11);
} else {
// GATECTRL: Gate Control; NL = 240 gate lines, first scan line is
// gate 80.; gate scan direction 319 -> 0
CMD(0xE4);
DATA(0x1D);
DATA(0x0A);
DATA(0x11);
}
// reset the column and page extents
display_set_window(0, 0, DISPLAY_RESX - 1, DISPLAY_RESY - 1);
padding->x = BX ? (MAX_DISPLAY_RESY - DISPLAY_RESY) : 0;
padding->y = BY ? (MAX_DISPLAY_RESY - DISPLAY_RESY) : 0;
}
uint32_t lx154a2422_transform_touch_coords(uint16_t x, uint16_t y) {
return touch_pack_xy(x, y);
}

View File

@ -1,7 +1,11 @@
#ifndef LX154A2422_H_
#define LX154A2422_H_
#include "displays/st7789v.h"
void lx154a2422_init_seq(void);
void lx154a2422_gamma(void);
void lx154a2422_rotate(int degrees, display_padding_t* padding);
uint32_t lx154a2422_transform_touch_coords(uint16_t x, uint16_t y);
#endif

View File

@ -1,3 +1,4 @@
#include "display_interface.h"
#include "displays/st7789v.h"
void tf15411a_init_seq(void) {
@ -91,3 +92,67 @@ void tf15411a_init_seq(void) {
DATA(0x37);
DATA(0x8F);
}
void tf15411a_rotate(int degrees, display_padding_t* padding) {
uint16_t shift = 0;
char BX = 0, BY = 0;
#define RGB (1 << 3)
#define ML (1 << 4) // vertical refresh order
#define MH (1 << 2) // horizontal refresh order
#define MV (1 << 5)
#define MX (1 << 6)
#define MY (1 << 7)
// MADCTL: Memory Data Access Control - reference:
// section 9.3 in the ILI9341 manual
// section 6.2.18 in the GC9307 manual
// section 8.12 in the ST7789V manual
uint8_t display_command_parameter = 0;
switch (degrees) {
case 0:
display_command_parameter = 0;
BY = 1;
break;
case 90:
display_command_parameter = MV | MX | MH | ML;
BX = 0;
shift = 1;
break;
case 180:
display_command_parameter = MX | MY | MH | ML;
BY = 1;
shift = 1;
break;
case 270:
display_command_parameter = MV | MY;
BX = 0;
break;
}
display_command_parameter ^= RGB | MY; // XOR RGB and MY settings
CMD(0x36);
DATA(display_command_parameter);
if (shift) {
// GATECTRL: Gate Control; NL = 240 gate lines, first scan line is
// gate 80.; gate scan direction 319 -> 0
CMD(0xE4);
DATA(0x1D);
DATA(0x00);
DATA(0x11);
} else {
// GATECTRL: Gate Control; NL = 240 gate lines, first scan line is
// gate 80.; gate scan direction 319 -> 0
CMD(0xE4);
DATA(0x1D);
DATA(0x0A);
DATA(0x11);
}
// reset the column and page extents
display_set_window(0, 0, DISPLAY_RESX - 1, DISPLAY_RESY - 1);
padding->x = BX ? (MAX_DISPLAY_RESY - DISPLAY_RESY) : 0;
padding->y = BY ? (MAX_DISPLAY_RESY - DISPLAY_RESY) : 0;
}

View File

@ -1,8 +1,9 @@
#ifndef TT_OLD1_H_
#define TT_OLD1_H_
#ifndef TF15411A_H_
#define TF15411A_H_
// GC9307 IC controller
void tf15411a_init_seq(void);
void tf15411a_rotate(int degrees, display_padding_t* padding);
#endif

View File

@ -67,11 +67,14 @@ __IO DISP_MEM_TYPE *const DISPLAY_DATA_ADDRESS =
#define DISPLAY_ID_ILI9341V 0x009341U
static int DISPLAY_ORIENTATION = -1;
static display_padding_t DISPLAY_PADDING = {0};
void display_pixeldata(uint16_t c) { PIXELDATA(c); }
void display_pixeldata_dirty(void) {}
#ifdef DISPLAY_IDENTIFY
static uint32_t read_display_id(uint8_t command) {
volatile uint8_t c = 0;
uint32_t id = 0;
@ -107,6 +110,9 @@ static uint32_t display_identify(void) {
id_set = 1;
return id;
}
#else
static uint32_t display_identify(void) { return DISPLAY_ID_ST7789V; }
#endif
bool display_is_inverted() {
bool inv_on = false;
@ -151,13 +157,11 @@ static void display_unsleep(void) {
}
}
static struct { uint16_t x, y; } BUFFER_OFFSET;
void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
x0 += BUFFER_OFFSET.x;
x1 += BUFFER_OFFSET.x;
y0 += BUFFER_OFFSET.y;
y1 += BUFFER_OFFSET.y;
x0 += DISPLAY_PADDING.x;
x1 += DISPLAY_PADDING.x;
y0 += DISPLAY_PADDING.y;
y1 += DISPLAY_PADDING.y;
uint32_t id = display_identify();
if ((id == DISPLAY_ID_ILI9341V) || (id == DISPLAY_ID_GC9307) ||
(id == DISPLAY_ID_ST7789V)) {
@ -185,70 +189,16 @@ int display_orientation(int degrees) {
// 2 bytes per pixel because we're using RGB 5-6-5 format
PIXELDATA(0x0000);
}
uint16_t shift = 0;
char BX = 0, BY = 0;
#ifdef TREZOR_MODEL_T
uint32_t id = display_identify();
if ((id == DISPLAY_ID_ILI9341V) || (id == DISPLAY_ID_GC9307) ||
(id == DISPLAY_ID_ST7789V)) {
#define RGB (1 << 3)
#define ML (1 << 4) // vertical refresh order
#define MH (1 << 2) // horizontal refresh order
#define MV (1 << 5)
#define MX (1 << 6)
#define MY (1 << 7)
// MADCTL: Memory Data Access Control - reference:
// section 9.3 in the ILI9341 manual
// section 6.2.18 in the GC9307 manual
// section 8.12 in the ST7789V manual
uint8_t display_command_parameter = 0;
switch (degrees) {
case 0:
display_command_parameter = 0;
BY = (id == DISPLAY_ID_GC9307);
break;
case 90:
display_command_parameter = MV | MX | MH | ML;
BX = (id != DISPLAY_ID_GC9307);
shift = 1;
break;
case 180:
display_command_parameter = MX | MY | MH | ML;
BY = (id == DISPLAY_ID_GC9307);
shift = 1;
break;
case 270:
display_command_parameter = MV | MY;
BX = (id != DISPLAY_ID_GC9307);
break;
}
if (id == DISPLAY_ID_GC9307) {
display_command_parameter ^= RGB | MY; // XOR RGB and MY settings
}
CMD(0x36);
DATA(display_command_parameter);
if (shift) {
// GATECTRL: Gate Control; NL = 240 gate lines, first scan line is
// gate 80.; gate scan direction 319 -> 0
CMD(0xE4);
DATA(0x1D);
DATA(0x00);
DATA(0x11);
} else {
// GATECTRL: Gate Control; NL = 240 gate lines, first scan line is
// gate 80.; gate scan direction 319 -> 0
CMD(0xE4);
DATA(0x1D);
DATA(0x0A);
DATA(0x11);
}
// reset the column and page extents
display_set_window(0, 0, DISPLAY_RESX - 1, DISPLAY_RESY - 1);
if (id == DISPLAY_ID_GC9307) {
tf15411a_rotate(degrees, &DISPLAY_PADDING);
} else {
lx154a2422_rotate(degrees, &DISPLAY_PADDING);
}
BUFFER_OFFSET.x = BX ? (MAX_DISPLAY_RESY - DISPLAY_RESY) : 0;
BUFFER_OFFSET.y = BY ? (MAX_DISPLAY_RESY - DISPLAY_RESY) : 0;
#else
DISPLAY_PANEL_ROTATE(degrees, &BUFFER_OFFSET);
#endif
}
}
return DISPLAY_ORIENTATION;
@ -357,13 +307,15 @@ void display_init(void) {
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_14, GPIO_PIN_RESET);
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
// LCD_FMARK/PD12 (tearing effect)
#ifdef DISPLAY_TE_PIN
// LCD_FMARK (tearing effect)
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStructure.Alternate = 0;
GPIO_InitStructure.Pin = GPIO_PIN_12;
HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.Pin = DISPLAY_TE_PIN;
HAL_GPIO_Init(DISPLAY_TE_PORT, &GPIO_InitStructure);
#endif
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Pull = GPIO_NOPULL;
@ -422,15 +374,18 @@ void display_reinit(void) {
}
void display_sync(void) {
#ifdef DISPLAY_TE_PIN
uint32_t id = display_identify();
if (id && (id != DISPLAY_ID_GC9307)) {
// synchronize with the panel synchronization signal
// in order to avoid visual tearing effects
while (GPIO_PIN_SET == HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_12)) {
while (GPIO_PIN_SET == HAL_GPIO_ReadPin(DISPLAY_TE_PORT, DISPLAY_TE_PIN)) {
}
while (GPIO_PIN_RESET == HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_12)) {
while (GPIO_PIN_RESET ==
HAL_GPIO_ReadPin(DISPLAY_TE_PORT, DISPLAY_TE_PIN)) {
}
}
#endif
}
void display_refresh(void) {}

View File

@ -2,6 +2,12 @@
#define _ST7789V_H
#include STM32_HAL_H
typedef struct {
uint16_t x;
uint16_t y;
} display_padding_t;
#include TREZOR_BOARD
// ILI9341V, GC9307 and ST7789V drivers support 240px x 320px display resolution

View File

@ -44,6 +44,7 @@
*/
#include STM32_HAL_H
#include TREZOR_BOARD
#include <string.h>
@ -68,7 +69,7 @@ void DMA2_Stream3_IRQHandler(void) {
}
static inline void sdcard_default_pin_state(void) {
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET); // SD_ON/PC0
HAL_GPIO_WritePin(SD_ENABLE_PORT, SD_ENABLE_PIN, GPIO_PIN_SET); // SD_ON
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_RESET); // SD_DAT0/PC8
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET); // SD_DAT1/PC9
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_10, GPIO_PIN_RESET); // SD_DAT2/PC10
@ -82,8 +83,8 @@ static inline void sdcard_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_0;
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.Pin = SD_ENABLE_PIN;
HAL_GPIO_Init(SD_ENABLE_PORT, &GPIO_InitStructure);
// configure SD GPIO
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
@ -99,12 +100,13 @@ static inline void sdcard_default_pin_state(void) {
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStructure.Pin = GPIO_PIN_13;
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.Pin = SD_DETECT_PIN;
HAL_GPIO_Init(SD_DETECT_PORT, &GPIO_InitStructure);
}
static inline void sdcard_active_pin_state(void) {
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_RESET); // SD_ON/PC0
HAL_GPIO_WritePin(SD_ENABLE_PORT, SD_ENABLE_PIN,
GPIO_PIN_RESET); // SD_ON/PC0
HAL_Delay(10); // we need to wait until the circuit fully kicks-in
GPIO_InitTypeDef GPIO_InitStructure;
@ -210,7 +212,8 @@ void sdcard_power_off(void) {
}
secbool sdcard_is_present(void) {
return sectrue * (GPIO_PIN_RESET == HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13));
return sectrue *
(GPIO_PIN_RESET == HAL_GPIO_ReadPin(SD_DETECT_PORT, SD_DETECT_PIN));
}
uint64_t sdcard_get_capacity_in_bytes(void) {

View File

@ -47,15 +47,14 @@
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
HAL_GPIO_WritePin(TOUCH_ON_PORT, TOUCH_ON_PIN,
GPIO_PIN_SET); // CTP_ON (active low) i.e.- CTPM power
// off when set/high/log 1
HAL_GPIO_WritePin(
GPIOC, GPIO_PIN_4,
GPIO_PIN_RESET); // CTP_INT/PC4 normally an input, but drive low as an
// output while powered off
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5,
GPIO_PIN_RESET); // CTP_REST/PC5 (active low) i.e.- CTPM
HAL_GPIO_WritePin(TOUCH_INT_PORT, TOUCH_INT_PIN,
GPIO_PIN_RESET); // CTP_INT normally an input, but drive
// low as an output while powered off
HAL_GPIO_WritePin(TOUCH_RST_PORT, TOUCH_RST_PIN,
GPIO_PIN_RESET); // CTP_REST (active low) i.e.- CTPM
// held in reset until released
// set above pins to OUTPUT / NOPULL
@ -64,10 +63,12 @@ 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;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_4 | GPIO_PIN_5;
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.Pin = TOUCH_INT_PIN;
HAL_GPIO_Init(TOUCH_INT_PORT, &GPIO_InitStructure);
GPIO_InitStructure.Pin = TOUCH_RST_PIN;
HAL_GPIO_Init(TOUCH_RST_PORT, &GPIO_InitStructure);
GPIO_InitStructure.Pin = TOUCH_ON_PIN;
HAL_GPIO_Init(TOUCH_ON_PORT, &GPIO_InitStructure);
// in-case power was on, or CTPM was active make sure to wait long enough
// for these changes to take effect. a reset needs to be low for
@ -77,20 +78,21 @@ static void touch_default_pin_state(void) {
}
static void touch_active_pin_state(void) {
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, GPIO_PIN_RESET); // CTP_ON/PB10
HAL_GPIO_WritePin(TOUCH_ON_PORT, TOUCH_ON_PIN, GPIO_PIN_RESET); // CTP_ON
HAL_Delay(10); // we need to wait until the circuit fully kicks-in
GPIO_InitTypeDef GPIO_InitStructure;
// PC4 capacitive touch panel module (CTPM) interrupt (INT) input
// capacitive touch panel module (CTPM) interrupt (INT) input
GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStructure.Pin = GPIO_PIN_4;
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
__HAL_GPIO_EXTI_CLEAR_FLAG(GPIO_PIN_4);
GPIO_InitStructure.Pin = TOUCH_INT_PIN;
HAL_GPIO_Init(TOUCH_INT_PORT, &GPIO_InitStructure);
__HAL_GPIO_EXTI_CLEAR_FLAG(TOUCH_INT_PIN);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_SET); // release CTPM reset
HAL_GPIO_WritePin(TOUCH_RST_PORT, TOUCH_RST_PIN,
GPIO_PIN_SET); // release CTPM reset
HAL_Delay(310); // "Time of starting to report point after resetting" min is
// 300ms, giving an extra 10ms
}
@ -115,6 +117,7 @@ void touch_power_on(void) {
// turn on CTP circuitry
touch_active_pin_state();
HAL_Delay(50);
}
@ -131,12 +134,12 @@ void touch_init(void) {
GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStructure.Pin = GPIO_PIN_4;
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
__HAL_GPIO_EXTI_CLEAR_FLAG(GPIO_PIN_4);
GPIO_InitStructure.Pin = TOUCH_INT_PIN;
HAL_GPIO_Init(TOUCH_INT_PORT, &GPIO_InitStructure);
__HAL_GPIO_EXTI_CLEAR_FLAG(TOUCH_INT_PIN);
touch_set_mode();
touch_sensitivity(0x06);
touch_sensitivity(TOUCH_SENSITIVITY);
}
void touch_sensitivity(uint8_t value) {
@ -161,9 +164,9 @@ uint32_t touch_is_detected(void) {
// Reference section 1.2 of "Application Note for FT6x06 CTPM". we
// configure the touch controller to use "interrupt trigger mode".
uint32_t event = __HAL_GPIO_EXTI_GET_FLAG(GPIO_PIN_4);
uint32_t event = __HAL_GPIO_EXTI_GET_FLAG(TOUCH_INT_PIN);
if (event != 0) {
__HAL_GPIO_EXTI_CLEAR_FLAG(GPIO_PIN_4);
__HAL_GPIO_EXTI_CLEAR_FLAG(TOUCH_INT_PIN);
}
return event;
@ -239,8 +242,8 @@ uint32_t touch_read(void) {
// first touch) (tested with FT6206)
const uint32_t event_flag = touch_data[3] & 0xC0;
if (touch_data[1] == GESTURE_NO_GESTURE) {
xy = touch_pack_xy((X_POS_MSB << 8) | X_POS_LSB,
(Y_POS_MSB << 8) | Y_POS_LSB);
xy = TRANSFORM_TOUCH_COORDS((X_POS_MSB << 8) | X_POS_LSB,
(Y_POS_MSB << 8) | Y_POS_LSB);
if ((number_of_touch_points == 1) && (event_flag == EVENT_PRESS_DOWN)) {
touching = 1;
return TOUCH_START | xy;