@@ -355,6 +355,159 @@ bool atci_adc_v(twr_atci_param_t *param)
355355 return true;
356356}
357357
358+ static bool get_gpio_channel (twr_atci_param_t * param , twr_gpio_channel_t * channel )
359+ {
360+ uint32_t ch ;
361+
362+ if (!twr_atci_get_uint (param , & ch ))
363+ {
364+ return false;
365+ }
366+
367+ if (ch > 20 )
368+ {
369+ return false;
370+ }
371+
372+ * channel = (twr_gpio_channel_t )ch ;
373+
374+ return true;
375+ }
376+
377+ bool atci_gpio_set (twr_atci_param_t * param )
378+ {
379+ twr_gpio_channel_t channel ;
380+ uint32_t value ;
381+
382+ if (!get_gpio_channel (param , & channel ))
383+ {
384+ twr_log_error ("Invalid parse channel number" );
385+ return false;
386+ }
387+
388+ if (!twr_atci_is_comma (param ))
389+ {
390+ twr_log_error ("Invalid parse comma" );
391+ return false;
392+ }
393+
394+ if (!twr_atci_get_uint (param , & value ))
395+ {
396+ twr_log_error ("Invalid parse value" );
397+ return false;
398+ }
399+
400+ twr_gpio_set_pull (channel , TWR_GPIO_PULL_NONE );
401+ twr_gpio_set_mode (channel , TWR_GPIO_MODE_OUTPUT );
402+
403+ twr_gpio_set_output (channel , value );
404+
405+ return true;
406+ }
407+
408+ bool atci_gpio_get (twr_atci_param_t * param )
409+ {
410+ twr_gpio_channel_t channel ;
411+ uint32_t value ;
412+
413+ if (!get_gpio_channel (param , & channel ))
414+ {
415+ twr_log_error ("Invalid parse channel number" );
416+ return false;
417+ }
418+
419+ twr_gpio_set_mode (channel , TWR_GPIO_MODE_INPUT );
420+
421+ value = twr_gpio_get_input (channel );
422+
423+ twr_atci_printfln ("$GPIOGET: %d" , value );
424+
425+ return true;
426+ }
427+
428+ bool atci_gpio_config (twr_atci_param_t * param )
429+ {
430+ twr_gpio_channel_t channel ;
431+ char str_mode [10 ];
432+ char str_pull [10 ];
433+
434+ twr_gpio_mode_t mode = TWR_GPIO_MODE_INPUT ;
435+ twr_gpio_pull_t pull = TWR_GPIO_PULL_NONE ;
436+
437+ if (!get_gpio_channel (param , & channel ))
438+ {
439+ twr_log_error ("Invalid parse channel number" );
440+ return false;
441+ }
442+
443+ if (!twr_atci_is_comma (param ))
444+ {
445+ twr_log_error ("Invalid parse comma" );
446+ return false;
447+ }
448+
449+ if (!twr_atci_get_string (param , str_mode , sizeof (str_mode )))
450+ {
451+ twr_log_error ("Invalid parse mode" );
452+ return false;
453+ }
454+
455+ if (strcmp (str_mode , "input" ) == 0 )
456+ {
457+ mode = TWR_GPIO_MODE_INPUT ;
458+ }
459+ else if (strcmp (str_mode , "output" ) == 0 )
460+ {
461+ mode = TWR_GPIO_MODE_OUTPUT ;
462+ }
463+ else if (strcmp (str_mode , "analog" ) == 0 )
464+ {
465+ mode = TWR_GPIO_MODE_ANALOG ;
466+ }
467+ else
468+ {
469+ twr_log_error ("Invalid mode" );
470+ return false;
471+ }
472+
473+ if (mode != TWR_GPIO_MODE_OUTPUT )
474+ {
475+ if (!twr_atci_is_comma (param ))
476+ {
477+ twr_log_error ("Invalid parse comma" );
478+ return false;
479+ }
480+ if (!twr_atci_get_string (param , str_pull , sizeof (str_pull )))
481+ {
482+ twr_log_error ("Invalid parse pull" );
483+ return false;
484+ }
485+
486+ if (strcmp (str_pull , "none" ) == 0 )
487+ {
488+ pull = TWR_GPIO_PULL_NONE ;
489+ }
490+ else if (strcmp (str_pull , "up" ) == 0 )
491+ {
492+ pull = TWR_GPIO_PULL_UP ;
493+ }
494+ else if (strcmp (str_pull , "down" ) == 0 )
495+ {
496+ pull = TWR_GPIO_PULL_DOWN ;
497+ }
498+ else
499+ {
500+ twr_log_error ("Invalid pull" );
501+ return false;
502+ }
503+ }
504+
505+ twr_gpio_set_mode (channel , mode );
506+ twr_gpio_set_pull (channel , pull );
507+
508+ return true;
509+ }
510+
358511bool atci_relay (twr_atci_param_t * param )
359512{
360513 uint32_t relay ;
@@ -460,11 +613,14 @@ void application_init(void)
460613 static twr_atci_command_t commands [] = {
461614 {"I" , at_i , NULL , NULL , NULL , "Request product information" },
462615 TWR_ATCI_COMMAND_CLAC ,
616+ {"$ADC" , NULL , atci_adc , NULL , NULL , "Read ADC value" },
617+ {"$ADCV" , NULL , atci_adc_v , NULL , NULL , "Read ADC voltage" },
618+ {"$GPIOC" , NULL , atci_gpio_config , NULL , NULL , "Configure GPIO pin" },
619+ {"$GPIOG" , NULL , atci_gpio_get , NULL , NULL , "Get GPIO pin value" },
620+ {"$GPIOS" , NULL , atci_gpio_set , NULL , NULL , "Set GPIO pin value" },
463621 {"$I2CMR" , NULL , atci_i2c_memory_read , NULL , NULL , "I2C memory read" },
464622 {"$I2CMW" , NULL , atci_i2c_memory_write , NULL , NULL , "I2C memory write" },
465623 {"$I2CSCAN" , NULL , atci_i2c_scan , NULL , NULL , "Scan I2C bus" },
466- {"$ADCV" , NULL , atci_adc_v , NULL , NULL , "Read ADC voltage" },
467- {"$ADC" , NULL , atci_adc , NULL , NULL , "Read ADC value" },
468624 {"$RELAY" , NULL , atci_relay , NULL , NULL , "Control relay" },
469625 {"$REBOOT" , at_reboot , NULL , NULL , NULL , "Reboot" },
470626 TWR_ATCI_COMMAND_HELP };
0 commit comments