ESP32 SPI驱动Li3dh&&kx203

代码下载地址:
https://download.csdn.net/download/qq_31806069/11239726

1、 在esp-idf/examples/peripherals/spi_master,修改的。

2、 SPI的最底层的驱动程序。
2.1(ESP32有四个SPI外设,称为SPI0,SPI1,HSPI和VSPI。 SPI0完全专用于ESP32用于将连接的SPI闪存设备映射到存储器的闪存缓存。 SPI1连接到与SPI0相同的硬件线,用于写入闪存芯片。 HSPI和VSPI可以免费使用。 SPI1,HSPI和VSPI都有三条片选线,允许它们最多驱动三个SPI器件作为主器件。)
2.2 spi_master.h的接口函数有两种方式调用SPI,中断传输SPI和轮询传输SPI。官方不建议混用两种方式。

bool LIS3DH_ReadReg(u8_t Reg, u8_t* Data)

{
    esp_err_t ret;
    spi_transaction_t t;
    ACC_CS_LOW(); // CSN low, initialize SPI communication...
    Reg|=0x80;
    memset(&t, 0, sizeof(t));       //Zero out the transaction
    t.length=8;                     //Command is 8 bits
    t.tx_buffer=&Reg;               //The data is the cmd itself
    
    #if IS_POLLIN
    ret=spi_device_polling_transmit(my_li3dh_spi, &t);  //Transmit!
    assert(ret==ESP_OK);            //Should have had no issues.
    #else
    spi_device_transmit(my_li3dh_spi, &t);
    #endif

    memset(&t, 0, sizeof(t));
    t.length=8;
    t.flags = SPI_TRANS_USE_RXDATA;
    
    #if IS_POLLIN
    ret = spi_device_polling_transmit(my_li3dh_spi, &t);

    assert( ret == ESP_OK );
    #else
    spi_device_transmit(my_li3dh_spi, &t);
    #endif
    
    *Data = t.rx_data[0];
    //printf("--->%x-%x-%x\n",t.rx_data[0],t.rx_data[1],t.rx_data[2]);
     ACC_CS_HIGH(); // CSN high, terminate SPI communication

    
    return true;
}
bool LIS3DH_WriteReg(u8_t WriteAddr, u8_t Data)
{

    esp_err_t ret;
    spi_transaction_t t;
    uint8_t buf[2];
    ACC_CS_LOW(); // CSN low, init SPI transaction

    WriteAddr &= 0x7f;
    buf[0] = WriteAddr;
    buf[1] = Data;
    memset(&t, 0, sizeof(t));       //Zero out the transaction
    t.length=8*2;                     //Command is 8 bits
    t.tx_buffer=buf;               //The data is the cmd itself

    //t.user=(void*)0;                //D/C needs to be set to 0
    #if IS_POLLIN
          ret=spi_device_polling_transmit(my_li3dh_spi, &t);  //Transmit!
        assert(ret==ESP_OK);
    #else
            spi_device_transmit(my_li3dh_spi, &t);
        //spi_device_queue_trans
    #endif
        //Should have had no issues.
    ACC_CS_HIGH(); // CSN high, terminate SPI communication



    return true;
}

3、 主函数

AxesRaw_t axes[32];
uint8_t get_data_item;
int16_t xt,yt,zt;
int i ;
static uint32_t AccSensorData[32];

void app_main()
{


    esp_err_t ret;
    spi_device_handle_t spi;

    spi_bus_config_t buscfg=
    {
        .miso_io_num=kx203_miso,
        .mosi_io_num=kx203_mosi,

        .sclk_io_num=kx203_clk,
        .quadwp_io_num=-1,
        .quadhd_io_num=-1,
        .max_transfer_sz=PARALLEL_LINES*320*2+8
    };
    spi_device_interface_config_t devcfg=
    {
        .clock_speed_hz=5*1000*1000,           //Clock out at 10 MHz
        .mode=0,                                //SPI mode 0
        .spics_io_num=-1,//PIN_NUM_CS,               //CS pin
        .queue_size=7,                          //We want to be able to queue 7 transactions at a time
        //.pre_cb=0,//lcd_spi_pre_transfer_callback,  //Specify pre-transfer callback to handle D/C line指定预传输回调以处理D/C线
    };


    //Initialize the SPI bus
    ret=spi_bus_initialize(HSPI_HOST, &buscfg, 1);// HSPI_HOST
    ESP_ERROR_CHECK(ret);
    //Attach the LCD to the SPI bus  将LCD连接到SPI总线上
    ret=spi_bus_add_device(HSPI_HOST, &devcfg, &spi);//HSPI_HOST
    ESP_ERROR_CHECK(ret);
    init_kx203_gpio();

    assignment_spi(spi);

    printf("------------>g_sensor_ok%d\n",AccSensorConfig());

    while(1)
    {
        printf("------------------->\n");
        get_data_item=GetData(axes);
        for(i= 0; i < get_data_item; i++)
        {
            xt=((int16_t)axes[i].AXIS_X)>>4;
            yt=((int16_t)axes[i].AXIS_Y)>>4;
            zt=((int16_t)axes[i].AXIS_Z)>>4;
            AccSensorData[i] = xt*xt+yt*yt+zt*zt;
            AccSensorData[i] /= 100;
            printf("%02d-x:%d-y:%d-z:%d\n",i,xt,yt,zt);
        }
        vTaskDelay(500 / portTICK_PERIOD_MS);
    }
}




4、运行结果。


image.png

你可能感兴趣的:(ESP32 SPI驱动Li3dh&&kx203)