diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h index ebf66a9f4a93cb658c79223c059e38d27e998331..9bc91189382365cb525fcdd49607610f928f3151 100644 --- a/epicardium/epicardium.h +++ b/epicardium/epicardium.h @@ -427,26 +427,26 @@ API(API_BUTTONS_READ, uint8_t epic_buttons_read(uint8_t mask)); /** GPIO pins IDs */ enum gpio_pin { /** ``1``, Wristband connector 1 */ - GPIO_WRISTBAND_1 = 1, + EPIC_GPIO_WRISTBAND_1 = 1, /** ``2``, Wristband connector 2 */ - GPIO_WRISTBAND_2 = 2, + EPIC_GPIO_WRISTBAND_2 = 2, /** ``3``, Wristband connector 3 */ - GPIO_WRISTBAND_3 = 3, + EPIC_GPIO_WRISTBAND_3 = 3, /** ``4``, Wristband connector 4 */ - GPIO_WRISTBAND_4 = 4, + EPIC_GPIO_WRISTBAND_4 = 4, }; /** GPIO pin modes */ enum gpio_mode { /** Configure the pin as input */ - GPIO_MODE_IN = (1<<0), + EPIC_GPIO_MODE_IN = (1<<0), /** Configure the pin as output */ - GPIO_MODE_OUT = (1<<1), + EPIC_GPIO_MODE_OUT = (1<<1), /** Enable the internal pull-up resistor */ - GPIO_PULL_UP = (1<<6), + EPIC_GPIO_PULL_UP = (1<<6), /** Enable the internal pull-down resistor */ - GPIO_PULL_DOWN = (1<<7), + EPIC_GPIO_PULL_DOWN = (1<<7), }; /** @@ -543,7 +543,7 @@ API(API_GPIO_WRITE_PIN, int epic_gpio_write_pin(uint8_t pin, bool on)); * :param uint8_t pin: ID of the pin to get the configuration of. Use on of the IDs defined in :c:type:`gpio_pin`. * :returns: ``-EINVAL`` if ``pin`` is not valid, an integer value otherwise. */ -API(API_GPIO_READ_PIN, uint32_t epic_gpio_read_pin(uint8_t pin)); +API(API_GPIO_READ_PIN, int epic_gpio_read_pin(uint8_t pin)); /** * LEDs diff --git a/epicardium/modules/gpio.c b/epicardium/modules/gpio.c index b6bc4f2cf343ba940d28f3420e6bccdccec9375c..9577f094a166b6c52f25f11f7dd9acce8077c293 100644 --- a/epicardium/modules/gpio.c +++ b/epicardium/modules/gpio.c @@ -8,40 +8,52 @@ * pins for wristband GPIO 1-4 (not 0-3 as the schematic states) */ static gpio_cfg_t gpio_configs[] = { - [GPIO_WRISTBAND_1] = { PORT_0, PIN_21, GPIO_FUNC_OUT, GPIO_PAD_NONE }, - [GPIO_WRISTBAND_2] = { PORT_0, PIN_22, GPIO_FUNC_OUT, GPIO_PAD_NONE }, - [GPIO_WRISTBAND_3] = { PORT_0, PIN_29, GPIO_FUNC_OUT, GPIO_PAD_NONE }, - [GPIO_WRISTBAND_4] = { PORT_0, PIN_20, GPIO_FUNC_OUT, GPIO_PAD_NONE }, + [EPIC_GPIO_WRISTBAND_1] = { PORT_0, + PIN_21, + GPIO_FUNC_OUT, + GPIO_PAD_NONE }, + [EPIC_GPIO_WRISTBAND_2] = { PORT_0, + PIN_22, + GPIO_FUNC_OUT, + GPIO_PAD_NONE }, + [EPIC_GPIO_WRISTBAND_3] = { PORT_0, + PIN_29, + GPIO_FUNC_OUT, + GPIO_PAD_NONE }, + [EPIC_GPIO_WRISTBAND_4] = { PORT_0, + PIN_20, + GPIO_FUNC_OUT, + GPIO_PAD_NONE }, }; int epic_gpio_set_pin_mode(uint8_t pin, uint8_t mode) { - if (pin < GPIO_WRISTBAND_1 || pin > GPIO_WRISTBAND_4) + if (pin < EPIC_GPIO_WRISTBAND_1 || pin > EPIC_GPIO_WRISTBAND_4) return -EINVAL; gpio_cfg_t *cfg = &gpio_configs[pin]; - bool is_input = (mode & GPIO_MODE_IN) == GPIO_MODE_IN; - bool is_output = (mode & GPIO_MODE_OUT) == GPIO_MODE_OUT; - - // Pins can't be input and output at the same time. - if (is_input && is_output) + if (mode & EPIC_GPIO_MODE_IN) { + cfg->func = GPIO_FUNC_IN; + if (mode & EPIC_GPIO_MODE_OUT) { + return -EINVAL; + } + } else if (mode & EPIC_GPIO_MODE_OUT) { + cfg->func = GPIO_FUNC_OUT; + if (mode & EPIC_GPIO_MODE_IN) { + return -EINVAL; + } + } else { return -EINVAL; + } - uint32_t func_value = 0; - if (is_input) - func_value |= GPIO_FUNC_IN; - if (is_output) - func_value |= GPIO_FUNC_OUT; - - uint32_t pad_value = 0; - if (mode & GPIO_PULL_UP) - pad_value |= GPIO_PAD_PULL_UP; - if (mode & GPIO_PULL_DOWN) - pad_value |= GPIO_PAD_PULL_DOWN; - - cfg->func = func_value; - cfg->pad = pad_value; + if (mode & EPIC_GPIO_PULL_UP) { + cfg->pad = GPIO_PAD_PULL_UP; + } else if (mode & EPIC_GPIO_PULL_DOWN) { + cfg->pad = GPIO_PAD_PULL_DOWN; + } else { + cfg->pad = GPIO_PAD_NONE; + } if (GPIO_Config(cfg) != E_NO_ERROR) return -EINVAL; @@ -50,30 +62,30 @@ int epic_gpio_set_pin_mode(uint8_t pin, uint8_t mode) int epic_gpio_get_pin_mode(uint8_t pin) { - if (pin < GPIO_WRISTBAND_1 || pin > GPIO_WRISTBAND_4) + if (pin < EPIC_GPIO_WRISTBAND_1 || pin > EPIC_GPIO_WRISTBAND_4) return -EINVAL; gpio_cfg_t *cfg = &gpio_configs[pin]; int res = 0; - if ((cfg->func & GPIO_FUNC_IN) == GPIO_FUNC_IN) - res |= GPIO_MODE_IN; - if ((cfg->func & GPIO_FUNC_OUT) == GPIO_FUNC_OUT) - res |= GPIO_MODE_OUT; - if ((cfg->pad & GPIO_PAD_PULL_UP) == GPIO_PAD_PULL_UP) - res |= GPIO_PULL_UP; - if ((cfg->pad & GPIO_PAD_PULL_DOWN) == GPIO_PAD_PULL_DOWN) - res |= GPIO_PULL_DOWN; + if (cfg->func == GPIO_FUNC_IN) + res |= EPIC_GPIO_MODE_IN; + else if (cfg->func == GPIO_FUNC_OUT) + res |= EPIC_GPIO_MODE_OUT; + if (cfg->pad == GPIO_PAD_PULL_UP) + res |= EPIC_GPIO_PULL_UP; + else if (cfg->pad == GPIO_PAD_PULL_DOWN) + res |= EPIC_GPIO_PULL_DOWN; return res; } int epic_gpio_write_pin(uint8_t pin, bool on) { - if (pin < GPIO_WRISTBAND_1 || pin > GPIO_WRISTBAND_4) + if (pin < EPIC_GPIO_WRISTBAND_1 || pin > EPIC_GPIO_WRISTBAND_4) return -EINVAL; gpio_cfg_t *cfg = &gpio_configs[pin]; - if ((cfg->func & GPIO_FUNC_IN) == GPIO_FUNC_IN) + if (cfg->func == GPIO_FUNC_IN) return -EINVAL; if (on) @@ -84,16 +96,16 @@ int epic_gpio_write_pin(uint8_t pin, bool on) return 0; } -uint32_t epic_gpio_read_pin(uint8_t pin) +int epic_gpio_read_pin(uint8_t pin) { - if (pin < GPIO_WRISTBAND_1 || pin > GPIO_WRISTBAND_4) + if (pin < EPIC_GPIO_WRISTBAND_1 || pin > EPIC_GPIO_WRISTBAND_4) return -EINVAL; gpio_cfg_t *cfg = &gpio_configs[pin]; - if ((cfg->func & GPIO_FUNC_OUT) == GPIO_FUNC_OUT) { - return GPIO_OutGet(cfg); - } else if ((cfg->func & GPIO_FUNC_IN) == GPIO_FUNC_IN) { - return GPIO_InGet(cfg); + if (cfg->func == GPIO_FUNC_OUT) { + return GPIO_OutGet(cfg) != 0; + } else if (cfg->func == GPIO_FUNC_IN) { + return GPIO_InGet(cfg) != 0; } else { return -EINVAL; } diff --git a/pycardium/modules/gpio.c b/pycardium/modules/gpio.c index c92d1023470655383a0d33c1cb6b29d13825c19a..49c3c37c4ea52ee1f207ddf5d78ecd65a957fdc9 100644 --- a/pycardium/modules/gpio.c +++ b/pycardium/modules/gpio.c @@ -28,10 +28,10 @@ static MP_DEFINE_CONST_FUN_OBJ_1(gpio_get_mode, mp_gpio_get_mode); static mp_obj_t mp_gpio_read(mp_obj_t pin_obj) { - int pin = mp_obj_get_int(pin_obj); - uint32_t value = epic_gpio_read_pin(pin); - if (value == -EINVAL) - mp_raise_OSError(EINVAL); + int pin = mp_obj_get_int(pin_obj); + int value = epic_gpio_read_pin(pin); + if (value < 0) + mp_raise_OSError(-value); return MP_OBJ_NEW_SMALL_INT(value); }; @@ -51,11 +51,13 @@ static MP_DEFINE_CONST_FUN_OBJ_2(gpio_write, mp_gpio_write); static const mp_rom_map_elem_t gpio_module_modes_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_mode) }, - { MP_ROM_QSTR(MP_QSTR_INPUT), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_IN) }, - { MP_ROM_QSTR(MP_QSTR_OUTPUT), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_OUT) }, - { MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_OBJ_NEW_SMALL_INT(GPIO_PULL_UP) }, + { MP_ROM_QSTR(MP_QSTR_INPUT), MP_OBJ_NEW_SMALL_INT(EPIC_GPIO_MODE_IN) }, + { MP_ROM_QSTR(MP_QSTR_OUTPUT), + MP_OBJ_NEW_SMALL_INT(EPIC_GPIO_MODE_OUT) }, + { MP_ROM_QSTR(MP_QSTR_PULL_UP), + MP_OBJ_NEW_SMALL_INT(EPIC_GPIO_PULL_UP) }, { MP_ROM_QSTR(MP_QSTR_PULL_DOWN), - MP_OBJ_NEW_SMALL_INT(GPIO_PULL_DOWN) }, + MP_OBJ_NEW_SMALL_INT(EPIC_GPIO_PULL_DOWN) }, }; static MP_DEFINE_CONST_DICT(gpio_module_modes_dict, gpio_module_modes_table); @@ -72,13 +74,13 @@ static const mp_rom_map_elem_t gpio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&gpio_read) }, { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&gpio_write) }, { MP_ROM_QSTR(MP_QSTR_WRISTBAND_1), - MP_OBJ_NEW_SMALL_INT(GPIO_WRISTBAND_1) }, + MP_OBJ_NEW_SMALL_INT(EPIC_GPIO_WRISTBAND_1) }, { MP_ROM_QSTR(MP_QSTR_WRISTBAND_2), - MP_OBJ_NEW_SMALL_INT(GPIO_WRISTBAND_2) }, + MP_OBJ_NEW_SMALL_INT(EPIC_GPIO_WRISTBAND_2) }, { MP_ROM_QSTR(MP_QSTR_WRISTBAND_3), - MP_OBJ_NEW_SMALL_INT(GPIO_WRISTBAND_3) }, + MP_OBJ_NEW_SMALL_INT(EPIC_GPIO_WRISTBAND_3) }, { MP_ROM_QSTR(MP_QSTR_WRISTBAND_4), - MP_OBJ_NEW_SMALL_INT(GPIO_WRISTBAND_4) }, + MP_OBJ_NEW_SMALL_INT(EPIC_GPIO_WRISTBAND_4) }, { MP_ROM_QSTR(MP_QSTR_mode), MP_ROM_PTR(&gpio_module_modes) }, }; static MP_DEFINE_CONST_DICT(gpio_module_globals, gpio_module_globals_table);