From f1bcd7bfb45a3bbfcd79a09eb7d822ae4ad3cf76 Mon Sep 17 00:00:00 2001 From: Abhishek Malik Date: Wed, 31 Aug 2016 11:51:25 -0700 Subject: [PATCH] A110x: Adding C source Signed-off-by: Abhishek Malik --- src/a110x/CMakeLists.txt | 14 +++-- src/a110x/a110x.c | 74 ++++++++++++++++++++++++ src/a110x/a110x.h | 120 +++++++++++++++++++++++++++++++++++++++ src/a110x/a110x_fti.c | 74 ++++++++++++++++++++++++ 4 files changed, 277 insertions(+), 5 deletions(-) create mode 100644 src/a110x/a110x.c create mode 100644 src/a110x/a110x.h create mode 100644 src/a110x/a110x_fti.c diff --git a/src/a110x/CMakeLists.txt b/src/a110x/CMakeLists.txt index 844846f7..f256ca99 100644 --- a/src/a110x/CMakeLists.txt +++ b/src/a110x/CMakeLists.txt @@ -1,5 +1,9 @@ -set (libname "a110x") -set (libdescription "upm a110x sensor module") -set (module_src ${libname}.cxx) -set (module_hpp ${libname}.hpp) -upm_module_init() +upm_mixed_module_init (NAME a110x + DESCRIPTION "upm a110x sensor module" + C_HDR a110x.h + C_SRC a110x.c + CPP_HDR a110x.hpp + CPP_SRC a110x.cxx + FTI_SRC a110x_fti.c + CPP_WRAPS_C + REQUIRES mraa) diff --git a/src/a110x/a110x.c b/src/a110x/a110x.c new file mode 100644 index 00000000..9f492b64 --- /dev/null +++ b/src/a110x/a110x.c @@ -0,0 +1,74 @@ +/* + * Author: Jon Trulson + * Abhishek Malik + * Copyright (c) 2016 Intel Corporation. + * + * 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 "a110x.h" + +a110x_context a110x_init(uint8_t pin){ + a110x_context dev = + (a110x_context) malloc(sizeof(struct _a110x_context)); + + if (!dev) + { + return NULL; + } + + dev->gpio_pin = pin; + dev->gpio = mraa_gpio_init(dev->gpio_pin); + if (mraa_gpio_dir(dev->gpio, MRAA_GPIO_IN) != MRAA_SUCCESS){ + free(dev); + return NULL; + } + dev->isr_installed = false; + return dev; +} + +void a110x_close(a110x_context dev){ + free(dev); +} + +upm_result_t a110x_magnet_detected(a110x_context dev, bool* res){ + int val = mraa_gpio_read(dev->gpio); + if (val == 0) + *res = false; + else + *res = true; + return UPM_SUCCESS; +} + +upm_result_t a110x_install_isr(a110x_context dev, + mraa_gpio_edge_t edge_level, + void (*isr)(void *), void *arg){ + if (dev->isr_installed) + a110x_uninstall_isr(dev); + + mraa_gpio_isr(dev->gpio, edge_level, isr, arg); + dev->isr_installed = true; + return UPM_SUCCESS; +} + +upm_result_t a110x_uninstall_isr(a110x_context dev){ + mraa_gpio_isr_exit(dev->gpio); + dev->isr_installed = false; + return UPM_SUCCESS; +} diff --git a/src/a110x/a110x.h b/src/a110x/a110x.h new file mode 100644 index 00000000..bc27e9f4 --- /dev/null +++ b/src/a110x/a110x.h @@ -0,0 +1,120 @@ +/* + * Author: Jon Trulson + * Abhishek Malik + * Copyright (c) 2016 Intel Corporation. + * + * 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 A110X_A110X_H_ +#define A110X_A110X_H_ + +#include +#include +#include + +#include "upm.h" +#include "mraa/gpio.h" + +/** + * @brief A110X Hall Effect library + * @defgroup a110x libupm-a110x + * @ingroup gpio electric robok + */ + +/** + * @library a110x + * @sensor a110x + * @comname A110X Hall Effect Sensor + * @altname Grove Hall Sensor + * @altid A1101, A1102, A1103, A1004, A1106 + * @type electric + * @web http://www.allegromicro.com/en/Products/Magnetic-Digital-Position-Sensor-ICs/Hall-Effect-Unipolar-Switches/A1101-2-3-4-6.aspx + * @con gpio + * @kit robok + * + * @brief API for the A110X Hall Effect sensors + * + * UPM module for the A110X (A1101, A1102, A1103, A1104, and A1106) + * Hall Effect sensors. It outputs a digital signal indicating + * whether it is detecting a magnetic field with south polarity + * perpendicular to the sensor element. + * + * @image html a110x.jpg + * An example showing a simple test for the presence of a field + * @snippet a110x.cxx Interesting + * An example demonstrating the use of an interrupt handler to count pulses + * @snippet a110x-intr.cxx Interesting + */ + +/** + * device context + */ +typedef struct _a110x_context { + mraa_gpio_context gpio; + uint8_t gpio_pin; + bool isr_installed; +} *a110x_context; + +/** + * A110X Initialization function + * + * @param pin GPIO pin to use + * @return device context pointer + */ +a110x_context a110x_init(uint8_t pin); + +/** + * A110X Initialization function + * + * @param dev a110x_context pointer + */ +void a110x_close(a110x_context dev); + +/** + * Determines whether a magnetic field of south polarity has been detected + * + * @param dev a110x_context pointer + * @param bool* to note the response + * @return upm_result_t UPM success/error code + */ +upm_result_t a110x_magnet_detected(a110x_context dev, bool* res); + +/** + * Installs an interrupt service routine (ISR) to be called when + * the appropriate magnetic field is detected + * + * @param dev a110x_context pointer + * @param mraa_gpio_edge_t edge trigger level + * @param isr ISR callback function + * @return upm_result_t UPM success/error code + */ +upm_result_t a110x_install_isr(a110x_context dev, + mraa_gpio_edge_t edge_level, + void (*isr)(void *), void *arg); + +/** + * Uninstalls the previously installed ISR + * + * @param dev a110x_context pointer + * @return upm_result_t UPM success/error code + */ +upm_result_t a110x_uninstall_isr(a110x_context dev); + +#endif /* A110X_A110X_H_ */ diff --git a/src/a110x/a110x_fti.c b/src/a110x/a110x_fti.c new file mode 100644 index 00000000..625363f5 --- /dev/null +++ b/src/a110x/a110x_fti.c @@ -0,0 +1,74 @@ +/* + * Author: Jon Trulson + * Abhishek Malik + * Copyright (c) 2016 Intel Corporation. + * + * 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 "a110x.h" +#include "upm_fti.h" + +/** + * This file implements the Function Table Interface (FTI) for this sensor + */ + +const char upm_a110x_name[] = "A110X"; +const char upm_a110x_description[] = "A110X Hall Effect Sensor"; +const upm_protocol_t upm_a110x_protocol[] = {UPM_GPIO}; +const upm_sensor_t upm_a110x_category[] = {UPM_ELECTRICITY}; + +// forward declarations +const upm_sensor_descriptor_t upm_a110x_get_descriptor(); +const void* upm_a110x_get_ft(upm_sensor_t sensor_type); +void* upm_a110x_init_name(); +void upm_a110x_close(void* dev); + +static const upm_sensor_ft ft = +{ + .upm_sensor_init_name = &upm_a110x_init_name, + .upm_sensor_close = &upm_a110x_close, + .upm_sensor_get_descriptor = &upm_a110x_get_descriptor +}; + +const void* upm_a110x_get_ft(upm_sensor_t sensor_type){ + if (sensor_type == UPM_SENSOR){ + return &ft; + } + return NULL; +} + +const upm_sensor_descriptor_t upm_a110x_get_descriptor(){ + upm_sensor_descriptor_t usd; + usd.name = upm_a110x_name; + usd.description = upm_a110x_description; + usd.protocol_size = 1; + usd.protocol = upm_a110x_protocol; + usd.category_size = 1; + usd.category = upm_a110x_category; + return usd; +} + +void* upm_a110x_init_name(){ + return NULL; +} + +void upm_a110x_close(void* dev){ + a110x_close((a110x_context)dev); +}