2014-11-04 07:40 AM
Hi all,
I am working on a project with stm32f407 and i want to communicate to stm32f407 boards with each other using can communication. I am using sn65hvd transceiver and i made this scheme : i am using 2 circuit like this. Their CANH and CANL are connected other CANH and CANL. i think there is no problem with transceiver part. Probably i am doing something wrong with the code. Here is my code, can anyone tell me where am i doing wrong? I am using can1 tx pb9, and can1 rx pb8. First stm32's tx and rx go receiver and 2 transceiver's canh and canl are common; then output of other transceiver(tx and rx) go other stm32's pb8 and pb9. Here is my code ofreceiverstm32#include ''can_control.h''
#include ''stm32f4_discovery.h''
#include <
stdint.h
>
#define CAN_CLK RCC_APB1Periph_CAN
#define CAN_RX_PIN GPIO_Pin_8
#define CAN_TX_PIN GPIO_Pin_9
#define CAN_GPIO_PORT GPIOB
#define CAN_GPIO_CLK RCC_AHBPeriph_GPIOB
#define CAN_AF_PORT GPIO_AF_4
#define CAN_RX_SOURCE GPIO_PinSource8
#define CAN_TX_SOURCE GPIO_PinSource9
CanTxMsg TxMessage;
CanRxMsg RxMessage;
static
void CAN_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN GPIOs configuration **************************************************/
/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
/* Connect CAN pins to AF7 */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_CAN1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_CAN1);
/* Configure CAN RX and TX pins */
GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN;
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(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = CAN_TX_PIN;
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(GPIOB, &GPIO_InitStructure);
/* NVIC configuration *******************************************************/
NVIC_InitStructure.NVIC_IRQChannel = CAN1_TX_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* CAN configuration ********************************************************/
/* Enable CAN clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
/* CAN register init */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */
CAN_InitStructure.CAN_TTCM = DISABLE;
CAN_InitStructure.CAN_ABOM = DISABLE;
CAN_InitStructure.CAN_AWUM = DISABLE;
CAN_InitStructure.CAN_NART = ENABLE;
CAN_InitStructure.CAN_RFLM = DISABLE;
CAN_InitStructure.CAN_TXFP = ENABLE;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
/* CAN Baudrate = 500kbps (CAN clocked at 48 MHz) */
CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq;
CAN_InitStructure.CAN_Prescaler = 16;
CAN_Init(CAN1, &CAN_InitStructure);
// 48Mhz / ( 8 * ( 1 + 6 + 5) ) = 500Kbps
// 48Mhz / 96
/* CAN filter init */
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);
/* transmit */
TxMessage.StdId = 0x321;
TxMessage.ExtId = 0x11;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.IDE = CAN_ID_STD;
TxMessage.DLC = 8;
/* Enable FIFO 0 message pending Interrupt */
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}
void DelayInt(void)
{
volatile int i = 1000000;
while(i--);
}
uint8_t CanSendRes = 0;
void CanControlInit(void)
{
uint8_t TransmitMailbox = 0;
CAN_Config();
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
}
Then here is my code of transmitter Stm32
#include ''can_control.h''
#include ''stm32f4_discovery.h''
#include <
stdint.h
>
#define CAN_CLK RCC_APB1Periph_CAN
#define CAN_RX_PIN GPIO_Pin_8
#define CAN_TX_PIN GPIO_Pin_9
#define CAN_GPIO_PORT GPIOB
#define CAN_GPIO_CLK RCC_AHBPeriph_GPIOB
#define CAN_AF_PORT GPIO_AF_4
#define CAN_RX_SOURCE GPIO_PinSource8
#define CAN_TX_SOURCE GPIO_PinSource9
CanTxMsg TxMessage;
CanRxMsg RxMessage;
static
void CAN_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN GPIOs configuration **************************************************/
/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
/* Connect CAN pins to AF7 */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_CAN1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_CAN1);
/* Configure CAN RX and TX pins */
GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN;
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(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = CAN_TX_PIN;
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(GPIOB, &GPIO_InitStructure);
/* NVIC configuration *******************************************************/
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* CAN configuration ********************************************************/
/* Enable CAN clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
/* CAN register init */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */
CAN_InitStructure.CAN_TTCM = DISABLE;
CAN_InitStructure.CAN_ABOM = DISABLE;
CAN_InitStructure.CAN_AWUM = DISABLE;
CAN_InitStructure.CAN_NART = ENABLE;
CAN_InitStructure.CAN_RFLM = DISABLE;
CAN_InitStructure.CAN_TXFP = ENABLE;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
/* CAN Baudrate = 500kbps (CAN clocked at 48 MHz) */
CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq;
CAN_InitStructure.CAN_Prescaler = 16;
CAN_Init(CAN1, &CAN_InitStructure);
// 48Mhz / ( 8 * ( 1 + 6 + 5) ) = 500Kbps
// 48Mhz / 96
/* CAN filter init */
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);
/* transmit */
TxMessage.StdId = 0x321;
TxMessage.ExtId = 0x11;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.IDE = CAN_ID_STD;
TxMessage.DLC = 8;
/* Enable FIFO 0 message pending Interrupt */
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}
void DelayInt(void)
{
volatile int i = 1000000;
while(i--);
}
uint8_t CanSendRes = 0;
void CanControlInit(void)
{
uint32_t i = 0;
uint8_t TransmitMailbox = 0;
CAN_Config();
TxMessage.StdId = 0x321;
TxMessage.ExtId = 0x11;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.IDE = CAN_ID_STD;
TxMessage.DLC = 8;
TxMessage.Data[0] = 0xFF;
TxMessage.Data[1] = 0xFF;
TxMessage.Data[2] = 0xFF;
TxMessage.Data[3] = 0xFF;
TxMessage.Data[4] = 0xAA;
TxMessage.Data[5] = 0xBB;
TxMessage.Data[6] = 0xCB;
TxMessage.Data[7] = 0x24;
while(1)
{
CAN_Transmit(CAN1, &TxMessage);
}
}
When i look at my pb9( tx pin) at scop, i cant see anything meaningfull, i think its just noise. i guess i'm doing something so wrong and cant see it, please help me about my problem i will be so appreciated.
(Sorry about my bad english and this long writing :) )
#stm32f2 #stm32 #stm32 #stm32 #can #can #can #bxcan #bxcan
2014-11-04 11:01 AM
I don't think your baud rate computation is correct, but at least they are the same.
I believe I've posted several working examples of CAN on F2/F4 platforms, please review.2014-11-04 01:17 PM
2014-11-04 02:26 PM
Ok, but I'm not going to rack up your code and evaluate it. You say it's broken, I don't need to confirm that diagnosis. You'd want to start with something known to work. You can then evaluate how your code is different. The CAN bus expects a receiving node to send the data too, the sender needs to pay attention to the status of the controller. If you enable interrupts, you must services them.
https://community.st.com/0D50X00009XkiDZSAZ
Edit: Fixed DEAD LINK, original post from Nov 4, 2014
2014-11-05 03:48 AM
i already wrote my second code with your post(on that link). But even i go with same code; i cant see anything at stm32's tx pin with scop. its just noise. i want to ask something, is there a problem with my transmit and receive function? my congiguration function is same with yours( the working code that you post on that link), but when i run transmit function, it doesnt transmit anything :\
void CanControl_rx(void)
{
CAN_Config();
while(1)
{
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
//DelayInt();
}
}
void CanControl_tx(void)
{
uint8_t i = 0;
uint8_t TransmitMailbox = 0;
CAN_Config();
CanTxMsg TxMessage;
// transmit */
TxMessage.StdId = 0x123;
TxMessage.ExtId = 0x00;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.IDE = CAN_ID_STD;
TxMessage.DLC = 8;
TxMessage.Data[0] = 0xFF;
TxMessage.Data[1] = 0xFF;
TxMessage.Data[2] = 0xFF;
TxMessage.Data[3] = 0xFF;
TxMessage.Data[4] = 0xAA;
TxMessage.Data[5] = 0xBB;
TxMessage.Data[6] = 0xCB;
TxMessage.Data[7] = 0x24;
while(1)
{
uint8_t TransmitMailbox = 0;
TransmitMailbox = CAN_Transmit(CAN1, &TxMessage);
while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK) && (i != 0xFFFFFF)) // Wait on Transmit
{
i++;
}
}
}
2014-11-05 11:35 AM
uint8_t i = 0;
// ????
Ok, and if you trigger on this noise and capture the whole envelope of the signal, what does that look like?
http://www.intrepidcs.com/wavebps/CANBusDecode.html
2014-11-17 01:56 AM
I have the same Problem. I have already tested various programs. I have started with the CubeMX and its configuration, went over to the StdPeriphLib. I only tested to send something from the Discovery to a Vector CAN Interface. I also watched the tx pin with a scope and recognized that tht tx pin at the STM goes from low to high when program starts and then never does anything.
I am using Keil uVision in its latest Version. I am very confused that the pin isnt doing anything. Attached is my cubemx main file. ________________ Attachments : main.c : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HznJ&d=%2Fa%2F0X0000000bPl%2FiM_2rSZDZ08oeEG9S_sX9qtrOFlVUrTZ1.M1mYZAnvo&asPdf=false2014-11-17 08:06 AM
I'm not a subscriber to HAL/CUBE, you'll have to find someone who's interested.
I will observe that there doesn't appear to be any pin configuration for CAN usage, the signals don't magically route themselves out of the chip, and the bus timing looks ill considered.2014-11-20 01:32 AM
Hallo
I am using STM32F429ZI for the first time and I need to implement an CAN Bus Communication of tranmsitting and receiving the messages.I am using TJA1041AT CAN transreceiver and to see the messgaes I am using PCAN BUS View.Herwith I am attching the program which I got ur from this website and in this I am facing the few problems. 1.I am not able to set the baud rate to 500Kbps. 2. I want to know on how to change the initalization from CAN1 To CAN2 ________________ Attachments : CAN1_TO_CAN2-Normal_temiz.rar : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hzdo&d=%2Fa%2F0X0000000bPi%2FrTKcZxXxIREB3hdhlOSoQXnu0ksQoVgNGCyDGnobZwA&asPdf=false2014-11-20 08:46 AM
CAN_InitStructure.CAN_Prescaler = RCC_Clocks.PCLK1_Frequency / (14 * 500000); // quanta by baudrate - 500kbps
APB clock would need to be cleanly divisible. CAN2 is a slave to CAN1, so you'd need both clocking. You'd need to change the pin/AF configurations, and the FIFO settings. With the standard split of resources CAN2 would be using 14..27