📄 drvspi.c
字号:
/* Configure the SPI clock. Only for master mode. */
/* If the DIV_ONE bit is set to 1, executing this function is unmeaningful. */
/*---------------------------------------------------------------------------------------------------------*/
uint32_t DrvSPI_SetClockFreq(E_DRVSPI_PORT eSpiPort, uint32_t u32Clock1, uint32_t u32Clock2)
{
uint32_t u32Div;
uint32_t u32Pclk;
u32Pclk = DrvSYS_GetHCLKFreq();
u32Div = 0xFFFF; /* Initial value */
if(u32Clock2!=0)
{
if(u32Clock2>u32Pclk)
u32Div = 0;
else
{
u32Div = (((u32Pclk / u32Clock2) + 1) >> 1) - 1;
if(u32Div > 65535)
u32Div = 65535;
}
SPI_PORT[eSpiPort]->DIVIDER.DIVIDER2 = u32Div;
}
else
SPI_PORT[eSpiPort]->DIVIDER.DIVIDER2 = 0xFFFF;
if(u32Clock1!=0)
{
if(u32Clock1>u32Pclk)
u32Div = 0;
else
{
u32Div = (((u32Pclk / u32Clock1) + 1) >> 1) - 1;
if(u32Div > 0xFFFF)
u32Div = 0xFFFF;
}
SPI_PORT[eSpiPort]->DIVIDER.DIVIDER = u32Div;
}
else
SPI_PORT[eSpiPort]->DIVIDER.DIVIDER = 0xFFFF;
return ( u32Pclk / ((u32Div+1)*2) );
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvSPI_GetClock1Freq */
/* */
/* Parameters: */
/* eSpiPort [in]: Specify the SPI port */
/* */
/* Returns: */
/* The current SPI bus clock frequency in Hz. */
/* */
/* Description: */
/* Get the SPI engine clock rate in Hz. Only for master mode. */
/*---------------------------------------------------------------------------------------------------------*/
uint32_t DrvSPI_GetClock1Freq(E_DRVSPI_PORT eSpiPort)
{
uint32_t u32Div;
uint32_t u32ApbClock;
u32ApbClock = DrvSYS_GetHCLKFreq();
u32Div = SPI_PORT[eSpiPort]->DIVIDER.DIVIDER;
return ((u32ApbClock >> 1) / (u32Div + 1)); /* SPI_CLK = APB_CLK / ((Divider + 1) * 2) */
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvSPI_GetClock2Freq */
/* */
/* Parameters: */
/* eSpiPort [in]: Specify the SPI port */
/* */
/* Returns: */
/* The frequency of variable clock 2 in Hz. */
/* */
/* Description: */
/* Get the clock rate of variable clock 2 in Hz. Only for master mode. */
/*---------------------------------------------------------------------------------------------------------*/
uint32_t DrvSPI_GetClock2Freq(E_DRVSPI_PORT eSpiPort)
{
uint32_t u32Div;
uint32_t u32ApbClock;
u32ApbClock = DrvSYS_GetHCLKFreq();
u32Div = SPI_PORT[eSpiPort]->DIVIDER.DIVIDER2;
return ((u32ApbClock >> 1) / (u32Div + 1)); /* SPI_CLK = APB_CLK / ((Divider + 1) * 2) */
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvSPI_SetVariableClockFunction */
/* */
/* Parameters: */
/* eSpiPort [in]: Specify the SPI port */
/* bEnable [in]: TRUE -- Enable variable clock function */
/* FALSE -- Disable variable clock function */
/* u32Pattern [in]: Specify the variable clock pattern */
/* */
/* Returns: */
/* None. */
/* */
/* Description: */
/* Set the variable clock function. Only for master mode. */
/* If the bit pattern of VARCLK is '0', the output frequency of SPICLK is according to the value of */
/* DIVIDER. */
/* If the bit pattern of VARCLK is '1', the output frequency of SPICLK is according to the value of */
/* DIVIDER2. */
/*---------------------------------------------------------------------------------------------------------*/
void DrvSPI_SetVariableClockFunction(E_DRVSPI_PORT eSpiPort, uint8_t bEnable, uint32_t u32Pattern)
{
if(bEnable)
{
SPI_PORT[eSpiPort]->CNTRL.VARCLK_EN = 1;
SPI_PORT[eSpiPort]->VARCLK = u32Pattern;
}
else
SPI_PORT[eSpiPort]->CNTRL.VARCLK_EN = 0;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvSPI_EnableInt */
/* */
/* Parameters: */
/* eSpiPort [in]: Specify the SPI port */
/* pfnCallback [in]: The callback function of SPI interrupt. */
/* u32UserData [in]: The parameter which will be passed to the callback function. */
/* */
/* Returns: */
/* None. */
/* */
/* Description: */
/* Enable the SPI interrupt of the specified SPI port and install the callback function. */
/*---------------------------------------------------------------------------------------------------------*/
void DrvSPI_EnableInt(E_DRVSPI_PORT eSpiPort, PFN_DRVSPI_CALLBACK pfnCallback, uint32_t u32UserData)
{
if(pfnCallback != NULL)
{
g_sSpiHandler[eSpiPort].pfnOneTransDoneCallBack = pfnCallback;
g_sSpiHandler[eSpiPort].u32OneTransDoneUserData = u32UserData;
}
SPI_PORT[eSpiPort]->CNTRL.IE = 1;
if(eSpiPort == eDRVSPI_PORT0)
{
NVIC_SetPriority(SPI0_IRQn, (1<<__NVIC_PRIO_BITS) - 2);
NVIC_EnableIRQ(SPI0_IRQn);
}
else if(eSpiPort == eDRVSPI_PORT1)
{
NVIC_SetPriority(SPI1_IRQn, (1<<__NVIC_PRIO_BITS) - 2);
NVIC_EnableIRQ(SPI1_IRQn);
}
else if(eSpiPort == eDRVSPI_PORT2)
{
NVIC_SetPriority(SPI2_IRQn, (1<<__NVIC_PRIO_BITS) - 2);
NVIC_EnableIRQ(SPI2_IRQn);
}
else
{
NVIC_SetPriority(SPI3_IRQn, (1<<__NVIC_PRIO_BITS) - 2);
NVIC_EnableIRQ(SPI3_IRQn);
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvSPI_DisableInt */
/* */
/* Parameters: */
/* eSpiPort [in]: Specify the SPI port */
/* */
/* Returns: */
/* None. */
/* */
/* Description: */
/* Disable the SPI interrupt. */
/*---------------------------------------------------------------------------------------------------------*/
void DrvSPI_DisableInt(E_DRVSPI_PORT eSpiPort)
{
g_sSpiHandler[eSpiPort].pfnOneTransDoneCallBack = NULL;
g_sSpiHandler[eSpiPort].u32OneTransDoneUserData = 0;
SPI_PORT[eSpiPort]->CNTRL.IE = 0;
if(eSpiPort == eDRVSPI_PORT0)
{
NVIC_DisableIRQ(SPI0_IRQn);
}
else if(eSpiPort == eDRVSPI_PORT1)
{
NVIC_DisableIRQ(SPI1_IRQn);
}
else if(eSpiPort == eDRVSPI_PORT2)
{
NVIC_DisableIRQ(SPI2_IRQn);
}
else
{
NVIC_DisableIRQ(SPI3_IRQn);
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvSPI_GetIntFlag */
/* */
/* Parameters: */
/* eSpiPort [in]: Specify the SPI port */
/* */
/* Returns: */
/* 0: SPI interrupt doesn't occur */
/* 1: SPI interrupt occurs */
/* */
/* Description: */
/* Return the SPI interrupt flag */
/*---------------------------------------------------------------------------------------------------------*/
uint32_t DrvSPI_GetIntFlag(E_DRV
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -