]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_gpio.c
549654fdf1c6e24da59512d87bff73ab2d71ba11
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
;
177 while ((opt
= getopt(argc
, argv
, PSTR("s"))) != -1) {
183 return CMD_RET_USAGE
;
187 /* remaining arguments */
191 /* print config of all pins */
192 for (pinargc
= 0; pinargc
< GPIO_MAX
; pinargc
++)
193 pinarg
[pinargc
] = pinargc
;
196 pinargc
= pinarg_get(argv
[optind
++], pinarg
);
198 return CMD_RET_USAGE
;
205 /* no more args, print pin config */
207 char *pin_names
[GPIO_MAX
+ 1];
208 uint_fast8_t pin_names_width
= pinnames_get(pin_names
);
211 print_pin(pinarg
[0], 0, pin_names
, pin_names_width
);
214 if (pin_names_width
> 0) {
215 if ( strlen("Name") > pin_names_width
)
216 pin_names_width
= strlen("Name");
217 char s
[pin_names_width
+1];
218 memset(s
, ' ', pin_names_width
);
219 s
[pin_names_width
] = '\0';
220 strncpy_P(s
, PSTR("Name"), 4);
221 printf_P(PSTR("Pin %s Config Level Divider Frequency/Hz\n"),s
);
222 memset(s
, '-', pin_names_width
);
223 printf_P(PSTR("----%s-----------------------------------\n"), s
);
225 printf_P(PSTR("Pin Config Level Divider Frequency/Hz\n"
226 "--------------------------------------\n"));
228 for (uint_fast8_t i
= 0; i
< pinargc
; i
++)
229 print_pin(pinarg
[i
], 1, pin_names
, pin_names_width
);
231 if (pin_names
[GPIO_MAX
] != NULL
)
232 free(pin_names
[GPIO_MAX
]);
233 return CMD_RET_SUCCESS
;
236 /* arguments must be in pairs: pins conf */
238 return CMD_RET_USAGE
;
242 gpiomode_t mode
= NONE
;
244 unsigned long value
= 0;
247 switch (toupper(argv
[optind
][0])) {
262 value
= strtoul(argv
[optind
], &endp
, 10);
271 if (*endp
&& strcmp_P(endp
, PSTR("Hz")) == 0) {
277 printf_P(PSTR("invalid parameter: '%s'\n"), argv
[optind
]);
278 return CMD_RET_USAGE
;
282 printf_P(PSTR("invalid value: %lu \n"));
283 return CMD_RET_USAGE
;
287 if (value
> F_CPU
/ 2) {
288 printf_P(PSTR("Max frequency is: %luHz\n"), F_CPU
/2);
289 return CMD_RET_USAGE
;
298 return CMD_RET_USAGE
;
300 for (uint_fast8_t i
= 0; i
< pinargc
; i
++) {
303 gpio_write(pinarg
[i
], level
);
307 gpio_config(pinarg
[i
], mode
);
310 if (gpio_clockdiv_set(pinarg
[i
], value
) < 0) {
311 printf_P(PSTR("Setting pin %d to %lu failed.\n"),
321 pinargc
= pinarg_get(argv
[optind
++], pinarg
);
325 return CMD_RET_SUCCESS
;