2016-01-26 08:40 AM
Hi.
I'm trying to make my STM32F4-Discovery talk to itsef through CAN,using this configuration and here's my code.
#include ''stm32f4xx_hal.h''
#include ''stm32f4xx_hal_can.h''
#include ''Board_Led.h''
CAN_HandleTypeDef CAN1_Handle, CAN2_Handle;
CAN_FilterConfTypeDef CAN2_Filter;
CanRxMsgTypeDef CAN1RX,CAN2RX;
CanTxMsgTypeDef CAN1TX,CAN2TX;
GPIO_InitTypeDef GPIO_Init;
static void SystemClock_Config(void);
void CAN1_Config(void);
void CAN2_Config(void);
void CAN_SendMsg(void);
void Button_Config(void);
int main (void)
{
HAL_Init();
SystemClock_Config();
LED_Initialize();
LED_On(0);
Button_Config();
CAN1_Config();
CAN2_Config();
while (1);
}
void CAN1_Config(void)
{
//GPIO Config PD0 = CAN1 RX, PD1 = CAN1 TX
__GPIOD_CLK_ENABLE();
GPIO_Init.Mode =GPIO_MODE_AF_PP;
GPIO_Init.Alternate= GPIO_AF9_CAN1;
GPIO_Init.Pin = GPIO_PIN_0|GPIO_PIN_1;
GPIO_Init.Pull = GPIO_PULLUP;
GPIO_Init.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(GPIOD,&GPIO_Init);
__CAN1_CLK_ENABLE();
CAN1_Handle.Instance= CAN1;
CAN1_Handle.pRxMsg = &CAN1RX;
CAN1_Handle.pTxMsg = &CAN1TX;
CAN1_Handle.Init.ABOM = DISABLE;
CAN1_Handle.Init.AWUM = DISABLE;
CAN1_Handle.Init.BS1 = CAN_BS1_9TQ;
CAN1_Handle.Init.BS2 = CAN_BS2_8TQ;
CAN1_Handle.Init.Mode = CAN_MODE_NORMAL;
CAN1_Handle.Init.NART = DISABLE;
CAN1_Handle.Init.Prescaler = 16;
CAN1_Handle.Init.RFLM = DISABLE;
CAN1_Handle.Init.TTCM = DISABLE;
CAN1_Handle.Init.SJW = CAN_SJW_4TQ;
CAN1_Handle.Init.TXFP = DISABLE;
if(HAL_CAN_Init(&CAN1_Handle)== HAL_OK)
{
printf(''CAN1 Initialized.\n'');
}
HAL_NVIC_EnableIRQ(CAN1_TX_IRQn);
}
void CAN2_Config(void)
{
//GPIO PB12 = CAN2RX, PB13 = CAN2TX
__GPIOB_CLK_ENABLE();
GPIO_Init.Mode =GPIO_MODE_AF_PP;
GPIO_Init.Alternate= GPIO_AF9_CAN2;
GPIO_Init.Pin = GPIO_PIN_12|GPIO_PIN_13;
GPIO_Init.Pull = GPIO_PULLUP;
GPIO_Init.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(GPIOB,&GPIO_Init);
__CAN2_CLK_ENABLE();
CAN2_Handle.Instance= CAN2;
CAN2_Handle.pRxMsg = &CAN2RX;
CAN2_Handle.pTxMsg = &CAN2TX;
CAN2_Handle.Init.ABOM = DISABLE;
CAN2_Handle.Init.AWUM = DISABLE;
CAN2_Handle.Init.BS1 = CAN_BS1_9TQ;
CAN2_Handle.Init.BS2 = CAN_BS2_8TQ;
CAN2_Handle.Init.Mode = CAN_MODE_NORMAL;
CAN2_Handle.Init.NART = DISABLE;
CAN2_Handle.Init.Prescaler = 16;
CAN2_Handle.Init.RFLM = DISABLE;
CAN2_Handle.Init.TTCM = DISABLE;
CAN2_Handle.Init.SJW = CAN_SJW_4TQ;
CAN2_Handle.Init.TXFP = DISABLE;
if(HAL_CAN_Init(&CAN2_Handle)== HAL_OK)
{
printf(''CAN2 Initialized.\n'');
}
CAN2_Filter.FilterNumber = 14;
CAN2_Filter.FilterActivation = ENABLE;
CAN2_Filter.BankNumber = 14;
CAN2_Filter.FilterFIFOAssignment = CAN_FilterFIFO0;
CAN2_Filter.FilterIdHigh =0x0;
CAN2_Filter.FilterIdLow = 0x0;
CAN2_Filter.FilterMaskIdHigh =0x0;
CAN2_Filter.FilterMaskIdLow =0x0;
CAN2_Filter.FilterMode = CAN_FILTERMODE_IDMASK;
CAN2_Filter.FilterScale = CAN_FILTERSCALE_32BIT;
if(HAL_CAN_ConfigFilter(&CAN2_Handle,&CAN2_Filter)== HAL_OK)
{
printf(''Filter configd.\n'');
}
HAL_NVIC_SetPriority(CAN2_RX0_IRQn,0,0);
HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);
HAL_CAN_Receive_IT(&CAN2_Handle,CAN_FIFO0);
}
void Button_Config(void)
{
__GPIOA_CLK_ENABLE();
GPIO_Init.Mode = GPIO_MODE_IT_RISING;
GPIO_Init.Pull = GPIO_PULLDOWN;
GPIO_Init.Speed = GPIO_SPEED_FAST;
GPIO_Init.Pin = GPIO_PIN_0;
HAL_GPIO_Init(GPIOA,&GPIO_Init);
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
}
void EXTI0_IRQHandler(void)
{
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
CAN_SendMsg();
}
void CAN_SendMsg(void)
{
CAN1TX.IDE = CAN_ID_STD;
CAN1TX.StdId = 10;
CAN1TX.ExtId = 0x01;
CAN1TX.RTR = CAN_RTR_DATA;
CAN1TX.DLC = 1;
CAN1TX.Data[0]=3;
if(HAL_CAN_Transmit_IT(&CAN1_Handle)== HAL_OK)
{
printf(''Msg sent.\n'');
}
}
void CAN2_RX0_IRQHandler(void)
{
HAL_CAN_IRQHandler(&CAN2_Handle);
}
void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef *hcan)
{
printf(''rceived %d'',CAN2RX.Data[0]);
}
The first time I push the button, the program indicates that the message is successfully sent. CAN2 receives nothing however.
HSE_Value is set to 8 MHz.
Any insights would be appreciated.
Thanks in advance.
#stm32f4 #can #discovery