📄 drvspi.c
字号:
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvSPI_SetTriggerMode */
/* */
/* Parameters: */
/* eSpiPort [in]: Specify the SPI port */
/* eSSTriggerMode [in]: Specify the trigger mode. (eDRVSPI_EDGE_TRIGGER or eDRVSPI_LEVEL_TRIGGER) */
/* */
/* Returns: */
/* None. */
/* */
/* Description: */
/* Set the trigger mode of slave select pin. Only for slave mode. */
/*---------------------------------------------------------------------------------------------------------*/
void DrvSPI_SetTriggerMode(E_DRVSPI_PORT eSpiPort, E_DRVSPI_SSLTRIG eSSTriggerMode)
{
SPI_PORT[eSpiPort]->SSR.SS_LTRIG = eSSTriggerMode;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvSPI_SetSlaveSelectActiveLevel */
/* */
/* Parameters: */
/* eSpiPort [in]: Specify the SPI port */
/* eSSActType [in]: Select the active type of slave select pin. */
/* eDRVSPI_ACTIVE_LOW_FALLING: Slave select pin is active low in level-trigger mode; */
/* or falling-edge trigger in edge-trigger mode. */
/* eDRVSPI_ACTIVE_HIGH_RISING: Slave select pin is active high in level-trigger mode; */
/* or rising-edge trigger in edge-trigger mode. */
/* */
/* Returns: */
/* None. */
/* */
/* Description: */
/* Set the active level of slave select. */
/*---------------------------------------------------------------------------------------------------------*/
void DrvSPI_SetSlaveSelectActiveLevel(E_DRVSPI_PORT eSpiPort, E_DRVSPI_SS_ACT_TYPE eSSActType)
{
SPI_PORT[eSpiPort]->SSR.SS_LVL = eSSActType;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvSPI_GetLevelTriggerStatus */
/* */
/* Parameters: */
/* eSpiPort [in]: Specify the SPI port */
/* */
/* Returns: */
/* TRUE : The transaction number and the transferred bit length met the requirements which defines in */
/* TX_NUM and TX_BIT_LEN among one transfer. */
/* FALSE: The transaction number or the transferred bit length of one transaction doesn't meet the */
/* requirements. */
/* */
/* Description: */
/* Get the level-trigger transmission status. Only for slave mode. */
/*---------------------------------------------------------------------------------------------------------*/
uint8_t DrvSPI_GetLevelTriggerStatus(E_DRVSPI_PORT eSpiPort)
{
if(SPI_PORT[eSpiPort]->SSR.LTRIG_FLAG==1)
return TRUE;
else
return FALSE;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvSPI_EnableAutoSS */
/* */
/* Parameters: */
/* eSpiPort [in]: Specify the SPI port */
/* eSlaveSel [in]: Select the slave select pins which will be used. */
/* It could be eDRVSPI_NONE, eDRVSPI_SS0, eDRVSPI_SS1 and eDRVSPI_SS0_SS1. */
/* */
/* Returns: */
/* None. */
/* */
/* Description: */
/* Enable the automatic slave select function and set the specified slave select pin. Only for master */
/* mode. */
/*---------------------------------------------------------------------------------------------------------*/
void DrvSPI_EnableAutoSS(E_DRVSPI_PORT eSpiPort, E_DRVSPI_SLAVE_SEL eSlaveSel)
{
SPI_PORT[eSpiPort]->SSR.AUTOSS = 1;
SPI_PORT[eSpiPort]->SSR.SSR = eSlaveSel;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvSPI_DisableAutoSS */
/* */
/* Parameters: */
/* eSpiPort [in]: Specify the SPI port */
/* */
/* Returns: */
/* None. */
/* */
/* Description: */
/* Disable the Automatic Slave Select function and deselect slave select pins. Only for master mode. */
/*---------------------------------------------------------------------------------------------------------*/
void DrvSPI_DisableAutoSS(E_DRVSPI_PORT eSpiPort)
{
SPI_PORT[eSpiPort]->SSR.AUTOSS = 0;
SPI_PORT[eSpiPort]->SSR.SSR = eDRVSPI_NONE;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvSPI_SetSS */
/* */
/* Parameters: */
/* eSpiPort [in]: Specify the SPI port */
/* eSlaveSel [in]: In automatic slave select operation, to use this parameter to select the slave */
/* select pins which will be used. The specified slave select pins will be controlled */
/* by hardware. In manual slave select operation, the specified slave select pins will */
/* be set to active state. It could be eDRVSPI_NONE, eDRVSPI_SS0, eDRVSPI_SS1 or */
/* eDRVSPI_SS0_SS1. */
/* */
/* Returns: */
/* None. */
/* */
/* Description: */
/* Set the slave select pins. Only for master mode. */
/*---------------------------------------------------------------------------------------------------------*/
void DrvSPI_SetSS(E_DRVSPI_PORT eSpiPort, E_DRVSPI_SLAVE_SEL eSlaveSel)
{
SPI_PORT[eSpiPort]->SSR.SSR = eSlaveSel;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvSPI_ClrSS */
/* */
/* Parameters: */
/* eSpiPort [in]: Specify the SPI port */
/* eSlaveSel [in]: Specify slave select pins */
/* It could be eDRVSPI_NONE, eDRVSPI_SS0, eDRVSPI_SS1 or eDRVSPI_SS0_SS1. */
/* */
/* Returns: */
/* None. */
/* */
/* Description: */
/* Set the specified slave select pins to inactive state. Only for master mode. */
/*---------------------------------------------------------------------------------------------------------*/
void DrvSPI_ClrSS(E_DRVSPI_PORT eSpiPort, E_DRVSPI_SLAVE_SEL eSlaveSel)
{
uint32_t u32Reg;
u32Reg = SPI_PORT[eSpiPort]->SSR.SSR;
u32Reg = u32Reg & (~eSlaveSel);
SPI_PORT[eSpiPort]->SSR.SSR = u32Reg;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvSPI_IsBusy */
/* */
/* Parameters: */
/* eSpiPort [in]: Specify the SPI port */
/* */
/* Returns: */
/* TRUE: The SPI port is in busy. */
/* FALSE: The SPI port is not in busy. */
/* */
/* Description: */
/* Check the busy status of the specified SPI port. */
/*---------------------------------------------------------------------------------------------------------*/
uint8_t DrvSPI_IsBusy(E_DRVSPI_PORT eSpiPort)
{
return ((SPI_PORT[eSpiPort]->CNTRL.GO_BUSY)?TRUE:FALSE);
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvSPI_BurstTransfer */
/* */
/* Parameters: */
/* eSpiPort [in]: Specify the SPI port */
/* i32BurstCnt [in]: Specify the transaction number in one transfer. It could be 1 or 2. */
/* i32Interval [in]: Specify the delay clocks between successive transactions. It could be 2~17. */
/* */
/* Returns: */
/* E_DRVSPI_ERR_BURST_CNT: The transaction number is out of range. */
/* E_DRVSPI_ERR_SUSPEND_INTERVAL: The suspend interval setting is out of range. */
/* E_SUCCESS: Success. */
/* */
/* Description: */
/* Configure the burst transfer settings. */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvSPI_BurstTransfer(E_DRVSPI_PORT eSpiPort, int32_t i32BurstCnt, int32_t i32Interval)
{
if((i32BurstCnt < 1) || (i32BurstCnt > 2))
{
return E_DRVSPI_ERR_BURST_CNT;
}
if((i32Interval < 2) || (i32Interval > 17))
{
return E_DRVSPI_ERR_SUSPEND_INTERVAL;
}
SPI_PORT[eSpiPort]->CNTRL.TX_NUM = i32BurstCnt-1;
SPI_PORT[eSpiPort]->CNTRL.SP_CYCLE = i32Interval-2;
return E_SUCCESS;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvSPI_SetClockFreq */
/* */
/* Parameters: */
/* eSpiPort [in]: Specify the SPI port */
/* u32Clock1 [in]: Specify the SPI clock rate in Hz. It's the target clock rate of SPI base clock and */
/* variable clock 1. */
/* u32Clock2 [in]: Specify the SPI clock rate in Hz. It's the target clock rate of variable clock 2. */
/* */
/* Returns: */
/* The actual clock rate of SPI engine clock is returned. */
/* SPI engine clock rate = APB clock rate / ((DIVIDER + 1) * 2) */
/* The actual clock rate may be different from the target SPI clock rate. */
/* For example, if the system clock rate is 12MHz and the target SPI base clock rate is 7MHz, the */
/* actual SPI clock rate will be 6MHz. */
/* */
/* Description: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -