FX3/CX3 SPI

来源:互联网 发布:专业金蝶初始数据录入 编辑:程序博客网 时间:2024/05/16 18:21

硬件spi,without DMA.
1. main中: io_cfg.useI2C = CyTrue;
2.

 #include "cyu3spi.h"CyU3PReturnStatus_tmy_spi_init(void){    CyU3PGpioSimpleConfig_t gpioConfig;    CyU3PSpiConfig_t spiConfig;    CyU3PReturnStatus_t status = CY_U3P_SUCCESS;    //gpio config.    status = CyU3PDeviceGpioOverride (18, CyTrue);    status = CyU3PDeviceGpioOverride (19, CyTrue);    status = CyU3PDeviceGpioOverride (17, CyTrue);    if (status != CY_U3P_SUCCESS)    {        CyU3PDebugPrint(4, "CyU3PDeviceGpioOverride Err = 0x%x\r\n",status);        return status;    }    gpioConfig.outValue = CyTrue;    gpioConfig.driveLowEn = CyTrue;    gpioConfig.driveHighEn = CyTrue;    gpioConfig.inputEn = CyFalse;    gpioConfig.intrMode = CY_U3P_GPIO_NO_INTR;    status = CyU3PGpioSetSimpleConfig(17, &gpioConfig);    if (status != CY_U3P_SUCCESS)     {        CyU3PDebugPrint(4, "CyU3PGpioSetSimpleConfig Err = 0x%x\r\n",status);    //  return status;     }    CyU3PGpioSetSimpleConfig(18, &gpioConfig);    CyU3PGpioSetSimpleConfig(19, &gpioConfig);    CyU3PGpioSetValue(17, CyTrue);    CyU3PGpioSetValue(18, CyTrue);    CyU3PGpioSetValue(19, CyFalse);    //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      = 10000000;    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, "CyU3PSpiSetConfig ok.\r\n");    //CyU3PSpiGetLock();    /////////////////////////////////////////////////////    const uint8_t  MPU_DEVICE_ID_REG = 0x75;    const uint8_t  MPU_6500_ID = 0x70;    uint8_t dat[2] ={ MPU_DEVICE_ID_REG };    dat[0] |= 0x80;    CyU3PSpiSetSsnLine (CyFalse);    status = CyU3PSpiTransmitWords(dat,1);    status = CyU3PSpiReceiveWords(dat,1);    CyU3PSpiSetSsnLine (CyTrue);    if(dat[0] != MPU_ID){        CyU3PDebugPrint(4, "Read MPU_ID Err =  0x%x,read:%d\r\n",status,dat[0]);        return 0;    }    CyU3PDebugPrint(4, "Read MPU_ID OK(0x70) =  0x%x\r\n",status);    //reset MCU    const uint8_t dat2[10] = {0x6B,0x80,0x68,0x03,0x6a,0x01,0x68,0x00,0x6a,0x00};    // reset MPU    MPU_WriteReg(dat2[0],dat2[1]); //reset    delay(1000);    MPU_WriteReg(dat2[2],dat2[3]); //reset GYTO ACCRL TEMP    MPU_WriteReg(dat2[4],dat2[5]); //reset GYTO ACCRL TEMP    MPU_WriteReg(dat2[6],dat2[7]); //reset GYTO ACCRL TEMP    MPU_WriteReg(dat2[8],dat2[9]); //reset GYTO ACCRL TEMP    delay(1000);    //MCU_Config    uint8_t Reg_Addr[8] ={        0x19, //SMPLRT_DIV        0x1A, //CONFIG        0x1B, //GYRO_CONFIG        0x1C, //ACCEL_CONFIG        0x1D, //ACCEL_CONFIG 2        0x6A, //USER_CTRL        0x6B, //PWR_MGMT_1        0x6C };//PWR_MGMT_2    uint8_t  Reg_Val[8] ={        0x07, //0x01, //SAMPLE_RATE        0x00, //Gyro Fs = 32KHz        0x18,// 0x1a,// 0x18,//csn change ;Gyro Full Scale = +-2000dps        0x18,// 0x08,  //0x18,//+-16G // 0x08,//+-4G // 0x18, //Acc Full Scale = +-16G        0x08, //Acc Rate = 4KHz        0x10, //SPI mode only.        0x00, //PLL if ready, else use the Internal oscillator        0x00 };//All sensors on        uint8_t i=0;        for(i = 1;i < 8; i++){            MPU_WriteReg(Reg_Addr[i],Reg_Val[i]);            delay(100);        }    return status;}
0 0
原创粉丝点击