1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 10:12:45 +01:00

cpu/native: enhance GPIO mocking with 2D array representation

This commit introduces a more robust GPIO mocking mechanism by utilizing
a 2-dimensional array. Each element of the array holds a gpio_mock_t
structure describing a pin's attributes such as value, mode, flank,
interruption callback, and callback argument.

This enhancement allows for the arbitrary simulation of GPIOs across
various microcontroller architectures using the current API, while
maintaining consistency through the use of the GPIO_PIN macro.

Additionally, it should be noted that only the maximum number of ports
and maximum number of pins can be altered according to the context.

The implemented API in gpio_mock.c remains rudimentary, providing no
validation but fulfilling the required functions. However, it remains
customizable as all its functions are marked as weak.

Signed-off-by: Gilles DOFFE <g.doffe@gmail.com>
This commit is contained in:
Gilles DOFFE 2024-02-27 00:16:20 +01:00
parent 24d9657b40
commit ebf95d2545
2 changed files with 85 additions and 12 deletions

View File

@ -96,6 +96,52 @@ typedef enum {
} gpio_flank_t;
/** @} */
#elif defined(MODULE_PERIPH_GPIO_MOCK)
/**
* @brief Mocked GPIO
*
* Mocked GPIO representation for simulation.
* @{
*/
typedef struct {
int value; /**< current value */
int mode; /**< current mode */
int flank; /**< flank to trigger interrupts */
void (*cb)(void *arg); /**< ISR */
void *arg; /**< ISR arg */
} gpio_mock_t;
/** @} */
#define GPIO_UNDEF 0
#ifndef GPIO_PORT_MAX
#define GPIO_PORT_MAX (16)
#endif
#ifndef GPIO_PIN_MAX
#define GPIO_PIN_MAX (32)
#endif
/**
* @brief Mocked GPIO array
*/
extern gpio_mock_t gpio_mock[GPIO_PORT_MAX][GPIO_PIN_MAX];
#define HAVE_GPIO_T
/**
* @brief Pointer on a mocked GPIO
*/
typedef gpio_mock_t* gpio_t;
/**
* @brief Define a custom GPIO_PIN macro for native mocked GPIO framework.
* Get the mocked GPIO object from mocked GPIO array.
*/
#define GPIO_PIN(port, pin) \
(((port >= 0) && (pin >= 0) && (port < GPIO_PORT_MAX) && (pin < GPIO_PIN_MAX)) \
? &gpio_mock[port][pin] \
: GPIO_UNDEF)
#endif /* MODULE_PERIPH_GPIO_LINUX | DOXYGEN */

View File

@ -19,23 +19,41 @@
#include "periph/gpio.h"
/**
* @brief Mocked GPIO array
*/
gpio_mock_t gpio_mock[GPIO_PORT_MAX][GPIO_PIN_MAX];
__attribute__((weak)) int gpio_init(gpio_t pin, gpio_mode_t mode) {
(void) pin;
(void) mode;
return 0;
if (pin) {
pin->mode = mode;
pin->value = 0;
return 0;
}
return -1;
}
__attribute__((weak)) int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank,
gpio_cb_t cb, void *arg)
{
(void) pin;
(void) mode;
(void) flank;
(void) cb;
(void) arg;
return 0;
if (pin) {
pin->mode = mode;
pin->flank = flank;
pin->value = 0;
pin->cb = cb;
pin->arg = arg;
return 0;
}
return -1;
}
__attribute__((weak)) void gpio_irq_enable(gpio_t pin)
@ -49,26 +67,35 @@ __attribute__((weak)) void gpio_irq_disable(gpio_t pin)
}
__attribute__((weak)) int gpio_read(gpio_t pin) {
(void) pin;
if (pin) {
return pin->value;
}
return 0;
return -1;
}
__attribute__((weak)) void gpio_set(gpio_t pin) {
(void) pin;
if (pin) {
pin->value = 1;
}
}
__attribute__((weak)) void gpio_clear(gpio_t pin) {
(void) pin;
if (pin) {
pin->value = 0;
}
}
__attribute__((weak)) void gpio_toggle(gpio_t pin) {
(void) pin;
if (pin) {
pin->value ^= 1;
}
}
__attribute__((weak)) void gpio_write(gpio_t pin, int value) {
(void) pin;
(void) value;
if (pin) {
pin->value = value;
}
}
/** @} */