时钟配置
时钟树
使用官方提供的MCUXpresso Config Tools v6工具可以很方便的配置和选择
由上图可知LPSPI由4个时钟源可以选择
分别是:PLL2 PFD2 clock output
、PLL2 main clock output
、PLL3 PFD0 clock output
、PLL3 PFD1 clock output
时钟源
查看rt1052的数据手册
根据数据手册可知使用库函数CLOCK_SetMux(kCLOCK_LpspiMux, 2);
选择的就是PLL2 main clock output
的时钟源
分频系数
分频系数很好理解,使用库函数的时候会在设置的值+1, CLOCK_SetDiv(kCLOCK_LpspiDiv, 4);
是5分频
注意事项
1、最大时钟
LPSPI最大支持时钟如下图所示:
但是我使用MCUXpresso Config Tools v6
工具发现只能配置到120MHz,超过这个波特率有概率会在spi发送函数中卡死
2、真实的SPI_CLK
The LPSPI functional clock is asynchronous to the bus clock and if it remains enabled in
low power modes then LPSPI can perform SPI bus transfers and low power wakeups in
both master and slave modes. The LPSPI divides the functional clock by a prescaler and
the resulting frequency must be at least two times faster than the SPI external clock
(LPSPI_SCK) frequency.
摘自数据手册,通过库函数代码发现库函数会计算出一个最佳波特率,其值会是设置的波特率的一半,比如正点原子的例程设置的96M但是最后分频下来真实的波特率是48MHz
uint32_t LPSPI_MasterSetBaudRate(LPSPI_Type *base,
uint32_t baudRate_Bps,
uint32_t srcClock_Hz,
uint32_t *tcrPrescaleValue)
{
......
uint32_t prescaler, bestPrescaler;
uint32_t scaler, bestScaler;
uint32_t realBaudrate, bestBaudrate;
uint32_t diff, min_diff;
uint32_t desiredBaudrate = baudRate_Bps;
/* calculate the baud rate difference based on the conditional statement
* that states that the calculated baud rate must not exceed the desired baud rate
*/
if (desiredBaudrate >= realBaudrate)
{
diff = desiredBaudrate - realBaudrate;
if (min_diff > diff)
{
/* a better match found */
min_diff = diff;
bestPrescaler = prescaler;
bestScaler = scaler;
bestBaudrate = realBaudrate;//计算最佳波特率切返回
}
}
}
}
......
/* return the actual calculated baud rate */
return bestBaudrate;
}