-
Notifications
You must be signed in to change notification settings - Fork 3
/
SigmaESP32.ino
224 lines (193 loc) · 6.48 KB
/
SigmaESP32.ino
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
// By R.Hasaranga
// http://www.hasaranga.com
// Code may not clean, but it works :-)
#include <WiFi.h>
#include <Wire.h>
#include "DSPWriter.h"
const char* ssid = "your router ssid";
const char* password = "your router password";
//Static IP address configuration
IPAddress staticIP(192, 168, 8, X); //static ip address (change according to your router config)
IPAddress gateway(192, 168, 8, 1); //IP sddress of WiFi router (Gateway)
IPAddress subnet(255, 255, 255, 0); //Subnet mask
IPAddress dns(8, 8, 8, 8); //DNS
WiFiServer tcpServer(8086);
unsigned char dataBuffer[50*1024];
#define STATE_START 0
#define STATE_READ_CMD 1
#define STATE_WRITE_CMD 2
#define CMD_WRITE 0x09
#define CMD_READ 0x0a
struct adauWriteHeader {
uint8_t command;
uint8_t safeload;
uint8_t placement;
uint16_t totalLen;
uint8_t chipAddr;
uint16_t dataLen;
uint16_t address;
};
struct adauReadHeader {
uint8_t command;
uint16_t totalLen;
uint8_t chipAddr;
uint16_t dataLen;
uint16_t address;
};
adauWriteHeader writeHeader;
adauReadHeader readHeader;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.config(staticIP, gateway, subnet, dns);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
Serial.println("WiFi connected.");
Wire.begin();
tcpServer.begin();
}
void loop() {
WiFiClient client = tcpServer.available(); // listen for incoming clients
if (client) {
Serial.println("new connection");
int writeIndex = 0;
int readIndex = 0;
int receivedByteCount = 0;
int currentState = STATE_START;
bool flashingDetected = false;
int lastRegSize;
while (client.connected())
{
digitalWrite(LED_BUILTIN, HIGH);
while (client.available())
{
dataBuffer[writeIndex] = client.read();
writeIndex++;
receivedByteCount++;
}
if(currentState == STATE_START)
{
if(receivedByteCount > readIndex)
{
if(dataBuffer[readIndex] == CMD_READ)
{
currentState = STATE_READ_CMD;
}
else if(dataBuffer[readIndex] == CMD_WRITE)
{
currentState = STATE_WRITE_CMD;
}
else
{
Serial.println("invalid operation");
break;
}
}
else
{
currentState = STATE_START;
if(writeIndex!=0)
Serial.println("data processed");
writeIndex = 0;
readIndex = 0;
receivedByteCount = 0;
flashingDetected = false;
}
}
else if(currentState == STATE_WRITE_CMD)
{
if(receivedByteCount >= (readIndex+sizeof(adauWriteHeader)))
{
writeHeader.safeload = dataBuffer[readIndex+1];
writeHeader.placement = dataBuffer[readIndex+2];
writeHeader.totalLen = (dataBuffer[readIndex+3]<<8) | dataBuffer[readIndex+4];
writeHeader.chipAddr = dataBuffer[readIndex+5];
writeHeader.dataLen = (dataBuffer[readIndex+6]<<8) | dataBuffer[readIndex+7];
writeHeader.address = (dataBuffer[readIndex+8]<<8) | dataBuffer[readIndex+9];
readIndex += 10;
digitalWrite(LED_BUILTIN, LOW);
uint8_t registerSize = writeHeader.dataLen;
uint16_t regAddress = writeHeader.address;
if((writeHeader.address == dspRegister::CoreRegister) & (writeHeader.dataLen == 2))
{
registerSize = CORE_REGISTER_R0_REGSIZE; // CORE_REGISTER_R0_REGSIZE or CORE_REGISTER_R4_REGSIZE
Serial.println("CORE_REGISTER_R0_REGSIZE");
}
else if((writeHeader.address == dspRegister::CoreRegister) & (writeHeader.dataLen == 24))
{
registerSize = HARDWARE_CONF_REGSIZE;
Serial.println("HARDWARE_CONF_REGSIZE");
}
else if(writeHeader.address == 0x0400)
{
registerSize = PROGRAM_REGSIZE;
flashingDetected = true;
Serial.println("PROGRAM_REGSIZE");
}
else if(writeHeader.address == 0)
{
registerSize = PARAMETER_REGSIZE;
Serial.println("PARAMETER_REGSIZE");
}
else
{
if(flashingDetected){
registerSize = lastRegSize;
Serial.println("UNKNOWN REGSIZE: using last reg size");
}
}
if(writeHeader.safeload == 1)
{
Serial.print("safeload data size: ");
Serial.println(writeHeader.dataLen);
int writeCount = writeHeader.dataLen/4;
uint8_t dataArray[5];
int safeLoadReadIndex = readIndex;
DSPWriter dspWriter;
while(writeCount)
{
dataArray[0] = 0;
dataArray[1] = dataBuffer[safeLoadReadIndex];
dataArray[2] = dataBuffer[safeLoadReadIndex+1];
dataArray[3] = dataBuffer[safeLoadReadIndex+2];
dataArray[4] = dataBuffer[safeLoadReadIndex+3];
dspWriter.safeload_writeRegister(regAddress, dataArray, writeCount==1);
regAddress++;
safeLoadReadIndex+=4;
--writeCount;
}
}
else
{
DSPWriter::writeRegisterBlock(regAddress, writeHeader.dataLen, &dataBuffer[readIndex], registerSize);
}
lastRegSize = registerSize;
digitalWrite(LED_BUILTIN, HIGH);
readIndex += writeHeader.dataLen;
currentState = STATE_START;
}
}
else if(currentState == STATE_READ_CMD)
{
if(receivedByteCount >= (readIndex+sizeof(adauReadHeader)))
{
readHeader.totalLen = (dataBuffer[readIndex+1]<<8) | dataBuffer[readIndex+2];
readHeader.chipAddr = dataBuffer[3];
readHeader.dataLen = (dataBuffer[4]<<8) | dataBuffer[5];
readHeader.address = (dataBuffer[6]<<8) | dataBuffer[7];
readIndex += 8;
Serial.println("============== read request ============");
}
}
}
client.stop();
Serial.println("disconnected");
digitalWrite(LED_BUILTIN, LOW);
}
}