sn74x154.c (1694B)
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 "sn74x154.h" 18 #include "gpio.h" 19 20 #define ADDRESS_PIN_COUNT 4 21 22 #ifndef SN74X154_ADDRESS_PINS 23 # error sn74x154: no address pins defined! 24 #endif 25 26 static const pin_t address_pins[ADDRESS_PIN_COUNT] = SN74X154_ADDRESS_PINS; 27 28 void sn74x154_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(SN74X154_E0_PIN) 35 gpio_set_pin_output(SN74X154_E0_PIN); 36 gpio_write_pin_high(SN74X154_E0_PIN); 37 #endif 38 39 #if defined(SN74X154_E1_PIN) 40 gpio_set_pin_output(SN74X154_E1_PIN); 41 gpio_write_pin_high(SN74X154_E1_PIN); 42 #endif 43 } 44 45 void sn74x154_set_enabled(bool enabled) { 46 #if defined(SN74X154_E0_PIN) 47 gpio_write_pin(SN74X154_E0_PIN, !enabled); 48 #endif 49 #if defined(SN74X154_E1_PIN) 50 gpio_write_pin(SN74X154_E1_PIN, !enabled); 51 #endif 52 } 53 54 void sn74x154_set_addr(uint8_t address) { 55 for (int i = 0; i < ADDRESS_PIN_COUNT; i++) { 56 gpio_write_pin(address_pins[i], address & (1 << i)); 57 } 58 }