Hi An,
From the code provide I can see (correct me if wrong) that you are using KSDK_1.3. Correct?
If you look at the following example as reference:
C:\Freescale\KSDK_1.3.0\examples\frdmk22f\driver_examples\dspi\dspi_polling\master\kds
You will see in the main() in main.c for setting up the SPI Master there is a HAL call to set various delays:
// Initialize the configurable delays: PCS-to-SCK, prescaler = 0, scaler = 1
DSPI_HAL_SetDelay(dspiBaseAddr, kDspiCtar0, 0, 1, kDspiPcsToSck);
The fsl_dspi_hal.h header has following information:
/*!
* @brief Manually configures the delay prescaler and scaler for a particular CTAR.
*
* This function configures the PCS to SCK delay pre-scalar (PCSSCK) and scalar (CSSCK),
* after SCK delay pre-scalar (PASC) and scalar (ASC), and the delay
* after transfer pre-scalar (PDT)and scalar (DT).
*
* These delay names are available in type dspi_delay_type_t.
*
* The user passes which delay they want to configure along with the prescaler and scaler value.
* This allows the user to directly set the prescaler/scaler values if they have
* pre-calculated them or if they simply wish to manually increment either value.
*
* @param base Module base pointer of type SPI_Type.
* @param whichCtar The desired Clock and Transfer Attributes Register (CTAR) of type
* dspi_ctar_selection_t.
* @param prescaler The prescaler delay value (can be an integer 0, 1, 2, or 3).
* @param scaler The scaler delay value (can be any integer between 0 to 15).
* @param whichDelay The desired delay to configure, must be of type dspi_delay_type_t
*/
void DSPI_HAL_SetDelay(SPI_Type * base, dspi_ctar_selection_t whichCtar, uint32_t prescaler,
uint32_t scaler, dspi_delay_type_t whichDelay);
The fsl_dspi_hal.c has the following:
/*FUNCTION**********************************************************************
*
* Function Name : DSPI_HAL_SetDelay
* Description : Manually configures the delay prescaler and scaler for a particular CTAR.
* This function configures the:
* PCS to SCK delay pre-scalar (PCSSCK) and scalar (CSSCK),
* After SCK delay pre-scalar (PASC) and scalar (ASC),
* Delay after transfer pre-scalar (PDT)and scalar (DT).
*
* These delay names are available in type dspi_delay_type_t.
*
* The user passes which delay they want to configure along with the prescaler and scaler value.
* This basically allows the user to directly set the prescaler/scaler values if they have
* pre-calculated them or if they simply wish to manually increment either value.
*END**************************************************************************/
void DSPI_HAL_SetDelay(SPI_Type * base, dspi_ctar_selection_t whichCtar, uint32_t prescaler,
uint32_t scaler, dspi_delay_type_t whichDelay)
{
/* these settings are only relevant in master mode */
if ((bool)SPI_RD_MCR_MSTR(base))
{
if (whichDelay == kDspiPcsToSck)
{
SPI_BWR_CTAR_PCSSCK(base, whichCtar, prescaler);
SPI_BWR_CTAR_CSSCK(base, whichCtar, scaler);
}
if (whichDelay == kDspiLastSckToPcs)
{
SPI_BWR_CTAR_PASC(base, whichCtar, prescaler);
SPI_BWR_CTAR_ASC(base, whichCtar, scaler);
}
if (whichDelay == kDspiAfterTransfer)
{
SPI_BWR_CTAR_PDT(base, whichCtar, prescaler);
SPI_BWR_CTAR_DT(base, whichCtar, scaler);
}
}
}
From the "C" code it shows there are 3 delays you can tune. The example code is only adjusting the "kDspiPcsToSck" delay but you can also add in calls to the "kDspiLastSckToPcs", and "kDspiAfterTransfer".
Now if you want the code to calculate the nears possible delay value(s) there is a function to review as well:
/*!
* @brief Calculates the delay prescaler and scaler based on the desired delay input in nanoseconds.
*
* This function calculates the values for:
* PCS to SCK delay pre-scalar (PCSSCK) and scalar (CSSCK), or
* After SCK delay pre-scalar (PASC) and scalar (ASC), or
* Delay after transfer pre-scalar (PDT)and scalar (DT).
*
* These delay names are available in type dspi_delay_type_t.
*
* The user passes which delay they want to configure along with the desired delay value in
* nanoseconds. The function calculates the values needed for the prescaler and scaler and
* returning the actual calculated delay as an exact delay match may not be possible. In this
* case, the closest match is calculated without going below the desired delay value input.
* It is possible to input a very large delay value that exceeds the capability of the part, in
* which case the maximum supported delay is returned. It is to the higher level
* peripheral driver to alert the user of an out of range delay input.
*
* @param base Module base pointer of type SPI_Type.
* @param whichCtar The desired Clock and Transfer Attributes Register (CTAR) of type
* dspi_ctar_selection_t.
* @param whichDelay The desired delay to configure, must be of type dspi_delay_type_t
* @param sourceClockInHz Module source input clock in Hertz
* @param delayInNanoSec The desired delay value in nanoseconds.
* @return The actual calculated delay value.
*/
uint32_t DSPI_HAL_CalculateDelay(SPI_Type * base, dspi_ctar_selection_t whichCtar,
dspi_delay_type_t whichDelay, uint32_t sourceClockInHz,
uint32_t delayInNanoSec);
In summary I think the dspi_blocking_example_master_frdmk22f example is setting up the Driver with default delays. Using the polling example to see how using HAL function calls to setup delays, should allow you to tune the SPI interface for your application.
Regards,
David