I using two MSP432 boards, each with one BoosterPack. I try to make communication between two MSP432 boards. My goal is to move two dots on each screen, and each joystick will control one dot. When I move one dot, it will also move on the other screen. My question is that I can not make communication between two boards.
Here is my code:
// Initial Circle Parameters
#define CIRCLE_X_COORD 64 // MAX = 128
#define CIRCLE_Y_COORD 64 // MAX = 128
#define CIRCLE_RADIUS 2
#define Mode 0 // 0 is Master; 1 is Slave
#include "msp.h"
#include <driverlib.h>
#include <grlib.h>
#include "Crystalfontz128x128_ST7735.h"
#include <stdio.h>
#include <stdint.h>
/* Graphic library context */
Graphics_Context g_sContext;
/* ADC results buffer */
static uint16_t resultsBuffer[2];
void map_ports();
void init_display();
void init_clock();
void init_interrupts();
void init_master();
void init_slave();
// Set up the circle
int32_t coord_x = CIRCLE_X_COORD;
int32_t coord_y = CIRCLE_Y_COORD;
int32_t circ_r = CIRCLE_RADIUS;
char clear_timer = 0;
/* Statics */
static volatile uint8_t inputData = 0;
static uint8_t outputData = 0;
char state = 0;
int countDraw = 0;
uint8_t getData[2] = {10,10};
/*
* Main function
*/
void main(void)
{
map_ports();
init_clock();
init_display();
init_interrupts();
// ?????? while (!(SPI_getInterruptStatus(EUSCI_A2_BASE,EUSCI_A_SPI_TRANSMIT_INTERRUPT)));
//SPI_transmitData(EUSCI_A2_BASE, outputData);
SPI_transmitData(EUSCI_A2_BASE,0x32);
while(1)
{
MAP_PCM_gotoLPM0();
__no_operation();
}
}
/*****************************
* Initializations
*****************************
*/
/* Port mapper configuration register */
const uint8_t port_mapping[] =
{
//Port P3:
PM_UCA2CLK, PM_NONE, PM_UCA2SOMI, PM_UCA2SIMO,PM_NONE,
PM_NONE, PM_NONE,PM_NONE
};
/* SPI Master Configuration Parameter */
const eUSCI_SPI_MasterConfig spiMasterConfig =
{
EUSCI_A_SPI_CLOCKSOURCE_ACLK, // ACLK Clock Source
3000000, // ACLK = DCO = 3MHZ
10000, // SPICLK = 500khz
EUSCI_A_SPI_MSB_FIRST, // MSB First
EUSCI_A_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT, // Phase
EUSCI_A_SPI_CLOCKPOLARITY_INACTIVITY_HIGH, // High polarity
EUSCI_A_SPI_3PIN // 3Wire SPI Mode
};
/* SPI Slave Configuration Parameter */
const eUSCI_SPI_SlaveConfig spiSlaveConfig =
{
EUSCI_A_SPI_MSB_FIRST, // MSB First
EUSCI_A_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT, // Phase
EUSCI_A_SPI_CLOCKPOLARITY_INACTIVITY_HIGH, // Normal Polarity
EUSCI_A_SPI_3PIN // 3wire mode
};
void map_ports(void)
{
MAP_PMAP_configurePorts((const uint8_t *) port_mapping, P3MAP, 1,
PMAP_DISABLE_RECONFIGURATION);
}
void init_clock(void)
{
/* Halting WDT and disabling master interrupts */
MAP_WDT_A_holdTimer();
MAP_Interrupt_disableMaster();
/* Set the core voltage level to VCORE1 */
MAP_PCM_setCoreVoltageLevel(PCM_VCORE1);
/* Set 2 flash wait states for Flash bank 0 and 1*/
MAP_FlashCtl_setWaitState(FLASH_BANK0, 2);
MAP_FlashCtl_setWaitState(FLASH_BANK1, 2);
/* Initializes Clock System */
MAP_CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_48);
MAP_CS_initClockSignal(CS_MCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1 );
MAP_CS_initClockSignal(CS_HSMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1 );
MAP_CS_initClockSignal(CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1 );
MAP_CS_initClockSignal(CS_ACLK, CS_REFOCLK_SELECT, CS_CLOCK_DIVIDER_1);
}
void init_display(void)
{
/* Initializes display */
Crystalfontz128x128_Init();
/* Set default screen orientation */
Crystalfontz128x128_SetOrientation(LCD_ORIENTATION_UP);
/* Initializes graphics context */
Graphics_initContext(&g_sContext, &g_sCrystalfontz128x128);
Graphics_setForegroundColor(&g_sContext, GRAPHICS_COLOR_RED);
Graphics_setBackgroundColor(&g_sContext, GRAPHICS_COLOR_WHITE);
GrContextFontSet(&g_sContext, &g_sFontFixed6x8);
Graphics_clearDisplay(&g_sContext);
}
void init_interrupts(void)
{
/* Configures Pin 6.0 and 4.4 as ADC input */
MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P6, GPIO_PIN0, GPIO_TERTIARY_MODULE_FUNCTION);
MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P4, GPIO_PIN4, GPIO_TERTIARY_MODULE_FUNCTION);
/* Initializing ADC (ADCOSC/64/8) */
MAP_ADC14_enableModule();
MAP_ADC14_initModule(ADC_CLOCKSOURCE_ADCOSC, ADC_PREDIVIDER_64, ADC_DIVIDER_8, 0);
/* Configuring ADC Memory (ADC_MEM0 - ADC_MEM1 (A15, A9) with repeat)
* with internal 2.5v reference */
MAP_ADC14_configureMultiSequenceMode(ADC_MEM0, ADC_MEM1, true);
MAP_ADC14_configureConversionMemory(ADC_MEM0,
ADC_VREFPOS_AVCC_VREFNEG_VSS,
ADC_INPUT_A15, ADC_NONDIFFERENTIAL_INPUTS);
MAP_ADC14_configureConversionMemory(ADC_MEM1,
ADC_VREFPOS_AVCC_VREFNEG_VSS,
ADC_INPUT_A9, ADC_NONDIFFERENTIAL_INPUTS);
/* Enabling the interrupt when a conversion on channel 1 (end of sequence)
* is complete and enabling conversions */
MAP_ADC14_enableInterrupt(ADC_INT1);
/* Enabling Interrupts */
MAP_Interrupt_enableInterrupt(INT_ADC14);
MAP_Interrupt_enableMaster();
/* Setting up the sample timer to automatically step through the sequence
* convert.
*/
MAP_ADC14_enableSampleTimer(ADC_AUTOMATIC_ITERATION);
/* Triggering the start of the sample */
MAP_ADC14_enableConversion();
MAP_ADC14_toggleConversionTrigger();
if (Mode == 0)
{
init_master();
}
else
{
init_slave();
}
}
void init_master(void)
{
/* Selecting P1.5 P1.6 and P1.7 in SPI mode */
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P3, GPIO_PIN2, GPIO_PRIMARY_MODULE_FUNCTION); //MISO 3.2
GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P3, GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION); //MOSI 3.3
GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P3, GPIO_PIN0, GPIO_PRIMARY_MODULE_FUNCTION); //ACLK 3.0
/* Configuring SPI in 3wire master mode */
SPI_initMaster(EUSCI_A2_BASE, &spiMasterConfig);
/* Enable SPI module */
SPI_enableModule(EUSCI_A2_BASE);
/* Enabling interrupts */
SPI_enableInterrupt(EUSCI_A2_BASE, EUSCI_A_SPI_RECEIVE_INTERRUPT);
Interrupt_enableInterrupt(INT_EUSCIA2);
Interrupt_enableSleepOnIsrExit();
}
void init_slave(void)
{
/* Selecting P1.5 P1.6 and P1.7 in SPI mode */
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P3, GPIO_PIN0, GPIO_PRIMARY_MODULE_FUNCTION); //ACLK 3.0
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P3, GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION); //MOSI 3.3
GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P3, GPIO_PIN2, GPIO_PRIMARY_MODULE_FUNCTION); //MISO 3.2
/* Configuring SPI in 3wire master mode */
SPI_initSlave(EUSCI_A2_BASE, &spiSlaveConfig);
/* Enable SPI module */
SPI_enableModule(EUSCI_A2_BASE);
/* Enabling interrupts */
SPI_enableInterrupt(EUSCI_A2_BASE, EUSCI_A_SPI_RECEIVE_INTERRUPT);
Interrupt_enableInterrupt(INT_EUSCIA2);
Interrupt_enableSleepOnIsrExit();
}
/* ***************
*
* SPI Handler
*
*****************
*/
void EUSCIA2_IRQHandler(void)
{
uint32_t status = SPI_getEnabledInterruptStatus(EUSCI_A2_BASE);
SPI_clearInterruptFlag(EUSCI_A2_BASE, status);
if(status & EUSCI_A_SPI_RECEIVE_INTERRUPT)
{
int jj = 0;
// USCI_B0 TX buffer ready?
while (!(SPI_getInterruptStatus(EUSCI_A2_BASE, EUSCI_A_SPI_TRANSMIT_INTERRUPT)));
inputData = SPI_receiveData(EUSCI_A2_BASE);
if(Mode == 0)
{
switch(state)
{
case 0:
getData[0] = inputData;
outputData = coord_x;
state++;
break;
case 1:
getData[1] = inputData;
outputData = coord_y;
state = 0;
break;
default:
break;
}
}
else if(Mode == 1)
{
switch(state)
{
case 0:
getData[1] = inputData;
outputData = coord_x;
state++;
break;
case 1:
getData[0] = inputData;
outputData = coord_y;
state = 0;
break;
default:
break;
}
}
// Send the next data packet
SPI_transmitData(EUSCI_A2_BASE, outputData);
// Delay between transmissions for slave to process information
if(Mode == 0)
for(jj=50;jj<50;jj++);
}
}
/*****************************
* Interrupts
*****************************
*/
/* This interrupt is fired whenever a conversion is completed and placed in
* ADC_MEM1. This signals the end of conversion and the results array is
* grabbed and placed in resultsBuffer */
void ADC14_IRQHandler(void)
{
if (++clear_timer == 0)
{
Graphics_clearDisplay(&g_sContext);
clear_timer = 0;
}
uint64_t status;
status = MAP_ADC14_getEnabledInterruptStatus();
MAP_ADC14_clearInterruptFlag(status);
/* ADC_MEM1 conversion completed */
if(status & ADC_INT1)
{
/* Store ADC14 conversion results */
resultsBuffer[0] = ADC14_getResult(ADC_MEM0);
resultsBuffer[1] = ADC14_getResult(ADC_MEM1);
if (resultsBuffer[0] > 11000 && coord_x < 125) coord_x++;
else if (resultsBuffer[0] < 5000 && coord_x > CIRCLE_RADIUS) coord_x--;
else coord_x = coord_x;
if (resultsBuffer[1] > 11000 && coord_y > CIRCLE_RADIUS) coord_y--;
else if (resultsBuffer[1] < 5000 && coord_y < 125) coord_y++;
else coord_y = coord_y;
Graphics_fillCircle(&g_sContext,
coord_x,
coord_y,
circ_r);
Graphics_fillCircle(&g_sContext,
getData[0],
getData[1],
circ_r+1);
/*
int buttonPressed = 0;
if (!(P4IN & GPIO_PIN1))
buttonPressed = 1;
if (buttonPressed && circ_r < CIRCLE_RADIUS + 4) circ_r++;
else if (!buttonPressed && circ_r > CIRCLE_RADIUS) circ_r--;*/
}
}
Thank you.