Merge pull request #10709 from keestux/eliminate-cast-ds18
drivers/ds18: refactor functions with const dev pointer
This commit is contained in:
commit
6767caea5f
@ -30,20 +30,20 @@
|
|||||||
#define ENABLE_DEBUG (0)
|
#define ENABLE_DEBUG (0)
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
|
||||||
static void ds18_low(ds18_t *dev)
|
static void ds18_low(const ds18_t *dev)
|
||||||
{
|
{
|
||||||
/* Set gpio as output and clear pin */
|
/* Set gpio as output and clear pin */
|
||||||
gpio_init(dev->pin, GPIO_OUT);
|
gpio_init(dev->params.pin, GPIO_OUT);
|
||||||
gpio_clear(dev->pin);
|
gpio_clear(dev->params.pin);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ds18_release(ds18_t *dev)
|
static void ds18_release(const ds18_t *dev)
|
||||||
{
|
{
|
||||||
/* Init pin as input */
|
/* Init pin as input */
|
||||||
gpio_init(dev->pin, dev->in_mode);
|
gpio_init(dev->params.pin, dev->params.in_mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ds18_write_bit(ds18_t *dev, uint8_t bit)
|
static void ds18_write_bit(const ds18_t *dev, uint8_t bit)
|
||||||
{
|
{
|
||||||
/* Initiate write slot */
|
/* Initiate write slot */
|
||||||
ds18_low(dev);
|
ds18_low(dev);
|
||||||
@ -59,7 +59,7 @@ static void ds18_write_bit(ds18_t *dev, uint8_t bit)
|
|||||||
xtimer_usleep(1);
|
xtimer_usleep(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ds18_read_bit(ds18_t *dev, uint8_t *bit)
|
static int ds18_read_bit(const ds18_t *dev, uint8_t *bit)
|
||||||
{
|
{
|
||||||
/* Initiate read slot */
|
/* Initiate read slot */
|
||||||
ds18_low(dev);
|
ds18_low(dev);
|
||||||
@ -67,7 +67,7 @@ static int ds18_read_bit(ds18_t *dev, uint8_t *bit)
|
|||||||
|
|
||||||
#if defined(MODULE_DS18_OPTIMIZED)
|
#if defined(MODULE_DS18_OPTIMIZED)
|
||||||
xtimer_usleep(DS18_SAMPLE_TIME);
|
xtimer_usleep(DS18_SAMPLE_TIME);
|
||||||
*bit = gpio_read(dev->pin);
|
*bit = gpio_read(dev->params.pin);
|
||||||
xtimer_usleep(DS18_DELAY_R_RECOVER);
|
xtimer_usleep(DS18_DELAY_R_RECOVER);
|
||||||
return DS18_OK;
|
return DS18_OK;
|
||||||
#else
|
#else
|
||||||
@ -75,7 +75,7 @@ static int ds18_read_bit(ds18_t *dev, uint8_t *bit)
|
|||||||
|
|
||||||
/* Measure time low of device pin, timeout after slot time*/
|
/* Measure time low of device pin, timeout after slot time*/
|
||||||
start = xtimer_now_usec();
|
start = xtimer_now_usec();
|
||||||
while (!gpio_read(dev->pin) && measurement < DS18_DELAY_SLOT) {
|
while (!gpio_read(dev->params.pin) && measurement < DS18_DELAY_SLOT) {
|
||||||
measurement = xtimer_now_usec() - start;
|
measurement = xtimer_now_usec() - start;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,7 +94,7 @@ static int ds18_read_bit(ds18_t *dev, uint8_t *bit)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ds18_read_byte(ds18_t *dev, uint8_t *byte)
|
static int ds18_read_byte(const ds18_t *dev, uint8_t *byte)
|
||||||
{
|
{
|
||||||
uint8_t bit = 0;
|
uint8_t bit = 0;
|
||||||
*byte = 0;
|
*byte = 0;
|
||||||
@ -111,14 +111,14 @@ static int ds18_read_byte(ds18_t *dev, uint8_t *byte)
|
|||||||
return DS18_OK;
|
return DS18_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ds18_write_byte(ds18_t *dev, uint8_t byte)
|
static void ds18_write_byte(const ds18_t *dev, uint8_t byte)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < 8; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
ds18_write_bit(dev, byte & (0x01 << i));
|
ds18_write_bit(dev, byte & (0x01 << i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ds18_reset(ds18_t *dev)
|
static int ds18_reset(const ds18_t *dev)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
@ -131,7 +131,7 @@ static int ds18_reset(ds18_t *dev)
|
|||||||
xtimer_usleep(DS18_DELAY_PRESENCE);
|
xtimer_usleep(DS18_DELAY_PRESENCE);
|
||||||
|
|
||||||
/* Check device presence */
|
/* Check device presence */
|
||||||
res = gpio_read(dev->pin);
|
res = gpio_read(dev->params.pin);
|
||||||
|
|
||||||
/* Sleep for reset delay */
|
/* Sleep for reset delay */
|
||||||
xtimer_usleep(DS18_DELAY_RESET);
|
xtimer_usleep(DS18_DELAY_RESET);
|
||||||
@ -139,7 +139,7 @@ static int ds18_reset(ds18_t *dev)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ds18_trigger(ds18_t *dev)
|
int ds18_trigger(const ds18_t *dev)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
@ -156,7 +156,7 @@ int ds18_trigger(ds18_t *dev)
|
|||||||
return DS18_OK;
|
return DS18_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ds18_read(ds18_t *dev, int16_t *temperature)
|
int ds18_read(const ds18_t *dev, int16_t *temperature)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
uint8_t b1 = 0, b2 = 0;
|
uint8_t b1 = 0, b2 = 0;
|
||||||
@ -190,7 +190,7 @@ int ds18_read(ds18_t *dev, int16_t *temperature)
|
|||||||
return DS18_OK;
|
return DS18_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ds18_get_temperature(ds18_t *dev, int16_t *temperature)
|
int ds18_get_temperature(const ds18_t *dev, int16_t *temperature)
|
||||||
{
|
{
|
||||||
|
|
||||||
DEBUG("[DS18] Convert T\n");
|
DEBUG("[DS18] Convert T\n");
|
||||||
@ -208,14 +208,14 @@ int ds18_init(ds18_t *dev, const ds18_params_t *params)
|
|||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
|
dev->params = *params;
|
||||||
|
|
||||||
/* Deduct the input mode from the output mode. If pull-up resistors are
|
/* Deduct the input mode from the output mode. If pull-up resistors are
|
||||||
* used for output then will be used for input as well. */
|
* used for output then will be used for input as well. */
|
||||||
dev->in_mode = (params->out_mode == GPIO_OD_PU) ? GPIO_IN_PU : GPIO_IN;
|
dev->params.in_mode = (dev->params.out_mode == GPIO_OD_PU) ? GPIO_IN_PU : GPIO_IN;
|
||||||
|
|
||||||
/* Initialize the device and the pin */
|
/* Initialize the device and the pin */
|
||||||
dev->pin = params->pin;
|
res = gpio_init(dev->params.pin, dev->params.in_mode) == 0 ? DS18_OK : DS18_ERROR;
|
||||||
dev->out_mode = params->out_mode;
|
|
||||||
res = gpio_init(dev->pin, dev->in_mode) == 0 ? DS18_OK : DS18_ERROR;
|
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,7 +26,7 @@
|
|||||||
|
|
||||||
static int read_temperature(const void *dev, phydat_t *res)
|
static int read_temperature(const void *dev, phydat_t *res)
|
||||||
{
|
{
|
||||||
if (ds18_get_temperature((ds18_t *)dev, &res->val[0]) == DS18_ERROR) {
|
if (ds18_get_temperature(dev, &res->val[0]) == DS18_ERROR) {
|
||||||
return -ECANCELED;
|
return -ECANCELED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -53,23 +53,22 @@ extern "C" {
|
|||||||
/** @} */
|
/** @} */
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Device descriptor for a ds18 device
|
|
||||||
*/
|
|
||||||
typedef struct {
|
|
||||||
gpio_t pin; /**< Pin the sensor is connected to */
|
|
||||||
gpio_mode_t out_mode; /**< Pin output mode */
|
|
||||||
gpio_mode_t in_mode; /**< Pin input mode */
|
|
||||||
} ds18_t;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Device initialization parameters
|
* @brief Device initialization parameters
|
||||||
*/
|
*/
|
||||||
typedef struct {
|
typedef struct {
|
||||||
gpio_t pin; /**< Pin the sensor is connected to */
|
gpio_t pin; /**< Pin the sensor is connected to */
|
||||||
gpio_mode_t out_mode; /**< Pin output mode */
|
gpio_mode_t out_mode; /**< Pin output mode */
|
||||||
|
gpio_mode_t in_mode; /**< Pin input mode (usually deduced from output mode) */
|
||||||
} ds18_params_t;
|
} ds18_params_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Device descriptor for a ds18 device
|
||||||
|
*/
|
||||||
|
typedef struct {
|
||||||
|
ds18_params_t params; /**< Device Parameters */
|
||||||
|
} ds18_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Initialize a ds18 device
|
* @brief Initialize a ds18 device
|
||||||
*
|
*
|
||||||
@ -91,7 +90,7 @@ int ds18_init(ds18_t *dev, const ds18_params_t *params);
|
|||||||
* @return 0 on success
|
* @return 0 on success
|
||||||
* @return -1 on error
|
* @return -1 on error
|
||||||
*/
|
*/
|
||||||
int ds18_trigger(ds18_t *dev);
|
int ds18_trigger(const ds18_t *dev);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Reads the scratchpad for the last conversion
|
* @brief Reads the scratchpad for the last conversion
|
||||||
@ -102,7 +101,7 @@ int ds18_trigger(ds18_t *dev);
|
|||||||
* @return 0 on success
|
* @return 0 on success
|
||||||
* @return -1 on error
|
* @return -1 on error
|
||||||
*/
|
*/
|
||||||
int ds18_read(ds18_t *dev, int16_t *temperature);
|
int ds18_read(const ds18_t *dev, int16_t *temperature);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief convenience fuction for triggering a conversion and reading the
|
* @brief convenience fuction for triggering a conversion and reading the
|
||||||
@ -117,7 +116,7 @@ int ds18_read(ds18_t *dev, int16_t *temperature);
|
|||||||
* @return 0 on success
|
* @return 0 on success
|
||||||
* @return -1 on error
|
* @return -1 on error
|
||||||
*/
|
*/
|
||||||
int ds18_get_temperature(ds18_t *dev, int16_t *temperature);
|
int ds18_get_temperature(const ds18_t *dev, int16_t *temperature);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -55,7 +55,7 @@ void auto_init_ds18(void)
|
|||||||
|
|
||||||
LOG_DEBUG("[auto_init_saul] initializing ds18 #%u\n", i);
|
LOG_DEBUG("[auto_init_saul] initializing ds18 #%u\n", i);
|
||||||
|
|
||||||
if (ds18_init(&ds18_devs[i], (ds18_params_t *) p) < 0) {
|
if (ds18_init(&ds18_devs[i], p) < 0) {
|
||||||
LOG_ERROR("[auto_init_saul] error initializing ds18 #%u\n", i);
|
LOG_ERROR("[auto_init_saul] error initializing ds18 #%u\n", i);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user