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

Workmem, CARD_FUNCTION, BRUTE, LOG and cleanup #171

Merged
34 changes: 24 additions & 10 deletions Firmware/ChameleonMini/AntennaLevel.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
#include "AntennaLevel.h"
#include "Application/Application.h"

#define FIELD_MIN_RSSI 500
void AntennaLevelInit(void) {
ADCA.CTRLA = ADC_ENABLE_bm;
ADCA.CTRLB = ADC_RESOLUTION_12BIT_gc;
ADCA.REFCTRL = ADC_REFSEL_INT1V_gc | ADC_BANDGAP_bm;
ADCA.PRESCALER = ADC_PRESCALER_DIV32_gc;
ADCA.CH0.CTRL = ADC_CH_INPUTMODE_SINGLEENDED_gc;
ADCA.CH0.MUXCTRL = ADC_CH_MUXPOS_PIN7_gc;
}

uint16_t AntennaLevelGet(void) {
ADCA.CH0.CTRL |= ADC_CH_START_bm;
while( !(ADCA.CH0.INTFLAGS & ADC_CH_CHIF_bm) );

ADCA.CH0.INTFLAGS = ADC_CH_CHIF_bm;

int16_t Result = ADCA.CH0RES - ANTENNA_LEVEL_OFFSET;
if (Result < 0) Result = 0;

return (uint16_t) (((uint32_t) Result * ANTENNA_LEVEL_NUMERATOR) / ANTENNA_LEVEL_DENOMINATOR);
}

void AntennaLevelTick(void)
{
void AntennaLevelTick(void) {
uint16_t rssi = AntennaLevelGet();

if (rssi < FIELD_MIN_RSSI)
{
//LEDHook(LED_FIELD_DETECTED, LED_OFF);
if (ActiveConfiguration.UidSize != 0) // this implies that we are emulating right now
ApplicationReset(); // reset the application just like a real card gets reset when there is no field
} else {
//LEDHook(LED_FIELD_DETECTED, LED_ON);
// UidSize != 0 implies that we are emulating right now
if( (rssi < ANTENNA_FIELD_MIN_RSSI) && (ActiveConfiguration.UidSize != 0) ) {
ApplicationReset(); // reset the application just like a real card gets reset when there is no field
}
}
46 changes: 14 additions & 32 deletions Firmware/ChameleonMini/AntennaLevel.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,40 +58,22 @@

#include "Common.h"

#define ANTENNA_LEVEL_R1 10E3
#define ANTENNA_LEVEL_R2 220E0
#define ANTENNA_LEVEL_VREF 1.0
#define ANTENNA_LEVEL_RES 4096
#define ANTENNA_LEVEL_OFFSET 190 /* LSB */
#define ANTENNA_LEVEL_R1 10E3
#define ANTENNA_LEVEL_R2 220E0
#define ANTENNA_LEVEL_VREF 1.0
#define ANTENNA_LEVEL_RES 4096
#define ANTENNA_LEVEL_OFFSET 190 /* LSB */

#define ANTENNA_LEVEL_MILLIVOLT 1E3
#define ANTENNA_LEVEL_FACTOR (ANTENNA_LEVEL_VREF * (ANTENNA_LEVEL_R1 + ANTENNA_LEVEL_R2) / (ANTENNA_LEVEL_RES * ANTENNA_LEVEL_R2) )
#define ANTENNA_LEVEL_SCALE ((uint32_t) 1<<16)
#define ANTENNA_LEVEL_NUMERATOR ((uint32_t) (ANTENNA_LEVEL_MILLIVOLT * ANTENNA_LEVEL_FACTOR * ANTENNA_LEVEL_SCALE + .5))
#define ANTENNA_LEVEL_DENOMINATOR (ANTENNA_LEVEL_SCALE)
#define ANTENNA_LEVEL_MILLIVOLT 1E3
#define ANTENNA_LEVEL_FACTOR (ANTENNA_LEVEL_VREF * (ANTENNA_LEVEL_R1 + ANTENNA_LEVEL_R2) / (ANTENNA_LEVEL_RES * ANTENNA_LEVEL_R2) )
#define ANTENNA_LEVEL_SCALE ((uint32_t) 1<<16)
#define ANTENNA_LEVEL_NUMERATOR ((uint32_t) (ANTENNA_LEVEL_MILLIVOLT * ANTENNA_LEVEL_FACTOR * ANTENNA_LEVEL_SCALE + .5))
#define ANTENNA_LEVEL_DENOMINATOR (ANTENNA_LEVEL_SCALE)

static inline void AntennaLevelInit(void)
{
ADCA.CTRLA = ADC_ENABLE_bm;
ADCA.CTRLB = ADC_RESOLUTION_12BIT_gc;
ADCA.REFCTRL = ADC_REFSEL_INT1V_gc | ADC_BANDGAP_bm;
ADCA.PRESCALER = ADC_PRESCALER_DIV32_gc;
ADCA.CH0.CTRL = ADC_CH_INPUTMODE_SINGLEENDED_gc;
ADCA.CH0.MUXCTRL = ADC_CH_MUXPOS_PIN7_gc;
#define ANTENNA_FIELD_MIN_RSSI 500

}

static inline uint16_t AntennaLevelGet(void)
{
ADCA.CH0.CTRL |= ADC_CH_START_bm;
while( !(ADCA.CH0.INTFLAGS & ADC_CH_CHIF_bm) );

ADCA.CH0.INTFLAGS = ADC_CH_CHIF_bm;

int16_t Result = ADCA.CH0RES - ANTENNA_LEVEL_OFFSET;
if (Result < 0) Result = 0;

return (uint16_t) (((uint32_t) Result * ANTENNA_LEVEL_NUMERATOR) / ANTENNA_LEVEL_DENOMINATOR);
}
void AntennaLevelInit(void);
uint16_t AntennaLevelGet(void);
void AntennaLevelTick(void);

#endif /* ANTENNALEVEL_H_ */
4 changes: 4 additions & 0 deletions Firmware/ChameleonMini/Application/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ INLINE void ApplicationTick(void) {
ActiveConfiguration.ApplicationTickFunc();
}

INLINE void ApplicationButton(void) {
ActiveConfiguration.ApplicationButtonFunc();
}

INLINE uint16_t ApplicationProcess(uint8_t* ByteBuffer, uint16_t ByteCount) {
return ActiveConfiguration.ApplicationProcessFunc(ByteBuffer, ByteCount);
}
Expand Down
Loading