2 * Copyright (C) 2012-2013 Czech Technical University in Prague
4 * Created on: 28.2.2013
9 * This document contains proprietary information belonging to Czech
10 * Technical University in Prague. Passing on and copying of this
11 * document, and communication of its contents is not permitted
12 * without prior written authorization.
17 * Commands for VBAT control
18 * - Power VBAT1 on and off using PWM to charge capacitors
29 #define VBATPWM_PERIOD 400
31 /** Delay between set and clear vbat */
32 static int vbatPwmRampProfile[] = {
43 * @brief Runs PWM defined by ramp profile on VBAT1
45 * @return value on VBAT port
49 int *ppwm = vbatPwmRampProfile;
51 volatile uint8_t val = 1;
56 desc = PIN_DSC_VBAT1EN;
57 hal_gpio_pin_set_value(desc, 0);
58 hal_gpio_pin_direction_output(desc, 0);
59 vTaskDelay(10/portTICK_RATE_MS+2);
61 while ((pwm = *(ppwm++)))
62 for (i = 0; i < pulse_cnt; i++) {
63 hal_gpio_pin_set_value(desc, 1);
64 for (j = 0; j < pwm; j++)
66 hal_gpio_pin_set_value(desc, 0);
67 for (j = 0; j < VBATPWM_PERIOD - pwm; j++)
70 hal_gpio_pin_set_value(desc, 1);
72 return hal_gpio_pin_get_value(desc);
76 * @brief Power on VBAT and VBAT1 using PWM
78 * @param[in] cmd_io Pointer to IO stack
79 * @param[in] des Pointer to command descriptor
80 * @param[in] param Parameters of command
81 * @return value on VBAT port
83 int cmd_do_power_on(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
87 hal_gpio_pin_set_value(PIN_DSC_VBATEN, 1);
88 ret = hal_gpio_pin_get_value(PIN_DSC_VBATEN);
89 ret |= vbat1_pwm() << 1;
90 return cmd_opchar_replong(cmd_io, param, ret, 0, 0);
94 * @brief Power off VBAT and VBAT1 using PWM
96 * @param[in] cmd_io Pointer to IO stack
97 * @param[in] des Pointer to command descriptor
98 * @param[in] param Parameters of command
99 * @return value on VBAT port
101 int cmd_do_power_off(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
105 hal_gpio_pin_set_value(PIN_DSC_VBAT1EN, 0);
106 ret = hal_gpio_pin_get_value(PIN_DSC_VBAT1EN);
107 hal_gpio_pin_set_value(PIN_DSC_VBATEN, 0);
108 ret |= hal_gpio_pin_get_value(PIN_DSC_VBATEN) << 1;
109 return cmd_opchar_replong(cmd_io, param, ret, 0, 0);
114 /** Command descriptor for poweron */
115 cmd_des_t const cmd_des_power_on = {
117 "poweron","Enable VBATEN and VBAT1EN power supply by using PWM hack",
118 "### Command syntax ###\n"
122 "### Description ###\n"
124 "This command tries to work around error on VBAT power supply wiring\n"
125 "and attempts to switch the power supply on.\n"
127 "It turns on the VBAT voltage by slowly charging the capacitors\n"
128 "connected to the VBAT1 signal by using the software-generated PWM\n"
129 "signal with increasing duty cycle.\n"
131 "The poweron command has to be launched before any access to any SPI\n"
132 "peripherals, otherwise they will not work (or the power supply has to\n"
133 "be electrically bypassed).\n"
135 "Please note that parameters for the PWM signal may change from device\n"
136 "to device and it might be necessary to tune them (in source code) for\n"
138 CMD_HANDLER(cmd_do_power_on), (void *)&cmd_list_vbat
141 /** Command descriptor for poweroff */
142 cmd_des_t const cmd_des_power_off = {
144 "poweroff","Disables VBATEN and VBAT1EN power supply",
145 "### Command syntax ###\n"
149 "### Description ###\n"
151 "This command turns off VBAT and VBAT1 voltages.\n",
152 CMD_HANDLER(cmd_do_power_off), (void *)&cmd_list_vbat
155 /** List of commands for vbat, defined as external */
156 cmd_des_t const *cmd_list_vbat[] = {