GithubHelp home page GithubHelp logo

stmicroelectronics / asm330lhh-pid Goto Github PK

View Code? Open in Web Editor NEW
8.0 12.0 2.0 128 KB

asm330lhh platform independent driver based on Standard C language and compliant with MISRA standard

License: BSD 3-Clause "New" or "Revised" License

HTML 1.29% CSS 23.21% C 75.50%
mems-sensors stmicroelectronics

asm330lhh-pid's Introduction

1 - Introduction

Sensor driver for ASM330LHH sensor written in C programming language. This repository contains the sensor driver files (.h and .c) to be included, or linked directly as a git submodule, in your project. The driver is MISRA compliant and the documentation can be generated using the Doxygen tool.

In order to clone the complete content of the repository folder, use the command:

git clone https://github.com/STMicroelectronics/ASM330LHH-PID/

Some examples of driver usage can be found here.


2 - Integration details

The driver is platform-independent, you only need to define two functions for read and write transactions from the sensor hardware bus (ie. SPI or I²C). A few devices integrate an extra bit in the communication protocol in order to enable multi read/write access, this bit must be managed in the read and write functions defined by the user. Please refer to the read and write implementation in the reference examples.

2.a Source code integration

  • Include in your project the driver files of the sensor (.h and .c)
  • Define in your code the read and write functions that use the I²C or SPI platform driver like the following:
/** Please note that is MANDATORY: return 0 -> no Error.**/
int32_t platform_write(void *handle, uint8_t Reg, const uint8_t *Bufp, uint16_t len)
int32_t platform_read(void *handle, uint8_t Reg, uint8_t *Bufp, uint16_t len)
  • Declare and initialize the structure of the device interface:
xxxxxxx_ctx_t dev_ctx; /** xxxxxxx is the used part number **/
dev_ctx.write_reg = platform_write;
dev_ctx.read_reg = platform_read;
  • If needed by the platform read and write functions, initialize the handle parameter:
dev_ctx.handle = &platform_handle;

Some integration examples can be found here.

2.b Required properties

  • A standard C language compiler for the target MCU
  • A C library for the target MCU and the desired interface (ie. SPI, I²C)

More Information: http://www.st.com

Copyright (C) 2021 STMicroelectronics

asm330lhh-pid's People

Contributors

avisconti avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

asm330lhh-pid's Issues

Declare register structures as unions with nested structures

Here is updated register data structure that is a union that has a byte variable and anonymous structure:

typedef union
{
    uint8_t buf;

    struct
    {
#if DRV_BYTE_ORDER == DRV_LITTLE_ENDIAN
  uint8_t not_used_01              : 6;
  uint8_t sdo_pu_en                : 1;
  uint8_t not_used_02              : 1;
#elif DRV_BYTE_ORDER == DRV_BIG_ENDIAN
  uint8_t not_used_02              : 1;
  uint8_t sdo_pu_en                : 1;
  uint8_t not_used_01              : 6;
#endif /* DRV_BYTE_ORDER */
    };
} asm330lhh_pin_ctrl_t;

Since the data structure is anonymous you can still access to the structure elements like before:
pin_ctrl.sdo_pu_en = 1;

It is convenient to have an access to the register structure as a byte, when you want to set default value in the register:

asm330lhh_pin_ctrl_t pin_ctrl = {.buf = 0x3f};

also you can use buf as pointer to a register structure, and then you don't need uint8_t type cast:

stc_imu_data_t imu_data[] =
{
    {ASM330LHH_REG_PIN_CTRL,          &pin_ctrl.buf},
    /* Here I would have all of the rest of the IMU registers, */
};

Have all the register macro grouped together instead of with its register data structure definition

It is easier to use and edit register address macro when they are grouped together instead of scattered all over the file. For example I wanted to create an array that countains all of the imu registers:

