]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_vbat.c
5f6b897ec7abac539ed5750d526c91a44896e53d
[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 {
57         int *ppwm = vbatPwmRampProfile;
58         int pwm;
59         volatile uint8_t val = 1;
60         int i, j;
61         int pulse_cnt = 100;
62         uint32_t desc;
63
64         desc = PIN_DSC_VBAT1EN;
65         hal_gpio_pin_set_value(desc, 0);
66         hal_gpio_pin_direction_output(desc, 0);
67         vTaskDelay(10/portTICK_RATE_MS+2);
68         _disable_IRQ();
69         while ((pwm = *(ppwm++)))
70                 for (i = 0; i < pulse_cnt; i++) {
71                         hal_gpio_pin_set_value(desc, 1);
72                         for (j = 0; j < pwm; j++)
73                                 ;
74                         hal_gpio_pin_set_value(desc, 0);
75                         for (j = 0; j < VBATPWM_PERIOD - pwm; j++)
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 {
93         int ret = 0;
94
95         hal_gpio_pin_set_value(PIN_DSC_VBATEN, 1);
96         ret = hal_gpio_pin_get_value(PIN_DSC_VBATEN);
97         ret |= vbat1_pwm() << 1;
98         return cmd_opchar_replong(cmd_io, param, ret, 0, 0);
99 }
100
101 /**
102  * @brief       Power off VBAT and VBAT1 using PWM
103  *
104  * @param[in]   cmd_io  Pointer to IO stack
105  * @param[in]   des             Pointer to command descriptor
106  * @param[in]   param   Parameters of command
107  * @return      value on VBAT port
108  */
109 int cmd_do_power_off(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
110 {
111         int ret = 0;
112
113         hal_gpio_pin_set_value(PIN_DSC_VBAT1EN, 0);
114         ret = hal_gpio_pin_get_value(PIN_DSC_VBAT1EN);
115         hal_gpio_pin_set_value(PIN_DSC_VBATEN, 0);
116         ret |= hal_gpio_pin_get_value(PIN_DSC_VBATEN) << 1;
117         return cmd_opchar_replong(cmd_io, param, ret, 0, 0);
118 }
119
120 #endif  /* DOCGEN */
121
122 /** Command descriptor for poweron */
123 cmd_des_t const cmd_des_power_on = {
124         0, 0,
125         "poweron","Enable VBATEN and VBAT1EN power supply by using PWM hack",
126         "### Command syntax ###\n"
127         "\n"
128         "    poweron\n"
129         "\n"
130         "### Description ###\n"
131         "\n"
132         "This command tries to work around error on VBAT power supply wiring\n"
133         "and attempts to switch the power supply on.\n"
134         "\n"
135         "It turns on the VBAT voltage by slowly charging the capacitors\n"
136         "connected to the VBAT1 signal by using the software-generated PWM\n"
137         "signal with increasing duty cycle.\n"
138         "\n"
139         "The poweron command has to be launched before any access to any SPI\n"
140         "peripherals, otherwise they will not work (or the power supply has to\n"
141         "be electrically bypassed).\n"
142         "\n"
143         "Please note that parameters for the PWM signal may change from device\n"
144         "to device and it might be necessary to tune them (in source code) for\n"
145         "each device.\n",
146         CMD_HANDLER(cmd_do_power_on), (void *)&cmd_list_vbat
147 };
148
149 /** Command descriptor for poweroff */
150 cmd_des_t const cmd_des_power_off = {
151         0, 0,
152         "poweroff","Disables VBATEN and VBAT1EN power supply",
153         "### Command syntax ###\n"
154         "\n"
155         "    poweroff\n"
156         "\n"
157         "### Description ###\n"
158         "\n"
159         "This command turns off VBAT and VBAT1 voltages.\n",
160         CMD_HANDLER(cmd_do_power_off), (void *)&cmd_list_vbat
161 };
162
163 /** List of commands for vbat, defined as external */
164 cmd_des_t const *cmd_list_vbat[] = {
165         &cmd_des_power_on,
166         &cmd_des_power_off,
167         NULL
168 };