0

I'm using digitalRead of wiringPi to detect packet received in CAN chip, so i'm send data and wait for falling edge of the can interrupt pin as you can see here:

gettimeofday(&t1, NULL);
digitalWrite(7,HIGH);
do{
    delayMicroseconds(10);

    if(!digitalRead(CAN_INT_PIN)){
    //if(waitForInterrupt(CAN_INT_PIN,-1)){
        //Dados novos no buffer, resta saber qual
        events = DRV_MCP25625_ModuleEventGet();
        if (events & DRV_MCP25625_RXB1_EVENT) {
           DRV_MCP25625_ReceiveMessageGet(DRV_MCP25625_RX_CH0, &rxObj, rxd, 8);
           pcBuf[0] = 'M';
           pcBuf[1] = rxObj.ctrl.DLC; //Data length
           pcBuf[2] = rxObj.id.SID & 0xFF;
           pcBuf[3] = (unsigned char) rxObj.id.SID>>8;
           memcpy(&pcBuf[4],rxd,8);
           // Clear flag
           DRV_MCP25625_ModuleEventClear(DRV_MCP25625_RXB1_EVENT);
           digitalWrite(7,LOW);
           return 12;


        }
       // Check RXB0
       if (events & DRV_MCP25625_RXB0_EVENT) {

           // Get message
           DRV_MCP25625_ReceiveMessageGet(DRV_MCP25625_RX_CH0, &rxObj, rxd, 8);
           pcBuf[0] = 'M';
           pcBuf[1] = rxObj.ctrl.DLC; //Data length
           pcBuf[2] = rxObj.id.SID & 0xFF;
           pcBuf[3] = (unsigned char) rxObj.id.SID>>8;
           memcpy(&pcBuf[4],rxd,8);
           // Clear flag
           DRV_MCP25625_ModuleEventClear(DRV_MCP25625_RXB0_EVENT);
           digitalWrite(7,LOW);
           return 12;

       }
    }

    gettimeofday(&t2, NULL);
    elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
    //elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms

    //printf("Elapsed Time: %.2f\n",elapsedTime);


}while(elapsedTime <= *iTimeout );
digitalWrite(7,LOW);

The problem is that sometimes raspberry don't detect falling edge of the pin. In this image is working: https://ibin.co/3M6fO9qtumjH.png

Here you can see that in channel 5 we have the interrupt in every packet received and after i get the packet over spi. At 1s260ms i send a packet over spi and CAN again, and wait... In next image you can see that i have the falling edge in channel 5 and the raspberry don't detects.. https://ibin.co/3M6hqWJzPMHA.png

Thanks

1 Answer 1

1

In summary the code is

digitalWrite(7,HIGH);
do
{
    delayMicroseconds(10);

    if (!digitalRead(CAN_INT_PIN))
    {
        /* code */
    }

    /* code */

} while (condtition);

digitalWrite(7,LOW);

So roughly 100 thousand times a second you test a GPIO.

However Linux is a multi-user, multi-tasking operating system. Every now and then it runs something other than your program. Your program will at intervals be paused for several milliseconds, perhaps tens of milliseconds depending on what else is going on.

Rather than polling you could use interrupts which may (or may not) prove to be more reliable. However there will be a latency of around 50 microseconds between the interrupt being detected and your user program being called.

2
  • Thanks.. I tested with waitForInterrupt() function but returns 1 without interrupt condition... May 11, 2017 at 16:31
  • 1
    @R.Rodrigues GPIO interrupts do work. Perhaps you misconfigured the waitForInterrupt() call or it's not the appropriate choice.
    – joan
    May 11, 2017 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.