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

feat(epicardium): gpios are configurable as ADC

fixes #82
parent 45c228ac
No related branches found
No related tags found
No related merge requests found
...@@ -457,6 +457,7 @@ enum gpio_mode { ...@@ -457,6 +457,7 @@ enum gpio_mode {
EPIC_GPIO_MODE_IN = (1<<0), EPIC_GPIO_MODE_IN = (1<<0),
/** Configure the pin as output */ /** Configure the pin as output */
EPIC_GPIO_MODE_OUT = (1<<1), EPIC_GPIO_MODE_OUT = (1<<1),
EPIC_GPIO_MODE_ADC = (1<<2),
/** Enable the internal pull-up resistor */ /** Enable the internal pull-up resistor */
EPIC_GPIO_PULL_UP = (1<<6), EPIC_GPIO_PULL_UP = (1<<6),
......
#include "epicardium.h" #include "epicardium.h"
#include "gpio.h" #include "gpio.h"
#include "max32665.h" #include "max32665.h"
#include "mxc_sys.h"
#include "adc.h"
#include "mxc_errors.h" #include "mxc_errors.h"
#include "modules/log.h"
/* /*
* Despite what the schematic (currently, 2019-08-18) says these are the correct * Despite what the schematic (currently, 2019-08-18) says these are the correct
...@@ -26,6 +29,14 @@ static gpio_cfg_t gpio_configs[] = { ...@@ -26,6 +29,14 @@ static gpio_cfg_t gpio_configs[] = {
GPIO_PAD_NONE }, GPIO_PAD_NONE },
}; };
static int s_adc_channels[] = {
[EPIC_GPIO_WRISTBAND_1] = ADC_CH_5,
[EPIC_GPIO_WRISTBAND_2] = ADC_CH_6,
[EPIC_GPIO_WRISTBAND_3] =
-1, //AFAICT on P0.29, there is no ADC available
[EPIC_GPIO_WRISTBAND_4] = ADC_CH_4,
};
int epic_gpio_set_pin_mode(uint8_t pin, uint8_t mode) int epic_gpio_set_pin_mode(uint8_t pin, uint8_t mode)
{ {
if (pin < EPIC_GPIO_WRISTBAND_1 || pin > EPIC_GPIO_WRISTBAND_4) if (pin < EPIC_GPIO_WRISTBAND_1 || pin > EPIC_GPIO_WRISTBAND_4)
...@@ -43,10 +54,20 @@ int epic_gpio_set_pin_mode(uint8_t pin, uint8_t mode) ...@@ -43,10 +54,20 @@ int epic_gpio_set_pin_mode(uint8_t pin, uint8_t mode)
if (mode & EPIC_GPIO_MODE_IN) { if (mode & EPIC_GPIO_MODE_IN) {
return -EINVAL; return -EINVAL;
} }
} else if (mode & EPIC_GPIO_MODE_ADC) {
if (s_adc_channels[pin] == -1) {
LOG_WARN("gpio", "ADC not available on pin %d", pin);
return -EINVAL;
}
cfg->func = GPIO_FUNC_ALT1;
if (mode & EPIC_GPIO_MODE_OUT) {
return -EINVAL;
}
} else { } else {
return -EINVAL; return -EINVAL;
} }
if (!(mode & EPIC_GPIO_MODE_ADC)) {
if (mode & EPIC_GPIO_PULL_UP) { if (mode & EPIC_GPIO_PULL_UP) {
cfg->pad = GPIO_PAD_PULL_UP; cfg->pad = GPIO_PAD_PULL_UP;
} else if (mode & EPIC_GPIO_PULL_DOWN) { } else if (mode & EPIC_GPIO_PULL_DOWN) {
...@@ -54,6 +75,9 @@ int epic_gpio_set_pin_mode(uint8_t pin, uint8_t mode) ...@@ -54,6 +75,9 @@ int epic_gpio_set_pin_mode(uint8_t pin, uint8_t mode)
} else { } else {
cfg->pad = GPIO_PAD_NONE; cfg->pad = GPIO_PAD_NONE;
} }
} else {
cfg->pad = GPIO_PAD_NONE;
}
if (GPIO_Config(cfg) != E_NO_ERROR) if (GPIO_Config(cfg) != E_NO_ERROR)
return -EINVAL; return -EINVAL;
...@@ -71,6 +95,8 @@ int epic_gpio_get_pin_mode(uint8_t pin) ...@@ -71,6 +95,8 @@ int epic_gpio_get_pin_mode(uint8_t pin)
res |= EPIC_GPIO_MODE_IN; res |= EPIC_GPIO_MODE_IN;
else if (cfg->func == GPIO_FUNC_OUT) else if (cfg->func == GPIO_FUNC_OUT)
res |= EPIC_GPIO_MODE_OUT; res |= EPIC_GPIO_MODE_OUT;
else if (cfg->func == GPIO_FUNC_ALT1)
res |= EPIC_GPIO_MODE_ADC;
if (cfg->pad == GPIO_PAD_PULL_UP) if (cfg->pad == GPIO_PAD_PULL_UP)
res |= EPIC_GPIO_PULL_UP; res |= EPIC_GPIO_PULL_UP;
else if (cfg->pad == GPIO_PAD_PULL_DOWN) else if (cfg->pad == GPIO_PAD_PULL_DOWN)
...@@ -106,6 +132,14 @@ int epic_gpio_read_pin(uint8_t pin) ...@@ -106,6 +132,14 @@ int epic_gpio_read_pin(uint8_t pin)
return GPIO_OutGet(cfg) != 0; return GPIO_OutGet(cfg) != 0;
} else if (cfg->func == GPIO_FUNC_IN) { } else if (cfg->func == GPIO_FUNC_IN) {
return GPIO_InGet(cfg) != 0; return GPIO_InGet(cfg) != 0;
} else if (cfg->func == GPIO_FUNC_ALT1) {
ADC_StartConvert(s_adc_channels[pin], 0, 0);
uint16_t value;
int rc = ADC_GetData(&value);
if (rc < 0) {
return -EIO;
}
return (int)value;
} else { } else {
return -EINVAL; return -EINVAL;
} }
......
...@@ -54,6 +54,7 @@ static const mp_rom_map_elem_t gpio_module_modes_table[] = { ...@@ -54,6 +54,7 @@ static const mp_rom_map_elem_t gpio_module_modes_table[] = {
{ MP_ROM_QSTR(MP_QSTR_INPUT), MP_OBJ_NEW_SMALL_INT(EPIC_GPIO_MODE_IN) }, { MP_ROM_QSTR(MP_QSTR_INPUT), MP_OBJ_NEW_SMALL_INT(EPIC_GPIO_MODE_IN) },
{ MP_ROM_QSTR(MP_QSTR_OUTPUT), { MP_ROM_QSTR(MP_QSTR_OUTPUT),
MP_OBJ_NEW_SMALL_INT(EPIC_GPIO_MODE_OUT) }, MP_OBJ_NEW_SMALL_INT(EPIC_GPIO_MODE_OUT) },
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_OBJ_NEW_SMALL_INT(EPIC_GPIO_MODE_ADC) },
{ MP_ROM_QSTR(MP_QSTR_PULL_UP), { MP_ROM_QSTR(MP_QSTR_PULL_UP),
MP_OBJ_NEW_SMALL_INT(EPIC_GPIO_PULL_UP) }, MP_OBJ_NEW_SMALL_INT(EPIC_GPIO_PULL_UP) },
{ MP_ROM_QSTR(MP_QSTR_PULL_DOWN), { MP_ROM_QSTR(MP_QSTR_PULL_DOWN),
......
...@@ -148,6 +148,7 @@ Q(WRISTBAND_3) ...@@ -148,6 +148,7 @@ Q(WRISTBAND_3)
Q(WRISTBAND_4) Q(WRISTBAND_4)
Q(INPUT) Q(INPUT)
Q(OUTPUT) Q(OUTPUT)
Q(ADC)
Q(PULL_UP) Q(PULL_UP)
Q(PULL_DOWN) Q(PULL_DOWN)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment