-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.c
129 lines (104 loc) · 3.15 KB
/
link.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
#include "link.h"
extern QuadControl quad;
void UARTStringPut(uint32_t ui32Base, char * string) {
unsigned int ptr = 0;
while (*(string+ptr) != '\0') {
UARTCharPut(ui32Base, *(string+ptr));
ptr++;
}
}
void LinkInit() {
char receive;
// Configure UART1
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART1);
GPIOPinConfigure(GPIO_PC4_U1RX);
GPIOPinConfigure(GPIO_PC5_U1TX);
GPIOPinTypeUART(GPIO_PORTC_BASE, GPIO_PIN_4 | GPIO_PIN_5);
UARTClockSourceSet(UART1_BASE, UART_CLOCK_PIOSC);
UARTFIFODisable(UART1_BASE);
/*
* Configure Xbee Baud Rate at 9600
*/
UARTConfigSetExpClk(UART1_BASE, 16000000, 9600, UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE);
UARTEnable(UART1_BASE);
// Enter AT Mode
UARTStringPut(UART1_BASE, "+++");
SysCtlDelay((SysCtlClockGet() / 3000) * 2000);
UARTStringPut(UART1_BASE, "ATBD ");
UARTStringPut(UART1_BASE, LINK_BD);
UARTStringPut(UART1_BASE, "\r");
SysCtlDelay((SysCtlClockGet() / 3000) * 1000);
UARTStringPut(UART1_BASE, "ATWR\r");
SysCtlDelay((SysCtlClockGet() / 3000) * 1000);
UARTStringPut(UART1_BASE, "ATCN\r");
SysCtlDelay((SysCtlClockGet() / 3000) * 1000);
/*
* Configure Link at target baud rate
*/
UARTDisable(UART1_BASE);
UARTConfigSetExpClk(UART1_BASE, 16000000, 115200, UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE);
UARTEnable(UART1_BASE);
// Clear Buffer
while (UARTCharsAvail(UART1_BASE) == true) {
UARTCharGet(UART1_BASE);
}
// Enter AT Mode
do {
UARTStringPut(UART1_BASE, "+++");
SysCtlDelay((SysCtlClockGet() / 3000) * 2000);
receive = UARTCharGet(UART1_BASE);
} while (receive != 'O');
UARTCharGet(UART1_BASE); // Drop the K
UARTCharGet(UART1_BASE); // Drop the CR
// Set PAN ID
do {
UARTStringPut(UART1_BASE, "ATID ");
UARTStringPut(UART1_BASE, LINK_ID);
UARTStringPut(UART1_BASE, "\r");
receive = UARTCharGet(UART1_BASE);
} while (receive != 'O');
UARTCharGet(UART1_BASE); // Drop the K
UARTCharGet(UART1_BASE); // Drop the CR
// Set Local Address
do {
UARTStringPut(UART1_BASE, "ATMY ");
UARTStringPut(UART1_BASE, LINK_MY);
UARTStringPut(UART1_BASE, "\r");
receive = UARTCharGet(UART1_BASE);
} while (receive != 'O');
UARTCharGet(UART1_BASE); // Drop the K
UARTCharGet(UART1_BASE); // Drop the CR
// Set Remote Address
do {
UARTStringPut(UART1_BASE, "ATDL ");
UARTStringPut(UART1_BASE, LINK_DL);
UARTStringPut(UART1_BASE, "\r");
receive = UARTCharGet(UART1_BASE);
} while (receive != 'O');
UARTCharGet(UART1_BASE); // Drop the K
UARTCharGet(UART1_BASE); // Drop the CR
// Write Configuration
do {
UARTStringPut(UART1_BASE, "ATWR\r");
receive = UARTCharGet(UART1_BASE);
} while (receive != 'O');
UARTCharGet(UART1_BASE); // Drop the K
UARTCharGet(UART1_BASE); // Drop the CR
// Close
do {
UARTStringPut(UART1_BASE, "ATCN\r");
receive = UARTCharGet(UART1_BASE);
} while (receive != 'O');
UARTCharGet(UART1_BASE); // Drop the K
UARTCharGet(UART1_BASE); // Drop the CR
}
void LinkSend() {
// Signature
UARTCharPut(UART1_BASE, 0xF0);
// Send Data
int i;
for (i = 0; i < sizeof(QuadControl); i++) {
UARTCharPut(UART1_BASE, *((unsigned char *) &quad + i));
}
}