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