Browse Source

drivers/io1_xplained: driver cleanup

master
Alexandre Abadie 6 years ago
parent
commit
26a17c2e75
  1. 61
      drivers/include/io1_xplained.h
  2. 2
      drivers/io1_xplained/Makefile
  3. 41
      drivers/io1_xplained/include/io1_xplained_params.h
  4. 73
      drivers/io1_xplained/io1_xplained.c
  5. 1
      drivers/io1_xplained/io1_xplained_saul.c

61
drivers/include/io1_xplained.h

@ -29,42 +29,46 @@ extern "C" {
#endif
/**
* @brief Device descriptor for the IO1 Xplained extension.
* @brief IO1 Xplained driver return codes
*/
typedef struct {
at30tse75x_t temp /**< On-board temperature sensor */;
} io1_xplained_t;
enum {
IO1_XPLAINED_OK = 0, /**< Initialization successful */
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
*/
typedef struct {
uint8_t addr; /**< extension custom address */
uint8_t addr; /**< extension custom address */
} io1_xplained_params_t;
/**
* @brief export SAUL endpoints
* @{
*/
extern const saul_driver_t io1_xplained_temperature_saul_driver;
/** @} */
/**
* @brief auto-initialize all configured IO1 Xplained extensions
* @brief Device descriptor for the IO1 Xplained extension.
*/
void io1_xplained_auto_init(void);
typedef struct {
io1_xplained_params_t params; /**< Initialization parameters */
at30tse75x_t temp; /**< On-board temperature sensor */
} io1_xplained_t;
/**
* @brief Initialize the given 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 -1 if given I2C is not enabled in board config
* @return IO1_XPLAINED_OK on success
* @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
@ -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[out] temperature Temperature in °C
*
* @return 0 on success
* @return -1 if device's I2C is not enabled in board config
* @return IO1_XPLAINED_READ_OK on success
* @return -IO1_XPLAINED_READ_ERR if temperature sensor read failed
*/
int io1_xplained_read_temperature(io1_xplained_t *dev, float *temperature);
/**
* @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
*
* @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
*
* @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
}

2
drivers/io1_xplained/Makefile

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

41
drivers/io1_xplained/include/io1_xplained_params.h

@ -21,9 +21,10 @@
#include "board.h"
#include "io1_xplained.h"
#include "saul.h"
#include "io1_xplained_internals.h"
#include "saul_reg.h"
#include "saul/periph.h"
#include "periph/gpio.h"
#ifdef __cplusplus
extern "C" {
@ -52,54 +53,28 @@ static const io1_xplained_params_t io1_xplained_params[] =
#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
*/
saul_reg_t io1_xplained_saul_reg[][4] =
saul_reg_info_t io1_xplained_saul_reg_info[][4] =
{
{
{
.name = "Temperature (IO1 Xplained)",
.driver = &io1_xplained_temperature_saul_driver
},
{
.name = "LED (IO1 Xplained)",
.driver = &gpio_saul_driver
},
{
.name = "GPIO1 (IO1 Xplained)",
.driver = &gpio_saul_driver
},
{
.name = "GPIO2 (IO1 Xplained)",
.driver = &gpio_saul_driver
},
{ .name = "Temperature (IO1 Xplained)" },
{ .name = "LED (IO1 Xplained)" },
{ .name = "GPIO1 (IO1 Xplained)" },
{ .name = "GPIO2 (IO1 Xplained)" }
}
};
#endif
#ifdef MODULE_SAUL_GPIO
/**
* @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_GPIO1_PIN,
IO1_GPIO2_PIN,
};
#endif
#ifdef __cplusplus

73
drivers/io1_xplained/io1_xplained.c

@ -18,105 +18,72 @@
* @}
*/
#include <math.h>
#include "log.h"
#include "io1_xplained.h"
#include "io1_xplained_internals.h"
#include "io1_xplained_params.h"
#include "at30tse75x.h"
#include "periph/i2c.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 *
*---------------------------------------------------------------------------*/
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 */
if (at30tse75x_init(&dev->temp,
I2C_DEV(0),
I2C_SPEED_NORMAL, (TEMPERATURE_BASE_ADDR | addr)) < 0) {
DEBUG("[Error] Cannot initialize temperature sensor.\n");
return -1;
I2C_SPEED_NORMAL, (TEMPERATURE_BASE_ADDR | dev->params.addr)) < 0) {
LOG_ERROR("Cannot initialize temperature sensor.\n");
return -IO1_XPLAINED_NOTEMP;
}
/* Use maximum resolution */
at30tse75x_set_resolution(&dev->temp, AT30TSE75X_RESOLUTION_12BIT);
if (gpio_init(IO1_LED_PIN, GPIO_OUT) < 0) {
DEBUG("[Error] GPIO LED not enabled\n");
return -1;
LOG_ERROR("GPIO LED not enabled\n");
return -IO1_XPLAINED_NOLED;
}
if (gpio_init(IO1_GPIO1_PIN, GPIO_OUT) < 0) {
DEBUG("[Error] GPIO1 not enabled\n");
return -1;
LOG_ERROR("GPIO1 not enabled\n");
return -IO1_XPLAINED_NOGPIO1;
}
if (gpio_init(IO1_GPIO2_PIN, GPIO_OUT) < 0) {
DEBUG("[Error] GPIO2 not enabled\n");
return -1;
LOG_ERROR("GPIO2 not enabled\n");
return -IO1_XPLAINED_NOGPIO2;
}
DEBUG("[Info] IO1 Xplained extension initialized!\n");
return 0;
}
LOG_DEBUG("IO1 Xplained extension initialized!\n");
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
}
return IO1_XPLAINED_OK;
}
int io1_xplained_read_temperature(io1_xplained_t *dev, float *temperature)
{
if (at30tse75x_get_temperature(&dev->temp, temperature) < 0) {
DEBUG("[Error] Cannot read IO1 Xplained temperatuse sensor.\n");
return -1;
LOG_ERROR("Cannot read temperature sensor.\n");
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);
return 0;
}
int io1_xplained_clear_led(void)
void io1_xplained_clear_led(void)
{
gpio_clear(IO1_LED_PIN);
return 0;
}
int io1_xplained_toggle_led(void)
void io1_xplained_toggle_led(void)
{
gpio_toggle(IO1_LED_PIN);
return 0;
}

1
drivers/io1_xplained/io1_xplained_saul.c

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

Loading…
Cancel
Save