diff --git a/Firmware/Chameleon-Mini/Codec/Codec.h b/Firmware/Chameleon-Mini/Codec/Codec.h index d985e42..84bf9c3 100644 --- a/Firmware/Chameleon-Mini/Codec/Codec.h +++ b/Firmware/Chameleon-Mini/Codec/Codec.h @@ -8,26 +8,6 @@ #ifndef CODEC_H_ #define CODEC_H_ -#include -#include -#include -#include "../Common.h" -#include "../Configuration.h" -#include "../Settings.h" - -#include "ISO14443-2A.h" -#include "Reader14443-2A.h" -#include "SniffISO14443-2A.h" -#include "ISO15693.h" - -/* Timing definitions for ISO14443A */ -#define ISO14443A_SUBCARRIER_DIVIDER 16 -#define ISO14443A_BIT_GRID_CYCLES 128 -#define ISO14443A_BIT_RATE_CYCLES 128 -#define ISO14443A_FRAME_DELAY_PREV1 1236 -#define ISO14443A_FRAME_DELAY_PREV0 1172 -#define ISO14443A_RX_PENDING_TIMEOUT 4 // ms - /* Peripheral definitions */ #define CODEC_DEMOD_POWER_PORT PORTB #define CODEC_DEMOD_POWER_MASK PIN0_bm @@ -87,6 +67,27 @@ #define CODEC_TIMER_TIMESTAMPS_CCA_VECT TCD1_CCA_vect #define CODEC_TIMER_TIMESTAMPS_CCB_VECT TCD1_CCB_vect +#ifndef __ASSEMBLER__ + +#include +#include +#include +#include "../Common.h" +#include "../Configuration.h" +#include "../Settings.h" + +#include "ISO14443-2A.h" +#include "Reader14443-2A.h" +#include "SniffISO14443-2A.h" +#include "ISO15693.h" + +/* Timing definitions for ISO14443A */ +#define ISO14443A_SUBCARRIER_DIVIDER 16 +#define ISO14443A_BIT_GRID_CYCLES 128 +#define ISO14443A_BIT_RATE_CYCLES 128 +#define ISO14443A_FRAME_DELAY_PREV1 1236 +#define ISO14443A_FRAME_DELAY_PREV0 1172 +#define ISO14443A_RX_PENDING_TIMEOUT 4 // ms #define CODEC_BUFFER_SIZE 256 @@ -285,4 +286,7 @@ bool CodecIsReaderToBeRestarted(void); void CodecThresholdSet(uint16_t th); uint16_t CodecThresholdIncrement(void); void CodecThresholdReset(void); + +#endif /* __ASSEMBLER__ */ + #endif /* CODEC_H_ */ diff --git a/Firmware/Chameleon-Mini/Codec/ISO14443-2A.c b/Firmware/Chameleon-Mini/Codec/ISO14443-2A.c index ddad5ce..507347e 100644 --- a/Firmware/Chameleon-Mini/Codec/ISO14443-2A.c +++ b/Firmware/Chameleon-Mini/Codec/ISO14443-2A.c @@ -79,13 +79,8 @@ static void StartDemod(void) { CODEC_DEMOD_IN_PORT.INT0MASK = CODEC_DEMOD_IN_MASK0; } -ISR(CODEC_DEMOD_IN_INT0_VECT) { - isr_func_CODEC_DEMOD_IN_INT0_VECT(); -} - -// ISR(CODEC_DEMOD_IN_INT0_VECT) // Find first pause and start sampling -void isr_ISO14443_2A_TCD0_CCC_vect(void) { +ISR_SHARED isr_ISO14443_2A_TCD0_CCC_vect(void) { /* This is the first edge of the first modulation-pause after StartDemod. * Now we have time to start * demodulating beginning from one bit-width after this edge. */ diff --git a/Firmware/Chameleon-Mini/Codec/ISO15693.c b/Firmware/Chameleon-Mini/Codec/ISO15693.c index 601be47..39ea805 100644 --- a/Firmware/Chameleon-Mini/Codec/ISO15693.c +++ b/Firmware/Chameleon-Mini/Codec/ISO15693.c @@ -89,7 +89,7 @@ static volatile uint16_t ReadCommandFromReader = 0; * and unregistered writing the INT0MASK to 0 */ // ISR(CODEC_DEMOD_IN_INT0_VECT) -void isr_ISO15693_CODEC_DEMOD_IN_INT0_VECT(void) { +ISR_SHARED isr_ISO15693_CODEC_DEMOD_IN_INT0_VECT(void) { /* Start sample timer CODEC_TIMER_SAMPLING (TCD0). * Set Counter Channel C (CCC) with relevant bitmask (TC0_CCCIF_bm), * the period for clock sampling is specified in StartISO15693Demod. @@ -146,8 +146,7 @@ INLINE void ISO15693_EOC(void) { * * It disables its own interrupt when receives an EOF (calling ISO15693_EOC) or when it receives garbage */ -// ISR(CODEC_TIMER_SAMPLING_CCC_VECT) // Reading data sent from the reader -void isr_ISO15693_CODEC_TIMER_SAMPLING_CCC_VECT(void) { +ISR_SHARED isr_ISO15693_CODEC_TIMER_SAMPLING_CCC_VECT(void) { /* Shift demod data */ SampleRegister = (SampleRegister << 1) | (!(CODEC_DEMOD_IN_PORT.IN & CODEC_DEMOD_IN_MASK) ? 0x01 : 0x00); diff --git a/Firmware/Chameleon-Mini/Codec/Reader14443-2A.c b/Firmware/Chameleon-Mini/Codec/Reader14443-2A.c index cc4180e..dc898ac 100644 --- a/Firmware/Chameleon-Mini/Codec/Reader14443-2A.c +++ b/Firmware/Chameleon-Mini/Codec/Reader14443-2A.c @@ -170,13 +170,10 @@ INLINE void BufferToSequence(void) { if (BitCount % 8) CodecBuffer[BitCount / 8] = SampleRegister >> (8 - (BitCount % 8)); } -// Frame Delay Time PCD to PICC ends -ISR(CODEC_TIMER_SAMPLING_CCC_VECT) { - isr_func_TCD0_CCC_vect(); -} // ISR (TCD0_CCC_vect) -void isr_Reader14443_2A_TCD0_CCC_vect(void) { +// Frame Delay Time PCD to PICC ends +ISR_SHARED isr_Reader14443_2A_TCD0_CCC_vect(void) { CODEC_TIMER_SAMPLING.INTFLAGS = TC0_CCCIF_bm; CODEC_TIMER_SAMPLING.INTCTRLB = TC_CCCINTLVL_OFF_gc; diff --git a/Firmware/Chameleon-Mini/Common.h b/Firmware/Chameleon-Mini/Common.h index 0456329..13af2a7 100644 --- a/Firmware/Chameleon-Mini/Common.h +++ b/Firmware/Chameleon-Mini/Common.h @@ -17,6 +17,9 @@ #define ODD_PARITY(Value) OddParityBit(Value)//(parity_even_bit(Value) ? 0 : 1) +#define ISR_SHARED \ + void __attribute__((signal)) // This function type has to be used for all the interrupt handlers that have to be changed at runtime + #define INLINE \ static inline __attribute__((always_inline)) diff --git a/Firmware/Chameleon-Mini/ISRSharing.S b/Firmware/Chameleon-Mini/ISRSharing.S new file mode 100644 index 0000000..1496cea --- /dev/null +++ b/Firmware/Chameleon-Mini/ISRSharing.S @@ -0,0 +1,34 @@ +; This file allows to share ISR at runtime with a low overhead. +; Functions that have to be shared MUST be defined as ISR_SHARED types +; (Defined in Common.h) to instruct GCC to compile them preserving the stack +; +; Created on: 2019-12-06 +; Author: ceres-c +; + +#include "Codec/Codec.h" +#include + +; Find first pause and start sampling +.global CODEC_DEMOD_IN_INT0_VECT +CODEC_DEMOD_IN_INT0_VECT: + push r30 + push r31 + lds r30, isr_func_CODEC_DEMOD_IN_INT0_VECT + lds r31, isr_func_CODEC_DEMOD_IN_INT0_VECT + 1 + icall + pop r31 + pop r30 + reti + +; Frame Delay Time PCD to PICC ends +.global CODEC_TIMER_SAMPLING_CCC_VECT +CODEC_TIMER_SAMPLING_CCC_VECT: + push r30 + push r31 + lds r30, isr_func_TCD0_CCC_vect + lds r31, isr_func_TCD0_CCC_vect + 1 + icall + pop r31 + pop r30 + reti diff --git a/Firmware/Chameleon-Mini/Makefile b/Firmware/Chameleon-Mini/Makefile index 257115f..874326e 100644 --- a/Firmware/Chameleon-Mini/Makefile +++ b/Firmware/Chameleon-Mini/Makefile @@ -97,7 +97,7 @@ F_CPU = 27120000 F_USB = 48000000 TARGET = Chameleon-Mini OPTIMIZATION = s -SRC += $(TARGET).c LUFADescriptors.c System.c Configuration.c Random.c Common.c Memory.c MemoryAsm.S Button.c Log.c Settings.c LED.c Map.c AntennaLevel.c +SRC += $(TARGET).c LUFADescriptors.c System.c ISRSharing.S Configuration.c Random.c Common.c Memory.c MemoryAsm.S Button.c Log.c Settings.c LED.c Map.c AntennaLevel.c SRC += Terminal/Terminal.c Terminal/Commands.c Terminal/XModem.c Terminal/CommandLine.c SRC += Codec/Codec.c Codec/ISO14443-2A.c Codec/Reader14443-2A.c Codec/SniffISO14443-2A.c Codec/Reader14443-ISR.S SRC += Application/MifareUltralight.c Application/MifareClassic.c Application/ISO14443-3A.c Application/Crypto1.c Application/Reader14443A.c Application/Sniff14443A.c Application/CryptoTDEA.S diff --git a/Firmware/Chameleon-Mini/System.c b/Firmware/Chameleon-Mini/System.c index 33f7e8c..330c8d9 100644 --- a/Firmware/Chameleon-Mini/System.c +++ b/Firmware/Chameleon-Mini/System.c @@ -140,4 +140,3 @@ void SystemInterruptInit(void) { PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm; sei(); } -