Skip to content

Commit c8ee55e

Browse files
Merge pull request #585 from sindarin-inc/icm42670-mreg
feat(icm42670): Expose functions for reading/writing registers (BSP-685)
2 parents 9ffc9e5 + 17e7df3 commit c8ee55e

File tree

3 files changed

+151
-1
lines changed

3 files changed

+151
-1
lines changed

components/icm42670/icm42670.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <sys/time.h>
1212
#include "esp_system.h"
1313
#include "esp_check.h"
14+
#include "esp_rom_sys.h"
1415
#include "icm42670.h"
1516

1617
#define I2C_CLK_SPEED 400000
@@ -414,3 +415,66 @@ esp_err_t icm42670_complimentory_filter(icm42670_handle_t sensor, const icm42670
414415

415416
return ESP_OK;
416417
}
418+
419+
esp_err_t icm42670_read_register(icm42670_handle_t sensor, uint8_t reg, uint8_t *val)
420+
{
421+
return icm42670_read(sensor, reg, val, 1);
422+
}
423+
424+
esp_err_t icm42670_write_register(icm42670_handle_t sensor, uint8_t reg, uint8_t val)
425+
{
426+
return icm42670_write(sensor, reg, &val, 1);
427+
}
428+
429+
esp_err_t icm42670_read_mreg_register(icm42670_handle_t sensor, uint8_t mreg, uint8_t reg,
430+
uint8_t *val)
431+
{
432+
uint8_t blk_sel_r = 0;
433+
if (mreg == 1) {
434+
blk_sel_r = 0;
435+
} else if (mreg == 2) {
436+
blk_sel_r = 0x28;
437+
} else if (mreg == 3) {
438+
blk_sel_r = 0x50;
439+
} else {
440+
ESP_LOGE(TAG, "Invalid MREG value %d", mreg);
441+
return ESP_ERR_INVALID_ARG;
442+
}
443+
ESP_RETURN_ON_ERROR(icm42670_write(sensor, ICM42670_BLK_SEL_R, &blk_sel_r, 1), TAG,
444+
"Failed to set BLK_SEL_R");
445+
ESP_RETURN_ON_ERROR(icm42670_write(sensor, ICM42670_MADDR_R, &reg, 1), TAG,
446+
"Failed to set MADDR_R");
447+
448+
esp_rom_delay_us(10);
449+
450+
ESP_RETURN_ON_ERROR(icm42670_read(sensor, ICM42670_M_R, val, 1), TAG, "Failed to read M_R");
451+
452+
esp_rom_delay_us(10);
453+
454+
return ESP_OK;
455+
}
456+
457+
esp_err_t icm42670_write_mreg_register(icm42670_handle_t sensor, uint8_t mreg, uint8_t reg,
458+
uint8_t val)
459+
{
460+
uint8_t blk_sel_w = 0;
461+
if (mreg == 1) {
462+
blk_sel_w = 0;
463+
} else if (mreg == 2) {
464+
blk_sel_w = 0x28;
465+
} else if (mreg == 3) {
466+
blk_sel_w = 0x50;
467+
} else {
468+
ESP_LOGE(TAG, "Invalid MREG value %d", mreg);
469+
return ESP_ERR_INVALID_ARG;
470+
}
471+
ESP_RETURN_ON_ERROR(icm42670_write(sensor, ICM42670_BLK_SEL_W, &blk_sel_w, 1), TAG,
472+
"Failed to set BLK_SEL_W");
473+
ESP_RETURN_ON_ERROR(icm42670_write(sensor, ICM42670_MADDR_W, &reg, 1), TAG,
474+
"Failed to set MADDR_W");
475+
ESP_RETURN_ON_ERROR(icm42670_write(sensor, ICM42670_M_W, &val, 1), TAG, "Failed to set M_W");
476+
477+
esp_rom_delay_us(10);
478+
479+
return ESP_OK;
480+
}

components/icm42670/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "2.0.1"
1+
version: "2.0.2"
22
description: I2C driver for ICM 42670 6-Axis MotionTracking
33
url: https://github.com/espressif/esp-bsp/tree/master/components/icm42670
44
dependencies:

