From c66410f6319284b94e21758539ac25bfb4694d3a Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Tue, 2 Oct 2018 21:26:48 -0400 Subject: folder restructure --- src/user/Makefile | 22 ++++++++++++++++++++ src/user/priv_gpio.h | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/user/user.c | 35 +++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 src/user/Makefile create mode 100644 src/user/priv_gpio.h create mode 100644 src/user/user.c (limited to 'src/user') diff --git a/src/user/Makefile b/src/user/Makefile new file mode 100644 index 0000000..d22fbf3 --- /dev/null +++ b/src/user/Makefile @@ -0,0 +1,22 @@ +CFILES = $(wildcard *.c) +AFILES = $(wildcard *.s) +OFILES = $(patsubst %.c, %.o, $(CFILES)) \ + $(patsubst %.s, %.asm.o, $(AFILES)) + +CFLAGS += -I.. -I../arch/cmsis + +all: $(OFILES) + +%.o: %.c + @echo " CC " $< + @$(CROSS)$(CC) $(CFLAGS) -c $< -o $@ + +%.asm.o: %.s + @echo " AS " $< + @$(CROSS)$(AS) $(AFLAGS) -c $< -o $@ + +clean: + @echo " CLEAN" + @rm -f $(OFILES) + + diff --git a/src/user/priv_gpio.h b/src/user/priv_gpio.h new file mode 100644 index 0000000..6b349fe --- /dev/null +++ b/src/user/priv_gpio.h @@ -0,0 +1,58 @@ +/** + * @file priv_gpio.h + * GPIO access for unpriviledged processes + * + * Copyright (C) 2018 Clyne Sullivan + * + * 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 . + */ + +#ifndef PRIV_GPIO_H_ +#define PRIV_GPIO_H_ + +// For value flags +#include + +/** + * Possible GPIO calls. + */ +enum GPIO_CALLS { + GPIO_MODE = 0, /**< Change GPIO mode */ + GPIO_TYPE, /**< Change GPIO type (PuPd/ODrain) */ + GPIO_PUPD, /**< Set/clear pullup/pulldown */ + GPIO_SPEED, /**< Set GPIO speed */ + GPIO_OUT, /**< Set GPIO output state */ +}; + +/** + * Provides unpriviledged GPIO access. + * @param call The type of GPIO call to make + * @param pin The pin to modify (0-15 = A, 16-31 = B, ...) + * @param value The value to pass to the call + */ +void gpio(uint32_t call, uint32_t pin, uint32_t value) +{ + register uint32_t r0 asm("r0") = call; + register uint32_t r1 asm("r1") = pin; + register uint32_t r2 asm("r2") = value; + + asm("\ + mov r0, %0; \ + mov r1, %1; \ + mov r2, %2; \ + svc 1; \ + " :: "r" (r0), "r" (r1), "r" (r2)); +} + +#endif // PRIV_GPIO_H_ diff --git a/src/user/user.c b/src/user/user.c new file mode 100644 index 0000000..fc51d29 --- /dev/null +++ b/src/user/user.c @@ -0,0 +1,35 @@ +#include +#include "priv_gpio.h" +#include + +void task1(void); +void task2(void); + + + +void user_main(void) +{ + gpio(GPIO_MODE, 5, OUTPUT); + task_start(task1, 512); + + for (int i = 0; i < 8; i++) { + gpio(GPIO_OUT, 5, !(i & 1)); + delay(200); + } +} + +void task1(void) +{ + delay(400); + task_start(task2, 1024); +} + +void task2(void) +{ + int state = 0; + delay(2500); + while (1) { + gpio(GPIO_OUT, 5, state ^= 1); + delay(500); + } +} -- cgit v1.2.3