从0开始创建FX3工程之五 - SPI

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

测试无误。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值