Created
September 14, 2020 23:22
-
-
Save SamL98/c542282e930e105108425670144112d5 to your computer and use it in GitHub Desktop.
HCS361 Trigger+Read
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define SAMPLE_OFFSET 188 | |
#define NSAMPLES 68 | |
#define TRACES_TO_CAPTURE 200 | |
#define FOB_PIN 5 | |
#define BUTTON_PIN 8 | |
#define TRIGGER_PIN 9 | |
unsigned int timeDiffs[NSAMPLES]; // the diffs (us) between all the codeword pulses received | |
unsigned char bits[NSAMPLES - 1]; // timeDiffs converted into bits according to the VPWM modulation in the datasheet | |
unsigned long lastTime = 0, currTime; // the time of the last and current pulses respectively | |
unsigned char samplesTilRecord = SAMPLE_OFFSET, // number of pulses received until the codeword is being transmitted | |
sampleNo = 0; // the current number of pulses received | |
unsigned int traceNo = 0; // the current number codewords read | |
void setup() { | |
Serial.begin(115200); | |
pinMode(FOB_PIN, INPUT); | |
pinMode(BUTTON_PIN, OUTPUT); | |
pinMode(TRIGGER_PIN, OUTPUT); | |
digitalWrite(BUTTON_PIN, LOW); | |
digitalWrite(TRIGGER_PIN, LOW); | |
cli(); | |
PCICR |= 4; // turn on PC interrupts for port d | |
PCMSK2 = 1 << FOB_PIN; // enable PC interrupts for the fob pin | |
sei(); | |
while (!Serial); | |
delay(5000); | |
} | |
ISR(PCINT2_vect) { | |
// Early exit until the codeword is being transmitted | |
if (samplesTilRecord) { | |
samplesTilRecord -= 1; | |
return; | |
} | |
// Don't overwrite any samples | |
if (sampleNo == NSAMPLES) | |
return; | |
// Get the time diff | |
currTime = micros(); | |
timeDiffs[sampleNo++] = currTime - lastTime; | |
lastTime = currTime; | |
} | |
void cvt_to_bits() { | |
unsigned char i; | |
for (i=1; i<NSAMPLES; i++) | |
bits[i - 1] = (1 - ((i - 1) & 1)) ^ (abs(timeDiffs[i] - 200) / 200); | |
} | |
unsigned long decode_bits(unsigned char s, unsigned char e) { | |
unsigned long decoded = 0; | |
unsigned char i; | |
for (i=s; i<e; i++) | |
decoded |= ((unsigned long)bits[i]) << (i - s); | |
return decoded; | |
} | |
unsigned char calculate_crc() { | |
unsigned char i, crc0 = 0, crc1 = 0, tmp_crc0; | |
for (i=0; i<NSAMPLES-3; i++) { | |
tmp_crc0 = (crc0 ^ bits[i]) ^ crc1; | |
crc1 = crc0 ^ bits[i]; | |
crc0 = tmp_crc0; | |
} | |
return (crc1 << 1) | crc0; | |
} | |
void loop() { | |
// Halt once we've collected the desired number of traces | |
while (traceNo == TRACES_TO_CAPTURE); | |
traceNo += 1; | |
// Send a button press and trigger the oscilloscope | |
digitalWrite(BUTTON_PIN, HIGH); | |
digitalWrite(TRIGGER_PIN, HIGH); | |
digitalWrite(BUTTON_PIN, LOW); | |
// Wait until the transmission has completed | |
while (sampleNo < NSAMPLES) | |
delay(1); | |
// Decode the VPWM and print the ciphertext | |
cvt_to_bits(); | |
Serial.println(decode_bits(0, 32)); | |
// Reset the trigger and sample reader | |
digitalWrite(TRIGGER_PIN, LOW); | |
samplesTilRecord = SAMPLE_OFFSET; | |
sampleNo = 0; | |
// Guestimated HCS cooldown time | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment