*/
#include "cmd_port.h"
+#include "stdio.h"
#ifndef DOCGEN
*/
int cmd_do_port_val(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
char *p;
- int opchar;
int i;
port_desc_t* desc;
uint32_t ret;
uint32_t values[MAX_PARAM_VALUES_NUM];
+ uint32_t val;
+ char portName[32];
+ char spareParams;
+ uint32_t numParams;
+
+ p = param[1];
+ if (sscanf(p, "%31s ", portName) != 1) {
+ return -CMDERR_BADPAR;
+ }
- if((opchar=cmd_opchar_check(cmd_io,des,param))<0) return opchar;
-
- if((desc = hal_port_get_dsc(param[1], param[2]-param[1]))==NULL) return -CMDERR_BADREG;
+ if((desc = hal_port_get_dsc(portName, -1))==NULL) return -CMDERR_BADREG;
- if(opchar==':'){
- p=param[3];
- si_skspace(&p);
- i = read_arg(&p, values, MAX_PARAM_VALUES_NUM, 16);
- if (i < 0)
- return i;
+ if(param[2] != NULL){ // More parameters = set values
+ p=param[2];
if (desc->port_setfnc_ptr == NULL) {
return -CMDERR_WRPERM;
}
else {
if (desc->interfaceType == PORT_INTERFACE_GPIO) {
- if (i != desc->numValues/8+1) // Information about pin values are encoded as hexadecimal 8b value
- return -CMDERR_BADPAR;
+ // Information about pin values are encoded as hexadecimal 8b value
+ numParams = desc->numValues/8+1;
}
else if (desc->interfaceType == PORT_INTERFACE_SPI) {
- if (i != desc->numValues) // Commands are passed as bytes
- return -CMDERR_BADPAR;
+ // Commands are passed as bytes
+ numParams = desc->numValues;
}
else if (desc->interfaceType == PORT_INTERFACE_ADC) {
return -CMDERR_BADPAR; // ADC is read only and no other port is supported
}
- if (desc->port_setfnc_ptr != NULL) {
- ret = desc->port_setfnc_ptr(desc->config, desc->numValues, values);
+ /* Read the parameter
+ * At the end test if no more parameters are left.
+ */
+ if (sscanf(p, " %x %1s", &val, &spareParams) != 1) {
+ return -CMDERR_BADPAR;
}
+ /* Divide the parameter into bytes, which is the format requested
+ * by the set function.
+ */
+ for (i = 0; i < numParams; i++) {
+ values[i] = ((val << (4-numParams+i)*8) & 0xFF000000) >> 3*8; // Left byte is the least significant
+ }
+ ret = desc->port_setfnc_ptr(desc->config, desc->numValues, values);
}
return cmd_opchar_replong(cmd_io, param, ret, 0, 16);
}
rpp_sci_printf("%d\r\n", values[i]);
}
}
- return cmd_opchar_replong(cmd_io, param, ret, 0, 16);
+ rpp_sci_printf("portval%s=%x", portName, ret);
+ return 0;
}
}
/** Command descriptor for read values from port command */
cmd_des_t const cmd_des_port_val={
- 0, CDESM_OPCHR|CDESM_RW,
+ 0, 0,
"portval*","Read or write values from or to the port",
"=== Command syntax ===\n"
"\n"
- " portval<NAME>:(<VAL>)\n"
+ " portval<NAME> <VAL>\n"
+ " portval<NAME>\n"
"where\n"
"* <NAME> is a string specifying the name of the port\n"
- "* : means set <VAL> on the port <NAME>\n"
- "* ? means get a value from the port <NAME>\n"
- "* <VAL> is a hexadecimal number in range 00 - FF\n"
+ "* <VAL> is a hexadecimal number in range 00 - FFFFFFFF\n"
"\n"
"=== Description ===\n"
"\n"
"\n"
"=== Example ===\n"
"\n"
- " --> portvalMOUTIN:(3A)\n"
+ " --> portvalMOUTIN 3A\n"
" portvalMOUTIN=0\n"
- " --> portvalMOUTIN?\n"
+ " --> portvalMOUTIN\n"
" 0\n"
" 1\n"
" 0\n"