2 * Copyright (C) 2012-2013 Czech Technical University in Prague
4 * Created on: 28.2.2013
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * Commands for VBAT control
26 * - Power VBAT1 on and off using PWM to charge capacitors
37 #define VBATPWM_PERIOD 400
39 /** Delay between set and clear vbat */
40 static int vbatPwmRampProfile[] = {
51 * @brief Runs PWM defined by ramp profile on VBAT1
53 * @return value on VBAT port
57 int *ppwm = vbatPwmRampProfile;
59 volatile uint8_t val = 1;
64 desc = PIN_DSC_VBAT1EN;
65 hal_gpio_pin_set_value(desc, 0);
66 hal_gpio_pin_direction_output(desc, 0);
67 vTaskDelay(10/portTICK_RATE_MS+2);
69 while ((pwm = *(ppwm++)))
70 for (i = 0; i < pulse_cnt; i++) {
71 hal_gpio_pin_set_value(desc, 1);
72 for (j = 0; j < pwm; j++)
74 hal_gpio_pin_set_value(desc, 0);
75 for (j = 0; j < VBATPWM_PERIOD - pwm; j++)
78 hal_gpio_pin_set_value(desc, 1);
80 return hal_gpio_pin_get_value(desc);
84 * @brief Power on VBAT and VBAT1 using PWM
86 * @param[in] cmd_io Pointer to IO stack
87 * @param[in] des Pointer to command descriptor
88 * @param[in] param Parameters of command
89 * @return value on VBAT port
91 int cmd_do_power_on(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
95 hal_gpio_pin_set_value(PIN_DSC_VBATEN, 1);
96 ret = hal_gpio_pin_get_value(PIN_DSC_VBATEN);
97 ret |= vbat1_pwm() << 1;
98 return cmd_opchar_replong(cmd_io, param, ret, 0, 0);
102 * @brief Power off VBAT and VBAT1 using PWM
104 * @param[in] cmd_io Pointer to IO stack
105 * @param[in] des Pointer to command descriptor
106 * @param[in] param Parameters of command
107 * @return value on VBAT port
109 int cmd_do_power_off(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
113 hal_gpio_pin_set_value(PIN_DSC_VBAT1EN, 0);
114 ret = hal_gpio_pin_get_value(PIN_DSC_VBAT1EN);
115 hal_gpio_pin_set_value(PIN_DSC_VBATEN, 0);
116 ret |= hal_gpio_pin_get_value(PIN_DSC_VBATEN) << 1;
117 return cmd_opchar_replong(cmd_io, param, ret, 0, 0);
122 /** Command descriptor for poweron */
123 cmd_des_t const cmd_des_power_on = {
125 "poweron","Enable VBATEN and VBAT1EN power supply by using PWM hack",
126 "### Command syntax ###\n"
130 "### Description ###\n"
132 "This command tries to work around error on VBAT power supply wiring\n"
133 "and attempts to switch the power supply on.\n"
135 "It turns on the VBAT voltage by slowly charging the capacitors\n"
136 "connected to the VBAT1 signal by using the software-generated PWM\n"
137 "signal with increasing duty cycle.\n"
139 "The poweron command has to be launched before any access to any SPI\n"
140 "peripherals, otherwise they will not work (or the power supply has to\n"
141 "be electrically bypassed).\n"
143 "Please note that parameters for the PWM signal may change from device\n"
144 "to device and it might be necessary to tune them (in source code) for\n"
146 CMD_HANDLER(cmd_do_power_on), (void *)&cmd_list_vbat
149 /** Command descriptor for poweroff */
150 cmd_des_t const cmd_des_power_off = {
152 "poweroff","Disables VBATEN and VBAT1EN power supply",
153 "### Command syntax ###\n"
157 "### Description ###\n"
159 "This command turns off VBAT and VBAT1 voltages.\n",
160 CMD_HANDLER(cmd_do_power_off), (void *)&cmd_list_vbat
163 /** List of commands for vbat, defined as external */
164 cmd_des_t const *cmd_list_vbat[] = {