can.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. ************************************************************************************************
  3. * @文件 : can.c
  4. * @作者 : 樊春春
  5. * @版本 : V1.0
  6. * @时间 : 2022/05/30 22:47:14
  7. * @邮箱 : [email protected]
  8. * @说明 : CAN初始化
  9. ************************************************************************************************
  10. **/
  11. #include "can.h"
  12. #include "fly_param.h"
  13. #include <string.h>
  14. CanRxMsg can1_recv_msg;
  15. CanRxMsg can2_recv_msg;
  16. CanData_TypeDef CanDataCan1; // CAN1数据
  17. CanData_TypeDef CanDataCan2; // CAN2数据
  18. void can_network_init(void)
  19. {
  20. CAN_InitTypeDef can_parameter;
  21. CAN_FilterInitTypeDef can_filter;
  22. CAN_StructInit(&can_parameter);
  23. CAN_FilterInit(&can_filter);
  24. /* initialize CAN register */
  25. CAN_DeInit(CAN1);
  26. CAN_DeInit(CAN2);
  27. /* initialize CAN parameters */
  28. can_parameter.CAN_TTCM = DISABLE;
  29. can_parameter.CAN_ABOM = DISABLE;
  30. can_parameter.CAN_AWUM = DISABLE;
  31. can_parameter.CAN_NART = DISABLE;
  32. can_parameter.CAN_RFLM = DISABLE;
  33. can_parameter.CAN_TXFP = DISABLE;
  34. can_parameter.CAN_Mode = CAN_Mode_Normal;
  35. can_parameter.CAN_SJW = CAN_SJW_1tq;
  36. can_parameter.CAN_BS1 = CAN_BS1_10tq; // CAN_BT_BS1_6TQ;
  37. can_parameter.CAN_BS2 = CAN_BS2_3tq; // CAN_BT_BS2_4TQ;
  38. /* 1MBps */
  39. #if CAN_BAUDRATE == 1000
  40. can_parameter.prescaler = 5;
  41. /* 500KBps */
  42. #elif CAN_BAUDRATE == 500
  43. can_parameter.prescaler = 10;
  44. /* 250KBps */
  45. #elif CAN_BAUDRATE == 250
  46. can_parameter.CAN_Prescaler = 12; // 24;//20;
  47. /* 125KBps */
  48. #elif CAN_BAUDRATE == 125
  49. can_parameter.prescaler = 40;
  50. /* 100KBps */
  51. #elif CAN_BAUDRATE == 100
  52. can_parameter.prescaler = 50;
  53. /* 50KBps */
  54. #elif CAN_BAUDRATE == 50
  55. can_parameter.prescaler = 100;
  56. /* 20KBps */
  57. #elif CAN_BAUDRATE == 20
  58. can_parameter.prescaler = 250;
  59. #else
  60. #error "please select list can baudrate in private defines in main.c "
  61. #endif
  62. /* initialize CAN */
  63. CAN_Init(CAN1, &can_parameter);
  64. CAN_Init(CAN2, &can_parameter);
  65. /* initialize filter */
  66. can_filter.CAN_FilterNumber = 0;
  67. can_filter.CAN_FilterMode = CAN_FilterMode_IdMask;
  68. can_filter.CAN_FilterScale = CAN_FilterScale_32bit;
  69. can_filter.CAN_FilterIdHigh = 0x0000;
  70. can_filter.CAN_FilterIdLow = 0x0000;
  71. can_filter.CAN_FilterMaskIdHigh = 0x0000;
  72. can_filter.CAN_FilterMaskIdLow = 0x0000;
  73. can_filter.CAN_FilterFIFOAssignment = CAN_FIFO0;
  74. can_filter.CAN_FilterActivation = ENABLE;
  75. CAN_FilterInit(&can_filter);
  76. /* CAN1 filter number */
  77. can_filter.CAN_FilterNumber = 15;
  78. can_filter.CAN_FilterMode = CAN_FilterMode_IdMask;
  79. can_filter.CAN_FilterScale = CAN_FilterScale_32bit;
  80. can_filter.CAN_FilterIdHigh = 0x0000;
  81. can_filter.CAN_FilterIdLow = 0x0000;
  82. can_filter.CAN_FilterMaskIdHigh = 0x0000;
  83. can_filter.CAN_FilterMaskIdLow = 0x0000;
  84. can_filter.CAN_FilterFIFOAssignment = CAN_FIFO0;
  85. can_filter.CAN_FilterActivation = ENABLE;
  86. CAN_FilterInit(&can_filter);
  87. can_nvic_config();
  88. /* CAN FIFO0 消息接收中断使能 */
  89. CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
  90. /* CAN FIFO0 消息接收中断使能 */
  91. CAN_ITConfig(CAN2, CAN_IT_FMP0, ENABLE);
  92. }
  93. void can_nvic_config(void)
  94. {
  95. /* configure CAN0 NVIC */
  96. NVIC_InitTypeDef NVIC_InitStructure;
  97. NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
  98. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  99. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  100. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  101. NVIC_Init(&NVIC_InitStructure);
  102. /* configure CAN1 NVIC */
  103. NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
  104. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  105. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  106. NVIC_Init(&NVIC_InitStructure);
  107. }
  108. /*!
  109. \brief this function handles CAN0 RX0 exception
  110. \param[in] none
  111. \param[out] none
  112. \retval none
  113. */
  114. void CAN1_RX0_IRQHandler(void)
  115. {
  116. /* check the receive message */
  117. CAN_Receive(CAN1, CAN_FIFO0, &can1_recv_msg);
  118. if ((can1_recv_msg.IDE == CAN_Id_Extended) && (CAN_MSG_LEN == can1_recv_msg.DLC))
  119. {
  120. memcpy(CanDataCan1.buf, can1_recv_msg.Data, CAN_MSG_LEN);
  121. CanDataCan1.can_id = can1_recv_msg.ExtId;
  122. InsertQueue(&CanQueueCan1, CanDataCan1);
  123. }
  124. OSSemPost(can1_sem);
  125. }
  126. /*!
  127. \brief this function handles CAN1 RX0 exception
  128. \param[in] none
  129. \param[out] none
  130. \retval none
  131. */
  132. void CAN2_RX0_IRQHandler(void)
  133. {
  134. /* check the receive message */
  135. CAN_Receive(CAN1, CAN_FIFO0, &can2_recv_msg);
  136. if (CAN_MSG_LEN == can2_recv_msg.DLC)
  137. {
  138. memcpy(CanDataCan2.buf, can2_recv_msg.Data, CAN_MSG_LEN);
  139. if (can2_recv_msg.IDE == CAN_Id_Extended)
  140. {
  141. CanDataCan2.can_id = can2_recv_msg.ExtId;
  142. }
  143. else
  144. {
  145. CanDataCan2.can_id = can2_recv_msg.StdId;
  146. }
  147. InsertQueue(&CanQueueCan2, CanDataCan2);
  148. }
  149. OSSemPost(can2_sem);
  150. }