Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Also, this rule is almost a direct contradiction to the rule described in “Movement of Objects”, but since these are not being treated as objects, I’m allowing it.

All embedded base drivers should be made independent of any HAL or other driver. Instead of relying on a specific HAL, function pointers should be used to allow any HAL to work with the driver

...

Code Block
typedef void (*I2C_WriteFuncPtr)(int device_addr, int reg_addr, int data);
typedef int (*I2C_ReadFuncPtr)(int device_addr, int reg_addr);

typedef struct static{
I2C_WriteFuncPtr local_I2C_Write = NULL;
static
I2C_ReadFuncPtr local_I2C_Read = NULL;
} void lsm6dso_init(I2C_WriteFuncPtr write_func, I2C_ReadFuncPtr read_func)
{
    local_I2C_Write = write_func;
    local_I2C_Read = read_func;
}

t;

lsm6dso_t imu; //DEFINE OBJECT HERE

int lsmdso_read_reg(uint8_t reg)
{
  return local_I2C_Read(...);
}


(main.c)

extern lsm6dso_init(_t imu; // FIND DEFINITION FROM DRIVER
imu = {.I2C_WriteFuncPtr = HAL_i2c_MemWrite, .I2C_ReadFuncPtr = HAL_i2c_MemRead)};

While this is obviously more work, it means that these drivers should succeed for the foreseeable future and for all different chip sets in use right now, without having to change.

Also note the method of updating these values does utilize extern. For devices where more than one of these devices may be present on the bus, have the driver and its APIs have a device select parameter, do not make multiple instances of the device struct.

Info

Note that this requires function signatures that match those of the function pointers defined in the driver. Make these as general as possible, and keep in mind wrappers may need to be made around the HAL in use to ensure their signature is the same