]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_vbat.c
Convert doc from MediaWiki syntax to Markdown
[pes-rpp/rpp-test-sw.git] / rpp-test-sw / commands / cmd_vbat.c
1 /*
2  * Copyright (C) 2012-2013 Czech Technical University in Prague
3  *
4  * Created on: 28.2.2013
5  *
6  * Authors:
7  *     - Michal Horn
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * File : cmd_vbat.c
23  *
24  * Abstract:
25  *      Commands for VBAT control
26  *              - Power VBAT1 on and off using PWM to charge capacitors
27  *
28  */
29
30 #include "cmd_vbat.h"
31
32 #ifndef DOCGEN
33
34 #include "rpp/rpp.h"
35 #include "hal/hal.h"
36
37 #define VBATPWM_PERIOD 400
38
39 /** Delay between set and clear vbat */
40 static int vbatPwmRampProfile[] = {
41                 19,
42                 20,
43                 21,
44                 23,
45                 27,
46                 30,
47                 0
48 };
49
50 /**
51  *      @brief  Runs PWM defined by ramp profile on VBAT1
52  *
53  *      @return value on VBAT port
54  */
55 int vbat1_pwm() {
56         int *ppwm = vbatPwmRampProfile;
57         int pwm;
58         volatile uint8_t val = 1;
59         int i, j;
60         int pulse_cnt = 100;
61         uint32_t desc;
62
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);
67         _disable_IRQ();
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++)
72                                 ;
73                     hal_gpio_pin_set_value(desc, 0);
74                         for (j = 0; j < VBATPWM_PERIOD - pwm; j++)
75                                 ;
76                 }
77         }
78     hal_gpio_pin_set_value(desc, 1);
79         _enable_IRQ();
80         return hal_gpio_pin_get_value(desc);
81 }
82
83 /**
84  * @brief       Power on VBAT and VBAT1 using PWM
85  *
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
90  */
91 int cmd_do_power_on(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
92         int ret = 0;
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);
97 }
98
99 /**
100  * @brief       Power off VBAT and VBAT1 using PWM
101  *
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
106  */
107 int cmd_do_power_off(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
108         int ret = 0;
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);
114 }
115
116 #endif  /* DOCGEN */
117
118 /** Command descriptor for poweron */
119 cmd_des_t const cmd_des_power_on = {
120     0, 0,
121     "poweron","Enable VBATEN and VBAT1EN power supply by using PWM hack",
122     "### Command syntax ###\n"
123     "\n"
124     "    poweron\n"
125     "\n"
126     "### Description ###\n"
127     "\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"
130     "\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"
134     "\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"
138     "\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"
141     "each device.\n",
142     CMD_HANDLER(cmd_do_power_on), (void *)&cmd_list_vbat
143 };
144
145 /** Command descriptor for poweroff */
146 cmd_des_t const cmd_des_power_off = {
147     0, 0,
148     "poweroff","Disables VBATEN and VBAT1EN power supply",
149     "### Command syntax ###\n"
150     "\n"
151     "    poweroff\n"
152     "\n"
153     "### Description ###\n"
154     "\n"
155     "This command turns off VBAT and VBAT1 voltages.\n",
156     CMD_HANDLER(cmd_do_power_off), (void *)&cmd_list_vbat
157 };
158
159 /** List of commands for vbat, defined as external */
160 cmd_des_t const *cmd_list_vbat[]={
161   &cmd_des_power_on,
162   &cmd_des_power_off,
163   NULL
164 };