diff options
Diffstat (limited to 'board/spring/usb_charging.c')
-rw-r--r-- | board/spring/usb_charging.c | 66 |
1 files changed, 58 insertions, 8 deletions
diff --git a/board/spring/usb_charging.c b/board/spring/usb_charging.c index 3f93fe6e72..d10f1cfa6e 100644 --- a/board/spring/usb_charging.c +++ b/board/spring/usb_charging.c @@ -7,12 +7,27 @@ #include "board.h" #include "console.h" +#include "gpio.h" #include "registers.h" #include "util.h" #define PWM_FREQUENCY 100 /* Hz */ -void board_configure_pwm(void) +static enum ilim_config current_ilim_config = ILIM_CONFIG_MANUAL_OFF; + +static void board_ilim_use_gpio(void) +{ + /* Disable counter */ + STM32_TIM_CR1(3) &= ~0x1; + + /* Disable TIM3 clock */ + STM32_RCC_APB1ENR &= ~0x2; + + /* Switch to GPIO */ + gpio_set_flags(GPIO_ILIM, GPIO_OUTPUT); +} + +static void board_ilim_use_pwm(void) { uint32_t val; @@ -52,8 +67,31 @@ void board_configure_pwm(void) STM32_TIM_CR1(3) |= (1 << 7) | (1 << 0); } +void board_ilim_config(enum ilim_config config) +{ + if (config == current_ilim_config) + return; + current_ilim_config = config; + + switch (config) { + case ILIM_CONFIG_MANUAL_OFF: + case ILIM_CONFIG_MANUAL_ON: + board_ilim_use_gpio(); + gpio_set_level(GPIO_ILIM, + config == ILIM_CONFIG_MANUAL_ON ? 1 : 0); + break; + case ILIM_CONFIG_PWM: + board_ilim_use_pwm(); + break; + default: + break; + } +} + void board_pwm_duty_cycle(int percent) { + if (current_ilim_config != ILIM_CONFIG_PWM) + board_ilim_config(ILIM_CONFIG_PWM); if (percent < 0) percent = 0; if (percent > 100) @@ -71,16 +109,28 @@ static int command_ilim(int argc, char **argv) int percent; if (argc >= 2) { - percent = strtoi(argv[1], &e, 0); - if (*e) - return EC_ERROR_PARAM1; - board_pwm_duty_cycle(percent); + if (strcasecmp(argv[1], "on") == 0) + board_ilim_config(ILIM_CONFIG_MANUAL_ON); + else if (strcasecmp(argv[1], "off") == 0) + board_ilim_config(ILIM_CONFIG_MANUAL_OFF); + else { + percent = strtoi(argv[1], &e, 0); + if (*e) + return EC_ERROR_PARAM1; + board_pwm_duty_cycle(percent); + } } - ccprintf("PWM duty cycle set to %d%%\n", STM32_TIM_CCR1(3)); + + if (current_ilim_config == ILIM_CONFIG_MANUAL_ON) + ccprintf("ILIM is GPIO high\n"); + else if (current_ilim_config == ILIM_CONFIG_MANUAL_OFF) + ccprintf("ILIM is GPIO low\n"); + else + ccprintf("ILIM is PWM duty cycle %d%%\n", STM32_TIM_CCR1(3)); return EC_SUCCESS; } DECLARE_CONSOLE_COMMAND(ilim, command_ilim, - "[percent]", - "Set or show ILIM duty cycle", + "[percent | on | off]", + "Set or show ILIM duty cycle/GPIO value", NULL); |