Jump to content

Software IIC master in C, C++ class and C++ template

Recommended Posts

This is software IIC master code in C, and C++ using a class and a template. There is also code/class/template for IIC EEPROM and the INA219 voltage/current monitor.


There are 4 core functions...

init(); // Init GPIO
start(); // Start bit
stop(); // Stop bit
unsigned ex(unsigned); // Data byte & ack exchange


and 3 helper functions that call ex();...

unsigned write(unsigned); // Write a byte & ack, verify
unsigned read(); // Read a byte & ack
unsigned read_nak(); // Read a byte & nak


Creating an instance of the IIC object is different for each code type:

T_IIC iic = { BIT1, BIT0, &P2IN, &P2OUT, &P2DIR };

C++ class:

C++ template:
CSWIIC  iic;


The C functions require that the IIC structure be passed as the first argument. The C++ class and template use the usual class.function notation.

a = iic_write(&iic, ;

C++ class or template:
a = iic.write(;


This is a disassembly of the start() function for all three versions. The C function and C++ class member function are identical code! The template allows the compiler to do compile time optimization that can not be done with C or C++ class. It is smaller, faster and uses less RAM.




Link to post
Share on other sites
  • 2 weeks later...

That's a great and useful comparison.


Back when the dinosaurs roamed and I was developing server software I wasn't a big fan of C++ templates as they are a pain in the butt to debug. But it looks like I should revisit my preconceptions and give them another look for microcontrollers.


It's worth noting that code length will change in favor or C/C++ when there are multiple instances of IIC as separate code will be generated for each instance. Performance and RAM footprint will still be in favor of templates.


The RAM footprint for C/C++ can be reduced (probably equalized for C) by declaring icc using the const modifier. This keeps the configuration data in ROM/FLASH which is perfectly fine assuming all parameters are fixed and known at compile time.


Assuming only one instance of IIC one could also use #define or enum instead the T_IIC structure. This would get rid of all the relative addressing that really bloats the code and get us pretty close to the template code and performance at the price of encapsulation and/or code reusability.


Still, the template is much more elegant and easier to use! So a big thumbs up for sharing your research.



Link to post
Share on other sites
  • 5 months later...



Thanks for this information. It also has changed my own mind into at least considering using C++ in a project where it makes sense. Granted I am not very familiar with templates in general, and you seem to use them in a way for which many might not consider using templates. e.g. meta-templates vs the standard generics type templates ( or at least how I always viewed templates ).


Not that I have any doubt, but if what you say on this particular post is true, I feel that I would be foolish to not use C++ templates. 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...