ESP-IDF 四线MMC/SD卡保存读写BMI160陀螺仪加速度数据代码
时间: 2023-12-09 08:03:51 浏览: 228
以下是ESP-IDF中四线MMC/SD卡保存读写BMI160陀螺仪加速度数据的示例代码:
```c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "sdmmc_cmd.h"
#include "sdmmc_cmd_api.h"
#include "esp_vfs_fat.h"
#include "ff.h"
#include "bmi160.h"
#define PIN_NUM_MISO 2
#define PIN_NUM_MOSI 15
#define PIN_NUM_CLK 14
#define PIN_NUM_CS 13
#define SPI_DMA_CHAN 2
#define SD_CARD_OPEN_RETRY_COUNT 10
#define FILE_NAME "/sdcard/bmi160_data.txt"
spi_device_handle_t spi;
esp_err_t sd_card_init(void)
{
//初始化SD卡驱动
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
//设置SD卡的SPI引脚
gpio_set_pull_mode(PIN_NUM_CS, GPIO_PULLUP_ONLY);
slot_config.gpio_cs = PIN_NUM_CS;
slot_config.width = 4;
//初始化SD卡对象
sdmmc_card_t *card;
esp_err_t ret = sdmmc_host_init_slot(&host, &slot_config, SD_CARD_OPEN_RETRY_COUNT, &card);
if (ret != ESP_OK) {
printf("Failed to initialize the SD card, error=%d", ret);
return ret;
}
//初始化FAT文件系统
static const char *mount_point = "/sdcard";
sdmmc_card_print_info(stdout, card);
esp_vfs_fat_mount_config_t mount_config = {
.format_if_mount_failed = true,
.max_files = 4,
.allocation_unit_size = 16 * 1024
};
ret = esp_vfs_fat_mount(mount_point, card->drv, &mount_config, &card);
if (ret != ESP_OK) {
printf("Failed to mount FAT filesystem, error=%d", ret);
sdmmc_card_deinit(card);
return ret;
}
return ESP_OK;
}
void spi_master_init(void)
{
spi_bus_config_t buscfg = {
.miso_io_num = PIN_NUM_MISO,
.mosi_io_num = PIN_NUM_MOSI,
.sclk_io_num = PIN_NUM_CLK,
.quadwp_io_num = -1,
.quadhd_io_num = -1
};
spi_device_interface_config_t devcfg = {
.clock_speed_hz = 1 * 1000 * 1000,
.mode = 0,
.spics_io_num = PIN_NUM_CS,
.queue_size = 7,
.flags = SPI_DEVICE_NO_DUMMY | SPI_DEVICE_HALFDUPLEX
};
//初始化SPI总线和SPI设备
esp_err_t ret = spi_bus_initialize(HSPI_HOST, &buscfg, SPI_DMA_CHAN);
ESP_ERROR_CHECK(ret);
ret = spi_bus_add_device(HSPI_HOST, &devcfg, &spi);
ESP_ERROR_CHECK(ret);
}
void bmi160_init(void)
{
bmi160_init_sensor();
bmi160_config_sens(BMI160_ACCEL_NORMAL_MODE, BMI160_GYRO_NORMAL_MODE, &spi);
bmi160_set_fifo_down(BMI160_FIFO_DOWN_1);
bmi160_set_acc_range(BMI160_ACCEL_RANGE_2G);
bmi160_set_gyro_range(BMI160_GYRO_RANGE_2000_DPS);
}
void app_main(void)
{
//初始化SD卡和SPI总线
sd_card_init();
spi_master_init();
//初始化BMI160陀螺仪加速度传感器
bmi160_init();
//打开文件
FIL file;
esp_err_t ret = f_open(&file, FILE_NAME, FA_WRITE | FA_OPEN_ALWAYS);
if (ret != ESP_OK) {
printf("Failed to open file %s, error=%d", FILE_NAME, ret);
return;
}
//开始读取FIFO中的数据并写入文件
while (1) {
uint8_t fifo_data[1024];
uint8_t acc_data[6];
uint8_t gyro_data[6];
uint16_t fifo_length;
//读取FIFO数据
bmi160_get_fifo_length(&fifo_length);
if (fifo_length > 0) {
fifo_length = fifo_length > sizeof(fifo_data) ? sizeof(fifo_data) : fifo_length;
bmi160_read_fifo_data(fifo_data, fifo_length, &spi);
//将数据写入文件
for (int i=0; i<fifo_length; i+=12) {
memcpy(acc_data, &fifo_data[i], 6);
memcpy(gyro_data, &fifo_data[i+6], 6);
char buf[128];
sprintf(buf, "%d %d %d %d %d %d\n", (int16_t)(acc_data[1]<<8 | acc_data[0]), (int16_t)(acc_data[3]<<8 | acc_data[2]), (int16_t)(acc_data[5]<<8 | acc_data[4]),
(int16_t)(gyro_data[1]<<8 | gyro_data[0]), (int16_t)(gyro_data[3]<<8 | gyro_data[2]), (int16_t)(gyro_data[5]<<8 | gyro_data[4]));
f_write(&file, buf, strlen(buf), NULL);
}
}
}
//关闭文件
f_close(&file);
}
```
在该示例代码中,我们首先初始化了SD卡驱动和SPI总线,并通过BMI160陀螺仪加速度传感器读取了FIFO中的数据,并将其写入到SD卡的文件中。需要注意的是,在运行该代码前,需要将SD卡插入ESP32的SD卡槽中,并在ESP32上进行正确的引脚连接。
阅读全文