You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.6 KiB

  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. #include "sn74x154.h"
  17. #include "gpio.h"
  18. #define ADDRESS_PIN_COUNT 4
  19. #ifndef SN74X154_ADDRESS_PINS
  20. # error sn74x154: no address pins defined!
  21. #endif
  22. static const pin_t address_pins[ADDRESS_PIN_COUNT] = SN74X154_ADDRESS_PINS;
  23. void sn74x154_init(void) {
  24. for (int i = 0; i < ADDRESS_PIN_COUNT; i++) {
  25. setPinOutput(address_pins[i]);
  26. writePinLow(address_pins[i]);
  27. }
  28. #if defined(SN74X154_E0_PIN)
  29. setPinOutput(SN74X154_E0_PIN);
  30. writePinHigh(SN74X154_E0_PIN);
  31. #endif
  32. #if defined(SN74X154_E1_PIN)
  33. setPinOutput(SN74X154_E1_PIN);
  34. writePinHigh(SN74X154_E1_PIN);
  35. #endif
  36. }
  37. void sn74x154_set_enabled(bool enabled) {
  38. #if defined(SN74X154_E0_PIN)
  39. writePin(SN74X154_E0_PIN, !enabled);
  40. #endif
  41. #if defined(SN74X154_E1_PIN)
  42. writePin(SN74X154_E1_PIN, !enabled);
  43. #endif
  44. }
  45. void sn74x154_set_addr(uint8_t address) {
  46. for (int i = 0; i < ADDRESS_PIN_COUNT; i++) {
  47. writePin(address_pins[i], address & (1 << i));
  48. }
  49. }