Layering drivers

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
Post Reply
simon.s
Posts: 2
Joined: Mon Jan 11, 2016 8:02 am

Layering drivers

Post by simon.s »

Hello.

I'm new to ChibiOS, but not to programming in general, or even real-time µC programming which is where I cut my teeth.

Now, I'd like to develop a driver for the (now defunct) Apple Desktop Bus, I'm developing a hardware dongle for the older Wacom Intuos tablets. As a stand-alone application, I think I can probably manage it using the (E)ICU and PWM drivers , but I'd quite like to pull the ADB side out into a separate driver that I can use in other applications.

The problem is that, from within (for example) an ICU callback, I only have access to the ICU driver object. For example, I might write something like this:

Code: Select all

void falling_edge_cb (ICUDriver * icu) {
 
  switch (state) {
  case WAIT_START:
    state = IN_START;
    break;
  case IN_START:
    state = IN_DATA;
    bit = 7;
    break;
  case MAYBE_STOP:
    bit = 7;
    byte_count++;
    state = IN_DATA;
    /* deliberately fall through */
  case IN_DATA:
    data = (data << 1);
    if (low_time < threshold_time) data |= 1;
    if (bit == 0) {
      state = MAYBE_STOP;
    } else {
      bit--;
    }
    break;
  default:
    /* error */
    state = ERROR;
    break;
  }
}

But this means I have variables `state`, `bit`, `data` and `byte_count` as file-local variables, rather than being able to wrap them into an ADB Codec driver object, which is where they should more reasonably live (they are ADB Codec state).

I think what I'm after is the ability to pass some sort of arbitrary "context" object into callbacks instead of the "driver" object that's being passed in now. That way my callbacks could get an ADBCodecDriver object, which contains the relevant ICUDriver object.

Does that make sense?

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

Re: Layering drivers

Post by Giovanni »

Hi,

It makes sense, usually you can do this by extending the structure of the driver. ICUDriver can be extended by defining the ICU_DRIVER_EXT_FIELDS macro, for example in halconf.h.

You could add a generic "link" field pointing to your context structure.

It is poor men way of creating a derived class "ICU with link".

Giovanni
simon.s
Posts: 2
Joined: Mon Jan 11, 2016 8:02 am

Re: Layering drivers

Post by simon.s »

thanks Giovanni, I'll have a look at that
Post Reply