From 70ac046f98453ca8262ecc721b30f6c435ee33a5 Mon Sep 17 00:00:00 2001 From: chrysn Date: Thu, 1 Mar 2012 02:47:06 +0100 Subject: added efm32 lightswitch example --- .../efm32-tg-stk3300/lightswitch/Makefile | 22 ++++++ .../tinygecko/efm32-tg-stk3300/lightswitch/README | 9 +++ .../efm32-tg-stk3300/lightswitch/lightswitch.c | 88 ++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 examples/efm32/tinygecko/efm32-tg-stk3300/lightswitch/Makefile create mode 100644 examples/efm32/tinygecko/efm32-tg-stk3300/lightswitch/README create mode 100644 examples/efm32/tinygecko/efm32-tg-stk3300/lightswitch/lightswitch.c (limited to 'examples/efm32/tinygecko/efm32-tg-stk3300') diff --git a/examples/efm32/tinygecko/efm32-tg-stk3300/lightswitch/Makefile b/examples/efm32/tinygecko/efm32-tg-stk3300/lightswitch/Makefile new file mode 100644 index 0000000..c6e785a --- /dev/null +++ b/examples/efm32/tinygecko/efm32-tg-stk3300/lightswitch/Makefile @@ -0,0 +1,22 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2012 chrysn +## +## This program is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see . +## + +BINARY = lightswitch + +include ../Makefile.include diff --git a/examples/efm32/tinygecko/efm32-tg-stk3300/lightswitch/README b/examples/efm32/tinygecko/efm32-tg-stk3300/lightswitch/README new file mode 100644 index 0000000..4b6b333 --- /dev/null +++ b/examples/efm32/tinygecko/efm32-tg-stk3300/lightswitch/README @@ -0,0 +1,9 @@ +============================================ +EFM32-TG-STK3300 Examples lightswitch README +============================================ + +This is a small example program for GPIO input and output (and, in future, IRQ +handling). + +It's intended for the EFM32-TG-STK3300 eval board. It turn the User LED on when +PB0 is pressed, and off when PB1 is pressed. diff --git a/examples/efm32/tinygecko/efm32-tg-stk3300/lightswitch/lightswitch.c b/examples/efm32/tinygecko/efm32-tg-stk3300/lightswitch/lightswitch.c new file mode 100644 index 0000000..7b4e0ce --- /dev/null +++ b/examples/efm32/tinygecko/efm32-tg-stk3300/lightswitch/lightswitch.c @@ -0,0 +1,88 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2012 chrysn + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +void gpio_setup(void); +void led_on(void); +void led_off(void); +bool pb0_get(void); +bool pb1_get(void); + +/** @file + * Example for switching the User LED of the EFM32-TG-STK330 eval board on and + * off using the buttons. + */ + +int main(void) +{ + gpio_setup(); + + while(1) { + if (pb0_get()) led_on(); + if (pb1_get()) led_off(); + }; +} + +/** + * Enable GPIO, and set up port D7 as an output pin. + */ + +void gpio_setup(void) +{ + // Before GPIO works, according to d0034_efm32tg_reference_manual.pdf + // note in section 28.3.7, we'll have to enable GPIO in CMU_HFPERCLKEN0 + + CMU_HFPERCLKEN0 |= CMU_HFPERCLKEN0_GPIO; + + // The User LED is connected to PD7 to the plus side of the LED + // according to t0011_efm32_tiny_gecko_stk_user_manual.pdf figures 16.2 + // and 16.3 (called UIF_LED0) + + gpio_set_mode(GPIO_PD, GPIO_MODE_PUSHPULL, GPIO7); + + // Button PB0 is connected to pin PD8 and pulled low when pushed, + // Botton PB1 to pin PB11 (sources as for LED). Pullups and debouncing + // are alreay in place in hardware, so no filtering or pullup is + // needed. + + gpio_set_mode(GPIO_PD, GPIO_MODE_INPUT, GPIO8); + gpio_set_mode(GPIO_PB, GPIO_MODE_INPUT, GPIO11); +} + +void led_on(void) +{ + gpio_set(GPIO_PD, GPIO7); +} + +void led_off(void) +{ + gpio_clear(GPIO_PD, GPIO7); +} + +bool pb0_get(void) +{ + return !gpio_get(GPIO_PD, GPIO8); +} + +bool pb1_get(void) +{ + return !gpio_get(GPIO_PB, GPIO11); +} -- cgit v1.2.3