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
27 #include "drv/digital_io_def.h"
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;
55 if (rpp_gio_set_output(DIO_PIN_NAME_VBAT1EN, 0) == FAILURE) {
56 return -CMDERR_BADDIO;
59 vTaskDelay(10/portTICK_RATE_MS+2);
61 while ((pwm = *(ppwm++)))
62 for (i = 0; i < pulse_cnt; i++) {
63 if (rpp_gio_set_val(DIO_PIN_NAME_VBAT1EN, 1) == FAILURE) {
64 return -CMDERR_BADDIO;
66 for (j = 0; j < pwm; j++)
68 if (rpp_gio_set_val(DIO_PIN_NAME_VBAT1EN, 0) == FAILURE) {
69 return -CMDERR_BADDIO;
71 for (j = 0; j < VBATPWM_PERIOD - pwm; j++)
74 if (rpp_gio_set_val(DIO_PIN_NAME_VBAT1EN, 1) == FAILURE) {
75 return -CMDERR_BADDIO;
78 int32_t ret = rpp_gio_get_val(DIO_PIN_NAME_VBAT1EN);
80 return -CMDERR_BADDIO;
86 * @brief Power on VBAT and VBAT1 using PWM
88 * @param[in] cmd_io Pointer to IO stack
89 * @param[in] des Pointer to command descriptor
90 * @param[in] param Parameters of command
91 * @return value on VBAT port
93 int cmd_do_power_on(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
97 if (rpp_gio_set_val(DIO_PIN_NAME_VBATEN, 1) == FAILURE) {
98 return -CMDERR_BADDIO;
100 ret = rpp_gio_get_val(DIO_PIN_NAME_VBATEN);
101 if (ret == FAILURE) {
102 return -CMDERR_BADDIO;
104 ret |= vbat1_pwm() << 1;
105 return cmd_opchar_replong(cmd_io, param, ret, 0, 0);
109 * @brief Power off VBAT and VBAT1 using PWM
111 * @param[in] cmd_io Pointer to IO stack
112 * @param[in] des Pointer to command descriptor
113 * @param[in] param Parameters of command
114 * @return value on VBAT port
116 int cmd_do_power_off(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
120 if (rpp_gio_set_val(DIO_PIN_NAME_VBAT1EN, 0) == FAILURE) {
121 return -CMDERR_BADDIO;
123 ret = rpp_gio_get_val(DIO_PIN_NAME_VBAT1EN);
124 if (ret == FAILURE) {
125 return -CMDERR_BADDIO;
127 if (rpp_gio_set_val(DIO_PIN_NAME_VBATEN, 0) == FAILURE) {
128 return -CMDERR_BADDIO;
130 ret = rpp_gio_get_val(DIO_PIN_NAME_VBATEN);
131 if (ret == FAILURE) {
132 return -CMDERR_BADDIO;
136 return cmd_opchar_replong(cmd_io, param, ret, 0, 0);
141 /** Command descriptor for poweron */
142 cmd_des_t const cmd_des_power_on = {
144 "poweron","Enable VBATEN and VBAT1EN power supply by using PWM hack",
145 "### Command syntax ###\n"
149 "### Description ###\n"
151 "This command tries to work around error on VBAT power supply wiring\n"
152 "and attempts to switch the power supply on.\n"
154 "It turns on the VBAT voltage by slowly charging the capacitors\n"
155 "connected to the VBAT1 signal by using the software-generated PWM\n"
156 "signal with increasing duty cycle.\n"
158 "The poweron command has to be launched before any access to any SPI\n"
159 "peripherals, otherwise they will not work (or the power supply has to\n"
160 "be electrically bypassed).\n"
162 "Please note that parameters for the PWM signal may change from device\n"
163 "to device and it might be necessary to tune them (in source code) for\n"
165 CMD_HANDLER(cmd_do_power_on), (void *)&cmd_list_vbat
168 /** Command descriptor for poweroff */
169 cmd_des_t const cmd_des_power_off = {
171 "poweroff","Disables VBATEN and VBAT1EN power supply",
172 "### Command syntax ###\n"
176 "### Description ###\n"
178 "This command turns off VBAT and VBAT1 voltages.\n",
179 CMD_HANDLER(cmd_do_power_off), (void *)&cmd_list_vbat
182 /** List of commands for vbat, defined as external */
183 cmd_des_t const *cmd_list_vbat[] = {