Giter Club home page Giter Club logo

libmcp23s17's Introduction

libmcp23s17

A simple library for accessing the MCP23S17 port expander through SPI.

Building

To build the library, use:

$ make

Install the library to /usr/local using:

$ make install

To test the library, compile and execute the example program:

$ make example
$ ./example

Another example program, using interrupts, is available. Build with:

$ make interrupt_example
$ ./interrupt_example

Usage

See example.c and interrupt_example.c for ideas on how to use.

Compile your software with the following flag:

-lmcp23s17

You can specify the path of the library manually, using:

-I/path/to/headers -L/path/to/libmcp23s17 -lmcp23s17

Documentation

An online version of the documentation is available at http://piface.github.io/libmcp23s17.

Build it with (assuming that you are in the directory of the cloned repository):

$ cd docs/
$ doxygen libmcp23s17-doc.conf

To view as HTML, point your browser to docs/html/index.html.

To view as PDF:

$ cd latex/
$ make

The pdf is called refman.pdf.

libmcp23s17's People

Contributors

robertof avatar scm6079 avatar stevensmi avatar tompreston avatar xaqq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

libmcp23s17's Issues

libmcp23s17 gets There was a error during the SPI transaction on read_reg / write_reg operations

I'm running Raspbian with the 3.18.4+ kernel and the device tree. There's a bug where read_reg or write_reg gets a "error during the SPI transaction".

I've chased the bug to mcp23s17.c and can fix it with this patch

--- /shared/Raspi/libmcp23s17~/src/mcp23s17.c   2015-01-29 23:23:28.967496000 +0000
+++ mcp23s17.c  2015-01-29 22:33:48.777496000 +0000
@@ -72,6 +72,7 @@
     uint8_t rx_buf[sizeof tx_buf];

     struct spi_ioc_transfer spi;
+    memset (&spi, 0, sizeof(spi));
     spi.tx_buf = (unsigned long) tx_buf;
     spi.rx_buf = (unsigned long) rx_buf;
     spi.len = sizeof tx_buf;
@@ -98,6 +99,7 @@
     uint8_t rx_buf[sizeof tx_buf];

     struct spi_ioc_transfer spi;
+    memset (&spi, 0, sizeof(spi));
     spi.tx_buf = (unsigned long) tx_buf;
     spi.rx_buf = (unsigned long) rx_buf;
     spi.len = sizeof tx_buf;

mcp23s17_wait_for_interrupt not working properly after more thousand interrupts

@tompreston (as already sent via mail to you)

During my test I detected, that the interrupt handling in your c-library is working not stable. After a few hours the interrupt handling drives crazy.
After some analysis I found out, that the problem seems to be in the mcp23s17_wait_for_interrupt function. In your version for each call a epoll handle will be installed. Even if you close the file handle fd the epoll handle seems to be existing in the background. The error message after some thousand calls was “[EMFILE] Too many open files”. I found more than thousand open file handles “anon_indode”.

Now I made some variables static. The epoll handling will be created only once. It seems to be stable now. Maybe you can check if the following changes makes sense, I’m not very familiar with soft interrupt handling.
Thanks in advantage, Wolfgang.

The follwoing changes seems to be stable even if a 1kHz Signal needs to be handled.
Everey 500 microsec a signal state change for days causes no Problems.

include <unistd.h>

include <errno.h>

include <string.h>

static int iEpollCreated = 0;
static int iGbl_fd = 0;
static int iGbl_epfd = 0;
static struct epoll_event Gbl_ev;
static struct epoll_event Gbl_events;

int mcp23s17_wait_for_interrupt(int timeout)
{
int n = -1;
char str_filenm[33];

snprintf(str_filenm, sizeof(str_filenm), "/sys/class/gpio/gpio%d/value", GPIO_INTERRUPT_PIN);

if(iEpollCreated <= 0)
   {
          iGbl_epfd = epoll_create(1);

         if(iGbl_epfd == 0)
         {
                fprintf(stderr,
                              "mcp23s17_wait_for_interrupt: There was a error during the epoll_create .\n");
                fprintf( stderr, "Error is %s (errno=%d)\n", strerror( errno ), errno );
         }
         iGbl_fd = open(str_filenm, O_RDONLY | O_NONBLOCK);
   }

if(iGbl_fd > 0)
{
   if(iEpollCreated <= 0)
   {
         Gbl_ev.events = EPOLLIN | EPOLLET;
         Gbl_ev.data.fd = iGbl_fd;

                if(epoll_ctl(iGbl_epfd, EPOLL_CTL_ADD, iGbl_fd, &Gbl_ev) != 0)
                {
                       fprintf(stderr,
                                    "mcp23s17_wait_for_interrupt: There was a error during the epoll_ctl EPOLL_CTL_ADD.\n");
                       fprintf( stderr, "Error is %s (errno=%d)\n", strerror( errno ), errno );
                }
                // Ignore GPIO Initial Event
                epoll_wait(iGbl_epfd, &Gbl_events, 1, 10);
   }

    // Wait for user event
    n = epoll_wait(iGbl_epfd, &Gbl_events, 1, timeout);

    iEpollCreated = 1;

    /*
    if(close(iGblEpoll_fd) < 0)
    {
         iEpollCreated = 0;
         fprintf(stderr,
                                    "mcp23s17_wait_for_interrupt: Can't close fd <%s> <%d>.\n", str_filenm, iGbl_fd);
                fprintf( stderr, "Error is %s (errno=%d)\n", strerror( errno ), errno );
    }
     */
}
else
   {
         fprintf(stderr,
                       "mcp23s17_wait_for_interrupt: Can't open fd <%s> <%d>.\n", str_filenm, iGbl_fd);
         fprintf( stderr, "Error is %s (errno=%d)\n", strerror( errno ), errno );
   }
return n;

}

Package

@thomasmacpherson

Please tag a release of this such that a tgz is accessible for direct download over http. Also, please adopt a version scheme such as semver, which the packaged version will respect when converting (your) upstream code to a redistributable.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.