@@ -644,10 +644,43 @@ REGISTER_PORT_DRIVER(gpio, gpio_driver_init, NULL, gpio_driver_create_port)
644
644
645
645
#ifdef CONFIG_AVM_ENABLE_GPIO_NIFS
646
646
647
+ static term nif_gpio_init (Context * ctx , int argc , term argv [])
648
+ {
649
+ UNUSED (argc );
650
+
651
+ gpio_num_t gpio_num ;
652
+ if (LIKELY (term_is_integer (argv [0 ]))) {
653
+ avm_int_t pin_int = term_to_int32 (argv [0 ]);
654
+ if (UNLIKELY ((pin_int < 0 ) || (pin_int >= GPIO_NUM_MAX ))) {
655
+ RAISE_ERROR (BADARG_ATOM );
656
+ }
657
+ gpio_num = (gpio_num_t ) pin_int ;
658
+ } else {
659
+ RAISE_ERROR (BADARG_ATOM );
660
+ }
661
+
662
+ gpio_config_t config = {};
663
+ config .pin_bit_mask = 1 << gpio_num ;
664
+ config .mode = GPIO_MODE_DISABLE ;
665
+ config .pull_up_en = GPIO_PULLUP_DISABLE ;
666
+ config .pull_down_en = GPIO_PULLDOWN_DISABLE ;
667
+ config .intr_type = GPIO_INTR_DISABLE ;
668
+
669
+ esp_err_t result = gpio_config (& config );
670
+
671
+ if (UNLIKELY (result != ESP_OK )) {
672
+ RAISE_ERROR (BADARG_ATOM );
673
+ }
674
+
675
+ return OK_ATOM ;
676
+ }
677
+
647
678
/* TODO: in the case of {error, Return} we should RAISE_ERROR(Reason) */
648
679
649
680
static term nif_gpio_set_pin_mode (Context * ctx , int argc , term argv [])
650
681
{
682
+ UNUSED (argc );
683
+
651
684
return gpio_set_pin_mode (ctx , argv [0 ], argv [1 ]);
652
685
}
653
686
@@ -706,6 +739,12 @@ static term nif_gpio_digital_read(Context *ctx, int argc, term argv[])
706
739
return gpio_digital_read (argv [0 ]);
707
740
}
708
741
742
+ static const struct Nif gpio_init_nif =
743
+ {
744
+ .base .type = NIFFunctionType ,
745
+ .nif_ptr = nif_gpio_init
746
+ };
747
+
709
748
static const struct Nif gpio_set_pin_mode_nif =
710
749
{
711
750
.base .type = NIFFunctionType ,
@@ -757,6 +796,15 @@ static const struct Nif gpio_digital_read_nif = {
757
796
758
797
const struct Nif * gpio_nif_get_nif (const char * nifname )
759
798
{
799
+ if (strcmp ("gpio:init/1" , nifname ) == 0 ) {
800
+ TRACE ("Resolved platform nif %s ...\n" , nifname );
801
+ return & gpio_init_nif ;
802
+ }
803
+ if (strcmp ("Elixir.GPIO:init/1" , nifname ) == 0 ) {
804
+ TRACE ("Resolved platform nif %s ...\n" , nifname );
805
+ return & gpio_init_nif ;
806
+ }
807
+
760
808
if (strcmp ("gpio:set_pin_mode/2" , nifname ) == 0 ) {
761
809
TRACE ("Resolved platform nif %s ...\n" , nifname );
762
810
return & gpio_set_pin_mode_nif ;
0 commit comments