cancel
Showing results for 
Search instead for 
Did you mean: 

CAN on NUCLEO F767ZI

Mohammad Hukan
Associate
Posted on January 26, 2017 at 16:32

Hi guys,

I have been struggling with CAN bus on the F767ZI for the past week with no luck! even the loopback example provided with the STM32Cube_FW_F7_V1.5.0 is not working!!

I've tried everything I can think of with no luck! here is the simplest code I used (generated mostly by CubeMX). CAN1 connected on pins PD1 and PD0 for TX and RX respectively that are connected to an MCP2551 transceiver. PCAN-USB is used to verify the operation (in addition to an oscilloscope) and the bus is terminated with 120 ohms resistors on both sides. I'm using PlatformIO for compiling the code and uploading it to the board. I have also used the mbed_CAN_api library with no luck!

please note that I have also tried running the I2C example provided in 

STM32Cube_FW_F7_V1.5.0 for this board but it didn't work, the I2C example on the  mbed website did work though. 

&sharpinclude 'main.h'

&sharpinclude 'stm32f7xx_hal.h'

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

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

CAN_HandleTypeDef hcan1;

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

/* USER CODE BEGIN 2 */

/* USER CODE END 2 */

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

{

/* USER CODE END WHILE */

HAL_CAN_Transmit_IT(&hcan1);

/* USER CODE BEGIN 3 */

}

/* USER CODE END 3 */

}

/** System Clock Configuration

*/

void SystemClock_Config(void)

{

RCC_OscInitTypeDef RCC_OscInitStruct;

RCC_ClkInitTypeDef RCC_ClkInitStruct;

/**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_HSI;

RCC_OscInitStruct.HSIState = RCC_HSI_ON;

RCC_OscInitStruct.HSICalibrationValue = 16;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

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_HSI;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != 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);

}

/* CAN1 init function */

static void MX_CAN1_Init(void)

{

static CanTxMsgTypeDef TxMessage;

hcan1.Instance = CAN1;

hcan1.pTxMsg = &TxMessage;

hcan1.Init.Prescaler = 2;

hcan1.Init.Mode = CAN_MODE_NORMAL;

hcan1.Init.SJW = CAN_SJW_1TQ;

hcan1.Init.BS1 = CAN_BS1_11TQ;

hcan1.Init.BS2 = CAN_BS2_4TQ;

hcan1.Init.TTCM = DISABLE;

hcan1.Init.ABOM = DISABLE;

hcan1.Init.AWUM = DISABLE;

hcan1.Init.NART = DISABLE;

hcan1.Init.RFLM = DISABLE;

hcan1.Init.TXFP = DISABLE;

if (HAL_CAN_Init(&hcan1) != HAL_OK)

{

Error_Handler();

}

hcan1.pTxMsg->StdId = 0x321;

hcan1.pTxMsg->ExtId = 0x00;

hcan1.pTxMsg->RTR = CAN_RTR_DATA;

hcan1.pTxMsg->IDE = CAN_ID_STD;

hcan1.pTxMsg->DLC = 1;

hcan1.pTxMsg->Data[0] = 0x00;

}

/** Pinout Configuration

*/

static void MX_GPIO_Init(void)

{

/* GPIO Ports Clock Enable */

__HAL_RCC_GPIOD_CLK_ENABLE();

}

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

#can #nucleo-f767zi
3 REPLIES 3
Oliver Beirne
Senior
Posted on January 26, 2017 at 18:04

Hello

I have moved your post to the

https://community.st.com/community/stm32-community/stm32-forum?sr=search&searchId=c28fa0ed-1c6a-4006-9449-be5c22f65865&searchIndex=0

‌ where someone should be able to assist you.

Thanks

Oli

Mohammad Hukan
Associate
Posted on January 26, 2017 at 18:25

update: so it turned out there is some conflict resulting from using PlatformIO (not sure what exactly though) and that's why the examples were not working. They are working now with MDK from ARM. still need to test the can code though ! 

Radu Ghiga
Associate II
Posted on May 03, 2018 at 08:30

Hello

While I realize this is a pretty old topic, I would like to know if you have managed to get the CAN working, and what was the problem.

I am facing some issues also with the CAN on 767ZI. The main issue is that I get an error saying that the '

CanTxMsgTypeDef' is an undefined type. I use STM32CubeMX to initialize the 767ZI. I could not find the file where this type is defined, nor do I know where it should be defined. 

Any suggestions for where to look or what file should I include to solve this type definition problem would really help me.

Thank you very much!

Radu