Converting code from Arduino to PIC

Hi guys, for the past 2 years we have been developing an arduino based product that gives multiple outputs via MOSFETs, reads inputs, monitors voltages, and currents loads of different communication options ranging from CAN to bluetooth, remote monitoring. It does a hell of a lot. After literally years spent learning to program Arduinos and developing this we have a finished product. Its beautiful. So obviously the powers than be have decided that we need to change from the atmega2560 to a PIC32MK1024GPD100 for reliability, cost and ease of production. We have MPLAB IDE installed and after several days looking at it we still cant figure it out. At this rate changing to PIC could set us back years. We dont realy want to sub the work out as we will loose the ability to make our own modifications to the product so to keep control and understanding would like to keep this in house. Im hoping that somewhere out there someone has an easy way to convert our Arduino code to work on a PIC. Does any one have any ideas that dont result in us going back to square one? Thanks

I doubt you'll find some magic converter utility. You probably just need to start working through the code line by line, including any libraries you used. I suspect you'll find that a lot of your code can be used as-is or with minor modifications until you get down to the architecture specific code.

After a quick search I found two different hardware packages that add support for PIC32 to the Arduino IDE: https://github.com/OLIMEX/Arduino_configurations/tree/master/PIC https://github.com/chipKIT32/chipKIT-core/tree/master/pic32 There might be others.

I realize you don't want to use the Arduino IDE anymore and these don't support your specific chip anyway but maybe that core code could serve as a reference for how you can implement the Arduino API functions you need to convert your firmware without starting over from scratch.

The Arduino IDE uses C++ . Briefly looking at the MPLAB IDE I have seen mention of 'C' compilers but not C++. If you have a large amount of code already written then either you find a C++ compiler for the PIC or you will have to rewrite your code into C ( this could be a big or small task depending on whether yourcode really makes use of C++ features or not).

I guess your existing code also uses libraries. Those libraries may not be available to you, so you will either have to develop compatible libraries or rewrite your code to use alternatives available on the PIC.

Is your product already being sold? It seems strange to make such a big change so late in the game.

I thought of that too but then saw the Wikipedia article for MPLAB said it does support C++ for PIC32.

pert: I thought of that too but then saw the Wikipedia article for MPLAB said it does support C++ for PIC32.

You are right I found a C++ compiler here; http://microchipdeveloper.com/xc32pp:start That will certainly help.

I suspect one of the problems will be that the code will have been developed specifically to run on Arduinos and without any thought of changing platforms. In other words access to the Arduino structure is probably peppered throughout the code rather than trying to isolate it in specific functions.

Some metrics on the number of lines of code, number of functions, amount of memory and I/O used etc. might give a bit of a handle on the scale of the problem.

The typical Arduino code is pretty high level. I can use the same sketch on an AVR, ESP8266, SAMD, etc. because the standard Arduino API remains mostly consistent even if the implementation of that API changes from platform to platform. For most Arduino users that happens without even intentionally trying to write portable code. So my idea is that the only code that will need to be rewritten is the architecture-specific parts of the API functions and some code from libraries.

Of course some people do go low level in their sketches, maybe because they need better performance, and that code will require a lot more work. So yes, without more information it's difficult to give detailed advice.

Why don't you use one of the Arduino supported controllers, and design your own PCB around it?

DrDiettrich: Why don't you use one of the Arduino supported controllers, and design your own PCB around it?

I agree, design your own PCB using a atmega2560 or similar I would not use a PIC32 - the harmony software is complex and has a very steep learning curve - on several projects end users requested a PIC32 (e.g. a modern uptodate 32bit processor!) - in the end the PIC32s were replaced with PIC24s we have just developed a prototype system using an Arduino MKRFOX1200 the final PCB will use a PIC24 - in the main because we have designed numerous PCBs with PIC24s and see no major problem porting the code

The chipkit stuff is pretty far along to being fully Arduino compatible for some pic32 chips. The latest version can be added with the board manager. I don’t know whether your particular chip is among those supported.

Your “powers that be” need their heads examined, though. No quick technical fix can overcome bad management in the long term.

Ps: in the end, pic32 uses gcc for its compiler suite, just like the avrs. So if mplab is the big stumbling block, there should be alternatives.