Skip to content

Commit

Permalink
drivers/lm75: add SAUL integration
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Aug 20, 2021
1 parent be85777 commit 42a5807
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
41 changes: 41 additions & 0 deletions drivers/lm75/lm75_saul.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2021 ML!PA Consulting GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup drivers_lm75
* @{
*
* @file
* @brief SAUL adaption for lm75 compatible device
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*
* @}
*/

#include <string.h>

#include "saul.h"
#include "lm75.h"

static int read_temperature(const void *dev, phydat_t *res)
{
int temperature;

lm75_get_temperature((lm75_t *)dev, &temperature);
res->val[0] = temperature / 10; /* phydat_t is 16 bit */
res->unit = UNIT_TEMP_C;
res->scale = -2;
return 1;
}

const saul_driver_t lm75_temperature_saul_driver = {
.read = read_temperature,
.write = saul_notsup,
.type = SAUL_SENSE_TEMP
};
65 changes: 65 additions & 0 deletions drivers/saul/init_devs/auto_init_lm75.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2021 ML!PA Consulting GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup sys_auto_init_saul
* @{
*
* @file
* @brief Auto initialization of lm75 compatible driver.
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*
* @}
*/

#include "assert.h"
#include "log.h"
#include "saul_reg.h"
#include "lm75.h"
#include "lm75_params.h"

/**
* @brief Define the number of configured sensors
*/
#define LM75_NUMOF ARRAY_SIZE(lm75_params)

/**
* @brief Allocation of memory for device descriptors
*/
static lm75_t lm75_devs[LM75_NUMOF];

/**
* @brief Memory for the SAUL registry entries
*/
static saul_reg_t saul_entries[LM75_NUMOF];

/**
* @brief Reference the driver structs.
*/
extern const saul_driver_t lm75_temperature_saul_driver;

void auto_init_lm75(void)
{
for (unsigned i = 0; i < LM75_NUMOF; i++) {
LOG_DEBUG("[auto_init_saul] initializing lm75 #%u\n", i);

if (lm75_init(&lm75_devs[i], &lm75_params[i]) < 0) {
LOG_ERROR("[auto_init_saul] error initializing lm75 #%u\n", i);
continue;
}

/* temperature */
saul_entries[i].dev = &lm75_devs[i];
saul_entries[i].name = "lm75";
saul_entries[i].driver = &lm75_temperature_saul_driver;

/* register to saul */
saul_reg_add(&(saul_entries[i]));
}
}
4 changes: 4 additions & 0 deletions drivers/saul/init_devs/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ void saul_init_devs(void)
extern void auto_init_lis3mdl(void);
auto_init_lis3mdl();
}
if (IS_USED(MODULE_LM75)) {
extern void auto_init_lm75(void);
auto_init_lm75();
}
if (IS_USED(MODULE_LPSXXX)) {
extern void auto_init_lpsxxx(void);
auto_init_lpsxxx();
Expand Down

0 comments on commit 42a5807

Please sign in to comment.