AnsweredAssumed Answered

STM32f7 CAN bus ACK error, no interrupt triggered

Question asked by Lucian Andercou on Sep 11, 2017

I am trying to send a CAN message from a  CanOe simulation which runs on my computer to an STM32f7 discovery board.

 

I am using CANcaseXL and TCAN1042EVM transceiver to send the CAN message and I'm getting an ACK error in the CanOe simulation. I measured TX on TP1 test point of the transceiver and it seems that the message is being sent correctly but it is not acknowledged by the board. 

 

The code I'm using is listed below: 

can.c file

 

#include "stm32f7xx_hal.h"
#include "stm32f7xx_hal_gpio.h"
#include "stm32f7xx_hal_can.h"
#include "stm32746g_discovery.h"
#include "stm32746g_discovery_lcd.h"


CAN_HandleTypeDef HCAN1;
CanTxMsgTypeDef TxMessage; /* Tx message struct */
CanRxMsgTypeDef RxMessage; /* Rx message struct */

void vCanInitialize()
{
GPIO_InitTypeDef GPIO_InitStruct;
CAN_FilterConfTypeDef CAN_FilterStruct;

__HAL_RCC_CAN1_CLK_ENABLE(); /* Enable CANBUS-1 clock */
__GPIOB_CLK_ENABLE(); /* Enable GPIOB clock */

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; /* Alternate Function Push-Pull mode */
GPIO_InitStruct.Pull = GPIO_PULLUP; /* No resistor */
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_CAN1; /* Alternate pin function: AF9 / CAN1 */
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9; /* PB8 CN7 D15(CAN1_RX) & PB9 CN7 D14(CAN1_TX) */
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* Initialize registers */

/* 500kbps fCANCLK:54MHz */
/* TQ = 54MHz / (Prescaler + 1) */
/* CAN Bitrate = 1 / ((SJW + BS1 + BS2) * TQ) bps */
HCAN1.Instance = CAN1;
HCAN1.pTxMsg = &TxMessage; /* Pointer to CAN Tx message */
HCAN1.pRxMsg = &RxMessage; /* Pointer to CAN Rx message */

HCAN1.Init.Prescaler = 8;
HCAN1.Init.Mode = CAN_MODE_NORMAL; /* Normal communication mode */
HCAN1.Init.SJW = CAN_SJW_1TQ; /* 1 time-quanta re-sync jump width */
HCAN1.Init.BS1 = CAN_BS1_9TQ; /* 9 tq before sampling point */
HCAN1.Init.BS2 = CAN_BS2_2TQ; /* 2 tq after sampling point (before end) */
HCAN1.Init.TTCM = DISABLE; /* Time-triggered communication */
HCAN1.Init.ABOM = DISABLE; /* Automatic bus-off management */
HCAN1.Init.AWUM = DISABLE; /* Automatic wake-up mode */
HCAN1.Init.NART = DISABLE; /* Non-automatic retransmission mode */
HCAN1.Init.RFLM = DISABLE; /* Receive FIFO locked mode */
HCAN1.Init.TXFP = DISABLE; /* Transmit FIFO priority */
HAL_CAN_Init(&HCAN1); /* Init CAN1 registers */

CAN_FilterStruct.FilterIdHigh = 0x0000; /* Upper 16bit filter ID */
CAN_FilterStruct.FilterIdLow = 0x0000; /* Filter lower 16bit ID */
CAN_FilterStruct.FilterMaskIdHigh = 0x0000; /* Upper 16bit filter mask */
CAN_FilterStruct.FilterMaskIdLow = 0x0000; /* Lower 16bit filter mask */
CAN_FilterStruct.FilterFIFOAssignment = CAN_FILTER_FIFO0; /* Which FIFO will be assigned to filter */
CAN_FilterStruct.FilterNumber = 0;
CAN_FilterStruct.FilterMode = CAN_FILTERMODE_IDMASK; /* Identifier mask mode */
CAN_FilterStruct.FilterScale = CAN_FILTERSCALE_32BIT; /* 32bit ID filter */
CAN_FilterStruct.FilterActivation = ENABLE; /* Enable this filter */
CAN_FilterStruct.BankNumber = 14; /* Start slave bank filter (?) */
HAL_CAN_ConfigFilter(&HCAN1, &CAN_FilterStruct); /* Initialize filter */

HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 1, 1); /* Set CAN1 Rx interrupt priority to 1-1 */
HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn); /* Enable CAN1 Rx Interrupt */
__HAL_CAN_ENABLE_IT(&HCAN1, CAN_IT_FMP0); /* Enable 'message pending in FIFO0' interrupt */

}

void CAN1SendMessage(uint16_t SndID, uint8_t length, uint8_t *data)
{
HCAN1.pTxMsg->RTR = CAN_RTR_DATA; /* Remote transmission request:data */
HCAN1.pTxMsg->IDE = CAN_ID_STD; /* Identifier type: standard */
HCAN1.pTxMsg->StdId = SndID; /* Standard ID of this device */
HCAN1.pTxMsg->DLC = length; /* Specify the data length */
uint8_t d;

for(d = 0; d < length; ++d)
{
HCAN1.pTxMsg->Data[d] = *(data + d); /* Write every byte to Data[] variable */
}
HAL_CAN_Transmit(&HCAN1, 500); /* Transmit the packet */
}

/* CAN1 Rx FIFO0 interrupt service routine */
void CAN1_RX0_IRQHandler()
{
if(HAL_CAN_Receive(&HCAN1, 0, 5000) == HAL_OK) /* Packet received from CANBUS successfully */
{
BSP_LCD_DisplayStringAt(50, 100, (uint8_t*)"Recieve CAN Data!!!", CENTER_MODE);
}
}