components/icm42670/include/icm42670.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,34 @@ extern "C" {
1515
#define ICM42670_I2C_ADDRESS 0x68 /*!< I2C address with AD0 pin low */
1616
#define ICM42670_I2C_ADDRESS_1 0x69 /*!< I2C address with AD0 pin high */
1717

18+
#define ICM42670_SIGNAL_PATH_RESET 0x02 /*!< Signal path reset */
19+
#define ICM42670_INT_CONFIG 0x06 /*!< Interrupt configuration */
20+
#define ICM42670_PWR_MGMT0 0x1F /*!< Power management 0 */
21+
#define ICM42670_APEX_CONFIG0 0x25 /*!< APEX configuration 0 */
22+
#define ICM42670_APEX_CONFIG1 0x26 /*!< APEX configuration 1 */
23+
#define ICM42670_WOM_CONFIG 0x27 /*!< Wake on Motion configuration */
24+
#define ICM42670_INT_SOURCE0 0x2B /*!< Interrupt source 0 */
25+
#define ICM42670_INT_SOURCE1 0x2C /*!< Interrupt source 1 */
26+
#define ICM42670_INTF_CONFIG0 0x35 /*!< Interface configuration 0 */
27+
#define ICM42670_INTF_CONFIG1 0x36 /*!< Interface configuration 1 */
28+
#define ICM42670_INT_STATUS 0x3A /*!< Interrupt status */
29+
#define ICM42670_INT_STATUS2 0x3B /*!< Interrupt status 2 */
30+
#define ICM42670_INT_STATUS3 0x3C /*!< Interrupt status 3 */
31+
#define ICM42670_BLK_SEL_W 0x79 /*! Select MREG1, MREG2, or MREG3 bank for writing */
32+
#define ICM42670_MADDR_W 0x7A /*! Set MREG* register address for writing */
33+
#define ICM42670_M_W 0x7B /*! Write MREG* register value */
34+
#define ICM42670_BLK_SEL_R 0x7C /*! Select MREG1, MREG2, or MREG3 bank for reading */
35+
#define ICM42670_MADDR_R 0x7D /*! Set MREG* register address for reading */
36+
#define ICM42670_M_R 0x7E /*! Read MREG* register value */
37+
38+
// MREG1 Registers
39+
#define ICM42670_MREG1_INT_CONFIG0 0x04 /*!< Interrupt configuration 0 */
40+
#define ICM42670_MREG1_INT_CONFIG1 0x05 /*!< Interrupt configuration 1 */
41+
#define ICM42670_MREG1_ACCEL_WOM_X_THR 0x4B /*!< WOM X threshold */
42+
#define ICM42670_MREG1_ACCEL_WOM_Y_THR 0x4C /*!< WOM Y threshold */
43+
#define ICM42670_MREG1_ACCEL_WOM_Z_THR 0x4D /*!< WOM Z threshold */
44+
45+
1846
typedef enum {
1947
ACCE_FS_16G = 0, /*!< Accelerometer full scale range is +/- 16g */
2048
ACCE_FS_8G = 1, /*!< Accelerometer full scale range is +/- 8g */
@@ -273,6 +301,64 @@ esp_err_t icm42670_get_temp_value(icm42670_handle_t sensor, float *value);
273301
esp_err_t icm42670_complimentory_filter(icm42670_handle_t sensor, const icm42670_value_t *acce_value,
274302
const icm42670_value_t *gyro_value, complimentary_angle_t *complimentary_angle);
275303

304+
/**
305+
* @brief Read a register
306+
*
307+
* @param sensor object handle of icm42670
308+
* @param reg register address
309+
* @param val value of the register
310+
*
311+
* @return
312+
* - ESP_OK Success
313+
* - ESP_FAIL Fail
314+
*/
315+
esp_err_t icm42670_read_register(icm42670_handle_t sensor, uint8_t reg, uint8_t *val);
316+
317+
/**
318+
* @brief Write to a register
319+
*
320+
* @param sensor object handle of icm42670
321+
* @param reg register address
322+
* @param val value to write
323+
*
324+
* @return
325+
* - ESP_OK Success
326+
* - ESP_FAIL Fail
327+
*/
328+
esp_err_t icm42670_write_register(icm42670_handle_t sensor, uint8_t reg, uint8_t val);
329+
330+
/**
331+
* @brief Read from a MREG register
332+
*
333+
* @param sensor object handle of icm42670
334+
* @param mreg which MREG bank to write (1-3)
335+
* @param reg register address
336+
* @param data data read
337+
*
338+
* @return
339+
* - ESP_OK Success
340+
* - ESP_INVALID_ARG Invalid MREG
341+
* - ESP_FAIL Fail
342+
*/
343+
esp_err_t icm42670_read_mreg_register(icm42670_handle_t sensor, uint8_t mreg, uint8_t reg,
344+
uint8_t *val);
345+
346+
/**
347+
* @brief Write to a MREG register
348+
*
349+
* @param sensor object handle of icm42670
350+
* @param mreg which MREG bank to write (1-3)
351+
* @param reg register address
352+
* @param data data to write
353+
*
354+
* @return
355+
* - ESP_OK Success
356+
* - ESP_INVALID_ARG Invalid MREG
357+
* - ESP_FAIL Fail
358+
*/
359+
esp_err_t icm42670_write_mreg_register(icm42670_handle_t sensor, uint8_t mreg, uint8_t reg,
360+
uint8_t val);
361+
276362
#ifdef __cplusplus
277363
}
278364
#endif

0 commit comments

Comments
 (0)