mbed_gpio.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "hal/gpio_api.h"
  17. static inline void _gpio_init_in(gpio_t *gpio, PinName pin, PinMode mode)
  18. {
  19. gpio_init(gpio, pin);
  20. if (pin != NC) {
  21. gpio_dir(gpio, PIN_INPUT);
  22. gpio_mode(gpio, mode);
  23. }
  24. }
  25. static inline void _gpio_init_out(gpio_t *gpio, PinName pin, PinMode mode, int value)
  26. {
  27. gpio_init(gpio, pin);
  28. if (pin != NC) {
  29. gpio_write(gpio, value);
  30. gpio_dir(gpio, PIN_OUTPUT);
  31. gpio_mode(gpio, mode);
  32. }
  33. }
  34. void gpio_init_in(gpio_t *gpio, PinName pin)
  35. {
  36. gpio_init_in_ex(gpio, pin, PullDefault);
  37. }
  38. void gpio_init_in_ex(gpio_t *gpio, PinName pin, PinMode mode)
  39. {
  40. _gpio_init_in(gpio, pin, mode);
  41. }
  42. void gpio_init_out(gpio_t *gpio, PinName pin)
  43. {
  44. gpio_init_out_ex(gpio, pin, 0);
  45. }
  46. void gpio_init_out_ex(gpio_t *gpio, PinName pin, int value)
  47. {
  48. _gpio_init_out(gpio, pin, PullNone, value);
  49. }
  50. void gpio_init_inout(gpio_t *gpio, PinName pin, PinDirection direction, PinMode mode, int value)
  51. {
  52. if (direction == PIN_INPUT) {
  53. _gpio_init_in(gpio, pin, mode);
  54. if (pin != NC) {
  55. gpio_write(gpio, value); // we prepare the value in case it is switched later
  56. }
  57. } else {
  58. _gpio_init_out(gpio, pin, mode, value);
  59. }
  60. }