summaryrefslogtreecommitdiffstats
path: root/m168_firmware/util.c
blob: 994f71b424bfe5a566e4e96c92590a683edf8fc7 (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
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
/*
 *   Wi-Fi Detector opensource firmware
 *   Utility functions
 *
 *   Copyright (C) 2007 Michael Buesch <mb@bu3sch.de>
 *
 *   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>


void mdelay(uint16_t msecs)
{
	uint8_t timer, i;

	TCCR0B = DELAY_1MS_TIMERFREQ;
	do {
		/* Delay one millisecond */
		for (i = DELAY_1MS_LOOP_TIMES; i; i--) {
			TCNT0 = 0;
			do {
				timer = TCNT0;
			} while (timer < DELAY_1MS_LOOP);
		}
	} while (--msecs);
	TCCR0B = 0;
}

void udelay(uint16_t usecs)
{
	uint8_t tmp;

	__asm__ __volatile__(
	"1:				\n"
	"	ldi %1, %2		\n"
	"2:				\n"
	"	dec %1			\n"
	"	brne 2b			\n"
	"	dec %A3			\n"
	"	brne 1b			\n"
	"	cp %B3, __zero_reg__	\n"
	"	breq 3f			\n"
	"	dec %B3			\n"
	"	ldi %A3, 0xFF		\n"
	"	rjmp 1b			\n"
	"3:				\n"
	: "=d" (usecs),
	  "=d" (tmp)
	: "M" (DELAY_1US_LOOP),
	  "0" (usecs)
	);
}

uint8_t eeprom_read8(const eeprom_uint8_t *addr)
{
	eeprom_busy_wait();
	return eeprom_read_byte((const uint8_t *)addr);
}

uint16_t eeprom_read16(const eeprom_uint16_t *addr)
{
	eeprom_busy_wait();
	return eeprom_read_word((const uint16_t *)addr);
}

void eeprom_write8(eeprom_uint8_t *addr, uint8_t value)
{
	eeprom_busy_wait();
	eeprom_write_byte((uint8_t *)addr, value);
}

void eeprom_write16(eeprom_uint16_t *addr, uint16_t value)
{
	eeprom_busy_wait();
	eeprom_write_word((uint16_t *)addr, value);
}

uint8_t hexdigit_to_ascii(uint8_t digit)
{
	/* Convert a hexadecimal digit (0-F) to an ASCII character */
	if (digit >= 0xA)
		digit += 0x41 - 0xA;
	else
		digit += 0x30;
	return digit;
}

void num16_to_ascii(uint8_t *buf, uint16_t v)
{
	uint16_t orig = v;

	if (orig >= 10000) {
		*buf++ = v / 10000 + '0';
		v %= 10000;
	}
	if (orig >= 1000) {
		*buf++ = v / 1000 + '0';
		v %= 1000;
	}
	if (orig >= 100) {
		*buf++ = v / 100 + '0';
		v %= 100;
	}
	if (orig >= 10) {
		*buf++ = v / 10 + '0';
		v %= 10;
	}
	*buf++ = v + '0';
	*buf = '\0';
}

void panic(const prog_char *msg)
{
	emergency_shutdown();
	lcd_clear_buffer();
	print(PSTR("Whee! PANIC :("));
	lcd_charcursor(1, 0);
	print(msg);
	lcd_commit();
	infinite_sleep();
}

void infinite_sleep(void)
{
	cli();
	set_sleep_mode(SLEEP_MODE_PWR_DOWN);
	while (1)
		sleep_mode();
}
bues.ch cgit interface