[PATCH 6/6] dev: Use C11 mutex for I2C framework

Sebastian Huber sebastian.huber at embedded-brains.de
Wed Dec 14 13:39:42 UTC 2016


---
 cpukit/dev/i2c/i2c-bus.c         | 54 +++++++++++++---------------------------
 cpukit/dev/include/dev/i2c/i2c.h |  4 ++-
 testsuites/libtests/i2c01/init.c |  2 --
 3 files changed, 20 insertions(+), 40 deletions(-)

diff --git a/cpukit/dev/i2c/i2c-bus.c b/cpukit/dev/i2c/i2c-bus.c
index 0f27d06..696bef5 100644
--- a/cpukit/dev/i2c/i2c-bus.c
+++ b/cpukit/dev/i2c/i2c-bus.c
@@ -33,20 +33,20 @@
 
 void i2c_bus_obtain(i2c_bus *bus)
 {
-  rtems_status_code sc;
+  int error;
 
-  sc = rtems_semaphore_obtain(bus->mutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
-  _Assert(sc == RTEMS_SUCCESSFUL);
-  (void) sc;
+  error = mtx_lock(&bus->mutex);
+  _Assert(error == thrd_success);
+  (void) error;
 }
 
 void i2c_bus_release(i2c_bus *bus)
 {
-  rtems_status_code sc;
+  int error;
 
-  sc = rtems_semaphore_release(bus->mutex);
-  _Assert(sc == RTEMS_SUCCESSFUL);
-  (void) sc;
+  error = mtx_unlock(&bus->mutex);
+  _Assert(error == thrd_success);
+  (void) error;
 }
 
 int i2c_bus_transfer(i2c_bus *bus, i2c_msg *msgs, uint32_t msg_count)
@@ -280,40 +280,25 @@ static int i2c_bus_set_clock_default(i2c_bus *bus, unsigned long clock)
   return -EIO;
 }
 
-static int i2c_bus_do_init(
+static void i2c_bus_do_init(
   i2c_bus *bus,
   void (*destroy)(i2c_bus *bus)
 )
 {
-  rtems_status_code sc;
-
-  sc = rtems_semaphore_create(
-    rtems_build_name('I', '2', 'C', ' '),
-    1,
-    RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
-    0,
-    &bus->mutex
-  );
-  if (sc != RTEMS_SUCCESSFUL) {
-    (*destroy)(bus);
-
-    rtems_set_errno_and_return_minus_one(ENOMEM);
-  }
+  int error;
 
   bus->transfer = i2c_bus_transfer_default;
   bus->set_clock = i2c_bus_set_clock_default;
   bus->destroy = destroy;
 
-  return 0;
+  error = mtx_init(&bus->mutex, mtx_recursive);
+  _Assert(error == thrd_success);
+  (void) error;
 }
 
 void i2c_bus_destroy(i2c_bus *bus)
 {
-  rtems_status_code sc;
-
-  sc = rtems_semaphore_delete(bus->mutex);
-  _Assert(sc == RTEMS_SUCCESSFUL);
-  (void) sc;
+  mtx_destroy(&bus->mutex);
 }
 
 void i2c_bus_destroy_and_free(i2c_bus *bus)
@@ -325,8 +310,8 @@ void i2c_bus_destroy_and_free(i2c_bus *bus)
 int i2c_bus_init(i2c_bus *bus)
 {
   memset(bus, 0, sizeof(*bus));
-
-  return i2c_bus_do_init(bus, i2c_bus_destroy);
+  i2c_bus_do_init(bus, i2c_bus_destroy);
+  return 0;
 }
 
 i2c_bus *i2c_bus_alloc_and_init(size_t size)
@@ -336,12 +321,7 @@ i2c_bus *i2c_bus_alloc_and_init(size_t size)
   if (size >= sizeof(*bus)) {
     bus = calloc(1, size);
     if (bus != NULL) {
-      int rv;
-
-      rv = i2c_bus_do_init(bus, i2c_bus_destroy_and_free);
-      if (rv != 0) {
-        return NULL;
-      }
+      i2c_bus_do_init(bus, i2c_bus_destroy_and_free);
     }
   }
 
diff --git a/cpukit/dev/include/dev/i2c/i2c.h b/cpukit/dev/include/dev/i2c/i2c.h
index 2ace4fc..6180a3f 100644
--- a/cpukit/dev/include/dev/i2c/i2c.h
+++ b/cpukit/dev/include/dev/i2c/i2c.h
@@ -32,6 +32,8 @@
 #include <sys/ioctl.h>
 #include <sys/stat.h>
 
+#include <threads.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif /* __cplusplus */
@@ -139,7 +141,7 @@ struct i2c_bus {
   /**
    * @brief Mutex to protect the bus access.
    */
-  rtems_id mutex;
+  mtx_t mutex;
 
   /**
    * @brief Default slave device address.
diff --git a/testsuites/libtests/i2c01/init.c b/testsuites/libtests/i2c01/init.c
index 0d6fc83..30e249d 100644
--- a/testsuites/libtests/i2c01/init.c
+++ b/testsuites/libtests/i2c01/init.c
@@ -659,8 +659,6 @@ static void Init(rtems_task_argument arg)
 
 #define CONFIGURE_MAXIMUM_TASKS 1
 
-#define CONFIGURE_MAXIMUM_SEMAPHORES 1
-
 #define CONFIGURE_INIT_TASK_STACK_SIZE (RTEMS_MINIMUM_STACK_SIZE + 2 * EEPROM_SIZE)
 
 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
-- 
1.8.4.5



More information about the devel mailing list