Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USB data transmission timeout handling && fingerprint/camera error prompts #229

Merged
merged 4 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions core/embed/extmod/modtrezorio/modtrezorio-poll.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,18 +248,23 @@ STATIC mp_obj_t mod_trezorio_poll(mp_obj_t ifaces, mp_obj_t list_ref,
}
}
} else if (mode == POLL_WRITE) {
int res = usb_webusb_can_write(iface);
if (sectrue == usb_hid_can_write(iface)) {
ret->items[0] = MP_OBJ_NEW_SMALL_INT(i);
ret->items[1] = mp_const_none;
return mp_const_true;
} else if (sectrue == usb_webusb_can_write(iface)) {
} else if (sectrue == res) {
ret->items[0] = MP_OBJ_NEW_SMALL_INT(i);
ret->items[1] = mp_const_none;
ret->items[1] = mp_const_true;
return mp_const_true;
} else if (res == -1) {
ret->items[0] = MP_OBJ_NEW_SMALL_INT(i);
ret->items[1] = mp_const_false;
return mp_const_true;
} else if (iface == SPI_IFACE) {
if (sectrue == spi_can_write()) {
ret->items[0] = MP_OBJ_NEW_SMALL_INT(i);
ret->items[1] = mp_const_none;
ret->items[1] = mp_const_true;
lihuanhuan marked this conversation as resolved.
Show resolved Hide resolved
return mp_const_true;
}
}
Expand Down
24 changes: 23 additions & 1 deletion core/embed/fp_sensor_wrapper/fingerprint.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
#endif
extern uint8_t MAX_USER_COUNT;

static bool fingerprint_module_status = false;

bool fingerprint_module_status_get(void)
{
return fingerprint_module_status;
}

#ifdef EMULATOR
void fingerprint_get_version(char* version)
{
Expand Down Expand Up @@ -67,13 +74,24 @@ void fingerprint_init(void)
ensure_ex(fpsensor_init(), FPSENSOR_OK, "fpsensor_init failed");
ensure_ex(fpsensor_adc_init(18, 13, 4, 3), FPSENSOR_OK, "fpsensor_adc_init failed");
ensure_ex(fpsensor_set_config_param(0xC0, 10), FPSENSOR_OK, "fpsensor_set_config_param failed");
ensure_ex(FpAlgorithmInit(TEMPLATE_ADDR_START), FPSENSOR_OK, "FpAlgorithmInit failed");
if ( FpAlgorithmInit(TEMPLATE_ADDR_START) == FPSENSOR_OK )
{
fingerprint_module_status = true;
}
else
{
return;
}
lihuanhuan marked this conversation as resolved.
Show resolved Hide resolved
MAX_USER_COUNT = MAX_FINGERPRINT_COUNT;
fingerprint_enter_sleep();
}

int fingerprint_enter_sleep(void)
{
if ( !fingerprint_module_status )
{
return -1;
}
lihuanhuan marked this conversation as resolved.
Show resolved Hide resolved
if ( FpsSleep(256) != 0 )
{
return -1;
Expand All @@ -84,6 +102,10 @@ int fingerprint_enter_sleep(void)

int fingerprint_detect(void)
{
if ( !fingerprint_module_status )
{
return 0;
}
return FpsDetectFinger();
}

Expand Down
1 change: 1 addition & 0 deletions core/embed/fp_sensor_wrapper/fingerprint.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ typedef enum _FP_RESULT
FP_NOT_MATCH = 6,
} FP_RESULT;

bool fingerprint_module_status_get(void);
void fingerprint_get_version(char* version);
void fingerprint_init(void);
int fingerprint_detect(void);
Expand Down
15 changes: 10 additions & 5 deletions core/embed/fp_sensor_wrapper/fpsensor_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ uint8_t fpsensor_get_HWID(uint8_t hwid[2])
buffer[0] = FPSENSOR_REG_HWID;
buffer[1] = 0x00;
buffer[2] = 0x00;
status = fpsensor_spi_transceive(buffer, 3);
memcpy(hwid, buffer + 1, 2);
memcpy(hw_id, hwid, 2);
hw_id_cached = true;
return status;
fpsensor_spi_transceive(buffer, 3);
status = buffer[0];
if ( status == FPSENSOR_OK )
{
memcpy(hwid, buffer + 1, 2);
memcpy(hw_id, hwid, 2);
hw_id_cached = true;
return FPSENSOR_OK;
}
return FPSENSOR_SPI_ERROR;
}

uint8_t fpsensor_print_VID(void)
Expand Down
31 changes: 21 additions & 10 deletions core/embed/trezorhal/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ unsigned char camera_sccb_read_reg(unsigned char reg_addr, unsigned char* data)
unsigned char ret = 0;
if ( HAL_I2C_Mem_Read(i2c_handle_camera, GC2145_ADDR, reg_addr, I2C_MEMADD_SIZE_8BIT, data, 1, 1000) !=
HAL_OK )
{
ret = 1;
}
return ret;
}

