Page 1 of 2
Correct use of Ext-Interrupts
Posted: Sun Apr 22, 2012 9:24 am
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.
Re: Correct use of Ext-Interrupts
Posted: Sun Apr 22, 2012 9:46 am
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
Re: Correct use of Ext-Interrupts
Posted: Sun Apr 22, 2012 9:59 am
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.
Re: Correct use of Ext-Interrupts
Posted: Sun Apr 22, 2012 11:58 am
by Giovanni
I will look into adding a similar functionality.
Giovanni
Re: Correct use of Ext-Interrupts
Posted: Sun Apr 22, 2012 12:32 pm
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?
Re: Correct use of Ext-Interrupts
Posted: Sun Apr 22, 2012 12:49 pm
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.
Re: Correct use of Ext-Interrupts
Posted: Sun Apr 22, 2012 4:40 pm
by Giovanni
I opened a thread about the EXT change proposal
here.
Giovanni
Re: Correct use of Ext-Interrupts
Posted: Wed May 02, 2012 6:50 am
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....
Re: Correct use of Ext-Interrupts
Posted: Wed May 02, 2012 8:32 am
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
Re: Correct use of Ext-Interrupts
Posted: Wed May 02, 2012 10:28 am
by Jacon
Also, have you tried
\testhal\STM32F4xx\EXT\
demo ?
It's perfectly what you want - simple & complete
