Step-by-Step dsPIC33CH Programming Example

This demo illustrates the process involved in usingMPLAB® Code Configurator (MCC) to configure the System, MSI module, I/O ports ownership in a Master project, and the Slave project of a dual-core device.

Objective

  • Start MCC in the Master core'sMPLAB® X IDE project for the dsPIC33CH128MP508 device.
  • Set up the configuration for MSI, I/O pin ownership in the Master project and export the settings.
    • Configure the MSI mailbox to transfer one word of data from the Master core to the Slave core.
    • Configure the MSI mailbox to receive one word of data from the Slave core to the Master core.
    • Assign an output pin ownership to the Slave core.
  • Start MCC in the Slave Project for dsPIC33CH128MP508S1.
  • Import the settings from the Master project into the Slave project.
  • Include Slave project in a Master project.
  • Generate code to:
    • Transfer data0xAAAA from the Master core to the Slave core using MSI.
    • Retransmit the received data at the Slave core to the Master core.
    • Flash an LED on data match at the Master core.
    • Flash an LED on valid data reception at the Slave core.
Demo.png

Hardware Requirements

Tool About Purchase
Explorer1632-50px.png
Explorer 16/32
Development Board
dsPIC33CH128MP508 PIMPurchase Link >

MCC System Requirements

Tool AboutInstallers
Installation
Instructions
Windows Linux Mac OSX
MPLAB® X
Integrated Development Environment
MPLAB® XC16
C Compiler
swtool-28px.png
MPLAB® Code Configurator
Dynamic Code Generation
  • MPLAB X IDE v4.20 or newer
  • XC16 compiler v1.35 or newer
  • MPLAB Code Configurator (MCC) Plugin v3.55.1 or newer
  • MPLAB Code Configurator (MCC) PIC24/dsPIC33/PIC32MM MCUs Library v1.65 or newer

Configuring MCC

The following sections provide procedures to configure the Master project and the Slave project using MCC.

Configuring the Master project

  • Create an MPLAB X IDE project with the dsPIC33CH128MP508 device and name the project "Master".
  • To launch MCC, click on the MCC icon in MPLAB X IDE or navigate toTools > Embedded > MPLAB Code Configurator v3: Open/Close.
  • UnderDevice Resources, selectPeripherals > Slave core > SLAVE1. The SLAVE1 module automatically moves to theProject Resources window.
  • Switch to the SLAVE1 module GUI.
  • Enter the Slave project name as "Slave".
DemoMaster1.png
  • In the MSI settings:
    • Enable Protocol A and change the direction toM->S.
    • Enable Protocol B.
DemoMaster2.png
  • In thePin Manager: Grid View tab:
    • SelectRE0 as GPIO output.
    • Assign ownership ofRE1 to SLAVE1.
DemoMaster3.png
  • InProject Resources, selectPin Module.
    • Ensure that the pins are configured as explained in the previous step.
    • Enter a custom name to pin RE0 as "LED_MASTER".
DemoMaster4.png
  • InProject Resources, selectSLAVE1.
    • Click onSave Master Settings to save the SLAVE1 settings.
    • The saved configuration file can found in the Master project directory asmaster_config.mc3.
DemoMaster5.png
DemoMaster6.png
  • Check theNotifications [MCC] tab for any warnings.

You should always resolve "severe" type notifications on their respective modules.

  • Next, click on theGenerate button in theProject Resources area. A confirmation window appears, indicating possible warnings. Continue to generate the code by clickingYes.
DemoMaster7.png
  • The MCC configuration is now complete. The generated files are now added to the project you created.
DemoMaster8.png

Configuring the Slave project

  • Create an MPLAB X IDE project with the dsPIC33CH128MP508S1 device and name the project "Slave".
  • To launch MCC, click on theMCC icon in MPLAB X IDE or navigate toTools > Embedded > MPLAB Code Configurator v3: Open/Close.
  • UnderProject Resources, selectMaster Core.
  • ClickLoad Master Settings and navigate to the Master project directory location and select themaster_config.mc3 file. Now the settings of SLAVE1 (Slave core) configured in the Master project are imported.
DemoSlave1.png
  • After importing, a pop-up window shows a conflict for pin RB1. The conflict arises since pin RB1 was assigned for CLKO functionality by default in the Slave project and the same pin is also assigned for CLKO in the Master project. Accept the override to retain the Master project settings.
DemoSlave2.png
  • The MSI settings configured in the Master project now get reflected in the Slave project.
DemoSlave3.png
  • In thePin Manager: Grid View tab:
    • SelectRE1 as GPIO output.
