fsl_spi set baud rate

cancel
Showing results for 
Search instead for 
Did you mean: 

fsl_spi set baud rate

Jump to solution
638 Views
mlyons
Contributor I

Hi, I'm trying to set a new baud rate for my fsl_spi component and I'm running into trouble.  I'm using PEx which allows you to create a list of baud rates but I don't know how to move to the higher speed.  I first initialize my SD card at 375kHz but then need to increase the clock speed for faster data transfer.  I'm running PEx, KSDK 1.2 and frdm-kl25z.


Thanks,
Michael

Labels (1)
1 Solution
397 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Michael,

when the SPI of Kl25 is configured as master mode, the SPI clock signal SPSCK is an output clock, it's output frequency is called baudrate:

BaudRateDivisor = (SPPR + 1) × [2**(SPR + 1)]

BaudRate = BusClock / BaudRateDivisor

For detailed information, pls refer to section 37.4.6 SPI Baud Rate Generation in the KL25P80M48SF0RM.pdf.

For the SPI0 module of Kl25, the SPI0 uses BusClock as it's driving clock, the BusClock is 24MHz, when the SPPR=0 and SPR=0, the SPSCK get the maximum baudrate, which is 24M/((0+1)*2)=12MHz, that is why the PE gives you a baudrate list, the 12MHz is the maximum value.

For the SPI1 module of KL25, the SPI1 uses System clock as it's driving clock, the system clock is 48MHz, with the same baudrate setting, the baudrate of SPi1 is twice of SPI0 module. When the SPPR=0 and SPR=0, the SPSCK get the maximum baudrate, which is 48M/((0+1)*2)=24MHz.

From software perspective, you can use the hal function to set baudrate. uint32_t SPI_HAL_SetBaud(SPI_Type * base, uint32_t bitsPerSec, uint32_t sourceClockInHz);

for example, this is the SPI initializing function:

/*FUNCTION**********************************************************************

*

* Function Name : SPI_DRV_MasterConfigureBus

* Description   : Configures the SPI port to access a device on the bus.

* The term "device" is used to indicate the SPI device for which the SPI master is communicating.

* The user has two options to configure the device parameters: either pass in the

* pointer to the device configuration structure to the desired transfer function (see

* SPI_DRV_MasterTransferDataBlocking or SPI_DRV_MasterTransferData) or pass it in to the

* SPI_DRV_MasterConfigureBus function.  The user can pass in a device structure to the transfer

* function which contains the parameters for the bus (the transfer function will then call

* this function). However, the user has the option to call this function directly especially

* to get the calculated baud rate, at which point they may pass in NULL for the device

* struct in the transfer function (assuming they have called this configure bus function

* first).

*

*END**************************************************************************/

void SPI_DRV_MasterConfigureBus(uint32_t instance,

                                const spi_master_user_config_t * device,

                                uint32_t * calculatedBaudRate)

{

    assert(device);

    /* instantiate local variable of type spi_master_state_t and point to global state */

    spi_master_state_t * spiState = (spi_master_state_t *)g_spiStatePtr[instance];

    SPI_Type *base = g_spiBase[instance];

    /* Configure the bus to access the provided device.*/

    *calculatedBaudRate = SPI_HAL_SetBaud(base, device->bitsPerSec, spiState->spiSourceClock);

    SPI_HAL_SetDataFormat(base, device->polarity, device->phase, device->direction);

#if FSL_FEATURE_SPI_16BIT_TRANSFERS

    SPI_HAL_Set8or16BitMode(base, device->bitCount);

#endif

}

Hope it can help you.

BR

XiangJun Rong

View solution in original post

1 Reply
398 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Michael,

when the SPI of Kl25 is configured as master mode, the SPI clock signal SPSCK is an output clock, it's output frequency is called baudrate:

BaudRateDivisor = (SPPR + 1) × [2**(SPR + 1)]

BaudRate = BusClock / BaudRateDivisor

For detailed information, pls refer to section 37.4.6 SPI Baud Rate Generation in the KL25P80M48SF0RM.pdf.

For the SPI0 module of Kl25, the SPI0 uses BusClock as it's driving clock, the BusClock is 24MHz, when the SPPR=0 and SPR=0, the SPSCK get the maximum baudrate, which is 24M/((0+1)*2)=12MHz, that is why the PE gives you a baudrate list, the 12MHz is the maximum value.

For the SPI1 module of KL25, the SPI1 uses System clock as it's driving clock, the system clock is 48MHz, with the same baudrate setting, the baudrate of SPi1 is twice of SPI0 module. When the SPPR=0 and SPR=0, the SPSCK get the maximum baudrate, which is 48M/((0+1)*2)=24MHz.

From software perspective, you can use the hal function to set baudrate. uint32_t SPI_HAL_SetBaud(SPI_Type * base, uint32_t bitsPerSec, uint32_t sourceClockInHz);

for example, this is the SPI initializing function:

/*FUNCTION**********************************************************************

*

* Function Name : SPI_DRV_MasterConfigureBus

* Description   : Configures the SPI port to access a device on the bus.

* The term "device" is used to indicate the SPI device for which the SPI master is communicating.

* The user has two options to configure the device parameters: either pass in the

* pointer to the device configuration structure to the desired transfer function (see

* SPI_DRV_MasterTransferDataBlocking or SPI_DRV_MasterTransferData) or pass it in to the

* SPI_DRV_MasterConfigureBus function.  The user can pass in a device structure to the transfer

* function which contains the parameters for the bus (the transfer function will then call

* this function). However, the user has the option to call this function directly especially

* to get the calculated baud rate, at which point they may pass in NULL for the device

* struct in the transfer function (assuming they have called this configure bus function

* first).

*

*END**************************************************************************/

void SPI_DRV_MasterConfigureBus(uint32_t instance,

                                const spi_master_user_config_t * device,

                                uint32_t * calculatedBaudRate)

{

    assert(device);

    /* instantiate local variable of type spi_master_state_t and point to global state */

    spi_master_state_t * spiState = (spi_master_state_t *)g_spiStatePtr[instance];

    SPI_Type *base = g_spiBase[instance];

    /* Configure the bus to access the provided device.*/

    *calculatedBaudRate = SPI_HAL_SetBaud(base, device->bitsPerSec, spiState->spiSourceClock);

    SPI_HAL_SetDataFormat(base, device->polarity, device->phase, device->direction);

#if FSL_FEATURE_SPI_16BIT_TRANSFERS

    SPI_HAL_Set8or16BitMode(base, device->bitCount);

#endif

}

Hope it can help you.

BR

XiangJun Rong

View solution in original post