Skip to content

Commit 5bdf0b3

Browse files
committed
Initial: add wifi library for ESP32-C3 module
1 parent 6b5d75e commit 5bdf0b3

File tree

4 files changed

+391
-0
lines changed

4 files changed

+391
-0
lines changed

libraries/WiFi/WiFiC3.h

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
SerialPassthroughESP32-C3 - Use esptool to flash the ES32-C3 module on Santiago and Portenta H33
3+
4+
Copyright (c) 2022 Arduino SA. All rights reserved.
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
unsigned long baud = 9600;
22+
23+
int rts = -1;
24+
int dtr = -1;
25+
26+
27+
#ifdef ARDUINO_PORTENTA_H33
28+
29+
#warning Compiling for Portenta H33
30+
#ifndef SerialNina
31+
#define SerialNina Serial5
32+
#endif
33+
34+
#ifndef NINA_GPIO0
35+
#define NINA_GPIO0 (100)
36+
#endif
37+
38+
#ifndef NINA_RESETN
39+
#define NINA_RESETN (101)
40+
#endif
41+
42+
#else
43+
44+
#warning Compiling for Santiago
45+
46+
#ifndef SerialNina
47+
#define SerialNina Serial2
48+
#endif
49+
50+
#ifndef NINA_GPIO0
51+
#define NINA_GPIO0 (28)
52+
#endif
53+
54+
#ifndef NINA_RESETN
55+
#define NINA_RESETN (29)
56+
#endif
57+
58+
#endif
59+
60+
void setup() {
61+
Serial.begin(baud, SERIAL_8N1);
62+
SerialNina.begin(baud);
63+
while (!Serial);
64+
65+
pinMode(NINA_GPIO0, OUTPUT);
66+
pinMode(NINA_RESETN, OUTPUT);
67+
68+
digitalWrite(NINA_GPIO0, HIGH);
69+
delay(100);
70+
digitalWrite(NINA_RESETN, HIGH);
71+
digitalWrite(NINA_RESETN, LOW);
72+
digitalWrite(NINA_RESETN, HIGH);
73+
74+
rts = Serial.rts();
75+
dtr = Serial.dtr();
76+
}
77+
78+
void loop() {
79+
80+
auto _rts = Serial.rts();
81+
auto _dtr = Serial.dtr();
82+
83+
if ((rts != _rts) || (dtr != _dtr)) {
84+
digitalWrite(NINA_RESETN, _rts ? LOW : HIGH);
85+
rts = _rts;
86+
digitalWrite(NINA_GPIO0, _dtr ? LOW : HIGH);
87+
dtr = _dtr;
88+
}
89+
90+
int len = 0;
91+
uint8_t auc_buffer[488];
92+
while (Serial.available() && len < sizeof(auc_buffer)) {
93+
auc_buffer[len++] = Serial.read();
94+
}
95+
if (len) {
96+
SerialNina.write(auc_buffer, len);
97+
}
98+
99+
len = 0;
100+
while (SerialNina.available() && len < sizeof(auc_buffer)) {
101+
auc_buffer[len++] = SerialNina.read();
102+
}
103+
if (len) {
104+
Serial.write(auc_buffer, len);
105+
}
106+
107+
// check if the USB virtual serial wants a new baud rate
108+
if (Serial.baud() != baud) {
109+
//rts = -1;
110+
//dtr = -1;
111+
112+
baud = Serial.baud();
113+
SerialNina.end();
114+
SerialNina.begin(baud);
115+
}
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
#include "Arduino.h"
2+
#include "SPI.h"
3+
4+
//#define
5+
6+
SPIClass& spi = SPI1;
7+
SPISettings spiSettings = SPISettings(4000000, MSBFIRST, SPI_MODE0);
8+
9+
#ifdef ARDUINO_PORTENTA_H33
10+
int cs = 103;
11+
byte hs = 100;
12+
#else
13+
int cs = 31;
14+
byte hs = 28;
15+
#endif
16+
17+
volatile bool readflag = false;
18+
19+
void writeSpi(void *data, int size) {
20+
21+
digitalWrite(cs, LOW);
22+
23+
delayMicroseconds(1);
24+
spi.beginTransaction(spiSettings);
25+
26+
spi.transfer(data, size);
27+
28+
//spi.transfer(data, size);
29+
spi.endTransaction();
30+
digitalWrite(cs, HIGH);
31+
}
32+
33+
uint32_t readSpi() {
34+
uint32_t read = 0x00;
35+
36+
digitalWrite(cs, LOW);
37+
38+
delayMicroseconds(1);
39+
40+
spi.beginTransaction(spiSettings);
41+
42+
43+
for (int i = 0; i < 4; i++) {
44+
read <<= 8;
45+
read |= spi.transfer(0);
46+
read = spi.transfer(0);
47+
}
48+
49+
spi.endTransaction();
50+
51+
digitalWrite(cs, HIGH);
52+
return read;
53+
}
54+
55+
void readSpi(uint8_t *data, int size, uint8_t *readBuffer) {
56+
uint8_t read[4096];
57+
58+
digitalWrite(cs, LOW);
59+
60+
delayMicroseconds(1);
61+
62+
spi.beginTransaction(spiSettings);
63+
64+
for (int i = 0; i < size; i++) {
65+
readBuffer[i] = spi.transfer(data[i]);
66+
}
67+
spi.endTransaction();
68+
69+
digitalWrite(cs, HIGH);
70+
}
71+
72+
void isrRead() {
73+
readflag = true;
74+
}
75+
76+
void spiBegin() {
77+
pinMode(cs, OUTPUT);
78+
pinMode(hs, INPUT);
79+
digitalWrite(cs, HIGH);
80+
spi.begin();
81+
attachInterrupt(hs, isrRead, RISING);
82+
}
83+
84+
void notifyWrite() {
85+
// Notify write
86+
uint8_t bufferw[7];
87+
bufferw[0] = 0x01;
88+
bufferw[1] = 0x00;
89+
bufferw[2] = 0x00;
90+
bufferw[3] = 0XFE;
91+
bufferw[4] = 0X10;
92+
bufferw[5] = 0x04;
93+
bufferw[6] = 0;
94+
writeSpi((void *)bufferw, sizeof(bufferw));
95+
}
96+
97+
98+
void queryTxStatus() {
99+
uint8_t buffers[7];
100+
buffers[0] = 0x02;
101+
buffers[1] = 0x04;
102+
buffers[2] = 0x00;
103+
buffers[3] = 0xFF;
104+
buffers[4] = 0xFF;
105+
buffers[5] = 0xFF;
106+
buffers[6] = 0xFF;
107+
writeSpi((void *)buffers, sizeof(buffers));
108+
}
109+
110+
void txSpiRequest(uint8_t * data, uint16_t * size) {
111+
// write data
112+
uint8_t bufferc[4096];
113+
bufferc[0] = 0x03;
114+
bufferc[1] = 0x00;
115+
bufferc[2] = 0x00;
116+
for (int i = 0; i < (*size) / 8; i++) {
117+
bufferc[i + 3] = (uint8_t) data[i];
118+
}
119+
writeSpi((void *)bufferc, (*size) / 8 + 3);
120+
}
121+
122+
void txDone() {
123+
// write done
124+
uint8_t bufferd[3];
125+
bufferd[0] = 0x07;
126+
bufferd[1] = 0x00;
127+
bufferd[2] = 0x00;
128+
writeSpi((void *)bufferd, sizeof(bufferd));
129+
}
130+
131+
void writeToSlave(uint8_t *data, uint16_t * size) {
132+
notifyWrite();
133+
delay(10);
134+
if (!readflag) {
135+
if (digitalRead(hs)) {
136+
Serial.println("no readflag but hs high");
137+
} else {
138+
Serial.println("no resp");
139+
return;
140+
}
141+
}
142+
readflag = false;
143+
144+
// query status
145+
queryTxStatus();
146+
147+
txSpiRequest(data, size);
148+
txDone();
149+
}
150+
151+
152+
153+
uint8_t queryRxStatus() {
154+
uint8_t buffers[7];
155+
uint8_t raedableByte = 0x00;
156+
buffers[0] = 0x02;
157+
buffers[1] = 0x04;
158+
buffers[2] = 0x00;
159+
buffers[3] = 0xFF;
160+
buffers[4] = 0xFF;
161+
buffers[5] = 0xFF;
162+
buffers[6] = 0xFF;
163+
164+
uint8_t readBuffer[4096];
165+
readSpi(buffers, sizeof(buffers), readBuffer);
166+
167+
raedableByte = readBuffer[5];
168+
return raedableByte;
169+
}
170+
171+
void sendRxSpiRequest(uint8_t *rxBuffer, uint8_t raedableByte) {
172+
// read data
173+
uint8_t bufferc[4096];
174+
bufferc[0] = 0x04;
175+
bufferc[1] = 0x00;
176+
bufferc[2] = 0x00;
177+
for (int i = 0; i < raedableByte; i++) {
178+
bufferc[3 + i] = 0xFF;
179+
}
180+
181+
readSpi(bufferc, 3 + raedableByte, rxBuffer);
182+
}
183+
184+
void rxDone() {
185+
// read done
186+
uint8_t bufferd[3];
187+
bufferd[0] = 0x08;
188+
bufferd[1] = 0x00;
189+
bufferd[2] = 0x00;
190+
writeSpi((void *)bufferd, sizeof(bufferd));
191+
}
192+
193+
uint8_t readFromSlave(uint8_t * rxBuffer) {
194+
if (!readflag) {
195+
return 0;
196+
}
197+
readflag = false;
198+
199+
200+
uint8_t raedableByte = queryRxStatus();
201+
uint8_t bufferc[4096];
202+
sendRxSpiRequest(rxBuffer, raedableByte);
203+
for (int i = 0; i < raedableByte; i++) {
204+
rxBuffer[i] = rxBuffer[3 + i];
205+
}
206+
rxDone();
207+
return raedableByte;
208+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "Arduino.h"
2+
#include "SPIProtocol.h"
3+
4+
uint16_t sizec = 0;
5+
uint8_t txBuffer[4096];
6+
uint8_t rxBuffer[4096];
7+
8+
//void isrRead() {
9+
// readflag = true;
10+
//}
11+
12+
#ifdef ARDUINO_PORTENTA_H33
13+
14+
#ifndef NINA_GPIO0
15+
#define NINA_GPIO0 (100)
16+
#endif
17+
18+
#ifndef NINA_RESETN
19+
#define NINA_RESETN (101)
20+
#endif
21+
22+
#else
23+
24+
25+
#ifndef NINA_GPIO0
26+
#define NINA_GPIO0 (28)
27+
#endif
28+
29+
#ifndef NINA_RESETN
30+
#define NINA_RESETN (29)
31+
#endif
32+
33+
#endif
34+
void setup() {
35+
Serial.begin(115200);
36+
37+
pinMode(NINA_GPIO0, OUTPUT);
38+
pinMode(NINA_RESETN, OUTPUT);
39+
40+
digitalWrite(NINA_GPIO0, HIGH);
41+
delay(100);
42+
digitalWrite(NINA_RESETN, HIGH);
43+
digitalWrite(NINA_RESETN, LOW);
44+
digitalWrite(NINA_RESETN, HIGH);
45+
46+
spiBegin();
47+
}
48+
49+
50+
void loop() {
51+
while (Serial.available()) {
52+
txBuffer[sizec] = Serial.read();
53+
sizec++;
54+
}
55+
if (sizec > 0) {
56+
Serial.println("sending");
57+
sizec *= 8;
58+
writeToSlave(txBuffer, &sizec);
59+
sizec = 0;
60+
}
61+
uint8_t raedableByte = readFromSlave(rxBuffer);
62+
if (raedableByte > 0) {
63+
for (int i = 0; i < raedableByte; i++) {
64+
Serial.print(char(rxBuffer[i]));
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)