2019-02-18 01:51 AM
Dear all,
I'm currently trying to work around the CAN Bus on the STM32F091CB uC, with StdPeriph_Lib_V1.5.0 Lib but the transmission is never successful (I have already programmed STM32F4 and STM32F7 boards with StdPeriph_Libs, without any problem).
This is my code:
#define CAN_RX_Pin GPIO_Pin_8
#define CAN_RX_Src GPIO_PinSource8
#define CAN_RX_Port GPIOB
#define CAN_TX_Pin GPIO_Pin_9
#define CAN_TX_Src GPIO_PinSource9
#define CAN_TX_Port GPIOB
#define CAN_AF_Port GPIO_AF_4
void init_CAN_Bus(void){
en_CAN_CLK();
init_CAN_GPIO();
init_CAN_Periph();
}
void en_CAN_CLK(void){
//Enable GPIOB Src Clk
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
//Enable CAN Periph Src Clk
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE);
}
void init_CAN_GPIO(void){
// Connect CAN pins to Altinative Funtion 4
GPIO_PinAFConfig(CAN_RX_Port, CAN_RX_Src, CAN_AF_Port);
GPIO_PinAFConfig(CAN_TX_Port, CAN_TX_Src, CAN_AF_Port);
//Configure CAN RX & TX pins
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = CAN_RX_Pin;
GPIO_Init(CAN_RX_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = CAN_TX_Pin;
GPIO_Init(CAN_TX_Port, &GPIO_InitStructure);
}
void init_CAN_Periph(void){
//Deinit CAN Periph
CAN_DeInit(CAN);
//Init CAN Periph
CAN_InitTypeDef CAN_InitStructure;
CAN_StructInit(&CAN_InitStructure);
CAN_InitStructure.CAN_TTCM = DISABLE;
CAN_InitStructure.CAN_ABOM = DISABLE;
CAN_InitStructure.CAN_AWUM = DISABLE;
CAN_InitStructure.CAN_NART = DISABLE;
CAN_InitStructure.CAN_RFLM = DISABLE;
CAN_InitStructure.CAN_TXFP = DISABLE;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
//CAN Baudrate = 500Kbps (CAN clocked at 48 MHz)
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_13tq; //Seg1
CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq; //Seg2
CAN_InitStructure.CAN_Prescaler = 6; //Prescaler
CAN_Init(CAN, &CAN_InitStructure);
//CAN Filter Init
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CAN_FilterInitStructure.CAN_FilterNumber = 0;
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
}
and i transmit with the following function:
uint8_t sndCANBus_Test(void){
static CanTxMsg TxMessage;
TxMessage.StdId = 0x321;
TxMessage.ExtId = 0x01;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.IDE = CAN_ID_STD;
TxMessage.Data[0]=0x01;
TxMessage.Data[1]=0x02;
TxMessage.Data[2]=0x03;
TxMessage.Data[3]=0x04;
TxMessage.Data[4]=0x05;
TxMessage.Data[5]=0x06;
TxMessage.Data[6]=0x07;
TxMessage.Data[7]=0x08;
TxMessage.DLC = sizeof(TxMessage.Data);
uint8_t aux_status;
aux_status = CAN_Transmit(CAN, &TxMessage);
return aux_status;
}
In the past, the configuration shown above worked for me.
I would be very grateful to all of you for any information that leads me to the solution of this problem, that is, to transmit data to the CAN Bus.
thanks in advance.