CAN Transmit gets a timeout error
Hi everyone,
I´m using a STM32F103 with SW4STM32 and CubeMX and I`m struggeling with the CAN Communication.
When I`ll try to transmit Data with HAL_CAN_Transmit then I get a timeout error. To monitor the transmitted data, I`m using PCAN. I already tested the wires with an oscilloscope and there is only a peak when the PD15 Pin is set to low, so there is no CAN Data on the wire. I read that it could be a bug from the HAL Library but I don`t know how to solve it.
So I hope anyone of you could help me and find a mistake. Here is my code
Thanks for your support
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_SPI1_Init();
MX_USART1_UART_Init();
MX_CAN_Init();
/* USER CODE BEGIN 2 */
__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
HAL_GPIO_WritePin(GPIOD, USBEnable_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, canRS_Pin, GPIO_PIN_RESET);
HAL_UART_Receive_IT(&huart1,&Rx_data, 1);
printf(“welcome\r\n”);
uint16_t SOC = 5500;
uint16_t BatVoltage = 590;
uint16_t cellVoltageMax = 3900;
uint16_t cellVoltageMin = 3800;
uint8_t tempMax = 50;
uint8_t tempMin = 47;
while (1)
{
printf(“Test\r\n”);
hcan.pTxMsg->Data0 = 0b01110000;
hcan.pTxMsg->Data1 = (uint8_t) (SOC/1000);
hcan.pTxMsg->Data2 = (uint8_t) ((BatVoltage & 0b111111111111) >> 4);
hcan.pTxMsg->Data3 = (uint8_t) (((BatVoltage > 1)) >> 8);
hcan.pTxMsg->Data4 = (uint8_t) (((cellVoltageMax & 0b0000000111110000) >> 1) | ((cellVoltageMin & 0b0001110000000000) >> 10));
hcan.pTxMsg->Data5 = (uint8_t) (((cellVoltageMin & 0b0000001111110000) >> 2) | (tempMax >> 6));
hcan.pTxMsg->Data6 = (uint8_t) ((tempMax > 6));
hcan.pTxMsg->Data7 = (uint8_t) (tempMin Data0);
if (HAL_CAN_Transmit(&hcan, 10) != HAL_OK)
{
/* Transmition Error */
//Error_Handler();
printf(“CAN Transmit Error\r\n”);
}
printf(“%d\r\n”, HAL_CAN_GetState(&hcan));
HAL_Delay(2000);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
static void MX_CAN_Init(void)
{
CAN_FilterConfTypeDef sFilterConfig;
static CanTxMsgTypeDef TxMessage;
static CanRxMsgTypeDef RxMessage;
hcan.Instance = CAN1;
hcan.pTxMsg = &TxMessage;
hcan.pRxMsg = &RxMessage;
hcan.Init.Prescaler = 4;
hcan.Init.Mode = CAN_MODE_NORMAL;
hcan.Init.SJW = CAN_SJW_1TQ;
hcan.Init.BS1 = CAN_BS1_12TQ;
hcan.Init.BS2 = CAN_BS2_5TQ;
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();
}
sFilterConfig.FilterNumber = 0;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = 0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.BankNumber = 14;
if (HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
{
/* Filter configuration Error */
Error_Handler();
}
hcan.pTxMsg->StdId = 0x321;
hcan.pTxMsg->ExtId = 0x01;
hcan.pTxMsg->RTR = CAN_RTR_DATA;
hcan.pTxMsg->IDE = CAN_ID_STD;
hcan.pTxMsg->DLC = 8;
}