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

embed: refactor sdcard/touch, extract touch_power_on/off functions from touch_init

This commit is contained in:
Pavol Rusnak 2018-07-19 00:01:54 +02:00
parent 3067339b41
commit 7ca460457e
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
6 changed files with 94 additions and 60 deletions

View File

@ -233,6 +233,7 @@ main_start:
#endif
touch_init();
touch_power_on();
// delay to detect touch
uint32_t touched = 0;

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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)

View File

@ -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; }