aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/potentiometer.c
blob: 5dbd4913146c18c56ad37e94bd2caa0f83d74b1d (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
/*
 * Potentiometer power control
 *
 * Copyright (c) 2018-2020 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "compat.h"
#include "debug.h"
#include "potentiometer.h"
#include "main.h"


/* Potentiometer enable pin. */
#if IS_ATMEGAx8
# define POTEN_PORT		PORTC
# define POTEN_DDR		DDRC
# define POTEN_LO_BIT		4
# define POTEN_HI_BIT		5
#else
# define POTEN_PORT		PORTB
# define POTEN_DDR		DDRB
# define POTEN_LO_BIT		2
# define POTEN_HI_BIT		3
#endif


/* Enable/disable the power supply to the potentiometer. */
void potentiometer_enable(bool enable)
{
	if (USE_DEEP_SLEEP) {
		if (enable) {
			/* Turn pot power supply on. */
			/* HI = driven */
			POTEN_PORT |= (1 << POTEN_HI_BIT);
			POTEN_DDR |= (1 << POTEN_HI_BIT);
			/* LO = driven */
			POTEN_PORT &= (uint8_t)~(1 << POTEN_LO_BIT);
			POTEN_DDR |= (1 << POTEN_LO_BIT);
		} else {
			/* Switch pot power supply to high impedance input. */
			if (CONF_ADCINVERT) {
				/* HI = driven */
				POTEN_PORT |= (1 << POTEN_HI_BIT);
				POTEN_DDR |= (1 << POTEN_HI_BIT);
				/* LO = high impedance */
				POTEN_DDR &= (uint8_t)~(1 << POTEN_LO_BIT);
				POTEN_PORT &= (uint8_t)~(1 << POTEN_LO_BIT);
			} else {
				/* LO = driven */
				POTEN_PORT &= (uint8_t)~(1 << POTEN_LO_BIT);
				POTEN_DDR |= (1 << POTEN_LO_BIT);
				/* HI = high impedance */
				POTEN_DDR &= (uint8_t)~(1 << POTEN_HI_BIT);
				POTEN_PORT &= (uint8_t)~(1 << POTEN_HI_BIT);
			}
		}
	}
}
bues.ch cgit interface