Changes to the EXT driver

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Changes to the EXT driver

Post 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
User avatar
Badger
Posts: 346
Joined: Mon Apr 18, 2011 6:07 pm

Re: Changes to the EXT driver

Post by Badger »

Sounds like a good approach. Would the driver then have an array of EXTChannelConfig pointers?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Changes to the EXT driver

Post by Giovanni »

I was thinking just a pointer to a configuration structure containing said array.
User avatar
Badger
Posts: 346
Joined: Mon Apr 18, 2011 6:07 pm

Re: Changes to the EXT driver

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Changes to the EXT driver

Post 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
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Changes to the EXT driver

Post 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
User avatar
levrik
Posts: 26
Joined: Wed Dec 21, 2011 7:35 am

Re: Changes to the EXT driver

Post 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
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Changes to the EXT driver

Post by Giovanni »

Hi,

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

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

Re: Changes to the EXT driver

Post 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).
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Changes to the EXT driver

Post 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
Post Reply