[STM32程序在发送CAN消息时退出主循环

问题描述 投票:0回答:1

我正在使用具有HAL库的STM32微控制器创建汽车CAN网络。上传的代码在STM32F446RE Nucleo板上使用。

我的目的是使用这样的空函数在程序开始时发送八个帧“ NMT”:

void cansend_NMT(uint8_t State, uint8_t NodeID)
{
    frame_NMT.TxHeader.StdId = 0x000;
    frame_NMT.TxHeader.RTR = CAN_RTR_DATA;
    frame_NMT.TxHeader.IDE = CAN_ID_STD;
    frame_NMT.TxHeader.DLC = 2;
    frame_NMT.TxHeader.TransmitGlobalTime=DISABLE;

    frame_NMT.TxData[0]=State;
    frame_NMT.TxData[1]=NodeID;

      if(HAL_CAN_AddTxMessage(&hcan1,&frame_NMT.TxHeader, frame_NMT.TxData, &TxMailbox) != HAL_OK)
      {
        /* Transmission request Error */
        Error_Handler();
      }
      while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan1) == 3) {}

      HAL_Delay(10);
}

然后在主循环中,我每100ms使用这样的功能发送一帧“ SYNC”:

void cansend_SYNC (void)
{
    frame_SYNC.TxHeader.StdId = 0x080;
    frame_SYNC.TxHeader.RTR = CAN_RTR_REMOTE;
    frame_SYNC.TxHeader.IDE = CAN_ID_STD;
    //frame_SYNC.TxHeader.DLC = 0;
    frame_SYNC.TxHeader.TransmitGlobalTime=DISABLE;


      if(HAL_CAN_AddTxMessage(&hcan1,&frame_SYNC.TxHeader, frame_SYNC.TxData, &TxMailbox) != HAL_OK)
      {
        /* Transmission request Error */
        Error_Handler();
      }
  while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan1) == 3) {}

      HAL_Delay(100);
}

输出看起来是这样的:前8个NMT帧已成功发送,然后在5个SYNC帧之后,程序退出主循环并再次发送8个NMT帧和5个SYNC帧。在调试会话期间,我注意到每次退出后程序都会返回到行int main(){

这是可怕的主体:

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  ** This notice applies to any and all portions of this file
  * that are not between comment pairs USER CODE BEGIN and
  * USER CODE END. Other portions of this file, whether 
  * inserted by the user or by software development tools
  * are owned by their respective copyright owners.
  *
  * COPYRIGHT(c) 2019 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "can.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */




/*## Definicje zmiennych ##*/
extern CAN_FilterTypeDef    filter_0;
CAN_RxHeaderTypeDef     RxHeader;
uint8_t                 RxData[8];
uint32_t                TxMailbox;
int                     counter = 0;







/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */


  /* MCU Configuration--------------------------------------------------------*/

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

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_CAN1_Init();
  /* USER CODE BEGIN 2 */

CANopen_OD();
config_filter_0();


HAL_CAN_WakeUp(&hcan1);
if (HAL_CAN_ConfigFilter(&hcan1, &filter_0)!= HAL_OK){
        Error_Handler();}

if (HAL_CAN_Start(&hcan1)!=HAL_OK){
        Error_Handler();}

if (HAL_CAN_ActivateNotification(&hcan1,CAN_IT_TX_MAILBOX_EMPTY) != HAL_OK){
        Error_Handler();}

/*******************************************************************************
    Initialization
*******************************************************************************/
cansend_NMT(OPERATIONAL_STATE, BMS.NodeID);
cansend_NMT(OPERATIONAL_STATE, Inverter_1.NodeID);
cansend_NMT(OPERATIONAL_STATE, Inverter_2.NodeID);
cansend_NMT(OPERATIONAL_STATE, MPPT_1.NodeID);
cansend_NMT(OPERATIONAL_STATE, MPPT_2.NodeID);
cansend_NMT(OPERATIONAL_STATE, MPPT_3.NodeID);
cansend_NMT(OPERATIONAL_STATE, Lights_Controller.NodeID);
cansend_NMT(OPERATIONAL_STATE, Dashboard.NodeID);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
/*******************************************************************************
            Start the transmission
//*******************************************************************************/
      cansend_SYNC();


    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */


}

  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage 
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 72;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 2;
  RCC_OscInitStruct.PLL.PLLR = 2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan)
{

}

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{

}

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */

  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{ 
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

什么可能导致问题?看起来像经过一定数量的发送消息后,整个MCU都会重置。同样,当我禁用HAL_Delay()时,我的CAN分析仪将读取1600-2000成功发送的帧,然后MCU复位。

c stm32 can-bus hal
1个回答
0
投票

董事会工作不正确,以某种方式被破坏。当我尝试另一块板时,一切都很好。

甚至很简单

while(1){
i++;
}

没有工作。测试了Nucleo F103RBT6上的代码,没有问题。线程可以关闭。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.