]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_gpio.c
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
4 * SPDX-License-Identifier: GPL-2.0
10 #include "print-utils.h"
11 #include "getopt-min.h"
17 static uint_fast8_t pinnames_get(char *pin_names
[GPIO_MAX
+1])
19 static const FLASH
char delim1
[] = {":= "};
20 static const FLASH
char delim2
[] = {", "};
21 uint_fast8_t width
= 0;
25 memset(pin_names
, 0, (GPIO_MAX
+1) * sizeof(char *));
27 /* TODO: enters endless loop on wrong parameters */
29 lp
= getenv_str(PSTR(ENV_PINALIAS
));
31 pin_names
[GPIO_MAX
] = strdup(lp
);
32 if (pin_names
[GPIO_MAX
] != NULL
) {
33 char *ptr
= strtok_P(pin_names
[GPIO_MAX
], delim1
);
35 if (((i
= strtoul(ptr
, &lp
, 10)) < GPIO_MAX
) &&
37 (ptr
= strtok_P(NULL
, delim2
)) != NULL
) {
39 ptr
= strtok_P(NULL
, delim1
);
43 for (i
= 0; i
< GPIO_MAX
; i
++)
44 if (strlen(pin_names
[i
]) > width
)
45 width
= strlen(pin_names
[i
]);
52 static size_t xstrlen(char *s
)
60 static const FLASH
char * const FLASH pinconf_str
[] = {
68 static const FLASH
char * const FLASH pinlevel_str
[] = {
74 static void print_pin(uint8_t pin
, uint_fast8_t multi
, char *pin_names
[], uint_fast8_t pn_width
)
77 const FLASH
char *levelp
;
80 pinconf
= gpio_config_get(pin
);
81 if (pinconf
== OUTPUT_TIMER
) {
82 div
= gpio_clockdiv_get(pin
);
83 levelp
= pinlevel_str
[2];
85 levelp
= pinlevel_str
[gpio_read(pin
)];
88 printf_P(PSTR("%3d "), pin
);
90 printf_P(PSTR("%s "), pin_names
[pin
]);
91 print_blanks(pn_width
- xstrlen(pin_names
[pin
]));
93 my_puts_P(pinconf_str
[pinconf
]);
94 print_blanks(7 - strlen_P(pinconf_str
[pinconf
]));
96 print_blanks(5 - strlen_P(levelp
));
97 if (pinconf
== OUTPUT_TIMER
)
98 printf_P(PSTR("%8ld %8ld"),
101 printf_P(PSTR("%d: \"%s\", "), pin
, pin_names
[pin
] ? pin_names
[pin
] : 0);
102 my_puts_P(pinconf_str
[pinconf
]);
103 my_puts_P(PSTR(", "));
106 if (pinconf
== OUTPUT_TIMER
)
107 printf_P(PSTR("divide by %ld (%ldHz)"),
110 my_puts_P(PSTR("\n"));
113 static int_fast8_t pinarg_insert(uint8_t pin
, uint_fast8_t count
, uint_fast8_t pinarg
[])
120 for (pos
= 0; pos
< count
; pos
++) {
121 if (pin
== pinarg
[pos
])
123 if (pin
< pinarg
[pos
])
126 for (uint_fast8_t i
= count
-1; i
== pos
; i
--)
127 pinarg
[i
+1] = pinarg
[i
];
133 static uint_fast8_t pinarg_get(char * arg
, uint_fast8_t pinarg
[])
138 uint_fast8_t count
= 0;
141 pin1
= strtoul(arg
, &endp
, 10);
142 if (endp
!= arg
&& *endp
== '-') {
144 uint_fast8_t pin2
= (int) strtoul(arg
, &endp
, 10);
146 for (; pin1
< pin2
; pin1
++)
147 if ((rc
= pinarg_insert(pin1
, count
, pinarg
)) >= 0)
155 if ((*endp
== ',' || *endp
== '\0') &&
156 (rc
= pinarg_insert(pin1
, count
, pinarg
)) >= 0) {
170 command_ret_t
do_gpio(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char *const argv
[])
172 uint_fast8_t printheader
= 1;
173 uint_fast8_t pinarg
[GPIO_MAX
];
174 uint_fast8_t pinargc
;
180 while ((opt
= getopt(argc
, argv
, PSTR("s"))) != -1) {
186 return CMD_RET_USAGE
;
190 /* remaining arguments */
194 /* print config of all pins */
195 for (pinargc
= 0; pinargc
< GPIO_MAX
; pinargc
++)
196 pinarg
[pinargc
] = pinargc
;
199 pinargc
= pinarg_get(argv
[optind
++], pinarg
);
201 return CMD_RET_USAGE
;
208 /* no more args, print pin config */
210 char *pin_names
[GPIO_MAX
+ 1];
211 uint_fast8_t pin_names_width
= pinnames_get(pin_names
);
214 print_pin(pinarg
[0], 0, pin_names
, pin_names_width
);
217 if (pin_names_width
> 0) {
218 if ( strlen("Name") > pin_names_width
)
219 pin_names_width
= strlen("Name");
220 char s
[pin_names_width
+1];
221 memset(s
, ' ', pin_names_width
);
222 s
[pin_names_width
] = '\0';
223 strncpy_P(s
, PSTR("Name"), 4);
224 printf_P(PSTR("Pin %s Config Level Divider Frequency/Hz\n"),s
);
225 memset(s
, '-', pin_names_width
);
226 printf_P(PSTR("----%s-----------------------------------\n"), s
);
228 printf_P(PSTR("Pin Config Level Divider Frequency/Hz\n"
229 "--------------------------------------\n"));
231 for (uint_fast8_t i
= 0; i
< pinargc
; i
++)
232 print_pin(pinarg
[i
], 1, pin_names
, pin_names_width
);
234 if (pin_names
[GPIO_MAX
] != NULL
)
235 free(pin_names
[GPIO_MAX
]);
236 return CMD_RET_SUCCESS
;
239 /* arguments must be in pairs: pins conf */
241 return CMD_RET_USAGE
;
245 gpiomode_t mode
= NONE
;
247 unsigned long value
= 0;
250 switch (toupper(argv
[optind
][0])) {
265 value
= strtoul(argv
[optind
], &endp
, 10);
274 if (*endp
&& strcmp_P(endp
, PSTR("Hz")) == 0) {
280 printf_P(PSTR("invalid parameter: '%s'\n"), argv
[optind
]);
281 return CMD_RET_USAGE
;
285 printf_P(PSTR("invalid value: %lu \n"));
286 return CMD_RET_USAGE
;
290 if (value
> F_CPU
/ 2) {
291 printf_P(PSTR("Max frequency is: %luHz\n"), F_CPU
/2);
292 return CMD_RET_USAGE
;
301 return CMD_RET_USAGE
;
303 for (uint_fast8_t i
= 0; i
< pinargc
; i
++) {
306 gpio_write(pinarg
[i
], level
);
310 gpio_config(pinarg
[i
], mode
);
313 if (gpio_clockdiv_set(pinarg
[i
], value
) < 0) {
314 printf_P(PSTR("Setting pin %d to %lu failed.\n"),
324 pinargc
= pinarg_get(argv
[optind
++], pinarg
);
328 return CMD_RET_SUCCESS
;