can.c 4.8 KB

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