Настройка шины SPI для ICM-42688-P на ESP32 (ESP_IDF)C++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Настройка шины SPI для ICM-42688-P на ESP32 (ESP_IDF)

Сообщение Anonymous »

Сейчас я пытаюсь включить шину SPI на моем ESP32-s3. Однако я не вижу, чтобы линия выбора чипа опускалась на осциллограф, когда я пытаюсь прочитать регистр WHO_AM_I. Более того, я не понимаю, почему для регистра WHO_AM_I указано 40, а должно быть 47. Будем признательны за любые комментарии.
Основная функция:
#include
#include
#include "DK42688_SPI.h"

using namespace std;

#define MOSI GPIO_NUM_11
#define MISO GPIO_NUM_13
#define SCLK GPIO_NUM_12
#define CS GPIO_NUM_10

DK42688_SPI_Config spi_config = {
.miso = MISO,
.mosi = MOSI,
.sclk = SCLK,
.cs = CS
};

extern "C" void app_main(void)
{
DK42688_SPI spi_host = DK42688_SPI(SPI2_HOST, &spi_config);
while(1){
spi_host.begin();
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}

DK42688_SPI.h:
#ifndef DK42688_SPI_H
#define DK42688_SPI_H

#include "register.h"
#include "esp_err.h"
#include "driver/spi_master.h"
#include "esp_log.h"
#include
#include "esp_system.h"

static const char *TAG = "DK42688 SPI Driver";

typedef struct {
int miso;
int mosi;
int sclk;
int cs;
} DK42688_SPI_Config;

class DK42688_SPI {
public:
DK42688_SPI(const spi_host_device_t spi_peripheral, DK42688_SPI_Config *spi_config);
void begin();

enum GyroFS : uint8_t {
dps2000 = 0x00,
dps1000 = 0x01,
dps500 = 0x02,
dps250 = 0x03,
dps125 = 0x04,
dps62_5 = 0x05,
dps31_25 = 0x06,
dps15_625 = 0x07
};
enum AccelFS : uint8_t {
gpm16 = 0x00,
gpm8 = 0x01,
gpm4 = 0x02,
gpm2 = 0x03
};
enum ODR : uint8_t {
odr32k = 0x01, // LN mode
odr16k = 0x02, // LN mode
odr8k = 0x03, // LN mode
odr4k = 0x04, // LN mode
odr2k = 0x05, // LN mode
odr1k = 0x06, // LN mode
odr200 = 0x07, // LP, LN mode
odr100 = 0x08, // LP, LN mode
odr50 = 0x09, // LP, LN mode
odr25 = 0x0A, // LP, LN mode
odr12a5 = 0x0B, // LP, LN mode
odr6a25 = 0x0C, // LP mode
odr3a125 = 0x0D, // LP mode
odr1a5625 = 0x0E, // LP mode
odr500 = 0x0F, // LP, LN mode
};

private:
// SPI configuration parameters
spi_bus_config_t _spi_bus_cfg{};
spi_device_interface_config_t _spi_interface_cfg{};
spi_device_handle_t _handle{};
spi_host_device_t host{};
spi_transaction_t transaction = {}; // define parameters for a single SPI transfer

// SPI functions
esp_err_t transferByte(const uint8_t reg_addr, const uint8_t data, bool readWrite);
uint8_t ReadRegister(const uint8_t reg_addr);
esp_err_t WriteRegister(const uint8_t reg_addr, const uint8_t reg_data);
esp_err_t who_am_i();
};

#endif // DK42688_SPI_H

DK42688_SPI.cpp:
Здесь я пытаюсь прочитать значение регистра в who_am_i().
#include "DK42688_SPI.h"

DK42688_SPI::DK42688_SPI(const spi_host_device_t spi_peripheral, DK42688_SPI_Config *spi_config) {
esp_err_t ret = ESP_OK;
host = spi_peripheral; // Configuring SPI host inside ESP32

transaction.tx_buffer = nullptr; // ptr to the memory buffer to be sent to slave device from ESP32
transaction.rx_buffer = nullptr; // ptr to the memory buffer to be received from slave device to ESP32
_spi_bus_cfg.mosi_io_num = spi_config->mosi; // Master Out Slave In
_spi_bus_cfg.miso_io_num = spi_config->miso; // Master In Slave Out
_spi_bus_cfg.sclk_io_num = spi_config->sclk; // Serial Clock
_spi_bus_cfg.quadwp_io_num = -1; // Quad Write Protect disabled
_spi_bus_cfg.quadhd_io_num = -1; // Quad Hold disabled
_spi_bus_cfg.max_transfer_sz = 100; // Maximum transfer size in bytes

ret = spi_bus_initialize(host, &_spi_bus_cfg, SPI_DMA_CH_AUTO); // Initialize the SPI bus
if (ret != ESP_OK) {
ESP_LOGE(TAG, "spi_bus_initialize failed: %s", esp_err_to_name(ret));
}

_spi_interface_cfg.command_bits = 8;
_spi_interface_cfg.address_bits = 0;
_spi_interface_cfg.dummy_bits = 0;
_spi_interface_cfg.mode = 3;
_spi_interface_cfg.duty_cycle_pos = 128; // 50% duty cycle
_spi_interface_cfg.cs_ena_pretrans = 0; // number of cycles the CS should be active before the transmission
_spi_interface_cfg.cs_ena_posttrans = 0; // number of cycles the CS should be active after the transmission
_spi_interface_cfg.spics_io_num = spi_config->cs;
_spi_interface_cfg.queue_size = 5; // max number of transactions that can be queued
_spi_interface_cfg.pre_cb = NULL;
_spi_interface_cfg.post_cb = NULL;
_spi_interface_cfg.clock_speed_hz = 24000000; // 24 MHz
ret = spi_bus_add_device(host, &_spi_interface_cfg, &_handle); // Add a device to the SPI bus
if (ret != ESP_OK) {
ESP_LOGE(TAG, "spi_bus_add_device failed: %s", esp_err_to_name(ret));
}
}

esp_err_t DK42688_SPI::transferByte(const uint8_t reg_addr, const uint8_t data, bool readWrite) {
transaction.flags = SPI_TRANS_USE_RXDATA | SPI_TRANS_USE_TXDATA;
if(readWrite) transaction.cmd = reg_addr & 0xff;
else transaction.cmd = reg_addr & 0x7f;
// print cmd
ESP_LOGI(TAG, "cmd: %d", transaction.cmd);
transaction.length = 16;
transaction.tx_data[0] = data;

return spi_device_transmit(_handle, &transaction);
}

uint8_t DK42688_SPI::ReadRegister(const uint8_t reg_addr) {
transferByte(reg_addr, 0, true);
return transaction.rx_data[0];
}

esp_err_t DK42688_SPI::WriteRegister(const uint8_t reg_addr, const uint8_t reg_data) {
esp_err_t status{ESP_OK};
status |= transferByte(reg_addr, reg_data, false);
return status;
}

esp_err_t DK42688_SPI::who_am_i() {
uint8_t who_am_i = ReadRegister(ICM42688reg::WHO_AM_I);
if (who_am_i != 0x47) {
ESP_LOGE(TAG, "WHO_AM_I register read failed: %x", who_am_i);
return ESP_FAIL;
}
return ESP_OK;
}

void DK42688_SPI::begin() {
esp_err_t ret = WriteRegister(ICM42688reg::DEVICE_CONFIG, 0x01); // Reset the device
vTaskDelay(100 / portTICK_PERIOD_MS);
if(ret != ESP_OK) {
ESP_LOGE(TAG, "Device reset failed");
}
ret = who_am_i();
if (ret != ESP_OK) {
ESP_LOGE(TAG, "WHO_AM_I register read failed");
}
}



Подробнее здесь: https://stackoverflow.com/questions/783 ... 32-esp-idf
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»