This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
/
wifi_spi.h
271 lines (220 loc) · 7.63 KB
/
wifi_spi.h
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
/**********************************************************************************************************************************
wifi_spi.h - Library for Arduino WiFiNINA module/shield.
Based on and modified from WiFiNINA library https://www.arduino.cc/en/Reference/WiFiNINA
to support nRF52, SAMD21/SAMD51, STM32F/L/H/G/WB/MP1, Teensy, etc. boards besides Nano-33 IoT, MKRWIFI1010, MKRVIDOR400, etc.
Built by Khoi Hoang https://github.com/khoih-prog/WiFiNINA_Generic
Licensed under MIT license
Copyright (c) 2018 Arduino SA. All rights reserved.
Copyright (c) 2011-2014 Arduino LLC. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Version: 1.8.15-1
Version Modified By Date Comments
------- ----------- ---------- -----------
1.5.0 K Hoang 27/03/2020 Initial coding to support other boards besides Nano-33 IoT, MKRWIFI1010, MKRVIDOR4000, etc.
such as Arduino Mega, Teensy, SAMD21, SAMD51, STM32, etc
...
1.8.13 K Hoang 03/08/2021 Sync with WiFiNINA v1.8.13 : new FW v1.4.8. Add support to ADAFRUIT_MATRIXPORTAL_M4_EXPRESS
1.8.14-1 K Hoang 25/11/2021 Fix examples to support ATmega4809 such as UNO_WIFI_REV2 and NANO_EVERY
1.8.14-2 K Hoang 31/12/2021 Add support to Nano_RP2040_Connect using arduino-pico core
1.8.14-3 K Hoang 31/12/2021 Fix issue with UDP for Nano_RP2040_Connect using arduino-pico core
1.8.14-4 K Hoang 01/05/2022 Fix bugs by using some PRs from original WiFiNINA. Add WiFiMulti-related examples
1.8.14-5 K Hoang 23/05/2022 Fix bug causing data lost when sending large files
1.8.14-6 K Hoang 17/08/2022 Add support to Teensy 4.x using WiFiNINA AirLift. Fix minor bug
1.8.14-7 K Hoang 11/11/2022 Modify WiFiWebServer example to avoid crash in arduino-pico core
1.8.15-0 K Hoang 14/11/2022 Fix severe limitation to permit sending much larger data than total 4K
1.8.15-1 K Hoang 18/11/2022 Using new WiFi101_Generic library to permit sending larger data than total 4K
***********************************************************************************************************************************/
#pragma once
#include <inttypes.h>
#include "utility/wl_definitions.h"
////////////////////////////////////////
#define CMD_FLAG 0
#define REPLY_FLAG 1<<7
#define DATA_FLAG 0x40
////////////////////////////////////////
#define WIFI_SPI_ACK 1
#define WIFI_SPI_ERR 0xFF
////////////////////////////////////////
#define TIMEOUT_CHAR 1000
////////////////////////////////////////
//#define MAX_SOCK_NUM 4 /**< Maxmimum number of socket */
#define NO_SOCKET_AVAIL 255
////////////////////////////////////////
#define START_CMD 0xE0
#define END_CMD 0xEE
#define ERR_CMD 0xEF
#define CMD_POS 1 // Position of Command OpCode on SPI stream
#define PARAM_LEN_POS 2 // Position of Param len on SPI stream
////////////////////////////////////////
enum
{
SET_NET_CMD = 0x10,
SET_PASSPHRASE_CMD = 0x11,
SET_KEY_CMD = 0x12,
// TEST_CMD = 0x13,
SET_IP_CONFIG_CMD = 0x14,
SET_DNS_CONFIG_CMD = 0x15,
SET_HOSTNAME_CMD = 0x16,
SET_POWER_MODE_CMD = 0x17,
SET_AP_NET_CMD = 0x18,
SET_AP_PASSPHRASE_CMD = 0x19,
SET_DEBUG_CMD = 0x1A,
GET_TEMPERATURE_CMD = 0x1B,
GET_REASON_CODE_CMD = 0x1F,
GET_CONN_STATUS_CMD = 0x20,
GET_IPADDR_CMD = 0x21,
GET_MACADDR_CMD = 0x22,
GET_CURR_SSID_CMD = 0x23,
GET_CURR_BSSID_CMD = 0x24,
GET_CURR_RSSI_CMD = 0x25,
GET_CURR_ENCT_CMD = 0x26,
SCAN_NETWORKS = 0x27,
START_SERVER_TCP_CMD = 0x28,
GET_STATE_TCP_CMD = 0x29,
DATA_SENT_TCP_CMD = 0x2A,
AVAIL_DATA_TCP_CMD = 0x2B,
GET_DATA_TCP_CMD = 0x2C,
START_CLIENT_TCP_CMD = 0x2D,
STOP_CLIENT_TCP_CMD = 0x2E,
GET_CLIENT_STATE_TCP_CMD = 0x2F,
DISCONNECT_CMD = 0x30,
// GET_IDX_SSID_CMD = 0x31,
GET_IDX_RSSI_CMD = 0x32,
GET_IDX_ENCT_CMD = 0x33,
REQ_HOST_BY_NAME_CMD = 0x34,
GET_HOST_BY_NAME_CMD = 0x35,
START_SCAN_NETWORKS = 0x36,
GET_FW_VERSION_CMD = 0x37,
// GET_TEST_CMD = 0x38,
SEND_DATA_UDP_CMD = 0x39,
GET_REMOTE_DATA_CMD = 0x3A,
GET_TIME_CMD = 0x3B,
GET_IDX_BSSID = 0x3C,
GET_IDX_CHANNEL_CMD = 0x3D,
PING_CMD = 0x3E,
GET_SOCKET_CMD = 0x3F,
// All command with DATA_FLAG 0x40 send a 16bit Len
SET_ENT_CMD = 0x40,
SEND_DATA_TCP_CMD = 0x44,
GET_DATABUF_TCP_CMD = 0x45,
INSERT_DATABUF_CMD = 0x46,
// regular format commands
SET_PIN_MODE = 0x50,
SET_DIGITAL_WRITE = 0x51,
SET_ANALOG_WRITE = 0x52,
GET_DIGITAL_READ = 0x53,
GET_ANALOG_READ = 0x54,
// regular format commands
WRITE_FILE = 0x60,
READ_FILE = 0x61,
DELETE_FILE = 0x62,
EXISTS_FILE = 0x63,
DOWNLOAD_FILE = 0x64,
APPLY_OTA_COMMAND = 0x65,
RENAME_FILE = 0x66,
// New from v1.7.0
DOWNLOAD_OTA = 0x67,
//////
};
////////////////////////////////////////
enum wl_tcp_state
{
CLOSED = 0,
LISTEN = 1,
SYN_SENT = 2,
SYN_RCVD = 3,
ESTABLISHED = 4,
FIN_WAIT_1 = 5,
FIN_WAIT_2 = 6,
CLOSE_WAIT = 7,
CLOSING = 8,
LAST_ACK = 9,
TIME_WAIT = 10
};
////////////////////////////////////////
enum numParams
{
PARAM_NUMS_0,
PARAM_NUMS_1,
PARAM_NUMS_2,
PARAM_NUMS_3,
PARAM_NUMS_4,
PARAM_NUMS_5,
PARAM_NUMS_6,
MAX_PARAM_NUMS
};
////////////////////////////////////////
#define MAX_PARAMS MAX_PARAM_NUMS-1
#define PARAM_LEN_SIZE 1
////////////////////////////////////////
typedef struct __attribute__((__packed__))
{
uint8_t paramLen;
char* param;
}
tParam;
////////////////////////////////////////
typedef struct __attribute__((__packed__))
{
uint16_t dataLen;
char* data;
}
tDataParam;
////////////////////////////////////////
typedef struct __attribute__((__packed__))
{
unsigned char cmd;
unsigned char tcmd;
unsigned char nParam;
tParam params[MAX_PARAMS];
}
tSpiMsg;
typedef struct __attribute__((__packed__))
{
unsigned char cmd;
unsigned char tcmd;
unsigned char nParam;
tDataParam params[MAX_PARAMS];
}
tSpiMsgData;
////////////////////////////////////////
typedef struct __attribute__((__packed__))
{
unsigned char cmd;
unsigned char tcmd;
//unsigned char totLen;
unsigned char nParam;
}
tSpiHdr;
////////////////////////////////////////
typedef struct __attribute__((__packed__))
{
uint8_t paramLen;
uint32_t param;
}
tLongParam;
////////////////////////////////////////
typedef struct __attribute__((__packed__))
{
uint8_t paramLen;
uint16_t param;
}
tIntParam;
////////////////////////////////////////
typedef struct __attribute__((__packed__))
{
uint8_t paramLen;
uint8_t param;
}
tByteParam;
////////////////////////////////////////