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
56 int *ppwm = vbatPwmRampProfile;
58 volatile uint8_t val = 1;
63 desc = PIN_DSC_VBAT1EN;
64 hal_gpio_pin_set_value(desc, 0);
65 hal_gpio_pin_direction_output(desc, 0);
66 vTaskDelay(10/portTICK_RATE_MS+2);
68 while((pwm = *(ppwm++))) {
69 for (i = 0; i < pulse_cnt; i++) {
70 hal_gpio_pin_set_value(desc, 1);
71 for (j = 0; j < pwm; j++)
73 hal_gpio_pin_set_value(desc, 0);
74 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[]) {
93 hal_gpio_pin_set_value(PIN_DSC_VBATEN, 1);
94 ret = hal_gpio_pin_get_value(PIN_DSC_VBATEN);
95 ret |= vbat1_pwm() << 1;
96 return cmd_opchar_replong(cmd_io, param, ret, 0, 0);
100 * @brief Power off VBAT and VBAT1 using PWM
102 * @param[in] cmd_io Pointer to IO stack
103 * @param[in] des Pointer to command descriptor
104 * @param[in] param Parameters of command
105 * @return value on VBAT port
107 int cmd_do_power_off(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
109 hal_gpio_pin_set_value(PIN_DSC_VBAT1EN, 0);
110 ret = hal_gpio_pin_get_value(PIN_DSC_VBAT1EN);
111 hal_gpio_pin_set_value(PIN_DSC_VBATEN, 0);
112 ret |= hal_gpio_pin_get_value(PIN_DSC_VBATEN) << 1;
113 return cmd_opchar_replong(cmd_io, param, ret, 0, 0);
118 /** Command descriptor for poweron */
119 cmd_des_t const cmd_des_power_on = {
121 "poweron","Enable VBATEN and VBAT1EN power supply by using PWM hack",
122 "### Command syntax ###\n"
126 "### Description ###\n"
128 "This command tries to work around error on VBAT power supply wiring\n"
129 "and attempts to switch the power supply on.\n"
131 "It turns on the VBAT voltage by slowly charging the capacitors\n"
132 "connected to the VBAT1 signal by using the software-generated PWM\n"
133 "signal with increasing duty cycle.\n"
135 "The poweron command has to be launched before any access to any SPI\n"
136 "peripherals, otherwise they will not work (or the power supply has to\n"
137 "be electrically bypassed).\n"
139 "Please note that parameters for the PWM signal may change from device\n"
140 "to device and it might be necessary to tune them (in source code) for\n"
142 CMD_HANDLER(cmd_do_power_on), (void *)&cmd_list_vbat
145 /** Command descriptor for poweroff */
146 cmd_des_t const cmd_des_power_off = {
148 "poweroff","Disables VBATEN and VBAT1EN power supply",
149 "### Command syntax ###\n"
153 "### Description ###\n"
155 "This command turns off VBAT and VBAT1 voltages.\n",
156 CMD_HANDLER(cmd_do_power_off), (void *)&cmd_list_vbat
159 /** List of commands for vbat, defined as external */
160 cmd_des_t const *cmd_list_vbat[]={