Skip to content
Snippets Groups Projects
Commit 26db6b6b authored by swym's avatar swym
Browse files

fix(gpio): fields in gpio_cfg_t are not bit-sets

parent 67b113ce
No related branches found
No related tags found
No related merge requests found
...@@ -543,7 +543,7 @@ API(API_GPIO_WRITE_PIN, int epic_gpio_write_pin(uint8_t pin, bool on)); ...@@ -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`. * :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. * :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 * LEDs
......
...@@ -21,27 +21,27 @@ int epic_gpio_set_pin_mode(uint8_t pin, uint8_t mode) ...@@ -21,27 +21,27 @@ int epic_gpio_set_pin_mode(uint8_t pin, uint8_t mode)
gpio_cfg_t *cfg = &gpio_configs[pin]; gpio_cfg_t *cfg = &gpio_configs[pin];
bool is_input = (mode & GPIO_MODE_IN) == GPIO_MODE_IN; if (mode & GPIO_MODE_IN) {
bool is_output = (mode & GPIO_MODE_OUT) == GPIO_MODE_OUT; cfg->func = GPIO_FUNC_IN;
if (mode & GPIO_MODE_OUT) {
// Pins can't be input and output at the same time.
if (is_input && is_output)
return -EINVAL; return -EINVAL;
}
} else if (mode & GPIO_MODE_OUT) {
cfg->func = GPIO_FUNC_OUT;
if (mode & GPIO_MODE_IN) {
return -EINVAL;
}
} else {
return -EINVAL;
}
uint32_t func_value = 0; if (mode & GPIO_PULL_UP) {
if (is_input) cfg->pad = GPIO_PAD_PULL_UP;
func_value |= GPIO_FUNC_IN; } else if (mode & GPIO_PULL_DOWN) {
if (is_output) cfg->pad = GPIO_PAD_PULL_DOWN;
func_value |= GPIO_FUNC_OUT; } else {
cfg->pad = GPIO_PAD_NONE;
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 (GPIO_Config(cfg) != E_NO_ERROR) if (GPIO_Config(cfg) != E_NO_ERROR)
return -EINVAL; return -EINVAL;
...@@ -55,13 +55,13 @@ int epic_gpio_get_pin_mode(uint8_t pin) ...@@ -55,13 +55,13 @@ int epic_gpio_get_pin_mode(uint8_t pin)
gpio_cfg_t *cfg = &gpio_configs[pin]; gpio_cfg_t *cfg = &gpio_configs[pin];
int res = 0; int res = 0;
if ((cfg->func & GPIO_FUNC_IN) == GPIO_FUNC_IN) if (cfg->func == GPIO_FUNC_IN)
res |= GPIO_MODE_IN; res |= GPIO_MODE_IN;
if ((cfg->func & GPIO_FUNC_OUT) == GPIO_FUNC_OUT) else if (cfg->func == GPIO_FUNC_OUT)
res |= GPIO_MODE_OUT; res |= GPIO_MODE_OUT;
if ((cfg->pad & GPIO_PAD_PULL_UP) == GPIO_PAD_PULL_UP) if (cfg->pad == GPIO_PAD_PULL_UP)
res |= GPIO_PULL_UP; res |= GPIO_PULL_UP;
if ((cfg->pad & GPIO_PAD_PULL_DOWN) == GPIO_PAD_PULL_DOWN) else if (cfg->pad == GPIO_PAD_PULL_DOWN)
res |= GPIO_PULL_DOWN; res |= GPIO_PULL_DOWN;
return res; return res;
...@@ -73,7 +73,7 @@ int epic_gpio_write_pin(uint8_t pin, bool on) ...@@ -73,7 +73,7 @@ int epic_gpio_write_pin(uint8_t pin, bool on)
return -EINVAL; return -EINVAL;
gpio_cfg_t *cfg = &gpio_configs[pin]; gpio_cfg_t *cfg = &gpio_configs[pin];
if ((cfg->func & GPIO_FUNC_IN) == GPIO_FUNC_IN) if (cfg->func == GPIO_FUNC_IN)
return -EINVAL; return -EINVAL;
if (on) if (on)
...@@ -84,16 +84,16 @@ int epic_gpio_write_pin(uint8_t pin, bool on) ...@@ -84,16 +84,16 @@ int epic_gpio_write_pin(uint8_t pin, bool on)
return 0; 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 < GPIO_WRISTBAND_1 || pin > GPIO_WRISTBAND_4)
return -EINVAL; return -EINVAL;
gpio_cfg_t *cfg = &gpio_configs[pin]; gpio_cfg_t *cfg = &gpio_configs[pin];
if ((cfg->func & GPIO_FUNC_OUT) == GPIO_FUNC_OUT) { if (cfg->func == GPIO_FUNC_OUT) {
return GPIO_OutGet(cfg); return GPIO_OutGet(cfg) != 0;
} else if ((cfg->func & GPIO_FUNC_IN) == GPIO_FUNC_IN) { } else if (cfg->func == GPIO_FUNC_IN) {
return GPIO_InGet(cfg); return GPIO_InGet(cfg) != 0;
} else { } else {
return -EINVAL; return -EINVAL;
} }
......
...@@ -29,9 +29,9 @@ static MP_DEFINE_CONST_FUN_OBJ_1(gpio_get_mode, mp_gpio_get_mode); ...@@ -29,9 +29,9 @@ 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) static mp_obj_t mp_gpio_read(mp_obj_t pin_obj)
{ {
int pin = mp_obj_get_int(pin_obj); int pin = mp_obj_get_int(pin_obj);
uint32_t value = epic_gpio_read_pin(pin); int value = epic_gpio_read_pin(pin);
if (value == -EINVAL) if (value < 0)
mp_raise_OSError(EINVAL); mp_raise_OSError(-value);
return MP_OBJ_NEW_SMALL_INT(value); return MP_OBJ_NEW_SMALL_INT(value);
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment