cancel
Showing results for 
Search instead for 
Did you mean: 

I can't experiment - I can't receive parcels with a CAN harness and special software.

MIgos.1
Associate II

Hello! I can't experiment - I can't receive parcels with a CAN harness and special software. Here is a similar experiment https://www.programmersought.com/article/2185872275/.

I have elements:

1 stm32f103c8t6 board,

2 Debugger st-link v2 (china),

3 SN65HVD230 CAN Bus transceiver,

4 Danfoss CG150 CAN harness.

I have software:

1 STM32CUBEMX 6, FW_F1 V1.8.3

2 Keil 5,

3 Danfoss CAN King (CAN wiretapping software).

Everything is being built and debugged, I have met all the conditions, but I do not see the parcels sent by me in CAN King. Can you help? Thank you in advance! Thank you for understanding.

* USER CODE BEGIN PV */
extern CAN_HandleTypeDef hcan;
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_NVIC_Init(void);
/* USER CODE BEGIN PFP */
extern uint8_t CAN_TxMessage(uint8_t ide,uint32_t id,uint8_t len,uint8_t *data);
extern void CAN_Config(void);
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
 
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
uint8_t data[8]={1,2,3,4,5,6,7,8};
  /* 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_TIM2_Init();
  MX_CAN_Init();
 
  /* Initialize interrupts */
  MX_NVIC_Init();
  /* USER CODE BEGIN 2 */
	CAN_Config (); // Конфигураци�? фильтра
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
			if(!CAN_TxMessage (0,0x222,8, data))// МОЖЕТ отправить
				HAL_GPIO_TogglePin(LedGreen_GPIO_Port,LedGreen_Pin);
			HAL_Delay(1000);
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
/* CAN init function */
void MX_CAN_Init(void)
{
 
  hcan.Instance = CAN1;
  hcan.Init.Prescaler = 6;
  hcan.Init.Mode = CAN_MODE_NORMAL;
  hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
  hcan.Init.TimeSeg1 = CAN_BS1_8TQ;
  hcan.Init.TimeSeg2 = CAN_BS2_7TQ;
  hcan.Init.TimeTriggeredMode = DISABLE;
  hcan.Init.AutoBusOff = ENABLE;
  hcan.Init.AutoWakeUp = ENABLE;
  hcan.Init.AutoRetransmission = DISABLE;
  hcan.Init.ReceiveFifoLocked = DISABLE;
  hcan.Init.TransmitFifoPriority = DISABLE;
  if (HAL_CAN_Init(&hcan) != HAL_OK)
  {
    Error_Handler();
  }
 
}
/* USER CODE BEGIN 1 */
void CAN_Data_Send(void)
{
	
	CAN_TxHeaderTypeDef Tx_Header;
	
	Tx_Header.ExtId = 0x0CFFEAD2;
	Tx_Header.RTR = CAN_RTR_DATA;
	Tx_Header.IDE = CAN_ID_EXT;
	Tx_Header.DLC = 8;
	
	uint32_t mail_Box;
 
	uint8_t Tx_Data[8] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
	
	HAL_CAN_AddTxMessage(&hcan, &Tx_Header, Tx_Data, &mail_Box);
}
 
void CAN_Config(void)
{
	CAN_FilterTypeDef CAN_FilterType;
	CAN_FilterType.FilterBank=0;
	CAN_FilterType.FilterIdHigh=0x0000;
	CAN_FilterType.FilterIdLow=0x0000;
	CAN_FilterType.FilterMaskIdHigh=0x0000;
	CAN_FilterType.FilterMaskIdLow=0x0000;
	CAN_FilterType.FilterFIFOAssignment=CAN_RX_FIFO0;
	CAN_FilterType.FilterMode=CAN_FILTERMODE_IDMASK;
	CAN_FilterType.FilterScale=CAN_FILTERSCALE_32BIT;
	CAN_FilterType.FilterActivation=ENABLE;
	CAN_FilterType.SlaveStartFilterBank=14;
	if(HAL_CAN_ConfigFilter(&hcan,&CAN_FilterType)!=HAL_OK)
	{
		Error_Handler();
	}
	if(HAL_CAN_ActivateNotification(&hcan,CAN_IT_RX_FIFO0_MSG_PENDING)!=HAL_OK)
	{
		Error_Handler();
	}
	if(HAL_CAN_Start(&hcan)!=HAL_OK)
	{
		Error_Handler();
	}
}
 
