2018-03-15 02:24 PM
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-interface2018-03-15 03:31 PM
If you don't have a transceiver chip, then you must pull up the CanRx pin, or the processor will hang at startup.
if you can single step, can you see the chip working upto a point ?
2018-03-15 03:33 PM
Have transceivers and something at the end of the cable to talk too, and respond/acknowledge reception.
2018-03-16 03:37 AM
I forgot to say that, I used MCP2551 as CAN transceiver and I mounted this IC on the board like this(forget 100nF in RX, I hadn't assign true value for it at that time):
and besides, I tested my code at loop-back mode in which, apparently, there is no need for external electrical connections, but I didn't get any result
About your question, I changed the code to this
while (1) {
sprintf( (char *) hcan.pTxMsg->Data, ''%s'', msg ); hcan.pTxMsg->DLC = strlen(msg); HAL_CAN_Transmit(&hcan,10); }and I didn't see any activity at mcu CAN output. After it, I unmounted the MCP2551 chip and put a 10K pull-up resistor and I checked it again and still no output!
2018-03-16 03:40 AM
No clive, but as I responded to T.J., I even tested the CAN in loop-back mode and I didn't get any result
2018-03-16 06:39 AM
I started with a third party transceiver, the CanDo unit.
I made it transmit every second, until I had the receiver working.
Then I worked on the transmitter code.
________________ Attachments : image001.jpg : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hxt5&d=%2Fa%2F0X0000000b1r%2FSHSsvxUyauWbgi3owlYmHWp.mub.JuUc5a9SY6Mijmo&asPdf=falseimage002.jpg : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hxt0&d=%2Fa%2F0X0000000b1s%2FM.yXvzpko9QTcjtIcIJczaZhx5KLtuuPCHF9MMF2X.E&asPdf=false