diff options
author | Richard Braun <rbraun@sceen.net> | 2018-02-27 23:59:27 +0100 |
---|---|---|
committer | Richard Braun <rbraun@sceen.net> | 2018-02-27 23:59:27 +0100 |
commit | 11eb99aad4363c8dc548991a255364b42ab542ce (patch) | |
tree | 3996840db2f0caf39c5733d65d901f215fec0e9c | |
parent | 46f181c702a4833816ce05f636eb32fca1aa310a (diff) |
led: new module
This module controls the blinking of the user LED.
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | src/gpio.c | 39 | ||||
-rw-r--r-- | src/gpio.h | 3 | ||||
-rw-r--r-- | src/led.c | 100 | ||||
-rw-r--r-- | src/led.h | 28 | ||||
-rw-r--r-- | src/main.c | 2 |
6 files changed, 168 insertions, 5 deletions
@@ -181,6 +181,7 @@ SOURCES = \ src/cpu_asm.S \ src/flash.c \ src/gpio.c \ + src/led.c \ src/main.c \ src/mem.c \ src/mutex.c \ @@ -20,6 +20,7 @@ * DEALINGS IN THE SOFTWARE. */ +#include <stdbool.h> #include <stdint.h> #include "gpio.h" @@ -50,14 +51,14 @@ gpio_compute_location(unsigned int io, unsigned int nr_bits, } static void -gpio_set(volatile struct gpio_regs *regs, unsigned int io, - uint32_t af, uint32_t speed, uint32_t pupd) +gpio_set_af(volatile struct gpio_regs *regs, unsigned int io, + uint32_t af, uint32_t speed, uint32_t pupd) { uint32_t shift, mask, value; volatile uint32_t *reg; gpio_compute_location(io, 2, &shift, &mask); - value = (af == 0) ? 1 : 2; + value = (af == 15) ? 1 : 2; regs->moder &= ~mask; regs->moder |= value << shift; @@ -81,9 +82,37 @@ gpio_set(volatile struct gpio_regs *regs, unsigned int io, *reg |= af << shift; } +static void +gpio_set_output(volatile struct gpio_regs *regs, unsigned int io, bool high) +{ + uint32_t shift, mask; + + gpio_set_af(gpio_c_regs, io, 15, 0, 0); + + gpio_compute_location(io, 1, &shift, &mask); + + if (high) { + regs->odr |= high << shift; + } else { + regs->odr &= ~mask; + } +} + void gpio_setup(void) { - gpio_set(gpio_c_regs, 6, 8, 1, 1); - gpio_set(gpio_c_regs, 7, 8, 1, 1); + gpio_set_af(gpio_c_regs, 6, 8, 1, 1); /* UART6 TX */ + gpio_set_af(gpio_c_regs, 7, 8, 1, 1); /* UART6 RX */ +} + +void +gpio_led_on(void) +{ + gpio_set_output(gpio_c_regs, 13, true); +} + +void +gpio_led_off(void) +{ + gpio_set_output(gpio_c_regs, 13, false); } @@ -28,4 +28,7 @@ */ void gpio_setup(void); +void gpio_led_on(void); +void gpio_led_off(void); + #endif /* _GPIO_H */ diff --git a/src/led.c b/src/led.c new file mode 100644 index 0000000..3a49f77 --- /dev/null +++ b/src/led.c @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2018 Richard Braun. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include <stdbool.h> +#include <stddef.h> +#include <stdio.h> +#include <string.h> + +#include <lib/shell.h> + +#include "gpio.h" +#include "led.h" +#include "thread.h" +#include "timer.h" + +#define LED_BLINK_INTERVAL THREAD_SCHED_FREQ + +static struct timer led_timer; +static bool led_on; +static volatile bool led_blinking_enabled; + +static void +led_shell_blink(int argc, char **argv) +{ + bool enabled; + + if (argc != 2) { + goto error; + } + + if (strcmp(argv[1], "on") == 0) { + enabled = true; + } else if (strcmp(argv[1], "off") == 0) { + enabled = false; + } else { + goto error; + } + + led_blinking_enabled = enabled; + + return; + +error: + printf("led: error: invalid arguments\n"); +} + +static struct shell_cmd led_shell_cmds[] = { + SHELL_CMD_INITIALIZER("led_blink", led_shell_blink, + "led_blink <on|off>", + "control led blinking"), +}; + +static void +led_toggle(void *arg) +{ + (void)arg; + + if (led_blinking_enabled) { + if (led_on) { + gpio_led_off(); + } else { + gpio_led_on(); + } + } + + led_on = !led_on; + timer_schedule(&led_timer, timer_get_time(&led_timer) + LED_BLINK_INTERVAL); +} + +void +led_setup(void) +{ + gpio_led_off(); + led_on = false; + led_blinking_enabled = true; + + SHELL_REGISTER_CMDS(led_shell_cmds); + + timer_init(&led_timer, led_toggle, NULL); + timer_schedule(&led_timer, timer_now() + LED_BLINK_INTERVAL); +} diff --git a/src/led.h b/src/led.h new file mode 100644 index 0000000..e25efb4 --- /dev/null +++ b/src/led.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2018 Richard Braun. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#ifndef _LED_H +#define _LED_H + +void led_setup(void); + +#endif /* _LED_H */ @@ -26,6 +26,7 @@ #include <lib/shell.h> #include "cpu.h" +#include "led.h" #include "main.h" #include "mem.h" #include "panic.h" @@ -51,6 +52,7 @@ main(void) thread_setup(); timer_setup(); shell_setup(); + led_setup(); sw_setup(); printf("X1 " QUOTE(VERSION) "\n\n"); |