drv_gpio.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-06-27 AHTYDHD the first version
  9. */
  10. #include "drv_gpio.h"
  11. #include <rthw.h>
  12. #include <rtdevice.h>
  13. #include "drv_gpio.h"
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include "inc/hw_memmap.h"
  17. #include "driverlib/sysctl.h"
  18. #include "driverlib/gpio.h"
  19. #include "driverlib/pin_map.h"
  20. #ifdef RT_USING_PIN
  21. static const struct pin_index pins[] =
  22. {
  23. _TM4C_PIN(0, F, 0),
  24. _TM4C_PIN(1, F, 1),
  25. _TM4C_PIN(2, F, 2),
  26. _TM4C_PIN(3, F, 3),
  27. _TM4C_PIN(4, F, 4)
  28. };
  29. /* this is pin_irq map, reserved for update */
  30. static struct rt_pin_irq_hdr pin_irq_hdr_tab[] =
  31. {
  32. {-1, 0, RT_NULL, RT_NULL},
  33. };
  34. static uint32_t pin_irq_enable_mask = 0;
  35. #define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
  36. static const struct pin_index *get_pin(uint8_t pin)
  37. {
  38. const struct pin_index *index;
  39. if (pin < ITEM_NUM(pins))
  40. {
  41. index = &pins[pin];
  42. if (index->index == -1)
  43. index = RT_NULL;
  44. }
  45. else
  46. {
  47. index = RT_NULL;
  48. }
  49. return index;
  50. };
  51. static void tm4c123_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode)
  52. {
  53. const struct pin_index *index;
  54. index = get_pin(pin);
  55. if (index == RT_NULL)
  56. {
  57. return;
  58. }
  59. if (mode == PIN_MODE_INPUT)
  60. {
  61. GPIOPinTypeGPIOInput(index ->gpioBaseAddress, index->pin);
  62. }
  63. else if (mode == PIN_MODE_OUTPUT)
  64. {
  65. GPIOPinTypeGPIOOutput(index->gpioBaseAddress, index->pin);
  66. }
  67. else if (mode == PIN_MODE_INPUT_PULLUP)
  68. {
  69. GPIODirModeSet(index->gpioBaseAddress, index->pin, GPIO_DIR_MODE_IN);
  70. GPIOPadConfigSet(index->gpioBaseAddress, index->pin, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
  71. }
  72. else if (mode == PIN_MODE_INPUT_PULLDOWN)
  73. {
  74. GPIODirModeSet(index->gpioBaseAddress, index->pin, GPIO_DIR_MODE_IN);
  75. GPIOPadConfigSet(index->gpioBaseAddress, index->pin, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPD);
  76. }
  77. else if (mode == PIN_MODE_OUTPUT_OD)
  78. {
  79. GPIOPadConfigSet(index->gpioBaseAddress, index->pin, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD);
  80. GPIODirModeSet(index->gpioBaseAddress, index->pin, GPIO_DIR_MODE_OUT);
  81. }
  82. }
  83. static void tm4c123_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t ui8Val)
  84. {
  85. const struct pin_index *index;
  86. index = get_pin(pin);
  87. if (index == RT_NULL)
  88. {
  89. return;
  90. }
  91. if (ui8Val)
  92. {
  93. GPIOPinWrite(index ->gpioBaseAddress, index->pin, index->pin);
  94. }
  95. else
  96. {
  97. GPIOPinWrite(index ->gpioBaseAddress, index->pin, 0);
  98. }
  99. }
  100. static rt_ssize_t tm4c123_pin_read(rt_device_t dev, rt_base_t pin)
  101. {
  102. const struct pin_index *index;
  103. rt_ssize_t value = 0;
  104. index = get_pin(pin);
  105. if (index == RT_NULL)
  106. {
  107. return value;
  108. }
  109. value = GPIOPinRead(index ->gpioBaseAddress, index ->pin);
  110. return value;
  111. }
  112. static rt_err_t tm4c123_pin_attach_irq(rt_device_t device, rt_int32_t pin, rt_uint32_t mode, void (*hdr)(void *args), void *args)
  113. {
  114. /* this is interface for pin_irq, reserved for update. */
  115. return RT_EOK;
  116. }
  117. static rt_err_t tm4c123_pin_dettach_irq(rt_device_t device, rt_int32_t pin)
  118. {
  119. /* this is interface for pin_irq, reserved for update. */
  120. return RT_EOK;
  121. }
  122. static rt_err_t tm4c123_pin_irq_enable(rt_device_t device, rt_base_t pin,
  123. rt_uint32_t enabled)
  124. {
  125. /* this is interface for pin_irq_enable, reserved for update. */
  126. return RT_EOK;
  127. }
  128. const static struct rt_pin_ops _tm4c123_pin_ops =
  129. {
  130. tm4c123_pin_mode,
  131. tm4c123_pin_write,
  132. tm4c123_pin_read,
  133. tm4c123_pin_attach_irq,
  134. tm4c123_pin_dettach_irq,
  135. tm4c123_pin_irq_enable,
  136. RT_NULL,
  137. };
  138. int rt_hw_pin_init(void)
  139. {
  140. SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
  141. return rt_device_pin_register("pin", &_tm4c123_pin_ops, RT_NULL);
  142. }
  143. INIT_BOARD_EXPORT(rt_hw_pin_init);
  144. #endif /*RT_USING_PIN*/
  145. /************************** end of file ******************/