]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - commands/cmd_port.c
Move commands to toplevel directory (outside of lib)
[pes-rpp/rpp-test-sw.git] / commands / cmd_port.c
1 /*
2  * Copyright (C) 2012-2013 Czech Technical University in Prague
3  *
4  * Created on: 28.2.2013
5  *
6  * Authors:
7  *     - Michal Horn
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * File : cmd_port.c
23  *
24  * Abstract:
25  *               Commands for port controlling
26  *              - Printing list of available ports (not yet available)
27  *              - Setting/getting port value*
28  */
29
30 #include "cmd_port.h"
31
32 #ifndef DOCGEN
33
34 #include "rpp/rpp.h"
35 #include "hal/hal.h"
36 #include "cmdproc_utils.h"
37
38
39 int cmd_do_port_list(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
40         uint32_t i;
41         rpp_sci_printf("List of all defined ports with its type. Those names can be used by portval command.\r\n");
42         const port_def_t* ports = hal_port_get_definitions();
43
44         for (i = 0; i < PORT_CNT; i++) {
45                 if (ports[i].name == PIN_NAME_UNUSED) continue;
46                 rpp_sci_printf("%s\r\n", ports[i].name);
47         }
48         return 1;
49 }
50
51
52
53 /**
54  * @brief       Read values from specified port
55  *
56  * Command syntax:  portvalNAME:(val)   - Set value val to port specified by NAME (can be obtsined by portlist command). 1st bit -> PORT_PIN_1, 2nd bit -> PORT_PIN_2)
57  *                                      portvalNAME?    - Get value on port specified by NAME (can be obtsined by portlist command). 1st bit -> PORT_PIN_1, 2nd bit -> PORT_PIN_2)
58  *
59  * @param[in]   cmd_io  Pointer to IO stack
60  * @param[in]   des             Pointer to command descriptor
61  * @param[in]   param   Parameters of command
62  * @return      0 when OK or error code
63  */
64 int cmd_do_port_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
65           char *p;
66           int opchar;
67           int i;
68           port_desc_t* desc;
69           uint32_t ret;
70           uint32_t values[MAX_PARAM_VALUES_NUM];
71
72           if((opchar=cmd_opchar_check(cmd_io,des,param))<0) return opchar;
73
74           if((desc = hal_port_get_dsc(param[1], param[2]-param[1]))==NULL) return -CMDERR_BADREG;
75
76           if(opchar==':'){
77             p=param[3];
78                 si_skspace(&p);
79                 i = read_arg(&p, values, MAX_PARAM_VALUES_NUM, 16);
80                 if (i < 0)
81                         return i;
82                 if (i != desc->numValues)
83                         return -CMDERR_BADPAR;
84
85             ret = desc->port_setfnc_ptr(desc->config, desc->numValues, values);
86             return cmd_opchar_replong(cmd_io, param, ret, 0, 16);
87           }
88           else{
89                 ret = desc->port_getfnc_ptr(desc->config, desc->numValues, values);
90                 for (i = 0; i < desc->numValues; i++) {
91                         rpp_sci_printf("%h\r\n", values[i]);
92                 }
93             return cmd_opchar_replong(cmd_io, param, ret, 0, 16);
94           }
95 }
96
97 #endif  /* DOCGEN */
98
99 /** Command descriptor for read values from port command */
100 cmd_des_t const cmd_des_port_val={
101     0, CDESM_OPCHR|CDESM_RW,
102     "portval*","Read or set values from port",
103     "=== Description ===\n"
104     "\n"
105     "This command set or get values of all pins on specified port. First\n"
106     "parameter specifies a name of the port, than operation character :\n"
107     "(setter) or ? (getter) and if setter, then another hexadecimal number\n"
108     "specifies the value assigned to the pins. First bit is assigned to the\n"
109     "first pin, second bit is assigned to the second pin, etc.\n"
110     "\n"
111     "Port names will be available in portlist command, witch is not yet\n"
112     "ready.\n"
113     "\n"
114     "=== Command syntax ===\n"
115     "\n"
116     "   --> portvalNAME:(VAL)\n"
117     "where\n"
118     "* NAME is a string\n"
119     "* : means assign VAL to port NAME, ? means get value from port NAME\n"
120     "* VAL is in range 00 - FF\n"
121     "\n"
122     "=== Example ===\n"
123     "\n"
124     "   --> portvalLOUT:(FA)\n"
125     "LOUT1 = 0\n"
126     "\n"
127     "LOUT2 = 1\n"
128     "\n"
129     "LOUT3 = 0\n"
130     "\n"
131     "LOUT4 = 1\n"
132     "\n"
133     "LOUT5-8 = 1\n",
134     CMD_HANDLER(cmd_do_port_val), (void *)&cmd_list_port
135 };
136
137 /** Command descriptor for port list printout */
138 cmd_des_t const cmd_des_port_list={
139     0, 0,
140     "portlist","Print list of port names",
141     "\n",
142     CMD_HANDLER(cmd_do_port_list), (void *)&cmd_list_port
143 };
144
145 /** List of commands for port, defined as external */
146 cmd_des_t const *cmd_list_port[]={
147   &cmd_des_port_val,
148   &cmd_des_port_list,
149   NULL
150 };