]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_gpio.c
7 #include "print-utils.h"
8 #include "getopt-min.h"
15 static const int namestr
= GPIO_MAX
;
16 static char *pin_names
[GPIO_MAX
+1];
17 static uint_least8_t pin_names_width
;
19 static void pinnames_get(void)
21 static const FLASH
char delim1
[] = {":= "};
22 static const FLASH
char delim2
[] = {", "};
27 if (pin_names
[namestr
] != NULL
)
28 free(pin_names
[namestr
]);
29 memset(pin_names
, 0, sizeof(pin_names
));
32 /* TODO: enters endless loop on wron parameters */
34 if ((lp
= getenv(PSTR(ENV_PINALIAS
))) != NULL
) {
35 pin_names
[namestr
] = strdup(lp
);
36 ptr
= strtok_P(pin_names
[namestr
], delim1
);
38 if (((i
= strtoul(ptr
, &lp
, 10)) < GPIO_MAX
) &&
40 (ptr
= strtok_P(NULL
, delim2
)) != NULL
) {
42 ptr
= strtok_P(NULL
, delim1
);
46 for (i
= 0; i
< GPIO_MAX
; i
++)
47 if (strlen(pin_names
[i
]) > pin_names_width
)
48 pin_names_width
= strlen(pin_names
[i
]);
53 static size_t xstrlen(char *s
)
61 static const FLASH
char * const FLASH pinconf_str
[] = {
69 static const FLASH
char * const FLASH pinlevel_str
[] = {
75 static int print_pin(int pin
, int multi
)
78 const FLASH
char *levelp
;
81 pinconf
= gpio_config_get(pin
);
82 if (pinconf
== OUTPUT_TIMER
) {
83 div
= gpio_clockdiv_get(pin
);
84 levelp
= pinlevel_str
[2];
86 levelp
= pinlevel_str
[gpio_read(pin
)];
89 printf_P(PSTR("%3d "), pin
);
90 if (pin_names_width
) {
91 printf_P(PSTR("%s "), pin_names
[pin
]);
92 print_blanks(pin_names_width
- xstrlen(pin_names
[pin
]));
94 my_puts_P(pinconf_str
[pinconf
]);
95 print_blanks(7 - strlen_P(pinconf_str
[pinconf
]));
97 print_blanks(5 - strlen_P(levelp
));
98 if (pinconf
== OUTPUT_TIMER
)
99 printf_P(PSTR("%8ld %8ld"),
102 printf_P(PSTR("%d: \"%s\", "), pin
, pin_names
[pin
] ? pin_names
[pin
] : 0);
103 my_puts_P(pinconf_str
[pinconf
]);
104 printf_P(PSTR(", "));
107 if (pinconf
== OUTPUT_TIMER
)
108 printf_P(PSTR("divide by %ld (%ldHz)"),
111 printf_P(PSTR("\n"));
116 static int_fast8_t pinarg_insert(int pin
, uint_fast8_t count
, uint_fast8_t pinarg
[])
120 if (pin
< 0 || pin
>= GPIO_MAX
)
123 for (pos
= 0; pos
< count
; pos
++) {
124 if (pin
== pinarg
[pos
])
126 if (pin
< pinarg
[pos
])
129 for (uint_fast8_t i
= count
-1; i
== pos
; i
--)
130 pinarg
[i
+1] = pinarg
[i
];
136 static uint_fast8_t pinarg_get(char * arg
, uint_fast8_t pinarg
[])
141 uint_fast8_t count
= 0;
144 pin1
= strtoul(arg
, &endp
, 10);
145 if (endp
!= arg
&& *endp
== '-') {
147 uint_fast8_t pin2
= (int) strtoul(arg
, &endp
, 10);
149 for (; pin1
< pin2
; pin1
++)
150 if ((rc
= pinarg_insert(pin1
, count
, pinarg
)) >= 0)
158 if ((*endp
== ',' || *endp
== '\0') &&
159 (rc
= pinarg_insert(pin1
, count
, pinarg
)) >= 0) {
173 command_ret_t
do_gpio(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *const argv
[])
175 char printheader
= 1;
176 uint_fast8_t pinarg
[GPIO_MAX
];
177 uint_fast8_t pinargc
;
179 (void) cmdtp
; (void) flag
;
185 while ((opt
= getopt(argc
, argv
, PSTR("s"))) != -1) {
191 return CMD_RET_USAGE
;
195 /* remaining arguments */
201 /* print cofig of all pins */
202 for (pinargc
= 0; pinargc
< GPIO_MAX
; pinargc
++)
203 pinarg
[pinargc
] = pinargc
;
206 pinargc
= pinarg_get(argv
[optind
++], pinarg
);
208 return CMD_RET_USAGE
;
214 /* no more args, print config */
216 print_pin(pinarg
[0], 0);
219 if (pin_names_width
> 0) {
220 if ( strlen("Name") > pin_names_width
)
221 pin_names_width
= strlen("Name");
222 char s
[pin_names_width
+1];
223 memset(s
, ' ', pin_names_width
);
224 s
[pin_names_width
] = '\0';
225 strncpy_P(s
, PSTR("Name"), 4);
226 printf_P(PSTR("Pin %s Config Level Divider Frequency/Hz\n"),s
);
227 memset(s
, '-', pin_names_width
);
228 printf_P(PSTR("----%s-----------------------------------\n"), s
);
230 printf_P(PSTR("Pin Config Level Divider Frequency/Hz\n"
231 "--------------------------------------\n"));
233 for (uint_fast8_t i
= 0; i
< pinargc
; i
++)
234 print_pin(pinarg
[i
], 1);
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
;