2 * Copyright (C) 2012-2013, 2015 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
28 #define VBATPWM_PERIOD 400
30 /** Delay between set and clear vbat */
31 static int vbatPwmRampProfile[] = {
42 * @brief Runs PWM defined by ramp profile on VBAT1
44 * @return value on VBAT port
48 int *ppwm = vbatPwmRampProfile;
50 volatile uint8_t val = 1;
54 if (rpp_gio_setup(PIN_VBAT1EN, RPP_GIO_IN, RPP_GIO_MODE_PULLDIS, false) == FAILURE) {
55 return -CMDERR_BADDIO;
58 vTaskDelay(10/portTICK_RATE_MS+2);
60 while ((pwm = *(ppwm++)))
61 for (i = 0; i < pulse_cnt; i++) {
62 if (rpp_gio_set(PIN_VBAT1EN, 1) == FAILURE) {
63 return -CMDERR_BADDIO;
65 for (j = 0; j < pwm; j++)
67 if (rpp_gio_set(PIN_VBAT1EN, 0) == FAILURE) {
68 return -CMDERR_BADDIO;
70 for (j = 0; j < VBATPWM_PERIOD - pwm; j++)
73 if (rpp_gio_set(PIN_VBAT1EN, 1) == FAILURE) {
74 return -CMDERR_BADDIO;
77 int32_t ret = rpp_gio_get(PIN_VBAT1EN);
79 return -CMDERR_BADDIO;
85 * @brief Power on VBAT and VBAT1 using PWM
87 * @param[in] cmd_io Pointer to IO stack
88 * @param[in] des Pointer to command descriptor
89 * @param[in] param Parameters of command
90 * @return value on VBAT port
92 int cmd_do_power_on(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
96 if (rpp_gio_set(PIN_VBATEN, 1) == FAILURE) {
97 return -CMDERR_BADDIO;
99 ret = rpp_gio_get(PIN_VBATEN);
100 if (ret == FAILURE) {
101 return -CMDERR_BADDIO;
103 ret |= vbat1_pwm() << 1;
104 return cmd_opchar_replong(cmd_io, param, ret, 0, 0);
108 * @brief Power off VBAT and VBAT1 using PWM
110 * @param[in] cmd_io Pointer to IO stack
111 * @param[in] des Pointer to command descriptor
112 * @param[in] param Parameters of command
113 * @return value on VBAT port
115 int cmd_do_power_off(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
119 if (rpp_gio_set(PIN_VBAT1EN, 0) == FAILURE) {
120 return -CMDERR_BADDIO;
122 ret = rpp_gio_get(PIN_VBAT1EN);
123 if (ret == FAILURE) {
124 return -CMDERR_BADDIO;
126 if (rpp_gio_set(PIN_VBATEN, 0) == FAILURE) {
127 return -CMDERR_BADDIO;
129 ret = rpp_gio_get(PIN_VBATEN);
130 if (ret == FAILURE) {
131 return -CMDERR_BADDIO;
135 return cmd_opchar_replong(cmd_io, param, ret, 0, 0);
140 /** Command descriptor for poweron */
141 cmd_des_t const cmd_des_power_on = {
143 "poweron","Enable VBATEN and VBAT1EN power supply by using PWM hack",
144 "### Command syntax ###\n"
148 "### Description ###\n"
150 "This command tries to work around error on VBAT power supply wiring\n"
151 "and attempts to switch the power supply on.\n"
153 "It turns on the VBAT voltage by slowly charging the capacitors\n"
154 "connected to the VBAT1 signal by using the software-generated PWM\n"
155 "signal with increasing duty cycle.\n"
157 "The poweron command has to be launched before any access to any SPI\n"
158 "peripherals, otherwise they will not work (or the power supply has to\n"
159 "be electrically bypassed).\n"
161 "Please note that parameters for the PWM signal may change from device\n"
162 "to device and it might be necessary to tune them (in source code) for\n"
164 CMD_HANDLER(cmd_do_power_on), (void *)&cmd_list_vbat
167 /** Command descriptor for poweroff */
168 cmd_des_t const cmd_des_power_off = {
170 "poweroff","Disables VBATEN and VBAT1EN power supply",
171 "### Command syntax ###\n"
175 "### Description ###\n"
177 "This command turns off VBAT and VBAT1 voltages.\n",
178 CMD_HANDLER(cmd_do_power_off), (void *)&cmd_list_vbat
181 /** List of commands for vbat, defined as external */
182 cmd_des_t const *cmd_list_vbat[] = {