/src/main/drivers/io.h

https://github.com/borisbstyle/betaflight · C Header · 121 lines · 71 code · 24 blank · 26 comment · 4 complexity · 026a3f59eab1d8cb8a744f28f43ab61e MD5 · raw file

  1. /*
  2. * This file is part of Cleanflight and Betaflight.
  3. *
  4. * Cleanflight and Betaflight are free software. You can redistribute
  5. * this software and/or modify this software under the terms of the
  6. * GNU General Public License as published by the Free Software
  7. * Foundation, either version 3 of the License, or (at your option)
  8. * any later version.
  9. *
  10. * Cleanflight and Betaflight are distributed in the hope that they
  11. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  12. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this software.
  17. *
  18. * If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #pragma once
  21. #include <stdbool.h>
  22. #include <stdint.h>
  23. #include "platform.h"
  24. #include "resource.h"
  25. #include "drivers/io_types.h"
  26. // preprocessor is used to convert pinid to requested C data value
  27. // compile-time error is generated if requested pin is not available (not set in TARGET_IO_PORTx)
  28. // ioTag_t and IO_t is supported, but ioTag_t is preferred
  29. // expand pinid to to ioTag_t
  30. #define IO_TAG(pinid) DEFIO_TAG(pinid)
  31. #if defined(STM32F1)
  32. // mode is using only bits 6-2
  33. #define IO_CONFIG(mode, speed) ((mode) | (speed))
  34. #define IOCFG_OUT_PP IO_CONFIG(GPIO_Mode_Out_PP, GPIO_Speed_2MHz)
  35. #define IOCFG_OUT_OD IO_CONFIG(GPIO_Mode_Out_OD, GPIO_Speed_2MHz)
  36. #define IOCFG_AF_PP IO_CONFIG(GPIO_Mode_AF_PP, GPIO_Speed_2MHz)
  37. #define IOCFG_AF_OD IO_CONFIG(GPIO_Mode_AF_OD, GPIO_Speed_2MHz)
  38. #define IOCFG_IPD IO_CONFIG(GPIO_Mode_IPD, GPIO_Speed_2MHz)
  39. #define IOCFG_IPU IO_CONFIG(GPIO_Mode_IPU, GPIO_Speed_2MHz)
  40. #define IOCFG_IN_FLOATING IO_CONFIG(GPIO_Mode_IN_FLOATING, GPIO_Speed_2MHz)
  41. #elif defined(STM32F7)
  42. //speed is packed inside modebits 5 and 2,
  43. #define IO_CONFIG(mode, speed, pupd) ((mode) | ((speed) << 2) | ((pupd) << 5))
  44. #define IOCFG_OUT_PP IO_CONFIG(GPIO_MODE_OUTPUT_PP, GPIO_SPEED_FREQ_LOW, GPIO_NOPULL)
  45. #define IOCFG_OUT_PP_UP IO_CONFIG(GPIO_MODE_OUTPUT_PP, GPIO_SPEED_FREQ_LOW, GPIO_PULLUP)
  46. #define IOCFG_OUT_PP_25 IO_CONFIG(GPIO_MODE_OUTPUT_PP, GPIO_SPEED_FREQ_HIGH, GPIO_NOPULL)
  47. #define IOCFG_OUT_OD IO_CONFIG(GPIO_MODE_OUTPUT_OD, GPIO_SPEED_FREQ_LOW, GPIO_NOPULL)
  48. #define IOCFG_AF_PP IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_LOW, GPIO_NOPULL)
  49. #define IOCFG_AF_PP_PD IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_LOW, GPIO_PULLDOWN)
  50. #define IOCFG_AF_PP_UP IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_LOW, GPIO_PULLUP)
  51. #define IOCFG_AF_OD IO_CONFIG(GPIO_MODE_AF_OD, GPIO_SPEED_FREQ_LOW, GPIO_NOPULL)
  52. #define IOCFG_IPD IO_CONFIG(GPIO_MODE_INPUT, GPIO_SPEED_FREQ_LOW, GPIO_PULLDOWN)
  53. #define IOCFG_IPU IO_CONFIG(GPIO_MODE_INPUT, GPIO_SPEED_FREQ_LOW, GPIO_PULLUP)
  54. #define IOCFG_IN_FLOATING IO_CONFIG(GPIO_MODE_INPUT, GPIO_SPEED_FREQ_LOW, GPIO_NOPULL)
  55. #define IOCFG_IPU_25 IO_CONFIG(GPIO_MODE_INPUT, GPIO_SPEED_FREQ_HIGH, GPIO_PULLUP)
  56. #elif defined(STM32F3) || defined(STM32F4)
  57. #define IO_CONFIG(mode, speed, otype, pupd) ((mode) | ((speed) << 2) | ((otype) << 4) | ((pupd) << 5))
  58. #define IOCFG_OUT_PP IO_CONFIG(GPIO_Mode_OUT, 0, GPIO_OType_PP, GPIO_PuPd_NOPULL) // TODO
  59. #define IOCFG_OUT_PP_UP IO_CONFIG(GPIO_Mode_OUT, 0, GPIO_OType_PP, GPIO_PuPd_UP)
  60. #define IOCFG_OUT_PP_25 IO_CONFIG(GPIO_Mode_OUT, GPIO_Speed_25MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL)
  61. #define IOCFG_OUT_OD IO_CONFIG(GPIO_Mode_OUT, 0, GPIO_OType_OD, GPIO_PuPd_NOPULL)
  62. #define IOCFG_AF_PP IO_CONFIG(GPIO_Mode_AF, 0, GPIO_OType_PP, GPIO_PuPd_NOPULL)
  63. #define IOCFG_AF_PP_PD IO_CONFIG(GPIO_Mode_AF, 0, GPIO_OType_PP, GPIO_PuPd_DOWN)
  64. #define IOCFG_AF_PP_UP IO_CONFIG(GPIO_Mode_AF, 0, GPIO_OType_PP, GPIO_PuPd_UP)
  65. #define IOCFG_AF_OD IO_CONFIG(GPIO_Mode_AF, 0, GPIO_OType_OD, GPIO_PuPd_NOPULL)
  66. #define IOCFG_IPD IO_CONFIG(GPIO_Mode_IN, 0, 0, GPIO_PuPd_DOWN)
  67. #define IOCFG_IPU IO_CONFIG(GPIO_Mode_IN, 0, 0, GPIO_PuPd_UP)
  68. #define IOCFG_IN_FLOATING IO_CONFIG(GPIO_Mode_IN, 0, 0, GPIO_PuPd_NOPULL)
  69. #define IOCFG_IPU_25 IO_CONFIG(GPIO_Mode_IN, GPIO_Speed_25MHz, 0, GPIO_PuPd_UP)
  70. #elif defined(UNIT_TEST) || defined(SIMULATOR_BUILD)
  71. # define IOCFG_OUT_PP 0
  72. # define IOCFG_OUT_OD 0
  73. # define IOCFG_AF_PP 0
  74. # define IOCFG_AF_OD 0
  75. # define IOCFG_IPD 0
  76. # define IOCFG_IPU 0
  77. # define IOCFG_IN_FLOATING 0
  78. #else
  79. # warning "Unknown TARGET"
  80. #endif
  81. // declare available IO pins. Available pins are specified per target
  82. #include "io_def.h"
  83. bool IORead(IO_t io);
  84. void IOWrite(IO_t io, bool value);
  85. void IOHi(IO_t io);
  86. void IOLo(IO_t io);
  87. void IOToggle(IO_t io);
  88. void IOInit(IO_t io, resourceOwner_e owner, uint8_t index);
  89. void IORelease(IO_t io); // unimplemented
  90. resourceOwner_e IOGetOwner(IO_t io);
  91. bool IOIsFreeOrPreinit(IO_t io);
  92. IO_t IOGetByTag(ioTag_t tag);
  93. void IOConfigGPIO(IO_t io, ioConfig_t cfg);
  94. #if defined(STM32F3) || defined(STM32F4) || defined(STM32F7)
  95. void IOConfigGPIOAF(IO_t io, ioConfig_t cfg, uint8_t af);
  96. #endif
  97. void IOInitGlobal(void);