Parent directory

sn74x138.c

1895 bytes
 1/* Copyright 2022
 2 *
 3 * This program is free software: you can redistribute it and/or modify
 4 * it under the terms of the GNU General Public License as published by
 5 * the Free Software Foundation, either version 2 of the License, or
 6 * (at your option) any later version.
 7 *
 8 * This program is distributed in the hope that it will be useful,
 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "sn74x138.h"
18#include "gpio.h"
19
20#define ADDRESS_PIN_COUNT 3
21
22#ifndef SN74X138_ADDRESS_PINS
23#    error sn74x138: no address pins defined!
24#endif
25
26static const pin_t address_pins[ADDRESS_PIN_COUNT] = SN74X138_ADDRESS_PINS;
27
28void sn74x138_init(void) {
29    for (int i = 0; i < ADDRESS_PIN_COUNT; i++) {
30        gpio_set_pin_output(address_pins[i]);
31        gpio_write_pin_low(address_pins[i]);
32    }
33
34#if defined(SN74X138_E1_PIN)
35    gpio_set_pin_output(SN74X138_E1_PIN);
36    gpio_write_pin_high(SN74X138_E1_PIN);
37#endif
38
39#if defined(SN74X138_E2_PIN)
40    gpio_set_pin_output(SN74X138_E2_PIN);
41    gpio_write_pin_high(SN74X138_E2_PIN);
42#endif
43#if defined(SN74X138_E3_PIN)
44    gpio_set_pin_output(SN74X138_E3_PIN);
45    gpio_write_pin_low(SN74X138_E3_PIN);
46#endif
47}
48
49void sn74x138_set_enabled(bool enabled) {
50#if defined(SN74X138_E1_PIN)
51    gpio_write_pin(SN74X138_E1_PIN, !enabled);
52#endif
53#if defined(SN74X138_E2_PIN)
54    gpio_write_pin(SN74X138_E2_PIN, !enabled);
55#endif
56#if defined(SN74X138_E3_PIN)
57    gpio_write_pin(SN74X138_E3_PIN, enabled);
58#endif
59}
60
61void sn74x138_set_addr(uint8_t address) {
62    for (int i = 0; i < ADDRESS_PIN_COUNT; i++) {
63        gpio_write_pin(address_pins[i], address & (1 << i));
64    }
65}