]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blobdiff - rpp-test-sw/commands/cmd_spi.c
Change license to MIT
[pes-rpp/rpp-test-sw.git] / rpp-test-sw / commands / cmd_spi.c
index d09b4bac5f17e2b4fac7fab7974b8432e40a612c..ae5e7db17eba37b15833fc79c6d6986cb5053666 100644 (file)
@@ -1,29 +1,37 @@
 /*
 /*
- * Copyright (C) 2012-2013 Czech Technical University in Prague
+ * Copyright (C) 2012-2013, 2015 Czech Technical University in Prague
  *
  * Created on: 28.2.2013
  *
  * Authors:
  *     - Michal Horn
  *
  *
  * Created on: 28.2.2013
  *
  * Authors:
  *     - Michal Horn
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
  *
  *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
  *
  *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
  *
  * File : cmd_spi.c
  *
  * Abstract:
  *
  * File : cmd_spi.c
  *
  * Abstract:
- *     Command for processing data from SPI and
- *     lowlevel command for sending data on SPI
+ *      Command for processing data from SPI and
+ *      lowlevel command for sending data on SPI
  *
  */
 
  *
  */
 
 #include "cmdproc_utils.h"
 #include "drv/drv.h"
 #include <_isfuncdcl.h>
 #include "cmdproc_utils.h"
 #include "drv/drv.h"
 #include <_isfuncdcl.h>
