diff --git a/firmware/drivers/tca4311a/tca4311a.c b/firmware/drivers/tca4311a/tca4311a.c index dba3da4..69087c9 100644 --- a/firmware/drivers/tca4311a/tca4311a.c +++ b/firmware/drivers/tca4311a/tca4311a.c @@ -1,7 +1,7 @@ /* * tca4311a.c * - * Copyright (C) 2020, SpaceLab. + * Copyright The EPS 2.0 Contributors. * * This file is part of EPS 2.0. * @@ -16,18 +16,18 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with EPS 2.0. If not, see . + * along with EPS 2.0. If not, see . * */ /** * \brief TCA4311A driver implementation. * - * \authors Gabriel Mariano Marcelino and Vinicius Pimenta Bernardo + * \author Gabriel Mariano Marcelino * - * \version 0.1.7 + * \version 0.2.40 * - * \date 2021/06/05 + * \date 2020/02/01 * * \addtogroup tca4311a * \{ @@ -40,77 +40,163 @@ int tca4311a_init(tca4311a_config_t config, bool en) { - int res_en = gpio_init(config.en_pin, (gpio_config_t){.mode=GPIO_MODE_OUTPUT}); + int res_i2c = i2c_init(config.i2c_port, config.i2c_config); - int res_ready = gpio_init(config.ready_pin, (gpio_config_t){.mode=GPIO_MODE_INPUT}); + gpio_config_t gpio_conf = {0}; - if ((res_en != 0) || (res_ready != 0)) + gpio_conf.mode = GPIO_MODE_OUTPUT; + + int res_en = gpio_init(config.en_pin, gpio_conf); + + gpio_conf.mode = GPIO_MODE_INPUT; + + int res_ready = gpio_init(config.ready_pin, gpio_conf); + + int err = -1; + + if ((res_i2c != 0) || (res_en != 0) || (res_ready != 0)) { - #if CONFIG_DRIVERS_DEBUG_ENABLED == 1 + #if defined(CONFIG_DRIVERS_DEBUG_ENABLED) && (CONFIG_DRIVERS_DEBUG_ENABLED == 1) sys_log_print_event_from_module(SYS_LOG_ERROR, TCA4311A_MODULE_NAME, "Error during the initialization!"); sys_log_new_line(); #endif /* CONFIG_DRIVERS_DEBUG_ENABLED */ - return TCA4311A_ERROR; - } - - if (en) - { - return tca4311a_enable(config); + err = TCA4311A_ERROR; } else { - return tca4311a_disable(config); + if (en) + { + err = tca4311a_enable(config); + } + else + { + err = tca4311a_disable(config); + } } + + return err; } int tca4311a_enable(tca4311a_config_t config) { + int err = TCA4311A_READY; + if (gpio_set_state(config.en_pin, true) != 0) { - #if CONFIG_DRIVERS_DEBUG_ENABLED == 1 + #if defined(CONFIG_DRIVERS_DEBUG_ENABLED) && (CONFIG_DRIVERS_DEBUG_ENABLED == 1) sys_log_print_event_from_module(SYS_LOG_ERROR, TCA4311A_MODULE_NAME, "Error during enable!"); sys_log_new_line(); #endif /* CONFIG_DRIVERS_DEBUG_ENABLED */ - return TCA4311A_ERROR; + err = TCA4311A_ERROR; } - return tca4311a_is_ready(config); + return err; } int tca4311a_disable(tca4311a_config_t config) { + int err = TCA4311A_READY; + if (gpio_set_state(config.en_pin, false) != 0) { - #if CONFIG_DRIVERS_DEBUG_ENABLED == 1 + #if defined(CONFIG_DRIVERS_DEBUG_ENABLED) && (CONFIG_DRIVERS_DEBUG_ENABLED == 1) sys_log_print_event_from_module(SYS_LOG_ERROR, TCA4311A_MODULE_NAME, "Error during disable!"); sys_log_new_line(); #endif /* CONFIG_DRIVERS_DEBUG_ENABLED */ - return TCA4311A_ERROR; + err = TCA4311A_ERROR; } - return tca4311a_is_ready(config); + return err; } int tca4311a_is_ready(tca4311a_config_t config) { + int err = -1; + int result = gpio_get_state(config.ready_pin); if (result == GPIO_STATE_HIGH) { - return TCA4311A_READY; + err = TCA4311A_READY; } else if (result == GPIO_STATE_LOW) { - return TCA4311A_NOT_READY; + err = TCA4311A_NOT_READY; } else { - #if CONFIG_DRIVERS_DEBUG_ENABLED == 1 + #if defined(CONFIG_DRIVERS_DEBUG_ENABLED) && (CONFIG_DRIVERS_DEBUG_ENABLED == 1) sys_log_print_event_from_module(SYS_LOG_ERROR, TCA4311A_MODULE_NAME, "Error reading the state!"); sys_log_new_line(); #endif /* CONFIG_DRIVERS_DEBUG_ENABLED */ - return TCA4311A_ERROR; + err = TCA4311A_ERROR; } + + return err; +} + +int tca4311a_write(tca4311a_config_t config, i2c_slave_adr_t adr, uint8_t *data, uint16_t len) +{ + int err = TCA4311A_READY; + + if (i2c_write(config.i2c_port, adr, data, len) != 0) + { + #if defined(CONFIG_DRIVERS_DEBUG_ENABLED) && (CONFIG_DRIVERS_DEBUG_ENABLED == 1) + sys_log_print_event_from_module(SYS_LOG_ERROR, TCA4311A_MODULE_NAME, "Error during writing!"); + sys_log_new_line(); + #endif /* CONFIG_DRIVERS_DEBUG_ENABLED */ + err = TCA4311A_ERROR; + } + + return err; +} + +int tca4311a_read(tca4311a_config_t config, i2c_slave_adr_t adr, uint8_t *data, uint16_t len) +{ + int err = TCA4311A_READY; + + if (i2c_read(config.i2c_port, adr, data, len) != 0) + { + #if defined(CONFIG_DRIVERS_DEBUG_ENABLED) && (CONFIG_DRIVERS_DEBUG_ENABLED == 1) + sys_log_print_event_from_module(SYS_LOG_ERROR, TCA4311A_MODULE_NAME, "Error during reading!"); + sys_log_new_line(); + #endif /* CONFIG_DRIVERS_DEBUG_ENABLED */ + err = TCA4311A_ERROR; + } + + return err; +} + +int tca4311a_write_byte(tca4311a_config_t config, i2c_slave_adr_t adr, uint8_t byte) +{ + int err = TCA4311A_READY; + + if (i2c_write(config.i2c_port, adr, &byte, 1) != 0) + { + #if defined(CONFIG_DRIVERS_DEBUG_ENABLED) && (CONFIG_DRIVERS_DEBUG_ENABLED == 1) + sys_log_print_event_from_module(SYS_LOG_ERROR, TCA4311A_MODULE_NAME, "Error writing a byte!"); + sys_log_new_line(); + #endif /* CONFIG_DRIVERS_DEBUG_ENABLED */ + err = TCA4311A_ERROR; + } + + return err; +} + +int tca4311a_read_byte(tca4311a_config_t config, i2c_slave_adr_t adr, uint8_t *byte) +{ + int err = TCA4311A_READY; + + if (i2c_read(config.i2c_port, adr, byte, 1) != 0) + { + #if defined(CONFIG_DRIVERS_DEBUG_ENABLED) && (CONFIG_DRIVERS_DEBUG_ENABLED == 1) + sys_log_print_event_from_module(SYS_LOG_ERROR, TCA4311A_MODULE_NAME, "Error reading a byte!"); + sys_log_new_line(); + #endif /* CONFIG_DRIVERS_DEBUG_ENABLED */ + err = TCA4311A_ERROR; + } + + return err; } /** \} End of tca4311a group */ diff --git a/firmware/drivers/tca4311a/tca4311a.h b/firmware/drivers/tca4311a/tca4311a.h index e24f124..bcdf80a 100644 --- a/firmware/drivers/tca4311a/tca4311a.h +++ b/firmware/drivers/tca4311a/tca4311a.h @@ -1,7 +1,7 @@ /* * tca4311a.h * - * Copyright (C) 2020, SpaceLab. + * Copyright The EPS 2.0 Contributors. * * This file is part of EPS 2.0. * @@ -23,11 +23,11 @@ /** * \brief TCA4311A driver definition. * - * \authors Gabriel Mariano Marcelino , Vinicius Pimenta Bernardo and Augusto Cezar Boldori Vassoler + * \author Gabriel Mariano Marcelino * - * \version 0.2.7 + * \version 0.2.40 * - * \date 2021/07/05 + * \date 01/02/2020 * * \defgroup tca4311a TCA4311A * \ingroup drivers @@ -40,7 +40,7 @@ #include #include -#include +#include #include #define TCA4311A_MODULE_NAME "TCA4311A" @@ -50,8 +50,8 @@ */ typedef struct { - i2c_slave_port_t i2c_port; /**< I2C (as slave) port number.*/ - i2c_slave_config_t i2c_config; /**< I2C (as slave) port configuration. */ + i2c_port_t i2c_port; /**< I2C port number.*/ + i2c_config_t i2c_config; /**< I2C port configuration. */ gpio_pin_t en_pin; /**< EN GPIO pin. */ gpio_pin_t ready_pin; /**< READY GPIO pin. */ } tca4311a_config_t; @@ -62,14 +62,14 @@ typedef struct typedef enum { TCA4311A_ERROR=-1, /**< Error during initialization. */ - TCA4311A_READY, /**< The chip is ready. */ - TCA4311A_NOT_READY /**< The chip is not ready. */ + TCA4311A_READY, /**< The chip is not ready. */ + TCA4311A_NOT_READY /**< The chip is ready. */ } tca4311a_status_e; /** * \brief Driver initialization. * - * This function initializes the GPIO pins of EN and READY pins. + * This function initializes the given I2C port and the GPIO pins of EN and READY pins. * * \param[in] config is the configuration parameters of the TCA4311A chip. * @@ -146,6 +146,86 @@ int tca4311a_disable(tca4311a_config_t config); */ int tca4311a_is_ready(tca4311a_config_t config); +/** + * \brief Writes data to a given I2C slave. + * + * \param[in] config is the configuration parameters of the TCA4311A chip. + * + * \param[in] adr is the 7-bit slave address to read. + * + * \param[in] data is the data to write. + * + * \param[in] len is the number of bytes to write. + * + * \return The status of the chip. It can be: + * \parblock + * -\b TCA4311A_ERROR + * -\b TCA4311A_NOT_READY + * -\b TCA4311A_READY + * . + * \endparblock + */ +int tca4311a_write(tca4311a_config_t config, i2c_slave_adr_t adr, uint8_t *data, uint16_t len); + +/** + * \brief Reads data from a given I2C slave. + * + * \param[in] config is the configuration parameters of the TCA4311A chip. + * + * \param[in] adr is the 7-bit slave address to read. + * + * \param[in] data is a pointer to store the read data. + * + * \param[in] len is the number of bytes to read. + * + * \return The status of the chip. It can be: + * \parblock + * -\b TCA4311A_ERROR + * -\b TCA4311A_NOT_READY + * -\b TCA4311A_READY + * . + * \endparblock + */ +int tca4311a_read(tca4311a_config_t config, i2c_slave_adr_t adr, uint8_t *data, uint16_t len); + +/** + * \brief Reads data from a given I2C slave. + * + * \param[in] config is the configuration parameters of the TCA4311A chip. + * + * \param[in] adr is the 7-bit slave address to read. + * + * \param[in] byte is the byte to be written to the slave. + * + * \return The status of the chip. It can be: + * \parblock + * -\b TCA4311A_ERROR + * -\b TCA4311A_NOT_READY + * -\b TCA4311A_READY + * . + * \endparblock + */ +int tca4311a_write_byte(tca4311a_config_t config, i2c_slave_adr_t adr, uint8_t byte); + +/** + * \brief Reads data from a given I2C slave. + * + * \param[in] config is the configuration parameters of the TCA4311A chip. + * + * \param[in] adr is the 7-bit slave address to read. + * + * \param[in] byte is a pointer to store the read byte from the slave. + * + * \return The status of the chip. It can be: + * \parblock + * -\b TCA4311A_ERROR + * -\b TCA4311A_NOT_READY + * -\b TCA4311A_READY + * . + * \endparblock + */ +int tca4311a_read_byte(tca4311a_config_t config, i2c_slave_adr_t adr, uint8_t *byte); + #endif /* TCA4311A_H_ */ /** \} End of tca4311a group */