-
Notifications
You must be signed in to change notification settings - Fork 3
/
telitcomm.c
executable file
·373 lines (326 loc) · 9.21 KB
/
telitcomm.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
//******************************************************************************
// Telit Communications Manager for Project Bacon
//
// Description: Initializes Telit GE865-QUAD and posts datapacket to server
// USCI_A0 RX interrupt triggers fills circular buffer
// ACLK = BRCLK = LFXT1 = 32768, MCLK = SMCLK = DCO~1048k
// //* An external watch crystal is required on XIN XOUT for ACLK *//
//
// | P4.7/UCA0RXD|------------>
// | | 4800 - 8N1
// | P4.6/UCA0TXD|<------------
//
// R. von Kurnatowski III
// TX/RX Labs
// November 2011
// Built with CCE Version: 4.0.1
//******************************************************************************
#include "msp430xg46x.h"
#include "telitcomm.h"
#include "txrxlpf.h"
#include "powermgmt.h"
#include "intrinsics.h"
#include "in430.h"
#include <stdio.h>
/* Global Variables */
static char resp[40];
volatile unsigned char ucRXBuffer[32];
volatile unsigned char ucWriteIndex = 0;
volatile unsigned char ucReadIndex = 0;
extern volatile unsigned char timedout;
gprs_state_t gprs_state = STATE_UNINIT;
static char sgactt1[] = "#";
static char sgactt2[] = "\n0";
static char *atstr[] = {"AT\r",0};
static telitcmd at = {atstr,&ackOK};
static char *atechostr[] = {"ATE\r",0};
static telitcmd atecho = {atechostr, &ackOK};
static char *atvstr[] = {"ATV0\r",0};
static telitcmd atv = {atvstr, &ackOK};
static char *atqstr[] = {"ATQ0\r",0};
static telitcmd atq = {atqstr, &ackOK};
static char *atflowctlstr[] = {"AT&K=0\r",0};
static telitcmd atflowctl = {atflowctlstr, &ackOK};
static char *atsetusidstr[] = {"AT#USERID=\"[email protected]\"\r",0};
static telitcmd atsetusid = {atsetusidstr, &ackOK};
static char *atsetpassstr[] = {"AT#PASSW=\"CINGULAR1\"\r",0};
static telitcmd atsetpass = {atsetpassstr, &ackOK};
static char *atcgdcontstr[] = {"AT+CGDCONT=1,\"IP\",\"WAP.CINGULAR\"\r", 0};
static telitcmd atcgdcont = {atcgdcontstr, &ackOK};
static char *atstorestr[] = {"AT&W\r", 0};
static telitcmd atstore = {atstorestr, &ackOK};
static char *atconnectstr[] = {"AT#SGACT=1,1\r",0};
static telitcmd atconnect = {atconnectstr, &ackSGACT};
static char hostname[] = HOSTNAME;
static char hostport[] = HOSTPORT;
static char *atopenscktstr[] = {"AT#SD=1,0,",hostport,",\"",hostname,"\"\r",0};
static telitcmd atopensckt = {atopenscktstr, &ackSD};
static char postpath[] = POSTPATH;
static char requesthost[] = REQUESTHOST;
static char preamble[] = PREAMBLE;
static char moduleid[] = MODULEID;
static char *atpoststr[] = {"POST ", postpath, " HTTP/1.1\r\nhost: ", requesthost,
"\r\nTransfer-Encoding: chunked\r\n\r\n8\r\n", preamble,
moduleid, "\r\n", 0};
static telitcmd atpost = {atpoststr, &ackNoOp};
static char *atposttermstr[] = {"4\r\n\xFF\xFF\xFF\xFF\r\n0\r\n\r\n", 0};
static telitcmd atpostterm = {atposttermstr, &ackSend};
static char *atdisconnectstr[] = {"AT#SGACT=1,0\r",0};
static telitcmd atdisconnect = {atdisconnectstr, &ackOK};
static char *atclosestr[] = {"AT#SH=1\r",0};
static telitcmd atclose = {atclosestr, &ackOK};
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCIA0RX_ISR (void)
{
ucRXBuffer[ucWriteIndex++] = UCA0RXBUF; // store received byte and
ucWriteIndex &= 0x1f; // reset index
}
unsigned char isRXBufferEmpty (void) {
return (ucWriteIndex == ucReadIndex);
}
unsigned char getRXBufferChar (void) {
unsigned char Byte;
if (ucWriteIndex != ucReadIndex) { // char still available
Byte = ucRXBuffer[ucReadIndex++]; // get byte from buffer
ucReadIndex &= 0x1f; // adjust index
return (Byte);
} else return (0); // if there is no new char
}
void clearRXBuffer() {
IE2 &= ~UCA0RXIE; // Disable USCI_A0 RX interrupt
ucReadIndex=0;
ucWriteIndex = 0;
IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
}
void do_init() {
P4SEL |= 0x0C0;
// P4.7,6 = USCI_A0 RXD/TXD
UCA0CTL1 |= UCSWRST;
UCA0CTL1 |= UCSSEL_1; // CLK = ACLK
UCA0BR0 = 0x06; // 32k/9600 - 3.41
UCA0BR1 = 0x00; //
UCA0MCTL = UCBRS2 +UCBRS1+ UCBRS0; // Modulation
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
gprs_state = STATE_INIT;
}
void do_uninit() {
UCA0CTL1 |= UCSWRST;
gprs_state = STATE_UNINIT;
}
unsigned int do_power() {
setPwrBusStatus(PM_SYSPWR, PM_ENABLE);
__no_operation();
P8SEL &= 0x00;
P8OUT |= 0x02;
P8DIR |= 0x02;
P8DIR &= ~0x01;
// make sure uart pins are all low
P4SEL &= 0x3F; //00111111
P4OUT &= 0x3F; //00111111
P4DIR |= 0xC0; //11000000
if(!(P8IN & 0x01)) {
P8OUT &= 0xFD;
setTimeout(2);
__bis_SR_register(LPM3);
__no_operation();
P8OUT |= 0x02;
setTimeout(2);
__bis_SR_register(LPM3);
__no_operation();
if(!(P8IN & 0x01)) {
__no_operation();
return 1;
}
}
__no_operation();
gprs_state = STATE_PWRD;
return 0;
}
unsigned int do_unpower() {
P8OUT |= 0x02;
P8DIR |= 0x02;
P8DIR &= 0xFE;
// make sure uart pins are all low
P4SEL &= 0x3F; //00111111
P4OUT &= 0x3F; //00111111
P4DIR |= 0xC0; //11000000
if((P8IN & 0x01)) {
P8OUT &= 0xFD;
setTimeout(2);
__bis_SR_register(LPM3);
__no_operation();
P8OUT |= 0x02;
setTimeout(2);
__bis_SR_register(LPM3);
__no_operation();
if((P8IN & 0x01)) return 1;
}
setPwrBusStatus(PM_SYSPWR, PM_DISABLE);
gprs_state = STATE_UNINIT;
return 0;
}
unsigned int do_reset() {
telitcmd *cmds[10] = {&atecho, &atv, &atq, &atflowctl, &atsetusid, &atsetpass, &atcgdcont, &atstore, &atclose, &atdisconnect};
unsigned char pos = 0;
while(pos<10) sendCmd(*(cmds[pos++]));
gprs_state = STATE_INIT;
return 0;
}
unsigned int do_open() {
telitcmd *cmds[4] = {&at, &atconnect, &atopensckt, &atpost};
unsigned char pos = 0;
unsigned char retried = 0;
while(pos<4) {
if(sendCmd(*(cmds[pos++]))) {
if(retried) {
return 1;
} else {
do_reset();
pos=0;
retried = 1;
}
}
}
gprs_state = STATE_OPEN;
return 0;
}
unsigned int do_close() {
telitcmd *cmds[3] = {&atpostterm, &atdisconnect, &atclose};
unsigned char pos = 0;
while(pos<3) sendCmd(*(cmds[pos++]));
gprs_state = STATE_INIT;
return 0;
}
unsigned int gprsSend(unsigned char *data, unsigned int len) {
switch (gprs_state) {
case STATE_UNINIT:
if(do_power()) {
__no_operation();
return 1;
}
case STATE_PWRD:
do_init();
case STATE_INIT:
if(do_open()) {
__no_operation();
return 1;
}
default:
break;
}
return sendData(data, len);
}
unsigned int gprsClose() {
switch (gprs_state) {
case STATE_OPEN:
case STATE_ERROR:
do_close();
case STATE_INIT:
do_uninit();
case STATE_PWRD:
do_unpower();
case STATE_UNINIT:
default:
return 0;
}
}
unsigned int memcmp(char *a, char *b, unsigned int length) {
while(length>0) {
if(*a != *b) return 1;
length--;
a++;
b++;
}
return 0;
}
unsigned int ackOK(void) {
getResponse();
return memcmp(resp, "0", 2);
}
unsigned int ackSGACT(void) {
getResponse();
if(memcmp(resp, sgactt1, 1)) {
__no_operation();
return 1;
}
getResponse();
if(memcmp(resp, sgactt2, 2)) {
__no_operation();
return 1;
}
__no_operation();
return 0;
}
unsigned int ackSend(void) {
unsigned int isbody = 1;
while(isbody) {
getResponse();
isbody = !(memcmp(resp, "0", 2));
}
return 0;
}
unsigned int ackSD(void) {
getResponse();
return memcmp(resp, "1", 2);
}
unsigned int ackNoOp(void) {
return 0;
}
unsigned int sendData(unsigned char *data, unsigned int len) {
unsigned int pos = 0;
char lb[7];
sprintf(lb, "%x\r\n", (int)len);// get length in hex
while(lb[pos] != '\0') {
while(!(IFG2&UCA0TXIFG));
UCA0TXBUF = lb[pos++];
}
pos =0;
while(pos < len) {
while(!(IFG2&UCA0TXIFG));
UCA0TXBUF = data[pos];
pos++;
}
pos =0;
char eb[] = "\r\n";
while(eb[pos] != '\0') {
while(!(IFG2&UCA0TXIFG));
UCA0TXBUF = eb[pos];
pos++;
}
return 0;
}
unsigned int sendCmd(telitcmd tcmd) {
unsigned int ret;
unsigned int pos=0;
IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
clearRXBuffer();
while(*(tcmd.cmd+pos) != 0) {
char *c = *(tcmd.cmd+pos);
pos++;
while(*c!='\0') {
while(!(IFG2&UCA0TXIFG));
UCA0TXBUF = *c;
c++;
}
}
ret = tcmd.callback();
IE2 &= ~UCA0RXIE; // Enable USCI_A0 RX interrupt
return ret;
}
unsigned char getResponse() {
unsigned char ptr = 0;
setTimeout(5);
unsigned char c;
while(!timedout) {
if (!isRXBufferEmpty()) {
c = getRXBufferChar();
if(c=='\r' || ptr>38) {
*(resp+ptr) = '\0';
clearTimeout();
return 0;
} else *(resp+(ptr++)) = c;
}
}
clearTimeout();
return 1;
}