summaryrefslogtreecommitdiffstats
path: root/firmware/util.c
blob: 9aa5d13dda184a81395e076f5473456a1f272bbf (plain)
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
/*
 *   OpenPSU firmware
 *   Utility functions
 *
 *   Copyright (C) 2007 Michael Buesch <m@bues.ch>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation; either version 2
 *   of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 */

#include "util.h"
#include "calibration.h"
#include "main.h"
#include "lcd.h"

#include <avr/io.h>
#include <avr/eeprom.h>
#include <avr/sleep.h>
#include <avr/wdt.h>


void panic(const char PROGPTR *msg)
{
	irq_disable();
	wdt_disable();
	emergency_shutdown();

	lcd_clear_buffer();
	lcd_put_str("Whee! PANIC :(\n");
	lcd_put_pstr(msg);
	lcd_commit();

	infinite_sleep();
}

void reboot(void)
{
	irq_disable();
	/* Enable the WDT in system reset mode. */
	wdt_enable(WDTO_15MS);
	while (1);
}

void infinite_sleep(void)
{
	irq_disable();
	wdt_disable();
	set_sleep_mode(SLEEP_MODE_PWR_DOWN);
	while (1)
		sleep_mode();
}

ISR(WDT_vect)
{
	panic(PSTR("watchdog"));
}

void wdt_enable_irq_mode(uint8_t timeout)
{
	uint8_t sreg;

	sreg = irq_disable_save();
	wdt_reset();
	__asm__ __volatile__(
		"sts %0, %1	\n\t"
		"sts %0, %2	\n\t"
		: /* none */
		: "M" (_SFR_MEM_ADDR(WDTCSR))
		, "r" (1 << WDCE)
		, "r" ((1 << WDIE) | (1 << WDIF) | timeout)
	);
	irq_restore(sreg);
}
bues.ch cgit interface