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.

65 lines
1.8 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 "sn74x138.h"
  17. #include "gpio.h"
  18. #define ADDRESS_PIN_COUNT 3
  19. #ifndef SN74X138_ADDRESS_PINS
  20. # error sn74x138: no address pins defined!
  21. #endif
  22. static const pin_t address_pins[ADDRESS_PIN_COUNT] = SN74X138_ADDRESS_PINS;
  23. void sn74x138_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(SN74X138_E1_PIN)
  29. setPinOutput(SN74X138_E1_PIN);
  30. writePinHigh(SN74X138_E1_PIN);
  31. #endif
  32. #if defined(SN74X138_E2_PIN)
  33. setPinOutput(SN74X138_E2_PIN);
  34. writePinHigh(SN74X138_E2_PIN);
  35. #endif
  36. #if defined(SN74X138_E3_PIN)
  37. setPinOutput(SN74X138_E3_PIN);
  38. writePinLow(SN74X138_E3_PIN);
  39. #endif
  40. }
  41. void sn74x138_set_enabled(bool enabled) {
  42. #if defined(SN74X138_E1_PIN)
  43. writePin(SN74X138_E1_PIN, !enabled);
  44. #endif
  45. #if defined(SN74X138_E2_PIN)
  46. writePin(SN74X138_E2_PIN, !enabled);
  47. #endif
  48. #if defined(SN74X138_E3_PIN)
  49. writePin(SN74X138_E3_PIN, enabled);
  50. #endif
  51. }
  52. void sn74x138_set_addr(uint8_t address) {
  53. for (int i = 0; i < ADDRESS_PIN_COUNT; i++) {
  54. writePin(address_pins[i], address & (1 << i));
  55. }
  56. }