/* canbus.c */

main.c file :

 

/**
******************************************************************************
* @file Templates/Src/main.c
* @author MCD Application Team
* @version V1.0.3
* @date 22-April-2016
* @brief STM32F7xx HAL API Template project
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
*
* 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.
*
******************************************************************************
*/

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32746g_discovery.h"
#include "stm32746g_discovery_lcd.h"
/** @addtogroup STM32F7xx_HAL_Examples
* @{
*/

/** @addtogroup Templates
* @{
*/

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void SystemClock_Config(void);
static void Error_Handler(void);
static void MPU_Config(void);
static void CPU_CACHE_Enable(void);
extern void vCanInitialize();
extern void CAN1SendMessage(uint16_t SndID, uint8_t length, uint8_t *data);

/* Private functions ---------------------------------------------------------*/

/**
* @brief Main program
* @param None
* @retval None
*/

static void LCD_SetHint(void)
{
/* Clear the LCD */
BSP_LCD_Clear(LCD_COLOR_WHITE);

/* Set LCD Demo description */
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_FillRect(0, 0, BSP_LCD_GetXSize(), 80);
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
BSP_LCD_SetFont(&Font24);
BSP_LCD_DisplayStringAt(0, 0, (uint8_t*)"LCD", CENTER_MODE);
BSP_LCD_SetFont(&Font12);
BSP_LCD_DisplayStringAt(0, 30, (uint8_t*)"This example shows the different", CENTER_MODE);
BSP_LCD_DisplayStringAt(0, 45, (uint8_t*)"LCD Features, use BUTTON", CENTER_MODE);
BSP_LCD_DisplayStringAt(0, 60, (uint8_t*)"to display next page", CENTER_MODE);

/* Set the LCD Text Color */
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_DrawRect(10, 90, BSP_LCD_GetXSize() - 20, BSP_LCD_GetYSize()- 100);
BSP_LCD_DrawRect(11, 91, BSP_LCD_GetXSize() - 22, BSP_LCD_GetYSize()- 102);
}

int main(void)
{
/* This project template calls firstly two functions in order to configure MPU feature
and to enable the CPU Cache, respectively MPU_Config() and CPU_CACHE_Enable().
These functions are provided as template implementation that User may integrate
in his application, to enhance the performance in case of use of AXI interface
with several masters. */
uint8_t data[8] = {0x03, 0xE2, 0x22, 0x31, 0x00, 0x00, 0x00, 0x00};
/* Configure the MPU attributes as Write Through */
MPU_Config();

/* Enable the CPU Cache */
CPU_CACHE_Enable();

/* STM32F7xx HAL library initialization:
- Configure the Flash ART accelerator on ITCM interface
- Configure the Systick to generate an interrupt each 1 msec
- Set NVIC Group Priority to 4
- Low Level Initialization
*/
HAL_Init();

/* Configure the System clock to have a frequency of 216 MHz */
SystemClock_Config();


/* Add your application code here
*/
vCanInitialize();

/* Infinite loop */
while (1)
{
CAN1SendMessage(0x271, sizeof(data), data);
}
}

/**
* @brief System Clock Configuration
* The system Clock is configured as follow :
* System Clock source = PLL (HSE)
* SYSCLK(Hz) = 216000000
* HCLK(Hz) = 216000000
* AHB Prescaler = 1
* APB1 Prescaler = 4
* APB2 Prescaler = 2
* HSE Frequency(Hz) = 25000000
* PLL_M = 25
* PLL_N = 432
* PLL_P = 2
* PLL_Q = 9
* VDD(V) = 3.3
* Main regulator output voltage = Scale1 mode
* Flash Latency(WS) = 7
* @param None
* @retval None
*/
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;

/* Enable HSE Oscillator and activate PLL with HSE as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 432;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 9;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}

/* activate the OverDrive to reach the 216 Mhz Frequency */
if(HAL_PWREx_EnableOverDrive() != HAL_OK)
{
Error_Handler();
}

/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
clocks dividers */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)
{
Error_Handler();
}
}

/**
* @brief This function is executed in case of error occurrence.
* @param None
* @retval None
*/
static void Error_Handler(void)
{
/* User may add here some code to deal with this error */
while(1)
{
}
}

/**
* @brief Configure the MPU attributes as Write Through for SRAM1/2.
* @note The Base Address is 0x20010000 since this memory interface is the AXI.
* The Region Size is 256KB, it is related to SRAM1 and SRAM2 memory size.
* @param None
* @retval None
*/
static void MPU_Config(void)
{
MPU_Region_InitTypeDef MPU_InitStruct;

/* Disable the MPU */
HAL_MPU_Disable();

/* Configure the MPU attributes as WT for SRAM */
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
MPU_InitStruct.BaseAddress = 0x20010000;
MPU_InitStruct.Size = MPU_REGION_SIZE_256KB;
MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
MPU_InitStruct.IsCacheable = MPU_ACCESS_CACHEABLE;
MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
MPU_InitStruct.Number = MPU_REGION_NUMBER0;
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
MPU_InitStruct.SubRegionDisable = 0x00;
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;

HAL_MPU_ConfigRegion(&MPU_InitStruct);

/* Enable the MPU */
HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
}

/**
* @brief CPU L1-Cache enable.
* @param None
* @retval None
*/
static void CPU_CACHE_Enable(void)
{
/* Enable I-Cache */
SCB_EnableICache();

/* Enable D-Cache */
SCB_EnableDCache();
}

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

/* Infinite loop */
while (1)
{
}
}
#endif

/**
* @}
*/

/**
* @}
*/

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

I've also attached a snapshot of my simulation setup.

 

Any help is much appreciated.

Attachments

Outcomes