aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/cpu-firmware/4094.c
blob: 6cec65424fcb63924868746e23fc250c0bab8f21 (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
/*
 *   74HCT4094 shift register driver
 *
 *   Copyright (C) 2009-2016 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
 *   version 2 as published by the Free Software Foundation.
 *
 *   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 "4094.h"
#include "util.h"

#include <avr/io.h>


#define _sr4094_clear(pin) \
	SR4094_##pin##_PORT = (uint8_t)(SR4094_##pin##_PORT & ~(1 << SR4094_##pin##_BIT))

#define _sr4094_set(pin) \
	SR4094_##pin##_PORT = (uint8_t)(SR4094_##pin##_PORT | (1u << SR4094_##pin##_BIT))


static inline void sr4094_transfer_start(void)
{
	_sr4094_clear(STROBE);
}

static inline void sr4094_transfer_end(void)
{
	_sr4094_set(STROBE);
}

static void sr4094_put_byte(uint8_t data)
{
	uint8_t mask = 0x80;

	do {
		if (data & mask)
			_sr4094_set(DATA);
		else
			_sr4094_clear(DATA);
		_sr4094_set(CLOCK);
		nop();
		nop();
		_sr4094_clear(CLOCK);
		mask >>= 1;
	} while (mask);
}

void sr4094_put_data(void *_data, uint8_t nr_chips)
{
	uint8_t *data = _data;
	uint8_t i;

	sr4094_transfer_start();
	for (i = 0; i < nr_chips; i++)
		sr4094_put_byte(data ? data[i] : 0);
	sr4094_transfer_end();
}

void sr4094_outen(uint8_t enable)
{
	if (enable)
		_sr4094_set(OUTEN);
	else
		_sr4094_clear(OUTEN);
}

void sr4094_init(void *initial_data, uint8_t nr_chips)
{
	sr4094_outen(0);
	SR4094_OUTEN_DDR |= (1 << SR4094_OUTEN_BIT);

	_sr4094_clear(DATA);
	SR4094_DATA_DDR |= (1 << SR4094_DATA_BIT);

	_sr4094_clear(CLOCK);
	SR4094_CLOCK_DDR |= (1 << SR4094_CLOCK_BIT);

	_sr4094_set(STROBE);
	SR4094_STROBE_DDR |= (1 << SR4094_STROBE_BIT);

	sr4094_put_data(initial_data, nr_chips);
}
bues.ch cgit interface