Skip to content
Snippets Groups Projects

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

Merged swym requested to merge swym/firmware:gpio_fix into master
3 files
+ 76
62
Compare changes
  • Side-by-side
  • Inline
Files
3
+ 53
41
@@ -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;
}
Loading