--- /dev/null
+/*
+ * Copyright (C) 2012-2013 Czech Technical University in Prague
+ *
+ * Created on: 28.2.2013
+ *
+ * Authors:
+ * - Michal Horn
+ *
+ * This document contains proprietary information belonging to Czech
+ * Technical University in Prague. Passing on and copying of this
+ * document, and communication of its contents is not permitted
+ * without prior written authorization.
+ *
+ * File : cmd_vbat.c
+ *
+ * Abstract:
+ * Commands for VBAT control
+ * - Power VBAT1 on and off using PWM to charge capacitors
+ *
+ */
+
+#include "cmd_vbat.h"
+
+#ifndef DOCGEN
+
+#include "rpp/rpp.h"
+#include "hal/hal.h"
+
+#define VBATPWM_PERIOD 400
+
+/** Delay between set and clear vbat */
+static int vbatPwmRampProfile[] = {
+ 19,
+ 20,
+ 21,
+ 23,
+ 27,
+ 30,
+ 0
+};
+
+/**
+ * @brief Runs PWM defined by ramp profile on VBAT1
+ *
+ * @return value on VBAT port
+ */
+int vbat1_pwm()
+{
+ int *ppwm = vbatPwmRampProfile;
+ int pwm;
+ volatile uint8_t val = 1;
+ int i, j;
+ int pulse_cnt = 100;
+ uint32_t desc;
+
+ desc = PIN_DSC_VBAT1EN;
+ hal_gpio_pin_set_value(desc, 0);
+ hal_gpio_pin_direction_output(desc, 0);
+ vTaskDelay(10/portTICK_RATE_MS+2);
+ _disable_IRQ();
+ while ((pwm = *(ppwm++)))
+ for (i = 0; i < pulse_cnt; i++) {
+ hal_gpio_pin_set_value(desc, 1);
+ for (j = 0; j < pwm; j++)
+ ;
+ hal_gpio_pin_set_value(desc, 0);
+ for (j = 0; j < VBATPWM_PERIOD - pwm; j++)
+ ;
+ }
+ hal_gpio_pin_set_value(desc, 1);
+ _enable_IRQ();
+ return hal_gpio_pin_get_value(desc);
+}
+
+/**
+ * @brief Power on VBAT and VBAT1 using PWM
+ *
+ * @param[in] cmd_io Pointer to IO stack
+ * @param[in] des Pointer to command descriptor
+ * @param[in] param Parameters of command
+ * @return value on VBAT port
+ */
+int cmd_do_power_on(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
+{
+ int ret = 0;
+
+ hal_gpio_pin_set_value(PIN_DSC_VBATEN, 1);
+ ret = hal_gpio_pin_get_value(PIN_DSC_VBATEN);
+ ret |= vbat1_pwm() << 1;
+ return cmd_opchar_replong(cmd_io, param, ret, 0, 0);
+}
+
+/**
+ * @brief Power off VBAT and VBAT1 using PWM
+ *
+ * @param[in] cmd_io Pointer to IO stack
+ * @param[in] des Pointer to command descriptor
+ * @param[in] param Parameters of command
+ * @return value on VBAT port
+ */
+int cmd_do_power_off(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
+{
+ int ret = 0;
+
+ hal_gpio_pin_set_value(PIN_DSC_VBAT1EN, 0);
+ ret = hal_gpio_pin_get_value(PIN_DSC_VBAT1EN);
+ hal_gpio_pin_set_value(PIN_DSC_VBATEN, 0);
+ ret |= hal_gpio_pin_get_value(PIN_DSC_VBATEN) << 1;
+ return cmd_opchar_replong(cmd_io, param, ret, 0, 0);
+}
+
+#endif /* DOCGEN */
+
+/** Command descriptor for poweron */
+cmd_des_t const cmd_des_power_on = {
+ 0, 0,
+ "poweron","Enable VBATEN and VBAT1EN power supply by using PWM hack",
+ "### Command syntax ###\n"
+ "\n"
+ " poweron\n"
+ "\n"
+ "### Description ###\n"
+ "\n"
+ "This command tries to work around error on VBAT power supply wiring\n"
+ "and attempts to switch the power supply on.\n"
+ "\n"
+ "It turns on the VBAT voltage by slowly charging the capacitors\n"
+ "connected to the VBAT1 signal by using the software-generated PWM\n"
+ "signal with increasing duty cycle.\n"
+ "\n"
+ "The poweron command has to be launched before any access to any SPI\n"
+ "peripherals, otherwise they will not work (or the power supply has to\n"
+ "be electrically bypassed).\n"
+ "\n"
+ "Please note that parameters for the PWM signal may change from device\n"
+ "to device and it might be necessary to tune them (in source code) for\n"
+ "each device.\n",
+ CMD_HANDLER(cmd_do_power_on), (void *)&cmd_list_vbat
+};
+
+/** Command descriptor for poweroff */
+cmd_des_t const cmd_des_power_off = {
+ 0, 0,
+ "poweroff","Disables VBATEN and VBAT1EN power supply",
+ "### Command syntax ###\n"
+ "\n"
+ " poweroff\n"
+ "\n"
+ "### Description ###\n"
+ "\n"
+ "This command turns off VBAT and VBAT1 voltages.\n",
+ CMD_HANDLER(cmd_do_power_off), (void *)&cmd_list_vbat
+};
+
+/** List of commands for vbat, defined as external */
+cmd_des_t const *cmd_list_vbat[] = {
+ &cmd_des_power_on,
+ &cmd_des_power_off,
+ NULL
+};