From 7ca460457ea5011edd9618efcd4508fef99a01b7 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Thu, 19 Jul 2018 00:01:54 +0200 Subject: [PATCH] embed: refactor sdcard/touch, extract touch_power_on/off functions from touch_init --- embed/bootloader/main.c | 1 + embed/prodtest/main.c | 5 ++ embed/trezorhal/sdcard.c | 38 +++++++------- embed/trezorhal/sdcard.h | 2 +- embed/trezorhal/touch.c | 106 +++++++++++++++++++++++++-------------- embed/trezorhal/touch.h | 2 + 6 files changed, 94 insertions(+), 60 deletions(-) diff --git a/embed/bootloader/main.c b/embed/bootloader/main.c index 6784ed9fbf..cd5165fecb 100644 --- a/embed/bootloader/main.c +++ b/embed/bootloader/main.c @@ -233,6 +233,7 @@ main_start: #endif touch_init(); + touch_power_on(); // delay to detect touch uint32_t touched = 0; diff --git a/embed/prodtest/main.c b/embed/prodtest/main.c index e96a67a923..beed1b5397 100644 --- a/embed/prodtest/main.c +++ b/embed/prodtest/main.c @@ -208,6 +208,8 @@ static void test_touch(const char *args) } display_refresh(); + touch_power_on(); + uint32_t evt = 0; if (touch_click_timeout(&evt, timeout * 1000)) { uint16_t x = touch_get_x(evt); @@ -218,6 +220,8 @@ static void test_touch(const char *args) } display_clear(); display_refresh(); + + touch_power_off(); } static void test_pwm(const char *args) @@ -253,6 +257,7 @@ static void test_sd(void) vcp_printf("ERROR sdcard_write_blocks (%d)", j); goto power_off; } + HAL_Delay(1000); if (sectrue != sdcard_read_blocks(buf2, 0, BLOCK_SIZE / SDCARD_BLOCK_SIZE)) { vcp_printf("ERROR sdcard_read_blocks (%d)", j); goto power_off; diff --git a/embed/trezorhal/sdcard.c b/embed/trezorhal/sdcard.c index 847bf35ee4..bec743a87e 100644 --- a/embed/trezorhal/sdcard.c +++ b/embed/trezorhal/sdcard.c @@ -62,11 +62,16 @@ static void sdcard_default_pin_state(void) { } void sdcard_init(void) { - // invalidate the sd_handle - sd_handle.Instance = NULL; - GPIO_InitTypeDef GPIO_InitStructure; + // configure the SD card circuitry on/off pin + GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStructure.Pull = GPIO_NOPULL; + GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStructure.Pin = GPIO_PIN_0; + HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET); + HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); + // configure SD GPIO GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; GPIO_InitStructure.Pull = GPIO_PULLUP; @@ -80,18 +85,10 @@ void sdcard_init(void) { // configure the SD card detect pin GPIO_InitStructure.Mode = GPIO_MODE_INPUT; GPIO_InitStructure.Pull = GPIO_PULLUP; - GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStructure.Pin = GPIO_PIN_13; HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); - // configure the SD card circuitry on/off pin - GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStructure.Pull = GPIO_NOPULL; - GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; - GPIO_InitStructure.Pin = GPIO_PIN_0; - HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET); - HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); - sdcard_default_pin_state(); } @@ -105,21 +102,16 @@ void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd) { __HAL_RCC_SDIO_CLK_DISABLE(); } -secbool sdcard_is_present(void) { - return sectrue * (GPIO_PIN_RESET == HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13)); -} - secbool sdcard_power_on(void) { - // turn on SD card circuitry - HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_RESET); // SD_ON/PC0 - HAL_Delay(50); - if (sectrue != sdcard_is_present()) { - goto error; + return secfalse; } if (sd_handle.Instance) { return sectrue; } + // turn on SD card circuitry + HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_RESET); // SD_ON/PC0 + HAL_Delay(50); // SD device interface configuration sd_handle.Instance = SDIO; @@ -161,6 +153,10 @@ void sdcard_power_off(void) { sdcard_default_pin_state(); } +secbool sdcard_is_present(void) { + return sectrue * (GPIO_PIN_RESET == HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13)); +} + uint64_t sdcard_get_capacity_in_bytes(void) { if (sd_handle.Instance == NULL) { return 0; diff --git a/embed/trezorhal/sdcard.h b/embed/trezorhal/sdcard.h index ebe2d47fb8..2e16d78d46 100644 --- a/embed/trezorhal/sdcard.h +++ b/embed/trezorhal/sdcard.h @@ -52,9 +52,9 @@ #define SDCARD_BLOCK_SIZE (512) void sdcard_init(void); -secbool __wur sdcard_is_present(void); secbool __wur sdcard_power_on(void); void sdcard_power_off(void); +secbool __wur sdcard_is_present(void); uint64_t sdcard_get_capacity_in_bytes(void); secbool __wur sdcard_read_blocks(uint32_t *dest, uint32_t block_num, uint32_t num_blocks); secbool __wur sdcard_write_blocks(const uint32_t *src, uint32_t block_num, uint32_t num_blocks); diff --git a/embed/trezorhal/touch.c b/embed/trezorhal/touch.c index 488623b35a..ee97e8b72b 100644 --- a/embed/trezorhal/touch.c +++ b/embed/trezorhal/touch.c @@ -39,20 +39,14 @@ static I2C_HandleTypeDef i2c_handle; -static void touch_power_on(void) -{ - HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, GPIO_PIN_RESET); // CTP_ON/PB10 - HAL_Delay(50); +static void touch_default_pin_state(void) { + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, GPIO_PIN_SET); // CTP_ON/PB10 + 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_5, GPIO_PIN_RESET); // CTPM_REST/PC5 + // don't touch CTPM_INT = leave in High Z } -/* -static void touch_power_off(void) -{ - HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, GPIO_PIN_SET); // CTP_ON/PB10 - HAL_Delay(50); -} -*/ - void touch_init(void) { GPIO_InitTypeDef GPIO_InitStructure; @@ -65,19 +59,56 @@ void touch_init(void) HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, GPIO_PIN_SET); HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); - touch_power_on(); - - // Enable I2C clock - __HAL_RCC_I2C1_CLK_ENABLE(); - - // Init SCL and SDA GPIO lines (PB6 & PB7) - GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7; + // 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.Pin = GPIO_PIN_4; + GPIO_InitStructure.Mode = GPIO_MODE_INPUT; + GPIO_InitStructure.Pull = GPIO_PULLUP; + GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStructure.Alternate = 0; + HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); + */ + + // PC5 capacitive touch panel module (CTPM) reset (RSTN) + GPIO_InitStructure.Pin = GPIO_PIN_5; + GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStructure.Pull = GPIO_NOPULL; + GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStructure.Alternate = 0; + HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_SET); // set the pin value before driving it out + HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); // switch the pin to be an output + + touch_default_pin_state(); +} + +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 touch_power_on(void) { + if (i2c_handle.Instance) { + return; + } + + // turn on CTP circuitry + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, GPIO_PIN_RESET); // CTP_ON/PB10 + HAL_Delay(50); + + // I2C device interface configuration i2c_handle.Instance = I2C1; i2c_handle.Init.ClockSpeed = 400000; i2c_handle.Init.DutyCycle = I2C_DUTYCYCLE_16_9; @@ -88,32 +119,31 @@ void touch_init(void) i2c_handle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; i2c_handle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; - ensure(sectrue * (HAL_OK == HAL_I2C_Init(&i2c_handle)), NULL); + if (HAL_OK != HAL_I2C_Init(&i2c_handle)) { + ensure(secfalse, NULL); + return; + } - // PC4 capacitive touch panel module (CTPM) interrupt (INT) input - //GPIO_InitStructure.Pin = GPIO_PIN_4; - //GPIO_InitStructure.Mode = GPIO_MODE_INPUT; - //GPIO_InitStructure.Pull = GPIO_PULLUP; - //GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW; - //GPIO_InitStructure.Alternate = 0; - //HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); - - // PC5 capacitive touch panel module (CTPM) reset (RSTN) - GPIO_InitStructure.Pin = GPIO_PIN_5; - GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStructure.Pull = GPIO_NOPULL; - GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW; - GPIO_InitStructure.Alternate = 0; - HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_RESET); // set the pin value before driving it out - HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); // switch the pin to be an output // reset the touch panel by keeping its reset line low (active low) low for a minimum of 5ms HAL_Delay(10); // being conservative, min is 5ms HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_SET); // release CTPM reset HAL_Delay(310); // "Time of starting to report point after resetting" min is 300ms, giving an extra 10ms // set register 0xA4 G_MODE to interrupt polling mode (0x00). basically, CTPM keeps this input line (to PC4) low while a finger is on the screen. - //uint8_t touch_panel_config[] = {0xA4, 0x00}; - //ensure(sectrue * (HAL_OK == HAL_I2C_Master_Transmit(&i2c_handle, TOUCH_ADDRESS, touch_panel_config, sizeof(touch_panel_config), 10)), NULL); + /* + uint8_t touch_panel_config[] = {0xA4, 0x00}; + ensure(sectrue * (HAL_OK == HAL_I2C_Master_Transmit(&i2c_handle, TOUCH_ADDRESS, touch_panel_config, sizeof(touch_panel_config), 10)), NULL); + */ +} + +void touch_power_off(void) { + if (i2c_handle.Instance != NULL) { + HAL_I2C_DeInit(&i2c_handle); + i2c_handle.Instance = NULL; + } + // turn off CTP circuitry + HAL_Delay(50); + touch_default_pin_state(); } uint32_t touch_read(void) diff --git a/embed/trezorhal/touch.h b/embed/trezorhal/touch.h index ff705d703f..ffe4776166 100644 --- a/embed/trezorhal/touch.h +++ b/embed/trezorhal/touch.h @@ -27,6 +27,8 @@ #define TOUCH_END (4U << 24) void touch_init(void); +void touch_power_on(void); +void touch_power_off(void); uint32_t touch_read(void); uint32_t touch_click(void); inline uint16_t touch_get_x(uint32_t evt) { return (evt >> 12) & 0xFFF; }