1 SPI 功能使能
#ifdef MY_SPI
io_cfg.useSpi = CyTrue;
#else
io_cfg.useSpi = CyFalse;
#endif
2 SPI 初始化
#ifdef MY_SPI
CyU3PReturnStatus_t my_spi_init(void)
{
CyU3PSpiConfig_t spiConfig;
CyU3PReturnStatus_t status = CY_U3P_SUCCESS;
//SPI_Config
status = CyU3PSpiInit ();
if (status != CY_U3P_SUCCESS)
{
CyU3PDebugPrint(4, "CyU3PSpiInit Err = 0x%x\r\n",status);//??? 0x44:CY_U3P_ERROR_NOT_CONFIGURED
return status;
}
CyU3PMemSet ((uint8_t *)&spiConfig, 0, sizeof(spiConfig));
spiConfig.isLsbFirst = CyFalse;//MSB1
spiConfig.cpol = CyFalse;//CyFalse SPI_CPOL_Low CyTrue1
spiConfig.ssnPol = CyFalse;//SSN is active low 1
spiConfig.cpha = CyFalse;//CyFalse SPI_CPHA_1Edge CyTrue1
spiConfig.leadTime = CY_U3P_SPI_SSN_LAG_LEAD_HALF_CLK;
spiConfig.lagTime = CY_U3P_SPI_SSN_LAG_LEAD_HALF_CLK;
spiConfig.ssnCtrl = CY_U3P_SPI_SSN_CTRL_FW;//SSN is controlled by API
spiConfig.clock = 8000000;//src:10000000 8000000
spiConfig.wordLen = 8;
status = CyU3PSpiSetConfig (&spiConfig, NULL);
if (status != CY_U3P_SUCCESS)
{
CyU3PDebugPrint(4, "CyU3PSpiSetConfig Err = 0x%x\r\n",status);
return status;
}
CyU3PDebugPrint(4, "\r\nCyU3PSpiSetConfig ok.\r\n");
return status;
}
#endif
3 spi读写函数
void SPI_WriteReg(uint8_t WriteAddr, uint8_t WriteData)
{
CyU3PSpiSetSsnLine (CyFalse);
CyU3PSpiTransmitWords(&WriteAddr,1);
CyU3PSpiTransmitWords(&WriteData,1);
CyU3PSpiSetSsnLine (CyTrue);
}
void SPI_ReadBuf( uint8_t ReadAddr, uint8_t *ReadBuf, uint8_t Bytes )
{
uint8_t i = 0;
CyU3PSpiSetSsnLine (CyFalse);
ReadAddr |= 0x80;
CyU3PSpiTransmitWords(&ReadAddr,1);
for(i=0; i< Bytes; i++){
CyU3PSpiReceiveWords(&ReadBuf[i],1);
}
CyU3PSpiSetSsnLine (CyTrue);
}
cyfxusbspidmamode
cyfxusbspigpiomode : IO口模拟
cyfxusbspiregmode
测试无误。