你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32的CAN slave mode与master mode的区别是什么?

[复制链接]
奏奏奏 提问时间:2016-12-31 14:30 /
本帖最后由 奏奏奏 于 2017-1-2 23:14 编辑

用的芯片是STM32F107
因为它有两个CAN口
做CAN的中继来用
问题是将CAN1口的接收数据用CAN2口发出是正常的
但是反过来将CAN2口的接收数据用CAN1口发出就出问题了
所以就在考虑是否关系到这个问题:CAN1口是master mode,而CAN2口是slave mode,是否CAN2口的接收中断用法与CAN1口的不一样
贴一下代码:

  1. void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* hcan)
  2. {
  3.         if(hcan == &hcan1)
  4.         {
  5.                 printf("\r\n CAN        receive ID=%x",hcan->pRxMsg->StdId);
  6.                 printf("\r\n CAN        receive length=%d        \r\n CAN        receive data:\r\n",hcan->pRxMsg->DLC);
  7.                 uint8_t j=hcan->pRxMsg->DLC;
  8.                 for(uint8_t i=0;i<j;i++)
  9.                 {
  10.                         printf(" %x        ",hcan->pRxMsg->Data);
  11.                 }
  12.                 printf("\r\n");
  13.                
  14.                 //转发到CAN2端口
  15.                
  16.         //
  17.                 /*##-1- Configure CAN2 Transmission Massage #####################################*/
  18.                 hcan2.pTxMsg->StdId = hcan->pRxMsg->StdId;
  19.                 hcan2.pTxMsg->RTR = CAN_RTR_DATA;
  20.                 hcan2.pTxMsg->IDE = CAN_ID_STD;
  21.                 hcan2.pTxMsg->DLC = hcan->pRxMsg->DLC;

  22.                 uint8_t j2=hcan->pRxMsg->DLC;
  23.                 for(uint8_t i=0;i<j2;i++)
  24.                 {

  25.                         hcan2.pTxMsg->Data =hcan->pRxMsg->Data;
  26.                 }

  27.                 HAL_CAN_Transmit(&hcan2, 10);
  28.         //        
  29.                
  30.                 __HAL_CAN_ENABLE_IT(hcan,CAN_IT_FMP0);//重新开启FIF00消息挂号中断                                
  31.         }


  32.         if(hcan == &hcan2)
  33.         {
  34.                 printf("\r\n CAN        receive ID=%x",hcan->pRxMsg->StdId);
  35.                 printf("\r\n CAN        receive length=%d        \r\n CAN        receive data:\r\n",hcan->pRxMsg->DLC);
  36.                 uint8_t j=hcan->pRxMsg->DLC;
  37.                 for(uint8_t i=0;i<j;i++)
  38.                 {
  39.                         printf(" %x        ",hcan->pRxMsg->Data);
  40.                 }
  41.                 printf("\r\n");
  42.                
  43.                 //转发到CAN1端口
  44.                
  45.         //
  46.                 /*##-1- Configure CAN2 Transmission Massage #####################################*/
  47.                 hcan1.pTxMsg->StdId = hcan->pRxMsg->StdId;
  48.                 hcan1.pTxMsg->RTR = CAN_RTR_DATA;
  49.                 hcan1.pTxMsg->IDE = CAN_ID_STD;
  50.                 hcan1.pTxMsg->DLC = hcan->pRxMsg->DLC;

  51.                 uint8_t j2=hcan->pRxMsg->DLC;
  52.                 for(uint8_t i=0;i<j2;i++)
  53.                 {

  54.                         hcan1.pTxMsg->Data =hcan->pRxMsg->Data;
  55.                 }

  56.                 HAL_CAN_Transmit_IT(&hcan1);
  57.         //        
  58.                
  59.                 __HAL_CAN_ENABLE_IT(hcan,CAN_IT_FMP0);//重新开启FIF00消息挂号中断                                
  60.         }
  61. }
复制代码


收藏 2 评论12 发布时间:2016-12-31 14:30

举报

12个回答
奏奏奏 回答时间:2017-1-2 22:54:18
找到解决CAN2接收问题的办法了
上面的代码中

sFilterConfig.FilterNumber = 0;
修改为:
sFilterConfig.FilterNumber = 14;

就可以解决了。

评分

参与人数 1ST金币 +2 收起 理由
zero99 + 2 结贴奖励

查看全部评分

