gpio.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. ******************************************************************************
  3. * File Name : gpio.c
  4. * Description : This file provides code for the configuration
  5. * of all used GPIO pins.
  6. ******************************************************************************
  7. ** This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * COPYRIGHT(c) 2019 STMicroelectronics
  14. *
  15. * Redistribution and use in source and binary forms, with or without modification,
  16. * are permitted provided that the following conditions are met:
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ******************************************************************************
  38. */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "gpio.h"
  41. /* USER CODE BEGIN 0 */
  42. #include "protect_check.h"
  43. /* USER CODE END 0 */
  44. /*----------------------------------------------------------------------------*/
  45. /* Configure GPIO */
  46. /*----------------------------------------------------------------------------*/
  47. /* USER CODE BEGIN 1 */
  48. /* USER CODE END 1 */
  49. /** Configure pins as
  50. * Analog
  51. * Input
  52. * Output
  53. * EVENT_OUT
  54. * EXTI
  55. * Free pins are configured automatically as Analog (this feature is enabled through
  56. * the Code Generation settings)
  57. */
  58. void MX_GPIO_Init(void)
  59. {
  60. GPIO_InitTypeDef GPIO_InitStruct;
  61. /* GPIO Ports Clock Enable */
  62. __HAL_RCC_GPIOC_CLK_ENABLE();
  63. __HAL_RCC_GPIOD_CLK_ENABLE();
  64. __HAL_RCC_GPIOA_CLK_ENABLE();
  65. __HAL_RCC_GPIOB_CLK_ENABLE();
  66. /*Configure GPIO pin Output Level */
  67. HAL_GPIO_WritePin(SYC_IO_GPIO_Port, SYC_IO_Pin, GPIO_PIN_RESET);
  68. /*Configure GPIO pins : PC13 PC14 PC15
  69. PC8 PC9 */
  70. GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15
  71. |GPIO_PIN_8|GPIO_PIN_9;
  72. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  73. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  74. /*Configure GPIO pins : PA3 PA8
  75. PA9 PA10 PA11 */
  76. GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_8
  77. |GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11;
  78. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  79. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  80. /*Configure GPIO pins : PB0 PB1 PB2 PB10
  81. PB11 PB13 PB14 PB15 */
  82. GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_10
  83. |GPIO_PIN_11|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  84. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  85. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  86. /*Configure GPIO pin : PtPin */
  87. GPIO_InitStruct.Pin = BREAK_IN_Pin;
  88. GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  89. GPIO_InitStruct.Pull = GPIO_NOPULL;
  90. HAL_GPIO_Init(BREAK_IN_GPIO_Port, &GPIO_InitStruct);
  91. /*Configure GPIO pins : PCPin PCPin */
  92. GPIO_InitStruct.Pin = SYC_IO_Pin;
  93. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  94. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  95. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  96. /* EXTI interrupt init*/
  97. HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
  98. HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
  99. }
  100. /* USER CODE BEGIN 2 */
  101. void Enable_PwmGpio_Out(void)
  102. {
  103. GPIO_InitTypeDef GPIO_InitStruct;
  104. GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  105. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  106. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  107. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  108. GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10;
  109. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  110. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  111. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  112. }
  113. void Disable_PwmGpio_Out(void)
  114. {
  115. GPIO_InitTypeDef GPIO_InitStruct;
  116. GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  117. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  118. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  119. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  120. GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10;
  121. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  122. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  123. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  124. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
  125. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET);
  126. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_RESET);
  127. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_RESET);
  128. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);
  129. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_RESET);
  130. }
  131. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  132. {
  133. static uint32_t SignalTrigCount = 0;
  134. static uint32_t DiffTime_ms_Old = 0;
  135. if(GPIO_Pin == SPEED_SENSOR_Pin)
  136. {
  137. //车速计算变量更新
  138. MC_SpeedSensorData.IsTrigFlag = TRUE; // 更新信号触发标志
  139. if(MC_SpeedSensorData.IsStopFlag == TRUE)
  140. {
  141. MC_SpeedSensorData.TrigSysTime = HAL_GetTick();
  142. MC_SpeedSensorData.DiffTime_ms = 0xFFFFFFFF;
  143. MC_SpeedSensorData.IsStopFlag = FALSE;
  144. }
  145. else
  146. {
  147. MC_SpeedSensorData.DiffTime_ms = HAL_GetTick() - MC_SpeedSensorData.TrigSysTime;
  148. if(MC_SpeedSensorData.DiffTime_ms < 75)//限制车速测量最高值,抗干扰,3600/0.075*2.19/1000 = 105km/h
  149. {
  150. MC_SpeedSensorData.DiffTime_ms = DiffTime_ms_Old;
  151. }
  152. MC_SpeedSensorData.TrigSysTime = HAL_GetTick();
  153. }
  154. DiffTime_ms_Old = MC_SpeedSensorData.DiffTime_ms;
  155. //车轮圈数更新
  156. if(MC_SpeedSensorData.DiffTime_ms >= 75) //滤掉干扰信号,电机转速按照2000rpm时,前齿按照20T,后齿按照11T时,车轮转速为2000 / 4.55 * 20 / 11 = 800rpm,即每圈周期75ms
  157. {
  158. SignalTrigCount++;
  159. MC_SpeedSensorData.WheelTurnCount = SignalTrigCount / ((MC_ConfigParam1.SpeedSensorPoles == 0) ? 1: MC_ConfigParam1.SpeedSensorPoles);
  160. }
  161. }
  162. else if(GPIO_Pin == BREAK_IN_Pin)
  163. {
  164. //过流触发标志置位
  165. MC_Protect_OverCurrentTrig_Flag = SET;
  166. }
  167. }
  168. /* USER CODE END 2 */
  169. /**
  170. * @}
  171. */
  172. /**
  173. * @}
  174. */
  175. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/