-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicroprocessor_decoder.c
More file actions
416 lines (340 loc) · 12.8 KB
/
microprocessor_decoder.c
File metadata and controls
416 lines (340 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include <MKL25Z4.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#define BUZZER_PIN 0
#define MIC_PIN_ADC 9
#define TRIG_PIN 1
#define ECHO_PIN 2
#define BUTTON_PIN 3
#define TSI_CH_MAIN 10
#define TSI_THRESHOLD 50
#define PASSWORD "1234"
#define LCD_RS_PIN 2
#define LCD_RW_PIN 4
#define LCD_EN_PIN 5
#define UART_BDL_VAL 68
#define PI 3.14159265f
#define SAMPLING_RATE 4000
#define GOERTZEL_THRESHOLD 16000.0f
#define MAX_MSG_LEN 4
const int fib_seq[] = {1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233};
#define FIB_LEN 12
#define BIT_TONE_MS 100
#define BIT_GAP_MS 50
#define BIT_SLOT_MS 150
#define BIT_SAMPLES 390
#define BIT_PRE_MS 25
#define GOERTZEL_MS ((BIT_SAMPLES * 1000) / SAMPLING_RATE)
#define BIT_RATIO 1.4f
#define BIT_MIN_MAG 1000.0f
volatile char secret_message[MAX_MSG_LEN + 1];
volatile char rx_buffer[32];
volatile int rx_index = 0;
volatile int password_received = 0;
int tsi_baseline = 0;
void delay_us(uint32_t us) { for (volatile uint32_t i = 0; i < us * 4; i++); }
void delay_ms(uint32_t ms) { for (uint32_t i = 0; i < ms; i++) delay_us(1000); }
void IntToString(int n, char *buffer) {
int i = 0;
if (n == 0) { buffer[i++] = '0'; buffer[i] = '\0'; return; }
int temp_n = n;
while (temp_n > 0) { buffer[i++] = (temp_n % 10) + '0'; temp_n /= 10; }
buffer[i] = '\0';
for (int j = 0; j < i / 2; j++) {
char temp = buffer[j]; buffer[j] = buffer[i - j - 1]; buffer[i - j - 1] = temp;
}
}
void UART1_Init(void) {
SIM->SCGC4 |= SIM_SCGC4_UART1_MASK;
SIM->SCGC5 |= SIM_SCGC5_PORTE_MASK;
PORTE->PCR[0] = PORT_PCR_MUX(3);
PORTE->PCR[1] = PORT_PCR_MUX(3);
UART1->BDH = 0; UART1->BDL = UART_BDL_VAL;
UART1->C2 = UART_C2_TE_MASK | UART_C2_RE_MASK | UART_C2_RIE_MASK;
NVIC_EnableIRQ(UART1_IRQn);
}
void UART_SendStr(char *str) {
while(*str) {
while(!(UART1->S1 & UART_S1_TDRE_MASK));
UART1->D = *str++;
}
}
void UART1_IRQHandler(void) {
if (UART1->S1 & UART_S1_RDRF_MASK) {
char c = UART1->D;
while(!(UART1->S1 & UART_S1_TDRE_MASK)); UART1->D = c;
if (c == '#' || c == '\r') {
rx_buffer[rx_index] = '\0';
password_received = 1;
rx_index = 0;
while(!(UART1->S1 & UART_S1_TDRE_MASK)); UART1->D = '\r';
while(!(UART1->S1 & UART_S1_TDRE_MASK)); UART1->D = '\n';
} else if (c != '\n') {
if (rx_index < 30) rx_buffer[rx_index++] = c;
else rx_index = 0;
}
}
}
void LCD_Pulse(void) { PTA->PSOR = (1 << LCD_EN_PIN); delay_us(10); PTA->PCOR = (1 << LCD_EN_PIN); delay_us(10); }
void LCD_Cmd(uint8_t cmd) { PTA->PCOR = (1 << LCD_RS_PIN) | (1 << LCD_RW_PIN); PTD->PDOR = cmd; LCD_Pulse(); delay_ms(2); }
void LCD_Data(uint8_t data) { PTA->PSOR = (1 << LCD_RS_PIN); PTA->PCOR = (1 << LCD_RW_PIN); PTD->PDOR = data; LCD_Pulse(); delay_us(50); }
void LCD_Init(void) {
SIM->SCGC5 |= SIM_SCGC5_PORTD_MASK | SIM_SCGC5_PORTA_MASK;
for(int i=0; i<8; i++) PORTD->PCR[i] = PORT_PCR_MUX(1);
PTD->PDDR = 0xFF;
PORTA->PCR[LCD_RS_PIN]=PORT_PCR_MUX(1); PORTA->PCR[LCD_RW_PIN]=PORT_PCR_MUX(1); PORTA->PCR[LCD_EN_PIN]=PORT_PCR_MUX(1);
PTA->PDDR |= (1<<LCD_RS_PIN) | (1<<LCD_RW_PIN) | (1<<LCD_EN_PIN);
delay_ms(50); LCD_Cmd(0x38); LCD_Cmd(0x0C); LCD_Cmd(0x01); delay_ms(2);
}
void LCD_Print(char *str) { while(*str) LCD_Data(*str++); }
void LCD_Clear(void) { LCD_Cmd(0x01); delay_ms(2); }
void LCD_SetCursor(uint8_t row, uint8_t col) { LCD_Cmd((row == 0 ? 0x80 : 0xC0) + col); }
void ADC_Init(void) {
SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK; SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK;
PORTB->PCR[1] = 0;
ADC0->CFG1 = ADC_CFG1_ADICLK(0) | ADC_CFG1_MODE(3) | ADC_CFG1_ADIV(1);
ADC0->CFG2 = ADC_CFG2_ADLSTS(3); ADC0->SC2 = 0; ADC0->SC3 = 0;
}
int ADC_Read(void) {
ADC0->SC1[0] = ADC_SC1_ADCH(MIC_PIN_ADC);
while(!(ADC0->SC1[0] & ADC_SC1_COCO_MASK));
return (int)ADC0->R[0];
}
void Play_Tone(int freq_hz, int duration_ms) {
if (freq_hz < 50) freq_hz = 50;
uint32_t period_us = 1000000 / freq_hz;
uint32_t cycles = (duration_ms * 1000) / period_us;
for (uint32_t i = 0; i < cycles; i++) {
PTB->PSOR = (1 << BUZZER_PIN); delay_us(period_us/2);
PTB->PCOR = (1 << BUZZER_PIN); delay_us(period_us/2);
}
}
void TSI_Init(void) {
SIM->SCGC5 |= SIM_SCGC5_TSI_MASK;
TSI0->GENCS = TSI_GENCS_MODE(0)|TSI_GENCS_REFCHRG(4)|TSI_GENCS_DVOLT(0)|TSI_GENCS_EXTCHRG(7)|TSI_GENCS_PS(4)|TSI_GENCS_NSCN(15)|TSI_GENCS_TSIEN_MASK;
}
int TSI_Read(void) {
TSI0->GENCS |= TSI_GENCS_EOSF_MASK; TSI0->DATA = TSI_DATA_TSICH(TSI_CH_MAIN) | TSI_DATA_SWTS_MASK;
while(!(TSI0->GENCS & TSI_GENCS_EOSF_MASK));
return (TSI0->DATA & TSI_DATA_TSICNT_MASK);
}
float Measure_Distance(void) {
uint32_t duration = 0, timeout = 0;
PTC->PCOR = (1 << TRIG_PIN); delay_us(2);
PTC->PSOR = (1 << TRIG_PIN); delay_us(10);
PTC->PCOR = (1 << TRIG_PIN);
while (!(PTC->PDIR & (1 << ECHO_PIN))) if (++timeout > 30000) return 999.0f;
while (PTC->PDIR & (1 << ECHO_PIN)) { duration++; delay_us(1); if (duration > 25000) return 999.0f; }
return (float)duration * 0.055f;
}
float Goertzel_Magnitude(int target_freq, int num_samples) {
float k = (int)(0.5f + ((float)num_samples * target_freq) / SAMPLING_RATE);
float omega = (2.0f * PI * k) / num_samples;
float coeff = 2.0f * cosf(omega);
float Q0 = 0, Q1 = 0, Q2 = 0;
for (int i = 0; i < num_samples; i++) {
int s = ADC_Read();
if (s < 0) return 0.0f;
s -= 39000;
Q0 = coeff * Q1 - Q2 + (float)s;
Q2 = Q1; Q1 = Q0;
delay_us(125);
}
return sqrtf(Q1*Q1 + Q2*Q2 - Q1*Q2*coeff);
}
int Execute_Security_Game(void) {
// --- PHASE 1: PASSWORD ---
LCD_Clear(); LCD_Print("IDENTITY CHECK");
UART_SendStr("AUTH REQUIRED: SEND PASS\r\n");
password_received = 0;
rx_index = 0;
memset((char*)rx_buffer, 0, 32);
while(1) {
if (password_received) {
password_received = 0;
if (strncmp((char*)rx_buffer, PASSWORD, 4) == 0) {
LCD_Clear(); LCD_Print("IDENTITY: OK");
delay_ms(1000);
break;
} else {
LCD_SetCursor(1,0); LCD_Print("INVALID KEY! ");
UART_SendStr("ACCESS DENIED. RETRY.\r\n");
delay_ms(1000); LCD_SetCursor(1,0); LCD_Print(" ");
}
rx_index = 0;
}
}
LCD_Clear(); LCD_Print("SENSOR ALIGN...");
delay_ms(1000);
int current_stage = 0;
int number_of = 30;
int countdown = 30;
while (current_stage < 3) {
countdown = 30;
number_of = 30;
while (countdown > 0) {
float d = Measure_Distance();
LCD_SetCursor(0, 0);
if(current_stage == 0) LCD_Print("ALIGN: MID-RANGE");
else if(current_stage == 1) LCD_Print("ALIGN: CLOSE-RNG");
else if(current_stage == 2) LCD_Print("ALIGN: LONG-RNG ");
countdown--;
if (current_stage == 0 && d >= 5 && d < 10) { number_of--; }
else if (current_stage == 1 && d >= 0 && d < 5) { number_of--; }
else if (current_stage == 2 && d >= 10 && d < 30) { number_of--; }
char timer_str[16];
sprintf(timer_str, "T:%d z:%d ", countdown, number_of);
LCD_SetCursor(1, 0);
LCD_Print(timer_str);
delay_ms(25);
}
LCD_SetCursor(1, 0);
LCD_Print("CALCULATING... ");
delay_ms(250);
int passed = 0;
if (number_of <= 25) passed = 1;
if (passed) {
LCD_Clear(); LCD_Print("STAGE COMPLETE");
delay_ms(1000);
current_stage++;
} else {
LCD_SetCursor(1, 0);
LCD_Print("ALIGN FAILED! ");
delay_ms(1000);
}
}
LCD_Clear(); LCD_Print("ACCESS GRANTED");
delay_ms(1000);
return 1;
}
void Mode_Decode(void) {
LCD_Clear(); LCD_Print("DECODE MODE");
LCD_SetCursor(1,0); LCD_Print("SIGNAL SEARCH...");
char temp_msg[MAX_MSG_LEN + 1];
memset(temp_msg, 0, sizeof(temp_msg));
int sync_high_count = 0;
while(1) {
if (Goertzel_Magnitude(1000, 200) > GOERTZEL_THRESHOLD) {
sync_high_count++;
if(sync_high_count > 5) {
LCD_Clear(); LCD_Print("SIGNAL LOCK!");
break;
}
} else { sync_high_count = 0; }
}
// Wait for Falling Edge
while(Goertzel_Magnitude(1000, 200) > (GOERTZEL_THRESHOLD / 1.75));
LCD_SetCursor(1,0); LCD_Print("RECEIVING... ");
delay_ms(500);
int fib_idx = 0;
int char_idx = 0;
while (char_idx < MAX_MSG_LEN) {
char current_char = 0;
for (int b = 7; b >= 0; b--) {
if (BIT_PRE_MS > 0) delay_ms(BIT_PRE_MS);
int f0 = 400 + fib_seq[fib_idx] * 10;
int f1 = 1200 + fib_seq[fib_idx] * 10;
float m0 = Goertzel_Magnitude(f0, BIT_SAMPLES);
float m1 = Goertzel_Magnitude(f1, BIT_SAMPLES);
if (m1 > BIT_MIN_MAG && m1 > (m0 * BIT_RATIO)) {
current_char |= (1 << b);
}
fib_idx = (fib_idx + 1) % FIB_LEN;
int used = BIT_PRE_MS + GOERTZEL_MS;
int remaining = BIT_SLOT_MS - used;
if (remaining > 0) delay_ms(remaining);
}
if (current_char == 0) break;
temp_msg[char_idx++] = current_char;
temp_msg[char_idx] = '\0';
}
LCD_Clear();
LCD_Print("DATA CAPTURED.");
LCD_SetCursor(1,0);
LCD_Print("DECRYPTING...");
delay_ms(2000);
if (Execute_Security_Game()) {
LCD_Clear();
LCD_Print("SECURE MSG: ");
LCD_SetCursor(1,0);
LCD_Print(temp_msg);
// Wait user exit
while(1) {
if ((TSI_Read() - tsi_baseline) > TSI_THRESHOLD) break;
delay_ms(100);
}
}
}
void Mode_Encode(void) {
LCD_Clear(); LCD_Print("ENCODE MODE");
delay_ms(1000);
if (!Execute_Security_Game()) return;
LCD_Clear(); LCD_Print("ENTER 4 CHARS:");
UART_SendStr("ENTER MSG (4 CHARS + #/ENTER):\r\n");
password_received = 0;
rx_index = 0;
memset((char*)rx_buffer, 0, 32);
while (!password_received);
strncpy((char*)secret_message, (char*)rx_buffer, MAX_MSG_LEN);
secret_message[MAX_MSG_LEN] = '\0';
LCD_Clear();
LCD_Print("MSG READY.");
LCD_SetCursor(1,0);
LCD_Print("TOUCH TO SEND>");
LCD_Clear(); LCD_Print("TRANSMITTING...");
LCD_SetCursor(1,0); LCD_Print("Tx: 1000 Hz (S)");
Play_Tone(1000, 2000);
LCD_SetCursor(1,0); LCD_Print("Tx: WAIT... ");
delay_ms(500);
int fib_idx = 0;
int len = strlen((char*)secret_message);
char num_buf[10];
for (int i = 0; i < len; i++) {
char c = secret_message[i];
for (int j = 7; j >= 0; j--) {
int bit = (c >> j) & 1;
int freq = (bit == 0) ? (400 + fib_seq[fib_idx]*10) : (1200 + fib_seq[fib_idx]*10);
IntToString(freq, num_buf);
LCD_SetCursor(1,0);
LCD_Print("Tx: ");
LCD_Print(num_buf);
LCD_Print(" Hz ");
Play_Tone(freq, BIT_TONE_MS);
delay_ms(BIT_GAP_MS);
fib_idx = (fib_idx + 1) % FIB_LEN;
}
delay_ms(200);
}
LCD_Clear(); LCD_Print("SENT.");
delay_ms(1000);
}
int main(void) {
SIM->COPC = 0;
SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTC_MASK;
PORTB->PCR[BUZZER_PIN] = PORT_PCR_MUX(1); PTB->PDDR |= (1 << BUZZER_PIN);
PORTC->PCR[TRIG_PIN] = PORT_PCR_MUX(1); PTC->PDDR |= (1 << TRIG_PIN);
PORTC->PCR[ECHO_PIN] = PORT_PCR_MUX(1); PTC->PDDR &= ~(1 << ECHO_PIN);
UART1_Init();
TSI_Init();
LCD_Init();
ADC_Init();
tsi_baseline = TSI_Read();
while(1) {
LCD_Clear();
LCD_Print("TOUCH: DECODE");
LCD_SetCursor(1,0);
LCD_Print("WAIT: ENCODE");
int mode = 1; // Default Encode
for (int i = 0; i < 30; i++) {
if ((TSI_Read() - tsi_baseline) > TSI_THRESHOLD) {
mode = 2; // Decode
while((TSI_Read() - tsi_baseline) > TSI_THRESHOLD); // Wait release
break;
}
delay_ms(100);
}
if (mode == 2) Mode_Decode();
else Mode_Encode();
}
}