uint8_t CAN_TxMessage(uint8_t ide,uint32_t id,uint8_t len,uint8_t *data)
{
	uint32_t TxMailbox;
	CAN_TxHeaderTypeDef CAN_TxHeader;
	HAL_StatusTypeDef HAL_RetVal; 
	uint16_t i = 0;
	if(ide == 0)
	{
		CAN_TxHeader.IDE = CAN_ID_STD;
		CAN_TxHeader.StdId = id;
	}
	else 
	{
		CAN_TxHeader.IDE = CAN_ID_EXT;
		CAN_TxHeader.ExtId = id;
	}
	CAN_TxHeader.DLC = len;
	CAN_TxHeader.RTR = CAN_RTR_DATA;
	CAN_TxHeader.TransmitGlobalTime = ENABLE;
	while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan) == 0)
	{
		i++;
		if(i>0xfffe)
			return 1;
	}
	HAL_Delay(500);
	HAL_RetVal = HAL_CAN_AddTxMessage(&hcan,&CAN_TxHeader,data,&TxMailbox);
	if(HAL_RetVal != HAL_OK)
	{
		return 1;
	}
	return 0;
}
 
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
	CAN_RxHeaderTypeDef CAN_RxHeader;
	HAL_StatusTypeDef HAL_Retval;
	uint8_t Rx_Data[8];
	uint8_t Data_Len=0;
	uint32_t ID=0;
	//uint8_t i;
	HAL_Retval = HAL_CAN_GetRxMessage(hcan,CAN_RX_FIFO0,&CAN_RxHeader,Rx_Data);
	if(HAL_Retval == HAL_OK)
	{
		Data_Len = CAN_RxHeader.DLC;
		if(CAN_RxHeader.IDE)
			ID = CAN_RxHeader.ExtId;
		else
			ID = CAN_RxHeader.StdId;
		//printf("id:%x\r\n",ID);
		//printf("Data_Len:%d\r\n",Data_Len);
		//for(i=0;i<8;i++)
		//printf("Rx_Data[%d]=%x\r\n",i,Rx_Data[i]);	
	}
}
 
/* USER CODE END 1 */

0693W000008GNQLQA4.jpg

8 REPLIES 8
KnarfB
Principal III

Don't see an error in your code. Only a nuisance: lines 62.. arte nor within a user code block, but inbetween two different ones. Should not hurt unless you re-generate the code.

To rule out electrical issues with your setup, test loopback mode instead of normal mode. Then the Rx callback shall betriggered after sending.

MIgos.1
Associate II

Hello! Thanks for the help! Corrected. Generated. I tested the "CAN_MODE_LOOPBACK" mode - everything is fine with this mode and the messages are received. In a similar experiment: https://github.com/exothink/eXoCAN, the author states: "Caution: The" SN65HVD230 CAN Bus Transceiver Communication Module For Arduino "sold on eBay do not work. I've tried several." And in another experiment: https://github.com/nopnop2002/Arduino-STM32-CAN, the author states: "... There is a module of SN65HVD230 like this ... There is a 120 ohms terminating resistor on the left side. ..A transmission fail will occur ... ". I tried both experiments and set an external resistance of 120 Ohm - no result, some errors are accepted by the "CAN king" program - this can be seen from the picture that I attached to the question. I built the "BPCanBlink" project in the "ARDUINO IDE" - there is no result, some errors are accepted by the "CAN king" program ... Now ordering the "CAN" "TJA1050" module ... Since the "CAN King" program accepts errors - this is a sign that that it might be most likely a sync error.The question is relevant! Thanks in advance for your help!

int main(void)
{
  /* USER CODE BEGIN 1 */
uint8_t data[8]={1,2,3,4,5,6,7,8};
  /* 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_TIM2_Init();
  MX_CAN_Init();
 
  /* Initialize interrupts */
  MX_NVIC_Init();
  /* USER CODE BEGIN 2 */
	CAN_Config (); // Конфигураци�? фильтра
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
	  if(!CAN_TxMessage (0,0x222,8, data)) // МОЖЕТ отправить
			 HAL_GPIO_TogglePin(LedGreen_GPIO_Port,LedGreen_Pin);
		HAL_GPIO_TogglePin(ToggleTest_GPIO_Port,ToggleTest_Pin);
	  HAL_Delay(1000);		
  }
  /* USER CODE END 3 */
}
 
/* CAN init function */
void MX_CAN_Init(void)
{
 
  hcan.Instance = CAN1;
  hcan.Init.Prescaler = 9;
  hcan.Init.Mode = CAN_MODE_LOOPBACK;
  hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
  hcan.Init.TimeSeg1 = CAN_BS1_8TQ;
  hcan.Init.TimeSeg2 = CAN_BS2_7TQ;
  hcan.Init.TimeTriggeredMode = DISABLE;
  hcan.Init.AutoBusOff = ENABLE;
  hcan.Init.AutoWakeUp = ENABLE;
  hcan.Init.AutoRetransmission = DISABLE;
  hcan.Init.ReceiveFifoLocked = DISABLE;
  hcan.Init.TransmitFifoPriority = DISABLE;
  if (HAL_CAN_Init(&hcan) != HAL_OK)
  {
    Error_Handler();
  }
 
}
*************************************************************************
*************************************************************************
*************************************************************************
#include <arduino.h>
#include <eXoCAN.h>
 
#define bluePillLED PC13
 
int txMsgID = 0x069;
uint8_t txData[8]{0x00, 0x01, 0x23, 0x45, 0xab, 0xcd, 0xef, 0xff};
uint8_t txDataLen = 8;
uint32_t txDly = 5000; // mSec
 
//  ****** uncomment the following for the second stm32f103 board ******
 