Expand All @@ -196,7 +198,7 @@ unsigned char camera_sccb_write_reg(unsigned char reg_addr, unsigned char* data)
return ret;
}

unsigned short camera_get_id(void)
bool camera_get_id(uint16_t* id)
{
union ID
{
Expand All @@ -206,22 +208,29 @@ unsigned short camera_get_id(void)
myid.u16 = 0;

if ( camera_sccb_read_reg(0xF0, &myid.u8[1]) )
return myid.u16;
{
return false;
}
if ( camera_sccb_read_reg(0xF1, &myid.u8[0]) )
return myid.u16;
{
return false;
}

return myid.u16;
*id = myid.u16;
return true;
}

unsigned char camera_is_online(void)
bool camera_is_online(void)
{
unsigned char ret = 0;
unsigned char read = 0;

if ( camera_sccb_read_reg(0xFB, &read) )
return ret;
{
return false;
}

ret = (read == GC2145_ADDR) ? 1 : 0;
ret = (read == GC2145_ADDR) ? true : false;

return ret;
}
Expand Down Expand Up @@ -298,11 +307,13 @@ void camera_config_init(void)

int camera_init(void)
{

i2c_handle_camera = &i2c_handles[i2c_find_channel_by_device(I2C_CAMERA)];
i2c_init_by_device(I2C_CAMERA);

camera_get_id();

if ( !camera_is_online() )
{
return -1;
}
dcmi_init();

return 0;
Expand Down
5 changes: 3 additions & 2 deletions core/embed/trezorhal/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __CAMERA_H__

#include STM32_HAL_H
#include "stdbool.h"

extern unsigned short CameraFrameCnt;

Expand Down Expand Up @@ -30,8 +31,8 @@ void camera_suspend(void);
void camera_resume(void);
unsigned char camera_sccb_read_reg(unsigned char reg_addr, unsigned char* data);
unsigned char camera_sccb_write_reg(unsigned char reg_addr, unsigned char* data);
unsigned short camera_get_id(void);
unsigned char camera_is_online(void);
bool camera_get_id(uint16_t* id);
bool camera_is_online(void);
void camera_capture_start(void);
int camera_capture_done(void);
void camera_power_off(void);
Expand Down
4 changes: 4 additions & 0 deletions core/embed/trezorhal/camera_qrcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ int camera_qr_decode(uint32_t x, uint32_t y, uint8_t* data, uint32_t data_len)
{

int len = 0;
if ( !camera_is_online() )
{
return 0;
}
camera_capture_start();
if ( camera_capture_done() )
{
Expand Down
70 changes: 48 additions & 22 deletions core/embed/trezorhal/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ static void lock_burnin_test_otp(void) {
}

#define TIMER_1S 10000
#define TEST_DURATION (3 * 60 * 60 * TIMER_1S) // 3 hours
#define TEST_DURATION (2 * 60 * 60 * TIMER_1S) // 2 hours

static void ui_generic_confirm_simple(const char *msg) {
if (msg == NULL) return;
Expand Down Expand Up @@ -550,6 +550,9 @@ static bool _motor_test(void) {

static bool _camera_test(void) {
int ui_res = 0;
if (!camera_is_online()) {
return false;
}
while (1) {
camera_capture_start();
if (camera_capture_done()) {
Expand Down Expand Up @@ -633,6 +636,9 @@ static bool _flashled_test(void) {
static bool _fp_test(void) {
int ui_res = 0;
uint8_t image_data[88 * 112 + 2];
if (!fingerprint_module_status_get()) {
return false;
}
ui_generic_confirm_simple("FINGERPRINT test");
while (1) {
if (FpsDetectFinger() == 1) {
Expand Down Expand Up @@ -797,6 +803,7 @@ void device_burnin_test(bool force) {
static uint16_t voltage_last = 0, current_last = 0,
discahrging_current_last = 0;
volatile uint32_t battery_pre = 0, battery_now = 0;
uint32_t loop_counter = 0;
uint8_t image_data[88 * 112 + 2];
int flashled_value = 1;

Expand Down Expand Up @@ -852,6 +859,14 @@ void device_burnin_test(bool force) {
return;
}

if (!fingerprint_module_status_get()) {
ensure(secfalse, "Fingerprint init failed");
}

if (!camera_is_online()) {
ensure(secfalse, "Camera init failed");
}

test_timer_init();
jpeg_init();
motor_init();
Expand Down Expand Up @@ -989,29 +1004,40 @@ void device_burnin_test(bool force) {
fingerprint_detect = false;
}

display_printf("Poll card...\n");
nfc_pwr_ctl(true);
HAL_TIM_Base_Stop(&TimHandle);
while (1) {
if (nfc_poll_card()) {
if (nfc_select_aid((uint8_t *)"\xD1\x56\x00\x01\x32\x83\x40\x01",
8)) {
if (lite_card_data_exchange_test()) {
break;
}
} else if (nfc_select_aid(
(uint8_t *)"\x6f\x6e\x65\x6b\x65\x79\x2e\x62"
"\x61\x63\x6b\x75\x70\x01",
14)) {
if (lite_card_data_exchange_test()) {
break;
loop_counter++;

if (loop_counter % 2) {
if (flashled_value) {
ble_set_flashled(0);
}

display_printf("Poll card...\n");
nfc_pwr_ctl(true);
HAL_TIM_Base_Stop(&TimHandle);
while (1) {
if (nfc_poll_card()) {
if (nfc_select_aid(
(uint8_t *)"\xD1\x56\x00\x01\x32\x83\x40\x01", 8)) {
if (lite_card_data_exchange_test()) {
break;
}
} else if (nfc_select_aid(
(uint8_t *)"\x6f\x6e\x65\x6b\x65\x79\x2e\x62"
"\x61\x63\x6b\x75\x70\x01",
14)) {
if (lite_card_data_exchange_test()) {
break;
}
}
}
}
nfc_pwr_ctl(false);
display_printf("Card test passed\n");
HAL_TIM_Base_Start(&TimHandle);
if (flashled_value) {
ble_set_flashled(1);
}
lihuanhuan marked this conversation as resolved.
Show resolved Hide resolved
}
nfc_pwr_ctl(false);
display_printf("Card test passed\n");
HAL_TIM_Base_Start(&TimHandle);

if (!_sdram_test()) {
display_printf("SDRAM test failed\n");
Expand Down Expand Up @@ -1076,13 +1102,13 @@ void device_burnin_test(bool force) {

flashled_now = HAL_GetTick();
if (flashled_value) {
if (flashled_now - flashled_pre > 1000) {
if (flashled_now - flashled_pre > 500) {
flashled_pre = flashled_now;
flashled_value = 0;
ble_set_flashled(flashled_value);
}
} else {
if (flashled_now - flashled_pre > 10000) {
if (flashled_now - flashled_pre > 20000) {
flashled_pre = flashled_now;
flashled_value = 1;
ble_set_flashled(flashled_value);
Expand Down
2 changes: 1 addition & 1 deletion core/embed/trezorhal/usb_webusb-defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ typedef struct {

secbool __wur usb_webusb_add(const usb_webusb_info_t *webusb_info);
secbool __wur usb_webusb_can_read(uint8_t iface_num);
secbool __wur usb_webusb_can_write(uint8_t iface_num);
int __wur usb_webusb_can_write(uint8_t iface_num);
int __wur usb_webusb_read(uint8_t iface_num, uint8_t *buf, uint32_t len);
int __wur usb_webusb_write(uint8_t iface_num, const uint8_t *buf, uint32_t len);

Expand Down
24 changes: 20 additions & 4 deletions core/embed/trezorhal/usb_webusb-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,34 @@ secbool usb_webusb_can_read(uint8_t iface_num) {
return sectrue;
}

secbool usb_webusb_can_write(uint8_t iface_num) {
int usb_webusb_can_write(uint8_t iface_num) {
static uint32_t start = 0;
lihuanhuan marked this conversation as resolved.
Show resolved Hide resolved
usb_iface_t *iface = usb_get_iface(iface_num);
if (iface == NULL) {
return secfalse; // Invalid interface number
}

if (usb_dev_handle.dev_state != USBD_STATE_CONFIGURED) {
return secfalse; // Device is not configured
}

if (iface->type != USB_IFACE_TYPE_WEBUSB) {
return secfalse; // Invalid interface type
}

if (iface->webusb.ep_in_is_idle == 0) {
if (start == 0) {
start = HAL_GetTick();
}
if (HAL_GetTick() - start > 500) {
lihuanhuan marked this conversation as resolved.
Show resolved Hide resolved
start = 0;
iface->webusb.ep_in_is_idle = 1;
return -1; // reset ep_in_is_idle
}
return secfalse; // Last transmission is not over yet
}
if (usb_dev_handle.dev_state != USBD_STATE_CONFIGURED) {
return secfalse; // Device is not configured
}

start = 0;
return sectrue;
}

Expand Down Expand Up @@ -276,6 +290,8 @@ static void usb_webusb_class_data_out(USBD_HandleTypeDef *dev,
if (ep_num == state->ep_out) {
// Save the report length to indicate we have read something, but don't
// schedule next reading until user reads this one
// Clear the IN EP buffer
USBD_LL_FlushEP(dev, state->ep_in);
lihuanhuan marked this conversation as resolved.
Show resolved Hide resolved
state->last_read_len = USBD_LL_GetRxDataSize(dev, ep_num);
}
}
Loading
Loading