任风吹吹 回答时间:2016-12-31 16:24:02
CAN1口是master mode,而CAN2口是slave mode。。。。
跟这个SLAVEMODE是没有关系的,这个只是说CAN2是没有自己的过滤器,而是共享了CAN1的过滤器组,所以CAN2就叫SLAVE了。

你的问题可以这样排查:
CAN2能接收到数据吗?
CAN2单独能发送数据吗?

评分

参与人数 1ST金币 +2 收起 理由
zero99 + 2

查看全部评分

奏奏奏 回答时间:2017-1-1 01:17:58
任风吹吹 发表于 2016-12-31 16:24
CAN1口是master mode,而CAN2口是slave mode。。。。
跟这个SLAVEMODE是没有关系的,这个只是说CAN2是没有 ...

好的,谢谢!我按这个思路查一遍
damiaa 回答时间:2017-1-1 10:28:20
顶起顶起
5265325 回答时间:2017-1-1 12:09:14
五哥1 回答时间:2017-1-1 14:43:16
二楼回答正确,你需要回答这两个问题
andypanfan 回答时间:2017-1-1 15:13:48
奏奏奏 回答时间:2017-1-2 20:07:41
本帖最后由 奏奏奏 于 2017-1-2 20:09 编辑
任风吹吹 发表于 2016-12-31 16:24
CAN1口是master mode,而CAN2口是slave mode。。。。
跟这个SLAVEMODE是没有关系的,这个只是说CAN2是没有 ...

