cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F072RBT6 CAN-BUS Problem

yalcinkarakoc87
Associate II
Posted on September 08, 2015 at 10:28

I want to use Stm32f072rbt6 can bus Port A CAN RX PA11 CAN TX PA12

I want to dara send and recive over can bus. I write this code bu it was'nt work. What is my mistake.

/* Includes ------------------------------------------------------------------*/
#include ''main.h''
/** @addtogroup STM32F072B_DISCOVERY
* @{
*/
/* Private variables ---------------------------------------------------------*/
static __IO uint32_t TimingDelay;
extern __IO uint32_t ButtonPressed;
void CANTX(void);
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_ClocksTypeDef RCC_Clocks;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program.
* @param None
* @retval None
*/
int main(void)
{ 
/*!< 
At
this stage the microcontroller clock setting is already configured, 
this is done through SystemInit() function which is called from startup
file (startup_stm32f0xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f0xx.c file*/ 
/* Setup SysTick Timer for 1 msec interrupts.*/
if (SysTick_Config(SystemCoreClock / 1000))
{ 
/* Capture error */ 
while (1);
} 
/* Initialize LEDs, User Button on STM32F072B-DISCO board ***********/
STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI); 
STM_EVAL_LEDInit(LED3);
STM_EVAL_LEDInit(LED4);
STM_EVAL_LEDInit(LED5);
STM_EVAL_LEDInit(LED6);
/* Enable Clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
/* Configure CAN RX and TX pins */
GPIO_InitStructure.GPIO_Pin
= 
GPIO_Pin_8
| GPIO_Pin_9;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Connect CAN pins to AF */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_4); // CAN1_RX
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_4); // CAN1_TX
/* Configure USART1 pins: Rx and Tx ----------------------------*/
GPIO_InitStructure.GPIO_Pin
= 
GPIO_Pin_9
| GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed
= 
GPIO_Speed_50MHz
;
GPIO_InitStructure.GPIO_Mode
= 
GPIO_Mode_AF
;
GPIO_InitStructure.GPIO_OType
= 
GPIO_OType_PP
;
GPIO_InitStructure.GPIO_PuPd
= 
GPIO_PuPd_UP
;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate
= 
9600
;
USART_InitStructure.USART_WordLength
= 
USART_WordLength_8b
;
USART_InitStructure.USART_StopBits
= 
USART_StopBits_1
;
USART_InitStructure.USART_Parity
= 
USART_Parity_No
;
USART_InitStructure.USART_HardwareFlowControl
= 
USART_HardwareFlowControl_None
;
USART_InitStructure.USART_Mode
= 
USART_Mode_Rx
| USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1,ENABLE);
/*---can---*/
RCC_GetClocksFreq(&RCC_Clocks);
/* CAN register init */
CAN_DeInit(CAN);
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
= 
DISABLE
;
CAN_InitStructure.CAN_RFLM
= 
DISABLE
;
CAN_InitStructure.CAN_TXFP
= 
DISABLE
;
CAN_InitStructure.CAN_Mode
= 
CAN_Mode_Normal
;
/* quanta 1+6+
7
= 
14
, 14 * 
24
= 
336
, 42000000 / 
336
= 
125000
*/
/* CAN 
Baudrate
= 
125Kbps
(CAN clocked at 42 MHz) 
Prescale
= 
24
*/
/* Requires a clock with integer division into APB clock */
CAN_InitStructure.CAN_SJW
= 
CAN_SJW_1tq
; // 1+6+
7
= 
14
, 1+14+
6
= 
21
, 1+15+
5
= 
21
CAN_InitStructure.CAN_BS1
= 
CAN_BS1_6tq
;
CAN_InitStructure.CAN_BS2
= 
CAN_BS2_7tq
;
CAN_InitStructure.CAN_Prescaler
= 
RCC_Clocks
.PCLK_Frequency / (14 * 125000); // quanta by baudrate
CAN_Init(CAN, &CAN_InitStructure);
/* CAN filter init */
CAN_FilterInitStructure.CAN_FilterMode
= 
CAN_FilterMode_IdMask
; // IdMask or IdList
CAN_FilterInitStructure.CAN_FilterScale
= 
CAN_FilterScale_32bit
; // 16 or 32
CAN_FilterInitStructure.CAN_FilterIdHigh
= 
0x0000
; // Everything, otherwise 11-bit in top bits
CAN_FilterInitStructure.CAN_FilterIdLow
= 
0x0000
;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh
= 
0x0000
;
CAN_FilterInitStructure.CAN_FilterMaskIdLow
= 
0x0000
;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment
= 
CAN_FIFO0
; // Rx
CAN_FilterInitStructure.CAN_FilterActivation
= 
ENABLE
;
CAN_FilterInitStructure.CAN_FilterNumber
= 
0
; // CAN1 [ 0..13]
CAN_FilterInit(&CAN_FilterInitStructure);
CAN_FilterInitStructure.CAN_FilterNumber
= 
14
; // CAN2 [.27]
CAN_FilterInit(&CAN_FilterInitStructure);
/* End of Initialisation */
/* Infinite loop */
while (1)
{ 
Demo();
}
}
/**
* @brief Demo regroup three chid demonstration on USB, Mems and LTS.
* @param None
* @retval None
*/
void Demo(void)
{
/* Blinking LEDs & Wait for User button to be pressed to switch to MEMS demo */
while (1)
{ 
USART_Cmd(USART1,ENABLE);
USART_SendData(USART1, 'B');
Delay(100);
CANTX();
}
}
void CANTX(void)
{
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] = 0x02;
TxMessage.Data[1] = 0x11;
TxMessage.Data[2] = 0x11;
TxMessage.Data[3] = 0x11;
while(1) // Do not want to exit
{
volatile uint32_t i;
static int 
j
= 
0
;
uint8_t 
TransmitMailbox
= 
0
;
TxMessage.Data[4] = (j >> 0) & 0xFF; // Cycling
TxMessage.Data[5] = (j >> 8) & 0xFF;
TxMessage.Data[6] = (j >> 16) & 0xFF;
TxMessage.Data[7] = (j >> 24) & 0xFF;
j++;
TransmitMailbox = CAN_Transmit(CAN, &TxMessage);
i = 0;
while((CAN_TransmitStatus(CAN, TransmitMailbox) != CANTXOK) && (i != 0xFFFFFF)) // Wait on Transmit
{
i++;
//CANRX(); // Pump RX
}
//CAN2RX();
}
}
/**
* @brief Inserts a delay time.
* @param nTime: specifies the delay time length, in 10 ms.
* @retval None
*/
void Delay(__IO uint32_t nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
/**
* @brief Decrements the TimingDelay variable.
* @param None
* @retval None
*/
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{ 
TimingDelay--;
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{ 
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d\r\n'', file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif

#stm32f072-can-bus
6 REPLIES 6
Posted on September 08, 2015 at 17:45

And what do you have the CAN bus connected too? You need to connect to something that can receive/acknowledge the packets.

What doesn't work and how do know?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
yalcinkarakoc87
Associate II
Posted on September 09, 2015 at 08:39

I have a WaveShare CAN Transceiver. Also i have PCAN -USB Monitor.  I tested CAN-Transceiver Over STM32F4 Kit. Transceiver is worked. Also I added PB8-PB9 CAN Pins my board doesnt work. Also i have Microelectronica STM32F4 Shield. This board has a can transceiver i plugged this transceiver on my board bu same result. 

I thing my code isn't work. Could you help me please ? Is there any working code STM32F072RBT6 ?

yalcinkarakoc87
Associate II
Posted on September 11, 2015 at 11:06

I dont now what is my error.  Hardware can be damaged. I want to check working code sample. 

jpeacock
Associate II
Posted on September 11, 2015 at 13:53

The CAN ESR register has error information so look at it first to determine why you don't have a connection.  The REC and TEC error counts plus flags will tell you if the bus has shut down due to excessive errors.  Look at the BOF flag, if set the bus has disconnected due to a failure in the interface or bus.  The LEC last error code will tell you why your transmit failed.

The CAN section in the reference manual has a section on error management, you might want to read it.

  Jack Peacock
l90mehdi
Associate II
Posted on August 31, 2016 at 11:12

hi dear clive1

i can receive message on the can bus from FIFO0 (my chip:STM32f103RBT6)

but i can not recieve any thing from FIFO1.

when i can receive the message from FIFO1????

please help me.

thank you 

Walid FTITI_O
Senior II
Posted on September 01, 2016 at 15:51

Hi khastar.mehdi,

It is related to a recent reported bug in Receive function about FiFONumber. It is under fix phase. Meanwhile , add the following line into can_Receive_IT() function before FMI line :

hcan->pRxMsg->FIFONumber = FIFONumber;

Check threads related to this topic: [DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Configure%20CAN%20to%20receive%20in%20both%20FIFOs&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=53]thread1 [DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Java/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Java/%5bSTM32F2%5d%20issue%20with%20CAN-HAL%20interrupt%20receivng&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000F9A0E3A95BA69146A17C2E80209ADC21&currentviews=18]thread2 -Hannibal-