Skip to content

Commit 7c8d9b2

Browse files
author
Max Vilimpoc
committed
Add TestEspApi example, exercising the non-RTOS C API.
1 parent 269d7ca commit 7c8d9b2

File tree

1 file changed

+313
-0
lines changed

1 file changed

+313
-0
lines changed
Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
1+
/**
2+
* TestEspApi by Max Vilimpoc
3+
* This code is released to the public domain.
4+
*
5+
* Test out the Expressif ESP8266 Non-OS API, exhaustively trying out
6+
* as many of the built-in functions as possible.
7+
*
8+
* Some of the code is based on examples in:
9+
* "20A-ESP8266__RTOS_SDK__Programming Guide__EN_v1.3.0.pdf"
10+
*/
11+
12+
#ifdef ESP8266
13+
extern "C" {
14+
#include "user_interface.h"
15+
}
16+
#endif
17+
18+
// Set up output serial port (could be a SoftwareSerial
19+
// if really wanted).
20+
21+
Stream& ehConsolePort(Serial);
22+
23+
// Wired to the blue LED on an ESP-01 board, active LOW.
24+
//
25+
// Cannot be used simultaneously with Serial.print/println()
26+
// calls, as TX is wired to the same pin.
27+
//
28+
// UNLESS: You swap the TX pin using the alternate pinout.
29+
const uint8_t LED_PIN = 1;
30+
31+
const char * const RST_REASONS[] =
32+
{
33+
"REASON_DEFAULT_RST",
34+
"REASON_WDT_RST",
35+
"REASON_EXCEPTION_RST",
36+
"REASON_SOFT_WDT_RST",
37+
"REASON_SOFT_RESTART",
38+
"REASON_DEEP_SLEEP_AWAKE",
39+
"REASON_EXT_SYS_RST"
40+
};
41+
42+
const char * const FLASH_SIZE_MAP_NAMES[] =
43+
{
44+
"FLASH_SIZE_4M_MAP_256_256",
45+
"FLASH_SIZE_2M",
46+
"FLASH_SIZE_8M_MAP_512_512",
47+
"FLASH_SIZE_16M_MAP_512_512",
48+
"FLASH_SIZE_32M_MAP_512_512",
49+
"FLASH_SIZE_16M_MAP_1024_1024",
50+
"FLASH_SIZE_32M_MAP_1024_1024"
51+
};
52+
53+
const char * const OP_MODE_NAMES[]
54+
{
55+
"NULL_MODE",
56+
"STATION_MODE",
57+
"SOFTAP_MODE",
58+
"STATIONAP_MODE"
59+
};
60+
61+
const char * const AUTH_MODE_NAMES[]
62+
{
63+
"AUTH_OPEN",
64+
"AUTH_WEP",
65+
"AUTH_WPA_PSK",
66+
"AUTH_WPA2_PSK",
67+
"AUTH_WPA_WPA2_PSK",
68+
"AUTH_MAX"
69+
};
70+
71+
const char * const PHY_MODE_NAMES[]
72+
{
73+
"",
74+
"PHY_MODE_11B",
75+
"PHY_MODE_11G",
76+
"PHY_MODE_11N"
77+
};
78+
79+
const char * const EVENT_NAMES[]
80+
{
81+
"EVENT_STAMODE_CONNECTED",
82+
"EVENT_STAMODE_DISCONNECTED",
83+
"EVENT_STAMODE_AUTHMODE_CHANGE",
84+
"EVENT_STAMODE_GOT_IP",
85+
"EVENT_SOFTAPMODE_STACONNECTED",
86+
"EVENT_SOFTAPMODE_STADISCONNECTED",
87+
"EVENT_MAX"
88+
};
89+
90+
const char * const EVENT_REASONS[]
91+
{
92+
"",
93+
"REASON_UNSPECIFIED",
94+
"REASON_AUTH_EXPIRE",
95+
"REASON_AUTH_LEAVE",
96+
"REASON_ASSOC_EXPIRE",
97+
"REASON_ASSOC_TOOMANY",
98+
"REASON_NOT_AUTHED",
99+
"REASON_NOT_ASSOCED",
100+
"REASON_ASSOC_LEAVE",
101+
"REASON_ASSOC_NOT_AUTHED",
102+
"REASON_DISASSOC_PWRCAP_BAD",
103+
"REASON_DISASSOC_SUPCHAN_BAD",
104+
"REASON_IE_INVALID",
105+
"REASON_MIC_FAILURE",
106+
"REASON_4WAY_HANDSHAKE_TIMEOUT",
107+
"REASON_GROUP_KEY_UPDATE_TIMEOUT",
108+
"REASON_IE_IN_4WAY_DIFFERS",
109+
"REASON_GROUP_CIPHER_INVALID",
110+
"REASON_PAIRWISE_CIPHER_INVALID",
111+
"REASON_AKMP_INVALID",
112+
"REASON_UNSUPP_RSN_IE_VERSION",
113+
"REASON_INVALID_RSN_IE_CAP",
114+
"REASON_802_1X_AUTH_FAILED",
115+
"REASON_CIPHER_SUITE_REJECTED",
116+
};
117+
118+
const char * const EVENT_REASONS_200[]
119+
{
120+
"REASON_BEACON_TIMEOUT",
121+
"REASON_NO_AP_FOUND"
122+
};
123+
124+
void wifi_event_handler_cb(System_Event_t * event)
125+
{
126+
ehConsolePort.print(EVENT_NAMES[event->event]);
127+
ehConsolePort.print(" (");
128+
129+
switch (event->event)
130+
{
131+
case EVENT_STAMODE_CONNECTED:
132+
break;
133+
case EVENT_STAMODE_DISCONNECTED:
134+
break;
135+
case EVENT_STAMODE_AUTHMODE_CHANGE:
136+
break;
137+
case EVENT_STAMODE_GOT_IP:
138+
break;
139+
case EVENT_SOFTAPMODE_STACONNECTED:
140+
case EVENT_SOFTAPMODE_STADISCONNECTED:
141+
{
142+
char mac[32] = {0};
143+
snprintf(mac, 32, MACSTR ", aid: %d" , MAC2STR(event->event_info.sta_connected.mac), event->event_info.sta_connected.aid);
144+
145+
ehConsolePort.print(mac);
146+
}
147+
break;
148+
}
149+
150+
ehConsolePort.println(")");
151+
}
152+
153+
void print_softap_config(Stream & consolePort, softap_config const& config)
154+
{
155+
consolePort.println();
156+
consolePort.println(F("SoftAP Configuration"));
157+
consolePort.println(F("--------------------"));
158+
159+
consolePort.print(F("ssid: "));
160+
consolePort.println((char *) config.ssid);
161+
162+
consolePort.print(F("password: "));
163+
consolePort.println((char *) config.password);
164+
165+
consolePort.print(F("ssid_len: "));
166+
consolePort.println(config.ssid_len);
167+
168+
consolePort.print(F("channel: "));
169+
consolePort.println(config.channel);
170+
171+
consolePort.print(F("authmode: "));
172+
consolePort.println(AUTH_MODE_NAMES[config.authmode]);
173+
174+
consolePort.print(F("ssid_hidden: "));
175+
consolePort.println(config.ssid_hidden);
176+
177+
consolePort.print(F("max_connection: "));
178+
consolePort.println(config.max_connection);
179+
180+
consolePort.print(F("beacon_interval: "));
181+
consolePort.print(config.beacon_interval);
182+
consolePort.println("ms");
183+
184+
consolePort.println(F("--------------------"));
185+
consolePort.println();
186+
}
187+
188+
void print_system_info(Stream & consolePort)
189+
{
190+
const rst_info * resetInfo = system_get_rst_info();
191+
consolePort.print(F("system_get_rst_info() reset reason: "));
192+
consolePort.println(RST_REASONS[resetInfo->reason]);
193+
194+
consolePort.print(F("system_get_free_heap_size(): "));
195+
consolePort.println(system_get_free_heap_size());
196+
197+
consolePort.print(F("system_get_os_print(): "));
198+
consolePort.println(system_get_os_print());
199+
system_set_os_print(1);
200+
consolePort.print(F("system_get_os_print(): "));
201+
consolePort.println(system_get_os_print());
202+
203+
system_print_meminfo();
204+
205+
consolePort.print(F("system_get_chip_id(): 0x"));
206+
consolePort.println(system_get_chip_id(), HEX);
207+
208+
consolePort.print(F("system_get_sdk_version(): "));
209+
consolePort.println(system_get_sdk_version());
210+
211+
consolePort.print(F("system_get_boot_version(): "));
212+
consolePort.println(system_get_boot_version());
213+
214+
consolePort.print(F("system_get_userbin_addr(): 0x"));
215+
consolePort.println(system_get_userbin_addr(), HEX);
216+
217+
consolePort.print(F("system_get_boot_mode(): "));
218+
consolePort.println(system_get_boot_mode() == 0 ? F("SYS_BOOT_ENHANCE_MODE") : F("SYS_BOOT_NORMAL_MODE"));
219+
220+
consolePort.print(F("system_get_cpu_freq(): "));
221+
consolePort.println(system_get_cpu_freq());
222+
223+
consolePort.print(F("system_get_flash_size_map(): "));
224+
consolePort.println(FLASH_SIZE_MAP_NAMES[system_get_flash_size_map()]);
225+
}
226+
227+
void print_wifi_general(Stream & consolePort)
228+
{
229+
consolePort.print(F("wifi_get_channel(): "));
230+
consolePort.println(wifi_get_channel());
231+
232+
consolePort.print(F("wifi_get_phy_mode(): "));
233+
consolePort.println(PHY_MODE_NAMES[wifi_get_phy_mode()]);
234+
}
235+
236+
void secure_softap_config(softap_config * config, const char * ssid, const char * password)
237+
{
238+
size_t ssidLen = strlen(ssid) < sizeof(config->ssid) ? strlen(ssid) : sizeof(config->ssid);
239+
size_t passwordLen = strlen(password) < sizeof(config->password) ? strlen(password) : sizeof(config->password);
240+
241+
memset(config->ssid, 0, sizeof(config->ssid));
242+
memcpy(config->ssid, ssid, ssidLen);
243+
244+
memset(config->password, 0, sizeof(config->password));
245+
memcpy(config->password, password, passwordLen);
246+
247+
config->ssid_len = ssidLen;
248+
config->channel = 1;
249+
config->authmode = AUTH_WPA2_PSK;
250+
// config->ssid_hidden = 1;
251+
config->max_connection = 4;
252+
// config->beacon_interval = 1000;
253+
}
254+
255+
void setup()
256+
{
257+
// Reuse default Serial port rate, so the bootloader
258+
// messages are also readable.
259+
260+
Serial.begin(74880);
261+
262+
// Try pushing frequency to 160MHz.
263+
system_update_cpu_freq(SYS_CPU_160MHZ);
264+
265+
Serial.println();
266+
Serial.println(F("ESP starting."));
267+
268+
// System usually boots up in about 200ms.
269+
270+
Serial.print(F("system_get_time(): "));
271+
Serial.println(system_get_time());
272+
273+
// set_event_handler_cb_stream(Serial);
274+
wifi_set_event_handler_cb(wifi_event_handler_cb);
275+
276+
print_system_info(Serial);
277+
278+
Serial.print(F("wifi_get_opmode(): "));
279+
Serial.print(wifi_get_opmode());
280+
Serial.print(F(" - "));
281+
Serial.println(OP_MODE_NAMES[wifi_get_opmode()]);
282+
283+
Serial.print(F("wifi_get_opmode_default(): "));
284+
Serial.print(wifi_get_opmode_default());
285+
Serial.print(F(" - "));
286+
Serial.println(OP_MODE_NAMES[wifi_get_opmode_default()]);
287+
288+
Serial.print(F("wifi_get_broadcast_if(): "));
289+
Serial.println(wifi_get_broadcast_if());
290+
291+
softap_config config;
292+
wifi_softap_get_config(&config);
293+
secure_softap_config(&config, "TestAP", "testtesttest");
294+
wifi_softap_set_config(&config);
295+
print_softap_config(Serial, config);
296+
297+
print_wifi_general(Serial);
298+
299+
// This doesn't work on an ESP-01.
300+
// wifi_set_sleep_type(LIGHT_SLEEP_T);
301+
302+
// Try this dirty little thing.
303+
// Doesn't work because ESP-01 module doesn't link XPD_DCDC -> RST.
304+
// ESP.deepSleep(15000);
305+
}
306+
307+
void loop()
308+
{
309+
Serial.print(F("system_get_time(): "));
310+
Serial.println(system_get_time());
311+
delay(1000);
312+
}
313+

0 commit comments

Comments
 (0)