Get Started
To get started with generated driver perform the following actions:
- Unzip the downloaded archive to a preferable location in your project and add them to your build system.
- Include the main driver’s header file. It is named like device_class.h. For example: the name of this file for TMP112 looks like tmp112_temperature_sensors.h
#include "dev_class.h"```
1. Use the Macro to create all the components of the device’s object. This Macro looks like **DEVICE_CLASS_DEVICE_DEFINE**
```C
DEV_CLASS_DEVICE_DEFINE(dev0, "DEV_0")
- Init device’s bus object.
static EMBEDD_RESULT dev0_bus_write(const struct embedd_device_t* dev, const uint8_t* data_ptr, uint32_t data_size);
static EMBEDD_RESULT dev0_bus_read(const struct embedd_device_t* dev, uint8_t* data_ptr, uint32_t data_size);
static embedd_bus_t dev0_bus = { .write = dev0_bus_write, .read = dev0_bus_read };
EMBEDD_RESULT dev0_init()
{
EMBEDD_RESULT res = EMBEDD_RESULT_OK;
dev0.bus = &dev0_bus;
return res;}
EMBEDD_RESULT dev0_bus_write(const struct embedd_device_t* dev, const uint8_t* data_ptr, uint32_t data_size)
{
if( ( dev == NULL ) || ( data_ptr == NULL ) )
{
return EMBEDD_RESULT_ERR;
}
return EMBEDD_RESULT_OK;
}
EMBEDD_RESULT dev0_bus_read(const struct embedd_device_t* dev, uint8_t* data_ptr, uint32_t data_size)
{
if( ( dev == NULL ) || ( data_ptr == NULL ) )
{
return EMBEDD_RESULT_ERR;
}
return EMBEDD_RESULT_OK;
}
- Check all “weak” functions and add implementation if it is needed.
void embedd_hal_sleep( uint32_t mseconds )
{
HAL_Delay( mseconds );
}
Device Usage
Registers
Registers are small storage locations that manage specific functions or data within the device. Each register has a unique address that allows it to be accessed and modified. These registers can store configuration settings, calibration data, or measurement outputs, controlling device behavior and facilitating communication between the peripheral device and the processor it’s connected to. They play a crucial role in setting operational modes, initiating actions, and reading sensor data, effectively acting as the interface for the internal state and operations of the device.
The file device_class_registers.h provides information about all the reagisters a device has. Let’s take HDC2080 as shown in the example, and write data to the HUM_OFFSET_ADJUST register and then read it back. The file hdc2080_humidity_moisture_sensors_data_types.h contains all the information about the type of data a register contains. Here is the information about data type contained in HUM_OFFSET_ADJUST register:
typedef int8_t hdc2080_offset_adjust_t;
#define HDC2080_OFFSET_ADJUST_02RH 0
#define HDC2080_OFFSET_ADJUST_04RH 1
#define HDC2080_OFFSET_ADJUST_08RH 2
#define HDC2080_OFFSET_ADJUST_16RH 3
#define HDC2080_OFFSET_ADJUST_31RH 4
#define HDC2080_OFFSET_ADJUST_63RH 5
#define HDC2080_OFFSET_ADJUST_125RH 6
#define HDC2080_OFFSET_ADJUST_25RH 7
#define HDC2080_OFFSET_ADJUST_DEFAULT 0
#define HDC2080_OFFSET_ADJUST_VALID(val) (\
(val) == HDC2080_OFFSET_ADJUST_02RH ||\
(val) == HDC2080_OFFSET_ADJUST_04RH ||\
(val) == HDC2080_OFFSET_ADJUST_08RH ||\
(val) == HDC2080_OFFSET_ADJUST_16RH ||\
(val) == HDC2080_OFFSET_ADJUST_31RH ||\
(val) == HDC2080_OFFSET_ADJUST_63RH ||\
(val) == HDC2080_OFFSET_ADJUST_125RH ||\
(val) == HDC2080_OFFSET_ADJUST_25RH ||\ 0)
The file hdc2080_humidity_moisture_sensors_registers.h contains all the information about the registers device has. Here is information about HUM_OFFSET_ADJUST register and its declaration:
#define hdc2080_offset_adjust_read_reg_addr 0x09
#define hdc2080_offset_adjust_write_reg_addr 0x09
#define hdc2080_offset_adjust_delay 0
typedef hdc2080_offset_adjust_t hdc2080_offset_adjust;
Here are the Macros providing an access to the device’s registers:
* \brief Write data to the register
*
* \param dev pointer to the device whose register should be written with data
* \param _typename register type
* \param var variable containing data to be written
* \return EMBEDD_RESULT operation result code
*/
#define HDC2080_HUMIDITY_MOISTURE_SENSORS_WRITE_REG(dev, _typename, var) \
((hdc2080_humidity_moisture_sensors_api_t*)(dev).api)->hdc2080_humidity_moisture_sensors_write_reg(&(dev), \
_typename##_write_reg_addr, &(var), sizeof(_typename), _typename##_delay)
* \brief Read data from the register
*
* \param dev pointer to the device from whose register data should be read
* \param _typename register type
* \param var Variable into which the data from the register will be copied
* \return EMBEDD_RESULT operation result code
*/
#define HDC2080_HUMIDITY_MOISTURE_SENSORS_READ_REG(dev, _typename, var) \
((hdc2080_humidity_moisture_sensors_api_t*)(dev).api)->hdc2080_humidity_moisture_sensors_read_reg(&(dev), \
_typename##_read_reg_addr, &(var), sizeof(_typename), _typename##_delay)
Finally, to demonstrate writing to and reading from the HUM_OFFSET_ADJUST register, consider the following C code snippet:
extern embedd_device_t hdc2080;
bool set_humidity_offset( void ) {
hdc2080_offset_adjust_t humidity_offset = HDC2080_OFFSET_ADJUST_04RH;
HDC2080_HUMIDITY_MOISTURE_SENSORS_WRITE_REG( &hdc2080, hdc2080_offset_adjust, humidity_offset);
hdc2080_offset_adjust_t read_back_offset;
HDC2080_HUMIDITY_MOISTURE_SENSORS_READ_REG( &hdc2080, hdc2080_offset_adjust, read_back_offset);
return (read_back_offset == humidity_offset) ? true : false;
}