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

drivers/pcf857x: use errno code for error reporting

This makes it easier to use common error reporting such as `strerror()`
or `tiny_strerror()` to give more insight on why something failed.

The custom error codes via `enum` have been updated to be synonymous
with the `errno` codes for backward compatibility.

In addition, `pcf857x_init()` has been updated to no longer or
together the return code, but rather abort on the first fail transaction
and return the error code as is. Otherwise (when both fail due to
different error codes) the returned error code may be garbage.
This commit is contained in:
Marian Buschsieweke 2023-11-22 09:22:37 +01:00
parent 1cdd114e6c
commit e7c9451b55
No known key found for this signature in database
GPG Key ID: 77AA882EC78084E6
2 changed files with 35 additions and 31 deletions

View File

@ -245,10 +245,10 @@ extern "C"
{
#endif
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include "kernel_defines.h"
#include "periph/gpio.h"
#include "periph/i2c.h"
@ -326,14 +326,18 @@ typedef uint8_t pcf857x_data_t; /**< type that can mask all expander pins */
#endif /* MODULE_PCF8575 || DOXYGEN */
/** @} */
/** Definition of PCF857X driver error codes */
/**
* @brief Definition of PCF857X driver error codes
*
* @deprecated These are aliases for errno error codes now, use them directly
*/
typedef enum {
PCF857X_OK, /**< success */
PCF857X_ERROR_I2C, /**< I2C communication error */
PCF857X_ERROR_INV_EXP, /**< invalid expander variant */
PCF857X_ERROR_INV_MODE, /**< invalid pin mode */
PCF857X_ERROR_INV_FLANK, /**< invalid interrupt flank */
PCF857X_ERROR_INT_PIN, /**< interrupt pin initialization failed */
PCF857X_OK = 0, /**< success */
PCF857X_ERROR_I2C = ENXIO, /**< I2C communication error */
PCF857X_ERROR_INV_EXP = ENOTSUP, /**< invalid expander variant */
PCF857X_ERROR_INV_MODE = EINVAL, /**< invalid pin mode */
PCF857X_ERROR_INV_FLANK = EINVAL, /**< invalid interrupt flank */
PCF857X_ERROR_INT_PIN = ENOSYS, /**< interrupt pin initialization failed */
} pcf857x_error_codes_t;
/**
@ -453,9 +457,8 @@ typedef struct {
* has to be defined by the default configuration parameter
* #PCF857X_PARAM_INT_PIN (pcf857x_params_t::int_pin).
*
* @retval PCF857X_OK on success
* @retval PCF857X_ERROR_* a negative error code on error,
* see #pcf857x_error_codes_t
* @retval 0 on success
* @retval <0 a negative errno error code on error
*/
int pcf857x_init(pcf857x_t *dev, const pcf857x_params_t *params);
@ -472,13 +475,12 @@ int pcf857x_init(pcf857x_t *dev, const pcf857x_params_t *params);
* the driver physically supports only the modes #GPIO_IN_PU and
* #GPIO_OD_PU. The other logically identical modes #GPIO_IN, #GPIO_OUT
* and #GPIO_OD are emulated. For the #GPIO_IN_PU mode the function returns
* with #PCF857X_ERROR_INV_MODE.
* with `-EINVAL`.
* - After initialization in #GPIO_OUT mode the pin is actively driven LOW,
* after initialization in all other modes the pin is pulled-up to HIGH.
*
* @retval PCF857X_OK on success
* @retval PCF857X_ERROR_* a negative error code on error,
* see #pcf857x_error_codes_t
* @retval 0 on success
* @retval <0 a negative errno error code on error
*/
int pcf857x_gpio_init(pcf857x_t *dev, gpio_t pin, gpio_mode_t mode);
@ -504,7 +506,7 @@ int pcf857x_gpio_init(pcf857x_t *dev, gpio_t pin, gpio_mode_t mode);
* the driver physically supports only the modes #GPIO_IN_PU and
* #GPIO_OD_PU. The other logically identical modes #GPIO_IN, #GPIO_OUT
* and #GPIO_OD are emulated. For the #GPIO_IN_PU mode the function returns
* with #PCF857X_ERROR_INV_MODE.
* with `-EINVAL`.
* - After initialization in #GPIO_OUT mode the pin is actively driven LOW,
* after initialization in all other modes the pin is pulled-up to HIGH.
*
@ -515,9 +517,8 @@ int pcf857x_gpio_init(pcf857x_t *dev, gpio_t pin, gpio_mode_t mode);
* @param[in] isr ISR that is called back from interrupt context
* @param[in] arg optional argument passed to the callback
*
* @retval PCF857X_OK on success
* @retval PCF857X_ERROR_* a negative error code on error,
* see #pcf857x_error_codes_t
* @retval 0 on success
* @retval <0 a negative errno error code on error
*/
int pcf857x_gpio_init_int(pcf857x_t *dev, gpio_t pin,
gpio_mode_t mode,

View File

@ -14,8 +14,9 @@
* @{
*/
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "pcf857x.h"
@ -104,7 +105,7 @@ int pcf857x_init(pcf857x_t *dev, const pcf857x_params_t *params)
dev->params.addr += PCF8575_BASE_ADDR;
break;
#endif
default: return -PCF857X_ERROR_INV_EXP;
default: return -ENOTSUP;
}
#if IS_USED(MODULE_PCF857X_IRQ)
@ -121,7 +122,7 @@ int pcf857x_init(pcf857x_t *dev, const pcf857x_params_t *params)
/* initialize the interrupt pin */
if (gpio_init_int(dev->params.int_pin,
GPIO_IN_PU, GPIO_FALLING, _irq_isr, (void*)dev)) {
return -PCF857X_ERROR_INT_PIN;
return -ENOSYS;
}
#endif /* MODULE_PCF857X_IRQ */
@ -131,13 +132,15 @@ int pcf857x_init(pcf857x_t *dev, const pcf857x_params_t *params)
/* write 1 to all pins to switch them to INPUTS pulled up to HIGH */
dev->out = ~0;
res |= _write(dev, dev->out);
res = _write(dev, dev->out);
/* initial read all pins */
res |= _read(dev, &dev->in);
if (!res) {
/* initial read all pins */
res = _read(dev, &dev->in);
/* set all pin modes to INPUT and set internal output data to 1 (HIGH) */
dev->modes = ~0;
/* set all pin modes to INPUT and set internal output data to 1 (HIGH) */
dev->modes = ~0;
}
_release(dev);
@ -161,7 +164,7 @@ int pcf857x_gpio_init(pcf857x_t *dev, gpio_t pin, gpio_mode_t mode)
*/
switch (mode) {
case GPIO_IN_PD: DEBUG_DEV("gpio mode GPIO_IN_PD not supported", dev);
return -PCF857X_ERROR_INV_MODE;
return -EINVAL;
case GPIO_OUT: dev->modes &= ~(1 << pin); /* set mode bit to 0 */
dev->out &= ~(1 << pin); /* set output bit to 0 */
break;
@ -228,7 +231,7 @@ int pcf857x_gpio_init_int(pcf857x_t *dev, gpio_t pin,
dev->enabled[pin] = true;
break;
default: DEBUG_DEV("invalid flank %d for pin %d", dev, flank, pin);
return -PCF857X_ERROR_INV_FLANK;
return -EINVAL;
}
return PCF857X_OK;
@ -428,7 +431,7 @@ static int _read(const pcf857x_t *dev, pcf857x_data_t *data)
if (res != 0) {
DEBUG_DEV("could not read data, reason %d (%s)",
dev, res, strerror(res * -1));
return -PCF857X_ERROR_I2C;
return res;
}
if (dev->pin_num == 8) {
@ -469,7 +472,7 @@ static int _write(const pcf857x_t *dev, pcf857x_data_t data)
if (res != 0) {
DEBUG_DEV("could not write data, reason %d (%s)",
dev, res, strerror(res * -1));
return -PCF857X_ERROR_I2C;
return res;
}
return PCF857X_OK;