2020-01-27 11:16 AM
2020-01-28 08:54 PM
Did you enable filter? Atleast "pass all" filter of 0x00 must be enabled.
2020-01-29 08:16 AM
thx for the reply, yes its enabled, i'm just testing the CAN loopback and transmit to a usart to see that it works, but still has issues. I'll attach the whole main.c file and left out unrelated code.
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include <stdio.h>
#include <malloc.h>
/* Private includes ----------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
CAN_HandleTypeDef hcan;
UART_HandleTypeDef huart2;
UART_HandleTypeDef huart3;
DMA_HandleTypeDef hdma_usart2_tx;
void SystemClock_Config(void);
static void MX_CAN_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN 0 */
CAN_HandleTypeDef hcan;
CAN_TxHeaderTypeDef TxHeader;
CAN_RxHeaderTypeDef RxHeader;
uint32_t TxMailbox;
uint8_t TxData[32];
uint8_t RxData[32];
uint8_t Data[8] = {0xDE, 0xAD, 0xBE, 0xEF};
uint8_t test[16] = "CAN works\r\n";
uint8_t start[30] = "Starting program\r\n";
uint8_t nbinterrupt;
int main(void)
{
/* 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_DMA_Init();
MX_CAN_Init();
MX_I2C1_Init();
MX_I2C2_Init();
MX_SPI1_Init();
MX_USART2_UART_Init();
MX_USART3_UART_Init();
MX_CRC_Init();
HAL_UART_Transmit(&huart2, start, sizeof(start), 100); //forward command to day cam
nbinterrupt = 0;
while (1)
{
/*if (HAL_CAN_AddTxMessage(&hcan, &TxHeader, test, &TxMailbox) == HAL_OK)
{
HAL_Delay(1000);
Error_Handler();
}*/
// if(HAL_CAN_GetRxFifoFillLevel(&hcan, CAN_RX_FIFO0) != 0)
// {
/* if (HAL_CAN_GetRxMessage(&hcan, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)
{
if (RxData[0] == 0xDE &&
RxData[1] == 0xAD &&
RxData[2] == 0xBE &&
RxData[3] == 0xEF)
{
HAL_UART_Transmit(&huart2, RxData, 32, 100); //forward command to day cam
HAL_Delay(1000);
}
}
else Error_Handler();
// }*/
//Error_Handler();
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan);
if (HAL_CAN_AddTxMessage(&hcan, &TxHeader, Data, &TxMailbox) != HAL_OK)
{
/* Transmission request Error */
Error_Handler();
}
else
{
HAL_UART_Transmit(&huart2, Data, 32, 100); //forward command to day cam
HAL_Delay(1000);
}
}
}
static void MX_CAN_Init(void)
{
CAN_FilterTypeDef sFilterConfig;
/*##-1- Configure the CAN peripheral #######################################*/
hcan.Instance = CAN;
hcan.Init.Prescaler = 12;
hcan.Init.Mode = CAN_MODE_LOOPBACK;
hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan.Init.TimeSeg1 = CAN_BS1_13TQ;
hcan.Init.TimeSeg2 = CAN_BS2_2TQ;
hcan.Init.TimeTriggeredMode = DISABLE;
hcan.Init.AutoBusOff = DISABLE;
hcan.Init.AutoWakeUp = DISABLE;
hcan.Init.AutoRetransmission = DISABLE;
hcan.Init.ReceiveFifoLocked = DISABLE;
hcan.Init.TransmitFifoPriority = ENABLE;
if (HAL_CAN_Init(&hcan) != HAL_OK)
{
Error_Handler();
}
/*##-2- Configure the CAN Filter ###########################################*/
sFilterConfig.FilterBank = 0;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0xffff;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0xffff;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.SlaveStartFilterBank = 14;
if (HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
{
/* Filter configuration Error */
Error_Handler();
}
/*##-3- Start the CAN peripheral ###########################################*/
if (HAL_CAN_Start(&hcan) != HAL_OK)
{
/* Start Error */
Error_Handler();
}
/*##-4- Activate CAN RX notification #######################################*/
if (HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
{
/* Notification Error */
Error_Handler();
}
/*##-5- Configure Transmission process #####################################*/
TxHeader.StdId = 0x321;
TxHeader.ExtId = 0x01;
TxHeader.RTR = CAN_RTR_DATA;
TxHeader.IDE = CAN_ID_EXT;
TxHeader.DLC = 4;
TxHeader.TransmitGlobalTime = DISABLE;
/*##-6- Configure Reception process #####################################*/
if(HAL_CAN_GetRxFifoFillLevel(&hcan, CAN_RX_FIFO0) != 0)
{
/* Reception Missing */
Error_Handler();
}
}
static void MX_USART2_UART_Init(void)
{
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */
/* USER CODE END USART2_Init 2 */
}
/* USER CODE BEGIN 4 */
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
/* Get RX message */
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)
{
/* Reception Error */
Error_Handler();
}
/* Display LEDx */
if ((RxHeader.ExtId == 0x01) && (RxHeader.IDE == CAN_ID_EXT) && (RxHeader.DLC == 4))
{
nbinterrupt++;
HAL_UART_Transmit(&huart2, Data, 32, 100); //forward command to day cam
//LED_Display(RxData[0]);
// ubKeyNumber = RxData[0];
}
}