How to Change the SPI mode (phase, polarity) Using the M4 and FreeRTOS

I can not stop the ECSPI channel, once initialized. I have three 16-bit SPI devices which operate at

  • 10MHz Mode 2

    .baudRate = 10000000,
    .clockPhase = ecspiClockPhaseFirstEdge,
    .clockPolarity = ecspiClockPolarityActiveLow,

  • 6MHz Mode 0, and

    .baudRate = 6000000,
    .clockPhase = ecspiClockPhaseFirstEdge,
    .clockPolarity = ecspiClockPolarityActiveHigh,

  • 2MHz Mode 0

    .baudRate = 2000000,
    .clockPhase = ecspiClockPhaseFirstEdge,
    .clockPolarity = ecspiClockPolarityActiveHigh,
    If I initialize the M4 for the 10MHz device, I can not switch the two mode bits by calling ECSPI_Init() or ECSPI_Disable() then ECSPI_Init().

ECSPI_SetBaudRate() does change the baudrate.

Vise Versa: If the M4 is initialized for the 6MHz device. I can switch the baudrate and talk to the 2MHz device, but not the 10MHz device.

I added a function ECSPI_SetSpiMode() to change the SPI mode after ECSPI_Init() was called.

To make the function available to your application, please extend ecspi.c/.h as follows:

ecspi.h

/*FUNCTION****************************************************************
 *
 * Function Name : ECSPI_SetSpiMode
 * Description   : Change the SPI mode
 *
 *END********************************************************************/
/*!
 * @brief Change the SPI mode
 *
 * @param base       eCSPI base pointer.
 * @param initConfig eCSPI initialization structure. The following fields are used:
 *                   channelSelect, clockPhase, clockPolarity
 */
void ECSPI_SetSpiMode(ECSPI_Type* base, const ecspi_init_config_t* initConfig);

ecspi.c

/*FUNCTION****************************************************************
 *
 * Function Name : ECSPI_SetSpiMode
 * Description   : Change the SPI mode
 *
 *END********************************************************************/
void ECSPI_SetSpiMode(ECSPI_Type* base, const ecspi_init_config_t* initConfig)
{
    volatile uint32_t configreg;

    /* eCSPI CONFIGREG Configuration */
    configreg = ECSPI_CONFIGREG_REG(base);
    configreg = ECSPI_CONFIGREG_REG(base) &
                    ~((1 << ECSPI_CONFIGREG_SCLK_PHA_SHIFT | 
                       1 << ECSPI_CONFIGREG_SCLK_POL_SHIFT) << initConfig->channelSelect);
    configreg |= ECSPI_CONFIGREG_SCLK_PHA(((initConfig->clockPhase) & 1) << (initConfig->channelSelect)) |
                 ECSPI_CONFIGREG_SCLK_POL(((initConfig->clockPolarity) & 1) << (initConfig->channelSelect));
    ECSPI_CONFIGREG_REG(base) = configreg;
}

Regards, Andy