Step-by-Step dsPIC33CH Programming Example

This demo illustrates the process involved in using MPLAB® 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's MPLAB® 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 data 0xAAAA 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 PIM Purchase Link >

MCC System Requirements

Tool About Installers
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 to Tools > Embedded > MPLAB Code Configurator v3: Open/Close.
  • Under Device Resources, select Peripherals > Slave core > SLAVE1. The SLAVE1 module automatically moves to the Project 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 to M->S.
    • Enable Protocol B.
DemoMaster2.png
  • In the Pin Manager: Grid View tab:
    • Select RE0 as GPIO output.
    • Assign ownership of RE1 to SLAVE1.
DemoMaster3.png
  • In Project Resources, select Pin 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
  • In Project Resources, select SLAVE1.
    • Click on Save Master Settings to save the SLAVE1 settings.
    • The saved configuration file can found in the Master project directory as master_config.mc3.
DemoMaster5.png
DemoMaster6.png
  • Check the Notifications [MCC] tab for any warnings.

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

  • Next, click on the Generate button in the Project Resources area. A confirmation window appears, indicating possible warnings. Continue to generate the code by clicking Yes.
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 the MCC icon in MPLAB X IDE or navigate to Tools > Embedded > MPLAB Code Configurator v3: Open/Close.
  • Under Project Resources, select Master Core.
  • Click Load Master Settings and navigate to the Master project directory location and select the master_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 the Pin Manager: Grid View tab:
    • Select RE1 as GPIO output.
DemoSlave4.png
  • In Project Resources, select Pin 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 the Notifications [MCC] tab for any warnings.

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

  • Next, in the Project Resources area, click on the Generate 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, select Slaves in the folder listing. Right-click and select Add Slave Project….
DemoInclusion1.png
  • Browse to the Slave project location and select the Slave.X image.
DemoInclusion2.png
  • In the Master project, select Slaves in the folders list. Right-click to change the properties. Select the Build checkbox.
DemoInclusion3.png

Application Code

Master Project

Edit the main.c file as shown in the example.

#include "mcc_generated_files/mcc.h"
 
#define DATA_UNDER_TEST 0xAAAA
 
int main(void)
{
    // initialize the device
    SYSTEM_Initialize();
 
    //Program and enable slave
    SLAVE1_Program();
    SLAVE1_Start();
 
    ProtocolA_DATA dataSend;
    ProtocolB_DATA dataReceive;
 
    dataSend.ProtocolA[0] = DATA_UNDER_TEST;
    dataReceive.ProtocolB[0] = 0;         //Initializing to known value.
 
    //Mailbox write
    SLAVE1_ProtocolAWrite((ProtocolA_DATA*)&dataSend);
 
    //Issue interrupt to slave
    SLAVE1_InterruptRequestGenerate();
    while(!SLAVE1_IsInterruptRequestAcknowledged());
    SLAVE1_InterruptRequestComplete();
    while(SLAVE1_IsInterruptRequestAcknowledged());
 
    //Wait for interrupt from slave
    while(!SLAVE1_IsInterruptRequested());
    SLAVE1_InterruptRequestAcknowledge();
    while(SLAVE1_IsInterruptRequested());
    SLAVE1_InterruptRequestAcknowledgeComplete();   
 
    //Mailbox read
    SLAVE1_ProtocolBRead((ProtocolB_DATA*)&dataReceive);
 
    //Glow LED on data match
    if(dataReceive.ProtocolB[0] == DATA_UNDER_TEST)
    {
        LED_MASTER_SetHigh();
    }
    else
    {
        LED_MASTER_SetLow();
    }
 
    while (1);
}

Slave Project

Edit the main.c file as shown in the example.

#include "mcc_generated_files/mcc.h"
 
#define DATA_UNDER_TEST 0xAAAA
 
int main(void)
{
    // initialize the device
    SYSTEM_Initialize();
 
    ProtocolA_DATA dataReceive;
    ProtocolB_DATA dataSend;
 
    dataReceive.ProtocolA[0] = 0;                 //Initializing to known value.
    dataSend.ProtocolB[0] = 0;                      //Initializing to known value.
 
    //Wait for interrupt from master    
    while(!MASTER_IsInterruptRequested());
    MASTER_InterruptRequestAcknowledge();
    while(MASTER_IsInterruptRequested());
    MASTER_InterruptRequestAcknowledgeComplete();
 
    //Mailbox read    
    MASTER_ProtocolARead((ProtocolA_DATA*)&dataReceive);
 
    //Copy the received data for retransmission
    dataSend.ProtocolB[0] = dataReceive.ProtocolA[0];
 
    //Mailbox write 
    MASTER_ProtocolBWrite((ProtocolB_DATA*)&dataSend);
 
    //Issue interrupt to master
    MASTER_InterruptRequestGenerate();
    while(!MASTER_IsInterruptRequestAcknowledged());
    MASTER_InterruptRequestComplete();
    while(MASTER_IsInterruptRequestAcknowledged());
 
    //Glow LED on data match
    if(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 transmits 0xAAAA 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
© 2019 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.