From 9a4b39d707e3ec9836b449519fea2e14c8602a0d Mon Sep 17 00:00:00 2001 From: Pratik Prajapati Date: Tue, 28 Jun 2016 19:00:26 +0530 Subject: [PATCH 1/3] Add spi library Signed-off-by: Pratik Prajapati --- src/CMakeLists.txt | 2 ++ src/spi_common.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++ src/spi_common.h | 45 ++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 src/spi_common.c create mode 100644 src/spi_common.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b73f607..d54378a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,8 @@ # Add executable targets ######################## ADD_LIBRARY(gpio OBJECT gpio.c) +ADD_LIBRARY(spi_common OBJECT spi_common.c) + ADD_EXECUTABLE(motion_click_app motion_click_app.c $) # Add install targets diff --git a/src/spi_common.c b/src/spi_common.c new file mode 100644 index 0000000..5df4a7a --- /dev/null +++ b/src/spi_common.c @@ -0,0 +1,73 @@ +/*************************************************************************************************** + * Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group companies + * and/or licensors + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to + * endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include "spi_common.h" + +int SpiInit(const char *spiPath) +{ + int fd; + unsigned int bits_per_word = 8; + unsigned int mode = SPI_MODE_3; + unsigned int speed = SPI_CLOCK_SPEED; + + if ((fd = open(spiPath, O_NONBLOCK)) == -1) + { + perror("SPI error: can't open device"); + return -1; + } + if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) + { + perror("SPI error: can't set mode"); + return -1; + } + if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits_per_word) == -1) + { + perror("SPI error: can't set bits per word"); + return -1; + } + + if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) + { + perror("SPI error: can't set max speed HZ"); + return -1; + } + + return fd; +} + +inline void SpiFree(int fd) +{ + close(fd); +} diff --git a/src/spi_common.h b/src/spi_common.h new file mode 100644 index 0000000..e76a64b --- /dev/null +++ b/src/spi_common.h @@ -0,0 +1,45 @@ +/*************************************************************************************************** + * Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group companies + * and/or licensors + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to + * endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @brief Library for SPI operations. + */ + + +#ifndef SPI_COMMON_H +#define SPI_COMMON_H + +#define SPI_PATH_FOR_MIKROBUS(no) ((no) == 1 ? "/dev/spidev0.2" : ((no) == 2 ? "/dev/spidev0.3" : "")) +#define SPI_CLOCK_SPEED 1000000 + +int SpiInit(const char *spi_path); +void SpiFree(int fd); + +#endif From 6aa74552c8e204aca44430d41f38d57193e1295a Mon Sep 17 00:00:00 2001 From: Pratik Prajapati Date: Tue, 28 Jun 2016 19:10:54 +0530 Subject: [PATCH 2/3] Add pwm library Signed-off-by: Pratik Prajapati --- src/CMakeLists.txt | 1 + src/pwm_common.c | 157 +++++++++++++++++++++++++++++++++++++++++++++ src/pwm_common.h | 45 +++++++++++++ 3 files changed, 203 insertions(+) create mode 100644 src/pwm_common.c create mode 100644 src/pwm_common.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d54378a..9431755 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,6 +2,7 @@ ######################## ADD_LIBRARY(gpio OBJECT gpio.c) ADD_LIBRARY(spi_common OBJECT spi_common.c) +ADD_LIBRARY(pwm_common OBJECT pwm_common.c) ADD_EXECUTABLE(motion_click_app motion_click_app.c $) diff --git a/src/pwm_common.c b/src/pwm_common.c new file mode 100644 index 0000000..a3c83fb --- /dev/null +++ b/src/pwm_common.c @@ -0,0 +1,157 @@ +/*************************************************************************************************** + * Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group companies + * and/or licensors + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to + * endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +#include +#include +#include + +#define PATH_MAX 64 +#define BUFFER_MAX 8 + +int PwmExport(unsigned int pwmNumber) +{ + int fd; + int bytesWritten, status = 0; + char buffer[BUFFER_MAX]; + + fd = open("/sys/class/pwm/pwmchip0/export", O_WRONLY); + if (fd == -1) + { + fprintf(stderr, "Failed to open export for writing\n"); + return -1; + } + + bytesWritten = snprintf(buffer, BUFFER_MAX, "%u", pwmNumber); + status = write(fd, buffer, bytesWritten); + if (status == -1) + { + fprintf(stderr, "Failed to export pwm\n"); + } + close(fd); + return 0; +} + +int PwmUnexport(unsigned int pwmNumber) +{ + int fd; + int bytesWritten, status = 0; + char buffer[BUFFER_MAX]; + + fd = open("/sys/class/pwm/pwmchip0/unexport", O_WRONLY); + if (fd == -1) + { + fprintf(stderr, "Failed to open unexport for writing"); + return -1; + } + + bytesWritten = snprintf(buffer, BUFFER_MAX, "%u", pwmNumber); + status = write(fd, buffer, bytesWritten); + if (status == -1) + { + fprintf(stderr, "Failed to unexport pwm"); + } + + close(fd); + return status; +} + +int PwmPeriod(unsigned int pwmNumber, unsigned int period) +{ + int fd; + char path[PATH_MAX]; + char buffer[BUFFER_MAX]; + int bytesWritten, status = 0; + + snprintf(path, PATH_MAX, "/sys/class/pwm/pwmchip0/pwm%u/period", pwmNumber); + fd = open(path, O_WRONLY); + if (fd == -1) + { + fprintf(stderr, "Failed to open pwm direction for writing\n"); + return -1; + } + + bytesWritten = snprintf(buffer, BUFFER_MAX, "%u", period); + status = write(fd, buffer, bytesWritten); + if (status == -1) + { + fprintf(stderr, "Failed to set pwm period\n"); + } + close(fd); + return status; +} + +int PwmDutyCycle(unsigned int pwmNumber, unsigned int dutyCycle) +{ + int fd; + char path[PATH_MAX]; + char buffer[BUFFER_MAX]; + int bytesWritten, status = 0; + + snprintf(path, PATH_MAX, "/sys/class/pwm/pwmchip0/pwm%u/duty_cycle", pwmNumber); + fd = open(path, O_WRONLY); + if (fd == -1) + { + fprintf(stderr, "Failed to open pwm duty_cycle for writing\n"); + return -1; + } + + bytesWritten = snprintf(buffer, BUFFER_MAX, "%u", dutyCycle); + status = write(fd, buffer, bytesWritten); + if (status == -1) + { + fprintf(stderr, "Failed to set pwm duty_cycle\n"); + } + close(fd); + return status; +} + +int PwmEnable(unsigned int pwmNumber) +{ + int fd; + int status = 0; + char path[PATH_MAX]; + + snprintf(path, PATH_MAX, "/sys/class/pwm/pwmchip0/pwm%u/enable", pwmNumber); + fd = open(path, O_WRONLY); + if (fd == -1) + { + fprintf(stderr, "Failed to open pwm enable for writing\n"); + return -1; + } + + status = write(fd, "1", sizeof("1")); + if (status == -1) + { + fprintf(stderr, "Failed to set pwm enable\n"); + } + close(fd); + return status; +} diff --git a/src/pwm_common.h b/src/pwm_common.h new file mode 100644 index 0000000..28e7eb7 --- /dev/null +++ b/src/pwm_common.h @@ -0,0 +1,45 @@ +/*************************************************************************************************** + * Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group companies + * and/or licensors + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to + * endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @brief Library for PWM operations. + */ + + +#ifndef PWM_COMMON_H +#define PWM_COMMON_H + +int PwmExport(unsigned int pwmNumber); +int PwmUnexport(unsigned int pwmNumber); +int PwmPeriod(unsigned int pwmNumber, unsigned int period); +int PwmDutyCycle(unsigned int pwmNumber, unsigned int dutyCycle); +int PwmEnable(unsigned int pwmNumber); + +#endif From 2666f0e19bb3b63b8a0ce815a5e6fc31723b9b1e Mon Sep 17 00:00:00 2001 From: Pratik Prajapati Date: Tue, 28 Jun 2016 19:12:07 +0530 Subject: [PATCH 3/3] Add bargraph click app Signed-off-by: Pratik Prajapati --- README.md | 15 +++- src/CMakeLists.txt | 3 +- src/bargraph_click_app.c | 180 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 194 insertions(+), 4 deletions(-) create mode 100644 src/bargraph_click_app.c diff --git a/README.md b/README.md index bbf5e23..f520bfc 100644 --- a/README.md +++ b/README.md @@ -18,9 +18,7 @@ After starting the app move your hand above the motion click. Press ctrl+c to exit the application. -Two options can be given: - -1) mikroBUS number to specify where motion click is connected. +mikroBUS number can be given in option to specify where motion click is connected. Default value for [mikroBUS](http://www.mikroe.com/mikrobus/) is 1 @@ -28,3 +26,14 @@ Default value for [mikroBUS](http://www.mikroe.com/mikrobus/) is 1 Ready for detecting motion... detected + +###Application to display binary representation of a number using [bargraph click](http://www.mikroe.com/click/bargraph/) + +Start the app and enter number to display. + +mikroBUS number can be given in option to specify where bargraph click is connected. + + $ bargraph_click_app -m + + Enter number to display (0 for exit): 2 + Enter number to display (0 for exit): 5 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9431755..b8f19dd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,8 +5,9 @@ ADD_LIBRARY(spi_common OBJECT spi_common.c) ADD_LIBRARY(pwm_common OBJECT pwm_common.c) ADD_EXECUTABLE(motion_click_app motion_click_app.c $) +ADD_EXECUTABLE(bargraph_click_app bargraph_click_app.c $ $) # Add install targets ###################### -INSTALL(TARGETS motion_click_app +INSTALL(TARGETS motion_click_app bargraph_click_app RUNTIME DESTINATION /usr/bin) diff --git a/src/bargraph_click_app.c b/src/bargraph_click_app.c new file mode 100644 index 0000000..509dfc7 --- /dev/null +++ b/src/bargraph_click_app.c @@ -0,0 +1,180 @@ +/*************************************************************************************************** + * Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group companies + * and/or licensors + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to + * endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "spi_common.h" +#include "pwm_common.h" + +/* bottom 10 bits of display_seg argument correspond to 10 display segments */ +static int send_to_bargraph(int fd, unsigned int display_seg) +{ + int ret; + unsigned int buf[2]; + struct spi_ioc_transfer tr[2]; + + /* buf[0] will be shifted into segments 8 and 9 */ + buf[0] = (display_seg >> 8) & 3; + /* buf[1] sets segments 0 to 7 */ + buf[1] = display_seg & 0x00ff; + + /* send message consisting of two transfers */ + memset(&tr, 0, sizeof(tr)); + tr[0].tx_buf = (unsigned long)&buf[0]; + tr[0].len = 1; + tr[1].tx_buf = (unsigned long)&buf[1]; + tr[1].len = 1; + ret = ioctl(fd, SPI_IOC_MESSAGE(2), &tr); + + if (ret == -1) + { + perror("SPI error: can't send data\n"); + return -1; + } + + return 0; +} + +static void PrintUsage(const char *program) +{ + printf("Usage: %s [options]\n\n" + " -m mikroBUS number\n" + " -h display this message\n\n", + program); +} + +static int ParseCmdOpts(int argc, char *argv[], char **spi_path) +{ + int opt; + uint8_t num; + + opterr = 0; + + while ((opt = getopt(argc, argv, "m:h")) != -1) + { + switch (opt) + { + case 'm': + num = strtoul(optarg, NULL, 10); + if (num != 1 && num != 2) + { + fprintf(stderr, "Error: invalid mikroBUS\n"); + return -1; + } + *spi_path = SPI_PATH_FOR_MIKROBUS(num); + break; + + case 'h': + PrintUsage(argv[0]); + return 0; + + default: + PrintUsage(argv[0]); + return -1; + } + } + return 1; +} + +static inline void clean_stdin() +{ + while (getchar()!='\n'); +} + +int main(int argc, char *argv[]) +{ + int fd, status; + unsigned int num_to_display; + char *spi_path = SPI_PATH_FOR_MIKROBUS(1); + char c; + + status = ParseCmdOpts(argc, argv, &spi_path); + if (status <= 0) + { + return status; + } + + fd = SpiInit(spi_path); + if (fd < 0) + { + return -1; + } + + status = PwmExport(0); + if (status == -1) + { + goto cleanup; + } + status = PwmPeriod(0, 100000); + if (status == -1) + { + goto cleanup; + } + status = PwmDutyCycle(0, 50000); + if (status == -1) + { + goto cleanup; + } + status = PwmEnable(0); + if (status == -1) + { + goto cleanup; + } + + // clear all segments + send_to_bargraph(fd, 0); + + while (1) + { + printf("\nEnter number to display (0 for exit): "); + fflush(stdout); + if (scanf("%u%c", &num_to_display, &c) != 2 || c != '\n') + clean_stdin(); + if (num_to_display == 0) + break; + send_to_bargraph(fd, num_to_display); + } + +cleanup: + send_to_bargraph(fd, 0); + PwmUnexport(0); + SpiFree(fd); + return status; +}