urbanzrim
Newbie level 4
- Joined
- Oct 2, 2012
- Messages
- 7
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,346
Hi, I'm trying to set up CAN communication between two nodes (stm32f0 discovery boards). I have trouble with initialization when I try to fill up the CAN_InitTypeDef struct. My code simply stops at "/*Can initialization */" when I try to assign "can_init.CAN_Prescaler = 4;". Any ideas?
Code:
#include "stm32f0xx.h"
#include "stm32f0xx_gpio.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_can.h"
#include "gpio_set.h"
#include "semihosting.h"
CAN_InitTypeDef can_init;
CanTxMsg can_msg;
int can_init_error;
uint8_t txBuff[4];
int main(void)
{
/* Initialize GPIO peripheral clock for ports A, B and C */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
/*Initialize all the ports buttons and LED's are connected to */
back_panel_led_init();
back_panel_buttons_init();
/* CAN initialization */
can_init.CAN_Prescaler = 4;
can_init.CAN_Mode = CAN_Mode_Normal;
can_init.CAN_SJW = CAN_SJW_1tq;
can_init.CAN_BS1 = CAN_BS1_14tq;
can_init.CAN_BS2 = CAN_BS2_3tq;
can_init.CAN_TTCM = DISABLE;
can_init.CAN_ABOM = DISABLE;
can_init.CAN_AWUM = DISABLE;
can_init.CAN_NART = DISABLE;
can_init.CAN_RFLM = DISABLE;
can_init.CAN_TXFP = DISABLE;
can_init_error = CAN_Init(CAN1, &can_init);
if(can_init_error == 0){
printf("CAN initialization process failed!");
}
else printf("CAN Successfully initialized");
can_msg.StdId = 0x011;
can_msg.IDE = CAN_Id_Standard;
can_msg.RTR = CAN_RTR_Data;
can_msg.DLC = 8;
can_msg.Data[0] = 0;
can_msg.Data[1] = 0;
can_msg.Data[2] = 0;
can_msg.Data[3] = 0;
can_msg.Data[4] = 0;
can_msg.Data[5] = 0;
can_msg.Data[6] = 0;
can_msg.Data[7] = 0;
while (1)
{
if(GPIO_ReadInputDataBit(GPIOA, BTN_1) == 0){
can_msg.Data[0] = 0x1;
}
else can_msg.Data[0] = 0x0;
CAN_Transmit(CAN1, &can_msg);
printf("sending!");
}
}