stm322xg_eval_io.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. ******************************************************************************
  3. * @file stm322xg_eval_io.c
  4. * @author MCD Application Team
  5. * @brief This file provides a set of functions needed to manage the IO pins
  6. * on STM322xG-EVAL evaluation board.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. * 1. Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ******************************************************************************
  35. */
  36. /* File Info : -----------------------------------------------------------------
  37. User NOTES
  38. 1. How To use this driver:
  39. --------------------------
  40. - This driver is used to drive the IO module of the STM322xG-EVAL evaluation
  41. board.
  42. - The STMPE811 IO expander device component driver must be included with this
  43. driver in order to run the IO functionalities commanded by the IO expander
  44. device mounted on the evaluation board.
  45. 2. Driver description:
  46. ---------------------
  47. + Initialization steps:
  48. o Initialize the IO module using the BSP_IO_Init() function. This
  49. function includes the MSP layer hardware resources initialization and the
  50. communication layer configuration to start the IO functionalities use.
  51. + IO functionalities use
  52. o The IO pin mode is configured when calling the function BSP_IO_ConfigPin(), you
  53. must specify the desired IO mode by choosing the "IO_ModeTypedef" parameter
  54. predefined value.
  55. o If an IO pin is used in interrupt mode, the function BSP_IO_ITGetStatus() is
  56. needed to get the interrupt status. To clear the IT pending bits, you should
  57. call the function BSP_IO_ITClear() with specifying the IO pending bit to clear.
  58. o The IT is handled using the corresponding external interrupt IRQ handler,
  59. the user IT callback treatment is implemented on the same external interrupt
  60. callback.
  61. o To get/set an IO pin combination state you can use the functions
  62. BSP_IO_ReadPin()/BSP_IO_WritePin() or the function BSP_IO_TogglePin() to toggle the pin
  63. state.
  64. ------------------------------------------------------------------------------*/
  65. /* Includes ------------------------------------------------------------------*/
  66. #include "stm322xg_eval_io.h"
  67. /** @addtogroup BSP
  68. * @{
  69. */
  70. /** @addtogroup STM322xG_EVAL
  71. * @{
  72. */
  73. /** @defgroup STM322xG_EVAL_IO STM322xG EVAL IO
  74. * @{
  75. */
  76. /** @defgroup STM322xG_EVAL_IO_Private_Types_Definitions STM322xG EVAL IO Private Types Definitions
  77. * @{
  78. */
  79. /**
  80. * @}
  81. */
  82. /** @defgroup STM322xG_EVAL_IO_Private_Variables STM322xG EVAL IO Private Variables
  83. * @{
  84. */
  85. static IO_DrvTypeDef *io_driver;
  86. /**
  87. * @}
  88. */
  89. /** @defgroup STM322xG_EVAL_IO_Private_Functions STM3222xG EVAL IO Private Functions
  90. * @{
  91. */
  92. /**
  93. * @brief Initializes and configures the IO functionalities and configures all
  94. * necessary hardware resources (GPIOs, clocks..).
  95. * @note BSP_IO_Init() is using HAL_Delay() function to ensure that stmpe811
  96. * IO Expander is correctly reset. HAL_Delay() function provides accurate
  97. * delay (in milliseconds) based on variable incremented in SysTick ISR.
  98. * This implies that if BSP_IO_Init() is called from a peripheral ISR process,
  99. * then the SysTick interrupt must have higher priority (numerically lower)
  100. * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
  101. * @retval IO_OK: if all initializations are OK. Other value if error.
  102. */
  103. uint8_t BSP_IO_Init(void)
  104. {
  105. uint8_t ret = IO_ERROR;
  106. if(stmpe811_io_drv.ReadID(IO_I2C_ADDRESS) == STMPE811_ID)
  107. {
  108. /* Initialize the IO driver structure */
  109. io_driver = &stmpe811_io_drv;
  110. io_driver->Init(IO_I2C_ADDRESS);
  111. io_driver->Start(IO_I2C_ADDRESS, IO_PIN_ALL);
  112. ret = IO_OK;
  113. }
  114. return ret;
  115. }
  116. /**
  117. * @brief Gets the selected pins IT status.
  118. * @param IO_Pin: Selected pins to check the status.
  119. * This parameter can be any combination of the IO pins.
  120. * @retval IO_OK: if read status OK. Other value if error.
  121. */
  122. uint8_t BSP_IO_ITGetStatus(uint16_t IO_Pin)
  123. {
  124. /* Return the IO Pin IT status */
  125. return (io_driver->ITStatus(IO_I2C_ADDRESS, IO_Pin));
  126. }
  127. /**
  128. * @brief Clears the selected IO IT pending bit.
  129. * @param IO_Pin: Selected pins to check the status.
  130. * This parameter can be any combination of the IO pins.
  131. */
  132. void BSP_IO_ITClear(uint16_t IO_Pin)
  133. {
  134. io_driver->ClearIT(IO_I2C_ADDRESS, IO_Pin);
  135. }
  136. /**
  137. * @brief Configures the IO pin(s) according to IO mode structure value.
  138. * @param IO_Pin: Output pin to be set or reset.
  139. * This parameter can be one of the following values:
  140. * @arg STMPE811_PIN_x: where x can be from 0 to 7
  141. * @param IO_Mode: IO pin mode to configure
  142. * This parameter can be one of the following values:
  143. * @arg IO_MODE_INPUT
  144. * @arg IO_MODE_OUTPUT
  145. * @arg IO_MODE_IT_RISING_EDGE
  146. * @arg IO_MODE_IT_FALLING_EDGE
  147. * @arg IO_MODE_IT_LOW_LEVEL
  148. * @arg IO_MODE_IT_HIGH_LEVEL
  149. * @retval IO_OK: if all initializations are OK. Other value if error.
  150. */
  151. uint8_t BSP_IO_ConfigPin(uint16_t IO_Pin, IO_ModeTypedef IO_Mode)
  152. {
  153. /* Configure the selected IO pin(s) mode */
  154. io_driver->Config(IO_I2C_ADDRESS, IO_Pin, IO_Mode);
  155. return IO_OK;
  156. }
  157. /**
  158. * @brief Sets the selected pins state.
  159. * @param IO_Pin: Selected pins to write.
  160. * This parameter can be any combination of the IO pins.
  161. * @param PinState: New pins state to write
  162. */
  163. void BSP_IO_WritePin(uint16_t IO_Pin, uint8_t PinState)
  164. {
  165. io_driver->WritePin(IO_I2C_ADDRESS, IO_Pin, PinState);
  166. }
  167. /**
  168. * @brief Gets the selected pins current state.
  169. * @param IO_Pin: Selected pins to read.
  170. * This parameter can be any combination of the IO pins.
  171. * @retval The current pins state
  172. */
  173. uint16_t BSP_IO_ReadPin(uint16_t IO_Pin)
  174. {
  175. return(io_driver->ReadPin(IO_I2C_ADDRESS, IO_Pin));
  176. }
  177. /**
  178. * @brief Toggles the selected pins state
  179. * @param IO_Pin: Selected pins to toggle.
  180. * This parameter can be any combination of the IO pins.
  181. */
  182. void BSP_IO_TogglePin(uint16_t IO_Pin)
  183. {
  184. if(io_driver->ReadPin(IO_I2C_ADDRESS, IO_Pin) == 1) /* Set */
  185. {
  186. io_driver->WritePin(IO_I2C_ADDRESS, IO_Pin, 0); /* Reset */
  187. }
  188. else
  189. {
  190. io_driver->WritePin(IO_I2C_ADDRESS, IO_Pin, 1); /* Set */
  191. }
  192. }
  193. /**
  194. * @}
  195. */
  196. /**
  197. * @}
  198. */
  199. /**
  200. * @}
  201. */
  202. /**
  203. * @}
  204. */
  205. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/