SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
sensor_bma423.cpp
Go to the documentation of this file.
1
30#include "sdkconfig.h"
31#include "esp_log.h"
32#include "esp_err.h"
33#include "i2c_driver.h"
34#include "freertos/FreeRTOS.h"
35
36#ifdef CONFIG_BMA423
37
38#include "AccelerometerDrv.hpp"
39
40static const char *TAG = "BMA";
41
43
44extern uint32_t hal_callback(SensorCommCustomHal::Operation op, void *param1, void *param2);
45
46static bool init_done = false;
47
48esp_err_t bma423_init()
49{
50 uint8_t address = BMA423_I2C_ADDR_SECONDARY;
51
52 ESP_LOGI(TAG, "----DRIVER BMA423----");
53
54 //* Implemented using read and write callback methods, applicable to other platforms
55#if CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
56
57
58 // * Provide the device address to the callback function
59 i2c_drv_device_init(address);
60
61 ESP_LOGI(TAG, "Implemented using read and write callback methods");
62 if (accel.begin(i2c_wr_function, hal_callback, address)) {
63 ESP_LOGI(TAG, "Initialization of BMA423 accelerometer is successful!");
64 } else {
65 ESP_LOGE(TAG, "Failed to initialize the BMA423 accelerometer!");
66 return ESP_FAIL;
67 }
68#endif
69
70 //* Use the built-in esp-idf communication method
71#if CONFIG_I2C_COMMUNICATION_METHOD_BUILTIN_RW
72#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
73
74 ESP_LOGI(TAG, "Implemented using built-in read and write methods (Use higher version >= 5.0 API)");
75
76 // * Using the new API of esp-idf 5.x, you need to pass the I2C BUS handle,
77 // * which is useful when the bus shares multiple devices.
78 extern i2c_master_bus_handle_t bus_handle;
79
80 if (accel.begin(bus_handle, address)) {
81 ESP_LOGI(TAG, "Initialization of BMA423 accelerometer is successful!");
82 } else {
83 ESP_LOGE(TAG, "Failed to initialize the BMA423 accelerometer!");
84 return ESP_FAIL;
85 }
86
87#else
88
89 ESP_LOGI(TAG, "Implemented using built-in read and write methods (Use lower version < 5.0 API)");
90 if (accel.begin((i2c_port_t)CONFIG_I2C_MASTER_PORT_NUM, address, CONFIG_SENSOR_SDA, CONFIG_SENSOR_SCL)) {
91 ESP_LOGI(TAG, "Initialization of BMA423 accelerometer is successful!");
92 } else {
93 ESP_LOGE(TAG, "Failed to initialize the BMA423 accelerometer!");
94 return ESP_FAIL;
95 }
96#endif //ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)
97#endif //CONFIG_I2C_COMMUNICATION_METHOD_BUILTIN_RW
98
99 init_done = true;
100
101 return ESP_OK;
102}
103
104void bma423_loop()
105{
106 if (!init_done) {
107 return;
108 }
109 ESP_LOGI("BMA423", "Temperature:%.2f*C", accel.getTemperature());
110}
111
112#endif
@license MIT License
uint32_t hal_callback(SensorCommCustomHal::Operation op, void *param1, void *param2)
bool i2c_wr_function(uint8_t addr, uint8_t reg, uint8_t *buf, size_t len, bool writeReg, bool isWrite)
@license MIT License
IMUdata accel[buffer_size]