Interface abstraction

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

Interface abstraction

Post by Badger »

It would be great if the library can work with multiple interfaces (FSMC, GPIO, SPI etc). This would really open up the options for how each display can be used.

My thinking was that we could do something similar to Chibios' BaseSequentialStream style abstraction. A pointer to each interface could then be cast to one generic type, e.g LCDInterfaceDriver*.

The abstraction would be very simple, and would just contain pointers to functions that do reading / writing over the appropriate peripheral.

Here's how BaseSequentialStream works in Chibios:

Code: Select all

/**
 * @brief   BaseSequentialStream specific methods.
 */
#define _base_sequential_stream_methods                                     \
  /* Stream write buffer method.*/                                          \
  size_t (*write)(void *instance, const uint8_t *bp, size_t n);             \
  /* Stream read buffer method.*/                                           \
  size_t (*read)(void *instance, uint8_t *bp, size_t n);

/**
 * @brief   @p BaseSequentialStream specific data.
 * @note    It is empty because @p BaseSequentialStream is only an interface
 *          without implementation.
 */
#define _base_sequential_stream_data

/**
 * @brief   @p BaseSequentialStream virtual methods table.
 */
struct BaseSequentialStreamVMT {
  _base_sequential_stream_methods
};

/**
 * @brief   Base stream class.
 * @details This class represents a generic blocking unbuffered sequential
 *          data stream.
 */
typedef struct {
  /** @brief Virtual Methods Table.*/
  const struct BaseSequentialStreamVMT *vmt;
  _base_sequential_stream_data
} BaseSequentialStream;


And then a driver has support of this abstract 'virtual class':

Code: Select all

/**
 * @brief   @p SerialDriver specific methods.
 */
#define _serial_driver_methods                                              \
  _base_asynchronous_channel_methods

/**
 * @extends BaseAsynchronousChannelVMT
 *
 * @brief   @p SerialDriver virtual methods table.
 */
struct SerialDriverVMT {
  _serial_driver_methods
};

/**
 * @extends BaseAsynchronousChannel
 *
 * @brief   Full duplex serial driver class.
 * @details This class extends @p BaseAsynchronousChannel by adding physical
 *          I/O queues.
 */
struct SerialDriver {
  /** @brief Virtual Methods Table.*/
  const struct SerialDriverVMT *vmt;
  _serial_driver_data
};


Basically we just define a set of function pointers that are at the start of every derived LCDInterfaceDriver struct.

I think the operations we need to support are very basic:
  • read(address, length)
  • write(address, data, length)

There should be no need to abstract the initialisation method.

Does anyone have any thoughts or suggestions?
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: Interface abstraction

Post by Tectu »

Yes, we have to think on how to implement the different Interfaces, I fully agree on that poing.

Your idea does not sound that bad, I am just not sure how we are exactly going to implement that. I am also not sure about performance issues.

My idea for the first implementation was to put that three secitions into the real driver source (_lld.c). The quick'n'dirty implementation would be

Code: Select all

#ifdef GPIO
    // put lowlevel access functions in here
#elseif FSMC
    // put lowlevel access functions in here
#elseif SPI
   // put lowlevel access functions in here
#endif

But of course your version is much much better :)
If you have time and feel to put any work into this, please feel free to make a demo driver, modify an existing driver, or write a new driver we don't already have with your method. I guess the changes in the highlevel functions will just be a few lines (passing the pointer in lcdInit() ?)


To the "current" performance. Here's a PM I got from Abhishek:
Time for some metrics. Using hal Counter in ChibiOS I measured no. of clock cycles taken to execute lcdClear() function:

It takes about 692000 cycles (~243fps Maximum possible Hardware refresh rate with STM32F4 Series) with the driver code. Bitbanging (even onto two different ports) rocks!

Sounds good to me :)


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

Re: Interface abstraction

Post by Badger »

I suppose we wont ever need more than one interface active at once so we might well be better off by defining the interfaces as macros.

The more I think about it something like your quick n dirty implementation would be best and also simplest. It is likely that different drivers communicate in different ways (e.g setting bits in SPI write etc), so it might prove too complicated to try and define a generic interface for each.

It is probably worth creating a separate FSMC / softFSMC driver that can be called in each of the display drivers to avoid code duplication.

About performance, it is reasonably quick providing you don't want to do anything else at the same time. DMA could be useful in some applications, but only when the content is computed elsewhere, e.g streaming from a camera.
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: Interface abstraction

Post by Tectu »

The other thing is that - as you mentioned somewhere else - there are some "strange" controllers, with do have for example 9-bit SPI.

I am not sure where you see code dublication with the quick'n'dity method? The compiler will just pick the matching code at compiling time and dropp the rest.
When you mean "higher low level functions" such as lld_lcdDrawPixel(), I don't see any problem there either.
IMHO we just need to prive the __inline functions for the actual reading and writing from/to the LCD controller?


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

Re: Interface abstraction

Post by Badger »

Tectu wrote:I am not sure where you see code dublication with the quick'n'dity method? The compiler will just pick the matching code at compiling time and dropp the rest.

I meant between drivers. But looking at the simplicity of the GPIO method it probably isn't an issue. Maybe we would gain something by having a driver with a nice configuration struct, but for now it will do.

For hardware FSMC support I will work on an FSMC driver for chibios that can be integrated into the main chibios source tree.
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: Interface abstraction

Post by Tectu »

Sounds good to me! :)

So what directory hirarchy would you recommend now? Can you show me one that is good in your opinion?



~ Tectu
Abhishek
Posts: 266
Joined: Wed May 23, 2012 3:15 pm

Re: Interface abstraction

Post by Abhishek »

I have some working code for the Nokia 6610 lcd with 9bit SPI. Would be willing to share it if required..

However now I'd prefer to stick to my new LCD and make the lib fully functional. If things go in the right way I will make the lib a part of my project.

We could put the interface related code in the glcdconf.h like the switches as Tectu mentioned. Although I'm not at my workbench right now, so couldn't write psuedocode but I guess you get the idea.
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: Interface abstraction

Post by Tectu »

Abhishek wrote:I have some working code for the Nokia 6610 lcd with 9bit SPI. Would be willing to share it if required..

Just write and pull request a driver, if you want to. We won't say no to more drivers.


We could put the interface related code in the glcdconf.h like the switches as Tectu mentioned.

I don't think that we should put some code in there. We would just go like:


Code: Select all

#define LCD_USE_SSD1289
//#define LCD_USE_S6D1121

#define LCD_USE_GPIO
//#define LCD_USE_SPI
// #define LCD_USE_FSMC



~ Tectu
Abhishek
Posts: 266
Joined: Wed May 23, 2012 3:15 pm

Re: Interface abstraction

Post by Abhishek »

Oh yes, that's what I was talking about.

I was thinking about merging my existing library with this one. Since I'm already working on a GUI for my project, I would be the user too!
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: Interface abstraction

Post by Tectu »

Yeah, as I said. Feel free to pull request everything that works, so we can go get straight forward!

But I guess some useful text rendering routine would be better than a GUI at the moment ;-)


~ Tectu
Locked