cancel
Showing results for 
Search instead for 
Did you mean: 

transmit interrupt of CAN works well in loopback mode but vice versa in normal mode

ttnghiabk
Associate II
Posted on April 06, 2012 at 04:57

Hi all

I have a project associated with using CAN net work, so I must to use CAN module of stm32f103rdt6

I test transmit interrupt of CAN in loopback mode operating corretly but when I change it into normal mode and change nothing esle in my code, transmit interupt didn't work, even I don't know whether my message is sent or not. This is my code I use to initiate modun CAN

CAN_DeInit(CANx);

  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_TXFP = ENABLE;

  CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;

  //CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack;

  

  /* CAN Baudrate = 1MBps*/

  CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;

  CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;

  CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq;

  CAN_InitStructure.CAN_Prescaler = 4;

  CAN_Init(CANx, &CAN_InitStructure);

  /* 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 = 00;

  TxMessage.ExtId = 0x00;

  TxMessage.RTR = CAN_RTR_DATA;

  TxMessage.IDE = CAN_ID_STD;

  TxMessage.DLC = 8;// em can gui 4 byte 1 lan

  

  TxMessage.Data[0]=0;

  TxMessage.Data[1]=0;

  TxMessage.Data[2]=0;

  TxMessage.Data[4]=0;

  TxMessage.Data[5]=0;

  TxMessage.Data[6]=0;

  TxMessage.Data[7]=0;

and this is my nvic configuration

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =1;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);  

 

    //nvic for can

    //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

    NVIC_InitStructure.NVIC_IRQChannel =USB_HP_CAN1_TX_IRQn ;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

and this is my interrupt function

void USB_HP_CAN1_TX_IRQHandler(void)

{

  //CTR_HP();

  ttnghiabk_senduart(''ok.da thanh cong'');

GPIO_WriteBit(GPIOB, GPIO_Pin_12, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_13)));

GPIO_WriteBit(GPIOB, GPIO_Pin_13, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_13)));

    CAN_ClearITPendingBit(CAN1,CAN_IT_TME);

}

I just change   '' CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack;''into

''CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;''. It's incredible, interrupt operation didn't work, I don't understand why???????

Can you show me reason. Thanks indeed

9 REPLIES 9
ttnghiabk
Associate II
Posted on April 10, 2012 at 08:42

poor me, nobody helps me,

domen23
Associate II
Posted on April 10, 2012 at 10:44

Seems obvious, but since you didn't mention it:

- did you check status bits in both cases?

- how about trying some example code? They mostly suck, but usually they do good as a working starting point.
ttnghiabk
Associate II
Posted on April 11, 2012 at 05:14

oh, of couse I use example source from ST's library. I just buitlt it from there

I down many right code about CAN from forum and an other source to check but I don't realize the diffirence. so that I said''It's incredible''.

hix. Can you recommend the detail more 

domen23
Associate II
Posted on April 11, 2012 at 08:43

Of course! :p Now let me repeat:

Seems obvious, but since you didn't mention it:

- did you check status bits in both cases?

Posted on April 11, 2012 at 14:05

Another problem here is that a whole bunch of code is missing. I don't see any clock or pin configuration.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ttnghiabk
Associate II
Posted on April 12, 2012 at 17:17

today, I check CAN_IER and CAN_TSR in keil simulator , and the result int both case is

CAN_TSR is set 0x1C000003 and CAN_IER is set 0x00000001, ( when I don't use command ''CAN_ClearITPendingBit(CAN1,CAN_IT_TME);'' in my interrupt function

that mean there's nothing wrong, ''TMEIE bit is set'' means interrupt is enable

and ''TX0K0 and RQCP0 are set'' means that message is transmitted successfully and interrupt flag is set

And here is my all source code

http://www.mediafire.com/?kx6plj1if4ci7bw

What can I do now ? please help me! thanks in advance

emmanuelmigabo2007
Associate II
Posted on October 17, 2012 at 10:23

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6c1&d=%2Fa%2F0X0000000bro%2FV5FbBPXZqMaKTWe.OuKT8zlvQum0cEsDcBRFhNMLJN8&asPdf=false
Posted on October 17, 2012 at 15:50

I'm using CAN on F2 devices.

The F1 board I have access to (STM3210E-EVAL) has CAN on PB8/9. I was able to generate CANTX traffic with this

CanTxMsg TxMessage;
CanRxMsg RxMessage;
void CAN_setup(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
#if 0 
/* Configure CAN1 RX pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure CAN1 TX pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); 
#endif
#if 1 // STM3210E-EVAL
/* Configure CAN1 RX pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure CAN1 TX pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); 
GPIO_PinRemapConfig(GPIO_Remap1_CAN1 , ENABLE); // PB.8 & PB.9
#endif
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);//Enable the clock for CAN
CAN_DeInit(CAN1);
/* Struct init*/
CAN_StructInit(&CAN_InitStructure);
/* Configure CAN1 **************************************************/ 
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 = ENABLE; // DISABLE;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
#if 0
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; 
CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq;
CAN_InitStructure.CAN_Prescaler = 32; // 125kbps
#else
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
CAN_InitStructure.CAN_Prescaler = 36000000 / (15 * 125000); // quanta by baudrate
#endif
CAN_Init(CAN1, &CAN_InitStructure);
/* CAN1 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 = 0x321 << 
5
;
CAN_FilterInitStructure.CAN_FilterIdLow
= 
0x0000
;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh
= 
0xFFE0
;
CAN_FilterInitStructure.CAN_FilterMaskIdLow
= 
0x0000
;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment
= 
0
;
CAN_FilterInitStructure.CAN_FilterActivation
= 
ENABLE
;
CAN_FilterInit(&CAN_FilterInitStructure);
/* Transmit INITIALIZATION*/
TxMessage.IDE
= 
CAN_ID_STD
;
TxMessage.DLC
= 
6
; 
TxMessage.StdId
= 
0x321
;
TxMessage.ExtId
= 
0
;
TxMessage.RTR
= 
CAN_RTR_DATA
; 
TxMessage.Data[0] = 0xCA;
TxMessage.Data[1] = 0xFE;
TxMessage.Data[7] = 0x55;
TxMessage.Data[8] = 0xAA;
}
int main(void)
{
volatile int i;
uint8_t TransmitMailbox;
uint32_t count;
CAN_setup();
count
= 
0
;
while(1)
{
TxMessage.Data[2] = (uint8_t)((count >> 0) & 0xFF);
TxMessage.Data[3] = (uint8_t)((count >> 8) & 0xFF);
TxMessage.Data[4] = (uint8_t)((count >> 16) & 0xFF);
TxMessage.Data[5] = (uint8_t)((count >> 24) & 0xFF);
count++;
TransmitMailbox = CAN_Transmit(CAN1, &TxMessage);
i = 0;
while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK) && (i < 0xFFFFFF))
{
i++;
}
}
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ma.alam9
Associate III
Posted on September 14, 2016 at 14:37

Hello All,

I have been using CAN Tx Rx with polling and timeout for a long time but now I want to do it with interrupts. I am usnig an STM32f334c8t7 processor which is interfraced to Microchip's 2562 CAN transciever. I can enable interrrupt in the IER and some CAN messages are sent (6 to be precise)  before it all freezes. I can actually not make up the sequence for transmitting via interrupts. When should i enable the TME interrupt when and what should be in the TX Complete callback function. Is it necessary to implement it or not? can somebody please draw a basic outline of the sequence. PS: I am using MX Cube for code generation and i think somehow this HAL locking unlocking is messing it all up. I want to quickly fill all the 3 mail boxes one by one and then they get transmitted and i get the interrupt upon which i would transmit even more messages and so on.

Thanks