]>
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+
13 #include "print-utils.h"
14 #include "getopt-min.h"
21 static const int namestr
= GPIO_MAX
;
22 static char *pin_names
[GPIO_MAX
+1];
23 static uint_least8_t pin_names_width
;
25 static void pinnames_get(void)
27 static const FLASH
char delim1
[] = {":= "};
28 static const FLASH
char delim2
[] = {", "};
33 if (pin_names
[namestr
] != NULL
)
34 free(pin_names
[namestr
]);
35 memset(pin_names
, 0, sizeof(pin_names
));
38 /* TODO: enters endless loop on wrong parameters */
40 if ((lp
= getenv_char(PSTR(ENV_PINALIAS
))) != NULL
) {
41 pin_names
[namestr
] = strdup(lp
);
42 ptr
= strtok_P(pin_names
[namestr
], delim1
);
44 if (((i
= strtoul(ptr
, &lp
, 10)) < GPIO_MAX
) &&
46 (ptr
= strtok_P(NULL
, delim2
)) != NULL
) {
48 ptr
= strtok_P(NULL
, delim1
);
52 for (i
= 0; i
< GPIO_MAX
; i
++)
53 if (strlen(pin_names
[i
]) > pin_names_width
)
54 pin_names_width
= strlen(pin_names
[i
]);
59 static size_t xstrlen(char *s
)
67 static const FLASH
char * const FLASH pinconf_str
[] = {
75 static const FLASH
char * const FLASH pinlevel_str
[] = {
81 static int print_pin(int pin
, int multi
)
84 const FLASH
char *levelp
;
87 pinconf
= gpio_config_get(pin
);
88 if (pinconf
== OUTPUT_TIMER
) {
89 div
= gpio_clockdiv_get(pin
);
90 levelp
= pinlevel_str
[2];
92 levelp
= pinlevel_str
[gpio_read(pin
)];
95 printf_P(PSTR("%3d "), pin
);
96 if (pin_names_width
) {
97 printf_P(PSTR("%s "), pin_names
[pin
]);
98 print_blanks(pin_names_width
- xstrlen(pin_names
[pin
]));
100 my_puts_P(pinconf_str
[pinconf
]);
101 print_blanks(7 - strlen_P(pinconf_str
[pinconf
]));
103 print_blanks(5 - strlen_P(levelp
));
104 if (pinconf
== OUTPUT_TIMER
)
105 printf_P(PSTR("%8ld %8ld"),
108 printf_P(PSTR("%d: \"%s\", "), pin
, pin_names
[pin
] ? pin_names
[pin
] : 0);
109 my_puts_P(pinconf_str
[pinconf
]);
110 printf_P(PSTR(", "));
113 if (pinconf
== OUTPUT_TIMER
)
114 printf_P(PSTR("divide by %ld (%ldHz)"),
117 printf_P(PSTR("\n"));
122 static int_fast8_t pinarg_insert(int pin
, uint_fast8_t count
, uint_fast8_t pinarg
[])
126 if (pin
< 0 || pin
>= GPIO_MAX
)
129 for (pos
= 0; pos
< count
; pos
++) {
130 if (pin
== pinarg
[pos
])
132 if (pin
< pinarg
[pos
])
135 for (uint_fast8_t i
= count
-1; i
== pos
; i
--)
136 pinarg
[i
+1] = pinarg
[i
];
142 static uint_fast8_t pinarg_get(char * arg
, uint_fast8_t pinarg
[])
147 uint_fast8_t count
= 0;
150 pin1
= strtoul(arg
, &endp
, 10);
151 if (endp
!= arg
&& *endp
== '-') {
153 uint_fast8_t pin2
= (int) strtoul(arg
, &endp
, 10);
155 for (; pin1
< pin2
; pin1
++)
156 if ((rc
= pinarg_insert(pin1
, count
, pinarg
)) >= 0)
164 if ((*endp
== ',' || *endp
== '\0') &&
165 (rc
= pinarg_insert(pin1
, count
, pinarg
)) >= 0) {
179 command_ret_t
do_gpio(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *const argv
[])
181 char printheader
= 1;
182 uint_fast8_t pinarg
[GPIO_MAX
];
183 uint_fast8_t pinargc
;
185 (void) cmdtp
; (void) flag
;
191 while ((opt
= getopt(argc
, argv
, PSTR("s"))) != -1) {
197 return CMD_RET_USAGE
;
201 /* remaining arguments */
207 /* print cofig of all pins */
208 for (pinargc
= 0; pinargc
< GPIO_MAX
; pinargc
++)
209 pinarg
[pinargc
] = pinargc
;
212 pinargc
= pinarg_get(argv
[optind
++], pinarg
);
214 return CMD_RET_USAGE
;
220 /* no more args, print config */
222 print_pin(pinarg
[0], 0);
225 if (pin_names_width
> 0) {
226 if ( strlen("Name") > pin_names_width
)
227 pin_names_width
= strlen("Name");
228 char s
[pin_names_width
+1];
229 memset(s
, ' ', pin_names_width
);
230 s
[pin_names_width
] = '\0';
231 strncpy_P(s
, PSTR("Name"), 4);
232 printf_P(PSTR("Pin %s Config Level Divider Frequency/Hz\n"),s
);
233 memset(s
, '-', pin_names_width
);
234 printf_P(PSTR("----%s-----------------------------------\n"), s
);
236 printf_P(PSTR("Pin Config Level Divider Frequency/Hz\n"
237 "--------------------------------------\n"));
239 for (uint_fast8_t i
= 0; i
< pinargc
; i
++)
240 print_pin(pinarg
[i
], 1);
242 return CMD_RET_SUCCESS
;
245 /* arguments must be in pairs: pins conf */
247 return CMD_RET_USAGE
;
251 gpiomode_t mode
= NONE
;
253 unsigned long value
= 0;
256 switch (toupper(argv
[optind
][0])) {
271 value
= strtoul(argv
[optind
], &endp
, 10);
280 if (*endp
&& strcmp_P(endp
, PSTR("Hz")) == 0) {
286 printf_P(PSTR("invalid parameter: '%s'\n"), argv
[optind
]);
287 return CMD_RET_USAGE
;
291 printf_P(PSTR("invalid value: %lu \n"));
292 return CMD_RET_USAGE
;
296 if (value
> F_CPU
/ 2) {
297 printf_P(PSTR("Max frequency is: %luHz\n"), F_CPU
/2);
298 return CMD_RET_USAGE
;
307 return CMD_RET_USAGE
;
309 for (uint_fast8_t i
= 0; i
< pinargc
; i
++) {
312 gpio_write(pinarg
[i
], level
);
316 gpio_config(pinarg
[i
], mode
);
319 if (gpio_clockdiv_set(pinarg
[i
], value
) < 0) {
320 printf_P(PSTR("Setting pin %d to %lu failed.\n"),
330 pinargc
= pinarg_get(argv
[optind
++], pinarg
);
334 return CMD_RET_SUCCESS
;