can.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. ******************************************************************************
  3. * File Name : CAN.c
  4. * Description : This file provides code for the configuration
  5. * of the CAN instances.
  6. ******************************************************************************
  7. *
  8. * COPYRIGHT(c) 2017 STMicroelectronics
  9. *
  10. * Redistribution and use in source and binary forms, with or without modification,
  11. * are permitted provided that the following conditions are met:
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. ******************************************************************************
  33. */
  34. /* Includes ------------------------------------------------------------------*/
  35. #include "can.h"
  36. #include "gpio.h"
  37. /* USER CODE BEGIN 0 */
  38. /* USER CODE END 0 */
  39. CAN_HandleTypeDef hcan;
  40. /* CAN init function */
  41. void MX_CAN_Init(void)
  42. {
  43. hcan.Instance = CAN1;
  44. hcan.Init.Prescaler = 48;
  45. hcan.Init.Mode = CAN_MODE_NORMAL;
  46. hcan.Init.SJW = CAN_SJW_1TQ;
  47. hcan.Init.BS1 = CAN_BS1_3TQ;
  48. hcan.Init.BS2 = CAN_BS2_2TQ;
  49. hcan.Init.TTCM = DISABLE;
  50. hcan.Init.ABOM = ENABLE;
  51. hcan.Init.AWUM = ENABLE;
  52. hcan.Init.NART = DISABLE;
  53. hcan.Init.RFLM = DISABLE;
  54. hcan.Init.TXFP = DISABLE;
  55. if (HAL_CAN_Init(&hcan) != HAL_OK)
  56. {
  57. Error_Handler();
  58. }
  59. }
  60. void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
  61. {
  62. GPIO_InitTypeDef GPIO_InitStruct;
  63. if(canHandle->Instance==CAN1)
  64. {
  65. /* USER CODE BEGIN CAN1_MspInit 0 */
  66. /* USER CODE END CAN1_MspInit 0 */
  67. /* Peripheral clock enable */
  68. __HAL_RCC_CAN1_CLK_ENABLE();
  69. /**CAN GPIO Configuration
  70. PB8 ------> CAN_RX
  71. PB9 ------> CAN_TX
  72. */
  73. GPIO_InitStruct.Pin = GPIO_PIN_8;
  74. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  75. GPIO_InitStruct.Pull = GPIO_NOPULL;
  76. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  77. GPIO_InitStruct.Pin = GPIO_PIN_9;
  78. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  79. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  80. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  81. __HAL_AFIO_REMAP_CAN1_2();
  82. /* Peripheral interrupt init */
  83. HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 0, 0);
  84. HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
  85. /* USER CODE BEGIN CAN1_MspInit 1 */
  86. /* USER CODE END CAN1_MspInit 1 */
  87. }
  88. }
  89. void HAL_CAN_MspDeInit(CAN_HandleTypeDef* canHandle)
  90. {
  91. if(canHandle->Instance==CAN1)
  92. {
  93. /* USER CODE BEGIN CAN1_MspDeInit 0 */
  94. /* USER CODE END CAN1_MspDeInit 0 */
  95. /* Peripheral clock disable */
  96. __HAL_RCC_CAN1_CLK_DISABLE();
  97. /**CAN GPIO Configuration
  98. PB8 ------> CAN_RX
  99. PB9 ------> CAN_TX
  100. */
  101. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8|GPIO_PIN_9);
  102. /* Peripheral interrupt Deinit*/
  103. HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
  104. }
  105. /* USER CODE BEGIN CAN1_MspDeInit 1 */
  106. /* USER CODE END CAN1_MspDeInit 1 */
  107. }
  108. /* USER CODE BEGIN 1 */
  109. /* USER CODE END 1 */
  110. /**
  111. * @}
  112. */
  113. /**
  114. * @}
  115. */
  116. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/