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.

52 lines
1.5 KiB

  1. /* Copyright 2016 Wez Furlong
  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. #pragma once
  17. // Some helpers for controlling gpio pins
  18. #include <avr/io.h>
  19. enum {
  20. PinDirectionInput = 0,
  21. PinDirectionOutput = 1,
  22. PinLevelHigh = 1,
  23. PinLevelLow = 0,
  24. };
  25. // ex: pinMode(B0, PinDirectionOutput);
  26. static inline void pinMode(uint8_t pin, int mode) {
  27. uint8_t bv = _BV(pin & 0xf);
  28. if (mode == PinDirectionOutput) {
  29. _SFR_IO8((pin >> 4) + 1) |= bv;
  30. } else {
  31. _SFR_IO8((pin >> 4) + 1) &= ~bv;
  32. _SFR_IO8((pin >> 4) + 2) &= ~bv;
  33. }
  34. }
  35. // ex: digitalWrite(B0, PinLevelHigh);
  36. static inline void digitalWrite(uint8_t pin, int mode) {
  37. uint8_t bv = _BV(pin & 0xf);
  38. if (mode == PinLevelHigh) {
  39. _SFR_IO8((pin >> 4) + 2) |= bv;
  40. } else {
  41. _SFR_IO8((pin >> 4) + 2) &= ~bv;
  42. }
  43. }
  44. // Return true if the pin is HIGH
  45. // digitalRead(B0)
  46. static inline bool digitalRead(uint8_t pin) {
  47. return _SFR_IO8(pin >> 4) & _BV(pin & 0xf);
  48. }