peripheral/gpio_irq/main.c

/*******************************************************************************
* # License
* Copyright 2019 Silicon Laboratories Inc. www.silabs.com
*******************************************************************************
*
* The licensor of this software is Silicon Laboratories Inc. Your use of this
* software is governed by the terms of Silicon Labs Master Software License
* Agreement (MSLA) available at
* www.silabs.com/about-us/legal/master-software-license-agreement. This
* software is distributed to you in Source Code format and is governed by the
* sections of the MSLA applicable to Source Code.
*
******************************************************************************/
/*
* Documentation for this app is available online.
* See https://docs.silabs.com/gecko-os/4/standard/latest/sdk/examples/basic/gpio
*/
#include "gos.h"
#define APPLICATION_START_LINE "\r\n\r\nGPIO IRQ Example app starting..."
/*************************************************************************************************/
void gos_app_init(void)
{
GOS_LOG(APPLICATION_START_LINE);
#ifdef PLATFORM_LED2
GOS_LOG("Press Button1 to toggle LED1");
GOS_LOG("Press Button2 to toggle LED2");
#else
GOS_LOG("Press Button1 or Button2 to toggle LED1");
#endif
// NOTE: Also see the basic.button example app which uses the gos_button_XXX APIs
gos_gpio_init(PLATFORM_LED1, GOS_GPIO_OUTPUT_PUSH_PULL, !PLATFORM_LED_ACTIVE_STATE);
#ifdef PLATFORM_LED2
gos_gpio_init(PLATFORM_LED2, GOS_GPIO_OUTPUT_PUSH_PULL, !PLATFORM_LED_ACTIVE_STATE);
#endif
gos_gpio_irq_enable(PLATFORM_BUTTON1, GOS_GPIO_TRIGGER_BOTH_EDGES, gpio_irq_callback, (void*)PLATFORM_BUTTON1);
gos_gpio_irq_enable(PLATFORM_BUTTON2, GOS_GPIO_TRIGGER_BOTH_EDGES, gpio_irq_callback, (void*)PLATFORM_BUTTON2);
}
/*************************************************************************************************
* Executes inside the GPIO hardware IRQ
*/
static void gpio_irq_callback(void *arg)
{
const gos_gpio_t button_gpio = (gos_gpio_t)arg;
#ifdef PLATFORM_LED2
const gos_gpio_t led_gpio = (button_gpio == PLATFORM_BUTTON1) ? PLATFORM_LED1 : PLATFORM_LED2;
#else
const gos_gpio_t led_gpio = PLATFORM_LED1;
#endif
/*
* NOTE: MUST use the Native API due to execution in the GPIO IRQ, Command APIs are not allowed in IRQs
*/
const bool value = gos_gpio_get(button_gpio);
gos_gpio_set(led_gpio, (value == PLATFORM_BUTTON_ACTIVE_STATE) ? PLATFORM_LED_ACTIVE_STATE : !PLATFORM_LED_ACTIVE_STATE);
}