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