+#include "spi_resp_transl.h"
 
 /**
 
 /**
- *     @brief  Translate SPI response according the command and peripheral type
+ *  @brief     Translate SPI response according the command and peripheral type
  *
  * @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
  */
  *
  * @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_spi_translate(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
-         char *p;
-         int i;
-         uint32_t ret;
-         uint32_t values[MAX_PARAM_VALUES_NUM];
-         uint32_t numFieldDesc;
-         uint32_t numCmdDesc;
-         uint32_t command;
-         uint32_t response;
-         spitr_reg_translate_table_t translatedData;
-         char peripheralName[32];
-         char spareParams;
+int cmd_do_spi_translate(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
+{
+       uint32_t command;
+       uint32_t response;
+       char peripheralName[32];
+       char spareParams;
 
 
-         p = param[1];
-         if (sscanf(p, "%31s ", peripheralName) != 1) {
-                 return -CMDERR_BADPAR;
-         }
+       if (sscanf(param[1], "%31s ", peripheralName) != 1)
+               return -CMDERR_BADPAR;
 
 
-         const spitr_cmd_map_t* cmdDesc = cmdDesc = get_spi_cmd_map(peripheralName, -1, &numCmdDesc);
-         if(cmdDesc == NULL) return -CMDERR_BADREG;
+       const spitr_port_cmds_t *cmds = spitr_get_cmds_by_port_name(peripheralName);
+       if (cmds == NULL) return -CMDERR_BADREG;
 
 
 
 
-         if(param[2] != NULL) {                // More parameters expected
-               p=param[2];
-               if (sscanf(p, "%x %x %1s", &values[0], &values[1], &spareParams) != 2) {
+       if (param[2] != NULL) {         // More parameters expected
+               if (sscanf(param[2], "%x %x %1s", &command, &response, &spareParams) != 2)
                        return -CMDERR_BADPAR;
                        return -CMDERR_BADPAR;
-               }
-               command = values[0];
-               response = values[1];
 
 
-               const spitr_field_desc_t* fieldDescs = get_spi_field_desc(cmdDesc, numCmdDesc, command, &numFieldDesc);
-               if (fieldDescs == NULL)
-                       return -CMDERR_BADPAR;
+               const spitr_cmd_desc_t *cmd_desc = get_spi_cmd_desc(cmds, command);
+               if (!cmd_desc)
+                       return -CMDERR_BADCMD;
 
 
-               ret = spitr_fill_tr_table(fieldDescs, numFieldDesc, response, &translatedData);
+               spitr_print_resp(cmd_desc, response);
 
 
-               for (i = 0; i < translatedData.num_rows; i++) {
-                       rpp_sci_printf("%s: %x\r\n", translatedData.row[i].field_name, translatedData.row[i].value);
-               }
-               return cmd_opchar_replong(cmd_io, param, ret, 0, 10);
-         }
-         else{
-                 return -CMDERR_BADPAR;
-         }
+               return cmd_opchar_replong(cmd_io, param, 0, 0, 10);
+       }
+       else
+               return -CMDERR_BADPAR;
 }
 
 
 }
 
 
@@ -97,7 +90,7 @@ int cmd_do_spi_translate(cmd_io_t *cmd_io, const struct cmd_des *des, char *para
 uint8_t spi_test_buf_tx[TEST_BUF];
 uint8_t spi_test_buf_rx[TEST_BUF];
 
 uint8_t spi_test_buf_tx[TEST_BUF];
 uint8_t spi_test_buf_rx[TEST_BUF];
 
-int spimst_print_rx(struct spi_drv *ifc, int status, int addr, uint8_t *buf)
+int spimst_print_rx(int status, uint8_t *buf)
 {
        int i;
 
 {
        int i;
 
@@ -106,7 +99,7 @@ int spimst_print_rx(struct spi_drv *ifc, int status, int addr, uint8_t *buf)
                return -1;
        }
 
                return -1;
        }
 
-       rpp_sci_printf("spirx:%#x(", addr);
+       rpp_sci_printf("spirx:(");
        for (i = 0; i < status; i++) {
                rpp_sci_printf("%#x", buf[i]);
                if (i < status-1)
        for (i = 0; i < status; i++) {
                rpp_sci_printf("%#x", buf[i]);
                if (i < status-1)
@@ -119,7 +112,7 @@ int spimst_print_rx(struct spi_drv *ifc, int status, int addr, uint8_t *buf)
 
 
 /**
 
 
 /**
- *     @brief  Send SPI command on SPI and receive response
+ *  @brief     Send SPI command on SPI and receive response
  *
  * @param[in]  cmd_io  Pointer to IO stack
  * @param[in]  des             Pointer to command descriptor
  *
  * @param[in]  cmd_io  Pointer to IO stack
  * @param[in]  des             Pointer to command descriptor
@@ -128,67 +121,53 @@ int spimst_print_rx(struct spi_drv *ifc, int status, int addr, uint8_t *buf)
  */
 int cmd_do_spimst(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
 {
  */
 int cmd_do_spimst(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
 {
-       spi_drv_t *ifc;
        int i;
        char *p;
        int i;
        char *p;
-       chartoken;
+       char *token;
        int ret;
        int ret;
-       unsigned int addr;
        unsigned int rq_len;
        unsigned int rq_len;
-       int spi;
+       enum spi_device dev;
        int values[MAX_PARAM_VALUES_NUM];
 
        int values[MAX_PARAM_VALUES_NUM];
 
-       p=param[1];
-       if (sscanf(p, "%d", &spi) != 1) {
+       p = param[1];
+       if (sscanf(p, "%d", &i) != 1)
                return -CMDERR_BADPAR;
                return -CMDERR_BADPAR;
-       }
-       p=param[2];
-       if (sscanf(p, " %d", &addr) != 1) {
+       dev = (enum spi_device)i;
+       if (dev >= _SPIDEV_COUNT)
                return -CMDERR_BADPAR;
                return -CMDERR_BADPAR;
-       }
        token = strtok(p, " ");
        token = strtok(NULL, " ");
        i = 0;
        while (i < MAX_PARAM_VALUES_NUM && token != NULL) {
        token = strtok(p, " ");
        token = strtok(NULL, " ");
        i = 0;
        while (i < MAX_PARAM_VALUES_NUM && token != NULL) {
-               if (sscanf(token, "%x", &values[i]) == EOF) {
+               if (sscanf(token, "%x", &values[i]) == EOF)
                        break;
                        break;
-               }
                token = strtok(NULL, " ");
                spi_test_buf_tx[i] = (uint8_t)values[i];
                i++;
        }
 
                token = strtok(NULL, " ");
                spi_test_buf_tx[i] = (uint8_t)values[i];
                i++;
        }
 
-
-       ifc = spi_find_drv(NULL, spi);
-       if (ifc == NULL)
-               return -CMDERR_NODEV;
-
-       if (!(ifc->flags & SPI_IFC_ON)) {
-               //if (spi_tms570_init(ifc) < 0)
-               return -CMDERR_BADCFG;
-       }
-
        rq_len = i;
 
        rq_len = i;
 
-       ret = spi_transfer(ifc, addr, rq_len, spi_test_buf_tx, spi_test_buf_rx);
-       spimst_print_rx(ifc, ret, addr, spi_test_buf_rx);
+       ret = spi_transfer(dev, rq_len, spi_test_buf_tx, spi_test_buf_rx);
+       spimst_print_rx(ret, spi_test_buf_rx);
        return 0;
 }
 
        return 0;
 }
 
-#endif /* DOCGEN */
+#endif  /* DOCGEN */
 /** Command descriptor for SPI response translation */
 /** Command descriptor for SPI response translation */
-cmd_des_t const cmd_des_spi_translate={
+cmd_des_t const cmd_des_spi_translate = {
        0, CDESM_OPCHR|CDESM_RW,
        "spitr*","Translate response from an SPI peripheral",
        0, CDESM_OPCHR|CDESM_RW,
        "spitr*","Translate response from an SPI peripheral",
-       "=== Command syntax ===\n"
+       "### Command syntax ###\n"
        "\n"
        "\n"
-       "   spitr<NAME> <CMD> <RESP>\n"
+       "    spitr<NAME> <CMD> <RESP>\n"
        "where\n"
        "where\n"
-       "* <NAME> is a string specifying the name of the peripheral (one of DINSPI, LOUT, DAC12, DAC34, HBR, FRAY1 and FRAY2)\n"
-       "* <CMD> is a hexadecimal number in range 0 - FFFFFFFF\n"
-       "* <RESP> is a hexadecimal number in range 0 - FFFFFFFF\n"
        "\n"
        "\n"
-       "=== Description ===\n"
+       "- `<NAME>` is a string specifying the name of the peripheral (one of DINSPI, LOUT, DAC12, DAC34, HBR, FRAY1 and FRAY2)\n"
+       "- `<CMD>` is a hexadecimal number in range 0 - FFFFFFFF\n"
+       "- `<RESP>` is a hexadecimal number in range 0 - FFFFFFFF\n"
+       "\n"
+       "### Description ###\n"
        "\n"
        "This command translates a response from SPI from many different\n"
        "peripherals into a human readable form. The SPI response is in the\n"
        "\n"
        "This command translates a response from SPI from many different\n"
        "peripherals into a human readable form. The SPI response is in the\n"
@@ -197,35 +176,35 @@ cmd_des_t const cmd_des_spi_translate={
        "response, the name of the peripheral and translates the response into\n"
        "the attribute-value table.\n"
        "\n"
        "response, the name of the peripheral and translates the response into\n"
        "the attribute-value table.\n"
        "\n"
-       "=== Example ===\n"
-       "   --> portvalDINSPI 7F 00 00\n"
-       "   portvalDINSPI=AAC03F\n"
-       "   --> spitrDINSPI 7F0000 3FC0AA\n"
-       "   Thermal flag        : 0\n"
-       "   INT flag    : 0\n"
-       "   SP0  - DIN0 : 1\n"
-       "   SP1  - DIN1 : 1\n"
-       "   SP2  - DIN2 : 1\n"
-       "   SP3  - DIN3 : 1\n"
-       "   SP4  - DIN4 : 1\n"
-       "   SP5  - DIN5 : 1\n"
-       "   SP6  - DIN6 : 1\n"
-       "   SP7  - DIN7 : 1\n"
-       "   SG0  - DIN8 : 0\n"
-       "   SG1  - DIN9 : 1\n"
-       "   SG2  - DIN10        : 0\n"
-       "   SG3  - DIN11        : 1\n"
-       "   SG4  - DIN12        : 0\n"
-       "   SG5  - DIN13        : 1\n"
-       "   SG6  - DIN14        : 0\n"
-       "   SG7  - DIN15        : 1\n"
-       "   SG8  - NA   : 0\n"
-       "   SG9  - NA   : 0\n"
-       "   SG10 - NA   : 0\n"
-       "   SG11 - NA   : 0\n"
-       "   SG12 - NA   : 0\n"
-       "   SG13 - NA   : 0\n"
-       "   spitrDINSPI=24\n"
+       "### Example ###\n"
+       "    --> portvalDINSPI 7F 00 00\n"
+       "    portvalDINSPI=AAC03F\n"
+       "    --> spitrDINSPI 7F0000 3FC0AA\n"
+       "    Thermal flag       : 0\n"
+       "    INT flag   : 0\n"
+       "    SP0  - DIN0        : 1\n"
+       "    SP1  - DIN1        : 1\n"
+       "    SP2  - DIN2        : 1\n"
+       "    SP3  - DIN3        : 1\n"
+       "    SP4  - DIN4        : 1\n"
+       "    SP5  - DIN5        : 1\n"
+       "    SP6  - DIN6        : 1\n"
+       "    SP7  - DIN7        : 1\n"
+       "    SG0  - DIN8        : 0\n"
+       "    SG1  - DIN9        : 1\n"
+       "    SG2  - DIN10       : 0\n"
+       "    SG3  - DIN11       : 1\n"
+       "    SG4  - DIN12       : 0\n"
+       "    SG5  - DIN13       : 1\n"
+       "    SG6  - DIN14       : 0\n"
+       "    SG7  - DIN15       : 1\n"
+       "    SG8  - NA  : 0\n"
+       "    SG9  - NA  : 0\n"
+       "    SG10 - NA  : 0\n"
+       "    SG11 - NA  : 0\n"
+       "    SG12 - NA  : 0\n"
+       "    SG13 - NA  : 0\n"
+       "    spitrDINSPI=24\n"
        "\n"
        "Translates response 0x3FC0AA returned by command 0x7F0000 into a human\n"
        "readable form.\n"
        "\n"
        "Translates response 0x3FC0AA returned by command 0x7F0000 into a human\n"
        "readable form.\n"
@@ -238,32 +217,33 @@ cmd_des_t const cmd_des_spi_translate={
 cmd_des_t const cmd_des_spimst = {
        0, 0,
        "spimst*", "Request SPI master communication",
 cmd_des_t const cmd_des_spimst = {
        0, 0,
        "spimst*", "Request SPI master communication",
-       "=== Command syntax ===\n"
+       "### Command syntax ###\n"
        "\n"
        "\n"
-       "   spimst<SPI> <ADDR> <DATA>\n"
+       "    spimst<SPI> <ADDR> <DATA>\n"
        "where\n"
        "where\n"
-       "* <SPI> is a number in range 0 - 4\n"
-       "* <ADDR> is a number in range 0 - 2\n"
-       "* <DATA> is a sequence of hexadecimal numbers, separated by spaces, e.g. 12 AA CD\n"
        "\n"
        "\n"
-       "=== Description ===\n"
+       "- `<SPI>` is a number in range 0 - 4\n"
+       "- `<ADDR>` is a number in range 0 - 2\n"
+       "- `<DATA>` is a sequence of hexadecimal numbers, separated by spaces, e.g. 12 AA CD\n"
+       "\n"
+       "### Description ###\n"
        "\n"
        "The command sends given data to the SPI peripheral and prints the\n"
        "response. The response contains the address and the received data in\n"
        "parentheses.\n"
        "\n"
        "\n"
        "The command sends given data to the SPI peripheral and prints the\n"
        "response. The response contains the address and the received data in\n"
        "parentheses.\n"
        "\n"
-       "=== Example ===\n"
+       "### Example ###\n"
        "\n"
        "\n"
-       "   --> spimst1 0 7F 00 00\n"
-       "   spirx:0x0(0x3f,0xc0,0xff)\n"
+       "    --> spimst1 0 7F 00 00\n"
+       "    spirx:0x0(0x3f,0xc0,0xff)\n"
        "\n"
        "Sends reset command (0x7F0000) to the DIN peripheral.\n",
        CMD_HANDLER(cmd_do_spimst), (void *)&cmd_list_spi
 };
 
 /** List of commands for SPI, defined as external */
        "\n"
        "Sends reset command (0x7F0000) to the DIN peripheral.\n",
        CMD_HANDLER(cmd_do_spimst), (void *)&cmd_list_spi
 };
 
 /** List of commands for SPI, defined as external */
-cmd_des_t const *cmd_list_spi[]={
-  &cmd_des_spi_translate,
-  &cmd_des_spimst,
-  NULL
+cmd_des_t const *cmd_list_spi[] = {
+       &cmd_des_spi_translate,
+       &cmd_des_spimst,
+       NULL
 };
 };