Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save protobits/81795f124f280fa48e885fabab1fcf04 to your computer and use it in GitHub Desktop.
Save protobits/81795f124f280fa48e885fabab1fcf04 to your computer and use it in GitHub Desktop.
static void initialize(FAR const struct sonar_lowerhalf_s* lower)
{
/* set pins as GPIOs */
lpc43_pin_config(PINCONF_PINS6 | PINCONF_PIN_1 | PINCONF_PULLDOWN | PINCONF_FUNC0); // GPIO0 (GPIO3[0]) -> trigger
lpc43_pin_config(PINCONF_PINS6 | PINCONF_PIN_5 | PINCONF_PULLDOWN | PINCONF_FUNC0); // GPIO2 (GPIO3[4]) -> echo
/* setup GPIOs */
lpc43_gpio_config(GPIO_PORT3 | GPIO_PIN0 | GPIO_MODE_OUTPUT); // GPIO0
lpc43_gpio_config(GPIO_PORT3 | GPIO_PIN4 | GPIO_MODE_PININTR | GPIO_INT_EDGE_BOTH | GPIO_PININT0); // GPIO2
/* set trigger to low */
lpc43_gpio_write(GPIO_PORT3 | GPIO_PIN0, false);
}
static void trigger(FAR const struct sonar_lowerhalf_s* lower)
{
/* send a 10uS pulse, as per datasheet */
lpc43_gpio_write(GPIO_PORT3 | GPIO_PIN0, true);
up_udelay(15);
lpc43_gpio_write(GPIO_PORT3 | GPIO_PIN0, false);
}
static void enable(FAR const struct sonar_lowerhalf_s* lower, sonar_interrupt_t handler, FAR void* arg)
{
if (handler)
{
printf("enabling IRQ\n");
interrupt_handler = handler;
interrupt_argument = arg;
int ret = irq_attach(LPC43M4_IRQ_PININT0, (xcpt_t)sonar_interrupt);
if (ret == OK) printf("OK\n");
up_enable_irq(LPC43M4_IRQ_PININT0);
/* TODO: enable pin interrupt here? maybe an IRQ is triggered here due to previous events */
}
else {
printf("disabling IRQ\n");
up_disable_irq(LPC43M4_IRQ_PININT0);
interrupt_handler = NULL;
}
}
static int sonar_interrupt(int irq, FAR void *context)
{
if (interrupt_handler)
interrupt_handler(&g_sonarlower, interrupt_argument);
return OK;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment