]> rtime.felk.cvut.cz Git - rpp-test-sw.git/commitdiff
SPI: cmd_proc command & refactoring
authorRostislav Lisovy <lisovy@gmail.com>
Tue, 2 Oct 2012 18:35:41 +0000 (20:35 +0200)
committerRostislav Lisovy <lisovy@gmail.com>
Fri, 5 Oct 2012 08:35:42 +0000 (10:35 +0200)
include/spi_drv.h [moved from include/spi_comm.h with 93% similarity]
include/spi_tms570.h
include/utils.h [new file with mode: 0644]
source/commands.c
source/spi_tms570.c
source/sys_main.c

similarity index 93%
rename from include/spi_comm.h
rename to include/spi_drv.h
index f8ddad0bbda920b0f06fe986776834c613081b34..8273f0290ff12f275440d9b78b2792ab699df05a 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef _SPI_COMM_H_
-#define _SPI_COMM_H_
+#ifndef _SPI_DRV_H_
+#define _SPI_DRV_H_
 
 #include "sys_common.h"
 #include "ul_list.h"
@@ -53,5 +53,6 @@ typedef unsigned long spi_isr_lock_level_t;
 
 /* ------------------------------------------------------------------------- */
 int spi_transfer(spi_drv_t *ifc, int addr, int rq_len, const void *tx_buf, void *rx_buf);
+spi_drv_t *spi_find_drv(char *name, int number);
 
-#endif /* _SPI_COMM_H_ */
+#endif /* _SPI_DRV_H_ */
index d2be3acc74e8f6caac5174675f691be58ec4d633..b6aadaf98ad1252a6446c4eddef5e4458e7e0433 100644 (file)
@@ -2,7 +2,7 @@
 #define _MYSPI_H_
 
 #include "sys_common.h"
-#include "spi_comm.h"
+#include "spi_drv.h"
 
 #define spiREG2        ((spiBASE_t *)0xFFF7F600U)
 #define spiREG4        ((spiBASE_t *)0xFFF7FA00U)
diff --git a/include/utils.h b/include/utils.h
new file mode 100644 (file)
index 0000000..c0f145e
--- /dev/null
@@ -0,0 +1,28 @@
+#ifndef _UTILS_DEF_H_
+#define _UTILS_DEF_H_
+
+/* skip space/blank characters, return 0 if no space found */
+int si_skspace(char **ps);
+
+/* skip spaces and checks for <sepchars>, */
+/* if no such char return -1, else char is returned */
+int si_fndsep(char **ps, char *sepchrs);
+
+/* reads max <n> letters and digits to <pout> */
+/* returns number of readed chars */
+int si_alnumn(char **ps, char *pout, int n);
+
+/* same as above, but only letters are read */
+int si_alphan(char **ps, char *pout, int n);
+
+/* reads long number, if no digit found return -1 */
+int si_long(char **ps, long *val, int base);
+
+/* reads numbers into array, size of element representation is selected by blen */
+int si_add_to_arr(char **ps, void **pdata, int *plen, int base, int elsize,
+                 char *stop_chars);
+
+/* concatenate C main style arguments into one line */
+int concat_args2line(char **pline, int argc, char **argv);
+
+#endif                         /* _UTILS_DEF_H_ */
index bf3256453bc042188ed43c571656fa29e69bce72..ce5970f967af2b2887cb0ea8737f835f5693b98b 100644 (file)
 #include "vbat.h"
 #include "lin.h"
 #include "emac_test.h"
+#include "spi_drv.h"
+#include "spi_tms570.h"
+
+#include "ctype.h"
+#include "utils.h"
 
 /** @brief Count of AD channels in ADC1
 */
@@ -953,6 +958,100 @@ int cmd_do_test_mout_fault(cmd_io_t *cmd_io, const struct cmd_des *des, char *pa
        return 0;
 }
 
