Step-by-Step dsPIC33CH Programming Example in MPLAB® Code Configurator (MCC) Classic
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.
Reference Materials
Hardware
Software
- MPLAB X IDE v4.20 or newer
- MPLAB XC16 compiler v1.35 or newer
- MCC Plugin v3.55.1 or newer
- MCC PIC24/dsPIC33/PIC32MM MCUs Library v1.65 or newer
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
Enter the Slave project name as "Slave".
In the MSI settings:
- Enable Protocol A and change the direction to M->S.
- Enable Protocol B.
In the Pin Manager: Grid View tab:
- Select RE0 as GPIO output.
- Assign ownership of RE1 to SLAVE1.
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".
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.
- Check the Notifications [MCC] tab for any warnings.
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.
The MCC configuration is now complete. The generated files are now added to the project you created.
Configuring the Slave Project
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.
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.
- The MSI settings configured in the Master project now get reflected in the Slave project.
In the Pin Manager: Grid View tab:
- Select RE1 as GPIO output.
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".
Check the Notifications [MCC] tab for any warnings.
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.
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….
Browse to the Slave project location and select the Slave.X image.
In the Master project, select Slaves in the folders list. Right-click to change the properties. Select the Build checkbox.
Application Code
Master Project
Edit the main.c file as shown in the example.
#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.
#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
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.