]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_vbat.c
Update library
[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 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.
13  *
14  * File : cmd_vbat.c
15  *
16  * Abstract:
17  *      Commands for VBAT control
18  *          - Power VBAT1 on and off using PWM to charge capacitors
19  *
20  */
21
22 #include "cmd_vbat.h"
23
24 #ifndef DOCGEN
25
26 #include "rpp/rpp.h"
27 #include "drv/digital_io_def.h"
28
29 #define VBATPWM_PERIOD 400
30
31 /** Delay between set and clear vbat */
32 static int vbatPwmRampProfile[] = {
33         19,
34         20,
35         21,
36         23,
37         27,
38         30,
39         0
40 };
41
42 /**
43  *      @brief  Runs PWM defined by ramp profile on VBAT1
44  *
45  *      @return value on VBAT port
46  */
47 int vbat1_pwm()
48 {
49         int *ppwm = vbatPwmRampProfile;
50         int pwm;
51         volatile uint8_t val = 1;
52         int i, j;
53         int pulse_cnt = 100;
54
55         if (rpp_gio_set_output(DIO_PIN_NAME_VBAT1EN, 0) == FAILURE) {
56                 return -CMDERR_BADDIO;
57         }
58
59         vTaskDelay(10/portTICK_RATE_MS+2);
60         _disable_IRQ();
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;
65                         }
66                         for (j = 0; j < pwm; j++)
67                                 ;
68                         if (rpp_gio_set_val(DIO_PIN_NAME_VBAT1EN, 0) == FAILURE) {
69                                 return -CMDERR_BADDIO;
70                         }
71                         for (j = 0; j < VBATPWM_PERIOD - pwm; j++)
72                                 ;
73                 }
74         if (rpp_gio_set_val(DIO_PIN_NAME_VBAT1EN, 1) == FAILURE) {
75                 return -CMDERR_BADDIO;
76         }
77         _enable_IRQ();
78         int32_t ret = rpp_gio_get_val(DIO_PIN_NAME_VBAT1EN);
79         if (ret == FAILURE) {
80                 return -CMDERR_BADDIO;
81         }
82         return ret;
83 }
84
85 /**
86  * @brief       Power on VBAT and VBAT1 using PWM
87  *
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
92  */
93 int cmd_do_power_on(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
94 {
95         int ret = 0;
96
97         if (rpp_gio_set_val(DIO_PIN_NAME_VBATEN, 1) == FAILURE) {
98                 return -CMDERR_BADDIO;
99         }
100         ret = rpp_gio_get_val(DIO_PIN_NAME_VBATEN);
101         if (ret == FAILURE) {
102                 return -CMDERR_BADDIO;
103         }
104         ret |= vbat1_pwm() << 1;
105         return cmd_opchar_replong(cmd_io, param, ret, 0, 0);
106 }
107
108 /**
109  * @brief       Power off VBAT and VBAT1 using PWM
110  *
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
115  */
116 int cmd_do_power_off(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
117 {
118         int ret, ret2 = 0;
119
120         if (rpp_gio_set_val(DIO_PIN_NAME_VBAT1EN, 0) == FAILURE) {
121                 return -CMDERR_BADDIO;
122         }
123         ret = rpp_gio_get_val(DIO_PIN_NAME_VBAT1EN);
124         if (ret == FAILURE) {
125                 return -CMDERR_BADDIO;
126         }
127         if (rpp_gio_set_val(DIO_PIN_NAME_VBATEN, 0) == FAILURE) {
128                 return -CMDERR_BADDIO;
129         }
130         ret = rpp_gio_get_val(DIO_PIN_NAME_VBATEN);
131         if (ret == FAILURE) {
132                 return -CMDERR_BADDIO;
133         }
134
135         ret |= ret2 << 1;
136         return cmd_opchar_replong(cmd_io, param, ret, 0, 0);
137 }
138
139 #endif  /* DOCGEN */
140
141 /** Command descriptor for poweron */
142 cmd_des_t const cmd_des_power_on = {
143         0, 0,
144         "poweron","Enable VBATEN and VBAT1EN power supply by using PWM hack",
145         "### Command syntax ###\n"
146         "\n"
147         "    poweron\n"
148         "\n"
149         "### Description ###\n"
150         "\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"
153         "\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"
157         "\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"
161         "\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"
164         "each device.\n",
165         CMD_HANDLER(cmd_do_power_on), (void *)&cmd_list_vbat
166 };
167
168 /** Command descriptor for poweroff */
169 cmd_des_t const cmd_des_power_off = {
170         0, 0,
171         "poweroff","Disables VBATEN and VBAT1EN power supply",
172         "### Command syntax ###\n"
173         "\n"
174         "    poweroff\n"
175         "\n"
176         "### Description ###\n"
177         "\n"
178         "This command turns off VBAT and VBAT1 voltages.\n",
179         CMD_HANDLER(cmd_do_power_off), (void *)&cmd_list_vbat
180 };
181
182 /** List of commands for vbat, defined as external */
183 cmd_des_t const *cmd_list_vbat[] = {
184         &cmd_des_power_on,
185         &cmd_des_power_off,
186         NULL
187 };