stc_imu_data_t imu_data[] =
{
    {ASM330LHH_REG_PIN_CTRL,           &pin_ctrl.buf},
    {ASM330LHH_REG_FIFO_CTRL1,         &fifo_ctrl1.buf},
    {ASM330LHH_REG_FIFO_CTRL2,         &fifo_ctrl2.buf},
    {ASM330LHH_REG_FIFO_CTRL3,         &fifo_ctrl3.buf},
    {ASM330LHH_REG_FIFO_CTRL4,         &fifo_ctrl4.buf},
    {ASM330LHH_REG_COUNTER_BDR_REG1,   &counter_bdr_reg1_t.buf},
    {ASM330LHH_REG_COUNTER_BDR_REG2,   &counter_bdr_reg2_t.buf},
    {ASM330LHH_REG_INT1_CTRL,          &int1_ctrl.buf},
    {ASM330LHH_REG_INT2_CTRL,          &int2_ctrl.buf},
    {ASM330LHH_REG_WHO_AM_I,           &who_am_i.buf},
    {ASM330LHH_REG_CTRL1_XL,           &ctrl1_xl.buf},
    {ASM330LHH_REG_CTRL2_G,            &ctrl2_g.buf},
    {ASM330LHH_REG_CTRL3_C,            &ctrl3_c.buf},
    {ASM330LHH_REG_CTRL4_C,            &ctrl4_c.buf},
    {ASM330LHH_REG_CTRL5_C,            &ctrl5_c.buf},
    {ASM330LHH_REG_CTRL6_C,            &ctrl6_c.buf},
    {ASM330LHH_REG_CTRL7_G,            &ctrl7_g.buf},
    {ASM330LHH_REG_CTRL8_XL,           &ctrl8_xl.buf},
    {ASM330LHH_REG_CTRL9_XL,           &ctrl9_xl.buf},
    {ASM330LHH_REG_CTRL10_C,           &ctrl10_c.buf},
    {ASM330LHH_REG_ALL_INT_SRC,        &all_int_src.buf},
    {ASM330LHH_REG_WAKE_UP_SRC,        &wake_up_src.buf},
    {ASM330LHH_REG_TAP_SRC,            &tap_src.buf},
    {ASM330LHH_REG_D6D_SRC,            &d6d_src.buf},
    {ASM330LHH_REG_STATUS_REG,         &status_reg.buf},
    {ASM330LHH_REG_OUT_TEMP_L,         &out_temp_l.buf},
    {ASM330LHH_REG_OUT_TEMP_H,         &out_temp_h.buf},
    {ASM330LHH_REG_OUTX_L_G,           &outx_l_g.buf},
    {ASM330LHH_REG_OUTX_H_G,           &outx_h_g.buf},
    {ASM330LHH_REG_OUTY_L_G,           &outy_l_g.buf},
    {ASM330LHH_REG_OUTY_H_G,           &outy_h_g.buf},
    {ASM330LHH_REG_OUTZ_L_G,           &outz_l_g.buf},
    {ASM330LHH_REG_OUTZ_H_G,           &outz_h_g.buf},
    {ASM330LHH_REG_OUTX_L_A,           &outx_l_a.buf},
    {ASM330LHH_REG_OUTX_H_A,           &outx_h_a.buf},
    {ASM330LHH_REG_OUTY_L_A,           &outy_l_a.buf},
    {ASM330LHH_REG_OUTY_H_A,           &outy_h_a.buf},
    {ASM330LHH_REG_OUTZ_L_A,           &outz_l_a.buf},
    {ASM330LHH_REG_OUTZ_H_A,           &outz_h_a.buf},
    {ASM330LHH_REG_FIFO_STATUS1,       &fifo_status1.buf},
    {ASM330LHH_REG_FIFO_STATUS2,       &fifo_status2.buf},
    {ASM330LHH_REG_TIMESTAMP0,         &timestamp0.buf},
    {ASM330LHH_REG_TIMESTAMP1,         &timestamp1.buf},
    {ASM330LHH_REG_TIMESTAMP2,         &timestamp2.buf},
    {ASM330LHH_REG_TIMESTAMP3,         &timestamp3.buf},
    {ASM330LHH_REG_INT_CFG0,           &int_cfg0_t.buf},
    {ASM330LHH_REG_INT_CFG1,           &int_cfg1_t.buf},
    {ASM330LHH_REG_THS_6D,             &ths_6d_t.buf},
    {ASM330LHH_REG_INT_DUR2,           &int_dur2_t.buf},
    {ASM330LHH_REG_WAKE_UP_THS,        &wake_up_ths_t.buf},
    {ASM330LHH_REG_WAKE_UP_DUR,        &wake_up_dur_t.buf},
    {ASM330LHH_REG_FREE_FALL,          &free_fall_t.buf},
    {ASM330LHH_REG_MD1_CFG,            &md1_cfg_t.buf},
    {ASM330LHH_REG_MD2_CFG,            &md2_cfg_t.buf},
    {ASM330LHH_REG_INTERNAL_FREQ_FINE, &internal_freq_fine.buf},
    {ASM330LHH_REG_X_OFS_USR,          &x_ofs_usr.buf},
    {ASM330LHH_REG_Y_OFS_USR,          &y_ofs_usr.buf},
    {ASM330LHH_REG_Z_OFS_USR,          &z_ofs_usr.buf},
    {ASM330LHH_REG_FIFO_DATA_OUT_TAG,  &fifo_data_out_tag.buf},
    {ASM330LHH_REG_FIFO_DATA_OUT_X_L,  &fifo_data_out_x_l.buf},
    {ASM330LHH_REG_FIFO_DATA_OUT_X_H,  &fifo_data_out_x_h.buf},
    {ASM330LHH_REG_FIFO_DATA_OUT_Y_L,  &fifo_data_out_y_l.buf},
    {ASM330LHH_REG_FIFO_DATA_OUT_Y_H,  &fifo_data_out_y_h.buf},
    {ASM330LHH_REG_FIFO_DATA_OUT_Z_L,  &fifo_data_out_z_l.buf},
    {ASM330LHH_REG_FIFO_DATA_OUT_Z_H,  &fifo_data_out_z_h.buf},
};

It was quite painful to find each macro and copy-paste every single one.

Also in my opinion having all of the register addresses together will help to check if addresses are correct, And edit, like for example I added prefix REG after prefix ASM330LHH. Well I edited that using find/replace function but, still when all of them are in one place I could use find/replace in selected text only instead of being extra careful and find/replace one by one so that I won't make mistake.

Have different names for register addresses defines and register bit fields values

When trying to use one of the register address macros, in application code, after typing IMU component name ASM330LHH, there is 246 hints when there is only 75 registers. This makes it searching for the macro harder than it needs to be.

My suggestion is to have names for macros and names for enum definitions different.

For example if the macro is register address, then adding prefix REG after prefix ASM330LHH would help.

Also I would say that prefix ASM330LHH might be redundant in enums, IMHO that prefix is already in the type name: asm330lhh_odr_xl_t, so I would think that the prefix is not necessary in every enum value.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.