forked from jharwinbarrozo/Satnogs-CNC-Rotator
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwatchdog.h
More file actions
102 lines (94 loc) · 3.15 KB
/
Copy pathwatchdog.h
File metadata and controls
102 lines (94 loc) · 3.15 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
/*!
* @file watchdog.h
*
* It is a driver for watchdog timer.
*
* Licensed under the GPLv3
*
*/
#ifndef WATCHDOG_H_
#define WATCHDOG_H_
#include <avr/wdt.h>
#include "globals.h"
#include "easycomm.h"
#include "rotator_pins.h"
/**************************************************************************/
/*!
@brief Class that functions for interacting with a watchdog timer
*/
/**************************************************************************/
class wdt_timer{
public:
/**************************************************************************/
/*!
@brief Initialize watchdog timer to 2sec time out and to set up
interrupt routine
*/
/**************************************************************************/
void watchdog_init() {
cli();
wdt_reset();
WDTCSR |= _BV(WDCE) | _BV(WDE);
WDTCSR = _BV(WDIE) | _BV(WDE) | _BV(WDP3) | _BV(WDP2) | _BV(WDP1);
sei();
}
/**************************************************************************/
/*!
@brief Reset the watchdog timer
*/
/**************************************************************************/
void watchdog_reset() {
wdt_reset();
}
};
/**************************************************************************/
/*!
@brief Watchdog timer interrupt routine that implements a minimal
easycomm protocol to get the errors and reset the uC
*/
/**************************************************************************/
ISR(WDT_vect) {
// Disable motors
digitalWrite(MOTOR_EN, LOW);
// Set error
rotator.rotator_error = wdt_error;
rotator.rotator_status = error;
// Enable interrupts for serial communication
sei();
while (1) {
// Reset the watchdog timer because the interrupts are enabled
wdt_reset();
// Implement a minimal easycomm protocol to get the errors and reset uC
char buffer[BUFFER_SIZE];
char incomingByte;
static uint16_t BufferCnt = 0;
String str1, str2, str3, str4, str5, str6;
while (rs485.available() > 0) {
incomingByte = rs485.read();
if (incomingByte == '\n' || incomingByte == '\r') {
buffer[BufferCnt] = 0;
if (buffer[0] == 'G' && buffer[1] == 'S') {
str1 = String("GS");
str2 = String(rotator.rotator_status, DEC);
str3 = String("\n");
rs485.print(str1 + str2 + str3);
} else if (buffer[0] == 'G' && buffer[1] == 'E') {
str1 = String("GE");
str2 = String(rotator.rotator_error, DEC);
str3 = String("\n");
rs485.print(str1 + str2 + str3);
} else if (buffer[0] == 'R' && buffer[1] == 'B') {
while(1);
}
BufferCnt = 0;
rs485.flush();
} else {
buffer[BufferCnt] = incomingByte;
BufferCnt++;
}
}
// Reset the watchdog timer
wdt_reset();
}
}
#endif /* WATCHDOG_H_ */