下面是LPC3131的SPI驱动程序,其中给DIV赋值的那句话,我没明白,哪位大侠给解释一下!
static STATUS spi_get_clock_div(UNS_32 target_clock,
UNS_32 *pDivSet)
{
UNS_32 div, spi_clk, ps, div1;
STATUS retvalue = _ERROR;
/* The SPI clock is derived from the (main system oscillator / 2),
so compute the best divider from that clock */
spi_clk = cgu_get_clk_freq(CGU_SB_SPI_CLK_ID);
/* Find closest divider to get at or under the target frequency.
Use smallest prescaler possible and rely on the divider to get
the closest target frequency */
div = (spi_clk + target_clock / 2) / target_clock;//div为什么这样计算,没看明白?
if ((div < SPI_MAX_DIVIDER) && (div > SPI_MIN_DIVIDER))
{
ps = (((div - 1) / 512) + 1) * 2;
div1 = ((((div + ps / 2) / ps) - 1));
/* write the divider settings */
*pDivSet = SPI_SLV1_CLK_PS(ps) | SPI_SLV1_CLK_DIV1((div1));
retvalue = _NO_ERROR;
}
return retvalue;
} |