|

楼主 |
发表于 2018-6-14 19:35:55
|
显示全部楼层
BSP_SPI_T gSpi3Struct = {
{GPIOB, GPIO_Pin_5}, //MOSI
{GPIOB, GPIO_Pin_4}, //MISO
{GPIOA, GPIO_Pin_15}, //NSS
{GPIOB, GPIO_Pin_3}, //CLK
SPI3,
};
void fpga_config(void)
{
/* ′ò?a GPIOE ê±?ó */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB, ENABLE);
/* ′ò?a SPI ê±?ó */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
/*SPI regster and port config*/
SPI_PortConfig(&gSpi3Struct);
}
void SPI_PortConfig(BSP_SPI_T * pSPI_Struct)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_PinAFConfig(pSPI_Struct->Clk.Port, GPIO_PinSource3, GPIO_AF_SPI3);
GPIO_PinAFConfig(pSPI_Struct->Miso.Port, GPIO_PinSource4, GPIO_AF_SPI3);
GPIO_PinAFConfig(pSPI_Struct->Mosi.Port, GPIO_PinSource5, GPIO_AF_SPI3);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(pSPI_Struct->pSPI, &SPI_InitStructure);
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
/*MOSI*/
GPIO_InitStructure.GPIO_Pin = pSPI_Struct->Mosi.Pin;
GPIO_Init(pSPI_Struct->Mosi.Port, &GPIO_InitStructure);
/*MISO*/
GPIO_InitStructure.GPIO_Pin = pSPI_Struct->Miso.Pin;
GPIO_Init(pSPI_Struct->Miso.Port, &GPIO_InitStructure);
/*CLK*/
GPIO_InitStructure.GPIO_Pin = pSPI_Struct->Clk.Pin;
GPIO_Init(pSPI_Struct->Clk.Port, &GPIO_InitStructure);
if(SPI_IsNssHardMode(pSPI_Struct->pSPI) == pdFALSE)
{
/*NSS confiured software mode,so configure NSS pin as output*/
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Pin = pSPI_Struct->Nss.Pin;
GPIO_Init(pSPI_Struct->Nss.Port, &GPIO_InitStructure);
}
else
{
/*NSS hardware mode*/
GPIO_PinAFConfig(pSPI_Struct->Nss.Port, GPIO_PinSource15, GPIO_AF_SPI3);
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Pin = pSPI_Struct->Nss.Pin;
GPIO_Init(pSPI_Struct->Nss.Port, &GPIO_InitStructure);
}
GPIO_SetBits(pSPI_Struct->Nss.Port, pSPI_Struct->Nss.Pin);
FPGA_read_EXTIConfig();
SPI_Cmd(pSPI_Struct->pSPI, ENABLE);
}
SPI_DataTrans_Rec(BSP_SPI_T* pSPI_Struct, uint16_t *pData2Snd, uint16_t *pRcvBuf, uint16_t Length)
{
uint16_t i;
for(i = 0; i < Length; i++)
{
while (SPI_I2S_GetFlagStatus(pSPI_Struct->pSPI, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(pSPI_Struct->pSPI, *(pData2Snd + i));
while (SPI_I2S_GetFlagStatus(pSPI_Struct->pSPI, SPI_I2S_FLAG_RXNE) == RESET);
*pRcvBuf++ = SPI_I2S_ReceiveData(pSPI_Struct->pSPI);
}
}
void SPI_DataTransMst(BSP_SPI_T* pSPI_Struct, uint16_t * puhSndValue, uint16_t * puhRcvValue, uint16_t Length, SPI_DIR_MODE_E SPI_DirMode)
{
if(SPI_IsNssHardMode(pSPI_Struct->pSPI) == pdFALSE)
{
if(pSPI_Struct->Nss.Port != NULL)
{
GPIO_ResetBits(pSPI_Struct->Nss.Port, pSPI_Struct->Nss.Pin);
bsp_DelayUS(2);
}
}
if((SPI_DirMode == SPI_DIR_FULL_DUP) || (SPI_DirMode ==SPI_DIR_READ_ONLY ))
{
SPI_DataTrans_Rec(pSPI_Struct, puhSndValue, puhRcvValue, Length);
}
else if(SPI_DirMode == SPI_DIR_WRITE_ONLY)
/*data transmit*/
SPI_DataTransmit(pSPI_Struct, puhSndValue, Length);
if(SPI_IsNssHardMode(pSPI_Struct->pSPI) == pdFALSE)
{
if(pSPI_Struct->Nss.Port != NULL)
{
GPIO_SetBits(pSPI_Struct->Nss.Port, pSPI_Struct->Nss.Pin);
bsp_DelayUS(2);
}
}
}
|
|