]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blobdiff - rpp-test-sw/commands/cmd_dac.c
Merge branches 'master' and 'rm48/master'
[pes-rpp/rpp-test-sw.git] / rpp-test-sw / commands / cmd_dac.c
diff --git a/rpp-test-sw/commands/cmd_dac.c b/rpp-test-sw/commands/cmd_dac.c
new file mode 100644 (file)
index 0000000..744c998
--- /dev/null
@@ -0,0 +1,230 @@
+/*
+ * 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_dac.c
+ *
+ * Abstract:
+ *      This file contains commands for DAC control. User can setup DIN pin as active or inactive, set voltage in mV units and set raw value on pin.
+ *
+ */
+
+#include "cmd_dac.h"
+#include "stdio.h"
+
+#ifndef DOCGEN
+
+#include "cmdproc_utils.h"
+#include "rpp/rpp.h"
+
+
+/**
+ * @brief      Setup DAC pin enable or disable
+ *
+ * @param[in]  cmd_io  Pointer to IO stack
+ * @param[in]  des             Pointer to command descriptor
+ * @param[in]  param   Parameters of command
+ * @return     0 when OK or error code
+ */
+int cmd_do_dac_pin_setup(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
+{
+       int pin;
+       char *p = param[1];
+       char spareParams;
+       int enabled;
+       int ret;
+
+       if (sscanf(p, "%d %d %1s", &pin, &enabled, &spareParams) != 2)
+               return -CMDERR_BADPAR;
+       ret = rpp_dac_setup(pin, enabled);
+       if (ret == -1) {
+               rpp_sci_printf("Pin out of range.\n");
+               return -CMDERR_BADPAR;
+       }
+
+       if (rpp_dac_update() == FAILURE) {
+               rpp_sci_printf("DAC update failed.\n");
+               return -CMDERR_EIO;
+       }
+
+       return cmd_opchar_replong(cmd_io, param, enabled, 0, 10);
+}
+
+/**
+ * @brief      Set DAC pin raw value
+ *
+ * @param[in]  cmd_io  Pointer to IO stack
+ * @param[in]  des             Pointer to command descriptor
+ * @param[in]  param   Parameters of command
+ * @return     0 when OK or error code
+ */
+int cmd_do_dac_pin_set_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
+{
+       int pin;
+       char *p = param[1];
+       char spareParams;
+       int val;
+       int ret;
+
+       if (sscanf(p, "%d %d %1s", &pin, &val, &spareParams) != 2)
+               return -CMDERR_BADPAR;
+       ret = rpp_dac_set(pin, val);
+       if (ret == -1) {
+               rpp_sci_printf("Pin out of range.\n");
+               return -CMDERR_BADPAR;
+       }
+       else if (ret == -2) {
+               rpp_sci_printf("Value out of range.\n");
+               return -CMDERR_BADPAR;
+       }
+
+       if (rpp_dac_update() == FAILURE) {
+               rpp_sci_printf("DAC update failed.\n");
+               return -CMDERR_EIO;
+       }
+       return cmd_opchar_replong(cmd_io, param, val, 0, 10);
+}
+
+/**
+ * @brief      Set DAC pin voltage value in mV units
+ *
+ * @param[in]  cmd_io  Pointer to IO stack
+ * @param[in]  des             Pointer to command descriptor
+ * @param[in]  param   Parameters of command
+ * @return     0 when OK or error code
+ */
+int cmd_do_dac_pin_set_voltage(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
+{
+       int pin;
+       char *p = param[1];
+       char spareParams;
+       int mV;
+       int ret;
+
+       if (sscanf(p, "%d %d %1s", &pin, &mV, &spareParams) != 2)
+               return -CMDERR_BADPAR;
+       ret = rpp_dac_set_voltage(pin, mV);
+       if (ret == -1) {
+               rpp_sci_printf("Pin out of range.\n");
+               return -CMDERR_BADPAR;
+       }
+       else if (ret == -2) {
+               rpp_sci_printf("Voltage out of range.\n");
+               return -CMDERR_BADPAR;
+       }
+
+       if (rpp_dac_update() == FAILURE) {
+               rpp_sci_printf("DAC update failed.\n");
+               return -CMDERR_EIO;
+       }
+       return cmd_opchar_replong(cmd_io, param, mV, 0, 10);
+}
+
+#endif  /* DOCGEN */
+
+/** Descriptor of command for dac pin setup */
+cmd_des_t const cmd_des_dac_pin_setup = {
+       0, 0,
+       "dacpinenable*","Enable or disable a DAC pin",
+       "### Command syntax ###\n"
+       "\n"
+       "    dacpinenable<PIN> <VALUE>\n"
+       "where\n"
+       "\n"
+       "- `<PIN>` is a number in range 1-4\n"
+       "- `<VALUE>` is a number 0 (disable) or 1 (enable)\n"
+       "\n"
+       "### Description ###\n"
+       "\n"
+       "Command for enabling or disabling of a DAC pin.\n"
+       "\n"
+       "Command always prints the actual state of the selected pin.\n"
+       "\n"
+       "### Example ###\n"
+       "\n"
+       "    --> dacpinenable1 1\n"
+       "    dacpinenable1=1\n"
+       "\n"
+       "Enables pin DAC1 and prints its actual state (which will be 1)\n",
+       CMD_HANDLER(cmd_do_dac_pin_setup), (void *)&cmd_list_dac
+};
+
+/** Descriptor of command for dac pin value set */
+cmd_des_t const cmd_des_dac_pin_set_val = {
+       0, 0,
+       "dacpinval*","Set raw value of a DAC register",
+       "### Command syntax ###\n"
+       "\n"
+       "    dacpinval<PIN> <VALUE>\n"
+       "where\n"
+       "\n"
+       "- `<PIN>` is a number in range 1-4\n"
+       "- `<VALUE>` is a number in range 0-4095\n"
+       "\n"
+       "### Description ###\n"
+       "\n"
+       "This command writes a raw value to DAC register that controls the DAC\n"
+       "output voltage according to the formula described in the datasheet.\n"
+       "`<PIN>` parameter selects which DAC pin to use.\n"
+       "\n"
+       "Command always prints the written raw value of the selected pin. There\n"
+       "is no way how to read the value out of the register.\n"
+       "\n"
+       "### Example ###\n"
+       "\n"
+       "    --> dacpinval1 4095\n"
+       "    dacpinval1 =4095\n"
+       "\n"
+       "Set pin DAC1 voltage to 12V, and prints the value (4095).\n",
+       CMD_HANDLER(cmd_do_dac_pin_set_val), (void *)&cmd_list_dac
+};
+
+/** Descriptor of command for dac pin voltage set */
+cmd_des_t const cmd_des_dac_pin_set_voltage = {
+       0, 0,
+       "dacpinvoltage*","Set voltage in mV of a DAC pin",
+       "### Command syntax ###\n"
+       "\n"
+       "    dacpinvoltage<PIN> <VALUE>\n"
+       "where\n"
+       "\n"
+       "- `<PIN>` is a number in range 1-4\n"
+       "- `<VALUE>` is a number in range 0-12000\n"
+       "\n"
+       "### Description ###\n"
+       "\n"
+       "This command sets the voltage on a DAC pin.\n"
+       "\n"
+       "The command always prints the actually set voltage of selected pin.\n"
+       "There is no way how to read the value back out of the pin.\n"
+       "\n"
+       "### Example ###\n"
+       "\n"
+       "    --> dacpinvoltage1 8000\n"
+       "    dacpinvoltage1 =8000\n"
+       "\n"
+       "Sets pin DAC1 to 8V, prints the actual voltage (8000)\n"
+       "\n"
+       "    --> dacpinvoltage2 500\n"
+       "    dacpinvoltage2 =500\n"
+       "\n"
+       "Sets pin DAC2 to 500mV, prints actual voltage (500)\n",
+       CMD_HANDLER(cmd_do_dac_pin_set_voltage), (void *)&cmd_list_dac
+};
+
+/** List of commands for dac, defined as external */
+cmd_des_t const *cmd_list_dac[] = {
+       &cmd_des_dac_pin_setup,
+       &cmd_des_dac_pin_set_val,
+       &cmd_des_dac_pin_set_voltage,
+       NULL
+};