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

feat(epicardium): gpios are configurable as ADC

fixes #82
parent 37da10f9
Branches
Tags
No related merge requests found
......@@ -459,6 +459,7 @@ enum gpio_mode {
EPIC_GPIO_MODE_IN = (1<<0),
/** Configure the pin as output */
EPIC_GPIO_MODE_OUT = (1<<1),
EPIC_GPIO_MODE_ADC = (1<<2),
/** Enable the internal pull-up resistor */
EPIC_GPIO_PULL_UP = (1<<6),
......
#include "epicardium.h"
#include "gpio.h"
#include "max32665.h"
#include "mxc_sys.h"
#include "adc.h"
#include "mxc_errors.h"
#include "modules/log.h"
/*
* Despite what the schematic (currently, 2019-08-18) says these are the correct
......@@ -26,6 +29,14 @@ static gpio_cfg_t gpio_configs[] = {
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)
{
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)
if (mode & EPIC_GPIO_MODE_IN) {
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 {
return -EINVAL;
}
if (!(mode & EPIC_GPIO_MODE_ADC)) {
if (mode & EPIC_GPIO_PULL_UP) {
cfg->pad = GPIO_PAD_PULL_UP;
} else if (mode & EPIC_GPIO_PULL_DOWN) {
......@@ -54,6 +75,9 @@ int epic_gpio_set_pin_mode(uint8_t pin, uint8_t mode)
} else {
cfg->pad = GPIO_PAD_NONE;
}
} else {
cfg->pad = GPIO_PAD_NONE;
}
if (GPIO_Config(cfg) != E_NO_ERROR)
return -EINVAL;
......@@ -71,6 +95,8 @@ int epic_gpio_get_pin_mode(uint8_t pin)
res |= EPIC_GPIO_MODE_IN;
else if (cfg->func == GPIO_FUNC_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)
res |= EPIC_GPIO_PULL_UP;
else if (cfg->pad == GPIO_PAD_PULL_DOWN)
......@@ -106,6 +132,14 @@ int epic_gpio_read_pin(uint8_t pin)
return GPIO_OutGet(cfg) != 0;
} else if (cfg->func == GPIO_FUNC_IN) {
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 {
return -EINVAL;
}
......
......@@ -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_OUTPUT),
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_OBJ_NEW_SMALL_INT(EPIC_GPIO_PULL_UP) },
{ MP_ROM_QSTR(MP_QSTR_PULL_DOWN),
......
......@@ -150,6 +150,7 @@ Q(WRISTBAND_3)
Q(WRISTBAND_4)
Q(INPUT)
Q(OUTPUT)
Q(ADC)
Q(PULL_UP)
Q(PULL_DOWN)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment