cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f103 partially receive from canbus and don't send

steeve
Visitor

hello, I have a can bus with 2 node (one STM32G0B1 with SN65HVD1050DR  transcriver and one esp8266 with 

MCP2515 module) that comunicate over canbus at 1Mbps.
I am trying to add a third node, it is a blue pill STM32F103C8T6. The chip onboard looks genuine (i have also tested the usb double buffer on it and it works) but who knows...
Transcriver attached to STM32F103 is a SN65HVD1050DR, as for the G0B1.
The problem is, whith STM32F103 i see only messages coming from STM32G0B1 (and not from the other node on bus), and it looks like it is not sending messages (other nodes on the bus don't receive also if they are accepting from any id). My ideas are: 1- fake chip, so canbus will never work properly, 2 - bad code.
Here are relevant code parts for STM32F103, do you see errors (i am quite new to C programming)?
With this code, the main loop run for some seconds (circa 10) than it enter in Error_Handler function,
 if i activate the AutoRetransmission it happens after 2 seconds or so.
If i put CANBUS in loopback mode it works.
I am not using USB on F103.
 
static void MX_CAN_Init(void) {
	hcan.Instance = CAN1;
	hcan.Init.Prescaler = 2;
	hcan.Init.Mode = CAN_MODE_NORMAL;
	hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
	hcan.Init.TimeSeg1 = CAN_BS1_11TQ;
	hcan.Init.TimeSeg2 = CAN_BS2_6TQ;
	hcan.Init.TimeTriggeredMode = DISABLE;
	hcan.Init.AutoBusOff = DISABLE;
	hcan.Init.AutoWakeUp = DISABLE;
	hcan.Init.AutoRetransmission = DISABLE;
	hcan.Init.ReceiveFifoLocked = DISABLE;
	hcan.Init.TransmitFifoPriority = DISABLE;

	if (HAL_CAN_Init(&hcan) != HAL_OK) {
		Error_Handler();
	}
	/* USER CODE BEGIN CAN_Init 2 */
	CAN_FilterTypeDef canfil;

	canfil.FilterBank = 0;
	canfil.FilterMode = CAN_FILTERMODE_IDMASK;
	canfil.FilterFIFOAssignment = CAN_RX_FIFO0;
	canfil.FilterIdHigh = 0x0;
	canfil.FilterIdLow = 0x0;
	canfil.FilterMaskIdHigh = 0;
	canfil.FilterMaskIdLow = 0;
	canfil.FilterScale = CAN_FILTERSCALE_32BIT;
	canfil.FilterActivation = ENABLE;
	canfil.SlaveStartFilterBank = 14;

	if (HAL_CAN_ConfigFilter(&hcan, &canfil) != HAL_OK)
	{
		Error_Handler();
	}
}

volatile uint16_t msgcount = 0;
uint8_t canRX[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
CAN_TxHeaderTypeDef txHeader;
uint32_t canMailbox;

int main(void) {
	HAL_Init();
	SystemClock_Config();
	MX_GPIO_Init();
	MX_CAN_Init();
	if (HAL_CAN_Start(&hcan) != HAL_OK)
	{
		Error_Handler();
	}
	if ( HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
	{
		Error_Handler();
	}
	txHeader.DLC = 8;
	txHeader.IDE = CAN_ID_STD;
	txHeader.RTR = CAN_RTR_DATA;
	txHeader.StdId = 0x030;
	txHeader.ExtId = 0x01;
	txHeader.TransmitGlobalTime = DISABLE;
	uint16_t delay = 500;
	while (1) {
		/* USER CODE END WHILE */
		HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
		uint8_t csend[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
		if (HAL_CAN_AddTxMessage(&hcan,&txHeader,csend,&canMailbox) != HAL_OK)
		{
			Error_Handler();
		}
		if (msgcount > 0) {
			delay = 100;
		}
		HAL_Delay(delay);
	}
}
void incrementMessageCount() {
	msgcount++;
}
CAN_RxHeaderTypeDef rxHeader;
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan1) {
	HAL_CAN_GetRxMessage(hcan1, CAN_RX_FIFO0, &rxHeader, canRX);
	incrementMessageCount();
}

 

1 ACCEPTED SOLUTION

Accepted Solutions
steeve
Visitor

turned out it was a transcriver issue, one pin on transcriver board was not soldered well. Sorry

View solution in original post

1 REPLY 1
steeve
Visitor

turned out it was a transcriver issue, one pin on transcriver board was not soldered well. Sorry