DemoSlave4.png
  • InProject Resources, selectPin Module.
    • Ensure that the pins are configured as explained in the previous step.
    • Enter the custom name for pin RE0 as "LED_SLAVE".
DemoSlave5.png
  • Check theNotifications [MCC] tab for any warnings.

You should always resolve "severe" type notifications on their respective modules.

  • Next, in theProject Resources area, click on theGenerate button.
  • The MCC configuration is now complete. The generated files are now added to the project you created.
DemoSlave6.png

Including the Slave project in the Master project

  • In the Master project, selectSlaves in the folder listing. Right-click and selectAdd Slave Project….
DemoInclusion1.png
  • Browse to the Slave project location and select theSlave.X image.
DemoInclusion2.png
  • In the Master project, selectSlaves in the folders list. Right-click to change the properties. Select theBuild checkbox.
DemoInclusion3.png

Application Code

Master Project

Edit themain.c file as shown in the example.

#include"mcc_generated_files/mcc.h"#defineDATA_UNDER_TEST0xAAAAintmain(void){// initialize the deviceSYSTEM_Initialize();//Program and enable slaveSLAVE1_Program();SLAVE1_Start();ProtocolA_DATAdataSend;ProtocolB_DATAdataReceive;dataSend.ProtocolA[0] =DATA_UNDER_TEST;dataReceive.ProtocolB[0] =0;//Initializing to known value.//Mailbox writeSLAVE1_ProtocolAWrite((ProtocolA_DATA*)&dataSend);//Issue interrupt to slaveSLAVE1_InterruptRequestGenerate();while(!SLAVE1_IsInterruptRequestAcknowledged());SLAVE1_InterruptRequestComplete();while(SLAVE1_IsInterruptRequestAcknowledged());//Wait for interrupt from slavewhile(!SLAVE1_IsInterruptRequested());SLAVE1_InterruptRequestAcknowledge();while(SLAVE1_IsInterruptRequested());SLAVE1_InterruptRequestAcknowledgeComplete();//Mailbox readSLAVE1_ProtocolBRead((ProtocolB_DATA*)&dataReceive);//Glow LED on data matchif(dataReceive.ProtocolB[0] ==DATA_UNDER_TEST){LED_MASTER_SetHigh();}else{LED_MASTER_SetLow();}while(1);}

Slave Project

Edit themain.c file as shown in the example.

#include"mcc_generated_files/mcc.h"#defineDATA_UNDER_TEST0xAAAAintmain(void){// initialize the deviceSYSTEM_Initialize();ProtocolA_DATAdataReceive;ProtocolB_DATAdataSend;dataReceive.ProtocolA[0] =0;//Initializing to known value.dataSend.ProtocolB[0] =0;//Initializing to known value.//Wait for interrupt from masterwhile(!MASTER_IsInterruptRequested());MASTER_InterruptRequestAcknowledge();while(MASTER_IsInterruptRequested());MASTER_InterruptRequestAcknowledgeComplete();//Mailbox readMASTER_ProtocolARead((ProtocolA_DATA*)&dataReceive);//Copy the received data for retransmissiondataSend.ProtocolB[0] =dataReceive.ProtocolA[0];//Mailbox writeMASTER_ProtocolBWrite((ProtocolB_DATA*)&dataSend);//Issue interrupt to masterMASTER_InterruptRequestGenerate();while(!MASTER_IsInterruptRequestAcknowledged());MASTER_InterruptRequestComplete();while(MASTER_IsInterruptRequestAcknowledged());//Glow LED on data matchif(dataReceive.ProtocolA[0] ==DATA_UNDER_TEST){LED_SLAVE_SetHigh();}else{LED_SLAVE_SetLow();}while(1);}

LEDs Display for Received Messages

  • Select the Master project as the main project.
  • Make and program the project.

The Master core is configured to transmit a word of data to the Slave core and the Slave core is configured to re-transmit to the Master core.

In case of a Power-On-Reset:

  • The Master core transmits0xAAAA and issues an interrupt to the Slave core. The Slave core acknowledges the interrupt, re-transmits the received data and issues an interrupt back to the Master core.
  • The Master core acknowledges the interrupt, receives the data, and verifies it.
  • When the transmitted data and received data match, the Master core flashes an LED (D3) connected to pin RE0 of the device.
  • Similarly, the received data at the Slave core is also compared and verified. The Slave core flashes an LED (D4) connected to pin RE1 of the device to acknowledge a successful reception.
DemoResult1.png
DemoResult2.png