[PATCH] Beagle BSP Improvements (GPIO driver)
Ketul Shah
ketulshah1993 at gmail.com
Thu Apr 23 18:35:29 UTC 2015
Hi all,
Ben Gras thanks for your comments and encouragement for merging with
mainline as a motivation for me.
I worked and redeveloped code for gpio driver also tried to approach
towards common api. I tried to address most of the comments. Herewith I
attached my patch. Would be happy to hear comments on that.
You can also find out my gist
<https://gist.github.com/ketul93/d717555951174a74c8b4> or commit
<https://github.com/ketul93/rtems/commit/e2484b80d8620cc78bf5f453e5cd904c7137a569>
on https://github.com/RTEMS/rtems .
I have been updating my work on https://github.com/ketul93/RTEMS-on-BBB.
Live demo of the updated code is found on :- https://youtu.be/bXurelOA3i0
Thanks.
Regards,
ketul
On 20 April 2015 at 19:50, Ben Gras <beng at shrike-systems.com> wrote:
> All,
>
> Good contribution, thank you.
>
> For GSOC, this is good proof of being able to progress and get
> something real working based on documentation. Great!
>
> If you want this merged with mainline - which I fully encourage! -
> then I suggest the following should be added to/changed first:
>
> - make the code that uses AM335X (beaglebone) specific registers,
> AM335X-specific :) i.e. add #if IS_AM335X around code that should
> only execute there. this BSP can be compiled for 2 different SOCs.
> - let it control all GPIO's - there are 4 banks of 32 each on the
> AM335x, but you only let the user control GPIO1. there should be a
> clean interface to control them all (clean means mostly: without
> duplicating code)
> - as Gedare mentioned on IRC, copy the RPi API. this is a first
> (second?) step to finding a generalized GPIO API.
> - don't add beagleboard.h, but add your definitions to
> libcpu/arm/shared/include/am335x.h
> - the code should use a more consistent indenting style, and make
> variable names more descriptive than 'tmp'.
> - don't leave the configure changes in like in acinclude.m4
>
> bonus:
> - add DM3730 (beagleboard) support.
>
> Good luck!
>
>
> On Fri, Apr 17, 2015 at 7:51 PM, Ketul Shah <ketulshah1993 at gmail.com>
> wrote:
> > Hello,
> >
> > I have proposed in GSoC on Beagle BSP Improvements. As starting I started
> > working for gpio driver development . At this stage I am able to
> demonstrate
> > USR LEDs pattern.
> >
> > Videos of that can be found here on YouTube.
> > https://youtu.be/B3mSsfo-PAQ &
> > https://youtu.be/M1aKpOhUvv4.
> >
> > Herewith I have attached patch.txt file. Alternatively you can have
> > https://gist.github.com/ketul93/2e0d2c4b8b586be62e1d that includes the
> newly
> > added files code. I would be happy to hear your reviews and changes on my
> > work.
> >
> > And also I have been updating my work on
> > https://github.com/ketul93/RTEMS-on-BBB repo.
> >
> > It would be great to have your comments on my proposal.
> >
> > Thanks and regards,
> > ketul
> >
> > _______________________________________________
> > devel mailing list
> > devel at rtems.org
> > http://lists.rtems.org/mailman/listinfo/devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/devel/attachments/20150424/201797c8/attachment-0002.html>
-------------- next part --------------
commit e2484b80d8620cc78bf5f453e5cd904c7137a569
Author: ketul93 <ketulshah1993 at gmail.com>
Date: Thu Apr 23 23:24:11 2015 +0530
GPIO Driver developement with sample code
diff --git a/c/src/lib/libbsp/arm/beagle/Makefile.am b/c/src/lib/libbsp/arm/beagle/Makefile.am
index abef8ba..ce1e270 100644
--- a/c/src/lib/libbsp/arm/beagle/Makefile.am
+++ b/c/src/lib/libbsp/arm/beagle/Makefile.am
@@ -39,6 +39,7 @@ include_bsp_HEADERS += ../shared/include/arm-release-id.h
include_bsp_HEADERS += ../shared/include/start.h
include_bsp_HEADERS += include/irq.h
include_bsp_HEADERS += include/i2c.h
+include_bsp_HEADERS += include/gpio.h
include_libcpu_HEADERS =
include_libcpu_HEADERS += ../../../libcpu/arm/shared/include/arm-cp15.h
@@ -111,6 +112,9 @@ libbsp_a_SOURCES += ../../shared/console.c \
../../shared/console_write.c \
../../shared/console_control.c
+# GPIO
+libbsp_a_SOURCES += gpio/gpio.c
+
# I2C
libbsp_a_SOURCES += misc/i2c.c
diff --git a/c/src/lib/libbsp/arm/beagle/gpio/gpio.c b/c/src/lib/libbsp/arm/beagle/gpio/gpio.c
new file mode 100644
index 0000000..26c56f6
--- /dev/null
+++ b/c/src/lib/libbsp/arm/beagle/gpio/gpio.c
@@ -0,0 +1,122 @@
+/**
+ * @file
+ *
+ * @ingroup arm_beagle
+ *
+ * @brief Global BSP definitions.
+ */
+
+/**
+ * Copyright (c) 2015 Ketul Shah <ketulshah1993 at gmail.com>
+
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.org/license/LICENSE.
+ */
+
+#include <libcpu/am335x.h>
+#include <rtems.h>
+#include <bsp/irq.h>
+#include <bsp/gpio.h>
+#include <bsp.h>
+#include <stdlib.h>
+#include <stdint.h>
+#define GPIO_PIN_COUNT 95
+
+static bool is_initialized = false;
+
+static bbb_gpio_pin gpio_pin[GPIO_PIN_COUNT];
+
+static uint32_t inline get_pin_addr(GPIO_PIN pin)
+{
+ return (pin.gpio_bank + pin.gpio_bank_pin/8);
+}
+
+static uint8_t inline get_pin_mask(GPIO_PIN pin)
+{
+ return (1 << pin.gpio_bank_pin%8);
+}
+
+void arm_delay(float sec)
+{
+ rtems_task_wake_after(sec*rtems_clock_get_ticks_per_second());
+}
+
+/**
+ * @brief Initializes the GPIO API.
+ * Allocates space to the gpio_pin array and sets every pin as NOT_USED.
+ * If the API has already been initialized silently exits.
+ */
+void gpio_initialize(void)
+{
+ int i;
+ if ( is_initialized )
+ return;
+
+ is_initialized = true;
+ for ( i = 0; i < GPIO_PIN_COUNT; i++ ) {
+ gpio_pin[i].pin_type = NOT_USED;
+ }
+}
+
+/**
+ * @brief Configures a GPIO pin to perform a certain function.
+ *
+ * @retval 0 Pin was configured successfully.
+ * @retval -1 The received pin is already being used, or unknown function.
+ */
+ int gpio_select_pin(GPIO_PIN pin, bbb_pin type)
+{
+
+ if (gpio_pin[pin.gpio_id].pin_type != NOT_USED)
+ return -1;
+ gpio_pin[pin.gpio_id].pin_type = type;
+
+ switch(type){
+
+ case DIGITAL_OUTPUT:
+
+ writeb(0,get_pin_addr(pin) + AM335X_GPIO_OE);
+
+ break;
+
+ default:
+
+ return -1;
+ }
+ return 0;
+}
+
+/**
+ * @brief Gives an output GPIO pin the logical value of 1.
+ * @retval 0 Pin was set successfully.
+ * @retval -1 The received pin is not configured as an digital output.
+ */
+int gpio_set(GPIO_PIN pin)
+{
+ if (gpio_pin[pin.gpio_id-1].pin_type != DIGITAL_OUTPUT)
+ return -1;
+
+ uint8_t gpiobyte = readb(get_pin_addr(pin) + AM335X_GPIO_SETDATAOUT);
+ gpiobyte |= get_pin_mask(pin);
+ writeb(gpiobyte, get_pin_addr(pin) + AM335X_GPIO_SETDATAOUT);
+
+ return 0;
+}
+
+/**
+ * @brief Gives an output GPIO pin the logical value of 0.
+ * @retval 0 Pin was cleared successfully.
+ * @retval -1 The received pin is not configured as an digital output.
+ */
+int gpio_clear(GPIO_PIN pin)
+{
+ if (gpio_pin[pin.gpio_id-1].pin_type != DIGITAL_OUTPUT)
+ return -1;
+
+ uint8_t gpiobyte = readb(get_pin_addr(pin) + AM335X_GPIO_CLEARDATAOUT);
+ gpiobyte &= get_pin_mask(pin);
+ writeb(gpiobyte, get_pin_addr(pin) + AM335X_GPIO_CLEARDATAOUT);
+
+ return 0;
+}
\ No newline at end of file
diff --git a/c/src/lib/libbsp/arm/beagle/include/gpio.h b/c/src/lib/libbsp/arm/beagle/include/gpio.h
new file mode 100644
index 0000000..dba58ae
--- /dev/null
+++ b/c/src/lib/libbsp/arm/beagle/include/gpio.h
@@ -0,0 +1,89 @@
+/**
+ * @file
+ *
+ * @ingroup arm_beagle
+ *
+ * @brief Global BSP definitions.
+ */
+
+/*
+ * Copyright (c) 2015 Ketul Shah <ketulshah1993 at gmail.com>
+
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.org/license/LICENSE.
+ */
+
+#ifndef LIBBSP_ARM_BEAGLE_GPIO_H
+#define LIBBSP_ARM_BEAGLE_GPIO_H
+
+#include <rtems.h>
+#include <libcpu/am335x.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/**
+ * @brief The set of possible functions a pin can have.
+ *
+ * Enumerated type to define a pin function.
+ */
+typedef enum
+{
+ DIGITAL_OUTPUT,
+ NOT_USED
+} bbb_pin;
+
+typedef enum
+{
+ PULL_UP,
+ PULL_DOWN,
+ NO_PULL_RESISTOR
+} bbb_gpio_input_mode;
+
+typedef struct
+{
+ /* The pin type */
+ bbb_pin pin_type;
+
+ /* GPIO input pin mode. */
+ bbb_gpio_input_mode input_mode;
+
+} bbb_gpio_pin;
+
+typedef struct {
+ char *gpio_name; /* Readable GPIO pin name */
+ unsigned int gpio_bank_pin; /* GPIO bank pin number as per Manual of AM335X */
+ unsigned int gpio_bank; /* GPIO bank determines register */
+ unsigned int gpio_id; /* unique pin number for every gpio*/
+ } GPIO_PIN;
+
+
+ #define USR0 ((GPIO_PIN){ "GPIO1_21", 21, AM335X_GPIO1, 1 })
+ #define USR1 ((GPIO_PIN){ "GPIO1_22", 22, AM335X_GPIO1, 2 })
+ #define USR2 ((GPIO_PIN){ "GPIO1_23", 23, AM335X_GPIO1, 3 })
+ #define USR3 ((GPIO_PIN){ "GPIO1_24", 24, AM335X_GPIO1, 4 })
+
+/**
+ * @brief Insert delay in seconds.
+ */
+extern void arm_delay(float sec);
+/**`
+ * @brief Initializes the GPIO API.
+ */
+extern void gpio_initialize(void);
+/**
+ * @brief Selects a GPIO pin for a specific function.
+ */
+extern int gpio_select_pin(GPIO_PIN pin, bbb_pin type);
+/**
+ * @brief Turns on the given pin.
+ */
+extern int gpio_set(GPIO_PIN pin);
+/**
+ * @brief Turns off the given pin.
+ */
+extern int gpio_clear(GPIO_PIN pin);
+
+#endif /* LIBBSP_ARM_BEAGLE_GPIO_H */
\ No newline at end of file
diff --git a/c/src/lib/libbsp/arm/beagle/preinstall.am b/c/src/lib/libbsp/arm/beagle/preinstall.am
index dd1ec95..e963bc1 100644
--- a/c/src/lib/libbsp/arm/beagle/preinstall.am
+++ b/c/src/lib/libbsp/arm/beagle/preinstall.am
@@ -110,6 +110,10 @@ $(PROJECT_INCLUDE)/bsp/i2c.h: include/i2c.h $(PROJECT_INCLUDE)/bsp/$(dirstamp)
$(INSTALL_DATA) $< $(PROJECT_INCLUDE)/bsp/i2c.h
PREINSTALL_FILES += $(PROJECT_INCLUDE)/bsp/i2c.h
+$(PROJECT_INCLUDE)/bsp/gpio.h: include/gpio.h $(PROJECT_INCLUDE)/bsp/$(dirstamp)
+ $(INSTALL_DATA) $< $(PROJECT_INCLUDE)/bsp/gpio.h
+PREINSTALL_FILES += $(PROJECT_INCLUDE)/bsp/gpio.h
+
$(PROJECT_INCLUDE)/libcpu/arm-cp15.h: ../../../libcpu/arm/shared/include/arm-cp15.h $(PROJECT_INCLUDE)/libcpu/$(dirstamp)
$(INSTALL_DATA) $< $(PROJECT_INCLUDE)/libcpu/arm-cp15.h
PREINSTALL_FILES += $(PROJECT_INCLUDE)/libcpu/arm-cp15.h
diff --git a/c/src/lib/libcpu/arm/shared/include/am335x.h b/c/src/lib/libcpu/arm/shared/include/am335x.h
index 1f638bd..d4cea8e 100644
--- a/c/src/lib/libcpu/arm/shared/include/am335x.h
+++ b/c/src/lib/libcpu/arm/shared/include/am335x.h
@@ -279,4 +279,24 @@
#define AM335X_WDT_WWPS 0x34
/* Command posted status */
#define AM335X_WDT_WSPR 0x48
- /* Activate/deactivate sequence */
+ /* Activate/deactivate sequence */
+ /* For BeagleBone Black GPIO*/
+#define AM335X_GPIO0 0x44E07000
+ /* GPIO Bank 0 base Register */
+#define AM335X_GPIO1 0x4804C000
+ /* GPIO Bank 1 base Register */
+#define AM335X_GPIO2 0x481AC000
+ /* GPIO Bank 2 base Register */
+#define AM335X_GPIO3 0x481AE000
+ /* GPIO Bank 3 base Register */
+#define AM335X_GPIO_OE 0x134
+ /* GPIO Output Enable Register */
+#define AM335X_GPIO_DATAOUT 0x13C
+ /* GPIO Output pin set Register */
+#define AM335X_GPIO_DATAIN 0x138
+ /* GPIO Read Data Register */
+#define AM335X_GPIO_CLEARDATAOUT 0x190
+ /* GPIO Output Clear Register */
+#define AM335X_GPIO_SETDATAOUT 0x194
+ /* GPIO Output Set Register */
+ /*Gpio memory registers am335x*/
\ No newline at end of file
diff --git a/testsuites/samples/Makefile.am b/testsuites/samples/Makefile.am
index 08455d3..de4f351 100644
--- a/testsuites/samples/Makefile.am
+++ b/testsuites/samples/Makefile.am
@@ -1,6 +1,6 @@
ACLOCAL_AMFLAGS = -I ../aclocal
-_SUBDIRS = hello capture ticker base_sp unlimited minimum fileio
+_SUBDIRS = hello capture ticker base_sp unlimited minimum fileio gpio
if MPTESTS
## base_mp is a sample multiprocessing test
diff --git a/testsuites/samples/configure.ac b/testsuites/samples/configure.ac
index e6f12d0..ac32583 100644
--- a/testsuites/samples/configure.ac
+++ b/testsuites/samples/configure.ac
@@ -60,6 +60,7 @@ AC_CHECK_SIZEOF([time_t])
AC_CONFIG_FILES([Makefile
base_sp/Makefile
hello/Makefile
+gpio/Makefile
loopback/Makefile
minimum/Makefile
fileio/Makefile
diff --git a/testsuites/samples/gpio/Makefile.am b/testsuites/samples/gpio/Makefile.am
new file mode 100644
index 0000000..5f06584
--- /dev/null
+++ b/testsuites/samples/gpio/Makefile.am
@@ -0,0 +1,20 @@
+
+rtems_tests_PROGRAMS = gpio
+gpio_SOURCES = init.c
+
+dist_rtems_tests_DATA = gpio.scn
+dist_rtems_tests_DATA += gpio.doc
+
+include $(RTEMS_ROOT)/make/custom/@RTEMS_BSP at .cfg
+include $(top_srcdir)/../automake/compile.am
+include $(top_srcdir)/../automake/leaf.am
+
+
+LINK_OBJS = $(gpio_OBJECTS)
+LINK_LIBS = $(gpio_LDLIBS)
+
+hello$(EXEEXT): $(gpio_OBJECTS) $(gpio_DEPENDENCIES)
+ @rm -f gpio$(EXEEXT)
+ $(make-exe)
+
+include $(top_srcdir)/../automake/local.am
diff --git a/testsuites/samples/gpio/gpio.doc b/testsuites/samples/gpio/gpio.doc
new file mode 100644
index 0000000..9812864
--- /dev/null
+++ b/testsuites/samples/gpio/gpio.doc
@@ -0,0 +1,9 @@
+# COPYRIGHT (c) 1989-1999.
+# On-Line Applications Research Corporation (OAR).
+#
+# The license and distribution terms for this file may be
+# found in the file LICENSE in this distribution or at
+# http://www.rtems.org/license/LICENSE.
+#
+
+
diff --git a/testsuites/samples/gpio/gpio.scn b/testsuites/samples/gpio/gpio.scn
new file mode 100644
index 0000000..057ab6e
--- /dev/null
+++ b/testsuites/samples/gpio/gpio.scn
@@ -0,0 +1,3 @@
+*** GPIO TEST ***
+Gpio Testing
+*** END OF GPIO TEST ***
diff --git a/testsuites/samples/gpio/init.c b/testsuites/samples/gpio/init.c
new file mode 100644
index 0000000..e4496fa
--- /dev/null
+++ b/testsuites/samples/gpio/init.c
@@ -0,0 +1,80 @@
+/*
+ * COPYRIGHT (c) 1989-2012.
+ * On-Line Applications Research Corporation (OAR).
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.org/license/LICENSE.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <rtems/test.h>
+#include <bsp/gpio.h> /* Calls the BSP gpio library */
+#include <rtems/status-checks.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+/* forward declarations to avoid warnings */
+rtems_task Init(rtems_task_argument argument);
+
+const char rtems_test_name[] = "Gpio Test";
+
+rtems_task Init(
+ rtems_task_argument ignored
+)
+{
+ rtems_test_begin();
+ printf("Starting Gpio Testing\n");
+ /* Initializes the GPIO API */
+ gpio_initialize();
+
+ gpio_select_pin(USR0, DIGITAL_OUTPUT);
+ gpio_select_pin(USR1, DIGITAL_OUTPUT);
+ gpio_select_pin(USR2, DIGITAL_OUTPUT);
+ gpio_select_pin(USR3, DIGITAL_OUTPUT);
+ printf("Clearing all USR LEDs \n");
+ gpio_clear(USR0);
+ gpio_clear(USR1);
+ gpio_clear(USR2);
+ gpio_clear(USR3);
+
+ /*Generation of USR LED pattern*/
+ gpio_set(USR0);
+ arm_delay(0.5);
+ gpio_set(USR1);
+ arm_delay(0.5);
+ gpio_set(USR2);
+ arm_delay(0.5);
+ gpio_set(USR3);
+ arm_delay(0.5);
+ gpio_clear(USR3);
+ arm_delay(1);
+ gpio_clear(USR2);
+ arm_delay(1);
+ gpio_clear(USR1);
+ arm_delay(1);
+ gpio_clear(USR0);
+ arm_delay(1);
+
+ printf("Gpio Test Completed\n");
+ rtems_test_end();
+ exit( 0 );
+}
+
+/* NOTICE: the clock driver is enabled */
+#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
+
+#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
+
+#define CONFIGURE_MAXIMUM_TASKS 1
+#define CONFIGURE_USE_DEVFS_AS_BASE_FILESYSTEM
+
+#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
+
+#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
+
+#define CONFIGURE_INIT
+#include <rtems/confdefs.h>
\ No newline at end of file
More information about the devel
mailing list