Page 1 of 2

Changes to the EXT driver

Posted: Sun Apr 22, 2012 4:38 pm
by Giovanni
The idea has been proposed in this other topic in the support forum here.

My idea for implementation is to add a new API:

Code: Select all

void extSetChannel(EXTChannelConfig *cfg);


The new function would change the EXTConfig structure and immediately reprogram the HW (no need to perform a extStart() again).

There are few problems:
1) The EXTConfig structure must be in ram in order to use extSetChannel() but the option to have it in flash is still present.
2) The EXTConfig structure must not contain channel-related custom fields, everything must be in EXTChannelConfig.

About point 2, the STM32 driver would lose the "exti" field in EXTConfig, the port assignment would be ORed to the "mode" field of the EXTChannelConfig structure (saving some space in EXTConfig too).

More ideas?

Giovanni

Re: Changes to the EXT driver

Posted: Mon Apr 23, 2012 12:49 pm
by Badger
Sounds like a good approach. Would the driver then have an array of EXTChannelConfig pointers?

Re: Changes to the EXT driver

Posted: Mon Apr 23, 2012 1:03 pm
by Giovanni
I was thinking just a pointer to a configuration structure containing said array.

Re: Changes to the EXT driver

Posted: Mon Apr 23, 2012 1:12 pm
by Badger
Ok, so the application would still allocate a main EXTConfig type structure (static but not const), but instead of it containing all the EXTChannelConfig structures it would just have an array of pointers? and then each interrupt could be enabled / disabled separately, without worrying about maintaining a global config.

Re: Changes to the EXT driver

Posted: Mon Apr 23, 2012 1:46 pm
by Giovanni
My idea was to keep the current configuration structure, this way you can still allocate it in flash if you don't need to change the driver behavior at runtime.
Allocating it in RAM would allow to just start with a zeroed configuration and then use the new API in order to program channels dynamically.

Giovanni

Re: Changes to the EXT driver

Posted: Mon Apr 23, 2012 8:07 pm
by Giovanni
I committed the change in the trunk, apparently everything is OK but it needs more tests. Apparently the driver become simpler after the change and takes less space.

Giovanni

Re: Changes to the EXT driver

Posted: Mon Apr 23, 2012 9:44 pm
by levrik
Hi,
it seems to me that there are too many chSysLocks in the new ext-functions:

Code: Select all

#define extSetChannelMode(extp, channel, extcp) {                           \
  chSysLock();           // <---- 1st Lock                                                   \
  extSetChannelModeI(extp, channel, extcp);                                 \
  chSysUnlock();                                                            \
}

Code: Select all

void extSetChannelModeI(EXTDriver *extp,
                        expchannel_t channel,
                        const EXTChannelConfig *extcp) {
  EXTChannelConfig *oldcp;

  chDbgCheck((extp != NULL) && (channel < EXT_MAX_CHANNELS) &&
             (extcp != NULL), "extSetChannelModeI");

  /* Note that here the access is enforced as non-const, known access
     violation.*/
  oldcp = (EXTChannelConfig *)&extp->config->channels[channel];

  chSysLock(); // <---- 2nd Lock

  chDbgAssert(extp->state == EXT_ACTIVE,
              "extSetChannelModeI(), #1", "invalid state");

  /* Overwiting the old channels configuration then the channel is reconfigured
     by the low level driver.*/
  *oldcp = *extcp;
  ext_lld_channel_enable(extp, channel);
  chSysUnlock();
}

After implementing the new version of ext-driver on my STM32F4-Discovery-board I ended up in port_halt() with dbg_panic_message "SV#4".extSetChannelModeI

Re: Changes to the EXT driver

Posted: Mon Apr 23, 2012 9:55 pm
by Giovanni
Hi,

Thanks for finding, that is an iclass function so those locks should not be there, fixed in repository.

Giovanni

Re: Changes to the EXT driver

Posted: Tue Apr 24, 2012 9:46 am
by Badger
I have got the latest code working, but It is different to my idea of how we can improve the driver. We could add an option to the driver to include the channel config in the driver memory and allow the adding of interrupts via a new function extAddChannel(port, pin, mode, callback). This would start the driver if needed, add the settings to the channel config structure and enable the interrupt. It would make it easy to configure different interrupts without having to manage one main structure (e.g a driver could more easily call this function).

Re: Changes to the EXT driver

Posted: Tue Apr 24, 2012 10:02 am
by Giovanni
I see some problems with that:

We could add an option to the driver to include the channel config in the driver memory


That would eat RAM when Flash could be used instead, RAM is more valuable. Making this an option would create two entirely different code paths in the driver.

extAddChannel(port, pin, mode, callback)


This is not portable, those parameters apply to the STM32 only, also, not all EXT channels are necessarily linked to the GPIO. This why I chose to use a configuration structure for the channel, a structure can be customized without changing the function signature.
Another problem is that not all port/pin combinations are valid, in the STM32 I could not configure GPIOB4 and GPIOC4 both as EXTI, the function would have to have a failure mode.

Giovanni