diff options
author | Andreas Eversberg <jolly@eversberg.eu> | 2023-08-27 20:28:43 +0200 |
---|---|---|
committer | Andreas Eversberg <jolly@eversberg.eu> | 2023-08-27 20:28:43 +0200 |
commit | 4252ca1f82457337b3559951e7d74494d5fa6a6d (patch) | |
tree | bee2d56d34e4ad36cbecf5979c7892c1a7df3eda /src/sim | |
parent | abe449bd0eaae332623e058912dfa2e8b80a0685 (diff) |
C-Netz-SIM: Added delay before answering to messages
This delay is required, so that the card reader has enough time to
release the I/O line. The AEG OLYMPIA now works.
Diffstat (limited to 'src/sim')
-rw-r--r-- | src/sim/sim.c | 19 | ||||
-rw-r--r-- | src/sim/sim.h | 2 | ||||
-rw-r--r-- | src/sim/sim.ino | 6 |
3 files changed, 22 insertions, 5 deletions
diff --git a/src/sim/sim.c b/src/sim/sim.c index baadb4f..f6c9beb 100644 --- a/src/sim/sim.c +++ b/src/sim/sim.c @@ -1212,8 +1212,11 @@ static int rx_char(sim_sim_t *sim, uint8_t c) sim->block_rx_data[sim->block_count++] = c; return 0; } - sim->l1_state = L1_STATE_IDLE; - rx_block(sim); + sim->l1_state = L1_STATE_COMPLETE; + /* Waiting for timeout, then process the received PDU. + * This way we detect garbage after the message. + * Also we wait for the card reader to release the I/O line. (22 bit durations minimum) */ + return 0; } return -1; @@ -1395,6 +1398,9 @@ int sim_rx(sim_sim_t *sim, uint8_t c) case L1_STATE_RECEIVE: rc = rx_char(sim, c); break; + case L1_STATE_COMPLETE: + PDEBUG(DSIM1, DEBUG_NOTICE, "Received garbage after message!\n"); + sim->l1_state = L1_STATE_GARBAGE; default: break; } @@ -1434,10 +1440,19 @@ void sim_timeout(sim_sim_t *sim) PDEBUG(DSIM1, DEBUG_NOTICE, "Timeout while receiving message!\n"); sim->block_state = BLOCK_STATE_ADDRESS; break; + case L1_STATE_GARBAGE: + PDEBUG(DSIM1, DEBUG_NOTICE, "Timeout after skipping garbage!\n"); + sim->l1_state = L1_STATE_IDLE; + break; case L1_STATE_SEND: PDEBUG(DSIM1, DEBUG_NOTICE, "Timeout while sending message!\n"); sim->l1_state = L1_STATE_IDLE; break; + case L1_STATE_COMPLETE: + /* We did not receive garbage after message, so we process it now. */ + sim->l1_state = L1_STATE_IDLE; + rx_block(sim); + break; default: break; } diff --git a/src/sim/sim.h b/src/sim/sim.h index 3b11552..dccd4ca 100644 --- a/src/sim/sim.h +++ b/src/sim/sim.h @@ -13,6 +13,8 @@ enum l1_state { L1_STATE_IDLE, /* waiting for message or reset */ L1_STATE_SEND, /* sending reply */ L1_STATE_RECEIVE, /* receiving message */ + L1_STATE_COMPLETE, /* received message complete, waiting for card reader to release */ + L1_STATE_GARBAGE, /* received garbage right after frame, waiting for timeout */ }; enum block_state { diff --git a/src/sim/sim.ino b/src/sim/sim.ino index 00b2de0..f8925e0 100644 --- a/src/sim/sim.ino +++ b/src/sim/sim.ino @@ -1,4 +1,4 @@ -/* SIM card for ATMEL + /* SIM card for ATMEL * * (C) 2020 by Andreas Eversberg <jolly@eversberg.eu> * All Rights Reserved @@ -27,14 +27,14 @@ extern "C" #if defined(__AVR_ATtiny85__) #define SERIAL_DATA 4 #define SERIAL_DELAY 124 -#define SERIAL_TIMEOUT 1200 /* > two bytes */ +#define SERIAL_TIMEOUT 800 /* > more than two bytes */ #else /* settings for Arduino UNO with 16 MHz */ #define STATUS_LED LED_BUILTIN #define RESET_PIN 6 #define SERIAL_DATA 7 #define SERIAL_DELAY 410 -#define SERIAL_TIMEOUT 2500 /* > two bytes */ +#define SERIAL_TIMEOUT 1600 /* > more than two bytes */ #endif /* to set fused for ATTINY85: * avrdude -c usbasp-clone -p t85 -U lfuse:w:0xc0:m -U hfuse:w:0xdf:m -U efuse:w:0xff:m |