-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathATC.c
378 lines (373 loc) · 13.6 KB
/
ATC.c
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include "ATC.h"
#include <string.h>
#include <stdlib.h>
uint8_t atc_cnt;
ATC_t atc_elements[ATC_MAX_ELEMENTS];
//#####################################################################################################
__weak void atc_userFoundString(ATC_Element_t ATC_Element, const char* foundStr, char* startPtr)
{
}
//#####################################################################################################
void atc_callback(ATC_Element_t ATC_Element)
{
if(LL_USART_IsActiveFlag_RXNE(atc_elements[ATC_Element].usart) && LL_USART_IsEnabledIT_RXNE(atc_elements[ATC_Element].usart))
{
if(atc_elements[ATC_Element].rxIndex < atc_elements[ATC_Element].rxSize - 1)
{
atc_elements[ATC_Element].rxBuffer[atc_elements[ATC_Element].rxIndex] = LL_USART_ReceiveData8(atc_elements[ATC_Element].usart);
atc_elements[ATC_Element].rxIndex++;
}
else
LL_USART_ReceiveData8(atc_elements[ATC_Element].usart);
}
atc_elements[ATC_Element].rxTime = HAL_GetTick();
}
//#####################################################################################################
void atc_callback_txDMA(ATC_Element_t ATC_Element)
{
#if (ATC_TXDMA == 1)
switch(atc_elements[ATC_Element].txDMA_Stream)
{
case LL_DMA_STREAM_0:
if(LL_DMA_IsActiveFlag_TC0(atc_elements[ATC_Element].txDMA))
{
LL_DMA_ClearFlag_TC0(atc_elements[ATC_Element].txDMA);
atc_elements[ATC_Element].txDMAdone = 1;
}
break;
case LL_DMA_STREAM_1:
if(LL_DMA_IsActiveFlag_TC1(atc_elements[ATC_Element].txDMA))
{
LL_DMA_ClearFlag_TC1(atc_elements[ATC_Element].txDMA);
atc_elements[ATC_Element].txDMAdone = 1;
}
break;
case LL_DMA_STREAM_2:
if(LL_DMA_IsActiveFlag_TC2(atc_elements[ATC_Element].txDMA))
{
LL_DMA_ClearFlag_TC2(atc_elements[ATC_Element].txDMA);
atc_elements[ATC_Element].txDMAdone = 1;
}
break;
case LL_DMA_STREAM_3:
if(LL_DMA_IsActiveFlag_TC3(atc_elements[ATC_Element].txDMA))
{
LL_DMA_ClearFlag_TC3(atc_elements[ATC_Element].txDMA);
atc_elements[ATC_Element].txDMAdone = 1;
}
break;
case LL_DMA_STREAM_4:
if(LL_DMA_IsActiveFlag_TC4(atc_elements[ATC_Element].txDMA))
{
LL_DMA_ClearFlag_TC4(atc_elements[ATC_Element].txDMA);
atc_elements[ATC_Element].txDMAdone = 1;
}
break;
case LL_DMA_STREAM_5:
if(LL_DMA_IsActiveFlag_TC5(atc_elements[ATC_Element].txDMA))
{
LL_DMA_ClearFlag_TC5(atc_elements[ATC_Element].txDMA);
atc_elements[ATC_Element].txDMAdone = 1;
}
break;
case LL_DMA_STREAM_6:
if(LL_DMA_IsActiveFlag_TC6(atc_elements[ATC_Element].txDMA))
{
LL_DMA_ClearFlag_TC6(atc_elements[ATC_Element].txDMA);
atc_elements[ATC_Element].txDMAdone = 1;
}
break;
case LL_DMA_STREAM_7:
if(LL_DMA_IsActiveFlag_TC7(atc_elements[ATC_Element].txDMA))
{
LL_DMA_ClearFlag_TC7(atc_elements[ATC_Element].txDMA);
atc_elements[ATC_Element].txDMAdone = 1;
}
break;
}
#endif
}
//#####################################################################################################
void atc_process(void)
{
for(uint8_t el = 0; el < atc_cnt ; el++)
{
if((atc_elements[el].rxIndex > 0) && (HAL_GetTick() - atc_elements[el].rxTime > atc_elements[el].rxTimeout))
{
// +++ answer AtCommand
for(uint8_t ans = 0 ; ans < ATC_MAX_ANSWER_SEARCHING_STRING ; ans++)
{
if(atc_elements[el].answerSearchingString[ans] == NULL)
break;
char *ansStr = strstr((char*)atc_elements[el].rxBuffer, atc_elements[el].answerSearchingString[ans]);
if(ansStr != NULL)
{
if(atc_elements[el].foundAnswerString != NULL)
strncpy(atc_elements[el].foundAnswerString, ansStr, atc_elements[el].foundAnswerSize);
atc_elements[el].foundAnswer = ans;
break;
}
}
// --- answer AtCommand
// +++ auto searching
for(uint8_t au = 0 ; au < ATC_MAX_ALWAYS_SEARCHING_STRING ; au++)
{
if(atc_elements[el].alwaysSearchingString[au] == NULL)
break;
char *autoStr = strstr((char*)atc_elements[el].rxBuffer, atc_elements[el].alwaysSearchingString[au]);
if(autoStr != NULL)
{
atc_userFoundString((ATC_Element_t)el, atc_elements[el].alwaysSearchingString[au], autoStr);
}
}
// --- auto searching
atc_elements[el].rxIndex = 0;
memset(atc_elements[el].rxBuffer, 0, atc_elements[el].rxSize);
}
}
}
//#####################################################################################################
uint8_t atc_sendString(ATC_Element_t ATC_Element, char *str, uint32_t timeout)
{
if((ATC_Element < ATC_Element_0) || (ATC_Element >= atc_cnt))
return 0;
while(atc_elements[ATC_Element].txBusy == 1)
atc_delay(1);
atc_elements[ATC_Element].txBusy = 1;
uint32_t startTime = HAL_GetTick();
#if (ATC_TXDMA == 0)
for(uint16_t i = 0 ; i<strlen(str) ; i++)
{
while (!LL_USART_IsActiveFlag_TXE(atc_elements[ATC_Element].usart))
{
if(HAL_GetTick() - startTime > timeout)
{
atc_elements[ATC_Element].txBusy = 0;
return 0;
}
}
LL_USART_TransmitData8(atc_elements[ATC_Element].usart, str[i]);
}
while (!LL_USART_IsActiveFlag_TC(atc_elements[ATC_Element].usart))
{
if(HAL_GetTick() - startTime > timeout)
{
atc_elements[ATC_Element].txBusy = 0;
return 0;
}
}
#endif
#if (ATC_TXDMA == 1)
atc_elements[ATC_Element].txDMAdone = 0;
LL_DMA_ConfigAddresses(atc_elements[ATC_Element].txDMA, atc_elements[ATC_Element].txDMA_Stream,\
(uint32_t)str, LL_USART_DMA_GetRegAddr(atc_elements[ATC_Element].usart),\
LL_DMA_GetDataTransferDirection(atc_elements[ATC_Element].txDMA, atc_elements[ATC_Element].txDMA_Stream));
LL_DMA_SetDataLength(atc_elements[ATC_Element].txDMA, atc_elements[ATC_Element].txDMA_Stream, strlen(str));
LL_DMA_EnableStream(atc_elements[ATC_Element].txDMA, atc_elements[ATC_Element].txDMA_Stream);
while(atc_elements[ATC_Element].txDMAdone == 0)
{
if(HAL_GetTick() - startTime > timeout)
{
LL_DMA_DisableStream(atc_elements[ATC_Element].txDMA, atc_elements[ATC_Element].txDMA_Stream);
atc_elements[ATC_Element].txBusy = 0;
return 0;
}
atc_delay(1);
}
#endif
atc_elements[ATC_Element].txBusy = 0;
return 1;
}
//#####################################################################################################
uint8_t atc_sendData(ATC_Element_t ATC_Element, uint8_t *data, uint16_t size, uint32_t timeout)
{
if((ATC_Element < ATC_Element_0) || (ATC_Element >= atc_cnt))
return 0;
while(atc_elements[ATC_Element].txBusy == 1)
atc_delay(1);
atc_elements[ATC_Element].txBusy = 1;
uint32_t startTime = HAL_GetTick();
#if (ATC_TXDMA == 0)
for (uint16_t i = 0; i<size; i++)
{
while (!LL_USART_IsActiveFlag_TXE(atc_elements[ATC_Element].usart))
{
if(HAL_GetTick() - startTime > timeout)
{
atc_elements[ATC_Element].txBusy = 0;
return 0;
}
}
LL_USART_TransmitData8(atc_elements[ATC_Element].usart, data[i]);
}
while (!LL_USART_IsActiveFlag_TC(atc_elements[ATC_Element].usart))
{
if(HAL_GetTick() - startTime > timeout)
{
atc_elements[ATC_Element].txBusy = 0;
return 0;
}
}
#endif
#if (ATC_TXDMA == 1)
atc_elements[ATC_Element].txDMAdone = 0;
LL_DMA_ConfigAddresses(atc_elements[ATC_Element].txDMA, atc_elements[ATC_Element].txDMA_Stream,\
(uint32_t)data, LL_USART_DMA_GetRegAddr(atc_elements[ATC_Element].usart),\
LL_DMA_GetDataTransferDirection(atc_elements[ATC_Element].txDMA, atc_elements[ATC_Element].txDMA_Stream));
LL_DMA_SetDataLength(atc_elements[ATC_Element].txDMA, atc_elements[ATC_Element].txDMA_Stream, size);
while(atc_elements[ATC_Element].txDMAdone == 0)
{
atc_delay(1);
if(HAL_GetTick() - startTime > timeout)
{
atc_elements[ATC_Element].txBusy = 0;
return 0;
}
}
#endif
atc_elements[ATC_Element].txBusy = 0;
return 1;
}
//#####################################################################################################
uint8_t atc_sendAtCommand(ATC_Element_t ATC_Element, char *atCommand, uint32_t wait_ms, char *answerString, uint16_t answerSize, uint8_t searchingItems,...)
{
if((ATC_Element < ATC_Element_0) || (ATC_Element >= atc_cnt))
return 0;
atc_process();
uint32_t startTime;
atc_elements[ATC_Element].foundAnswerSize = answerSize;
atc_elements[ATC_Element].foundAnswerString = answerString;
va_list tag;
va_start (tag,searchingItems);
for(uint8_t i=0; i<searchingItems ; i++)
{
char *str = va_arg (tag, char*);
#if (ATC_CMSIS_OS == 0)
atc_elements[ATC_Element].answerSearchingString[i] = malloc(strlen(str));
#endif
#if (ATC_CMSIS_OS == 1)
atc_elements[ATC_Element].answerSearchingString[i] = pvPortMalloc(strlen(str));
#endif
if(atc_elements[ATC_Element].answerSearchingString[i] != NULL)
strcpy(atc_elements[ATC_Element].answerSearchingString[i], str);
}
va_end(tag);
atc_elements[ATC_Element].foundAnswer = -1;
uint8_t retValue = 0;
if(atc_sendString(ATC_Element, atCommand, 1000) == 0)
goto SEND_FAILD;
if(answerString != NULL)
memset(answerString, 0 , answerSize);
startTime = HAL_GetTick();
while(1)
{
atc_delay(1);
atc_process();
if(atc_elements[ATC_Element].foundAnswer >= 0)
{
retValue = atc_elements[ATC_Element].foundAnswer + 1;
break;
}
if(HAL_GetTick() - startTime > wait_ms)
{
if(searchingItems == 0)
retValue = 1;
break;
}
}
SEND_FAILD:
#if (ATC_CMSIS_OS == 0)
for(uint8_t i = 0 ; i < ATC_MAX_ANSWER_SEARCHING_STRING ; i++)
{
free(atc_elements[ATC_Element].answerSearchingString[i]);
atc_elements[ATC_Element].answerSearchingString[i] = NULL;
}
free(atc_elements[ATC_Element].foundAnswerString);
atc_elements[ATC_Element].foundAnswerString = NULL;
#endif
#if (ATC_CMSIS_OS == 1)
for(uint8_t i = 0 ; i < ATC_MAX_ANSWER_SEARCHING_STRING ; i++)
{
vPortFree(atc_elements[ATC_Element].answerSearchingString[i]);
atc_elements[ATC_Element].answerSearchingString[i] = NULL;
}
#endif
return retValue;
}
//#####################################################################################################
ATC_Element_t atc_getElementByName(char *name)
{
for(uint8_t i = 0 ; i < ATC_MAX_ELEMENTS ; i++)
{
if(strcmp(atc_elements[i].name, name) == 0)
return (ATC_Element_t)i;
}
return ATC_Element_Error;
}
//#####################################################################################################
bool atc_addAutoSearchString(ATC_Element_t ATC_Element, char *str)
{
if((ATC_Element < ATC_Element_0) || (ATC_Element >= atc_cnt))
return false;
for(uint8_t s = 0; s < ATC_MAX_ALWAYS_SEARCHING_STRING ; s++)
{
if(atc_elements[ATC_Element].alwaysSearchingString[s] == NULL)
{
#if (ATC_CMSIS_OS == 0)
atc_elements[ATC_Element].alwaysSearchingString[s] = malloc(strlen(str));
#endif
#if (ATC_CMSIS_OS == 1)
atc_elements[ATC_Element].alwaysSearchingString[s] = pvPortMalloc(strlen(str));
#endif
if(atc_elements[ATC_Element].alwaysSearchingString[s] != NULL)
{
strcpy(atc_elements[ATC_Element].alwaysSearchingString[s], str);
return true;
}
else
break;
}
}
return false;
}
//#####################################################################################################
uint16_t atc_getSize(ATC_Element_t ATC_Element)
{
return sizeof(atc_elements[ATC_Element]);
}
//#####################################################################################################
#if (ATC_TXDMA == 0)
bool atc_init(ATC_Element_t ATC_Element, USART_TypeDef *usart, uint16_t rxSize, uint16_t rxTimeout, char* name)
#endif
#if (ATC_TXDMA == 1)
bool atc_init(ATC_Element_t ATC_Element, USART_TypeDef *usart, uint16_t rxSize, uint16_t rxTimeout, char* name, DMA_TypeDef *DMA, uint32_t LL_DMA_STREAM_)
#endif
{
if(ATC_Element != atc_cnt)
return false;
memset(&atc_elements[ATC_Element], 0, sizeof(ATC_t));
atc_elements[ATC_Element].usart = usart;
atc_elements[ATC_Element].rxSize = rxSize;
atc_elements[ATC_Element].rxTimeout = rxTimeout;
strncpy(atc_elements[ATC_Element].name, name, 8);
#if (ATC_TXDMA == 1)
atc_elements[ATC_Element].txDMA = DMA;
atc_elements[ATC_Element].txDMA_Stream = LL_DMA_STREAM_;
LL_DMA_EnableIT_TC(atc_elements[ATC_Element].txDMA, atc_elements[ATC_Element].txDMA_Stream);
LL_USART_EnableDMAReq_TX(atc_elements[ATC_Element].usart);
#endif
#if (ATC_CMSIS_OS == 0)
atc_elements[ATC_Element].rxBuffer = malloc(rxSize);
#endif
#if (ATC_CMSIS_OS == 1)
atc_elements[ATC_Element].rxBuffer = pvPortMalloc(rxSize);
#endif
if(atc_elements[ATC_Element].rxBuffer != NULL)
{
atc_cnt++;
LL_USART_EnableIT_RXNE(atc_elements[ATC_Element].usart);
return true;
}
return false;
}
//#####################################################################################################