Jump to content

Try to develop a set of classes for the communication interfaces in C++


Follep
 Share

Recommended Posts

I'm starting to break into the PIC32 family, specifically the PIC32MZ series(http://www.kynix.com/Parts/3959097/PIC32MZ0512EFE064-E%2FMR.html), namely because I'm starting to want to do things that I want more horsepower for than an AVR can deliver. I've been reading a bunch of tutorial docs and source on the subject, but I figured I'd bring this question here.

What I'm trying to do is develop a set of classes for the communication interfaces in C++ such that they are unit-inspecific; that is, a generic class that is initialized with the number of the communication interface unit; essentially "one set of code for any number of interfaces". Let me give you an example.

We know from the datasheet that the PIC32MZ family has 6 4-wire SPI modules. This example, while it (presumably) works, it has a number of things that I consider undesirable for my uses:

  •     It's in C. I want to use classes to help with my data management, so preferably I'm looking for a C++ solution. Obviously, it'd take me all of ten minutes to rewrite this code, but see the next point.
  •     It's specific to SPI2. This means I'd need an individual class for each of the six SPI units, even if I only need one or two. What I'd like to develop is a unified class for SPI, such that I can call one class and declare the unit number (1 to 6) in the constructor and have it initialize the appropriate one. Basing off the above example, the constructor would look like so:
SPI::SPI (uint baud, uint u_num)
{
     // Some discrimination of what u_num is
     // Setup
}

 

Things I've considered, but thrown-out/decided against/unsure if feasible or how to do:

  •     If/else/switch statements in each method. This is the obviously easiest of all possible routes, but also the dumbest and the most sub-optimal. Every time the method calls, it would have to evaluate which unit to assign the parameters to which is wasted time.
  •     Function pointers from Microchip functions into arrays for calls. So, SPI1-6 functions such as configure, send byte, etc. would map into an array, when the class method calls it plugs the unit number (stored in a class variable) into the array and pumps the parameters in to the function referenced by the pointer table.
  •     Macros. Using the register macros provided by Microchip somehow to my advantage, although I have no idea (not afraid to say that one) how I would use that or to my advantage. Again, the gut feeling is it'd have to be some sort of pointer table.
  •     Raw addresses. I seem to remember that the datasheet gives the addresses for the registers in the memory space, although it's hard to draw a reference to them right now on demand. I believe they were virtual addresses - my main issue with this is that I don't have a clue how to leverage virtual memory from a high-level context (I've always understood that the OS and CPU manage virtual memory to physical address mapping, but how that translates up the totem pole I have no idea of), and so I wouldn't have any idea how to make the class work. Essentially, if I could figure it out (providing this was feasible), I could figure out the association (offset) between say, SPI1 registers and SPI2 registers and make the class methods auto-apply that offset depending on the unit class variable.

Thank you if any of you can help.

Link to comment
Share on other sites

  • Moderators

@Follep welcome to the forum. Just out of curiosity, what led you to post this on an AutoIt forum rather than one of the many forums that are dedicated to C++? You are certainly welcome to ask the question here; we created this subforum as a place for our members who code in other languages to chat, and so you may get someone who wanders by to take a look at your issue. But you will by no means get the number of eyes on it that you would by posting in the correct forum.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...