satnogs-rotator-firmware
Loading...
Searching...
No Matches
watchdog.h
Go to the documentation of this file.
1
10#ifndef WATCHDOG_H_
11#define WATCHDOG_H_
12
13#include <Arduino.h>
14#if defined(__AVR__)
15#include <avr/wdt.h>
16#endif
17#include "globals.h"
18#include "easycomm.h"
19#include "rotator_pins.h"
20
21/**************************************************************************/
25/**************************************************************************/
27public:
28
29 /**************************************************************************/
34 /**************************************************************************/
36#if defined(__AVR__)
37 cli();
38 wdt_reset();
39#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
40 WDTCSR |= _BV(WDCE) | _BV(WDE);
41 WDTCSR = _BV(WDIE) | _BV(WDE) | _BV(WDP3) | _BV(WDP2) | _BV(WDP1);
42#endif
43 sei();
44#endif
45 }
46
47 /**************************************************************************/
51 /**************************************************************************/
53#if defined(__AVR__)
54 wdt_reset();
55#endif
56 }
57};
58
59/**************************************************************************/
64/**************************************************************************/
65#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
66ISR(WDT_vect) {
67 // Disable motors
68 digitalWrite(MOTOR_EN, LOW);
69 // Set error
72 // Enable interrupts for serial communication
73 sei();
74
75 while (1) {
76 // Reset the watchdog timer because the interrupts are enabled
77 wdt_reset();
78 // Implement a minimal easycomm protocol to get the errors and reset uC
79 char buffer[BUFFER_SIZE];
80 char incomingByte;
81 static uint16_t BufferCnt = 0;
82 String str1, str2, str3, str4, str5, str6;
83 while (rs485.available() > 0) {
84 incomingByte = rs485.read();
85 if (incomingByte == '\n' || incomingByte == '\r') {
86 buffer[BufferCnt] = 0;
87 if (buffer[0] == 'G' && buffer[1] == 'S') {
88 str1 = String("GS");
89 str2 = String(rotator.rotator_status, DEC);
90 str3 = String("\n");
91 rs485.print(str1 + str2 + str3);
92 } else if (buffer[0] == 'G' && buffer[1] == 'E') {
93 str1 = String("GE");
94 str2 = String(rotator.rotator_error, DEC);
95 str3 = String("\n");
96 rs485.print(str1 + str2 + str3);
97 } else if (buffer[0] == 'R' && buffer[1] == 'B') {
98 while(1);
99 }
100 BufferCnt = 0;
101 rs485.flush();
102 } else {
103 buffer[BufferCnt] = incomingByte;
104 BufferCnt++;
105 }
106 }
107 // Reset the watchdog timer
108 wdt_reset();
109 }
110}
111#endif
112
113#endif /* WATCHDOG_H_ */
Class that functions for interacting with a RS485 transceiver.
Definition rs485.h:25
uint8_t available(void)
The number of chars/uint8_t that are available in RS485 buffer.
Definition rs485.h:75
uint8_t read()
Read a char/uint8_t from RS485 bus.
Definition rs485.h:65
void print(String str)
Print a string to RS485 bus.
Definition rs485.h:52
void flush()
Waits for the transmission of outgoing serial data to complete.
Definition rs485.h:84
Class that functions for interacting with a watchdog timer.
Definition watchdog.h:26
void watchdog_reset()
Reset the watchdog timer.
Definition watchdog.h:52
void watchdog_init()
Initialize watchdog timer to 2sec time out and to set up interrupt routine.
Definition watchdog.h:35
#define BUFFER_SIZE
Set the size of serial buffer.
Definition easycomm.h:23
@ wdt_error
Definition globals.h:22
_rotator rotator
Definition globals.h:57
@ r_error
Definition globals.h:17
#define MOTOR_EN
Digital output, to enable the motors.
enum _rotator_error rotator_error
Rotator error.
Definition globals.h:42
enum _rotator_status rotator_status
Rotator status.
Definition globals.h:41