4 * Created on: 28.2.2013
7 * Commands for VBAT control
8 * - Power VBAT1 on and off using PWM to charge capacitors
12 #include "commands/cmd_vbat.h"
15 #define VBATPWM_PERIOD 400
17 /** Delay between set and clear vbat */
18 static int vbatPwmRampProfile[] = {
29 * @brief Runs PWM defined by ramp profile on VBAT1
31 * @return value on VBAT port
34 int *ppwm = vbatPwmRampProfile;
36 volatile uint8_t val = 1;
41 desc = PIN_DSC_VBAT1EN;
42 hal_gpio_pin_set_value(desc, 0);
43 hal_gpio_pin_direction_output(desc, 0);
44 vTaskDelay(10/portTICK_RATE_MS+2);
46 while((pwm = *(ppwm++))) {
47 for (i = 0; i < pulse_cnt; i++) {
48 hal_gpio_pin_set_value(desc, 1);
49 for (j = 0; j < pwm; j++)
51 hal_gpio_pin_set_value(desc, 0);
52 for (j = 0; j < VBATPWM_PERIOD - pwm; j++)
56 hal_gpio_pin_set_value(desc, 1);
58 return hal_gpio_pin_get_value(desc);
62 * @brief Power on VBAT and VBAT1 using PWM
64 * Command syntax: poweron
66 * @param[in] cmd_io Pointer to IO stack
67 * @param[in] des Pointer to command descriptor
68 * @param[in] param Parameters of command
69 * @return value on VBAT port
71 int cmd_do_power_on(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
73 hal_gpio_pin_set_value(PIN_DSC_VBATEN, 1);
74 ret = hal_gpio_pin_get_value(PIN_DSC_VBATEN);
75 ret |= vbat1_pwm() << 1;
76 return cmd_opchar_replong(cmd_io, param, ret, 0, 0);
80 * @brief Power off VBAT and VBAT1 using PWM
82 * Command syntax: poweroff
84 * @param[in] cmd_io Pointer to IO stack
85 * @param[in] des Pointer to command descriptor
86 * @param[in] param Parameters of command
87 * @return value on VBAT port
89 int cmd_do_power_off(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
91 hal_gpio_pin_set_value(PIN_DSC_VBAT1EN, 0);
92 ret = hal_gpio_pin_get_value(PIN_DSC_VBAT1EN);
93 hal_gpio_pin_set_value(PIN_DSC_VBATEN, 0);
94 ret |= hal_gpio_pin_get_value(PIN_DSC_VBATEN) << 1;
95 return cmd_opchar_replong(cmd_io, param, ret, 0, 0);
98 /** Command descriptor for poweron */
99 cmd_des_t const cmd_des_power_on = {
101 "poweron","Set VBATEN and VBAT1EN using PWM",
102 cmd_do_power_on, (void *)&cmd_list_vbat
105 /** Command descriptor for poweroff */
106 cmd_des_t const cmd_des_power_off = {
108 "poweroff","Clear VBATEN and VBAT1EN",
109 cmd_do_power_off, (void *)&cmd_list_vbat
112 /** List of commands for vbat, defined as external */
113 cmd_des_t const *cmd_list_vbat[]={