-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions.c
649 lines (519 loc) · 19.5 KB
/
Functions.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/*
* Functions.c
*
* Created on: 3 de des. 2018
* Author: arnaudeveloper
* Email: developerarnau@gmail.com
* License: GNU General Public License v3.0
* https://github.com/arnaudeveloper/msp430-HM10
*/
#include <msp430.h>
#include <string.h>
#include "Functions.h"
/* AT_Commands library*/
#include "AT_Commands.h"
/*
* init_UART()
*/
void init_UART()
{
P3SEL = BIT3+BIT4; //P3.4 RX ; P3.3 TX
UCA0CTL1 |= UCSWRST;
UCA0CTL1 |= UCSSEL_1;
UCA0BR0 = 0x03;
UCA0BR1 = 0x00;
UCA0MCTL = UCBRS_3+UCBRF_0;
UCA0CTL1 &= ~UCSWRST;
UCA0IE |= UCRXIE;
}
/*
* init_Timer()
*/
void init_Timer()
{
TA0CCTL0 &= ~CCIE; // CCR0 interrupt disabled
TA0CCR0 = 12800;
TA0CTL = TASSEL_1 + MC_1 + TACLR; // ACLK, up mode, clear TAR
}
/*
* init_GPIOs()
*/
void init_GPIOs()
{
//LED1
P1DIR |= BIT0; // P1.0 set as output
P1OUT |= BIT0; // P1.0 ON
//LED2
P4DIR |= BIT7; // P4.7 set as output
P4OUT &= ~BIT7; // P4.7 OFF
//BUTTON 1
P1REN |= BIT1; // Enable internal resistors for P1.1
P1OUT |= BIT1; // Set pull-up for P1.1
P1IE |= BIT1; // Enable interrupts for P1.1
P1IES |= BIT1; // Set interrupt to falling flank for P1.1.
P1IFG &= ~BIT1; // Set to zero button flag
//BUTTON 2
P2REN |= BIT1; // Enable internal resistors for P2.1
P2OUT |= BIT1; // Set pull-up for P2.1
P2IE |= BIT1; // Enable interrupts for P2.1
P2IES |= BIT1; // Set interrupt to falling flank for P2.1.
P2IFG &= ~BIT1; // Set to zero button flag
}
/*
* config_INITIAL()
* This functions is used to establish the default configuration of the HM-10,
* set the Notifications ON and get the owner MAC address.
* AT commands for config_INITIAL are:
* 1. AT_RENEW
* 2. AT_RESET
* 3. AT_NOTI1
* 4. AT_ADDR?
*/
void config_INITIAL()
{
dis_ok=FALSE;
get_address=FALSE;
enviar = FALSE;
estat=0;
lost= FALSE;
connection= FALSE;
pointer = &word_check[0];
i=j=k=u=0;
contador = 0;
match=0;
/* AT command */
while(match==0)
{
n_letters=AT_2(pointer);
TA0CCTL0 = CCIE; // Start Timer
__bis_SR_register(LPM3_bits); // Enter LPM
}
__delay_cycles(10000);
match=0;
/* RENEW command */
while(match==0)
{
n_letters=AT_RENEW2(pointer);
TA0CCTL0 = CCIE; // Start Timer
__bis_SR_register(LPM3_bits); // Enter LPM
}
__delay_cycles(10000);
match=0;
/* RESET command */
while(match==0)
{
n_letters=AT_RESET2(pointer);
TA0CCTL0 = CCIE; // Start Timer
__bis_SR_register(LPM3_bits); // Enter LPM
}
__delay_cycles(10000);
match=0;
/* AT command*/
while(match==0)
{
n_letters=AT_2(pointer);
TA0CCTL0 = CCIE; // Start Timer
__bis_SR_register(LPM3_bits); // Enter LPM
}
__delay_cycles(10000);
match=0;
/* NOTI command */
while(match==0)
{
n_letters=AT_NOTI(pointer);
TA0CCTL0 = CCIE; // Start Timer
__bis_SR_register(LPM3_bits); // Enter LPM
}
__delay_cycles(10000);
match=0;
/* AT command*/
while(match==0)
{
n_letters=AT_2(pointer);
TA0CCTL0 = CCIE; // Start Timer
__bis_SR_register(LPM3_bits); // Enter LPM0
}
__delay_cycles(10000);
match=0;
/* ADDR command */
while(match==0)
{
__delay_cycles(10000);
n_letters=AT_ADDR(pointer);
TA0CCTL0 = CCIE; // Start Timer
__bis_SR_register(LPM3_bits); // Enter LPM0
}
__delay_cycles(10000);
}
/*
* config_DISC()
* This function is used to discover the master.
* 1st. Configure module as role master.
* 2nd. Discovery all devices in a range.
* 3rd. Try to connect to each device and ask if it is a master
* 4th. If it's a master save MAC address
* 5th. Cut off communication
*
*/
void config_DISC()
{
char x; //Counter loop "for"
pointer = &word_check[0];
/* Reset all address */
memset(&address2,0, sizeof address2);
memset(&address3,0, sizeof address3);
memset(&address4,0, sizeof address4);
memset(&address_M,0, sizeof address_M);
__delay_cycles(10000); // Used to pause the data streaming and give enough time to process data
match=0; // Set match = 0
/* Set IMME=1. See datasheet */
while(match==0) // Resend the command if the communication fail
{
n_letters=AT_IMME2(pointer); // AT_IMME command
TA0CCTL0 = CCIE; // Start Timer
__bis_SR_register(LPM3_bits); // Enter LPM0
}
__delay_cycles(10000); // Used to pause the data streaming and give enough time to process data
match=0; // Set match = 0
/* Set ROLE=1. See datasheet */
while(match==0) // Resend the command if the communication fail
{
n_letters=AT_ROLE2(pointer); // AT_ROLE command
TA0CCTL0 = CCIE; // Start Timer
__bis_SR_register(LPM3_bits); // Enter LPM0
}
__delay_cycles(10000); // Used to pause the data streaming and give enough time to process data
match=0; // Set match = 0
/* The following lines are used to launch the AT_DISC? command
* In that case we disable the timer and the detection of OK,
* in order to get improve the response detection.
* This command has a long response, so timer will interrupt
* and resend the command
*/
TA0CCTL0 &= ~CCIE; // CCR0 interrupt disabled
dis_ok=TRUE; // Disabled OK detection
while(match==0 || get_address==0) // Resend the command if the communication fail
{
__delay_cycles(1000000); // Used to pause the data streaming and give enough time to process data
n_letters=AT_DISC(pointer); // AT_DISC? command
// TA0CCTL0 = CCIE; //Start Timer
__bis_SR_register(LPM3_bits); // Enter LPM0
}
__delay_cycles(5000000); // Used to pause the data streaming and give enough time to process data
/* In order to use AT_CONN command. See datasheet */
// match=0; // Set match = 0
// for(x=contador;x>0;x--)
// {
// while(match==0) // Resend the command is the communication fail
// {
//
// n_letters= AT_CONN(pointer,x-1); //AT_CONN command
// TA0CCTL0 = CCIE; //Start Timer
// __bis_SR_register(LPM3_bits); // Enter LPM0
// }
// __delay_cycles(500000); // Used to pause the data streaming and give enough time to process data
//
// SEND();
// __delay_cycles(500000); // Used to pause the data streaming and give enough time to process data
//
// n_letters= AT_2(pointer); //Used to cut off communication
// }
/* The following lines are used to connect and check if the module is a master */
/* Start trying connect to every device */
dis_ok=FALSE; // Enable OK detection
match=FALSE; // Set match = 0
x=0;
connection = FALSE;
lost=FALSE;
master_detected= FALSE;
__delay_cycles(500000); // Used to pause the data streaming and give enough time to process data
/* Set AT_CON*/
while(match==0) // Resend the command is the communication fail
{
n_letters= AT_CON(pointer,address2); // AT_CON0 command. Connect to addres2
TA0CCTL0 = CCIE; // Start Timer
__bis_SR_register(LPM3_bits); // Enter LPM0
if(x>10)match=1; // Used for bad communications
x++;
}
__delay_cycles(500000); // Used to pause the data streaming and give enough time to process data
/* If connection = TRUE, a connection has been established*/
if(connection)
{
/* Initiate the protocol to know if we have connected to a correct module */
x=0;
match=0;
while(match==0)
{
SEND(); // Send ACK
TA0CCTL0 = CCIE; // Start Timer
__bis_SR_register(LPM3_bits);
if(x>10)match=1; // Used for bad communications
x++;
}
/* If we have connected to the correct module x<10 */
if(x>10)
{
/* We have connected to an incorrect module. Therefore, disconnect */
match=0;
while(match==0 && lost==0)
{
pointer = &word_check[0];
n_letters=AT_2(pointer);
TA0CCTL0 = CCIE;
__bis_SR_register(LPM3_bits);
}
/* Sometimes we receive OK before LOST.
* If we receive OK this indicates that the connection has been cut,
* but Green LED will be TURN-ON, so TURN-OFF
*/
P4OUT &= ~BIT7; //Green LED OFF
}
else
{
/* If it is a master, save the address*/
if(master_detected)
{
memcpy(address_M, address2,12);
}
while(lost==0) // Resend the command is the communication fail
{
pointer = &word_check[0];
__delay_cycles(500000); // Used to pause the data streaming and give enough time to process data
n_letters= AT_2(pointer); // Used to cut off communication
TA0CCTL0 = CCIE; // Start Timer
__bis_SR_register(LPM3_bits); // Enter LPM0
}
}
}// End of 1st address
match=0; // Set match = 0
x=0;
connection = FALSE;
lost=0;
__delay_cycles(500000); // Used to pause the data streaming and give enough time to process data
/* Set AT_CON*/
while(match==0) // Resend the command is the communication fail
{
n_letters= AT_CON(pointer,address3); // AT_CON0 command. Connect to addres2
TA0CCTL0 = CCIE; // Start Timer
__bis_SR_register(LPM3_bits); // Enter LPM0
if(x>10)match=1; // Used for bad communications
x++;
}
__delay_cycles(500000); // Used to pause the data streaming and give enough time to process data
if(connection)
{
x=0;
match=0;
while(match==0)
{
SEND();
TA0CCTL0 = CCIE;
__bis_SR_register(LPM3_bits);
if(x>10)match=1; // Used for bad communications
x++;
}
if(x>10)
{
match=0;
while(match==0 && lost==0)
{
pointer = &word_check[0];
n_letters=AT_2(pointer);
TA0CCTL0 = CCIE; // Start el Timer
__bis_SR_register(LPM3_bits); // Enter LPM0
}
P4OUT &= ~BIT7; //Green LED OFF
}
else
{
if(master_detected)
{
memcpy(address_M, address3,12);
}
while(lost==0) // Resend the command if the communication fail
{
pointer = &word_check[0];
__delay_cycles(500000); // Used to pause the data streaming and give enough time to process data
n_letters= AT_2(pointer); // Used to cut off communication
TA0CCTL0 = CCIE; // Start Timer
__bis_SR_register(LPM3_bits); // Enter LPM0
}
}
}// End of 2nd address
match=0; // Set match = 0
x=0;
connection = FALSE;
lost=0;
__delay_cycles(500000); // Used to pause the data streaming and give enough time to process data
while(match==0) // Resend the command is the communication fail
{
n_letters= AT_CON(pointer,address4); // AT_CON0 command. Connect to addres4
TA0CCTL0 = CCIE; // Start Timer
__bis_SR_register(LPM3_bits); // Enter LPM0
if(x>10)match=1; // Used for bad communications
x++;
}
__delay_cycles(500000); // Used to pause the data streaming and give enough time to process data
if(connection)
{
x=0;
match=0;
while(match==0)
{
SEND();
TA0CCTL0 = CCIE;
__bis_SR_register(LPM3_bits);
if(x>10)match=1; // Used for bad communications
x++;
}
if(x>10)
{
match=0;
while(match==0 && lost==0)
{
pointer = &word_check[0];
n_letters=AT_2(pointer);
TA0CCTL0 = CCIE; // Start el Timer
__bis_SR_register(LPM3_bits); // Enter LPM0
}
P4OUT &= ~BIT7; //Green LED OFF
}
else
{
if(master_detected)
{
memcpy(address_M, address4,12);
}
while(lost==0) // Resend the command if the communication fail
{
pointer = &word_check[0];
__delay_cycles(500000); // Used to pause the data streaming and give enough time to process data
n_letters= AT_2(pointer); // Used to cut off communication
TA0CCTL0 = CCIE; // Start Timer
__bis_SR_register(LPM3_bits); // Enter LPM0
}
}
}//End of 3rd address
}//End of config_DISC()
/*
* connect_ARDU()
*/
void connect_ARDU()
{
char x; //Counter loop "for"
char address_static[12]={'6','0','6','4','0','5','C','F','C','D','4','F'};
pointer = &word_check[0];
__delay_cycles(10000); // Used to pause the data streaming and give enough time to process data
match=0; // Set match = 0
/* Set IMME=1. See datasheet */
while(match==0) // Resend the command if the communication fail
{
n_letters=AT_IMME2(pointer); // AT_IMME command
TA0CCTL0 = CCIE; // Start Timer
__bis_SR_register(LPM3_bits); // Enter LPM0
}
__delay_cycles(10000); // Used to pause the data streaming and give enough time to process data
match=0; // Set match = 0
/* Set ROLE=1. See datasheet */
while(match==0) // Resend the command if the communication fail
{
n_letters=AT_ROLE2(pointer); // AT_ROLE command
TA0CCTL0 = CCIE; // Start Timer
__bis_SR_register(LPM3_bits); // Enter LPM0
}
__delay_cycles(10000); // Used to pause the data streaming and give enough time to process data
/* The following lines ara used to launch the AT_DISC? command
* In that case we disable the timer and the detection of OK,
* in order to get improve the response detection.
* This command has a long response, so timer will interrupt
* and resend the command
*/
TA0CCTL0 &= ~CCIE; // CCR0 interrupt disabled
dis_ok=TRUE; // Disabled OK detection
while(match==0 || get_address==0) // Resend the command if the communication fail
{
__delay_cycles(1000000); // Used to pause the data streaming and give enough time to process data
n_letters=AT_DISC(pointer); // AT_DISC? command
// TA0CCTL0 = CCIE; //Start Timer
__bis_SR_register(LPM3_bits); // Enter LPM0
}
__delay_cycles(5000000); // Used to pause the data streaming and give enough time to process data
dis_ok=FALSE; // Enable OK detection
match=FALSE; // Set match = 0
x=0;
connection = FALSE;
lost=FALSE;
master_detected= FALSE;
__delay_cycles(500000); // Used to pause the data streaming and give enough time to process data
while(match==0) // Resend the command is the communication fail
{
n_letters= AT_CON(pointer,address_static); // AT_CON0 command. Connect to addres4
TA0CCTL0 = CCIE; // Start Timer
__bis_SR_register(LPM3_bits); // Enter LPM0
if(x>10)match=1; // Used for bad communications
x++;
}
__delay_cycles(500000); // Used to pause the data streaming and give enough time to process data
}//End of connect_ARDU()
/*
* TxUAC0(byte TXData)
*/
void TxUAC0(byte TXData)
{
while (!(UCA0IFG & UCTXIFG)); //Wait until TX buffer will be ready
UCA0TXBUF = TXData;
}
/*
* SEND()
*/
void SEND()
{
byte bCount,bPacketLength;
byte TxBuffer[15]={'#','?','M'};
memcpy(TxBuffer+3,address1,12); //Send the address
bPacketLength = sizeof(TxBuffer);
for(bCount=0; bCount<bPacketLength; bCount++)
{
TxUAC0(TxBuffer[bCount]);
}
/* Wait until last byte has been transmitted*/
while(UCA1STAT & UCBUSY);
}
void send_rol()
{
byte bCount,bPacketLength;
byte TxBuffer[15]={'#','!','M'};
memcpy(TxBuffer+3,address1,12); //Send address
bPacketLength = sizeof(TxBuffer);
for(bCount=0; bCount<bPacketLength; bCount++)
{
TxUAC0(TxBuffer[bCount]);
}
/* Wait until last byte has been transmitted*/
while(UCA1STAT & UCBUSY);
}
void send_ack()
{
byte bCount,bPacketLength;
byte TxBuffer[15]={'#','!'};
bPacketLength = sizeof(TxBuffer);
for(bCount=0; bCount<bPacketLength; bCount++)
{
TxUAC0(TxBuffer[bCount]);
}
/* Wait until last byte has been transmitted*/
while(UCA1STAT & UCBUSY);
}
void send_hello()
{
byte bCount,bPacketLength;
byte TxBuffer[15]={'#','H','E','L','L','O'};
bPacketLength = sizeof(TxBuffer);
for(bCount=0; bCount<bPacketLength; bCount++)
{
TxUAC0(TxBuffer[bCount]);
}
/* Wait until last byte has been transmitted*/
while(UCA1STAT & UCBUSY);
}