r/lambdachip • • Nov 05 '21

How get gpio input?

Gpio function are below:

(gpio-set! gpio-name value)

(device-configure! gpio-name)

(gpio-toggle! gpio-name)

So, how do I input gpio values?

2 Upvotes

4 comments sorted by

1

u/Rafael_Lee Nov 08 '21

This is a missing API. I will add it soon.

1

u/Rafael_Lee Nov 09 '21

gpio-get! API is added.

(display "(gpio-get! 'dev_led2) = ")
(display (gpio-get! 'dev_led2))
(newline)

laco (https://www.gitlab.com/lambdachip/laco.git) e663ecc - (87 minutes ago) Add primitive gpio-get! - Rafael Lee (HEAD -> dev, origin/dev)

lambdachip (https://www.gitlab.com/lambdachip/lambdachip.git) b49ef4d - (78 minutes ago) Add primitive gpio-get! - Rafael Lee (HEAD -> dev, origin/dev)

Actually, user doesn't need to configure GPIO pins which used as GPIO as default in scheme.For other pins (default function are I2C or SPI, they need to configure using device-configure!)

// lambdachip/primitives.c
static object_t _os_device_configure (vm_t vm, object_t ret, object_t obj)
{
  VALIDATE (obj, symbol);

  *ret = GLOBAL_REF (none_const);
  // const char *str_buf = GET_SYBOL ((u32_t)obj->value);
  super_device *p = translate_supper_dev_from_symbol (obj);

  // FIXME: flags for different pin shall be different
  if (SUPERDEVICE_TYPE_GPIO_PIN == p->type)
    {
      gpio_pin_configure (p->dev, p->gpio_pin, GPIO_OUTPUT_ACTIVE | LED0_FLAGS);
    }
  else if (SUPERDEVICE_TYPE_I2C == p->type)
    {
      i2c_configure (p->dev, 0);
    }
  else if (SUPERDEVICE_TYPE_SPI == p->type)
    {
    }
  else
    {
      PANIC ("device type not defined, cannot handle");
    }

  return ret;
}

Pre configured pins are list here

// lambdachip-zephyr/src/main.c
  /* only non default devices need to be configured */
  /* Do not configure multi function GPIO pins */
  /* Set LED pin as output */
  /* I2C and SPI are configured automatically when system boots */
  assert (gpio_pin_configure (GLOBAL_REF (dev_led0), LED0_PIN,
                              GPIO_OUTPUT_ACTIVE | LED0_FLAGS)
          >= 0);
  assert (gpio_pin_configure (GLOBAL_REF (dev_led1), LED1_PIN,
                              GPIO_OUTPUT_ACTIVE | LED1_FLAGS)
          >= 0);
  assert (gpio_pin_configure (GLOBAL_REF (dev_led2), LED2_PIN,
                              GPIO_OUTPUT_ACTIVE | LED2_FLAGS)
          >= 0);
  assert (gpio_pin_configure (GLOBAL_REF (dev_led3), LED3_PIN,
                              GPIO_OUTPUT_ACTIVE | LED3_FLAGS)
          >= 0);

  assert (gpio_pin_configure (GLOBAL_REF (dev_gpio_pb6), GPIO_PB6_PIN,
                              GPIO_OUTPUT_ACTIVE | GPIO_PB6_FLAGS)
          >= 0);

  assert (gpio_pin_configure (GLOBAL_REF (dev_gpio_pb7), GPIO_PB7_PIN,
                              GPIO_OUTPUT_ACTIVE | GPIO_PB7_FLAGS)
          >= 0);

  gpio_pin_set (GLOBAL_REF (dev_led0), LED0_PIN, 0);
  gpio_pin_set (GLOBAL_REF (dev_led1), LED1_PIN, 0);
  gpio_pin_set (GLOBAL_REF (dev_led2), LED2_PIN, 0);
  gpio_pin_set (GLOBAL_REF (dev_led3), LED3_PIN, 0);

I'm not quite sure about Zephyr default behavior of configure GPIO,I2C and SPI devices. The device configuring of Zephyr has changed a lot from v2.4.0 to v2.7.0. I'll figure it out soon.

2

u/nalaginrut Nov 09 '21

BTW, this API doesn't imply side-effect, so "!" should be dropped when you release it.

1

u/Rafael_Lee Nov 11 '21

Changed to

gpio-get

Checked Zephyr code, GPIOA to GPIOG are configured along with SPI, UART, I2C when Zephyr initialize, GPIO pins need to be configured manually. Configuration is done in lambdachip-zephyr/src/main.c.