Krolyxon
Commit a51e4c5

Branches

Tags

Refs →

orion-rffirmwarecc1101.cpp

150 lines · 2.9 KB · C++

1#include <Arduino.h>
2#include "ELECHOUSE_CC1101_SRC_DRV.h"
3#include "cc1101.h"
4#include "config.h"
5
6// ===== CONFIG =====
7#define RAW_BUF_MAX 512
8
9// ===== STATE =====
10static bool cc1101Inited = false;
11
12// ===== CAPTURE STATE =====
13volatile unsigned long captureBuffer[RAW_BUF_MAX];
14volatile int pulseIndex = 0;
15volatile unsigned long lastEdgeTime = 0;
16volatile bool capturing = false;
17
18// ===== RF SETTINGS =====
19float currentFreq = 433.92;
20float dataRate = 3.79372;
21float deviation = 0.0;
22float rxBW = 325.0;
23int powerLevel = 10;
24
25// ===== ISR =====
26void IRAM_ATTR pulseISR()
27{
28 unsigned long now = micros();
29
30 if (!capturing) return;
31 if (pulseIndex >= RAW_BUF_MAX) return;
32
33 unsigned long duration = now - lastEdgeTime;
34
35 if (duration < 50) return;
36
37 captureBuffer[pulseIndex++] = duration;
38 lastEdgeTime = now;
39}
40
41// ===== OOK SETUP =====
42void setupOOKMode()
43{
44 ELECHOUSE_cc1101.SetRx();
45 ELECHOUSE_cc1101.setMHZ(currentFreq);
46
47 ELECHOUSE_cc1101.setModulation(0); // ASK/OOK
48 ELECHOUSE_cc1101.setDRate(dataRate);
49 ELECHOUSE_cc1101.setDeviation(0);
50 ELECHOUSE_cc1101.setRxBW(rxBW);
51 ELECHOUSE_cc1101.setSyncMode(0);
52 ELECHOUSE_cc1101.setPA(powerLevel);
53}
54
55// ===== INIT (LAZY, SAFE) =====
56bool initCC1101()
57{
58 if (cc1101Inited)
59 return true;
60
61 Serial.println("Initializing CC1101...");
62
63 // IMPORTANT: no detachInterrupt here (causes crash if not installed)
64
65 ELECHOUSE_cc1101.setSpiPin(
66 cc1101_SCK,
67 cc1101_MISO,
68 cc1101_MOSI,
69 CC1101_CS
70 );
71
72 ELECHOUSE_cc1101.setGDO(CC1101_GDO0, CC1101_GDO2);
73
74 if (!ELECHOUSE_cc1101.getCC1101())
75 {
76 Serial.println("❌ CC1101 NOT FOUND");
77 return false;
78 }
79
80 delay(10); // let SPI settle
81
82 ELECHOUSE_cc1101.Init(); // THIS WAS YOUR FREEZE POINT
83 setupOOKMode();
84
85 pinMode(CC1101_GDO0, INPUT);
86
87 Serial.println("✅ CC1101 READY");
88
89 cc1101Inited = true;
90 return true;
91}
92
93// ===== CAPTURE CONTROL =====
94void startCapture()
95{
96 pulseIndex = 0;
97 capturing = true;
98 lastEdgeTime = micros();
99
100 attachInterrupt(
101 digitalPinToInterrupt(CC1101_GDO0),
102 pulseISR,
103 CHANGE
104 );
105
106 Serial.println("Capture started");
107}
108
109bool isCC1101Ready() {
110 return cc1101Inited;
111}
112
113void stopCapture()
114{
115 capturing = false;
116
117 detachInterrupt(digitalPinToInterrupt(CC1101_GDO0));
118
119 Serial.println("Capture stopped");
120}
121
122// ===== DEBUG PRINT =====
123void printCapture()
124{
125 Serial.println("Captured pulses:");
126
127 for (int i = 0; i < pulseIndex; i++)
128 {
129 Serial.println(captureBuffer[i]);
130 }
131}
132
133
134// ===== REPLAY =====
135void replaySignal()
136{
137 Serial.println("Replaying...");
138
139 ELECHOUSE_cc1101.SetTx();
140 pinMode(CC1101_GDO0, OUTPUT);
141
142 for (int i = 0; i < pulseIndex; i++)
143 {
144 digitalWrite(CC1101_GDO0, (i % 2 == 0) ? HIGH : LOW);
145 delayMicroseconds(captureBuffer[i]);
146 }
147
148 digitalWrite(CC1101_GDO0, LOW);
149 ELECHOUSE_cc1101.SetRx();
150}