From 0a2de608c9b9c067f016086a9d4cbe36cdb08641 Mon Sep 17 00:00:00 2001 From: Pierre Le Meur Date: Wed, 13 Nov 2024 09:59:45 +0100 Subject: [PATCH 01/10] Create driver and example for SHA5461AS --- drivers/include/sha5461as.h | 170 +++++++++++++++++++ drivers/sha5461as/Makefile | 1 + drivers/sha5461as/Makefile.dep | 2 + drivers/sha5461as/Makefile.include | 2 + drivers/sha5461as/include/sha5461as_params.h | 103 +++++++++++ drivers/sha5461as/sha5461as.c | 166 ++++++++++++++++++ tests/drivers/sha5461as/Makefile | 7 + tests/drivers/sha5461as/README.md | 17 ++ tests/drivers/sha5461as/main.c | 85 ++++++++++ 9 files changed, 553 insertions(+) create mode 100644 drivers/include/sha5461as.h create mode 100644 drivers/sha5461as/Makefile create mode 100644 drivers/sha5461as/Makefile.dep create mode 100644 drivers/sha5461as/Makefile.include create mode 100644 drivers/sha5461as/include/sha5461as_params.h create mode 100644 drivers/sha5461as/sha5461as.c create mode 100644 tests/drivers/sha5461as/Makefile create mode 100644 tests/drivers/sha5461as/README.md create mode 100644 tests/drivers/sha5461as/main.c diff --git a/drivers/include/sha5461as.h b/drivers/include/sha5461as.h new file mode 100644 index 000000000000..b421670b0ecc --- /dev/null +++ b/drivers/include/sha5461as.h @@ -0,0 +1,170 @@ +/* + * Copyright (C) 2024 Orange + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @defgroup drivers_sha5461as SHA5461AS + * @ingroup drivers_display + * @brief Device driver interface for the SHA5461AS 7 segments display + * @{ + * + * @file + * @brief Device driver interface for the SHA5461AS 7 segments display + * + * @author Pierre Le Meur + */ + +#ifndef SHA5461AS_H +#define SHA5461AS_H + +#include + +#include "periph/gpio.h" +#include "macros/units.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define SHA5461AS_MAX_DIGITS (4) /**< Max pin number, can be below or equal */ +#ifndef XTIMER_DEV +#define XTIMER_DEV TIMER_DEV(0) /**< default timer */ +#endif +#ifndef XTIMER_HZ +#define XTIMER_HZ MHZ(1) /**< default timer hz */ +#endif +#ifndef SHA5461AS_DELAY +#define SHA5461AS_DELAY (XTIMER_HZ/2600) /**< with 1MHz and a cycle as 2600 we got 2.6ms each shift, so + undetectable frequency by human eyes*/ +#endif +#define TIMER_RUNNING (1) /**< status when timer is running */ +#define TIMER_STOPPED (0) /**< status when timer is stopped */ + +/** + * @brief Return codes + */ +enum { + SHA5461AS_OK = 0, /**< All ok */ + SHA5461AS_ERR_A_GPIO, /**< Something went wrong with A GPIO */ + SHA5461AS_ERR_B_GPIO, /**< Something went wrong with B GPIO */ + SHA5461AS_ERR_C_GPIO, /**< Something went wrong with C GPIO */ + SHA5461AS_ERR_D_GPIO, /**< Something went wrong with D GPIO */ + SHA5461AS_ERR_E_GPIO, /**< Something went wrong with E GPIO */ + SHA5461AS_ERR_F_GPIO, /**< Something went wrong with F GPIO */ + SHA5461AS_ERR_G_GPIO, /**< Something went wrong with G GPIO */ + SHA5461AS_ERR_DP_GPIO, /**< Something went wrong with DP GPIO */ + SHA5461AS_ERR_DIG1_GPIO, /**< Something went wrong with DIG1 GPIO */ + SHA5461AS_ERR_DIG2_GPIO, /**< Something went wrong with DIG2 GPIO */ + SHA5461AS_ERR_DIG3_GPIO, /**< Something went wrong with DIG3 GPIO */ + SHA5461AS_ERR_DIG4_GPIO, /**< Something went wrong with DIG4 GPIO */ + SHA5461AS_ERR_DIGITS, /**< Something went wrong with digits value */ +}; + +/** + * @brief Device initialization parameters + */ +typedef struct { + gpio_t a; /**< Data input pin */ + gpio_t b; /**< Data input pin */ + gpio_t c; /**< Data input pin */ + gpio_t d; /**< Data input pin */ + gpio_t e; /**< Data input pin */ + gpio_t f; /**< Data input pin */ + gpio_t g; /**< Data input pin */ + gpio_t dp; /**< Data input pin */ + gpio_t dig1; /**< Data input pin */ + gpio_t dig2; /**< Data input pin */ + gpio_t dig3; /**< Data input pin */ + gpio_t dig4; /**< Data input pin */ +} sha5461as_params_t; + +/** + * @brief Device descriptor for the SHA5461AS + */ +typedef struct { + sha5461as_params_t params; /**< Device parameters */ + uint32_t value; /**< Binary value to display */ + uint8_t digits; /**< Number of digits */ + uint8_t current_digit; /**< Displayed digit */ + int status; /**< Status of the timer_periodic for this dev */ +} sha5461as_t; + +/** + * @brief Initialize the given DSP0401 + * + * @param[out] dev Initialized device descriptor of SHA5461AS device + * @param[in] params Device parameters to use + * + * @return SHA5461AS_OK if everything is good + * @return -SHA5461AS_ERR_A_GPIO if an error occured during A GPIO + * @return -SHA5461AS_ERR_B_GPIO if an error occured during B GPIO + * @return -SHA5461AS_ERR_C_GPIO if an error occured during C GPIO + * @return -SHA5461AS_ERR_D_GPIO if an error occured during D GPIO + * @return -SHA5461AS_ERR_E_GPIO if an error occured during E GPIO + * @return -SHA5461AS_ERR_F_GPIO if an error occured during F GPIO + * @return -SHA5461AS_ERR_G_GPIO if an error occured during G GPIO + * @return -SHA5461AS_ERR_DP_GPIO if an error occured during DP GPIO + * @return -SHA5461AS_ERR_DIG1_GPIO if an error occured during DIG1 GPIO + * @return -SHA5461AS_ERR_DIG2_GPIO if an error occured during DIG2 GPIO + * @return -SHA5461AS_ERR_DIG3_GPIO if an error occured during DIG3 GPIO + * @return -SHA5461AS_ERR_DIG4_GPIO if an error occured during DIG4 GPIO + */ +int sha5461as_init(sha5461as_t* dev, const sha5461as_params_t* params); + +/** + * @brief Shift the current digit off and the next one on + * + * @param[in] dev Initialized device descriptor of SHA5461AS device + * + */ +int sha5461as_shift(sha5461as_t* dev); + +/** + * @brief Set the value for every digit + * + * @param[in] dev Initialized device descriptor of SHA5461AS device + * @param[in] index the digit number between [0, SHA5461AS_DIGITS[ + * @param[in] value the value as an @ref uint32_t + * + */ +int sha5461as_set_all_value(sha5461as_t* dev, uint32_t value); + +/** + * @brief Set the value for one digit + * + * @param[in] dev Initialized device descriptor of SHA5461AS device + * @param[in] index the digit number between [0, SHA5461AS_DIGITS[ + * @param[in] value the value as an @ref uint8_t, A is equal to the first bit + * B the second.... DP the 8th bit. + * + */ +int sha5461as_set_value(sha5461as_t* dev, int index, uint8_t value); + +/** + * @brief Set the value for one digit + * + * @param[in] dev Initialized device descriptor of SHA5461AS device + * @param[in] index the digit number between [0, SHA5461AS_DIGITS[ + * @param[in] value the value as an @ref uint8_t, A is equal to the first bit + * B the second.... DP the 8th bit. + * + */ +int sha5461as_set_value(sha5461as_t* dev, int index, uint8_t value); + +/** + * @brief Start/stop an periodic timer event to shift between each 7seg + * + * @param[in] dev Initialized device descriptor of SHA5461AS device + */ +int sha5461as_display(sha5461as_t* dev); + +#ifdef __cplusplus +} +#endif + +#endif /* SHA5461AS_H */ +/** @} */ diff --git a/drivers/sha5461as/Makefile b/drivers/sha5461as/Makefile new file mode 100644 index 000000000000..48422e909a47 --- /dev/null +++ b/drivers/sha5461as/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/drivers/sha5461as/Makefile.dep b/drivers/sha5461as/Makefile.dep new file mode 100644 index 000000000000..8934cbbd2278 --- /dev/null +++ b/drivers/sha5461as/Makefile.dep @@ -0,0 +1,2 @@ +FEATURES_REQUIRED += periph_gpio +FEATURES_REQUIRED += periph_timer_periodic diff --git a/drivers/sha5461as/Makefile.include b/drivers/sha5461as/Makefile.include new file mode 100644 index 000000000000..268a19379bba --- /dev/null +++ b/drivers/sha5461as/Makefile.include @@ -0,0 +1,2 @@ +USEMODULE_INCLUDES_sha5461as := $(LAST_MAKEFILEDIR)/include +USEMODULE_INCLUDES += $(USEMODULE_INCLUDES_sha5461as) diff --git a/drivers/sha5461as/include/sha5461as_params.h b/drivers/sha5461as/include/sha5461as_params.h new file mode 100644 index 000000000000..425633cb39f2 --- /dev/null +++ b/drivers/sha5461as/include/sha5461as_params.h @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2024 Orange + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup drivers_sha5461as + * @{ + * + * @file + * @brief Default configuration for SHA5461AS + * + * @author Pierre Le Meur + * + */ + +#ifndef SHA5461AS_PARAMS_H +#define SHA5461AS_PARAMS_H + +#include "sha5461as.h" +#include "periph/gpio.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * @name Set default configuration parameters for the SHA5461AS (for arduino-nano-33-ble) + * @{ + */ +#ifndef SHA5461AS_PARAM_A_PIN +#define SHA5461AS_PARAM_A_PIN GPIO_PIN(1,11) /* D2 */ +#endif +#ifndef SHA5461AS_PARAM_B_PIN +#define SHA5461AS_PARAM_B_PIN GPIO_PIN(1,12) /* D3 */ +#endif +#ifndef SHA5461AS_PARAM_C_PIN +#define SHA5461AS_PARAM_C_PIN GPIO_PIN(1,15) /* D4 */ +#endif +#ifndef SHA5461AS_PARAM_D_PIN +#define SHA5461AS_PARAM_D_PIN GPIO_PIN(1,13) /* D5 */ +#endif +#ifndef SHA5461AS_PARAM_E_PIN +#define SHA5461AS_PARAM_E_PIN GPIO_PIN(1,14) /* D6 */ +#endif +#ifndef SHA5461AS_PARAM_F_PIN +#define SHA5461AS_PARAM_F_PIN GPIO_PIN(0,23) /* D7 */ +#endif +#ifndef SHA5461AS_PARAM_G_PIN +#define SHA5461AS_PARAM_G_PIN GPIO_PIN(0,21) /* D8 */ +#endif +#ifndef SHA5461AS_PARAM_DP_PIN +#define SHA5461AS_PARAM_DP_PIN GPIO_PIN(0,27) /* D9 */ +#endif + +#ifndef SHA5461AS_PARAM_DIG1_PIN +#define SHA5461AS_PARAM_DIG1_PIN GPIO_PIN(1,2) /* D10 */ +#endif +#ifndef SHA5461AS_PARAM_DIG2_PIN +#define SHA5461AS_PARAM_DIG2_PIN GPIO_PIN(1,1) /* D11 */ +#endif +#ifndef SHA5461AS_PARAM_DIG3_PIN +#define SHA5461AS_PARAM_DIG3_PIN GPIO_PIN(1,8) /* D12 */ +#endif +#ifndef SHA5461AS_PARAM_DIG4_PIN +#define SHA5461AS_PARAM_DIG4_PIN GPIO_PIN(0,13) /* D13 */ +#endif + +#ifndef SHA5461AS_PARAMS +#define SHA5461AS_PARAMS { .a = SHA5461AS_PARAM_A_PIN, \ + .b = SHA5461AS_PARAM_B_PIN, \ + .c = SHA5461AS_PARAM_C_PIN, \ + .d = SHA5461AS_PARAM_D_PIN, \ + .e = SHA5461AS_PARAM_E_PIN, \ + .f = SHA5461AS_PARAM_F_PIN, \ + .g = SHA5461AS_PARAM_G_PIN, \ + .dp = SHA5461AS_PARAM_DP_PIN, \ + .dig1 = SHA5461AS_PARAM_DIG1_PIN, \ + .dig2 = SHA5461AS_PARAM_DIG2_PIN, \ + .dig3 = SHA5461AS_PARAM_DIG3_PIN, \ + .dig4 = SHA5461AS_PARAM_DIG4_PIN } + +#endif +/**@}*/ + +/** + * @brief Configure SHA5461AS + */ +static const sha5461as_params_t sha5461as_params[] = +{ + SHA5461AS_PARAMS, +}; + +#ifdef __cplusplus +} +#endif + +#endif /* SHA5461AS_PARAMS_H */ +/** @} */ diff --git a/drivers/sha5461as/sha5461as.c b/drivers/sha5461as/sha5461as.c new file mode 100644 index 000000000000..73608da6f3ec --- /dev/null +++ b/drivers/sha5461as/sha5461as.c @@ -0,0 +1,166 @@ +/* + * Copyright (C) 2024 Orange + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup drivers_sha5461as + * @{ + * + * @file + * @brief Device driver interface for the SHA5461AS 7 segments display + * + * @author Pierre Le Meur + * + * @} + */ +#include + +#include "sha5461as.h" + +#include "board.h" +#include "periph/gpio.h" +#include "periph/timer.h" + +#define ENABLE_DEBUG 0 +#include "debug.h" + +#define PIN_A (dev->params.a) +#define PIN_B (dev->params.b) +#define PIN_C (dev->params.c) +#define PIN_D (dev->params.d) +#define PIN_E (dev->params.e) +#define PIN_F (dev->params.f) +#define PIN_G (dev->params.g) +#define PIN_DP (dev->params.dp) +#define PIN_DIG1 (dev->params.dig1) +#define PIN_DIG2 (dev->params.dig2) +#define PIN_DIG3 (dev->params.dig3) +#define PIN_DIG4 (dev->params.dig4) + +#define NB_PIN (12) + +static void _set_pin_value(sha5461as_t* dev) +{ + const gpio_t pins[] = + { + PIN_A, PIN_B, PIN_C, PIN_D, PIN_E, PIN_F, PIN_G, PIN_DP + }; + + uint8_t mask = 0xFF; + uint8_t current_value = (uint8_t)(dev->value >> (dev->current_digit * 8) & mask); + + for ( int i = 1; i <= 8; i++) + { + if (current_value & (1 << (8 - i))) + { + gpio_set(pins[i-1]); + DEBUG("PIN SET\n"); + } else + { + gpio_clear(pins[i-1]); + DEBUG("PIN CLEAR\n"); + } + } +} + +static void _shift_display(void* arg, int chan) +{ + (void) chan; + sha5461as_t* dev = (sha5461as_t*)arg; + + const gpio_t digit_pins[] = + { + PIN_DIG1, PIN_DIG2, PIN_DIG3, PIN_DIG4 + }; + + gpio_clear(digit_pins[dev->current_digit]); + dev->current_digit += 1; + dev->current_digit = dev->current_digit % dev->digits; + gpio_set(digit_pins[dev->current_digit]); + + _set_pin_value(dev); +} + +int sha5461as_init(sha5461as_t* dev, const sha5461as_params_t* params) +{ + dev->params = *params; + dev->current_digit = 0; + + if (dev->digits <= 0 || dev->digits > SHA5461AS_MAX_DIGITS) + { + return -SHA5461AS_ERR_DIGITS; + } + + const gpio_t pins[] = + { + PIN_A, PIN_B, PIN_C, PIN_D, PIN_E, PIN_F, PIN_G, PIN_DP, + PIN_DIG1, PIN_DIG2, PIN_DIG3, PIN_DIG4 + }; + + const int pin_errs[] = + { + SHA5461AS_ERR_A_GPIO, SHA5461AS_ERR_B_GPIO, SHA5461AS_ERR_C_GPIO, SHA5461AS_ERR_D_GPIO, + SHA5461AS_ERR_E_GPIO, SHA5461AS_ERR_F_GPIO, SHA5461AS_ERR_G_GPIO, SHA5461AS_ERR_DP_GPIO, + SHA5461AS_ERR_DIG1_GPIO, SHA5461AS_ERR_DIG2_GPIO, SHA5461AS_ERR_DIG3_GPIO, + SHA5461AS_ERR_DIG4_GPIO + }; + + for (int i = 0; i < NB_PIN; i++) + { + if (gpio_init(pins[i], GPIO_OUT) < 0) + { + DEBUG("[Error] Initializing error"); + return pin_errs[i]; + } + gpio_clear(pins[i]); + } + + return SHA5461AS_OK; +} + +int sha5461as_shift(sha5461as_t* dev) +{ + _shift_display(dev, 0); + return 1; +} + +int sha5461as_set_all_value(sha5461as_t* dev, uint32_t value) +{ + dev->value = value; + return 1; +} + +int sha5461as_set_value(sha5461as_t* dev, int index, uint8_t value) +{ + value <<= (index * 8); + uint32_t up_value = dev->value >> ((index + 1) * 8); + uint32_t down_value = ((0b00000001 << (index * 8)) - 1) & dev->value; + + dev->value = up_value | value | down_value; + + return 1; +} + +int sha5461as_display(sha5461as_t* dev) +{ + if ( dev->status == TIMER_RUNNING ) + { + timer_stop(XTIMER_DEV); + dev->status = TIMER_STOPPED; + return 0; + } + + if ( timer_init(XTIMER_DEV, XTIMER_HZ, _shift_display, dev) != 0 ) + { + return -1; + } + + timer_set_periodic(XTIMER_DEV, 0, SHA5461AS_DELAY, TIM_FLAG_RESET_ON_MATCH); + + dev->status = TIMER_RUNNING; + return 0; +} diff --git a/tests/drivers/sha5461as/Makefile b/tests/drivers/sha5461as/Makefile new file mode 100644 index 000000000000..8e4267ef52ce --- /dev/null +++ b/tests/drivers/sha5461as/Makefile @@ -0,0 +1,7 @@ +include ../Makefile.drivers_common + +USEMODULE += sha5461as +USEMODULE += ztimer +USEMODULE += ztimer_usec + +include $(RIOTBASE)/Makefile.include diff --git a/tests/drivers/sha5461as/README.md b/tests/drivers/sha5461as/README.md new file mode 100644 index 000000000000..3f808b48c968 --- /dev/null +++ b/tests/drivers/sha5461as/README.md @@ -0,0 +1,17 @@ +## About + +This is a test application for the [SHA5461AS](https://www.datasheetcafe.com/wp-content/uploads/2019/01/SH5461AS-datasheet.pdf), +a 4 digits with 7 segments display. + +## Connecting the device + +The display needs to be connected as follows: + +- Connect all the pins `X` to the board `SH5461AS_PARAM_X_PIN` +- Connect `DP` to the board `SHA5461AS_PARAM_DP_PIN` +- Connect all the pins `DIGX` to the board `SHA5461AS_PARAM_DIGX_PIN` + +For each digit pin (`DIGX`), you need to use a transistor and link it to `GND`. + +![datasheet image](https://www.datasheetcafe.com/wp-content/uploads/2015/09/SH5461AS.gif) + diff --git a/tests/drivers/sha5461as/main.c b/tests/drivers/sha5461as/main.c new file mode 100644 index 000000000000..445783dd0fa2 --- /dev/null +++ b/tests/drivers/sha5461as/main.c @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2024 Orange + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief Test application for the SHT5461AS 4x7seg + * + * @author Pierre Le Meur + * + * @} + */ + + +#include +#include + +#include "sha5461as_params.h" +#include "sha5461as.h" +#include "timex.h" +#include "ztimer.h" + +#define TEST_DELAY_US (1U * US_PER_SEC) /* 2 seconds delay between each test */ + +int main(void) +{ + sha5461as_t dev; + dev.digits = SHA5461AS_MAX_DIGITS; + + puts("Initializing SHA5461AS device."); + if (sha5461as_init(&dev, &sha5461as_params[0]) != 0) { + puts("[Error] Cannot initialize DSP0401 display."); + return 1; + } + puts("Initialization successful."); + + ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.dig1); + gpio_set(dev.params.dig2); + gpio_set(dev.params.dig3); + gpio_set(dev.params.dig4); + gpio_set(dev.params.a);ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.b);ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.c);ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.d);ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.e);ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.f);ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.g);ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.dp); + + gpio_clear(dev.params.dig2); + gpio_clear(dev.params.dig3); + gpio_clear(dev.params.dig4); + + ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + /* R I O T */ + /* 11101110 00001100 11111100 00001110 */ + uint32_t binary_riot = 0b11101110000011001111110000001110; + sha5461as_set_all_value(&dev, binary_riot); + + if ( sha5461as_display(&dev) == 0) { + puts("Launched..."); + } else { + puts("Error"); + } + + for (volatile uint32_t i = 0; i < CLOCK_CORECLOCK / 5; ++i) {} + + if ( sha5461as_display(&dev) == 0) { + puts("...Stopped"); + } else { + puts("Error"); + } + + while(1) {} + + return 0; +} From bc0545d974c647c0f1593b18484d4507b041d00b Mon Sep 17 00:00:00 2001 From: Pierre Le Meur Date: Wed, 13 Nov 2024 11:26:33 +0100 Subject: [PATCH 02/10] Refacto files with uncrustify --- drivers/include/sha5461as.h | 110 +++++++++---------- drivers/sha5461as/include/sha5461as_params.h | 81 ++++++++++---- drivers/sha5461as/sha5461as.c | 59 +++++----- tests/drivers/sha5461as/README.md | 1 - tests/drivers/sha5461as/main.c | 30 ++--- 5 files changed, 151 insertions(+), 130 deletions(-) diff --git a/drivers/include/sha5461as.h b/drivers/include/sha5461as.h index b421670b0ecc..7d8af803dfcb 100644 --- a/drivers/include/sha5461as.h +++ b/drivers/include/sha5461as.h @@ -30,67 +30,67 @@ extern "C" { #endif -#define SHA5461AS_MAX_DIGITS (4) /**< Max pin number, can be below or equal */ +#define SHA5461AS_MAX_DIGITS (4) /**< Max pin number, can be below or equal */ #ifndef XTIMER_DEV -#define XTIMER_DEV TIMER_DEV(0) /**< default timer */ +#define XTIMER_DEV TIMER_DEV(0) /**< default timer */ #endif #ifndef XTIMER_HZ #define XTIMER_HZ MHZ(1) /**< default timer hz */ #endif #ifndef SHA5461AS_DELAY -#define SHA5461AS_DELAY (XTIMER_HZ/2600) /**< with 1MHz and a cycle as 2600 we got 2.6ms each shift, so - undetectable frequency by human eyes*/ +#define SHA5461AS_DELAY (XTIMER_HZ / 2600) /**< with 1MHz and a cycle as 2600 we got 2.6ms each shift, so + undetectable frequency by human eyes*/ #endif -#define TIMER_RUNNING (1) /**< status when timer is running */ -#define TIMER_STOPPED (0) /**< status when timer is stopped */ +#define TIMER_RUNNING (1) /**< status when timer is running */ +#define TIMER_STOPPED (0) /**< status when timer is stopped */ /** * @brief Return codes */ enum { - SHA5461AS_OK = 0, /**< All ok */ - SHA5461AS_ERR_A_GPIO, /**< Something went wrong with A GPIO */ - SHA5461AS_ERR_B_GPIO, /**< Something went wrong with B GPIO */ - SHA5461AS_ERR_C_GPIO, /**< Something went wrong with C GPIO */ - SHA5461AS_ERR_D_GPIO, /**< Something went wrong with D GPIO */ - SHA5461AS_ERR_E_GPIO, /**< Something went wrong with E GPIO */ - SHA5461AS_ERR_F_GPIO, /**< Something went wrong with F GPIO */ - SHA5461AS_ERR_G_GPIO, /**< Something went wrong with G GPIO */ - SHA5461AS_ERR_DP_GPIO, /**< Something went wrong with DP GPIO */ - SHA5461AS_ERR_DIG1_GPIO, /**< Something went wrong with DIG1 GPIO */ - SHA5461AS_ERR_DIG2_GPIO, /**< Something went wrong with DIG2 GPIO */ - SHA5461AS_ERR_DIG3_GPIO, /**< Something went wrong with DIG3 GPIO */ - SHA5461AS_ERR_DIG4_GPIO, /**< Something went wrong with DIG4 GPIO */ - SHA5461AS_ERR_DIGITS, /**< Something went wrong with digits value */ + SHA5461AS_OK = 0, /**< All ok */ + SHA5461AS_ERR_A_GPIO, /**< Something went wrong with A GPIO */ + SHA5461AS_ERR_B_GPIO, /**< Something went wrong with B GPIO */ + SHA5461AS_ERR_C_GPIO, /**< Something went wrong with C GPIO */ + SHA5461AS_ERR_D_GPIO, /**< Something went wrong with D GPIO */ + SHA5461AS_ERR_E_GPIO, /**< Something went wrong with E GPIO */ + SHA5461AS_ERR_F_GPIO, /**< Something went wrong with F GPIO */ + SHA5461AS_ERR_G_GPIO, /**< Something went wrong with G GPIO */ + SHA5461AS_ERR_DP_GPIO, /**< Something went wrong with DP GPIO */ + SHA5461AS_ERR_DIG1_GPIO, /**< Something went wrong with DIG1 GPIO */ + SHA5461AS_ERR_DIG2_GPIO, /**< Something went wrong with DIG2 GPIO */ + SHA5461AS_ERR_DIG3_GPIO, /**< Something went wrong with DIG3 GPIO */ + SHA5461AS_ERR_DIG4_GPIO, /**< Something went wrong with DIG4 GPIO */ + SHA5461AS_ERR_DIGITS, /**< Something went wrong with digits value */ }; /** * @brief Device initialization parameters */ typedef struct { - gpio_t a; /**< Data input pin */ - gpio_t b; /**< Data input pin */ - gpio_t c; /**< Data input pin */ - gpio_t d; /**< Data input pin */ - gpio_t e; /**< Data input pin */ - gpio_t f; /**< Data input pin */ - gpio_t g; /**< Data input pin */ - gpio_t dp; /**< Data input pin */ - gpio_t dig1; /**< Data input pin */ - gpio_t dig2; /**< Data input pin */ - gpio_t dig3; /**< Data input pin */ - gpio_t dig4; /**< Data input pin */ + gpio_t a; /**< Data input pin */ + gpio_t b; /**< Data input pin */ + gpio_t c; /**< Data input pin */ + gpio_t d; /**< Data input pin */ + gpio_t e; /**< Data input pin */ + gpio_t f; /**< Data input pin */ + gpio_t g; /**< Data input pin */ + gpio_t dp; /**< Data input pin */ + gpio_t dig1; /**< Data input pin */ + gpio_t dig2; /**< Data input pin */ + gpio_t dig3; /**< Data input pin */ + gpio_t dig4; /**< Data input pin */ } sha5461as_params_t; /** * @brief Device descriptor for the SHA5461AS */ typedef struct { - sha5461as_params_t params; /**< Device parameters */ - uint32_t value; /**< Binary value to display */ - uint8_t digits; /**< Number of digits */ - uint8_t current_digit; /**< Displayed digit */ - int status; /**< Status of the timer_periodic for this dev */ + sha5461as_params_t params; /**< Device parameters */ + uint32_t value; /**< Binary value to display */ + uint8_t digits; /**< Number of digits */ + uint8_t current_digit; /**< Displayed digit */ + int status; /**< Status of the timer_periodic for this dev */ } sha5461as_t; /** @@ -113,54 +113,42 @@ typedef struct { * @return -SHA5461AS_ERR_DIG3_GPIO if an error occured during DIG3 GPIO * @return -SHA5461AS_ERR_DIG4_GPIO if an error occured during DIG4 GPIO */ -int sha5461as_init(sha5461as_t* dev, const sha5461as_params_t* params); +int sha5461as_init(sha5461as_t *dev, const sha5461as_params_t *params); /** - * @brief Shift the current digit off and the next one on - * - * @param[in] dev Initialized device descriptor of SHA5461AS device - * - */ -int sha5461as_shift(sha5461as_t* dev); - -/** - * @brief Set the value for every digit + * @brief Shift the current digit off and the next one on * * @param[in] dev Initialized device descriptor of SHA5461AS device - * @param[in] index the digit number between [0, SHA5461AS_DIGITS[ - * @param[in] value the value as an @ref uint32_t * */ -int sha5461as_set_all_value(sha5461as_t* dev, uint32_t value); +int sha5461as_shift(sha5461as_t *dev); /** - * @brief Set the value for one digit + * @brief Set the value for every digit * * @param[in] dev Initialized device descriptor of SHA5461AS device - * @param[in] index the digit number between [0, SHA5461AS_DIGITS[ - * @param[in] value the value as an @ref uint8_t, A is equal to the first bit - * B the second.... DP the 8th bit. + * @param[in] value the value as an uint32_t * */ -int sha5461as_set_value(sha5461as_t* dev, int index, uint8_t value); +int sha5461as_set_all_value(sha5461as_t *dev, uint32_t value); /** * @brief Set the value for one digit * * @param[in] dev Initialized device descriptor of SHA5461AS device * @param[in] index the digit number between [0, SHA5461AS_DIGITS[ - * @param[in] value the value as an @ref uint8_t, A is equal to the first bit - * B the second.... DP the 8th bit. + * @param[in] value the value as an uint8_t, A is equal to the first bit + * B the second.... DP the 8th bit. * */ -int sha5461as_set_value(sha5461as_t* dev, int index, uint8_t value); +int sha5461as_set_value(sha5461as_t *dev, int index, uint8_t value); /** * @brief Start/stop an periodic timer event to shift between each 7seg - * - * @param[in] dev Initialized device descriptor of SHA5461AS device + * + * @param[in] dev Initialized device descriptor of SHA5461AS device */ -int sha5461as_display(sha5461as_t* dev); +int sha5461as_display(sha5461as_t *dev); #ifdef __cplusplus } diff --git a/drivers/sha5461as/include/sha5461as_params.h b/drivers/sha5461as/include/sha5461as_params.h index 425633cb39f2..a3ac201f8953 100644 --- a/drivers/sha5461as/include/sha5461as_params.h +++ b/drivers/sha5461as/include/sha5461as_params.h @@ -33,57 +33,96 @@ extern "C" { * @{ */ #ifndef SHA5461AS_PARAM_A_PIN -#define SHA5461AS_PARAM_A_PIN GPIO_PIN(1,11) /* D2 */ +/** + * @brief SHA5461AS the A pin + */ +#define SHA5461AS_PARAM_A_PIN GPIO_PIN(1, 11) /* D2 */ #endif #ifndef SHA5461AS_PARAM_B_PIN -#define SHA5461AS_PARAM_B_PIN GPIO_PIN(1,12) /* D3 */ +/** + * @brief SHA5461AS the B pin + */ +#define SHA5461AS_PARAM_B_PIN GPIO_PIN(1, 12) /* D3 */ #endif #ifndef SHA5461AS_PARAM_C_PIN -#define SHA5461AS_PARAM_C_PIN GPIO_PIN(1,15) /* D4 */ +/** + * @brief SHA5461AS the C pin + */ +#define SHA5461AS_PARAM_C_PIN GPIO_PIN(1, 15) /* D4 */ #endif #ifndef SHA5461AS_PARAM_D_PIN -#define SHA5461AS_PARAM_D_PIN GPIO_PIN(1,13) /* D5 */ +/** + * @brief SHA5461AS the D pin + */ +#define SHA5461AS_PARAM_D_PIN GPIO_PIN(1, 13) /* D5 */ #endif #ifndef SHA5461AS_PARAM_E_PIN -#define SHA5461AS_PARAM_E_PIN GPIO_PIN(1,14) /* D6 */ +/** + * @brief SHA5461AS the E pin + */ +#define SHA5461AS_PARAM_E_PIN GPIO_PIN(1, 14) /* D6 */ #endif #ifndef SHA5461AS_PARAM_F_PIN -#define SHA5461AS_PARAM_F_PIN GPIO_PIN(0,23) /* D7 */ +/** + * @brief SHA5461AS the F pin + */ +#define SHA5461AS_PARAM_F_PIN GPIO_PIN(0, 23) /* D7 */ #endif #ifndef SHA5461AS_PARAM_G_PIN -#define SHA5461AS_PARAM_G_PIN GPIO_PIN(0,21) /* D8 */ +/** + * @brief SHA5461AS the G pin + */ +#define SHA5461AS_PARAM_G_PIN GPIO_PIN(0, 21) /* D8 */ #endif #ifndef SHA5461AS_PARAM_DP_PIN -#define SHA5461AS_PARAM_DP_PIN GPIO_PIN(0,27) /* D9 */ +/** + * @brief SHA5461AS the DP pin + */ +#define SHA5461AS_PARAM_DP_PIN GPIO_PIN(0, 27) /* D9 */ #endif #ifndef SHA5461AS_PARAM_DIG1_PIN -#define SHA5461AS_PARAM_DIG1_PIN GPIO_PIN(1,2) /* D10 */ +/** + * @brief SHA5461AS the first digit pin + */ +#define SHA5461AS_PARAM_DIG1_PIN GPIO_PIN(1, 2) /* D10 */ #endif #ifndef SHA5461AS_PARAM_DIG2_PIN -#define SHA5461AS_PARAM_DIG2_PIN GPIO_PIN(1,1) /* D11 */ +/** + * @brief SHA5461AS the second digit pin + */ +#define SHA5461AS_PARAM_DIG2_PIN GPIO_PIN(1, 1) /* D11 */ #endif #ifndef SHA5461AS_PARAM_DIG3_PIN -#define SHA5461AS_PARAM_DIG3_PIN GPIO_PIN(1,8) /* D12 */ +/** + * @brief SHA5461AS the third digit pin + */ +#define SHA5461AS_PARAM_DIG3_PIN GPIO_PIN(1, 8) /* D12 */ #endif #ifndef SHA5461AS_PARAM_DIG4_PIN -#define SHA5461AS_PARAM_DIG4_PIN GPIO_PIN(0,13) /* D13 */ +/** + * @brief SHA5461AS the fourth digit pin + */ +#define SHA5461AS_PARAM_DIG4_PIN GPIO_PIN(0, 13) /* D13 */ #endif #ifndef SHA5461AS_PARAMS -#define SHA5461AS_PARAMS { .a = SHA5461AS_PARAM_A_PIN, \ - .b = SHA5461AS_PARAM_B_PIN, \ - .c = SHA5461AS_PARAM_C_PIN, \ - .d = SHA5461AS_PARAM_D_PIN, \ - .e = SHA5461AS_PARAM_E_PIN, \ - .f = SHA5461AS_PARAM_F_PIN, \ - .g = SHA5461AS_PARAM_G_PIN, \ - .dp = SHA5461AS_PARAM_DP_PIN, \ +/** + * @brief The initialization parameters for the SHA5461AS driver + */ +#define SHA5461AS_PARAMS { .a = SHA5461AS_PARAM_A_PIN, \ + .b = SHA5461AS_PARAM_B_PIN, \ + .c = SHA5461AS_PARAM_C_PIN, \ + .d = SHA5461AS_PARAM_D_PIN, \ + .e = SHA5461AS_PARAM_E_PIN, \ + .f = SHA5461AS_PARAM_F_PIN, \ + .g = SHA5461AS_PARAM_G_PIN, \ + .dp = SHA5461AS_PARAM_DP_PIN, \ .dig1 = SHA5461AS_PARAM_DIG1_PIN, \ .dig2 = SHA5461AS_PARAM_DIG2_PIN, \ .dig3 = SHA5461AS_PARAM_DIG3_PIN, \ .dig4 = SHA5461AS_PARAM_DIG4_PIN } - + #endif /**@}*/ diff --git a/drivers/sha5461as/sha5461as.c b/drivers/sha5461as/sha5461as.c index 73608da6f3ec..ebc2cb0f0ffc 100644 --- a/drivers/sha5461as/sha5461as.c +++ b/drivers/sha5461as/sha5461as.c @@ -43,36 +43,34 @@ #define NB_PIN (12) -static void _set_pin_value(sha5461as_t* dev) +static void _set_pin_value(sha5461as_t *dev) { - const gpio_t pins[] = + const gpio_t pins[] = { PIN_A, PIN_B, PIN_C, PIN_D, PIN_E, PIN_F, PIN_G, PIN_DP }; uint8_t mask = 0xFF; uint8_t current_value = (uint8_t)(dev->value >> (dev->current_digit * 8) & mask); - - for ( int i = 1; i <= 8; i++) - { - if (current_value & (1 << (8 - i))) - { - gpio_set(pins[i-1]); + + for (int i = 1; i <= 8; i++) { + if (current_value & (1 << (8 - i))) { + gpio_set(pins[i - 1]); DEBUG("PIN SET\n"); - } else - { - gpio_clear(pins[i-1]); + } + else { + gpio_clear(pins[i - 1]); DEBUG("PIN CLEAR\n"); } } } -static void _shift_display(void* arg, int chan) +static void _shift_display(void *arg, int chan) { - (void) chan; - sha5461as_t* dev = (sha5461as_t*)arg; + (void)chan; + sha5461as_t *dev = (sha5461as_t *)arg; - const gpio_t digit_pins[] = + const gpio_t digit_pins[] = { PIN_DIG1, PIN_DIG2, PIN_DIG3, PIN_DIG4 }; @@ -85,23 +83,22 @@ static void _shift_display(void* arg, int chan) _set_pin_value(dev); } -int sha5461as_init(sha5461as_t* dev, const sha5461as_params_t* params) +int sha5461as_init(sha5461as_t *dev, const sha5461as_params_t *params) { dev->params = *params; dev->current_digit = 0; - if (dev->digits <= 0 || dev->digits > SHA5461AS_MAX_DIGITS) - { + if (dev->digits <= 0 || dev->digits > SHA5461AS_MAX_DIGITS) { return -SHA5461AS_ERR_DIGITS; } - const gpio_t pins[] = + const gpio_t pins[] = { PIN_A, PIN_B, PIN_C, PIN_D, PIN_E, PIN_F, PIN_G, PIN_DP, PIN_DIG1, PIN_DIG2, PIN_DIG3, PIN_DIG4 }; - const int pin_errs[] = + const int pin_errs[] = { SHA5461AS_ERR_A_GPIO, SHA5461AS_ERR_B_GPIO, SHA5461AS_ERR_C_GPIO, SHA5461AS_ERR_D_GPIO, SHA5461AS_ERR_E_GPIO, SHA5461AS_ERR_F_GPIO, SHA5461AS_ERR_G_GPIO, SHA5461AS_ERR_DP_GPIO, @@ -109,10 +106,8 @@ int sha5461as_init(sha5461as_t* dev, const sha5461as_params_t* params) SHA5461AS_ERR_DIG4_GPIO }; - for (int i = 0; i < NB_PIN; i++) - { - if (gpio_init(pins[i], GPIO_OUT) < 0) - { + for (int i = 0; i < NB_PIN; i++) { + if (gpio_init(pins[i], GPIO_OUT) < 0) { DEBUG("[Error] Initializing error"); return pin_errs[i]; } @@ -122,19 +117,19 @@ int sha5461as_init(sha5461as_t* dev, const sha5461as_params_t* params) return SHA5461AS_OK; } -int sha5461as_shift(sha5461as_t* dev) +int sha5461as_shift(sha5461as_t *dev) { _shift_display(dev, 0); return 1; } -int sha5461as_set_all_value(sha5461as_t* dev, uint32_t value) +int sha5461as_set_all_value(sha5461as_t *dev, uint32_t value) { dev->value = value; return 1; } -int sha5461as_set_value(sha5461as_t* dev, int index, uint8_t value) +int sha5461as_set_value(sha5461as_t *dev, int index, uint8_t value) { value <<= (index * 8); uint32_t up_value = dev->value >> ((index + 1) * 8); @@ -145,20 +140,18 @@ int sha5461as_set_value(sha5461as_t* dev, int index, uint8_t value) return 1; } -int sha5461as_display(sha5461as_t* dev) +int sha5461as_display(sha5461as_t *dev) { - if ( dev->status == TIMER_RUNNING ) - { + if (dev->status == TIMER_RUNNING) { timer_stop(XTIMER_DEV); dev->status = TIMER_STOPPED; return 0; } - if ( timer_init(XTIMER_DEV, XTIMER_HZ, _shift_display, dev) != 0 ) - { + if (timer_init(XTIMER_DEV, XTIMER_HZ, _shift_display, dev) != 0) { return -1; } - + timer_set_periodic(XTIMER_DEV, 0, SHA5461AS_DELAY, TIM_FLAG_RESET_ON_MATCH); dev->status = TIMER_RUNNING; diff --git a/tests/drivers/sha5461as/README.md b/tests/drivers/sha5461as/README.md index 3f808b48c968..464e3ae25040 100644 --- a/tests/drivers/sha5461as/README.md +++ b/tests/drivers/sha5461as/README.md @@ -14,4 +14,3 @@ The display needs to be connected as follows: For each digit pin (`DIGX`), you need to use a transistor and link it to `GND`. ![datasheet image](https://www.datasheetcafe.com/wp-content/uploads/2015/09/SH5461AS.gif) - diff --git a/tests/drivers/sha5461as/main.c b/tests/drivers/sha5461as/main.c index 445783dd0fa2..2d4ed7c934ac 100644 --- a/tests/drivers/sha5461as/main.c +++ b/tests/drivers/sha5461as/main.c @@ -33,7 +33,7 @@ int main(void) { sha5461as_t dev; dev.digits = SHA5461AS_MAX_DIGITS; - + puts("Initializing SHA5461AS device."); if (sha5461as_init(&dev, &sha5461as_params[0]) != 0) { puts("[Error] Cannot initialize DSP0401 display."); @@ -46,13 +46,13 @@ int main(void) gpio_set(dev.params.dig2); gpio_set(dev.params.dig3); gpio_set(dev.params.dig4); - gpio_set(dev.params.a);ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); - gpio_set(dev.params.b);ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); - gpio_set(dev.params.c);ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); - gpio_set(dev.params.d);ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); - gpio_set(dev.params.e);ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); - gpio_set(dev.params.f);ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); - gpio_set(dev.params.g);ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.a); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.b); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.c); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.d); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.e); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.f); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.g); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); gpio_set(dev.params.dp); gpio_clear(dev.params.dig2); @@ -65,21 +65,23 @@ int main(void) uint32_t binary_riot = 0b11101110000011001111110000001110; sha5461as_set_all_value(&dev, binary_riot); - if ( sha5461as_display(&dev) == 0) { + if (sha5461as_display(&dev) == 0) { puts("Launched..."); - } else { + } + else { puts("Error"); } for (volatile uint32_t i = 0; i < CLOCK_CORECLOCK / 5; ++i) {} - if ( sha5461as_display(&dev) == 0) { + if (sha5461as_display(&dev) == 0) { puts("...Stopped"); - } else { + } + else { puts("Error"); } - while(1) {} - + while (1) {} + return 0; } From 7a1beca13920dfc6cb9ab4c5bc81e31f09a7afd4 Mon Sep 17 00:00:00 2001 From: Pierre Le Meur Date: Wed, 13 Nov 2024 11:42:02 +0100 Subject: [PATCH 03/10] Correcting spelling mistakes --- drivers/include/sha5461as.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/drivers/include/sha5461as.h b/drivers/include/sha5461as.h index 7d8af803dfcb..04dd0d7cc92f 100644 --- a/drivers/include/sha5461as.h +++ b/drivers/include/sha5461as.h @@ -100,18 +100,18 @@ typedef struct { * @param[in] params Device parameters to use * * @return SHA5461AS_OK if everything is good - * @return -SHA5461AS_ERR_A_GPIO if an error occured during A GPIO - * @return -SHA5461AS_ERR_B_GPIO if an error occured during B GPIO - * @return -SHA5461AS_ERR_C_GPIO if an error occured during C GPIO - * @return -SHA5461AS_ERR_D_GPIO if an error occured during D GPIO - * @return -SHA5461AS_ERR_E_GPIO if an error occured during E GPIO - * @return -SHA5461AS_ERR_F_GPIO if an error occured during F GPIO - * @return -SHA5461AS_ERR_G_GPIO if an error occured during G GPIO - * @return -SHA5461AS_ERR_DP_GPIO if an error occured during DP GPIO - * @return -SHA5461AS_ERR_DIG1_GPIO if an error occured during DIG1 GPIO - * @return -SHA5461AS_ERR_DIG2_GPIO if an error occured during DIG2 GPIO - * @return -SHA5461AS_ERR_DIG3_GPIO if an error occured during DIG3 GPIO - * @return -SHA5461AS_ERR_DIG4_GPIO if an error occured during DIG4 GPIO + * @return -SHA5461AS_ERR_A_GPIO if an error occurred during A GPIO + * @return -SHA5461AS_ERR_B_GPIO if an error occurred during B GPIO + * @return -SHA5461AS_ERR_C_GPIO if an error occurred during C GPIO + * @return -SHA5461AS_ERR_D_GPIO if an error occurred during D GPIO + * @return -SHA5461AS_ERR_E_GPIO if an error occurred during E GPIO + * @return -SHA5461AS_ERR_F_GPIO if an error occurred during F GPIO + * @return -SHA5461AS_ERR_G_GPIO if an error occurred during G GPIO + * @return -SHA5461AS_ERR_DP_GPIO if an error occurred during DP GPIO + * @return -SHA5461AS_ERR_DIG1_GPIO if an error occurred during DIG1 GPIO + * @return -SHA5461AS_ERR_DIG2_GPIO if an error occurred during DIG2 GPIO + * @return -SHA5461AS_ERR_DIG3_GPIO if an error occurred during DIG3 GPIO + * @return -SHA5461AS_ERR_DIG4_GPIO if an error occurred during DIG4 GPIO */ int sha5461as_init(sha5461as_t *dev, const sha5461as_params_t *params); From 005250fbd86afd9cb31fbe0222703b297370e409 Mon Sep 17 00:00:00 2001 From: Pierre Le Meur Date: Thu, 14 Nov 2024 15:23:44 +0100 Subject: [PATCH 04/10] drivers/digit7seg: add 4-digit 7-segment display driver --- drivers/{sha5461as => digit7seg}/Makefile | 0 drivers/{sha5461as => digit7seg}/Makefile.dep | 0 drivers/digit7seg/Makefile.include | 2 + drivers/digit7seg/digit7seg.c | 178 +++++++++++++++++ drivers/digit7seg/doc.txt | 185 ++++++++++++++++++ drivers/digit7seg/include/digit7seg_params.h | 150 ++++++++++++++ drivers/include/digit7seg.h | 168 ++++++++++++++++ drivers/include/sha5461as.h | 158 --------------- drivers/sha5461as/Makefile.include | 2 - drivers/sha5461as/include/sha5461as_params.h | 142 -------------- drivers/sha5461as/sha5461as.c | 159 --------------- .../drivers/{sha5461as => digit7seg}/Makefile | 2 +- tests/drivers/digit7seg/README.md | 38 ++++ tests/drivers/{sha5461as => digit7seg}/main.c | 49 +++-- tests/drivers/sha5461as/README.md | 16 -- 15 files changed, 750 insertions(+), 499 deletions(-) rename drivers/{sha5461as => digit7seg}/Makefile (100%) rename drivers/{sha5461as => digit7seg}/Makefile.dep (100%) create mode 100644 drivers/digit7seg/Makefile.include create mode 100644 drivers/digit7seg/digit7seg.c create mode 100644 drivers/digit7seg/doc.txt create mode 100644 drivers/digit7seg/include/digit7seg_params.h create mode 100644 drivers/include/digit7seg.h delete mode 100644 drivers/include/sha5461as.h delete mode 100644 drivers/sha5461as/Makefile.include delete mode 100644 drivers/sha5461as/include/sha5461as_params.h delete mode 100644 drivers/sha5461as/sha5461as.c rename tests/drivers/{sha5461as => digit7seg}/Makefile (83%) create mode 100644 tests/drivers/digit7seg/README.md rename tests/drivers/{sha5461as => digit7seg}/main.c (50%) delete mode 100644 tests/drivers/sha5461as/README.md diff --git a/drivers/sha5461as/Makefile b/drivers/digit7seg/Makefile similarity index 100% rename from drivers/sha5461as/Makefile rename to drivers/digit7seg/Makefile diff --git a/drivers/sha5461as/Makefile.dep b/drivers/digit7seg/Makefile.dep similarity index 100% rename from drivers/sha5461as/Makefile.dep rename to drivers/digit7seg/Makefile.dep diff --git a/drivers/digit7seg/Makefile.include b/drivers/digit7seg/Makefile.include new file mode 100644 index 000000000000..844ed5d81231 --- /dev/null +++ b/drivers/digit7seg/Makefile.include @@ -0,0 +1,2 @@ +USEMODULE_INCLUDES_digit7seg := $(LAST_MAKEFILEDIR)/include +USEMODULE_INCLUDES += $(USEMODULE_INCLUDES_digit7seg) diff --git a/drivers/digit7seg/digit7seg.c b/drivers/digit7seg/digit7seg.c new file mode 100644 index 000000000000..89916b9d71d1 --- /dev/null +++ b/drivers/digit7seg/digit7seg.c @@ -0,0 +1,178 @@ +/* + * Copyright (C) 2024 Orange + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup drivers_digit7seg + * @file + * @brief Device driver for less than 5 digits of 7 segments without IC + * @author Pierre Le Meur + */ + +#include "digit7seg.h" + +#define ENABLE_DEBUG 0 +#include "debug.h" + +#define PIN_A (dev->params.data_a) +#define PIN_B (dev->params.data_b) +#define PIN_C (dev->params.data_c) +#define PIN_D (dev->params.data_d) +#define PIN_E (dev->params.data_e) +#define PIN_F (dev->params.data_f) +#define PIN_G (dev->params.data_g) +#define PIN_DP (dev->params.data_dp) +#define PIN_DIG1 (dev->params.dig1) +#define PIN_DIG2 (dev->params.dig2) +#define PIN_DIG3 (dev->params.dig3) +#define PIN_DIG4 (dev->params.dig4) + +/** The 7 segments + decimal point + the number of digits */ +#define NB_PIN (8+dev->params.digits) + +static void _set_pin_value(digit7seg_t *dev) +{ + const gpio_t pins[] = + { + PIN_A, PIN_B, PIN_C, PIN_D, PIN_E, PIN_F, PIN_G, PIN_DP + }; + + uint8_t mask = 0xFF; + uint8_t current_value = (uint8_t)(dev->value >> (dev->current_digit * 8) & mask); + + for (int i = 1; i <= 8; i++) { + if (current_value & (1 << (8 - i))) { + gpio_set(pins[i - 1]); + DEBUG("PIN SET\n"); + } + else { + gpio_clear(pins[i - 1]); + DEBUG("PIN CLEAR\n"); + } + } +} + +static void _shift_display(void *arg, int chan) +{ + (void)chan; + digit7seg_t *dev = (digit7seg_t*)arg; + + const gpio_t digit_pins[] = + { + PIN_DIG1, PIN_DIG2, PIN_DIG3, PIN_DIG4 + }; + + gpio_clear(digit_pins[dev->current_digit]); + dev->current_digit += 1; + dev->current_digit = dev->current_digit % dev->params.digits; + gpio_set(digit_pins[dev->current_digit]); + + DEBUG("[INFO] On display %d\n", dev->current_digit); + + _set_pin_value(dev); +} + +int digit7seg_init(digit7seg_t *dev, const digit7seg_params_t *params) +{ + dev->params = *params; + dev->current_digit = 0; + dev->value = 0; + + if (dev->params.digits <= 0 || dev->params.digits > DIGIT7SEG_MAX_DIGITS) { + DEBUG("[Error] Invalid number of digit.\n"); + return -DIGIT7SEG_ERR_DIGITS; + } + + const gpio_t pins[] = + { + PIN_A, PIN_B, PIN_C, PIN_D, PIN_E, PIN_F, PIN_G, PIN_DP, + PIN_DIG1, PIN_DIG2, PIN_DIG3, PIN_DIG4 + }; + + const int pin_errs[] = + { + DIGIT7SEG_ERR_A_GPIO, DIGIT7SEG_ERR_B_GPIO, DIGIT7SEG_ERR_C_GPIO, DIGIT7SEG_ERR_D_GPIO, + DIGIT7SEG_ERR_E_GPIO, DIGIT7SEG_ERR_F_GPIO, DIGIT7SEG_ERR_G_GPIO, DIGIT7SEG_ERR_DP_GPIO, + DIGIT7SEG_ERR_DIG1_GPIO, DIGIT7SEG_ERR_DIG2_GPIO, DIGIT7SEG_ERR_DIG3_GPIO, + DIGIT7SEG_ERR_DIG4_GPIO + }; + + for (int i = 0; i < NB_PIN; i++) { + if (!gpio_is_valid(pins[i])) { + DEBUG("[Error] GPIO isn't valid.\n"); + return -pin_errs[i]; + } + + if (gpio_init(pins[i], GPIO_OUT) < 0) { + DEBUG("[Error] Initializing gpio error.\n"); + return -pin_errs[i]; + } + + gpio_clear(pins[i]); + } + + return DIGIT7SEG_OK; +} + +int digit7seg_shift(digit7seg_t *dev) +{ + _shift_display(dev, 0); + return 0; +} + +int digit7seg_set_all_value(digit7seg_t *dev, uint32_t value) +{ + dev->value = value; + return 0; +} + +int digit7seg_set_value(digit7seg_t *dev, int index, uint8_t value) +{ + if (index < 0 || index >= dev->params.digits) { + return -1; + } + + uint32_t temp_value = value << (index * 8); + uint32_t up_value = dev->value >> ((index + 1) * 8); + up_value <<= ((index + 1) * 8); + uint32_t down_value = ((0b00000001 << (index * 8)) - 1) & dev->value; + + dev->value = up_value | temp_value | down_value; + + return 0; +} + +int digit7seg_poweron(digit7seg_t *dev) +{ + if (dev->status == TIMER_RUNNING) { + DEBUG("[Error] Timer is already running.\n"); + return -1; + } + + if (timer_init(dev->params.timer, DIGIT7SEG_TIMER_HZ, _shift_display, dev) != 0) { + DEBUG("[Error] Impossible to init timer.\n"); + return -1; + } + + timer_set_periodic(dev->params.timer, 0, DIGIT7SEG_DELAY, TIM_FLAG_RESET_ON_MATCH); + + dev->status = TIMER_RUNNING; + return 0; +} + +int digit7seg_poweroff(digit7seg_t *dev) +{ + if (dev->status == TIMER_STOPPED) { + DEBUG("[Error] Timer is already stopped.\n"); + return -1; + } + + timer_stop(dev->params.timer); + dev->status = TIMER_STOPPED; + + return 0; +} diff --git a/drivers/digit7seg/doc.txt b/drivers/digit7seg/doc.txt new file mode 100644 index 000000000000..0daeb9aa7c60 --- /dev/null +++ b/drivers/digit7seg/doc.txt @@ -0,0 +1,185 @@ +/* + * Copyright (C) 2024 Orange + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + +@defgroup drivers_digit7seg Generic 7 segments driver +@ingroup drivers_display +@brief Device driver for less than 5 digits of 7 segments without IC + +## About + +This driver was developed to works with [5461AS](http://www.topliteusa.com/uploadfile/2014/0825/A-5461AS.pdf) +in a way that all 4 digits (and less) with 7 segments without integrated controller +can be controlled into [RIOT-OS](https://github.com/RIOT-OS/RIOT). + +### 7 segments + +Each digit contains 7 digit (plus decimal point), if the segment is set high and his +digit is set high it will be lit. +Each value is cut into 8 bits (a, b, c, d, e, f, g, dp), if the bit is 1 current is sent otherwise +not. + +To display 0 we will pass the number +``` + dp g f e d c b a + 0 0 1 1 1 1 1 1 +``` +``` + --a-- + | | + f b + | | + --g-- + | | + e c + | | + --d-- .dp +``` + +### 4 digits of 7 segments + +To set up a 4 digits of 7 segments display, we need transistors to allow current to pass +only when the corresponding digit pin digX is high. + +All the digits share the same value pins. To display different values on each digit, we shift the value +and turn on each digit at a rate that is imperceptible to the human eye. + +``` + .----.----.----.----. + | | | | |-- a + | | | | |-- b + | | | | |-- c + | | | | |-- d + | | | | |-- e + | | | | |-- f + | | | | |-- g + | | | | |-- dp + '----'----'----'----' + | | | | + T-------------------- dig1 + | | | | + | T--------------- dig2 + | | | | + | | T---------- dig3 + | | | | + | | | T----- dig4 + | | | | + └──--└──--└──--└──--- GND +``` + +@note T represent a NPN transistor + +## Error Handling + +All driver functions return 0 on success or one of a negative error code defined by +#digit7seg_error_codes. + +## Usage + +To use the digit7seg driver, the configuration must be set by including `digit7seg_params.h` +or defining a `digit7seg_params_t` struct. + +The structure of a configuration is defined in #digit7seg_params_t and consists of the following +parameters: + +Parameter | Symbol in ```digit7seg_params.h``` | Default +:------------------------------|:-----------------------------------|:------------------ +Data pin for a segment | DIGIT7SEG_PARAM_A_PIN | GPIO_PIN(1, 11) +Data pin for b segment | DIGIT7SEG_PARAM_B_PIN | GPIO_PIN(1, 12) +Data pin for c segment | DIGIT7SEG_PARAM_C_PIN | GPIO_PIN(1, 15) +Data pin for d segment | DIGIT7SEG_PARAM_D_PIN | GPIO_PIN(1, 13) +Data pin for e segment | DIGIT7SEG_PARAM_E_PIN | GPIO_PIN(1, 14) +Data pin for f segment | DIGIT7SEG_PARAM_F_PIN | GPIO_PIN(0, 23) +Data pin for g segment | DIGIT7SEG_PARAM_G_PIN | GPIO_PIN(0, 21) +Data pin for dp segment | DIGIT7SEG_PARAM_DP_PIN | GPIO_PIN(0, 27) +Pin to on digit 1 | DIGIT7SEG_PARAM_DIG1_PIN | GPIO_PIN(1, 2) +Pin to on digit 2 | DIGIT7SEG_PARAM_DIG2_PIN | GPIO_PIN(1, 1) +Pin to on digit 3 | DIGIT7SEG_PARAM_DIG3_PIN | GPIO_PIN(1, 8) +Pin to on digit 4 | DIGIT7SEG_PARAM_DIG4_PIN | GPIO_PIN(0, 13) +Timer for periodic interrupt | DIGIT7SEG_PARAM_TIMER | TIMER_DEV(2) +Number of digits on the periph | DIGIT7SEG_PARAM_DIGITS | 4 + +The default configuration can be overwritten by the application. + +Example: +``` +#define DIGIT7SEG_PARAM_TIMER TIMER_DEV(1) +#define DIGIT7SEG_PARAM_DIGITS (2) +... +#include "digit7seg.h" +#include "digit7seg_param.h" +``` + +Another way to override params is to override DIGIT7SEG_PARAMS that will +be stocked into digit7seg_params. + +@note At least all the data_* pin must be defined, depending of the number of digits set, dig4, dig3, dig2 can be set at GPIO_UNDEF. + +Example: +``` +#define DIGIT7SEG_PARAMS { .data_a = GPIO(1, 14), \ + .data_b = GPIO(1, 17), \ + .data_c = GPIO(1, 15), \ + .data_d = GPIO(1, 13), \ + .data_e = GPIO(1, 12), \ + .data_f = GPIO(0, 23), \ + .data_g = GPIO(0, 21), \ + .data_dp = GPIO(1, 11), \ + .dig1 = GPIO(0, 13), \ + .dig2 = GPIO(1, 8), \ + .dig3 = GPIO_UNDEF, \ + .dig4 = GPIO_UNDEF, \ + .timer = TIMER_DEV(2), \ + .digits = 2 } +... +#include "digit7seg.h" +#include "digit7seg_param.h" +``` + +The ::digit7seg_init function initializes a #digit7seg_t and checks if every parameter is correctly set. + +Example: +``` +digit7seg_t dev; + +if (digit7seg_init(&dev, &digit7seg_params[0]) != DIGIT7SEG_OK) { + ... /* error handling */ +} +``` + +Once the device is initialized and configured : +- ::digit7seg_set_all_value function can be used to set the value for all the digits. +- ::digit7seg_poweron function starts a periodic timer that cycles through each digit to give the impression that they are all lit simultaneously. +- ::digit7seg_poweroff function stops this timer. + +Example: +``` +/* R I O T */ +/* 11101110 00001100 11111100 00001110 */ +uint32_t binary_riot = 0b11101110000011001111110000001110; +digit7seg_set_all_value(&dev, binary_riot); + +if (digit7seg_poweron(&dev) == 0) { + puts("Launched..."); +} +else { + puts("Error"); +} + +... + +if (digit7seg_poweroff(&dev) == 0) { + puts("...Stopped"); +} +else { + puts("Error"); +} + +``` +*/ diff --git a/drivers/digit7seg/include/digit7seg_params.h b/drivers/digit7seg/include/digit7seg_params.h new file mode 100644 index 000000000000..7ca6248276d3 --- /dev/null +++ b/drivers/digit7seg/include/digit7seg_params.h @@ -0,0 +1,150 @@ +/* + * Copyright (C) 2024 Orange + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup drivers_digit7seg + * @file + * @brief Default params for device driver with less than 5 digits of + * 7 segments without IC + * @author Pierre Le Meur + */ + +#ifndef DIGIT7SEG_PARAMS_H +#define DIGIT7SEG_PARAMS_H + +#include "digit7seg.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @name Set default configuration parameters for a digit 7 segments (for arduino-nano-33-ble) + * @{ + */ +#ifndef DIGIT7SEG_PARAM_A_PIN +/** + * @brief digit 7 seg a pin + */ +# define DIGIT7SEG_PARAM_A_PIN GPIO_PIN(1, 11) /* D2 */ +#endif +#ifndef DIGIT7SEG_PARAM_B_PIN +/** + * @brief digit 7 seg b pin + */ +# define DIGIT7SEG_PARAM_B_PIN GPIO_PIN(1, 12) /* D3 */ +#endif +#ifndef DIGIT7SEG_PARAM_C_PIN +/** + * @brief digit 7 seg c pin + */ +# define DIGIT7SEG_PARAM_C_PIN GPIO_PIN(1, 15) /* D4 */ +#endif +#ifndef DIGIT7SEG_PARAM_D_PIN +/** + * @brief digit 7 seg d pin + */ +# define DIGIT7SEG_PARAM_D_PIN GPIO_PIN(1, 13) /* D5 */ +#endif +#ifndef DIGIT7SEG_PARAM_E_PIN +/** + * @brief digit 7 seg e pin + */ +# define DIGIT7SEG_PARAM_E_PIN GPIO_PIN(1, 14) /* D6 */ +#endif +#ifndef DIGIT7SEG_PARAM_F_PIN +/** + * @brief digit 7 seg f pin + */ +# define DIGIT7SEG_PARAM_F_PIN GPIO_PIN(0, 23) /* D7 */ +#endif +#ifndef DIGIT7SEG_PARAM_G_PIN +/** + * @brief digit 7 seg g pin + */ +# define DIGIT7SEG_PARAM_G_PIN GPIO_PIN(0, 21) /* D8 */ +#endif +#ifndef DIGIT7SEG_PARAM_DP_PIN +/** + * @brief digit 7 seg dp pin + */ +# define DIGIT7SEG_PARAM_DP_PIN GPIO_PIN(0, 27) /* D9 */ +#endif + +#ifndef DIGIT7SEG_PARAM_DIG1_PIN +/** + * @brief digit 7 seg first digit pin + */ +# define DIGIT7SEG_PARAM_DIG1_PIN GPIO_PIN(1, 2) /* D10 */ +#endif +#ifndef DIGIT7SEG_PARAM_DIG2_PIN +/** + * @brief digit 7 seg second digit pin + */ +# define DIGIT7SEG_PARAM_DIG2_PIN GPIO_PIN(1, 1) /* D11 */ +#endif +#ifndef DIGIT7SEG_PARAM_DIG3_PIN +/** + * @brief digit 7 seg third digit pin + */ +# define DIGIT7SEG_PARAM_DIG3_PIN GPIO_PIN(1, 8) /* D12 */ +#endif +#ifndef DIGIT7SEG_PARAM_DIG4_PIN +/** + * @brief digit 7 seg fourth digit pin + */ +# define DIGIT7SEG_PARAM_DIG4_PIN GPIO_PIN(0, 13) /* D13 */ +#endif +#ifndef DIGIT7SEG_PARAM_TIMER +/** + * @brief digit 7 seg timer to use + */ +# define DIGIT7SEG_PARAM_TIMER TIMER_DEV(2) +#endif +#ifndef DIGIT7SEG_PARAM_DIGITS +/** + * @brief digit 7 seg number of digits + */ +# define DIGIT7SEG_PARAM_DIGITS (4) +#endif + +#ifndef DIGIT7SEG_PARAMS +/** + * @brief The initialization parameters for the digit 7 seg driver + */ +#define DIGIT7SEG_PARAMS { .data_a = DIGIT7SEG_PARAM_A_PIN, \ + .data_b = DIGIT7SEG_PARAM_B_PIN, \ + .data_c = DIGIT7SEG_PARAM_C_PIN, \ + .data_d = DIGIT7SEG_PARAM_D_PIN, \ + .data_e = DIGIT7SEG_PARAM_E_PIN, \ + .data_f = DIGIT7SEG_PARAM_F_PIN, \ + .data_g = DIGIT7SEG_PARAM_G_PIN, \ + .data_dp = DIGIT7SEG_PARAM_DP_PIN, \ + .dig1 = DIGIT7SEG_PARAM_DIG1_PIN, \ + .dig2 = DIGIT7SEG_PARAM_DIG2_PIN, \ + .dig3 = DIGIT7SEG_PARAM_DIG3_PIN, \ + .dig4 = DIGIT7SEG_PARAM_DIG4_PIN, \ + .timer = DIGIT7SEG_PARAM_TIMER, \ + .digits = DIGIT7SEG_PARAM_DIGITS } + +#endif +/**@}*/ + +/** + * @brief Configure a digit 7 segments + */ +static const digit7seg_params_t digit7seg_params[] = +{ + DIGIT7SEG_PARAMS, +}; + +#ifdef __cplusplus +} +#endif + +#endif /* DIGIT7SEG_PARAMS_H */ diff --git a/drivers/include/digit7seg.h b/drivers/include/digit7seg.h new file mode 100644 index 000000000000..1dca430ccce3 --- /dev/null +++ b/drivers/include/digit7seg.h @@ -0,0 +1,168 @@ +/* + * Copyright (C) 2024 Orange + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup drivers_digit7seg + * @brief Device driver for less than 5 digits of 7 segments without IC + * @file + * @author Pierre Le Meur + * @{ + * + */ + +#ifndef DIGIT7SEG_H +#define DIGIT7SEG_H + +#include + +#include "periph/gpio.h" +#include "periph/timer.h" +#include "macros/units.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define DIGIT7SEG_MAX_DIGITS (4) /**< Max pin number, can be below or equal */ +#ifndef DIGIT7SEG_TIMER_HZ +# define DIGIT7SEG_TIMER_HZ MHZ(1) /**< default timer hz */ +#endif +#ifndef DIGIT7SEG_DELAY +/** + * With 1MHz and a cycle as 2600 we got 2.6ms each shift, + * so undetectable frequency by human eyes + * */ +# define DIGIT7SEG_DELAY (DIGIT7SEG_TIMER_HZ / 2600) +#endif +#define TIMER_RUNNING (1) /**< status when timer is running */ +#define TIMER_STOPPED (0) /**< status when timer is stopped */ + +/** + * @brief Return codes for @ref digit7seg_init + */ +typedef enum { + DIGIT7SEG_OK = 0, /**< All ok */ + DIGIT7SEG_ERR_A_GPIO, /**< Something went wrong with A GPIO */ + DIGIT7SEG_ERR_B_GPIO, /**< Something went wrong with B GPIO */ + DIGIT7SEG_ERR_C_GPIO, /**< Something went wrong with C GPIO */ + DIGIT7SEG_ERR_D_GPIO, /**< Something went wrong with D GPIO */ + DIGIT7SEG_ERR_E_GPIO, /**< Something went wrong with E GPIO */ + DIGIT7SEG_ERR_F_GPIO, /**< Something went wrong with F GPIO */ + DIGIT7SEG_ERR_G_GPIO, /**< Something went wrong with G GPIO */ + DIGIT7SEG_ERR_DP_GPIO, /**< Something went wrong with DP GPIO */ + DIGIT7SEG_ERR_DIG1_GPIO, /**< Something went wrong with DIG1 GPIO */ + DIGIT7SEG_ERR_DIG2_GPIO, /**< Something went wrong with DIG2 GPIO */ + DIGIT7SEG_ERR_DIG3_GPIO, /**< Something went wrong with DIG3 GPIO */ + DIGIT7SEG_ERR_DIG4_GPIO, /**< Something went wrong with DIG4 GPIO */ + DIGIT7SEG_ERR_DIGITS, /**< Something went wrong with digits value */ +} digit7seg_error_codes; + +/** + * @brief Device initialization parameters, each data_* pin representing + * a segment and dig* representing a digit pin to turn on. + * ``` + * --a-- + * | | + * f b + * | | + * --g-- + * | | + * e c + * | | + * --d-- .dp + *``` + */ +typedef struct { + gpio_t data_a; /**< Data input pin */ + gpio_t data_b; /**< Data input pin */ + gpio_t data_c; /**< Data input pin */ + gpio_t data_d; /**< Data input pin */ + gpio_t data_e; /**< Data input pin */ + gpio_t data_f; /**< Data input pin */ + gpio_t data_g; /**< Data input pin */ + gpio_t data_dp; /**< Data input pin */ + gpio_t dig1; /**< Data input pin */ + gpio_t dig2; /**< Data input pin */ + gpio_t dig3; /**< Data input pin */ + gpio_t dig4; /**< Data input pin */ + tim_t timer; /**< Timer to use in digit7seg_power* */ + uint8_t digits; /**< Number of digits */ +} digit7seg_params_t; + +/** + * @brief Device descriptor for the DIGIT7SEG + */ +typedef struct { + digit7seg_params_t params; /**< Device parameters */ + uint32_t value; /**< Binary value to display */ + uint8_t current_digit; /**< Displayed digit */ + uint8_t status; /**< Status of the timer_periodic for this dev */ +} digit7seg_t; + +/** + * @brief Initialize the given DIGIT7SEG + * + * @param[out] dev Initialized device descriptor of DIGIT7SEG device + * @param[in] params Device parameters to use + * + * @return 0 on success or negative error code, see #digit7seg_error_codes + */ +int digit7seg_init(digit7seg_t *dev, const digit7seg_params_t *params); + +/** + * @brief Shift the current digit off and the next one on + * + * @param[in] dev Initialized device descriptor of DIGIT7SEG device + * + */ +int digit7seg_shift(digit7seg_t *dev); + +/** + * @brief Set the value for every digit + * + * @param[in] dev Initialized device descriptor of DIGIT7SEG device + * @param[in] value the value as an uint32_t + * + */ +int digit7seg_set_all_value(digit7seg_t *dev, uint32_t value); + +/** + * @brief Set the value for one digit + * + * @param[in] dev Initialized device descriptor of DIGIT7SEG device + * @param[in] index the digit number between [0, DIGIT7SEG_MAX_DIGITS[ + * @param[in] value the value as an uint8_t, a is equal to the first bit + * b the second.... dp the 8th bit. + * + */ +int digit7seg_set_value(digit7seg_t *dev, int index, uint8_t value); + +/** + * @brief Start an periodic timer event to shift between each 7seg + * + * @param[in] dev Initialized device descriptor of DIGIT7SEG device + * + * @return -1 if the digit was already power on + */ +int digit7seg_poweron(digit7seg_t *dev); + +/** + * @brief Stop an periodic timer event to shift between each 7seg + * + * @param[in] dev Initialized device descriptor of DIGIT7SEG device + * + * @return -1 if the digit was already power off + */ +int digit7seg_poweroff(digit7seg_t *dev); + +#ifdef __cplusplus +} +#endif + +#endif /* DIGIT7SEG_H */ +/** @} */ diff --git a/drivers/include/sha5461as.h b/drivers/include/sha5461as.h deleted file mode 100644 index 04dd0d7cc92f..000000000000 --- a/drivers/include/sha5461as.h +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Copyright (C) 2024 Orange - * - * This file is subject to the terms and conditions of the GNU Lesser - * General Public License v2.1. See the file LICENSE in the top level - * directory for more details. - */ - -/** - * @defgroup drivers_sha5461as SHA5461AS - * @ingroup drivers_display - * @brief Device driver interface for the SHA5461AS 7 segments display - * @{ - * - * @file - * @brief Device driver interface for the SHA5461AS 7 segments display - * - * @author Pierre Le Meur - */ - -#ifndef SHA5461AS_H -#define SHA5461AS_H - -#include - -#include "periph/gpio.h" -#include "macros/units.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define SHA5461AS_MAX_DIGITS (4) /**< Max pin number, can be below or equal */ -#ifndef XTIMER_DEV -#define XTIMER_DEV TIMER_DEV(0) /**< default timer */ -#endif -#ifndef XTIMER_HZ -#define XTIMER_HZ MHZ(1) /**< default timer hz */ -#endif -#ifndef SHA5461AS_DELAY -#define SHA5461AS_DELAY (XTIMER_HZ / 2600) /**< with 1MHz and a cycle as 2600 we got 2.6ms each shift, so - undetectable frequency by human eyes*/ -#endif -#define TIMER_RUNNING (1) /**< status when timer is running */ -#define TIMER_STOPPED (0) /**< status when timer is stopped */ - -/** - * @brief Return codes - */ -enum { - SHA5461AS_OK = 0, /**< All ok */ - SHA5461AS_ERR_A_GPIO, /**< Something went wrong with A GPIO */ - SHA5461AS_ERR_B_GPIO, /**< Something went wrong with B GPIO */ - SHA5461AS_ERR_C_GPIO, /**< Something went wrong with C GPIO */ - SHA5461AS_ERR_D_GPIO, /**< Something went wrong with D GPIO */ - SHA5461AS_ERR_E_GPIO, /**< Something went wrong with E GPIO */ - SHA5461AS_ERR_F_GPIO, /**< Something went wrong with F GPIO */ - SHA5461AS_ERR_G_GPIO, /**< Something went wrong with G GPIO */ - SHA5461AS_ERR_DP_GPIO, /**< Something went wrong with DP GPIO */ - SHA5461AS_ERR_DIG1_GPIO, /**< Something went wrong with DIG1 GPIO */ - SHA5461AS_ERR_DIG2_GPIO, /**< Something went wrong with DIG2 GPIO */ - SHA5461AS_ERR_DIG3_GPIO, /**< Something went wrong with DIG3 GPIO */ - SHA5461AS_ERR_DIG4_GPIO, /**< Something went wrong with DIG4 GPIO */ - SHA5461AS_ERR_DIGITS, /**< Something went wrong with digits value */ -}; - -/** - * @brief Device initialization parameters - */ -typedef struct { - gpio_t a; /**< Data input pin */ - gpio_t b; /**< Data input pin */ - gpio_t c; /**< Data input pin */ - gpio_t d; /**< Data input pin */ - gpio_t e; /**< Data input pin */ - gpio_t f; /**< Data input pin */ - gpio_t g; /**< Data input pin */ - gpio_t dp; /**< Data input pin */ - gpio_t dig1; /**< Data input pin */ - gpio_t dig2; /**< Data input pin */ - gpio_t dig3; /**< Data input pin */ - gpio_t dig4; /**< Data input pin */ -} sha5461as_params_t; - -/** - * @brief Device descriptor for the SHA5461AS - */ -typedef struct { - sha5461as_params_t params; /**< Device parameters */ - uint32_t value; /**< Binary value to display */ - uint8_t digits; /**< Number of digits */ - uint8_t current_digit; /**< Displayed digit */ - int status; /**< Status of the timer_periodic for this dev */ -} sha5461as_t; - -/** - * @brief Initialize the given DSP0401 - * - * @param[out] dev Initialized device descriptor of SHA5461AS device - * @param[in] params Device parameters to use - * - * @return SHA5461AS_OK if everything is good - * @return -SHA5461AS_ERR_A_GPIO if an error occurred during A GPIO - * @return -SHA5461AS_ERR_B_GPIO if an error occurred during B GPIO - * @return -SHA5461AS_ERR_C_GPIO if an error occurred during C GPIO - * @return -SHA5461AS_ERR_D_GPIO if an error occurred during D GPIO - * @return -SHA5461AS_ERR_E_GPIO if an error occurred during E GPIO - * @return -SHA5461AS_ERR_F_GPIO if an error occurred during F GPIO - * @return -SHA5461AS_ERR_G_GPIO if an error occurred during G GPIO - * @return -SHA5461AS_ERR_DP_GPIO if an error occurred during DP GPIO - * @return -SHA5461AS_ERR_DIG1_GPIO if an error occurred during DIG1 GPIO - * @return -SHA5461AS_ERR_DIG2_GPIO if an error occurred during DIG2 GPIO - * @return -SHA5461AS_ERR_DIG3_GPIO if an error occurred during DIG3 GPIO - * @return -SHA5461AS_ERR_DIG4_GPIO if an error occurred during DIG4 GPIO - */ -int sha5461as_init(sha5461as_t *dev, const sha5461as_params_t *params); - -/** - * @brief Shift the current digit off and the next one on - * - * @param[in] dev Initialized device descriptor of SHA5461AS device - * - */ -int sha5461as_shift(sha5461as_t *dev); - -/** - * @brief Set the value for every digit - * - * @param[in] dev Initialized device descriptor of SHA5461AS device - * @param[in] value the value as an uint32_t - * - */ -int sha5461as_set_all_value(sha5461as_t *dev, uint32_t value); - -/** - * @brief Set the value for one digit - * - * @param[in] dev Initialized device descriptor of SHA5461AS device - * @param[in] index the digit number between [0, SHA5461AS_DIGITS[ - * @param[in] value the value as an uint8_t, A is equal to the first bit - * B the second.... DP the 8th bit. - * - */ -int sha5461as_set_value(sha5461as_t *dev, int index, uint8_t value); - -/** - * @brief Start/stop an periodic timer event to shift between each 7seg - * - * @param[in] dev Initialized device descriptor of SHA5461AS device - */ -int sha5461as_display(sha5461as_t *dev); - -#ifdef __cplusplus -} -#endif - -#endif /* SHA5461AS_H */ -/** @} */ diff --git a/drivers/sha5461as/Makefile.include b/drivers/sha5461as/Makefile.include deleted file mode 100644 index 268a19379bba..000000000000 --- a/drivers/sha5461as/Makefile.include +++ /dev/null @@ -1,2 +0,0 @@ -USEMODULE_INCLUDES_sha5461as := $(LAST_MAKEFILEDIR)/include -USEMODULE_INCLUDES += $(USEMODULE_INCLUDES_sha5461as) diff --git a/drivers/sha5461as/include/sha5461as_params.h b/drivers/sha5461as/include/sha5461as_params.h deleted file mode 100644 index a3ac201f8953..000000000000 --- a/drivers/sha5461as/include/sha5461as_params.h +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Copyright (C) 2024 Orange - * - * This file is subject to the terms and conditions of the GNU Lesser - * General Public License v2.1. See the file LICENSE in the top level - * directory for more details. - */ - -/** - * @ingroup drivers_sha5461as - * @{ - * - * @file - * @brief Default configuration for SHA5461AS - * - * @author Pierre Le Meur - * - */ - -#ifndef SHA5461AS_PARAMS_H -#define SHA5461AS_PARAMS_H - -#include "sha5461as.h" -#include "periph/gpio.h" - -#ifdef __cplusplus -extern "C" { -#endif - - -/** - * @name Set default configuration parameters for the SHA5461AS (for arduino-nano-33-ble) - * @{ - */ -#ifndef SHA5461AS_PARAM_A_PIN -/** - * @brief SHA5461AS the A pin - */ -#define SHA5461AS_PARAM_A_PIN GPIO_PIN(1, 11) /* D2 */ -#endif -#ifndef SHA5461AS_PARAM_B_PIN -/** - * @brief SHA5461AS the B pin - */ -#define SHA5461AS_PARAM_B_PIN GPIO_PIN(1, 12) /* D3 */ -#endif -#ifndef SHA5461AS_PARAM_C_PIN -/** - * @brief SHA5461AS the C pin - */ -#define SHA5461AS_PARAM_C_PIN GPIO_PIN(1, 15) /* D4 */ -#endif -#ifndef SHA5461AS_PARAM_D_PIN -/** - * @brief SHA5461AS the D pin - */ -#define SHA5461AS_PARAM_D_PIN GPIO_PIN(1, 13) /* D5 */ -#endif -#ifndef SHA5461AS_PARAM_E_PIN -/** - * @brief SHA5461AS the E pin - */ -#define SHA5461AS_PARAM_E_PIN GPIO_PIN(1, 14) /* D6 */ -#endif -#ifndef SHA5461AS_PARAM_F_PIN -/** - * @brief SHA5461AS the F pin - */ -#define SHA5461AS_PARAM_F_PIN GPIO_PIN(0, 23) /* D7 */ -#endif -#ifndef SHA5461AS_PARAM_G_PIN -/** - * @brief SHA5461AS the G pin - */ -#define SHA5461AS_PARAM_G_PIN GPIO_PIN(0, 21) /* D8 */ -#endif -#ifndef SHA5461AS_PARAM_DP_PIN -/** - * @brief SHA5461AS the DP pin - */ -#define SHA5461AS_PARAM_DP_PIN GPIO_PIN(0, 27) /* D9 */ -#endif - -#ifndef SHA5461AS_PARAM_DIG1_PIN -/** - * @brief SHA5461AS the first digit pin - */ -#define SHA5461AS_PARAM_DIG1_PIN GPIO_PIN(1, 2) /* D10 */ -#endif -#ifndef SHA5461AS_PARAM_DIG2_PIN -/** - * @brief SHA5461AS the second digit pin - */ -#define SHA5461AS_PARAM_DIG2_PIN GPIO_PIN(1, 1) /* D11 */ -#endif -#ifndef SHA5461AS_PARAM_DIG3_PIN -/** - * @brief SHA5461AS the third digit pin - */ -#define SHA5461AS_PARAM_DIG3_PIN GPIO_PIN(1, 8) /* D12 */ -#endif -#ifndef SHA5461AS_PARAM_DIG4_PIN -/** - * @brief SHA5461AS the fourth digit pin - */ -#define SHA5461AS_PARAM_DIG4_PIN GPIO_PIN(0, 13) /* D13 */ -#endif - -#ifndef SHA5461AS_PARAMS -/** - * @brief The initialization parameters for the SHA5461AS driver - */ -#define SHA5461AS_PARAMS { .a = SHA5461AS_PARAM_A_PIN, \ - .b = SHA5461AS_PARAM_B_PIN, \ - .c = SHA5461AS_PARAM_C_PIN, \ - .d = SHA5461AS_PARAM_D_PIN, \ - .e = SHA5461AS_PARAM_E_PIN, \ - .f = SHA5461AS_PARAM_F_PIN, \ - .g = SHA5461AS_PARAM_G_PIN, \ - .dp = SHA5461AS_PARAM_DP_PIN, \ - .dig1 = SHA5461AS_PARAM_DIG1_PIN, \ - .dig2 = SHA5461AS_PARAM_DIG2_PIN, \ - .dig3 = SHA5461AS_PARAM_DIG3_PIN, \ - .dig4 = SHA5461AS_PARAM_DIG4_PIN } - -#endif -/**@}*/ - -/** - * @brief Configure SHA5461AS - */ -static const sha5461as_params_t sha5461as_params[] = -{ - SHA5461AS_PARAMS, -}; - -#ifdef __cplusplus -} -#endif - -#endif /* SHA5461AS_PARAMS_H */ -/** @} */ diff --git a/drivers/sha5461as/sha5461as.c b/drivers/sha5461as/sha5461as.c deleted file mode 100644 index ebc2cb0f0ffc..000000000000 --- a/drivers/sha5461as/sha5461as.c +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Copyright (C) 2024 Orange - * - * This file is subject to the terms and conditions of the GNU Lesser - * General Public License v2.1. See the file LICENSE in the top level - * directory for more details. - */ - -/** - * @ingroup drivers_sha5461as - * @{ - * - * @file - * @brief Device driver interface for the SHA5461AS 7 segments display - * - * @author Pierre Le Meur - * - * @} - */ -#include - -#include "sha5461as.h" - -#include "board.h" -#include "periph/gpio.h" -#include "periph/timer.h" - -#define ENABLE_DEBUG 0 -#include "debug.h" - -#define PIN_A (dev->params.a) -#define PIN_B (dev->params.b) -#define PIN_C (dev->params.c) -#define PIN_D (dev->params.d) -#define PIN_E (dev->params.e) -#define PIN_F (dev->params.f) -#define PIN_G (dev->params.g) -#define PIN_DP (dev->params.dp) -#define PIN_DIG1 (dev->params.dig1) -#define PIN_DIG2 (dev->params.dig2) -#define PIN_DIG3 (dev->params.dig3) -#define PIN_DIG4 (dev->params.dig4) - -#define NB_PIN (12) - -static void _set_pin_value(sha5461as_t *dev) -{ - const gpio_t pins[] = - { - PIN_A, PIN_B, PIN_C, PIN_D, PIN_E, PIN_F, PIN_G, PIN_DP - }; - - uint8_t mask = 0xFF; - uint8_t current_value = (uint8_t)(dev->value >> (dev->current_digit * 8) & mask); - - for (int i = 1; i <= 8; i++) { - if (current_value & (1 << (8 - i))) { - gpio_set(pins[i - 1]); - DEBUG("PIN SET\n"); - } - else { - gpio_clear(pins[i - 1]); - DEBUG("PIN CLEAR\n"); - } - } -} - -static void _shift_display(void *arg, int chan) -{ - (void)chan; - sha5461as_t *dev = (sha5461as_t *)arg; - - const gpio_t digit_pins[] = - { - PIN_DIG1, PIN_DIG2, PIN_DIG3, PIN_DIG4 - }; - - gpio_clear(digit_pins[dev->current_digit]); - dev->current_digit += 1; - dev->current_digit = dev->current_digit % dev->digits; - gpio_set(digit_pins[dev->current_digit]); - - _set_pin_value(dev); -} - -int sha5461as_init(sha5461as_t *dev, const sha5461as_params_t *params) -{ - dev->params = *params; - dev->current_digit = 0; - - if (dev->digits <= 0 || dev->digits > SHA5461AS_MAX_DIGITS) { - return -SHA5461AS_ERR_DIGITS; - } - - const gpio_t pins[] = - { - PIN_A, PIN_B, PIN_C, PIN_D, PIN_E, PIN_F, PIN_G, PIN_DP, - PIN_DIG1, PIN_DIG2, PIN_DIG3, PIN_DIG4 - }; - - const int pin_errs[] = - { - SHA5461AS_ERR_A_GPIO, SHA5461AS_ERR_B_GPIO, SHA5461AS_ERR_C_GPIO, SHA5461AS_ERR_D_GPIO, - SHA5461AS_ERR_E_GPIO, SHA5461AS_ERR_F_GPIO, SHA5461AS_ERR_G_GPIO, SHA5461AS_ERR_DP_GPIO, - SHA5461AS_ERR_DIG1_GPIO, SHA5461AS_ERR_DIG2_GPIO, SHA5461AS_ERR_DIG3_GPIO, - SHA5461AS_ERR_DIG4_GPIO - }; - - for (int i = 0; i < NB_PIN; i++) { - if (gpio_init(pins[i], GPIO_OUT) < 0) { - DEBUG("[Error] Initializing error"); - return pin_errs[i]; - } - gpio_clear(pins[i]); - } - - return SHA5461AS_OK; -} - -int sha5461as_shift(sha5461as_t *dev) -{ - _shift_display(dev, 0); - return 1; -} - -int sha5461as_set_all_value(sha5461as_t *dev, uint32_t value) -{ - dev->value = value; - return 1; -} - -int sha5461as_set_value(sha5461as_t *dev, int index, uint8_t value) -{ - value <<= (index * 8); - uint32_t up_value = dev->value >> ((index + 1) * 8); - uint32_t down_value = ((0b00000001 << (index * 8)) - 1) & dev->value; - - dev->value = up_value | value | down_value; - - return 1; -} - -int sha5461as_display(sha5461as_t *dev) -{ - if (dev->status == TIMER_RUNNING) { - timer_stop(XTIMER_DEV); - dev->status = TIMER_STOPPED; - return 0; - } - - if (timer_init(XTIMER_DEV, XTIMER_HZ, _shift_display, dev) != 0) { - return -1; - } - - timer_set_periodic(XTIMER_DEV, 0, SHA5461AS_DELAY, TIM_FLAG_RESET_ON_MATCH); - - dev->status = TIMER_RUNNING; - return 0; -} diff --git a/tests/drivers/sha5461as/Makefile b/tests/drivers/digit7seg/Makefile similarity index 83% rename from tests/drivers/sha5461as/Makefile rename to tests/drivers/digit7seg/Makefile index 8e4267ef52ce..a0a2883deeee 100644 --- a/tests/drivers/sha5461as/Makefile +++ b/tests/drivers/digit7seg/Makefile @@ -1,6 +1,6 @@ include ../Makefile.drivers_common -USEMODULE += sha5461as +USEMODULE += digit7seg USEMODULE += ztimer USEMODULE += ztimer_usec diff --git a/tests/drivers/digit7seg/README.md b/tests/drivers/digit7seg/README.md new file mode 100644 index 000000000000..a8241eccd0a1 --- /dev/null +++ b/tests/drivers/digit7seg/README.md @@ -0,0 +1,38 @@ +## About + +This is a test application designed for the [5461AS](http://www.topliteusa.com/uploadfile/2014/0825/A-5461AS.pdf), +but can be applied to every less than 5 digits with 7 segments display without IC. + +## Connecting the device + +The display needs to be connected as follows: + +- Connect all the pins `X` to the board `DIGIT7SEG_PARAM_X_PIN` +- Connect `DP` to the board `DIGIT7SEG_PARAM_DP_PIN` +- Connect all the pins `DIGX` to the board `DIGIT7SEG_PARAM_DIGX_PIN` + +For each digit pin (`DIGX`), you need to use a transistor and link it to `GND`. + +@note T represent a NPN transistor +``` + .----.----.----.----. + | | | | |-- a + | | | | |-- b + | | | | |-- c + | | | | |-- d + | | | | |-- e + | | | | |-- f + | | | | |-- g + | | | | |-- dp + '----'----'----'----' + | | | | + T-------------------- dig1 + | | | | + | T--------------- dig2 + | | | | + | | T---------- dig3 + | | | | + | | | T----- dig4 + | | | | + └──--└──--└──--└──--- GND +``` diff --git a/tests/drivers/sha5461as/main.c b/tests/drivers/digit7seg/main.c similarity index 50% rename from tests/drivers/sha5461as/main.c rename to tests/drivers/digit7seg/main.c index 2d4ed7c934ac..77b3759bd648 100644 --- a/tests/drivers/sha5461as/main.c +++ b/tests/drivers/digit7seg/main.c @@ -11,31 +11,29 @@ * @{ * * @file - * @brief Test application for the SHT5461AS 4x7seg + * @brief Test application for a 4x7seg * * @author Pierre Le Meur * * @} */ - #include #include -#include "sha5461as_params.h" -#include "sha5461as.h" +#include "digit7seg_params.h" +#include "digit7seg.h" #include "timex.h" #include "ztimer.h" -#define TEST_DELAY_US (1U * US_PER_SEC) /* 2 seconds delay between each test */ +#define TEST_DELAY_US (1U * US_PER_SEC) /* 1 seconds delay between each test */ int main(void) { - sha5461as_t dev; - dev.digits = SHA5461AS_MAX_DIGITS; + digit7seg_t dev; - puts("Initializing SHA5461AS device."); - if (sha5461as_init(&dev, &sha5461as_params[0]) != 0) { + puts("Initializing the digit 7 seg device."); + if (digit7seg_init(&dev, &digit7seg_params[0]) != 0) { puts("[Error] Cannot initialize DSP0401 display."); return 1; } @@ -46,14 +44,14 @@ int main(void) gpio_set(dev.params.dig2); gpio_set(dev.params.dig3); gpio_set(dev.params.dig4); - gpio_set(dev.params.a); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); - gpio_set(dev.params.b); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); - gpio_set(dev.params.c); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); - gpio_set(dev.params.d); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); - gpio_set(dev.params.e); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); - gpio_set(dev.params.f); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); - gpio_set(dev.params.g); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); - gpio_set(dev.params.dp); + gpio_set(dev.params.data_a); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.data_b); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.data_c); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.data_d); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.data_e); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.data_f); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.data_g); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); + gpio_set(dev.params.data_dp); gpio_clear(dev.params.dig2); gpio_clear(dev.params.dig3); @@ -63,24 +61,33 @@ int main(void) /* R I O T */ /* 11101110 00001100 11111100 00001110 */ uint32_t binary_riot = 0b11101110000011001111110000001110; - sha5461as_set_all_value(&dev, binary_riot); + digit7seg_set_all_value(&dev, binary_riot); - if (sha5461as_display(&dev) == 0) { + if (digit7seg_poweron(&dev) == 0) { puts("Launched..."); } else { puts("Error"); } - for (volatile uint32_t i = 0; i < CLOCK_CORECLOCK / 5; ++i) {} + ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US * 3); - if (sha5461as_display(&dev) == 0) { + if (digit7seg_poweroff(&dev) == 0) { puts("...Stopped"); } else { puts("Error"); } + ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US * 3); + + if (digit7seg_poweron(&dev) == 0) { + puts("Launched."); + } + else { + puts("Error"); + } + while (1) {} return 0; diff --git a/tests/drivers/sha5461as/README.md b/tests/drivers/sha5461as/README.md deleted file mode 100644 index 464e3ae25040..000000000000 --- a/tests/drivers/sha5461as/README.md +++ /dev/null @@ -1,16 +0,0 @@ -## About - -This is a test application for the [SHA5461AS](https://www.datasheetcafe.com/wp-content/uploads/2019/01/SH5461AS-datasheet.pdf), -a 4 digits with 7 segments display. - -## Connecting the device - -The display needs to be connected as follows: - -- Connect all the pins `X` to the board `SH5461AS_PARAM_X_PIN` -- Connect `DP` to the board `SHA5461AS_PARAM_DP_PIN` -- Connect all the pins `DIGX` to the board `SHA5461AS_PARAM_DIGX_PIN` - -For each digit pin (`DIGX`), you need to use a transistor and link it to `GND`. - -![datasheet image](https://www.datasheetcafe.com/wp-content/uploads/2015/09/SH5461AS.gif) From 4d541ba50a3891b0ca45f5f8fefa1344760cec56 Mon Sep 17 00:00:00 2001 From: Pierre Le Meur Date: Fri, 15 Nov 2024 09:47:00 +0100 Subject: [PATCH 05/10] drivers/digit7seg: initialize all var of digit7seg_t in digit7seg_init --- drivers/digit7seg/digit7seg.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/digit7seg/digit7seg.c b/drivers/digit7seg/digit7seg.c index 89916b9d71d1..979fc348ebd2 100644 --- a/drivers/digit7seg/digit7seg.c +++ b/drivers/digit7seg/digit7seg.c @@ -81,6 +81,7 @@ int digit7seg_init(digit7seg_t *dev, const digit7seg_params_t *params) dev->params = *params; dev->current_digit = 0; dev->value = 0; + dev->status = TIMER_STOPPED; if (dev->params.digits <= 0 || dev->params.digits > DIGIT7SEG_MAX_DIGITS) { DEBUG("[Error] Invalid number of digit.\n"); From 37ceefb9d1b92d35f9ede4ace3c13417294ab28f Mon Sep 17 00:00:00 2001 From: Pierre Le Meur Date: Fri, 15 Nov 2024 10:48:09 +0100 Subject: [PATCH 06/10] drivers/digit7seg: change _set_pin_value function drivers/digit7seg: add return value doc in header and remove unused params in digit7seg_t drivers/digit7seg: add return value doc in header drivers/digit7seg: add return value doc in header --- drivers/digit7seg/digit7seg.c | 45 +++++++++------------------------- drivers/digit7seg/doc.txt | 2 +- drivers/include/digit7seg.h | 26 ++++++-------------- tests/drivers/digit7seg/main.c | 11 +++------ 4 files changed, 24 insertions(+), 60 deletions(-) diff --git a/drivers/digit7seg/digit7seg.c b/drivers/digit7seg/digit7seg.c index 979fc348ebd2..4a30048f79ca 100644 --- a/drivers/digit7seg/digit7seg.c +++ b/drivers/digit7seg/digit7seg.c @@ -33,6 +33,7 @@ /** The 7 segments + decimal point + the number of digits */ #define NB_PIN (8+dev->params.digits) +#define BYTE_BITS (0x08) static void _set_pin_value(digit7seg_t *dev) { @@ -44,13 +45,13 @@ static void _set_pin_value(digit7seg_t *dev) uint8_t mask = 0xFF; uint8_t current_value = (uint8_t)(dev->value >> (dev->current_digit * 8) & mask); - for (int i = 1; i <= 8; i++) { - if (current_value & (1 << (8 - i))) { - gpio_set(pins[i - 1]); + for (int i = 0; i < BYTE_BITS; i++) { + if (current_value & (1 << i)) { + gpio_set(pins[i]); DEBUG("PIN SET\n"); } else { - gpio_clear(pins[i - 1]); + gpio_clear(pins[i]); DEBUG("PIN CLEAR\n"); } } @@ -81,7 +82,6 @@ int digit7seg_init(digit7seg_t *dev, const digit7seg_params_t *params) dev->params = *params; dev->current_digit = 0; dev->value = 0; - dev->status = TIMER_STOPPED; if (dev->params.digits <= 0 || dev->params.digits > DIGIT7SEG_MAX_DIGITS) { DEBUG("[Error] Invalid number of digit.\n"); @@ -119,16 +119,9 @@ int digit7seg_init(digit7seg_t *dev, const digit7seg_params_t *params) return DIGIT7SEG_OK; } -int digit7seg_shift(digit7seg_t *dev) -{ - _shift_display(dev, 0); - return 0; -} - -int digit7seg_set_all_value(digit7seg_t *dev, uint32_t value) +void digit7seg_set_all_value(digit7seg_t *dev, uint32_t value) { dev->value = value; - return 0; } int digit7seg_set_value(digit7seg_t *dev, int index, uint8_t value) @@ -137,10 +130,10 @@ int digit7seg_set_value(digit7seg_t *dev, int index, uint8_t value) return -1; } - uint32_t temp_value = value << (index * 8); - uint32_t up_value = dev->value >> ((index + 1) * 8); - up_value <<= ((index + 1) * 8); - uint32_t down_value = ((0b00000001 << (index * 8)) - 1) & dev->value; + uint32_t temp_value = value << (index * BYTE_BITS); + uint32_t up_value = dev->value >> ((index + 1) * BYTE_BITS); + up_value <<= ((index + 1) * BYTE_BITS); + uint32_t down_value = ((0b00000001 << (index * BYTE_BITS)) - 1) & dev->value; dev->value = up_value | temp_value | down_value; @@ -149,31 +142,17 @@ int digit7seg_set_value(digit7seg_t *dev, int index, uint8_t value) int digit7seg_poweron(digit7seg_t *dev) { - if (dev->status == TIMER_RUNNING) { - DEBUG("[Error] Timer is already running.\n"); - return -1; - } if (timer_init(dev->params.timer, DIGIT7SEG_TIMER_HZ, _shift_display, dev) != 0) { - DEBUG("[Error] Impossible to init timer.\n"); + DEBUG("[Error] Not possible to init timer.\n"); return -1; } timer_set_periodic(dev->params.timer, 0, DIGIT7SEG_DELAY, TIM_FLAG_RESET_ON_MATCH); - - dev->status = TIMER_RUNNING; return 0; } -int digit7seg_poweroff(digit7seg_t *dev) +void digit7seg_poweroff(digit7seg_t *dev) { - if (dev->status == TIMER_STOPPED) { - DEBUG("[Error] Timer is already stopped.\n"); - return -1; - } - timer_stop(dev->params.timer); - dev->status = TIMER_STOPPED; - - return 0; } diff --git a/drivers/digit7seg/doc.txt b/drivers/digit7seg/doc.txt index 0daeb9aa7c60..6f07d8e331b0 100644 --- a/drivers/digit7seg/doc.txt +++ b/drivers/digit7seg/doc.txt @@ -77,7 +77,7 @@ and turn on each digit at a rate that is imperceptible to the human eye. ## Error Handling -All driver functions return 0 on success or one of a negative error code defined by +Most of driver functions return 0 on success or one of a negative error code defined by #digit7seg_error_codes. ## Usage diff --git a/drivers/include/digit7seg.h b/drivers/include/digit7seg.h index 1dca430ccce3..9dd29b464224 100644 --- a/drivers/include/digit7seg.h +++ b/drivers/include/digit7seg.h @@ -20,9 +20,9 @@ #include +#include "macros/units.h" #include "periph/gpio.h" #include "periph/timer.h" -#include "macros/units.h" #ifdef __cplusplus extern "C" { @@ -39,8 +39,6 @@ extern "C" { * */ # define DIGIT7SEG_DELAY (DIGIT7SEG_TIMER_HZ / 2600) #endif -#define TIMER_RUNNING (1) /**< status when timer is running */ -#define TIMER_STOPPED (0) /**< status when timer is stopped */ /** * @brief Return codes for @ref digit7seg_init @@ -101,27 +99,18 @@ typedef struct { digit7seg_params_t params; /**< Device parameters */ uint32_t value; /**< Binary value to display */ uint8_t current_digit; /**< Displayed digit */ - uint8_t status; /**< Status of the timer_periodic for this dev */ } digit7seg_t; /** * @brief Initialize the given DIGIT7SEG * - * @param[out] dev Initialized device descriptor of DIGIT7SEG device + * @param[out] dev DIGIT7SEG device descriptor to initialize * @param[in] params Device parameters to use * * @return 0 on success or negative error code, see #digit7seg_error_codes */ int digit7seg_init(digit7seg_t *dev, const digit7seg_params_t *params); -/** - * @brief Shift the current digit off and the next one on - * - * @param[in] dev Initialized device descriptor of DIGIT7SEG device - * - */ -int digit7seg_shift(digit7seg_t *dev); - /** * @brief Set the value for every digit * @@ -129,7 +118,7 @@ int digit7seg_shift(digit7seg_t *dev); * @param[in] value the value as an uint32_t * */ -int digit7seg_set_all_value(digit7seg_t *dev, uint32_t value); +void digit7seg_set_all_value(digit7seg_t *dev, uint32_t value); /** * @brief Set the value for one digit @@ -139,6 +128,8 @@ int digit7seg_set_all_value(digit7seg_t *dev, uint32_t value); * @param[in] value the value as an uint8_t, a is equal to the first bit * b the second.... dp the 8th bit. * + * @return 0 on success + * @return -1 when an incorrect digit is passed */ int digit7seg_set_value(digit7seg_t *dev, int index, uint8_t value); @@ -147,7 +138,8 @@ int digit7seg_set_value(digit7seg_t *dev, int index, uint8_t value); * * @param[in] dev Initialized device descriptor of DIGIT7SEG device * - * @return -1 if the digit was already power on + * @return 0 on success + * @return -1 if the timer was impossible to start */ int digit7seg_poweron(digit7seg_t *dev); @@ -155,10 +147,8 @@ int digit7seg_poweron(digit7seg_t *dev); * @brief Stop an periodic timer event to shift between each 7seg * * @param[in] dev Initialized device descriptor of DIGIT7SEG device - * - * @return -1 if the digit was already power off */ -int digit7seg_poweroff(digit7seg_t *dev); +void digit7seg_poweroff(digit7seg_t *dev); #ifdef __cplusplus } diff --git a/tests/drivers/digit7seg/main.c b/tests/drivers/digit7seg/main.c index 77b3759bd648..19fb34abc0c2 100644 --- a/tests/drivers/digit7seg/main.c +++ b/tests/drivers/digit7seg/main.c @@ -59,8 +59,8 @@ int main(void) ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US); /* R I O T */ - /* 11101110 00001100 11111100 00001110 */ - uint32_t binary_riot = 0b11101110000011001111110000001110; + /* 01110111 00110000 00111111 01110000 */ + uint32_t binary_riot = 0b01110111001100000011111101110000; digit7seg_set_all_value(&dev, binary_riot); if (digit7seg_poweron(&dev) == 0) { @@ -72,12 +72,7 @@ int main(void) ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US * 3); - if (digit7seg_poweroff(&dev) == 0) { - puts("...Stopped"); - } - else { - puts("Error"); - } + digit7seg_poweroff(&dev); ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US * 3); From b42ef802b1f40d217e963dfabcab27427dba4d59 Mon Sep 17 00:00:00 2001 From: Pierre Le Meur Date: Mon, 25 Nov 2024 11:13:41 +0100 Subject: [PATCH 07/10] drivers/digit7seg: add asset in init --- drivers/digit7seg/digit7seg.c | 21 ++------------------- drivers/digit7seg/doc.txt | 7 +------ drivers/include/digit7seg.h | 14 +------------- 3 files changed, 4 insertions(+), 38 deletions(-) diff --git a/drivers/digit7seg/digit7seg.c b/drivers/digit7seg/digit7seg.c index 4a30048f79ca..c03dd67c1704 100644 --- a/drivers/digit7seg/digit7seg.c +++ b/drivers/digit7seg/digit7seg.c @@ -13,6 +13,7 @@ * @author Pierre Le Meur */ +#include #include "digit7seg.h" #define ENABLE_DEBUG 0 @@ -94,25 +95,8 @@ int digit7seg_init(digit7seg_t *dev, const digit7seg_params_t *params) PIN_DIG1, PIN_DIG2, PIN_DIG3, PIN_DIG4 }; - const int pin_errs[] = - { - DIGIT7SEG_ERR_A_GPIO, DIGIT7SEG_ERR_B_GPIO, DIGIT7SEG_ERR_C_GPIO, DIGIT7SEG_ERR_D_GPIO, - DIGIT7SEG_ERR_E_GPIO, DIGIT7SEG_ERR_F_GPIO, DIGIT7SEG_ERR_G_GPIO, DIGIT7SEG_ERR_DP_GPIO, - DIGIT7SEG_ERR_DIG1_GPIO, DIGIT7SEG_ERR_DIG2_GPIO, DIGIT7SEG_ERR_DIG3_GPIO, - DIGIT7SEG_ERR_DIG4_GPIO - }; - for (int i = 0; i < NB_PIN; i++) { - if (!gpio_is_valid(pins[i])) { - DEBUG("[Error] GPIO isn't valid.\n"); - return -pin_errs[i]; - } - - if (gpio_init(pins[i], GPIO_OUT) < 0) { - DEBUG("[Error] Initializing gpio error.\n"); - return -pin_errs[i]; - } - + assert(gpio_init(pins[i], GPIO_OUT)); gpio_clear(pins[i]); } @@ -142,7 +126,6 @@ int digit7seg_set_value(digit7seg_t *dev, int index, uint8_t value) int digit7seg_poweron(digit7seg_t *dev) { - if (timer_init(dev->params.timer, DIGIT7SEG_TIMER_HZ, _shift_display, dev) != 0) { DEBUG("[Error] Not possible to init timer.\n"); return -1; diff --git a/drivers/digit7seg/doc.txt b/drivers/digit7seg/doc.txt index 6f07d8e331b0..a5755de26b29 100644 --- a/drivers/digit7seg/doc.txt +++ b/drivers/digit7seg/doc.txt @@ -174,12 +174,7 @@ else { ... -if (digit7seg_poweroff(&dev) == 0) { - puts("...Stopped"); -} -else { - puts("Error"); -} +digit7seg_poweroff(&dev) ``` */ diff --git a/drivers/include/digit7seg.h b/drivers/include/digit7seg.h index 9dd29b464224..c063372cff30 100644 --- a/drivers/include/digit7seg.h +++ b/drivers/include/digit7seg.h @@ -44,19 +44,7 @@ extern "C" { * @brief Return codes for @ref digit7seg_init */ typedef enum { - DIGIT7SEG_OK = 0, /**< All ok */ - DIGIT7SEG_ERR_A_GPIO, /**< Something went wrong with A GPIO */ - DIGIT7SEG_ERR_B_GPIO, /**< Something went wrong with B GPIO */ - DIGIT7SEG_ERR_C_GPIO, /**< Something went wrong with C GPIO */ - DIGIT7SEG_ERR_D_GPIO, /**< Something went wrong with D GPIO */ - DIGIT7SEG_ERR_E_GPIO, /**< Something went wrong with E GPIO */ - DIGIT7SEG_ERR_F_GPIO, /**< Something went wrong with F GPIO */ - DIGIT7SEG_ERR_G_GPIO, /**< Something went wrong with G GPIO */ - DIGIT7SEG_ERR_DP_GPIO, /**< Something went wrong with DP GPIO */ - DIGIT7SEG_ERR_DIG1_GPIO, /**< Something went wrong with DIG1 GPIO */ - DIGIT7SEG_ERR_DIG2_GPIO, /**< Something went wrong with DIG2 GPIO */ - DIGIT7SEG_ERR_DIG3_GPIO, /**< Something went wrong with DIG3 GPIO */ - DIGIT7SEG_ERR_DIG4_GPIO, /**< Something went wrong with DIG4 GPIO */ + DIGIT7SEG_OK = 0, DIGIT7SEG_ERR_DIGITS, /**< Something went wrong with digits value */ } digit7seg_error_codes; From d548653defd0a722056c6d6ded7c30692602a212 Mon Sep 17 00:00:00 2001 From: Pierre Le Meur Date: Mon, 2 Dec 2024 15:31:16 +0100 Subject: [PATCH 08/10] drivers/digit7seg: Add assert on gpio --- drivers/digit7seg/digit7seg.c | 14 +++++++++----- drivers/include/digit7seg.h | 2 +- tests/drivers/digit7seg/main.c | 11 ----------- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/drivers/digit7seg/digit7seg.c b/drivers/digit7seg/digit7seg.c index c03dd67c1704..07e6957bfb49 100644 --- a/drivers/digit7seg/digit7seg.c +++ b/drivers/digit7seg/digit7seg.c @@ -49,11 +49,9 @@ static void _set_pin_value(digit7seg_t *dev) for (int i = 0; i < BYTE_BITS; i++) { if (current_value & (1 << i)) { gpio_set(pins[i]); - DEBUG("PIN SET\n"); } else { gpio_clear(pins[i]); - DEBUG("PIN CLEAR\n"); } } } @@ -73,8 +71,6 @@ static void _shift_display(void *arg, int chan) dev->current_digit = dev->current_digit % dev->params.digits; gpio_set(digit_pins[dev->current_digit]); - DEBUG("[INFO] On display %d\n", dev->current_digit); - _set_pin_value(dev); } @@ -95,9 +91,17 @@ int digit7seg_init(digit7seg_t *dev, const digit7seg_params_t *params) PIN_DIG1, PIN_DIG2, PIN_DIG3, PIN_DIG4 }; + const char* pins_debug[] = + { + "A", "B", "C", "D", "E", "F", "G", "DP", + "DIG1", "DIG2", "DIG3", "DIG4" + }; + for (int i = 0; i < NB_PIN; i++) { - assert(gpio_init(pins[i], GPIO_OUT)); + DEBUG("[DIGIT7SEG] Trying the pin %s:...", pins_debug[i]); + assert(gpio_init(pins[i], GPIO_OUT) == 0); gpio_clear(pins[i]); + DEBUG("Worked\n"); } return DIGIT7SEG_OK; diff --git a/drivers/include/digit7seg.h b/drivers/include/digit7seg.h index c063372cff30..49314aa97565 100644 --- a/drivers/include/digit7seg.h +++ b/drivers/include/digit7seg.h @@ -44,7 +44,7 @@ extern "C" { * @brief Return codes for @ref digit7seg_init */ typedef enum { - DIGIT7SEG_OK = 0, + DIGIT7SEG_OK = 0, /**< All ok */ DIGIT7SEG_ERR_DIGITS, /**< Something went wrong with digits value */ } digit7seg_error_codes; diff --git a/tests/drivers/digit7seg/main.c b/tests/drivers/digit7seg/main.c index 19fb34abc0c2..422ae5b647d8 100644 --- a/tests/drivers/digit7seg/main.c +++ b/tests/drivers/digit7seg/main.c @@ -74,16 +74,5 @@ int main(void) digit7seg_poweroff(&dev); - ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US * 3); - - if (digit7seg_poweron(&dev) == 0) { - puts("Launched."); - } - else { - puts("Error"); - } - - while (1) {} - return 0; } From 1d1da21b6f309be789bbbc349a513d171290baf3 Mon Sep 17 00:00:00 2001 From: Pierre Le Meur Date: Mon, 2 Dec 2024 16:26:57 +0100 Subject: [PATCH 09/10] drivers/digit7seg: sort import --- drivers/digit7seg/digit7seg.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/digit7seg/digit7seg.c b/drivers/digit7seg/digit7seg.c index 07e6957bfb49..41fa292cc745 100644 --- a/drivers/digit7seg/digit7seg.c +++ b/drivers/digit7seg/digit7seg.c @@ -14,11 +14,12 @@ */ #include -#include "digit7seg.h" -#define ENABLE_DEBUG 0 +#define ENABLE_DEBUG 0 #include "debug.h" +#include "digit7seg.h" + #define PIN_A (dev->params.data_a) #define PIN_B (dev->params.data_b) #define PIN_C (dev->params.data_c) From c2d13292662fec79d262601811c00280d3307602 Mon Sep 17 00:00:00 2001 From: Pierre Le Meur Date: Mon, 2 Dec 2024 21:37:38 +0100 Subject: [PATCH 10/10] drivers/digit7seg: Remove whitespace --- drivers/digit7seg/digit7seg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/digit7seg/digit7seg.c b/drivers/digit7seg/digit7seg.c index 41fa292cc745..bafee10ca131 100644 --- a/drivers/digit7seg/digit7seg.c +++ b/drivers/digit7seg/digit7seg.c @@ -15,7 +15,7 @@ #include -#define ENABLE_DEBUG 0 +#define ENABLE_DEBUG 0 #include "debug.h" #include "digit7seg.h"