Skip to content

Instantly share code, notes, and snippets.

@pabigot
Created June 11, 2020 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pabigot/f5e954d572d6ed958aec84a86bb944c4 to your computer and use it in GitHub Desktop.
Save pabigot/f5e954d572d6ed958aec84a86bb944c4 to your computer and use it in GitHub Desktop.
Sample with button and power management
/*
* Copyright (c) 2020 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#include <zephyr.h>
#include <device.h>
#include <drivers/gpio.h>
#include <string.h>
#include <stdio.h>
#include <devicetree.h>
#define SW0 DT_ALIAS(sw0)
#define LED0 DT_ALIAS(led0)
#define LED1 DT_ALIAS(led1)
static struct device *cons;
static struct device *led0;
static struct device *led1;
static struct device *sw0;
static struct gpio_callback sw0_data;
static const char *now_str(void)
{
static char buf[16]; /* ...HH:MM:SS.MMM */
uint32_t now = k_uptime_get_32();
unsigned int ms = now % MSEC_PER_SEC;
unsigned int s;
unsigned int min;
unsigned int h;
now /= MSEC_PER_SEC;
s = now % 60U;
now /= 60U;
min = now % 60U;
now /= 60U;
h = now;
snprintf(buf, sizeof(buf), "%u:%02u:%02u.%03u",
h, min, s, ms);
return buf;
}
void
button_pressed(struct device *dev, struct gpio_callback *cb,
uint32_t pins)
{
static uint32_t count;
++count;
gpio_pin_toggle(led1, DT_GPIO_PIN(LED1, gpios));
printk("%s button pressed %u\n", now_str(), count);
if ((count % 8) == 4) {
printk("Turning off console\n");
device_set_power_state(cons, DEVICE_PM_LOW_POWER_STATE, NULL, NULL);
} else if ((count % 8) == 0) {
device_set_power_state(cons, DEVICE_PM_ACTIVE_STATE, NULL, NULL);
printk("Turned on console\n");
}
}
void
main(void)
{
int ret;
cons = device_get_binding(DT_LABEL(DT_CHOSEN(zephyr_console)));
led0 = device_get_binding(DT_GPIO_LABEL(LED0, gpios));
led1 = device_get_binding(DT_GPIO_LABEL(LED1, gpios));
sw0 = device_get_binding(DT_GPIO_LABEL(SW0, gpios));
printk("Device started %p %p %p %p!\n", cons, led0, led1, sw0);
printk("Starting Button Interrupt!\n");
gpio_init_callback(&sw0_data, button_pressed,
BIT(DT_GPIO_PIN(SW0, gpios)));
ret = gpio_pin_configure(led0, DT_GPIO_PIN(LED0, gpios),
DT_GPIO_FLAGS(LED0, gpios)
| GPIO_OUTPUT_ACTIVE);
if (ret == 0) {
ret = gpio_pin_configure(led1, DT_GPIO_PIN(LED1, gpios),
DT_GPIO_FLAGS(LED1, gpios)
| GPIO_OUTPUT_INACTIVE);
}
if (ret == 0) {
ret = gpio_pin_configure(sw0, DT_GPIO_PIN(SW0, gpios),
DT_GPIO_FLAGS(SW0, gpios) | GPIO_INPUT);
}
if (ret == 0) {
ret = gpio_pin_interrupt_configure(sw0, DT_GPIO_PIN(SW0, gpios),
GPIO_INT_EDGE_TO_ACTIVE);
}
if (ret == 0) {
gpio_add_callback(sw0, &sw0_data);
}
printk("%s Setup: %d\n", now_str(), ret);
while (1) {
printk("%s looping\n", now_str());
k_sleep(K_SECONDS(1));
gpio_pin_toggle(led0, DT_GPIO_PIN(LED0, gpios));
}
}
CONFIG_SYS_POWER_MANAGEMENT=y
CONFIG_DEVICE_POWER_MANAGEMENT=y
CONFIG_GPIO=y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment