Optimitzations to reduce number of interruptions.

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos
2026-02-19 15:47:13 +01:00
parent 636f929f2d
commit 5dd2f7fa73
4 changed files with 61 additions and 33 deletions

View File

@@ -190,7 +190,9 @@ void *apdu_thread(void *arg) {
uint32_t m = 0; uint32_t m = 0;
queue_remove_blocking(&usb_to_card_q, &m); queue_remove_blocking(&usb_to_card_q, &m);
uint32_t flag = m + 1; uint32_t flag = m + 1;
queue_add_blocking(&card_to_usb_q, &flag); if (m != EV_CMD_AVAILABLE) {
queue_add_blocking(&card_to_usb_q, &flag);
}
if (m == EV_VERIFY_CMD_AVAILABLE || m == EV_MODIFY_CMD_AVAILABLE) { if (m == EV_VERIFY_CMD_AVAILABLE || m == EV_MODIFY_CMD_AVAILABLE) {
set_res_sw(0x6f, 0x00); set_res_sw(0x6f, 0x00);

View File

@@ -19,32 +19,53 @@
#define _DEBUG_H_ #define _DEBUG_H_
#if defined(DEBUG_APDU) && DEBUG_APDU == 1 #if defined(DEBUG_APDU) && DEBUG_APDU == 1
#define DEBUG_PAYLOAD(_p, _s) { \ #include <inttypes.h>
printf("Payload %s (%zu bytes) [%s:%d]:\n", #_p, (size_t)(_s), __FILE__, __LINE__); \ #include <stddef.h>
for (size_t _i = 0; _i < (size_t)(_s); _i += 16) { \ #include <stdint.h>
printf("%" PRIxPTR "h : ", (uintptr_t) (_i + _p)); \ #include <stdio.h>
for (size_t _j = 0; _j < 16; _j++) { \
if (_j < (size_t)(_s) - _i) printf("%02X ", (_p)[_i + _j]); \ static inline void debug_payload_impl(const char *name, const uint8_t *p, size_t s, const char *file, int line) {
else printf(" "); \ printf("Payload %s (%zu bytes) [%s:%d]:\n", name, s, file, line);
if (_j == 7) printf(" "); \ for (size_t i = 0; i < s; i += 16) {
} printf(": "); \ printf("%" PRIxPTR "h : ", (uintptr_t)(p + i));
for (size_t _j = 0; _j < 16; _j++) { \ for (size_t j = 0; j < 16; j++) {
if (_j < (size_t)(_s) - _i && (_p)[_i + _j] > 32 && (_p)[_i + _j] != 127 && (_p)[_i + _j] < 176) printf("%c", (_p)[_i + _j]); \ if (j < s - i) {
else printf(" "); \ printf("%02X ", p[i + j]);
if (_j == 7) printf(" "); \ }
} \ else {
printf("\n"); \ printf(" ");
} printf("\n"); \ }
} if (j == 7) {
#define DEBUG_DATA(_p, _s) { \ printf(" ");
printf("Data %s (%zu bytes) [%s:%d]:\n", #_p, (size_t)(_s), __FILE__, __LINE__); \ }
char *_tmp = (char *) calloc(2 * (_s) + 1, sizeof(char)); \ }
for (size_t _i = 0; _i < (size_t)(_s); _i++) { \ printf(": ");
sprintf(&_tmp[2 * _i], "%02X", (_p)[_i]); \ for (size_t j = 0; j < 16; j++) {
} \ if (j < s - i && p[i + j] > 32 && p[i + j] != 127 && p[i + j] < 176) {
printf("%s\n", _tmp); \ printf("%c", p[i + j]);
free(_tmp); \ }
else {
printf(" ");
}
if (j == 7) {
printf(" ");
}
}
printf("\n");
} }
printf("\n");
}
static inline void debug_data_impl(const char *name, const uint8_t *p, size_t s, const char *file, int line) {
printf("Data %s (%zu bytes) [%s:%d]:\n", name, s, file, line);
for (size_t i = 0; i < s; i++) {
printf("%02X", p[i]);
}
printf("\n");
}
#define DEBUG_PAYLOAD(_p, _s) debug_payload_impl(#_p, (const uint8_t *)(_p), (size_t)(_s), __FILE__, __LINE__)
#define DEBUG_DATA(_p, _s) debug_data_impl(#_p, (const uint8_t *)(_p), (size_t)(_s), __FILE__, __LINE__)
#else #else
#define DEBUG_PAYLOAD(_p, _s) #define DEBUG_PAYLOAD(_p, _s)

View File

@@ -177,20 +177,20 @@ int driver_write_ccid(uint8_t itf, const uint8_t *tx_buffer, uint16_t buffer_siz
if (*tx_buffer != 0x81) { if (*tx_buffer != 0x81) {
DEBUG_PAYLOAD(tx_buffer, buffer_size); DEBUG_PAYLOAD(tx_buffer, buffer_size);
} }
int r = tud_vendor_n_write(itf, tx_buffer, buffer_size); uint32_t written = tud_vendor_n_write(itf, tx_buffer, buffer_size);
if (r > 0) { if (written > 0) {
tud_vendor_n_flush(itf); tud_vendor_n_flush(itf);
ccid_tx[itf].r_ptr += (uint16_t)buffer_size; ccid_tx[itf].r_ptr += (uint16_t)written;
if (ccid_tx[itf].r_ptr >= ccid_tx[itf].w_ptr) { if (ccid_tx[itf].r_ptr >= ccid_tx[itf].w_ptr) {
ccid_tx[itf].r_ptr = ccid_tx[itf].w_ptr = 0; ccid_tx[itf].r_ptr = ccid_tx[itf].w_ptr = 0;
} }
} }
#ifdef ENABLE_EMULATION #ifdef ENABLE_EMULATION
tud_vendor_tx_cb(itf, r); tud_vendor_tx_cb(itf, written);
#endif #endif
return r; return (int)written;
} }
int ccid_write_fast(uint8_t itf, const uint8_t *buffer, uint16_t buffer_size) { int ccid_write_fast(uint8_t itf, const uint8_t *buffer, uint16_t buffer_size) {

View File

@@ -178,8 +178,10 @@ void usb_send_event(uint32_t flag) {
if (flag == EV_CMD_AVAILABLE) { if (flag == EV_CMD_AVAILABLE) {
timeout_start(); timeout_start();
} }
uint32_t m; if (flag != EV_CMD_AVAILABLE) {
queue_remove_blocking(&card_to_usb_q , &m); uint32_t m;
queue_remove_blocking(&card_to_usb_q , &m);
}
#ifndef ENABLE_EMULATION #ifndef ENABLE_EMULATION
mutex_exit(&mutex); mutex_exit(&mutex);
#endif #endif
@@ -254,6 +256,9 @@ void usb_task() {
int card_status(uint8_t itf) { int card_status(uint8_t itf) {
if (card_locked_itf == itf) { if (card_locked_itf == itf) {
if (timeout == 0) {
return PICOKEY_ERR_FILE_NOT_FOUND;
}
uint32_t m = 0x0; uint32_t m = 0x0;
#ifndef ENABLE_EMULATION #ifndef ENABLE_EMULATION
mutex_enter_blocking(&mutex); mutex_enter_blocking(&mutex);