Hi,
I am currently working on a project in which I make two microcontrollers communicate through the spi. In this project I have to send a state from the first microcontroller to the second one in order to control it and keep it in the same state.
In summary I send either state 1 or state 0 by spi.
I noticed during tests that the state read by the slave is sometimes shifted and there is no way to resynchronize the reading without doing a reset, which makes me think that the problem comes from the slave.
So I send you my code here hoping that you can help me (not in order).
//
// spi_slave_init - Configure the SPI for the lead board
//
void spi_slave_init(void)
{
SPI_disableModule(SPIA_BASE); // Disable SPI A to configure and reset
SPI_reset(SPIA_BASE);
SPI_setConfig(SPIA_BASE, DEVICE_LSPCLK_FREQ, // Configure SPI A as SLAVE with 16 bits transfer at 500kHz
SPI_PROT_POL0PHA0,
SPI_MODE_SLAVE, 500000, 16);
SPI_disableLoopback(SPIA_BASE);
SPI_setEmulationMode(SPIA_BASE, SPI_EMULATION_FREE_RUN);
//
// FIFO and interrupt configuration
//
SPI_enableFIFO(SPIA_BASE);
SPI_clearInterruptStatus(SPIA_BASE, SPI_INT_RXFF);
SPI_setFIFOInterruptLevel(SPIA_BASE, SPI_FIFO_TX1, SPI_FIFO_RX1);
SPI_enableInterrupt(SPIA_BASE, SPI_INT_RXFF);
SPI_enableModule(SPIA_BASE); // Enable SPI A to start communication
}
//
// spi_slave_gpio_init - Configure the SPI GPIO for the slave
//
void spi_slave_gpio_init(void)
{
EALLOW;
GPIO_setMasterCore(SPIA_CLK, GPIO_CORE_CPU1);
GPIO_setMasterCore(SPIA_STE, GPIO_CORE_CPU1);
GPIO_setMasterCore(SPIA_SIMO, GPIO_CORE_CPU1);
GPIO_setMasterCore(SPIA_SOMI, GPIO_CORE_CPU1);
GPIO_setDirectionMode(SPIA_CLK, GPIO_DIR_MODE_IN); // Configure SCLK as input
GPIO_setDirectionMode(SPIA_STE, GPIO_DIR_MODE_IN); // Configure Slave Select as input
GPIO_setDirectionMode(SPIA_SIMO, GPIO_DIR_MODE_IN); // Configure SPI Slave Input Master Output as input
GPIO_setDirectionMode(SPIA_SOMI, GPIO_DIR_MODE_OUT); // Configure SPI Slave output Master Input as output
GPIO_setPinConfig(GPIO_56_SPIA_CLK); // Set the pin 56 as SCLK
GPIO_setPinConfig(GPIO_57_SPIA_STE); // Set the pin 57 as Slave Select
GPIO_setPinConfig(GPIO_16_SPIA_SIMO); // Set the pin 16 as SIMO
GPIO_setPinConfig(GPIO_17_SPIA_SOMI); // Set the pin 17 as SOMI
GPIO_setQualificationMode(SPIA_CLK,GPIO_QUAL_SYNC); // No synchronization
GPIO_setQualificationMode(SPIA_STE,GPIO_QUAL_SYNC); // No synchronization
GPIO_setQualificationMode(SPIA_SIMO,GPIO_QUAL_SYNC); // No synchronization
GPIO_setQualificationMode(SPIA_SOMI,GPIO_QUAL_SYNC); // No synchronization
EDIS;
}
void spi_data_received(void)
{
static uint16_t oldSpiDataReceived = 0;
spiDataReceived = SPI_readDataBlockingFIFO(SPIA_BASE); //SPI_receive16Bits(SPIA_BASE, SPI_DATA_BIG_ENDIAN, 0, 0);
if((spiDataReceived == 0)&&(spiDataReceived != oldSpiDataReceived))
{
BUCK_HBridgePwrOnOff = 0;
BUCK_HBridge24vOnOff = 0;
BUCK_Hbridge24vOffState();
BUCK_HbridgePwrOffState();
oldSpiDataReceived = spiDataReceived;
}
else if((spiDataReceived == 1)&&(spiDataReceived != oldSpiDataReceived))
{
//
// Initialize global variables used in solution
//
BUCK_initUserVariables();
BUCK_initProgramVariables();
BUCK_HBridgePwrEnState = HbPwrEnState_Init;
oldSpiDataReceived = spiDataReceived;
}
}
__interrupt void INT_mySPIA_RX_ISR(void)
{
spi_data_received();
SPI_resetRxFIFO(SPIA_BASE);
Interrupt_clearACKGroup(INTERRUPT_ACK_GROUP6);
SPI_clearInterruptStatus(SPIA_BASE, SPI_INT_RXFF);
}
Here is an example of what I can read on the slave when the data is shifted.
I am sending '1' through SPI and i read '32768'.
Thank you in advance for your answer !