STM32F103C8t - CAN problem
Hello guys!
I am new in using CAN interface, and for the first time, I started to use it in my project. I used STM32F103C8t. When I wanted to use CAN interface, there only happens time-out exception in run-time and I figured it out both in run-mode and then in debug mode. I wrote a very small project to work on it and I got the same problem. I doubted if the mcu is not working well, I changed it and still not change, I even changed the mcu with stm32f103cBt one and no change either.
I give you the simple code I created using HAL library and STM32Cube here and I hope you can help me
this is the code:
In this code if CAN works, the relay must toggle almost in 10HZ frequency, and if time-out happens the relay must toggle in 1Hz frequency. In practice, the relay toggles in 1Hz!!!
CAN_HandleTypeDef hcan;
CanTxMsgTypeDef can_tx_msg;
CanRxMsgTypeDef can_rx_msg;uint8_t can_tx_buff[8];int main(void){ /* USER CODE BEGIN 1 */ char *msg = 'salam'; /* 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_CAN_Init(); /* USER CODE BEGIN 2 */ hcan.pTxMsg = &can_tx_msg; hcan.pRxMsg = &can_rx_msg; /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { sprintf( (char *) hcan.pTxMsg->Data, '%s', msg ); hcan.pTxMsg->DLC = strlen(msg); HAL_CAN_Transmit(&hcan,1000); HAL_Delay(100); HAL_GPIO_TogglePin( relay_test_GPIO_Port, relay_test_Pin ); /* 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; RCC_ClkInitTypeDef RCC_ClkInitStruct; /**Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = 16; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } /**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(__FILE__, __LINE__); } /**Configure the Systick interrupt time */ HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); /**Configure the Systick */ HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); /* SysTick_IRQn interrupt configuration */ HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);}/* CAN init function */static void MX_CAN_Init(void){ hcan.Instance = CAN1; hcan.Init.Prescaler = 16; hcan.Init.Mode = CAN_MODE_NORMAL; hcan.Init.SJW = CAN_SJW_1TQ; hcan.Init.BS1 = CAN_BS1_1TQ; hcan.Init.BS2 = CAN_BS2_1TQ; hcan.Init.TTCM = DISABLE; hcan.Init.ABOM = DISABLE; hcan.Init.AWUM = DISABLE; hcan.Init.NART = DISABLE; hcan.Init.RFLM = DISABLE; hcan.Init.TXFP = DISABLE; if (HAL_CAN_Init(&hcan) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); }}/** Configure pins as * Analog * Input * Output * EVENT_OUT * EXTI*/static void MX_GPIO_Init(void){GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */ __HAL_RCC_GPIOA_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(relay_test_GPIO_Port, relay_test_Pin, GPIO_PIN_RESET); /*Configure GPIO pin : relay_test_Pin */ GPIO_InitStruct.Pin = relay_test_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(relay_test_GPIO_Port, &GPIO_InitStruct);}#stm32f103c8t #can-interface