This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_tiva.c
304 lines (276 loc) · 9.93 KB
/
main_tiva.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
//v2 not have ring buffer
#include "main.h"
#include "app/inc/init.h"
// first connect to raspberry, then allow tiva send data to Raspberry
const int Address = 0x10; // lora address
volatile bool on_connect = false;
bool flagLed1 = false;
bool flagLed2 = false;
uint8_t array_connect[7];
uint8_t array_send[7] = {0};
volatile bool alarmLed1 = false;
volatile bool alarmLed2 = false;
volatile uint32_t timerLed1 = 0;
volatile uint32_t timerLed2 = 0;
typedef struct {
bool dht11;
}FLAG;
FLAG flag;
typedef enum {
CONNECT,
GETDATA,
CHECKCRC,
CHECKFRAME} STATE;
STATE state = CONNECT; // first state of state machine
/* define data for ring buffer*/
RINGBUF UART0_RxRingBuff;
#define SIZE 100
uint8_t buffer[SIZE] = {0}; // buffer store data
void UART1_Handler(void) //uart receive timeout 0.1s
{
uint32_t ui32Status;
volatile uint8_t data;
ui32Status = UARTIntStatus(UART1_BASE, true); //get interrupt status
UARTIntClear(UART1_BASE, ui32Status); //clear the asserted interrupts
while(UARTCharsAvail(UART1_BASE)) //loop while there are chars
{
data = UARTCharGetNonBlocking(UART1_BASE);
RINGBUF_Put(&UART0_RxRingBuff, data);
}
}
void SysTick_Handler(void) //systick timer use for check flag and keep_alive message
{
static uint32_t tick = 60;
tick--;
if(tick%3 == 0)
flag.dht11 = true; // ngat la I, bat co dht11
if (tick == 0)
tick = 60;
}
//****************Timer 2 cound down connect to raspberry
void TIMER2A_Handler(void)
{
TimerIntClear(TIMER2_BASE, TIMER_TIMA_TIMEOUT);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1,~GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_1));
uart_send_command(Address,array_connect,0x28,0xFF,0xFF); //send connect message
}
//****************Timer 0 cound down LED 1
void TIMER3A_Handler(void)
{
TimerIntClear(TIMER3_BASE, TIMER_TIMA_TIMEOUT);
timerLed1--;
uart_send_command(Address,array_send,0x29,0x00,timerLed1);
if (timerLed1 == 0)
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);
uart_send_command(Address,array_send,0x21,0x00,0x00);
flagLed1 = false;
TimerDisable(TIMER3_BASE, TIMER_A);
}
}
//****************Timer 1 cound down LED 2
void TIMER1A_Handler(void)
{
TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
timerLed2--;
uart_send_command(Address,array_send,0x30,0x00,timerLed2);
if (timerLed2 == 0)
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0);
uart_send_command(Address,array_send,0x22,0x00,0x00);
flagLed2 = false;
TimerDisable(TIMER1_BASE, TIMER_A);
}
}
/*
void WDT0_Handler(void){ // Watchdog timer interrupt and reset MCU
WatchdogIntClear(WATCHDOG0_BASE);
} */
/*******************State machine
1: get data from ring buff
2: check data and calculate CRC
3: CRC true, check byte in frame
*/
int uart_get(uint8_t *msg);
void check_frame(uint8_t *array);
int main(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_PLL |
SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); //setup clock 50Mhz
// configure ringbufffer
RINGBUF_Init(&UART0_RxRingBuff, buffer, SIZE);
init_gpio();
init_uart();
init_timer();
init_adc();
SysTickPeriodSet(SysCtlClockGet()); //interrupt every 1s
// interrupt priority
IntPrioritySet(INT_UART1, 0x00);
IntPrioritySet(INT_TIMER2A, 0x20);
IntPrioritySet(INT_TIMER1A, 0x40);
IntPrioritySet(INT_TIMER3A, 0x60);
SysTickIntEnable();
IntMasterEnable(); // enable global interrupt
uint16_t crc_test; // crc check
uint8_t array[20]; // array to store data which get get from ringbuffer
uint32_t temp, humidity;
uint16_t current;
uint8_t currentHigh;
uint8_t currentLow;
while (1)
{
switch (state)
{
case CONNECT:
TimerEnable(TIMER2_BASE, TIMER_A); // wait 5s for timeout;
state = GETDATA;
break;
case GETDATA:
if (uart_get(array) == 1) // if receive '\n' end string, transfer to next state CHECKCRC
{
state = CHECKCRC;
break;
}
break;
case CHECKCRC:
crc_test = crc_16(array,5);
if (crc_test != 0)
{//error receive
uart_send_command(Address,array_send,0x26,0xFF,0xFF);
memset (array,0,10);
state = GETDATA;
break;
}
else
{ // receive success, crc_test=0
state = CHECKFRAME;
break;
}
case CHECKFRAME:
check_frame(array);
memset (array,0,10);
state = GETDATA;
break;
}
if (on_connect)
{
// send data every 5s
if( flag.dht11 == true) // neu co bat. lau du lieu va gui ve raspberry
{
ReadDHT(&temp, &humidity);
uart_send_command(Address,array_send,0x25,0x00,humidity);
SysCtlDelay(SysCtlClockGet()/30000);
uart_send_command(Address,array_send,0x24,0x00,temp);
flag.dht11 = false;
if (flagLed1 || flagLed2)
{
current = getAmpere();
if (current < 255)
uart_send_command(Address,array_send,0x23,0x00,current);
else
{
currentHigh = current >> 8;
currentLow = current;
uart_send_command(Address,array_send,0x23,currentHigh,currentLow);
}
}
}
}
}
}
///////////////////////////////////////////////////////////////////
//
//FUNCTION -----------------------------
//
//
int uart_get(uint8_t *msg)
{
uint8_t get;
static uint16_t index = 0;
while (RINGBUF_Get(&UART0_RxRingBuff, &get))
{
if (get == 0x0A)//receice end of string
{
index = 0;
return 1;
}
else
{
*(msg+index) = get;
index++;
}
}
return 0;
}
void check_frame(uint8_t *array )
{
uint8_t *ptr;
ptr = array;
//// check address and implement command
if (*ptr == Address) // kiem tra dia chi
{
switch (*(ptr+1)) // kiem tra yeu cau
{
case 0x21: //led 1
if(*(ptr+2) == 0x11) //on led 1
{
flagLed1 = true;
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);
if (alarmLed1)
{
alarmLed1 = false;
TimerEnable(TIMER3_BASE, TIMER_A);
}
uart_send_command(Address,array_send,0x21,0x00,0x11); // gui cho raspberry biet da hoan tat
}
else if (*(ptr+2) == 0x00) //led off
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);
uart_send_command(Address,array_send,0x21,0x00,0x00);
flagLed1 = false;
TimerDisable(TIMER3_BASE, TIMER_A);
}
break;
case 0x22: //led 2
if(*(ptr+2) == 0x11) //on led 2
{
flagLed2 = true;
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, GPIO_PIN_3);
if (alarmLed2)
{
alarmLed2 = false;
TimerEnable(TIMER1_BASE, TIMER_A);
}
uart_send_command(Address, array_send,0x22,0x00,0x11);
}
else if (*(ptr+2) == 0x00)//led off
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0);
uart_send_command(Address,array_send,0x22,0x00,0x00);
flagLed2 = false;
TimerDisable(TIMER1_BASE, TIMER_A);
}
break;
case 0x27:
uart_send_command(Address,array_send,0x27,0xFF,0xFF);
break;
case 0x28:
on_connect = true;
TimerDisable(TIMER2_BASE, TIMER_A);
SysTickEnable();
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1,0);
//uart_send_string("connect success");
break;
case 0x29:
alarmLed1 = true;
timerLed1 = *(ptr+2);
uart_send_command(Address,array_send,0x29,0x00,timerLed1);
break;
case 0x30:
alarmLed2 = true;
timerLed2 = *(ptr+2);
uart_send_command(Address,array_send,0x30,0x00,timerLed2);
break;
}
}
}