cancel
Showing results for 
Search instead for 
Did you mean: 

CAN communication on STM32F429ZI- Nucleo

Nada Ali
Associate II
Posted on April 24, 2018 at 14:03

Hi

I've already downloaded the en.stm32cubef4 package, but the CAN examples are on another board. If I want to use the examples on my STM32F429ZI- Nucleo and MCP2561 transceiver, what should I change in the example?

I tried to use the MBED online compiler and I used this example:

#include 'mbed.h'

DigitalOut led1(LED1);

DigitalOut led2(LED2);

CAN can1(PB_8,PB_9);

char counter = 0;

main()

{

printf('start\n');

can1.frequency(2500000);

//can1.reset();

//can1.mode(CAN::Normal);

while(true){

if(can1.write(CANMessage(1337, &counter, 1)))

{

// counter++;

printf('Message sent: %d\n', counter);

}

else

{

printf('error');

}

counter++;

led1=!led1;

wait_ms(100);

}

}

with no luck. I was receiving error. I tried the CAN communication on LPC1768 mbed board with MCP2561 transceiver and the results were perfect.

Please, it's really urgent and am still new to this board. 

Thank you

Nada

9 REPLIES 9
T J
Lead
Posted on April 24, 2018 at 16:14

Can frequency is tooooo high... 

my someone else is toooo high... to read it...

then you must be sure to turn ABOM to enable

then you must be sure that no two transmitters can not transmit the same MsgID.

then it should be good. up to 250,000Hz.

Nada Ali
Associate II
Posted on April 24, 2018 at 16:22

Thank you T J Now I noticed that I add 0 by mistake. Sorry

Nada Ali
Associate II
Posted on April 24, 2018 at 16:26

T J another question I can enable 

ABOM  on the MBED compiler and what did you mean by 

two transmitters can transmit the same MsgID?

Thank you

T J
Lead
Posted on April 24, 2018 at 16:59

' CanBus '  is a hardware message priority system.

Two devices can start transmitting at the same time, when 1 ID bit is high on one transmitter and low from the other transmitter, the Low wins and the first transmitter stops transmission to relinquish the bus to the higher priority message.

This saves heaps of time in transmission and retries.

if the MsgID is the same from two transmitters you have bypassed the ' hardware CanBus message system'.

Now, if the data is different from these two transmitters, the bus will go into error, and you may need a power down to fix it.

The 'CanBus message hardware function' only checks the MsgID, if the following data is in contention, the Bus will lock down into error.

sorry for the Typo, corrected now.

Posted on April 24, 2018 at 20:34

Thank you TJ for your help.  

Nada Ali
Associate II
Posted on April 25, 2018 at 11:51

Hi T J

Still no luck in making this code working. I tried to replace the mbed library with mbed-dev but I don't know how will I write it in my code cause I got an error when I wrote #include ''mbed-dev.h'' cause in one thread I saw someone used this library and changed the can_api.c to 

enable 

ABOM.

 if I want to 

enable 

ABOM

? , can I do that with mbed.h and where?

Posted on April 26, 2018 at 01:44

in MBED, I use this code:

    can.frequency(20000);                                // set the bit rate

    can.attach(&onMsgReceived,CAN::RxIrq);   // attach the 'CAN  receive-complete' interrupt service routine (ISR)

    can.attach(&showTxCANSent,CAN::TxIrq);   // attach the 'CAN transmit-complete' interrupt service routine (ISR)

    I dont set ABOM, in MBED, it must already be done.

void onMsgReceived(void) {

    msgAvailable = true;

        can.nread();

}

void CAN::nread(void) {

    lock();

    readCanRxFifos(&_can);

    unlock();

}

void readCanRxFifos(can_t *obj){

    

  CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);

    int readCanBytecount;

    char canFifo1FullFlag = can->RF1R & CAN_RF1R_FMP1;

    if(canFifo1FullFlag){

        {

            readCanBytecount = (can->sFIFOMailBox[1].RDTR &0x0f);    

            canRxMsgBottomWord[canRxMsgIN]    = can->sFIFOMailBox[1].RDLR;

            canRxMsgTopWord   [canRxMsgIN]    = can->sFIFOMailBox[1].RDHR;

            canRxMsgID [canRxMsgIN] = can->sFIFOMailBox[1].RIR >> 21;

            can->RF1R |= CAN_RF1R_RFOM1;                         // release FIFO

            canRxMsgLength [canRxMsgIN] =    readCanBytecount;  

            ++ canRxMsgIN;

                 canRxMsgIN &= 0x3F;    // 64 entries only

            canRxMsgTableEMPTY = false;

            if( canRxMsgIN == canRxMsgOUT) canRxMsgTableFULL = true;

        }

                can->IER |= CAN_IER_FMPIE1;                               // (11)        Set FIFO1 message pending IT enable

    }

    char canFifo0FullFlag = can->RF0R & CAN_RF0R_FMP0;

    if(canFifo0FullFlag){

        //while ( canFifo0FullFlag !=0)

        //while((can->RF0R & CAN_RF0R_FMP0)!=0)

        {

            readCanBytecount         = (can->sFIFOMailBox[0].RDTR &0x0f);   

            canRxMsgBottomWord[canRxMsgIN]    = can->sFIFOMailBox[0].RDLR;

            canRxMsgTopWord   [canRxMsgIN]    = can->sFIFOMailBox[0].RDHR;

            uint32_t canRxmsgID =  can->sFIFOMailBox[0].RIR >> 21;

            can->RF0R |= CAN_RF0R_RFOM0;                         // release FIFO

            if ( canRxMsgTableFULL){    

                canRxMsgTableOverflow = true;  // now dump new frame...

            }else{

                canRxMsgID [canRxMsgIN] = canRxmsgID;

                canRxMsgLength [canRxMsgIN] = readCanBytecount;

            ++     canRxMsgIN;

                    canRxMsgIN &= 0x3F;    // 64 entries only

                    canRxMsgTableEMPTY = false;

                if( canRxMsgIN == canRxMsgOUT)

                    canRxMsgTableFULL = true;

            }

            

            //length = sprintf(string + length,'%08X, %08X :W',canFifoBuf.d32[0],canFifoBuf.d32[1]);

//            for( int i = 0; i < readCanBytecount; i++){

//                

//                canRxBuffer[canRxpointerIN++] =  canFifoBuf.d8[i];            

//                canRxpointerIN &= 0xFF;

//                if (canRxpointerIN == canRxpointerOUT )        CanRxbufferOverrun = true;

//                //length += sprintf(string + length,'%02X, ',canFifoBuf.d8[i]);

//            }

            //sprintf(string + length -2,'\n\r');        // remove 2 bytes, the last comma and space

        }

        can->IER |= CAN_IER_FMPIE0;                // (11)        Set FIFO1 message pending IT enable

    }

//            if (length >0)  puts(string);

}

Nada Ali
Associate II
Posted on April 26, 2018 at 07:44

Thank you so much T J for your support and help. I will try it and keep you updated.

Nada

Posted on April 26, 2018 at 08:57

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=0680X000006I6v3&d=%2Fa%2F0X0000000bxy%2F.pqX6UiHY6LwhHu17hj44ZcyMdCx_jHUpv0Bt5kbRe0&asPdf=false