-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcom.c
More file actions
157 lines (132 loc) · 3.02 KB
/
com.c
File metadata and controls
157 lines (132 loc) · 3.02 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
#include <avr/io.h>
#include <stdio.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "groom/com.h"
#include "groom/usart_mux.h"
#include "groom/usart.h"
volatile int i=0;
volatile uint8_t buffer[20]; //buffer for data passing
volatile uint8_t StrRxFlag=0;
volatile uint8_t interruptstate=0; //interrupt state
volatile char c; // char for com protocol
/*
Sending data, enter DeviceSendID and string pointer.
Data will be sent as a string to the slave board and should end with \r
'5' is for slave alpha '6' is for beta
will return 1 if send successfully, 0 if fail
sample usage:
uint8_t status;
char control[50];
sprintf(control,"ASMDSFE");
status=com_sendata('5',control);
*/
uint8_t com_senddata(char DeviceSendID, char * data_send){
int count;
if (DeviceSendID==SEND_ALPHA) {
usart_mux_set(0);
}else if (DeviceSendID==SEND_BETA) {
usart_mux_set(1);
}
//_delay_ms(100);
interruptstate=COMMAND_MODE;
sei();//enable interrupt
usart_out(DeviceSendID);
//_delay_ms(100);
for(count=0;count<100;count++){
_delay_ms(10);
if (c==ACK) {
c=DEFAULT;
usart_outstring(data_send);
return 1;
}
}
c=DEFAULT;
cli();
return 0;
}
char* com_requestdata(char DeviceDATAID){
//set deviceID
int count=0;
if (DeviceDATAID==READ_ALPHA) {
usart_mux_set(0);
//PORTD &= ~(1 << PD2); // Not pressed, LED off select 0;
}else if (DeviceDATAID==READ_BETA) {
usart_mux_set(1);
//PORTD |= 1 << PD2; // select 1;
}
//_delay_ms(100);
interruptstate=TRANSMIT_MODE;
usart_out(DeviceDATAID);
sei();
while (1) {
_delay_ms(10);
count++;
if(StrRxFlag || count>100){ //time_out
if(StrRxFlag){
StrRxFlag=0; // Reset String received flag
count=0;
return buffer;
}else{
sprintf(buffer,"TIME_OUT");
count=0;
i=0; //Reset buffer index
interruptstate=COMMAND_MODE;
return buffer;
}
}
}
cli();
}
// heartbeat function
uint8_t com_heartbeat(char DeviceID){
int count;
//set deviceID
if (DeviceID==HB_ALPHA) {
usart_mux_set(0);
}else if (DeviceID==HB_BETA) {
usart_mux_set(1);
}
//_delay_ms(100);
interruptstate=COMMAND_MODE;
sei();//enable interrupt
usart_out(DeviceID);
for(count=0;count<100;count++){
_delay_ms(10);
if (c==ACTIVE_RESPONSE) {
c=DEFAULT;
return 1;
}
if (c==ACTIVE_RESPONSE_MOTION) {
c=DEFAULT;
return 2;
}
}
c=DEFAULT;
cli();
return 0;
}
//interrupt handler
ISR(USART_RX_vect)
{
switch (interruptstate) {
case COMMAND_MODE:
while (!(UCSR0A & (1<<RXC0)));
c=UDR0; //Read USART data register
break;
case TRANSMIT_MODE:
while (!(UCSR0A & (1<<RXC0)));
buffer[i]=UDR0; //Read USART data register
if(buffer[i++]=='\r') //check for carriage return terminator and increment buffer index
{
// if terminator detected
StrRxFlag=1; //Set String received flag
buffer[i-1]=0x00; //Set string terminator to 0x00
i=0; //Reset buffer index
interruptstate=COMMAND_MODE;
}
break;
default:
break;
}
}