排查过了。
CAN2不能接收到数据!问题在这里
CAN2单独发送数据正常。
附上main.c的代码:

  1. /**
  2.   ******************************************************************************
  3.   * File Name          : main.c
  4.   * Description        : Main program body
  5.   ******************************************************************************
  6.   *
  7.   * COPYRIGHT(c) 2016 STMicroelectronics
  8.   *
  9.   * Redistribution and use in source and binary forms, with or without modification,
  10.   * are permitted provided that the following conditions are met:
  11.   *   1. Redistributions of source code must retain the above copyright notice,
  12.   *      this list of conditions and the following disclaimer.
  13.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  14.   *      this list of conditions and the following disclaimer in the documentation
  15.   *      and/or other materials provided with the distribution.
  16.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  17.   *      may be used to endorse or promote products derived from this software
  18.   *      without specific prior written permission.
  19.   *
  20.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.   *
  31.   ******************************************************************************
  32.   */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32f1xx_hal.h"
  35. #include "can.h"
  36. #include "usart.h"
  37. #include "gpio.h"

  38. /* USER CODE BEGIN Includes */

  39. /* USER CODE END Includes */

  40. /* Private variables ---------------------------------------------------------*/

  41. /* USER CODE BEGIN PV */
  42. /* Private variables ---------------------------------------------------------*/
  43. CAN_FilterConfTypeDef  sFilterConfig;
  44. static CanTxMsgTypeDef        TxMessage;
  45. static CanRxMsgTypeDef        RxMessage;
  46. /* USER CODE END PV */

  47. /* Private function prototypes -----------------------------------------------*/
  48. void SystemClock_Config(void);
  49. void Error_Handler(void);

  50. /* USER CODE BEGIN PFP */
  51. /* Private function prototypes -----------------------------------------------*/

  52. /* USER CODE END PFP */

  53. /* USER CODE BEGIN 0 */

  54. /* USER CODE END 0 */

  55. int main(void)
  56. {

  57.   /* USER CODE BEGIN 1 */

  58.   /* USER CODE END 1 */

  59.   /* MCU Configuration----------------------------------------------------------*/

  60.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  61.   HAL_Init();

  62.   /* Configure the system clock */
  63.   SystemClock_Config();

  64.   /* Initialize all configured peripherals */
  65.   MX_GPIO_Init();
  66.   MX_CAN1_Init();
  67.   MX_CAN2_Init();
  68.   MX_USART1_UART_Init();

  69.   /* USER CODE BEGIN 2 */
  70.   printf("\n\r UART Printf Example: retarget the C library printf function to the UART\n\r");
  71.        
  72. //  hcan1.pTxMsg = &TxMessage;
  73.   hcan2.pRxMsg = &RxMessage;
  74.         __HAL_CAN_ENABLE_IT(&hcan2,CAN_IT_FMP1);//重新开启FIF00消息挂号中断
  75. //        __HAL_CAN_ENABLE_IT(&hcan2,CAN_IT_FMP0);//重新开启FIF00消息挂号中断
  76.   printf("**** This is CAN test program ****\r\n\r\n");

  77.   /*##-1- Configure CAN2 Transmission Massage #####################################*/
  78. //  hcan2.pTxMsg->StdId = 0x123;
  79. //  hcan2.pTxMsg->RTR = CAN_RTR_DATA;
  80. //  hcan2.pTxMsg->IDE = CAN_ID_STD;
  81. //  hcan2.pTxMsg->DLC = 8;
  82. //  hcan2.pTxMsg->Data[0] = 'C';
  83. //  hcan2.pTxMsg->Data[1] = 'A';
  84. //  hcan2.pTxMsg->Data[2] = 'N';
  85. //  hcan2.pTxMsg->Data[3] = ' ';
  86. //  hcan2.pTxMsg->Data[4] = 'T';
  87. //  hcan2.pTxMsg->Data[5] = 'e';
  88. //  hcan2.pTxMsg->Data[6] = 's';
  89. //  hcan2.pTxMsg->Data[7] = 't';

  90.   /*##-2- Configure the CAN1 Filter ###########################################*/
  91.   sFilterConfig.FilterNumber = 0;
  92.   sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  93.   sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  94.   sFilterConfig.FilterIdHigh = 0x0000;
  95.   sFilterConfig.FilterIdLow = 0x0000;
  96.   sFilterConfig.FilterMaskIdHigh = 0x0000;
  97.   sFilterConfig.FilterMaskIdLow = 0x0000;
  98.   sFilterConfig.FilterFIFOAssignment = 1;
  99.   sFilterConfig.FilterActivation = ENABLE;
  100.   sFilterConfig.BankNumber = 14;
  101.   HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig);       
  102.   /* USER CODE END 2 */

  103.   /* Infinite loop */
  104.   /* USER CODE BEGIN WHILE */
  105.   while (1)
  106.   {
  107.   /* USER CODE END WHILE */

  108.   /* USER CODE BEGIN 3 */
  109. //      printf("\n\r welcome to www.waveshere.com !!!\n\r");
  110.       HAL_Delay(1000);
  111.       /*##-3- Start the CAN2 Transmission process #####################################*/
  112. //      if(HAL_CAN_Transmit(&hcan2, 10) != HAL_OK)
  113. //      {
  114. //          /* Transmition Error */
  115. //          Error_Handler();
  116. //      }
  117. //                        HAL_CAN_Transmit(&hcan2, 10);
  118. //               
  119. //      if(HAL_CAN_GetState(&hcan2) != HAL_CAN_STATE_READY)
  120. //      {
  121. //          Error_Handler();
  122. //            
  123. //      }
  124.       /*##-4- Start the CAN1 Reception process ########################################*/
  125. //      if(HAL_CAN_Receive(&hcan1, CAN_FIFO0,10) != HAL_OK)
  126. //      {
  127. //          /* Reception Error */
  128. //          Error_Handler();   
  129. //      }
  130. //                        HAL_CAN_Receive(&hcan1, CAN_FIFO0,10);
  131.                        
  132. //      if(HAL_CAN_GetState(&hcan1) != HAL_CAN_STATE_READY)
  133. //      {
  134. //          Error_Handler();
  135. //      }
  136. //      printf("StdId : %x\r\n",hcan1.pRxMsg->StdId);
  137. //      printf("RxMsg : %s",hcan1.pRxMsg->Data);
  138. //      printf("\r\n\r\n");     
  139.         
  140. //      HAL_Delay(1000);

  141.   }
  142.   /* USER CODE END 3 */

  143. }

  144. /** System Clock Configuration
  145. */
  146. void SystemClock_Config(void)
  147. {

  148.   RCC_OscInitTypeDef RCC_OscInitStruct;
  149.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  150.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  151.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  152.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV5;
  153.   RCC_OscInitStruct.Prediv1Source = RCC_PREDIV1_SOURCE_PLL2;
  154.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  155.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  156.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  157.   RCC_OscInitStruct.PLL2.PLL2State = RCC_PLL2_ON;
  158.   RCC_OscInitStruct.PLL2.PLL2MUL = RCC_PLL2_MUL8;
  159.   RCC_OscInitStruct.PLL2.HSEPrediv2Value = RCC_HSE_PREDIV2_DIV5;
  160.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  161.   {
  162.     Error_Handler();
  163.   }

  164.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  165.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  166.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  167.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  168.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  169.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  170.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  171.   {
  172.     Error_Handler();
  173.   }

  174.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  175.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  176.   __HAL_RCC_PLLI2S_ENABLE();

  177.   /* SysTick_IRQn interrupt configuration */
  178.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  179. }

  180. /* USER CODE BEGIN 4 */
  181. void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* hcan)
  182. {
  183.         printf("\r\n CAN        receive ID=%x",hcan->pRxMsg->StdId);
  184.         printf("\r\n CAN        receive length=%d        \r\n CAN        receive data:\r\n",hcan->pRxMsg->DLC);
  185.         uint8_t j=hcan->pRxMsg->DLC;
  186.         for(uint8_t i=0;i<j;i++)
  187.         {
  188.                 printf(" %x        ",hcan->pRxMsg->Data[i]);
  189.         }
  190.         printf("\r\n");
  191.        
  192.         //转发到CAN2端口
  193.        
  194. //
  195.   /*##-1- Configure CAN2 Transmission Massage #####################################*/
  196. //  hcan1.pTxMsg->StdId = hcan->pRxMsg->StdId;
  197. //  hcan1.pTxMsg->RTR = CAN_RTR_DATA;
  198. //  hcan1.pTxMsg->IDE = CAN_ID_STD;
  199. //  hcan1.pTxMsg->DLC = hcan->pRxMsg->DLC;
  200. ////  hcan2.pTxMsg->Data[0] = 'C';
  201. ////  hcan2.pTxMsg->Data[1] = 'A';
  202. ////  hcan2.pTxMsg->Data[2] = 'N';
  203. ////  hcan2.pTxMsg->Data[3] = ' ';
  204. ////  hcan2.pTxMsg->Data[4] = 'T';
  205. ////  hcan2.pTxMsg->Data[5] = 'e';
  206. ////  hcan2.pTxMsg->Data[6] = 's';
  207. ////  hcan2.pTxMsg->Data[7] = 't';
  208. //        uint8_t j2=hcan->pRxMsg->DLC;
  209. //        for(uint8_t i=0;i<j2;i++)
  210. //        {
  211. ////                printf(" %x        ",hcan->pRxMsg->Data[i]);
  212. //                hcan1.pTxMsg->Data[i] =hcan->pRxMsg->Data[i];
  213. //        }
  214. ////        printf("\r\n");
  215. //        HAL_CAN_Transmit(&hcan1, 10);
  216. //       
  217.        
  218. //        __HAL_CAN_ENABLE_IT(hcan,CAN_IT_FMP0);//重新开启FIF00消息挂号中断               
  219.         __HAL_CAN_ENABLE_IT(hcan,CAN_IT_FMP1);//重新开启FIF00消息挂号中断               
  220. }
  221. /* USER CODE END 4 */

  222. /**
  223.   * @brief  This function is executed in case of error occurrence.
  224.   * @param  None
  225.   * @retval None
  226.   */
  227. void Error_Handler(void)
  228. {
  229.   /* USER CODE BEGIN Error_Handler */
  230.   /* User can add his own implementation to report the HAL error return state */
  231.   while(1)
  232.   {
  233.   }
  234.   /* USER CODE END Error_Handler */
  235. }

  236. #ifdef USE_FULL_ASSERT

  237. /**
  238.    * @brief Reports the name of the source file and the source line number
  239.    * where the assert_param error has occurred.
  240.    * @param file: pointer to the source file name
  241.    * @param line: assert_param error line source number
  242.    * @retval None
  243.    */
  244. void assert_failed(uint8_t* file, uint32_t line)
  245. {
  246.   /* USER CODE BEGIN 6 */
  247.   /* User can add his own implementation to report the file name and line number,
  248.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  249.   /* USER CODE END 6 */

  250. }

  251. #endif

  252. /**
  253.   * @}
  254.   */

  255. /**
  256.   * @}
  257. */

  258. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码


小小超 回答时间:2017-1-3 08:40:33
学习一下!!!!
jcx0324 回答时间:2017-1-3 09:11:19
其实是一样的,只是硬件资源上的区别

评分

参与人数 1ST金币 +2 收起 理由
zero99 + 2

查看全部评分

Ian-392967 回答时间:2017-3-11 19:16:29
大哥猜对了!更您学的在网上分享一下资料。
zbber 回答时间:2017-3-11 23:16:53
CAN1口是master mode,而CAN2口是slave mode

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版