Correct use of Ext-Interrupts

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

Moderators: RoccoMarco, lbednarz, tfAteba

User avatar
levrik
Posts: 26
Joined: Wed Dec 21, 2011 7:35 am

Correct use of Ext-Interrupts

Post by levrik »

Hello,
I'm trying to implement some kind of serial driver with use of SPI- and EXTI-driver on the STM32F4 Discovery board for RFM12. To get an idea how other drivers are used in a driver itself I've already had a look in mmc_spi.[ch]. My project is based on the demo-project coming along with ChibiOS(trunk) for the Discovery-Kit.
How is the intended way to use the EXT-I-driver within another driver (and fill the EXTConfig-structure correctly)? As i can see, the config structure is declared const and global in the example. At this point i could fill all values but the callback-function, because this callback-function i'd like to declare static and local within "rfm_lld.c"-file. My first intention was to deliver a pointer to this EXTConfig structure to "rfmObjectInit()" and fill it in "rfm_lld_start()" by the rfm-driver itself:

Code: Select all

void rfm_lld_start(RFMDriver *rfmp) {
[...]
  spiStart(rfmp->spiDevice, rfmp->spicfg);

  rfmp->extcfg->channels[11].mode = EXT_CH_MODE_FALLING_EDGE | EXT_CH_MODE_AUTOSTART;
  rfmp->extcfg->channels[11].cb = rfm_cb;
  rfmp->extcfg->exti[2] = (EXT_MODE_GPIOB) << 12;
  extStart(rfmp->extDevice, rfmp->extcfg);
[...]
}
but it doesn't seem to me being the right approach.

I hope someone can give me a hint for the right direction.

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

Re: Correct use of Ext-Interrupts

Post by Giovanni »

Hi,

I don't see problems with your approach, you don't need "extDevice" because there is always a single EXT driver in the system, you may refer &EXTD1 directly and save some space (unlike SPIs).
You may add some abstraction in order to modify the EXT configuration structure instead of accessing the fields directly, well, probably it would be a good idea to add such an abstraction directly in the EXT driver because its peculiarity.

What you are doing is a "complex" driver which uses other drivers instead of accessing directly HW resources like "normal" drivers do, much like the MMC_SPI and USB_CDC drivers so it fits well in the general architecture.

Giovanni
User avatar
Badger
Posts: 346
Joined: Mon Apr 18, 2011 6:07 pm

Re: Correct use of Ext-Interrupts

Post by Badger »

I was going to suggest similar functionality as an improvement but haven't got around to it yet. In my code I've written this function:

Code: Select all

   void addInterrupt(uint32_t port, uint8_t pin, uint32_t mode, extcallback_t cb) {
      if(pin > 16)
         chSysHalt();

      uint8_t b = pin % 4;
      uint8_t c = (pin - b)/4;
      uint16_t mask_clr = ~(0xFF << b*4);
      uint16_t mask_set = (((uint16_t)port & 0xFF) << b*4);
      
      chSysLockFromIsr();
      extcfg.channels[pin].mode = mode;
      extcfg.channels[pin].cb = cb;
      /* clear bits for this pin */
      extcfg.exti[c] &= mask_clr;
      /* set bits */
      extcfg.exti[c] = extcfg.exti[c] | mask_set;
      chSysUnlockFromIsr();

      extStart(&EXTD1, &extcfg);
      extChannelEnable(&EXTD1, pin);
   }


I'm sure the function could be optimised, but it works for me. I think that the bulk of the config structure could be contained within the lld with a function like this to turn on interrupts as required.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Correct use of Ext-Interrupts

Post by Giovanni »

I will look into adding a similar functionality.

Giovanni
User avatar
levrik
Posts: 26
Joined: Wed Dec 21, 2011 7:35 am

Re: Correct use of Ext-Interrupts

Post by levrik »

Giovanni wrote:I will look into adding a similar functionality.

Giovanni

That would be very much appreciated. Thank you.

Badger wrote:I was going to suggest similar functionality as an improvement but haven't got around to it yet. In my code I've written this function:

Code: Select all

   void addInterrupt(uint32_t port, uint8_t pin, uint32_t mode, extcallback_t cb) {
      if(pin > 16)
         chSysHalt();

      uint8_t b = pin % 4;
      uint8_t c = (pin - b)/4;
      uint16_t mask_clr = ~(0xFF << b*4);
      uint16_t mask_set = (((uint16_t)port & 0xFF) << b*4);
      
      chSysLockFromIsr();
      extcfg.channels[pin].mode = mode;
      extcfg.channels[pin].cb = cb;
      /* clear bits for this pin */
      extcfg.exti[c] &= mask_clr;
      /* set bits */
      extcfg.exti[c] = extcfg.exti[c] | mask_set;
      chSysUnlockFromIsr();

      extStart(&EXTD1, &extcfg);
      extChannelEnable(&EXTD1, pin);
   }


I'm sure the function could be optimised, but it works for me. I think that the bulk of the config structure could be contained within the lld with a function like this to turn on interrupts as required.

Did i understand you correctly by putting this function into ext.c, because there i think it belongs?
User avatar
Badger
Posts: 346
Joined: Mon Apr 18, 2011 6:07 pm

Re: Correct use of Ext-Interrupts

Post by Badger »

you can put it anywhere that has access to your EXTConfig structure. it wouldn't be wise to put it in ext.c because the code is stm32 specific. I created an ExtInt.c file that included the EXTConfig statically defined and that function.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Correct use of Ext-Interrupts

Post by Giovanni »

I opened a thread about the EXT change proposal here.

Giovanni
gordurq
Posts: 2
Joined: Wed May 02, 2012 6:35 am

Re: Correct use of Ext-Interrupts

Post by gordurq »

Does anybody have a simple example (start to finish) of using an external interrupt on the STM32F$ discovery board. No matter what I do I get the same results...no interrupt. If I set a software interrupt by setting EXTI_SWIER via gdb and then continue, I get the interrupt. But toggling the pin...no way....nope. In the development forum some one posted some code that after corrections worked, I tried it but no go. I tried the addInterrupt routine from above, same results. This is driving me nuts....
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Correct use of Ext-Interrupts

Post by Giovanni »

The STM32 EXT driver in ChibiOS is the example you need, study that code.

Look into ./os/hal/platforms/STM32/ext_lld.c and ext_lld.h.

Giovanni
Jacon
Posts: 143
Joined: Wed Dec 08, 2010 7:52 am
Has thanked: 50 times
Been thanked: 5 times

Re: Correct use of Ext-Interrupts

Post by Jacon »

Also, have you tried
\testhal\STM32F4xx\EXT\
demo ?

It's perfectly what you want - simple & complete ;)
Post Reply