1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

drivers/io1_xplained: driver cleanup

This commit is contained in:
Alexandre Abadie 2017-02-28 23:11:35 +01:00
parent 1162f2beff
commit 26a17c2e75
5 changed files with 56 additions and 122 deletions

View File

@ -29,42 +29,46 @@ extern "C" {
#endif #endif
/** /**
* @brief Device descriptor for the IO1 Xplained extension. * @brief IO1 Xplained driver return codes
*/ */
typedef struct { enum {
at30tse75x_t temp /**< On-board temperature sensor */; IO1_XPLAINED_OK = 0, /**< Initialization successful */
} io1_xplained_t; IO1_XPLAINED_NOTEMP, /**< Error during temperature sensor initialization */
IO1_XPLAINED_NOLED, /**< Error during extension LED initialization */
IO1_XPLAINED_NOGPIO1, /**< Error during extension GPIO1 initialization */
IO1_XPLAINED_NOGPIO2, /**< Error during extension GPIO2 initialization */
IO1_XPLAINED_READ_OK, /**< Temperature read successful */
IO1_XPLAINED_READ_ERR /**< Error when reading temperature sensor */
};
/** /**
* @brief Device initialization parameters * @brief Device initialization parameters
*/ */
typedef struct { typedef struct {
uint8_t addr; /**< extension custom address */ uint8_t addr; /**< extension custom address */
} io1_xplained_params_t; } io1_xplained_params_t;
/** /**
* @brief export SAUL endpoints * @brief Device descriptor for the IO1 Xplained extension.
* @{
*/ */
extern const saul_driver_t io1_xplained_temperature_saul_driver; typedef struct {
/** @} */ io1_xplained_params_t params; /**< Initialization parameters */
at30tse75x_t temp; /**< On-board temperature sensor */
/** } io1_xplained_t;
* @brief auto-initialize all configured IO1 Xplained extensions
*/
void io1_xplained_auto_init(void);
/** /**
* @brief Initialize the given IO1 Xplained extension * @brief Initialize the given IO1 Xplained extension
* *
* @param[out] dev Initialized device descriptor of IO1 Xplained extension * @param[out] dev Initialized device descriptor of IO1 Xplained extension
* @param[in] addr Custom address of the extension board * @param[in] params Device initialization parameters
* *
* @return 0 on success * @return IO1_XPLAINED_OK on success
* @return -1 if given I2C is not enabled in board config * @return -IO1_XPLAINED_NOTEMP if temperature sensor initialization failed
* @return -IO1_XPLAINED_NOLED if LED initialization failed
* @return -IO1_XPLAINED_NOGPIO1 if GPIO1 initialization failed
* @return -IO1_XPLAINED_NOGPIO2 if GPIO2 initialization failed
*/ */
int io1_xplained_init(io1_xplained_t *dev, uint8_t addr); int io1_xplained_init(io1_xplained_t *dev, const io1_xplained_params_t *params);
/** /**
* @brief Read temperature value from the given IO1 Xplained extension, returned in °C * @brief Read temperature value from the given IO1 Xplained extension, returned in °C
@ -72,34 +76,25 @@ int io1_xplained_init(io1_xplained_t *dev, uint8_t addr);
* @param[in] dev Device descriptor of IO1 Xplained to read from * @param[in] dev Device descriptor of IO1 Xplained to read from
* @param[out] temperature Temperature in °C * @param[out] temperature Temperature in °C
* *
* @return 0 on success * @return IO1_XPLAINED_READ_OK on success
* @return -1 if device's I2C is not enabled in board config * @return -IO1_XPLAINED_READ_ERR if temperature sensor read failed
*/ */
int io1_xplained_read_temperature(io1_xplained_t *dev, float *temperature); int io1_xplained_read_temperature(io1_xplained_t *dev, float *temperature);
/** /**
* @brief Set the on-board led of the IO1 Xplained extension * @brief Set the on-board led of the IO1 Xplained extension
*
* @return 0 on success
* @return -1 if extension GPIO is not enabled in board config
*/ */
int io1_xplained_set_led(void); void io1_xplained_set_led(void);
/** /**
* @brief Clear the on-board led of the IO1 Xplained extension * @brief Clear the on-board led of the IO1 Xplained extension
*
* @return 0 on success
* @return -1 if extension GPIO is not enabled in board config
*/ */
int io1_xplained_clear_led(void); void io1_xplained_clear_led(void);
/** /**
* @brief Toggle the on-board led of the IO1 Xplained extension * @brief Toggle the on-board led of the IO1 Xplained extension
*
* @return 0 on success
* @return -1 if extension GPIO is not enabled in board config
*/ */
int io1_xplained_toggle_led(void); void io1_xplained_toggle_led(void);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -1,3 +1 @@
MODULE = io1_xplained
include $(RIOTBASE)/Makefile.base include $(RIOTBASE)/Makefile.base

View File

@ -21,9 +21,10 @@
#include "board.h" #include "board.h"
#include "io1_xplained.h" #include "io1_xplained.h"
#include "saul.h" #include "io1_xplained_internals.h"
#include "saul_reg.h" #include "saul_reg.h"
#include "saul/periph.h" #include "saul/periph.h"
#include "periph/gpio.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -52,54 +53,28 @@ static const io1_xplained_params_t io1_xplained_params[] =
#endif #endif
}; };
/**
* @brief Get the number of configured IO1 Xplained extension
*/
#define IO1_XPLAINED_NUMOF (sizeof(io1_xplained_params) / sizeof(io1_xplained_params[0]))
/**
* @brief Reference the gpio driver struct
*/
extern saul_driver_t gpio_saul_driver;
#ifdef MODULE_SAUL_REG
/** /**
* @brief Allocate and configure entries to the SAUL registry * @brief Allocate and configure entries to the SAUL registry
*/ */
saul_reg_t io1_xplained_saul_reg[][4] = saul_reg_info_t io1_xplained_saul_reg_info[][4] =
{ {
{ {
{ { .name = "Temperature (IO1 Xplained)" },
.name = "Temperature (IO1 Xplained)", { .name = "LED (IO1 Xplained)" },
.driver = &io1_xplained_temperature_saul_driver { .name = "GPIO1 (IO1 Xplained)" },
}, { .name = "GPIO2 (IO1 Xplained)" }
{
.name = "LED (IO1 Xplained)",
.driver = &gpio_saul_driver
},
{
.name = "GPIO1 (IO1 Xplained)",
.driver = &gpio_saul_driver
},
{
.name = "GPIO2 (IO1 Xplained)",
.driver = &gpio_saul_driver
},
} }
}; };
#endif
#ifdef MODULE_SAUL_GPIO
/** /**
* @brief Allocate and configure the extension LED gpios * @brief Allocate and configure the extension LED gpios
*/ */
static gpio_t io1_xplained_saul_gpios[3] = gpio_t io1_xplained_saul_gpios[3] =
{ {
IO1_LED_PIN, IO1_LED_PIN,
IO1_GPIO1_PIN, IO1_GPIO1_PIN,
IO1_GPIO2_PIN, IO1_GPIO2_PIN,
}; };
#endif
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -18,105 +18,72 @@
* @} * @}
*/ */
#include <math.h>
#include "log.h" #include "log.h"
#include "io1_xplained.h" #include "io1_xplained.h"
#include "io1_xplained_internals.h" #include "io1_xplained_internals.h"
#include "io1_xplained_params.h"
#include "at30tse75x.h" #include "at30tse75x.h"
#include "periph/i2c.h" #include "periph/i2c.h"
#include "periph/gpio.h" #include "periph/gpio.h"
#include "xtimer.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
/**
* @brief Allocation of memory for device descriptors
*/
io1_xplained_t io1_xplained_devs[IO1_XPLAINED_NUMOF];
/*---------------------------------------------------------------------------* /*---------------------------------------------------------------------------*
* IO1 Xplained Core API * * IO1 Xplained Core API *
*---------------------------------------------------------------------------*/ *---------------------------------------------------------------------------*/
int io1_xplained_init(io1_xplained_t *dev, uint8_t addr) int io1_xplained_init(io1_xplained_t *dev, const io1_xplained_params_t *params)
{ {
dev->params = *params;
/* Initialize I2C interface */ /* Initialize I2C interface */
if (at30tse75x_init(&dev->temp, if (at30tse75x_init(&dev->temp,
I2C_DEV(0), I2C_DEV(0),
I2C_SPEED_NORMAL, (TEMPERATURE_BASE_ADDR | addr)) < 0) { I2C_SPEED_NORMAL, (TEMPERATURE_BASE_ADDR | dev->params.addr)) < 0) {
DEBUG("[Error] Cannot initialize temperature sensor.\n"); LOG_ERROR("Cannot initialize temperature sensor.\n");
return -1; return -IO1_XPLAINED_NOTEMP;
} }
/* Use maximum resolution */ /* Use maximum resolution */
at30tse75x_set_resolution(&dev->temp, AT30TSE75X_RESOLUTION_12BIT); at30tse75x_set_resolution(&dev->temp, AT30TSE75X_RESOLUTION_12BIT);
if (gpio_init(IO1_LED_PIN, GPIO_OUT) < 0) { if (gpio_init(IO1_LED_PIN, GPIO_OUT) < 0) {
DEBUG("[Error] GPIO LED not enabled\n"); LOG_ERROR("GPIO LED not enabled\n");
return -1; return -IO1_XPLAINED_NOLED;
} }
if (gpio_init(IO1_GPIO1_PIN, GPIO_OUT) < 0) { if (gpio_init(IO1_GPIO1_PIN, GPIO_OUT) < 0) {
DEBUG("[Error] GPIO1 not enabled\n"); LOG_ERROR("GPIO1 not enabled\n");
return -1; return -IO1_XPLAINED_NOGPIO1;
} }
if (gpio_init(IO1_GPIO2_PIN, GPIO_OUT) < 0) { if (gpio_init(IO1_GPIO2_PIN, GPIO_OUT) < 0) {
DEBUG("[Error] GPIO2 not enabled\n"); LOG_ERROR("GPIO2 not enabled\n");
return -1; return -IO1_XPLAINED_NOGPIO2;
} }
DEBUG("[Info] IO1 Xplained extension initialized!\n"); LOG_DEBUG("IO1 Xplained extension initialized!\n");
return 0; return IO1_XPLAINED_OK;
}
void io1_xplained_auto_init(void)
{
for (unsigned i = 0; i < IO1_XPLAINED_NUMOF; i++) {
if (io1_xplained_init(&io1_xplained_devs[i],
io1_xplained_params[i].addr) < 0) {
LOG_ERROR("Unable to initialize IO1 Xplained #%i\n", i);
}
#ifdef MODULE_SAUL_REG
io1_xplained_saul_reg[i][0].dev = &io1_xplained_devs[i];
saul_reg_add(&io1_xplained_saul_reg[i][0]);
#endif
#ifdef MODULE_SAUL_GPIO
for (unsigned j = 1; j < 4; j++) {
io1_xplained_saul_reg[i][j].dev = &(io1_xplained_saul_gpios[j-1]);
saul_reg_add(&(io1_xplained_saul_reg[i][j]));
}
#endif
}
} }
int io1_xplained_read_temperature(io1_xplained_t *dev, float *temperature) int io1_xplained_read_temperature(io1_xplained_t *dev, float *temperature)
{ {
if (at30tse75x_get_temperature(&dev->temp, temperature) < 0) { if (at30tse75x_get_temperature(&dev->temp, temperature) < 0) {
DEBUG("[Error] Cannot read IO1 Xplained temperatuse sensor.\n"); LOG_ERROR("Cannot read temperature sensor.\n");
return -1; return -IO1_XPLAINED_READ_ERR;
} }
return 0; return IO1_XPLAINED_READ_OK;
} }
int io1_xplained_set_led(void) void io1_xplained_set_led(void)
{ {
gpio_set(IO1_LED_PIN); gpio_set(IO1_LED_PIN);
return 0;
} }
int io1_xplained_clear_led(void) void io1_xplained_clear_led(void)
{ {
gpio_clear(IO1_LED_PIN); gpio_clear(IO1_LED_PIN);
return 0;
} }
int io1_xplained_toggle_led(void) void io1_xplained_toggle_led(void)
{ {
gpio_toggle(IO1_LED_PIN); gpio_toggle(IO1_LED_PIN);
return 0;
} }

View File

@ -22,7 +22,6 @@
#include "saul.h" #include "saul.h"
#include "io1_xplained.h" #include "io1_xplained.h"
#include "xtimer.h"
static float temperature; static float temperature;