+/* SPI Master testing command */
+#define TEST_BUF 64
+uint8_t spi_test_buf_tx[TEST_BUF];
+uint8_t spi_test_buf_rx[TEST_BUF];
+
+static int test_rd_arr(char **ps, uint8_t *buf, int n)
+{
+       long val;
+       int c;
+       int i;
+
+       if (si_fndsep(ps, "({") < 0)
+               return -CMDERR_BADSEP;
+       i = 0;
+       si_skspace(ps);
+       if ((**ps != ')') && (**ps != '}'))
+               do {
+                       if (i >= n)
+                               return -CMDERR_BADPAR;
+                       if (si_long(ps, &val, 16) < 0)
+                               return -CMDERR_BADPAR;
+                       buf[i] = val;
+                       i++;
+                       if ((c = si_fndsep(ps, ",)}")) < 0)
+                               return -CMDERR_BADSEP;
+               } while (c == ',');
+
+       return i;
+}
+
+int spimst_print_rx(struct spi_drv *ifc, int status, int addr, uint8_t *buf)
+{
+       int i;
+
+//FIXME print vs. printf
+
+       print("SPI\n\r");
+       printf("SPI! %02X ", addr);
+       if (status < 0)
+               print("FAIL ");
+
+       printf(" RX(");
+       for (i = 0; i < status; i++)
+               printf("%s%02X", i ? "," : "", buf[i]);
+       printf(")");
+       printf("\n");
+
+       return 0;
+}
+
+
+int cmd_do_spimst(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
+{
+       spi_drv_t *ifc;
+       int i;
+       char *p;
+       long val;
+       int ret;
+       unsigned int addr;
+       unsigned int rq_len;
+
+       if (!&param[1] || !(*param[1] >= '0' && *param[1] <= '9'))
+               return -CMDERR_BADSUF;
+
+       if (*param[2] != ':')
+               return -CMDERR_OPCHAR;
+
+       ifc = spi_find_drv(NULL, *param[1] - '0');
+       if (ifc == NULL)
+               return -CMDERR_NODEV;
+
+       if (!(ifc->flags & SPI_IFC_ON)) {
+               //if (spi_tms570_init(ifc) < 0)
+               return -CMDERR_BADCFG;
+       }
+
+       si_skspace(&p);
+       if (isdigit(*p)) {
+               if (si_long(&p, &val, 16) < 0)
+                       return -CMDERR_BADPAR;
+               addr = val;
+       }
+       si_skspace(&p);
+       i = test_rd_arr(&p, spi_test_buf_tx, TEST_BUF);
+       if (i < 0)
+               return 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);
+       return 0;
+}
+
+
 /* ------------------------------
  * User defined command variables
  * ------------------------------
@@ -1064,6 +1163,12 @@ cmd_des_t const cmd_des_help={
     cmd_do_help, (void *)&cmd_list
 };
 
+cmd_des_t const cmd_des_spimst = { 0, CDESM_OPCHR,
+       "SPIMST#", "SPI master communication request",
+       cmd_do_spimst, (void *)&cmd_list
+};
+
+
 /*  ------------------------
  *  Command lists definitons
  *  ------------------------
index b2c26ded42055c97a6578193a0714061954dcfa5..622fc02d9b1cbd9eeaa321b470d6e91e020f453c 100644 (file)
@@ -1,7 +1,7 @@
 /* Code based on Halcogen generated source code */
 
 #include "spi_tms570.h"
-#include "spi_comm.h"
+#include "spi_drv.h"
 #include "sys_common.h"
 
 static int spi_tms570_ctrl_fnc(spi_drv_t *ifc, int ctrl, void *p);
@@ -434,6 +434,13 @@ void spi_tms570_isr(int spi_ifc, uint32_t flags)
        }
 }
 
+spi_drv_t *spi_find_drv(char *name, int number)
+{
+       if (number < 0 || number >= (sizeof(spi_tms570_ifcs)/sizeof(spi_tms570_ifcs[0])))
+               return NULL;
+
+       return  &spi_tms570_ifcs[number].spi_drv;
+}
 
 #pragma INTERRUPT(spi2LowLevelInterrupt, IRQ)
 void spi2LowLevelInterrupt(void)
index c12663907581ed0a6d006878b2f2e964d5de7832..daaf2340fcb2cb3889befea04b1ccb1147b651b4 100644 (file)
@@ -30,7 +30,7 @@
 #include "lin.h"\r
 #include "emac_test.h"\r
 #include "spi_tms570.h"\r
-#include "spi_comm.h"\r
+#include "spi_drv.h"\r
 /* USER CODE END */\r
 \r
 \r