Ext driver example

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Ext driver example

Post by Tectu »

Hello Folks,

Could someone provide a very bare example, how to use the EXT driver? I didn't had any problems at all with other drivers like SPI, PAL, ADC etc, when I read the documentation. But I really don't have any clue how to use the EXT driver to use external interrupts, for example for a touchpad.


Thanks

~ Tectu
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Ext driver example

Post by Giovanni »

Hi,

There are examples under ./testhal.

Giovanni
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: Ext driver example

Post by Tectu »

ooh! Thats pretty useful.

Thanks for the tip, sir!
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: Ext driver example

Post by Tectu »

I've one more question:
How about arguments etc? Is it possible to pass a pointer to a big buffer to the callback function? I'd need a buffer of 2048 bytes size there. I guess it's not that good when I write unsigned char buffer[2048]; into the callback function.

How to make this work the right way?


~ Tectu
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Ext driver example

Post by Giovanni »

Hi,

In general large buffers should never be automatic variables. If you make it static you don't need to pass a pointer, you already know where it is.

Giovanni
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: Ext driver example

Post by Tectu »

Can I go with another question?

I use the EXT driver to fire an interrupt when my touchpad controller puts PEN_IRQ low. This works great. I can toggle an LED by pressing the touchpad ;-)
Now, of course, I want to use the ISR to read out the X and Y coordinates. Now: When I make:

Code: Select all

static void touchIRQ(EXTDriver *extp, expchannel_t channel) {
    (void)extp;
    (void)channel;

    lcdDrawPixel(tpReadY(), tpReadY(), Green);
}


I can compile, and load the .elf. When I run, nothing works :D It just hangs there and not even anything from the main gets executed. When I comment the lcdDrawPixel line out, it works. I could figure out that the problem is the calling of tpReadX() and tpReadY() causes this error.

This is my tpReadX():

Code: Select all

static uint16_t tpReadRealX(void) {
   uint32_t results = 0;
   uint16_t i, x;

   for(i=0; i<CONVERSIONS; i++) {
      readX();   
      results += readX();
   }

        // get from 0 to 2048   to  0 to LCD "length"
   x = (((lcdGetHeight()-1) * (results/CONVERSIONS)) / 2048);

   return x;
}

uint16_t tpReadX(void) {
        // calibrate
   return cal.xm * tpReadRealX() + cal.xn;
}




I also tried:

Code: Select all

static void touchIRQ(EXTDriver *extp, expchannel_t channel) {
    (void)extp;
    (void)channel;

    lcdDrawPixel(tpReadY(), tpReadY(), Green);
}

void foo(void) {
    uint16_t x, y;

    x = tpReadX();
    y = tpReadY();
}


This gave the same result.


Any ideas?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Ext driver example

Post by Giovanni »

Are you OK with using complex graphic primitives from ISRs?

BTW, moving to the support forum in order to reduce noise in the development forum.

Giovanni
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: Ext driver example

Post by Tectu »

Thanks for the moving, I'll post similar question to there from now on (if i am allowed to ask more ;-))

What would be the propper way to do that then? I never worked with an RTOS. I come from the AVR side.
I guess I can use external interrupts from the PEN_IRQ signal from the touchpad controller somehow to read out the actual coordinates?


~ Tectu
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Ext driver example

Post by Giovanni »

You are welcome to ask questions :-)

If you need to perform an "heavy" operation in response of an external event (the callback) usually you wake up a thread to do the work out of ISR context.
There is an article in the documentation about various ways to wakeup a thread:

http://www.chibios.org/dokuwiki/doku.ph ... tos:wakeup

the right one depends on what you want to achieve. Additionally you may use a mailbox if you need to send messages from the ISR to the thread.

Giovanni
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: Ext driver example

Post by Tectu »

Giovanni wrote:You are welcome to ask questions :-)

That's nice to hear. I always try to not ask too stupid questions :D


Thanks for the link. This looks like what I need. I did the following:

Code: Select all

static Thread *tpThread = NULL;
void mythread(void *p) {
    while (TRUE) {
        palTogglePad(GPIOD, GPIOD_LED2);
        chEvtWaitAny((eventmask_t)1);
    }   
}

static void touchIRQ(EXTDriver *extp, expchannel_t channel) {
    (void)extp;
    (void)channel;

    chSysLockFromIsr();
    chEvtSignalFlagsI(tpThread, (eventmask_t)1);
    chSysUnlockFromIsr()
}


When I compile, load and run, the same behaviour appears again: Nothing works, It hangs as before I even get to the change to calibrate the screen. I get into _unhandled_exception.

Any ideas?
I don't think that it is some issue with my touchpad code, because it works great without the IRQ stuff.


~ Tectu
Post Reply