cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F303K8 CAN(controller area network) RECEIVE PROBLEM

abdou si
Associate III
Posted on April 26, 2017 at 12:08

Hello 

i am working with CAN of STM32F303K8 i can transmit the data but i can't receive data  i get the fonction receive returne HAL_TIMEOUT 

i am working with 1M and  32 clock 

my code 

/* Includes ------------------------------------------------------------------*/

&sharpinclude 'main.h'

&sharpinclude 'stm32f3xx_hal.h'

&sharpinclude 'stdio.h'

UART_HandleTypeDef huart2;

/* USER CODE BEGIN Includes */

&sharpifdef __GNUC__

/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf

set to 'Yes') calls __io_putchar() */

&sharpdefine PUTCHAR_PROTOTYPE int __io_putchar(int ch)

&sharpelse

&sharpdefine PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

&sharpendif /* __GNUC__ */

/* USER CODE END PFP */

/* USER CODE BEGIN 4 */

/**

* @brief Retargets the C library printf function to the USART.

* @param None

* @retval None

*/

PUTCHAR_PROTOTYPE

{

/* Place your implementation of fputc here */

/* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */

// HAL_UART_Transmit(&UartHandle, (uint8_t *)&ch, 1, 0xFFFF);

HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF);

return ch;

}

/* USER CODE END Includes */

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

CAN_HandleTypeDef hcan;

/* USER CODE BEGIN PV */

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

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

void Error_Handler(void);

static void MX_GPIO_Init(void);

static void MX_CAN_Init(void);

static void MX_USART2_UART_Init(void);

/* USER CODE BEGIN PFP */

/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

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();

/* Configure the system clock */

SystemClock_Config();

/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_CAN_Init();

MX_USART2_UART_Init();

/* USER CODE BEGIN 2 */

/* USER CODE END 2 */

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

{

/* USER CODE END WHILE */

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 1.0);

HAL_Delay( 1000);

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3,0);

HAL_Delay( 1000);

HAL_StatusTypeDef status;

status = HAL_CAN_Receive(&hcan, CAN_FIFO0, 1000);

if (status != HAL_OK)

{

while(1)

{

}

}

printf('CAN Receive id : %x \n ',hcan.pRxMsg->StdId);

}

/* USER CODE END 3 */

}

/** System Clock Configuration

*/

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;

RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;

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();

}

/**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)

{

static CanTxMsgTypeDef TxMessage;

static CanRxMsgTypeDef RxMessage;

hcan.pTxMsg = &TxMessage;

hcan.pRxMsg = &RxMessage;

hcan.Instance = CAN;

hcan.Init.Prescaler = 4;

hcan.Init.Mode = CAN_MODE_NORMAL;

hcan.Init.SJW = CAN_SJW_1TQ;

hcan.Init.BS1 = CAN_BS1_5TQ;

hcan.Init.BS2 = CAN_BS2_2TQ;

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();

}

}

/* USART2 init function */

static void MX_USART2_UART_Init(void)

{

huart2.Instance = USART2;

huart2.Init.BaudRate = 9600;

huart2.Init.WordLength = UART_WORDLENGTH_8B;

huart2.Init.StopBits = UART_STOPBITS_1;

huart2.Init.Parity = UART_PARITY_NONE;

huart2.Init.Mode = UART_MODE_TX_RX;

huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;

huart2.Init.OverSampling = UART_OVERSAMPLING_16;

huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

if (HAL_UART_Init(&huart2) != HAL_OK)

{

Error_Handler();

}

}

/** 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();

__HAL_RCC_GPIOB_CLK_ENABLE();

/*Configure GPIO pin Output Level */

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);

/*Configure GPIO pin : PB3 */

GPIO_InitStruct.Pin = GPIO_PIN_3;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**

* @brief This function is executed in case of error occurrence.

* @param None

* @retval None

*/

void Error_Handler(void)

{

/* USER CODE BEGIN Error_Handler */

/* User can add his own implementation to report the HAL error return state */

while(1)

{

}

/* USER CODE END Error_Handler */

}

&sharpifdef 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,

ex: printf('Wrong parameters value: file %s on line %d\r\n', file, line) */

/* USER CODE END 6 */

}

&sharpendif

/**

* @}

*/

/**

* @}

*/

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

any help please 

#stm32 #can
2 REPLIES 2
rohit2
Associate II
Posted on May 12, 2017 at 08:42

HI ,

Am also facing same issue.

What am facing is , am not getting recieve interrupt for Can1  which is set as master mode, and my controller is Stm32f427zgt6.

HAve checked wave forms too on scope all are good.

Another thing i observed is when 2 devices are connected, in parallel having different ID , my master board start to sense, i do not understand this behavior.

Posted on May 15, 2017 at 17:45

Hi

KAdecha.Rohit

,

Which firmware package are you using? Please make sure that you are using the latest version of STM32CubeF4 which includes HAL CAN updates.

Thanks

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen