]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp/lib/cmdproc/src/commands/cmd_vbat.c
3b31b07ddcfc169546153cecf9b040b78ffabf68
[pes-rpp/rpp-test-sw.git] / rpp / lib / cmdproc / src / commands / cmd_vbat.c
1 /*
2  * cmd_vbat.c
3  *
4  *  Created on: 28.2.2013
5  *      Author: Michal Horn
6  *
7  *  Commands for VBAT control
8  *  - Power VBAT1 on and off using PWM to charge capacitors
9  */
10
11 #include "rpp/rpp.h"
12 #include "commands/cmd_vbat.h"
13 #include "hal/hal.h"
14
15 #define VBATPWM_PERIOD 400
16
17 /** Delay between set and clear vbat */
18 static int vbatPwmRampProfile[] = {
19                 19,
20                 20,
21                 21,
22                 23,
23                 27,
24                 30,
25                 0
26 };
27
28 /**
29  *      @brief  Runs PWM defined by ramp profile on VBAT1
30  *
31  *      @return value on VBAT port
32  */
33 int vbat1_pwm() {
34         int *ppwm = vbatPwmRampProfile;
35         int pwm;
36         volatile uint8_t val = 1;
37         int i, j;
38         int pulse_cnt = 100;
39         uint32_t desc;
40
41         desc = PIN_DSC_VBAT1EN;
42     hal_gpio_pin_set_value(desc, 0);
43     hal_gpio_pin_direction_output(desc, 0);
44         vTaskDelay(10/portTICK_RATE_MS+2);
45         _disable_IRQ();
46         while((pwm = *(ppwm++))) {
47                 for (i = 0; i < pulse_cnt; i++) {
48                     hal_gpio_pin_set_value(desc, 1);
49                         for (j = 0; j < pwm; j++)
50                                 ;
51                     hal_gpio_pin_set_value(desc, 0);
52                         for (j = 0; j < VBATPWM_PERIOD - pwm; j++)
53                                 ;
54                 }
55         }
56     hal_gpio_pin_set_value(desc, 1);
57         _enable_IRQ();
58         return hal_gpio_pin_get_value(desc);
59 }
60
61 /**
62  * @brief       Power on VBAT and VBAT1 using PWM
63  *
64  * Command syntax: poweron
65  *
66  * @param[in]   cmd_io  Pointer to IO stack
67  * @param[in]   des             Pointer to command descriptor
68  * @param[in]   param   Parameters of command
69  * @return      value on VBAT port
70  */
71 int cmd_do_power_on(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
72         int ret = 0;
73         hal_gpio_pin_set_value(PIN_DSC_VBATEN, 1);
74         ret = hal_gpio_pin_get_value(PIN_DSC_VBATEN);
75     ret |= vbat1_pwm() << 1;
76     return cmd_opchar_replong(cmd_io, param, ret, 0, 0);
77 }
78
79 /**
80  * @brief       Power off VBAT and VBAT1 using PWM
81  *
82  * Command syntax: poweroff
83  *
84  * @param[in]   cmd_io  Pointer to IO stack
85  * @param[in]   des             Pointer to command descriptor
86  * @param[in]   param   Parameters of command
87  * @return      value on VBAT port
88  */
89 int cmd_do_power_off(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
90         int ret = 0;
91         hal_gpio_pin_set_value(PIN_DSC_VBAT1EN, 0);
92         ret = hal_gpio_pin_get_value(PIN_DSC_VBAT1EN);
93         hal_gpio_pin_set_value(PIN_DSC_VBATEN, 0);
94         ret |= hal_gpio_pin_get_value(PIN_DSC_VBATEN) << 1;
95     return cmd_opchar_replong(cmd_io, param, ret, 0, 0);
96 }
97
98 /** Command descriptor for poweron */
99 cmd_des_t const cmd_des_power_on = {
100     0, 0,
101     "poweron","Set VBATEN and VBAT1EN using PWM",
102     cmd_do_power_on, (void *)&cmd_list_vbat
103 };
104
105 /** Command descriptor for poweroff */
106 cmd_des_t const cmd_des_power_off = {
107     0, 0,
108     "poweroff","Clear VBATEN and VBAT1EN",
109     cmd_do_power_off, (void *)&cmd_list_vbat
110 };
111
112 /** List of commands for vbat, defined as external */
113 cmd_des_t const *cmd_list_vbat[]={
114   &cmd_des_power_on,
115   &cmd_des_power_off,
116   NULL
117 };
118