-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathar_wd.c
192 lines (148 loc) · 4.49 KB
/
ar_wd.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
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <util/delay.h>
#include <stdio.h>
#include "uart.h"
#include "ring_buffer/ring_buffer.h"
#define DEFAULT_COUNTER_VALUE 300 //5 mins should be enough for fsck to finish
#define MINIMAL_COUNTER_VALUE 60 //protect against 'set 1' and similar
#define PING_LED PORTB1
#define WARNING_LED PORTB4
#define RELAY PORTB2
#define BUZZER PORTB3
#define HEARTBEAT_LED PORTB5 //yellow arduino led
#define DEFAULT_FREQ_2400 104 //2400 Hz from 16Mhz input with prescaler = 64
#define DEFAULT_FREQ_3000 83
#define UART_FIFO_SIZE 64
//FIXME: this should be calculated from F_CPU
#define FREQ_1S 15624 //1 second for 16MHz and prescaler=1024
#define PING_CMD "ping"
#define SHOW_CMD "show"
#define SET_CMD "set"
#define HELP_CMD "help"
const char help_reply[] = {"help - this help\r\nping - reset timer\r\nshow - show maximum timer value\r\nset <value> - set maximum timer value\r\n"};
volatile unsigned long int counter=DEFAULT_COUNTER_VALUE; //current counter, decreases every second in COMP_A ISR
unsigned long int max_counter_value=DEFAULT_COUNTER_VALUE; //user-adjustable maximum counter value
FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
FILE uart_input = FDEV_SETUP_STREAM(NULL, uart_getchar, _FDEV_SETUP_READ);
unsigned char uart_recv_buffer [UART_FIFO_SIZE+1];
struct buf_info uart_rx;
int main (void)
{
wdt_enable(WDTO_2S);
uart_init();
stdout=&uart_output;
stdin=&uart_input;
//command prompt
putchar ('>');
rb_init (&uart_rx, uart_recv_buffer, UART_FIFO_SIZE+1);
//enable uart interrupts
UCSR0B |= _BV(RXCIE0);
/* set pins of PORTB for output*/
DDRB |= _BV(DDB5) | _BV(DDB1) | _BV (DDB4) | _BV(DDB2) | _BV (DDB3);
//enable timer for counting seconds
TCCR1B = _BV (WGM12); //CTC enable
TCCR1B |= _BV(CS10) | _BV(CS12); //prescaler=1024
TIMSK1 |= _BV (OCIE1A); //enable interrupt
OCR1A = FREQ_1S;
sei(); //enable interrupts
for (;;)
{
wdt_reset();
sleep_mode();
}
return 0;
}
ISR (TIMER1_COMPA_vect)
{
PORTB ^= _BV(HEARTBEAT_LED);
counter--;
if (counter == 0)
{
//light up warning led
PORTB |= _BV(WARNING_LED);
//do reset
PORTB |= _BV(RELAY);
//enable timer for buzzer
TCCR2A = _BV (WGM21); //CTC mode
TCCR2A |= _BV (COM2A0); //toggle OC2A on compare match
TCCR2B = _BV (CS22); //prescaler = 64
OCR2A = DEFAULT_FREQ_3000;
//disable all interrupts
//cli();
_delay_ms (500);
OCR2A = DEFAULT_FREQ_2400;
_delay_ms (500);
//enable interrupts
//sei();
//reset is done
PORTB &= ~_BV(WARNING_LED);
PORTB &= ~_BV(RELAY);
//disable buzzer
TCCR2B = 0;
TCCR2A = 0;
//reset counter
counter = DEFAULT_COUNTER_VALUE; //reset to default value to allow PC to boot successfully
}
}
ISR (USART_RX_vect)
{
char recv = UDR0;
//echo
putchar(recv);
rb_write(&uart_rx, recv);
if (recv == '\r')
{
putchar('\n');
//process command
char cmd [UART_FIFO_SIZE+1];
int npos = 0;
int chr = rb_read (&uart_rx);
while (chr != EEMPTY)
{
cmd[npos++]=(char) chr;
chr = rb_read(&uart_rx);
}
//strip \r\n at the end
while (cmd[npos-1] == '\r' || cmd [npos-1] == '\n')
{
cmd[--npos]=0;
}
//printf ("npos=%d, str='%s'\r\n", npos, cmd);
if (npos > 0)
{
if (strncmp (cmd, PING_CMD, npos) == 0)
{
PORTB |= _BV (PING_LED);
counter=max_counter_value;
printf ("resetting timer to %d\r\n", max_counter_value);
PORTB &= ~_BV(PING_LED);
} else if (strncmp (cmd, SHOW_CMD, npos) == 0)
{
printf ("max_counter_value is %d\r\n", max_counter_value);
} else if (strncmp (cmd, HELP_CMD, npos) == 0)
{
puts (help_reply);
} else if ((npos >= sizeof(SET_CMD)-1) && (strncmp (cmd, SET_CMD, sizeof (SET_CMD)-1) == 0))
{
char *endptr=0;
unsigned long int new_cval = strtoul (cmd+sizeof(SET_CMD)-1, &endptr, 10);
//printf ("*endptr = %hhu\n", *endptr);
if (new_cval == 0)
{
printf ("error parsing number at pos %d (symbol='%c')\r\n", endptr - cmd, *endptr);
} else
{
max_counter_value=new_cval < MINIMAL_COUNTER_VALUE? MINIMAL_COUNTER_VALUE: new_cval;
printf ("setting max_counter_value to %lu\r\n", max_counter_value);
}
} else
{
printf ("unknown command: '%s' - try 'help'\r\n",cmd);
}
}
putchar ('>');
}
}