// int txMsgID = 0x005;
// uint8_t txData[8]{{0x01, 0xfe, 0xdc, 0xba, 0x11, 0x12, 0x34, 0x56};
// uint_8 txDataLen = 8;
// uint32_t txDly = 1000;  // mSec
 
int id, fltIdx;
uint8_t rxbytes[8];
eXoCAN can;
 
void setup()
{
  can.begin(STD_ID_LEN, BR250K, PORTA_11_12_WIRE_PULLUP); // 11b IDs, 250k bit rate, no transceiver chip, portA pins 11,12
  can.filterMask16Init(0, 0, 0x7ff, 0, 0);                // filter bank 0, filter 0: don't pass any, flt 1: pass all msgs
  pinMode(bluePillLED, OUTPUT);
}
 
uint32_t last = 0;
void loop()
{
  if (millis() / txDly != last)             // tx every txDly
  {
    last = millis() / txDly;
    can.transmit(txMsgID, txData, txDataLen);
  }
 
  if (can.receive(id, fltIdx, rxbytes) > -1) // poll for rx
  {
    digitalWrite(PC13, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(100);              // wait for a second
    digitalWrite(PC13, LOW);    // turn the LED off by making the voltage LOW
    delay(100);              // wait for a second
  }
}

0693W000007Z46GQAS.jpg0693W000007Z42nQAC.jpg 

Ozone
Lead

> Everything is being built and debugged, I have met all the conditions, but I do not see the parcels sent by me in CAN King. Can you help?

Do you see anything in your CAN monitoring software, like BUS_OFF, BUS_heavy / BUS_Light consitions ?

MIgos.1
Associate II

Probably, since the layout of the board is not very good, I see, with the help of an oscilloscope, the sendings to the "CAN" indistinctly extraneous noise climb into the measurements. This noise is approximately 1 mV. In the picture, I circled it in red - it says "ErrorFrame" in the "CAN King" program window - the program reads errors. This is a symptom - this is most likely a synchronization error between the "-stm32" board and the "SN65HVD230" module. How to defeat this error? I set everything up to "baud rate 250k". Maybe there is a method by listening to the registers of the "Stm32f103c8t6" microcontroller to solve such problems? Thank! Thank you for your understanding!0693W000007Z5fbQAC.jpg

MIgos.1
Associate II

Hello! The module "TJA1050" came to me. It did not help, the problem remained. I see in the program "CAN King" - the same "On Bus" and errors. I looked at the "Tx" channel with the oscilloscope - there are not regular sendings, this can be seen from the attached figure: "CAN_Tx_TJA1050". If the sending is not regular, then maybe these are interruptions? Here are my interrupt settings - picture: "interrupt settings". The "Stm32" controller sends non-periodic messages to the "Tx" channel (I have this pin "PA12"). What is it coming from? Thank you in advance for your help!

0693W000008wgNAQAY.jpg0693W000008wgMqQAI.jpg0693W000008wgMlQAI.jpg0693W000008wgMbQAI.jpg

MIgos.1
Associate II

Hello friends! Tried four debuggers:

1.Arduino IDE,

2.Keil 5,

3.IAR 9,

4.CooCox IDE. The problems are the same. The first problem is the non-periodicity of messages when listening by the Tx oscilloscope, both in NORMAL mode and in LOOPBACK mode. The second problem is that there is no HAL_CAN_RxFifo0MsgPendingCallback interrupt when working in normal mode. In LOOPBACK mode, this interrupt is present. I will call this feedback problem and, in my opinion, it has been in STM32CUBEMX for STM32F103C8T6 for a very long time. How to achieve normal sending of messages to CAN BUS so that these messages can be seen by special software using the DANFOSS CG150 harness? I ask for help! Thank you all in advance! Thank you for understanding!

0693W000008x2IHQAY.jpg

Ozone
Lead

I guess there is something wrong on physical bus level.

Have you checked the signals with a scope ?

CANH and CANL should be symmetrical, one the inverted of the other.

In most cases, a connection works even without ground (i.e. CAN_GND not connected).

Perhaps you have a problem with GND level offset between the two boards.

For CAN error frames, see e.g. here: https://www.copperhilltechnologies.com/can-bus-guide-error-frame/

MIgos.1
Associate II

Hello, friends! I have read and tried to become the attempt you recommended. This is what I see on the oscilloscope:

In NORMAL transmission mode, where channel # 1 is CANH and channel # 2 is CANL. Like the chart from the recommended article. In the LOOPBACK transmission mode, I see a normal schedule on the CANH and CANL channels. I also noticed that if you just listen to the CAN line, without sending anything there, and at the same time, forming parcels using the Can King special software. Then these parcels, my assembly STMCUBEMX 6.0.1 pack 1.8.3 for the Stm32F103C8t6 chip (and TJA1050) does not receive and there is no interruption for receiving parcels. How to tie all this information into one and correct the error, while I do not have an understanding? Thank you all in advance for your help! Thank you for understanding!

0693W000008x8JQQAY.jpg0693W000008x8IwQAI.jpg