summaryrefslogtreecommitdiffstats
path: root/firmware/ltc1446.c
blob: f8bc8bb4a395bdcb0bb0e8e4f9e315e6667d9cbf (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
/*
 *   Linear Technology LTC1446 D/A Converter
 *   Device driver
 *
 *   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 "ltc1446.h"
#include "util.h"

#include <avr/io.h>


/* The hardware connection definitions */
#define LTC1446_PORT	PORTD
#define LTC1446_DDR	DDRD
#define LTC1446_CLK	(1 << 4) /* Clock pin */
#define LTC1446_DIN	(1 << 5) /* Data input pin */
#define LTC1446_CS	(1 << 6) /* Chip select pin */


/* Put a data word into the device. */
static void ltc1446_put(uint16_t data)
{
	uint16_t mask;
	uint8_t bits = 12; /* 12 bit resolution */

	/* The bits are shifted MSB to LSB into the device. */
	mask = (1 << (bits - 1));
	do {
		if (data & mask)
			LTC1446_PORT |= LTC1446_DIN;
		else
			LTC1446_PORT &= ~LTC1446_DIN;
		LTC1446_PORT |= LTC1446_CLK;
		_delay_us(1);
		LTC1446_PORT &= ~LTC1446_CLK;
		_delay_us(1);
		mask >>= 1;
	} while (--bits);
}

void ltc1446_init(void)
{
	LTC1446_DDR |= (LTC1446_CLK | LTC1446_DIN | LTC1446_CS);
	LTC1446_PORT &= ~(LTC1446_CLK | LTC1446_DIN);
	LTC1446_PORT |= LTC1446_CS;
	_delay_ms(10);

	ltc1446_write(0, 0);
}

void ltc1446_write(uint16_t chan_a, uint16_t chan_b)
{
	LTC1446_PORT &= ~LTC1446_CS;
	_delay_us(50);

	ltc1446_put(chan_a);
	ltc1446_put(chan_b);

	LTC1446_PORT |= LTC1446_CS;
	_delay_us(50);
}
bues.ch cgit interface