Repository navigation
Expand file tree
/
Copy pathKIM_Arduino_Library.cpp
More file actions
244 lines (195 loc) · 6.61 KB
/
Copy pathKIM_Arduino_Library.cpp
File metadata and controls
244 lines (195 loc) · 6.61 KB
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
// -------------------------------------------------------------------------- //
//! @file KIM_Arduino_Library.cpp
//! @brief Handles the interface between KIM interface shield and Arduino
//! @note Board compatibility :
//! - Arduino UNO
//! - Arduino UNO Wifi Rev2
//! - Wemos D1
//! - Arduino MKR Wifi 1010
//! - Arduino NANO 33 BLE
//! @author Kineis
//! @date 2022-02-03
//! @version 0.0.1
// -------------------------------------------------------------------------- //
// -------------------------------------------------------------------------- //
// Includes
// -------------------------------------------------------------------------- //
#include "KIM_Arduino_Library.h"
// -------------------------------------------------------------------------- //
//! Constructor
// -------------------------------------------------------------------------- //
KIM::KIM(SerialUART *device)
{
kimSerial = device;
pinMode(USER_INT_KIM, OUTPUT);
pinMode(ON_OFF_KIM, OUTPUT);
pinMode(RX_KIM, OUTPUT);
pinMode(TX_KIM, INPUT);
digitalWrite(USER_INT_KIM, LOW);
digitalWrite(ON_OFF_KIM, LOW);
kimState = UNKNOWN;
}
// -------------------------------------------------------------------------- //
//! Toggle WU pin
// -------------------------------------------------------------------------- //
void KIM::KIM_userWakeupPinToggle()
{
digitalWrite(USER_INT_KIM, HIGH);
delay(50);
digitalWrite(USER_INT_KIM, LOW);
}
// -------------------------------------------------------------------------- //
//! Turn ON or OFF KIM module
// -------------------------------------------------------------------------- //
void KIM::KIM_powerON(bool powerON)
{
if (powerON) {
digitalWrite(ON_OFF_KIM, powerON);
#if softSerial
new (kimSerial) SerialUART(RX_KIM, TX_KIM); //Initialize UART
#endif
kimSerial->begin(BAUDRATE);
kimSerial->flush(); //Clean TX buffer
while (kimSerial->available() > 0) //Clean RX buffer
kimSerial->read();
delay(10);
} else {
kimSerial->flush(); //Clean TX buffer
while (kimSerial->available() > 0) //Clean RX buffer
kimSerial->read();
digitalWrite(ON_OFF_KIM, powerON);
#if softSerial
kimSerial->~SerialUART(); //De-initialize UART
#else
kimSerial->end();
#endif
}
}
// -------------------------------------------------------------------------- //
//! Toggle WU pin and check KIM module
// -------------------------------------------------------------------------- //
bool KIM::KIM_check()
{
KIM_userWakeupPinToggle();
KIM_sendATCommandGet("PING", 4);
if (response[0] != '+') {
if (isKIM2 == true) {
isKIM2 = false;
AT_REQUEST[3] = '\0';
} else {
isKIM2 = true;
AT_REQUEST[3] = '\n';
}
KIM_sendATCommandGet("PING", 4);
}
if (KIM_getState() == KIM_OK)
return true;
else
return false;
}
// -------------------------------------------------------------------------- //
//! Returns the KIM module last State (Updated when a command is sent)
// -------------------------------------------------------------------------- //
retStatusKIM_t KIM::KIM_getState()
{
return kimState;
}
// -------------------------------------------------------------------------- //
//! Returns the KIM module nature (true : KIM2 ; false : KIM1)
// -------------------------------------------------------------------------- //
bool KIM::KIM_isKIM2()
{
return isKIM2;
}
// -------------------------------------------------------------------------- //
//! This function sends AT command request to the KIM module.
// -------------------------------------------------------------------------- //
char *KIM::KIM_sendATCommandGet(char ATcmd[], uint8_t len)
{
// Request AT command starts with "AT+"
for (uint8_t i = 0; i < sizeof(AT); i++)
command[i] = AT[i];
// AT command type
for (uint8_t i = 0; i < len; i++)
command[i + sizeof(AT)] = ATcmd[i];
// AT command request ends with "=?\r\0"
for (uint8_t i = 0; i < sizeof(AT_REQUEST); i++)
command[i + sizeof(AT) + len] = AT_REQUEST[i];
kimState = KIM_sendUART();
return &response[0];
}
// -------------------------------------------------------------------------- //
//! This function sends AT command to the KIM module.
// -------------------------------------------------------------------------- //
char *KIM::KIM_sendATCommandSet(char ATcmd[], uint8_t len, char ATcmdValue[], uint8_t valueLen)
{
// Request AT command starts with "AT+"
for (uint8_t i = 0; i < sizeof(AT); i++)
command[i] = AT[i];
// AT command type
for (uint8_t i = 0; i < len; i++)
command[i + sizeof(AT)] = ATcmd[i];
command[sizeof(AT) + len] = '=';
// AT command value
for (uint8_t i = 0; i < valueLen; i++)
command[i + sizeof(AT) + len + 1] = ATcmdValue[i];
// AT command ends with "\r\0"
for (uint8_t i = 0; i < sizeof(AT_REQUEST) - 2; i++)
command[i + sizeof(AT) + len + 1 + valueLen] = AT_REQUEST[i + 2];
kimState = KIM_sendUART();
return &response[0];
}
// -------------------------------------------------------------------------- //
//! This function sends command to the KIM module.
// -------------------------------------------------------------------------- //
retStatusKIM_t KIM::KIM_sendUART()
{
bool delayedResponse = false;
uint8_t i = 0;
kimSerial->flush(); //Clean TX buffer
while (kimSerial->available() > 0) //Clean RX buffer
kimSerial->read();
if(command[3] == 'T' && command[4] == 'X')
delayedResponse = true;
if (delayedResponse == false)
i = 5;
response[0] = '\0';
kimSerial->print(command);
for ( ; i < 10; i++) {
uint8_t k = kimSerial->readBytesUntil('\n', response, 20);
response[k] = '\0';
if (response[0] == '+') {
if (response[1] != 'E' && response[2] == 'R')/*+ERROR*/
return KIM_ERROR;
else
if (delayedResponse == false)
return KIM_OK;
else
delayedResponse = false;
}
}
return KIM_TIMEOUT;
}
// -------------------------------------------------------------------------- //
//! Send raw data to KIM UART bus.
// -------------------------------------------------------------------------- //
char *KIM::KIM_sendUARTRaw(char RAWcmd[], uint8_t len)
{
for (uint8_t i = 0; i < len - 3; i++)
command[i] = RAWcmd[i];
// Be sure that command ends with "\r\0"
command[len - 3] = '\r';
command[len - 2] = '\n';
command[len - 1] = '\0';
kimState = KIM_sendUART();
return &response[0];
}
// -------------------------------------------------------------------------- //
//! Binary uint8_t[] to hex-string (char[]) convert function.
// -------------------------------------------------------------------------- //
void KIM::KIM_uint2hexString(uint8_t *input, uint8_t len, char *output)
{
uint8_t idx = 0;
for (idx = 0; idx < len; idx++)
sprintf((char *)(output + idx * 2), "%02X", input[idx]);
}