stm32f can communication problem? - Page 1
Forum section: electronics / general

author: koray692
author_rank: Contributor
timestamp: March 12, 2015, 10:00:56 am
content: hello friend.I wri?te a code but unfourtuanetly it doesnt work.In debug RxMessage.Data become 0x58 then TxMessage.Data become 0x59 after that nothing change Let me share code;#include "stm32f4xx.h"#include <stm32f4xx_rcc.h>#include <stm32f4xx_can.h>#include <stm32f4xx_gpio.h>#define CAN_GPIO_PORT GPIOD#define CAN_RX_SOURCE GPIO_PinSource0 //PD0#define CAN_TX_SOURCE GPIO_PinSource1 //PD1#define CAN_AF_PORT GPIO_AF_CAN1#define CANx CAN1void CanBusInit(void);CanTxMsg TxMessage;CanRxMsg RxMessage;int main(){int i=0;CanBusInit();TxMessage.StdId=0x123;TxMessage.ExtId=0x13375000;TxMessage.IDE=CAN_ID_STD;TxMessage.RTR=CAN_RTR_DATA;TxMessage.DLC=0x1;TxMessage.Data=0;RxMessage.Data=0;CAN_Transmit(CANx,&TxMessage);CAN_Receive(CANx,CAN_FIFO0,&RxMessage);// RxMessage.StdId=;// RxMessage.ExtId=;// RxMessage.IDE=;// RxMessage.RTR=;// RxMessage.DLC=;// RxMessage.Data=;// RxMessage.FMI=;while(1){for(i=0;i<5000000;i++);CAN_Transmit(CANx,&TxMessage);CAN_Receive(CANx,CAN_FIFO0,&RxMessage);TxMessage.Data=RxMessage.Data+1;}}void CanBusInit(void){GPIO_InitTypeDef GPIO_InitStructure;CAN_InitTypeDef CAN_InitStructure;CAN_FilterInitTypeDef CANFilter_InitStructure;/* Enable GPIO clock */RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);/* CAN configuration ********************************************************//* Enable CAN clock */RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);/* Connect CAN pins to AF9 */GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_RX_SOURCE, CAN_AF_PORT);GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_TX_SOURCE, CAN_AF_PORT);/* Configure CAN RX and TX pins */GPIO_InitStructure.GPIO_Pin = CAN_RX_SOURCE | CAN_TX_SOURCE;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_Init(CAN_GPIO_PORT, &GPIO_InitStructure);/* CAN register init */CAN_DeInit(CANx);/* CAN cell init */CAN_InitStructure.CAN_TTCM = DISABLE; // time-triggered communication mode = DISABLEDCAN_InitStructure.CAN_ABOM = DISABLE; // automatic bus-off management mode = DISABLEDCAN_InitStructure.CAN_AWUM = DISABLE; // automatic wake-up mode = DISABLEDCAN_InitStructure.CAN_NART = ENABLE; // non-automatic retransmission mode = ENABLE (To prevent endless spam)CAN_InitStructure.CAN_RFLM = DISABLE; // receive FIFO locked mode = DISABLEDCAN_InitStructure.CAN_TXFP = DISABLE; // transmit FIFO priority = DISABLEDCAN_InitStructure.CAN_Mode = CAN_Mode_Normal;CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;/* CAN Baudrate */CAN_InitStructure.CAN_BS1 = CAN_BS1_14tq;CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;CAN_InitStructure.CAN_Prescaler = 4;CAN_Init(CANx, &CAN_InitStructure);CANFilter_InitStructure.CAN_FilterActivation = ENABLE;CANFilter_InitStructure.CAN_FilterNumber = 14;CANFilter_InitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;CANFilter_InitStructure.CAN_FilterScale = CAN_FilterScale_16bit;CANFilter_InitStructure.CAN_FilterIdHigh = 0x00;CANFilter_InitStructure.CAN_FilterIdLow = 0x00;CANFilter_InitStructure.CAN_FilterMaskIdHigh = 0x00;CANFilter_InitStructure.CAN_FilterMaskIdLow = 0x00;CANFilter_InitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;CAN_FilterInit(&CANFilter_InitStructure);}

author: magetoo
author_rank: Frequent Contributor
timestamp: March 14, 2015, 03:43:56 am
content: Quote from: koray692 on March 12, 2015, 10:00:56 amDisclaimer: I'm not an expert on CAN and I've never used those chips.But that is a tight loop that does nothing except send and receive. Is it possible that you are filling up a FIFO and you need to take some special action when it is full?Does CAN_Receive() wait for a CAN frame every time it is called? Does it work if you slow the program down with a small delay after sending and receiving?

author: AndreasF
author_rank: Frequent Contributor
timestamp: March 14, 2015, 11:51:49 am
content: I know nothing about CAN, but I suspect this line might be problematic:Code:GPIO_InitStructure.GPIO_Pin = CAN_RX_SOURCE | CAN_TX_SOURCE;since youCode:#define CAN_RX_SOURCE GPIO_PinSource0 //PD0#define CAN_TX_SOURCE GPIO_PinSource1 //PD1However, the GPIO_InitTypeDef structure wants pin definitions like this:Code:#define GPIO_Pin_0 ((uint16_t)0x0001)#define GPIO_Pin_1 ((uint16_t)0x0002)#define GPIO_Pin_2 ((uint16_t)0x0004)...but the GPIO_PinSource defines are different:Code:#define GPIO_PinSource0 ((uint8_t)0x00)#define GPIO_PinSource1 ((uint8_t)0x01)...(One of these quirks of the Standard Peripheral Library, GPIO_PinAFConfig wants the "PinSource" (i.e. numeric) style pin definition as input, whereas GPIO_InitTypeDef wants a bitfield.)In other words, your receive pin PD0 should work because GPIO_PinSource1 happens to be identical (apart from type) to GPIO_Pin_0, but your receive pin has not been properly initialized.