Step-by-Step dsPIC33CH Programming Example in MPLAB® Code Configurator (MCC) Classic

Last modified by Microchip on 2024/04/10 14:50

This demo illustrates the process involved in using MPLAB® Code Configurator (MCC) Classic version to configure the system, Master Slave Interface (MSI) module, input/output (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 the 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.

Block diagram showing how Master and Slave cores work together


Reference Materials

Hardware

Software

Back to Top

Configuring the Projects with MCC Classic

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".

Back to Top


To launch MCC, click on the MCC icon in MPLAB X IDE or navigate to Tools > Embedded > MPLAB Code Configurator v3: Open/Close.

Back to Top


Under Device Resources, select Peripherals > Slave core > SLAVE1. The SLAVE1 module automatically moves to the Project Resources window.

Back to Top


Switch to the SLAVE1 module GUI.

Back to Top


Enter the Slave project name as "Slave".

Slave Project name field in the Easy Setup tab

Back to Top


In the MSI settings:

  • Enable Protocol A and change the direction to M->S.
  • Enable Protocol B.

Setting MSI protocols for Master and Slave interfaces under Mailbox Configuration

Back to Top


In the Pin Manager: Grid View tab:

  • Select RE0 as GPIO output.
  • Assign ownership of RE1 to SLAVE1.

Pin Manager: Grid View tab

Back to Top


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".

Easy Setup tab

Back to Top


In Project Resources, select SLAVE1.

  • Click on Save Master Settings to save the SLAVE1 settings.
  • The saved configuration file can be found in the Master project directory as master_config.mc3.

Project Resources pane

Output window showing that saving the configuration was successful

  • Check the Notifications [MCC] tab for any warnings.

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

Back to Top


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.

MCC Confirmation window

The MCC configuration is now complete. The generated files are now added to the project you created.

Master Project files shown after MCC generation

Back to Top

Configuring the Slave Project

Create an MPLAB X IDE project with the dsPIC33CH128MP508S1 device and name the project "Slave".

Back to Top


To launch MCC, click the MCC icon in MPLAB X IDE or navigate to Tools > Embedded > MPLAB Code Configurator v3: Open/Close.

Back to Top


Under Project Resources, select Master Core.

Back to Top


Click Load Master Settings, 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.

Load Master Settings window

Back to Top


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.

Pin conflict warning message

  • The MSI settings configured in the Master project now get reflected in the Slave project.

MSI Mailbox Configuration

Back to Top


In the Pin Manager: Grid View tab:

  • Select RE1 as GPIO output.

Selecting pin RE0 as GPIO output in Pin Manager: Grid View tab

Back to Top


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".

Pin Module pane

Back to Top


Check the Notifications [MCC] tab for any warnings. 

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

Back to Top


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.

Image showing all the generated Slave project files

Back to Top

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….

Projects pane

Back to Top


Browse to the Slave project location and select the Slave.X image.

Add Slave Project window

Back to Top


In the Master project, select Slaves in the folders list. Right-click to change the properties. Select the Build checkbox.

Project Properties - Master window

Back to Top

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);
}

Back to Top

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);
}

Back to Top

LEDs Display for Received Messages

Select the Master project as the main project.

Back to Top


Make and program the project.

Back to Top

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.

Image showing the D3 and D4 LEDs on the Explorer 16/32 Demo Board lit up

Explanation of LED functions in the demo project to go with Explorer 16/32 Pic

Back to Top