From d9b6d6aff8b3581546319dfeef8e2b7159e0cb85 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sun, 24 Jan 2016 09:15:01 -0600 Subject: [PATCH] Flesh out unfinished lock() method implementations in al SPI drivers --- configs/mcu123-lpc214x/src/lpc2148_spi1.c | 27 ++++++++++++++++--- configs/olimex-strp711/src/str71_spi.c | 33 +++++++++++++++++++---- configs/zp214xpa/src/lpc2148_spi1.c | 27 ++++++++++++++++--- 3 files changed, 74 insertions(+), 13 deletions(-) diff --git a/configs/mcu123-lpc214x/src/lpc2148_spi1.c b/configs/mcu123-lpc214x/src/lpc2148_spi1.c index ef50d9577e..674629368b 100644 --- a/configs/mcu123-lpc214x/src/lpc2148_spi1.c +++ b/configs/mcu123-lpc214x/src/lpc2148_spi1.c @@ -1,7 +1,7 @@ /**************************************************************************** * config/mcu123-lpc214x/src/lpc2148_spi1.c * - * Copyright (C) 2008-2010, 2012 Gregory Nutt. All rights reserved. + * Copyright (C) 2008-2010, 2012, 2016 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -65,6 +65,8 @@ #include #include #include +#include +#include #include #include @@ -149,7 +151,8 @@ static const struct spi_ops_s g_spiops = .registercallback = 0, /* Not implemented */ }; -static struct spi_dev_s g_spidev = { &g_spiops }; +static struct spi_dev_s g_spidev = {&g_spiops}; +static sem_t g_exclsem = SEM_INITIALIZER(1); /* For mutually exclusive access */ /**************************************************************************** * Public Data @@ -182,9 +185,25 @@ static struct spi_dev_s g_spidev = { &g_spiops }; static int spi_lock(FAR struct spi_dev_s *dev, bool lock) { - /* Not implemented */ + if (lock) + { + /* Take the semaphore (perhaps waiting) */ - return -ENOSYS; + while (sem_wait(&g_exclsem) != 0) + { + /* The only case that an error should occur here is if the wait + * was awakened by a signal. + */ + + DEBUGASSERT(errno == EINTR); + } + } + else + { + (void)sem_post(&g_exclsem); + } + + return OK; } /**************************************************************************** diff --git a/configs/olimex-strp711/src/str71_spi.c b/configs/olimex-strp711/src/str71_spi.c index da9a9c2ca8..106199127f 100644 --- a/configs/olimex-strp711/src/str71_spi.c +++ b/configs/olimex-strp711/src/str71_spi.c @@ -1,7 +1,7 @@ /**************************************************************************** * config/olimex-strp711/src/str71_spi.c * - * Copyright (C) 2008-2010,2012 Gregory Nutt. All rights reserved. + * Copyright (C) 2008-2010, 2012, 2016 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -42,6 +42,8 @@ #include #include #include +#include +#include #include #include @@ -381,6 +383,7 @@ struct str71x_spidev_s bool initialized; /* Initialize port only once! */ uint32_t spibase; /* BSPIn base address */ uint16_t csbit; /* BSPIn SS bit int GPIO0 */ + sem_t exclsem; /* Supports mutually exclusive access */ }; /**************************************************************************** @@ -430,7 +433,8 @@ static struct str71x_spidev_s g_spidev0 = { .spidev = { &g_spiops }, .spibase = STR71X_BSPI0_BASE, - .csbit = ENC_GPIO0_CS + .csbit = ENC_GPIO0_CS, + .exclsem = SEM_INITIALIZER(1) }; #endif @@ -439,7 +443,8 @@ static struct str71x_spidev_s g_spidev1 = { .spidev = { &g_spiops }, .spibase = STR71X_BSPI1_BASE, - .csbit = MMCSD_GPIO0_CS + .csbit = MMCSD_GPIO0_CS, + .exclsem = SEM_INITIALIZER(1) }; #endif @@ -561,9 +566,27 @@ static inline void spi_drain(FAR struct str71x_spidev_s *priv) static int spi_lock(FAR struct spi_dev_s *dev, bool lock) { - /* Not implemented */ + FAR struct str71x_spidev_s *priv = (FAR struct str71x_spidev_s *)dev; - return -ENOSYS; + if (lock) + { + /* Take the semaphore (perhaps waiting) */ + + while (sem_wait(&priv->exclsem) != 0) + { + /* The only case that an error should occur here is if the wait + * was awakened by a signal. + */ + + DEBUGASSERT(errno == EINTR); + } + } + else + { + (void)sem_post(&priv->exclsem); + } + + return OK; } /**************************************************************************** diff --git a/configs/zp214xpa/src/lpc2148_spi1.c b/configs/zp214xpa/src/lpc2148_spi1.c index 970cb93fec..f3913f1725 100644 --- a/configs/zp214xpa/src/lpc2148_spi1.c +++ b/configs/zp214xpa/src/lpc2148_spi1.c @@ -1,7 +1,7 @@ /**************************************************************************** * config/zp214xpa/src/lpc2148_spi1.c * - * Copyright (C) 2008-2010, 2012 Gregory Nutt. All rights reserved. + * Copyright (C) 2008-2010, 2012, 2016 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -66,6 +66,8 @@ #include #include #include +#include +#include #include #include @@ -152,7 +154,8 @@ static const struct spi_ops_s g_spiops = .registercallback = 0, /* Not implemented */ }; -static struct spi_dev_s g_spidev = { &g_spiops }; +static struct spi_dev_s g_spidev = {&g_spiops}; +static sem_t g_exclsem = SEM_INITIALIZER(1); /* For mutually exclusive access */ /**************************************************************************** * Public Data @@ -185,9 +188,25 @@ static struct spi_dev_s g_spidev = { &g_spiops }; static int spi_lock(FAR struct spi_dev_s *dev, bool lock) { - /* Not implemented -- the UG_2864AMBAG01 is the only device on SPI1 */ + if (lock) + { + /* Take the semaphore (perhaps waiting) */ - return -ENOSYS; + while (sem_wait(&g_exclsem) != 0) + { + /* The only case that an error should occur here is if the wait + * was awakened by a signal. + */ + + DEBUGASSERT(errno == EINTR); + } + } + else + { + (void)sem_post(&g_exclsem); + } + + return OK; } /****************************************************************************