2017-08-18 02:52:54 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/sensors/hc_sr04.c
|
|
|
|
*
|
2021-03-13 10:55:07 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2017-08-18 02:52:54 +02:00
|
|
|
*
|
2021-03-13 10:55:07 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-08-18 02:52:54 +02:00
|
|
|
*
|
2021-03-13 10:55:07 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2017-08-18 02:52:54 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <sys/types.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2017-08-18 02:52:54 +02:00
|
|
|
#include <debug.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <errno.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
|
2017-08-18 02:52:54 +02:00
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/irq.h>
|
|
|
|
#include <nuttx/kmalloc.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2017-08-18 02:52:54 +02:00
|
|
|
#include <nuttx/random.h>
|
|
|
|
#include <nuttx/sensors/hc_sr04.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-Processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_HCSR04_DEBUG
|
|
|
|
# define hcsr04_dbg(x, ...) _info(x, ##__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
# define hcsr04_dbg(x, ...) sninfo(x, ##__VA_ARGS__)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
2020-03-30 23:06:15 +02:00
|
|
|
****************************************************************************/
|
2017-08-18 02:52:54 +02:00
|
|
|
|
|
|
|
static int hcsr04_open(FAR struct file *filep);
|
|
|
|
static int hcsr04_close(FAR struct file *filep);
|
|
|
|
static ssize_t hcsr04_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t hcsr04_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static int hcsr04_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
|
|
|
|
static int hcsr04_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct hcsr04_dev_s
|
|
|
|
{
|
|
|
|
FAR struct hcsr04_config_s *config;
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t devlock;
|
2017-08-18 17:44:38 +02:00
|
|
|
sem_t conv_donesem;
|
2017-08-18 02:52:54 +02:00
|
|
|
int time_start_pulse;
|
|
|
|
int time_finish_pulse;
|
|
|
|
volatile bool rising;
|
2024-08-25 01:21:12 +02:00
|
|
|
FAR struct pollfd *fds[CONFIG_HCSR04_NPOLLWAITERS];
|
2017-08-18 02:52:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
2019-12-07 02:07:34 +01:00
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
2017-08-18 02:52:54 +02:00
|
|
|
|
|
|
|
static const struct file_operations g_hcsr04ops =
|
|
|
|
{
|
|
|
|
hcsr04_open, /* open */
|
|
|
|
hcsr04_close, /* close */
|
|
|
|
hcsr04_read, /* read */
|
|
|
|
hcsr04_write, /* write */
|
|
|
|
NULL, /* seek */
|
2019-05-22 02:57:54 +02:00
|
|
|
hcsr04_ioctl, /* ioctl */
|
2023-01-02 14:02:51 +01:00
|
|
|
NULL, /* mmap */
|
2023-01-02 18:06:12 +01:00
|
|
|
NULL, /* truncate */
|
2019-05-22 02:57:54 +02:00
|
|
|
hcsr04_poll /* poll */
|
2017-08-18 02:52:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int hcsr04_read_distance(FAR struct hcsr04_dev_s *priv)
|
|
|
|
{
|
2017-08-18 17:44:38 +02:00
|
|
|
int done;
|
|
|
|
|
2020-05-17 15:56:21 +02:00
|
|
|
nxsem_get_value(&priv->conv_donesem, &done);
|
2017-08-18 17:44:38 +02:00
|
|
|
|
|
|
|
if (done == 0)
|
2017-08-18 02:52:54 +02:00
|
|
|
{
|
|
|
|
return (priv->time_finish_pulse - priv->time_start_pulse);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hcsr04_start_measuring(FAR struct hcsr04_dev_s *priv)
|
|
|
|
{
|
|
|
|
/* Configure the interruption */
|
|
|
|
|
|
|
|
priv->rising = true;
|
|
|
|
priv->config->irq_setmode(priv->config, priv->rising);
|
|
|
|
priv->config->irq_enable(priv->config, true);
|
|
|
|
|
|
|
|
/* Send to 10uS trigger pulse */
|
|
|
|
|
|
|
|
priv->config->set_trigger(priv->config, true);
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(10);
|
2017-08-18 02:52:54 +02:00
|
|
|
priv->config->set_trigger(priv->config, false);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hcsr04_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct hcsr04_dev_s *priv = inode->i_private;
|
2020-03-30 23:06:15 +02:00
|
|
|
int ret;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2020-03-30 23:06:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2017-08-18 02:52:54 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->devlock);
|
2017-08-18 02:52:54 +02:00
|
|
|
hcsr04_dbg("OPENED\n");
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hcsr04_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct hcsr04_dev_s *priv = inode->i_private;
|
2020-03-30 23:06:15 +02:00
|
|
|
int ret;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2020-03-30 23:06:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2017-08-18 02:52:54 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->devlock);
|
2017-08-18 02:52:54 +02:00
|
|
|
hcsr04_dbg("CLOSED\n");
|
2020-01-02 17:49:34 +01:00
|
|
|
return OK;
|
2017-08-18 02:52:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t hcsr04_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct hcsr04_dev_s *priv = inode->i_private;
|
|
|
|
int distance = 0;
|
|
|
|
ssize_t length = 0;
|
2020-03-30 23:06:15 +02:00
|
|
|
int ret;
|
2017-10-04 23:22:27 +02:00
|
|
|
|
|
|
|
/* Get exclusive access */
|
2017-08-18 02:52:54 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2020-03-30 23:06:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return (ssize_t)ret;
|
|
|
|
}
|
2017-08-18 02:52:54 +02:00
|
|
|
|
|
|
|
/* Setup and send a pulse to start measuring */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
hcsr04_start_measuring(priv);
|
2017-08-18 02:52:54 +02:00
|
|
|
|
2020-02-22 19:31:14 +01:00
|
|
|
/* Wait the conversion to finish */
|
2017-08-18 02:52:54 +02:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
/* Get exclusive access */
|
|
|
|
|
2020-03-30 23:06:15 +02:00
|
|
|
ret = nxsem_wait_uninterruptible(&priv->conv_donesem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return (ssize_t)ret;
|
|
|
|
}
|
2017-08-18 02:52:54 +02:00
|
|
|
|
|
|
|
distance = hcsr04_read_distance(priv);
|
|
|
|
if (distance < 0)
|
|
|
|
{
|
|
|
|
hcsr04_dbg("failed to read the distance\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This interface is mainly intended for easy debugging in nsh. */
|
|
|
|
|
|
|
|
length = snprintf(buffer, buflen, "%d\n", distance);
|
|
|
|
if (length > buflen)
|
|
|
|
{
|
|
|
|
length = buflen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->devlock);
|
2017-08-18 02:52:54 +02:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t hcsr04_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
ssize_t length = 0;
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hcsr04_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct hcsr04_dev_s *priv = inode->i_private;
|
2020-01-09 18:59:20 +01:00
|
|
|
int ret = OK;
|
2017-10-04 23:22:27 +02:00
|
|
|
|
|
|
|
/* Get exclusive access */
|
2017-08-18 02:52:54 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2020-03-30 23:06:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2017-08-18 02:52:54 +02:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case SNIOC_START_CONVERSION:
|
|
|
|
ret = hcsr04_start_measuring(priv);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SNIOC_READ_RAW_DATA:
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef CONFIG_HCSR04_DEBUG
|
|
|
|
case SNIOC_DUMP_REGS:
|
|
|
|
ret = hcsr04_dump_registers(priv);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->devlock);
|
2017-08-18 02:52:54 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool hcsr04_sample(FAR struct hcsr04_dev_s *priv)
|
|
|
|
{
|
2017-08-18 17:44:38 +02:00
|
|
|
int done;
|
|
|
|
|
2020-05-17 15:56:21 +02:00
|
|
|
nxsem_get_value(&priv->conv_donesem, &done);
|
2017-08-18 17:44:38 +02:00
|
|
|
|
|
|
|
return (done == 0);
|
2017-08-18 02:52:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int hcsr04_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct hcsr04_dev_s *priv;
|
|
|
|
uint32_t flags;
|
2020-01-02 17:49:34 +01:00
|
|
|
int ret = OK;
|
2017-10-04 23:22:27 +02:00
|
|
|
int i;
|
2017-08-18 02:52:54 +02:00
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(fds);
|
2017-08-18 02:52:54 +02:00
|
|
|
inode = filep->f_inode;
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
priv = inode->i_private;
|
2017-08-18 02:52:54 +02:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
/* Get exclusive access */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2020-03-30 23:06:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2017-08-18 02:52:54 +02:00
|
|
|
|
|
|
|
if (setup)
|
|
|
|
{
|
|
|
|
/* Ignore waits that do not include POLLIN */
|
|
|
|
|
|
|
|
if ((fds->events & POLLIN) == 0)
|
|
|
|
{
|
|
|
|
ret = -EDEADLK;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is a request to set up the poll. Find an available slot for
|
|
|
|
* the poll structure reference.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_HCSR04_NPOLLWAITERS; i++)
|
|
|
|
{
|
|
|
|
/* Find an available slot */
|
|
|
|
|
|
|
|
if (!priv->fds[i])
|
|
|
|
{
|
|
|
|
/* Bind the poll structure and this slot */
|
|
|
|
|
|
|
|
priv->fds[i] = fds;
|
|
|
|
fds->priv = &priv->fds[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= CONFIG_HCSR04_NPOLLWAITERS)
|
|
|
|
{
|
|
|
|
fds->priv = NULL;
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
if (hcsr04_sample(priv))
|
|
|
|
{
|
2023-11-19 12:19:53 +01:00
|
|
|
poll_notify(&fds, 1, POLLIN);
|
2017-08-18 02:52:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
}
|
|
|
|
else if (fds->priv)
|
|
|
|
{
|
|
|
|
/* This is a request to tear down the poll. */
|
|
|
|
|
2024-08-25 01:21:12 +02:00
|
|
|
FAR struct pollfd **slot = (FAR struct pollfd **)fds->priv;
|
2017-08-18 02:52:54 +02:00
|
|
|
DEBUGASSERT(slot != NULL);
|
|
|
|
|
|
|
|
/* Remove all memory of the poll setup */
|
|
|
|
|
|
|
|
*slot = NULL;
|
|
|
|
fds->priv = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->devlock);
|
2017-08-18 02:52:54 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hcsr04_int_handler(int irq, FAR void *context, FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct hcsr04_dev_s *priv = (FAR struct hcsr04_dev_s *)arg;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL);
|
|
|
|
|
|
|
|
/* Is this the start of the pulse used to encode the distance ? */
|
|
|
|
|
|
|
|
if (priv->rising)
|
|
|
|
{
|
|
|
|
/* Get the clock ticks from the free running timer */
|
|
|
|
|
|
|
|
priv->time_start_pulse = priv->config->get_clock(priv->config);
|
|
|
|
|
|
|
|
/* Now we need to wait for the falling edge interruption */
|
|
|
|
|
|
|
|
priv->rising = false;
|
|
|
|
priv->config->irq_setmode(priv->config, priv->rising);
|
|
|
|
priv->config->irq_enable(priv->config, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Get the clock ticks from the free running timer */
|
|
|
|
|
|
|
|
priv->time_finish_pulse = priv->config->get_clock(priv->config);
|
|
|
|
|
|
|
|
/* Disable interruptions */
|
|
|
|
|
|
|
|
priv->config->irq_enable(priv->config, false);
|
|
|
|
|
2020-02-22 19:31:14 +01:00
|
|
|
/* Conversion is done */
|
2017-08-18 02:52:54 +02:00
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->conv_donesem);
|
2017-08-18 02:52:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
hcsr04_dbg("HC-SR04 interrupt\n");
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(priv->fds, CONFIG_HCSR04_NPOLLWAITERS, POLLIN);
|
2017-08-18 02:52:54 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int hcsr04_register(FAR const char *devpath,
|
|
|
|
FAR struct hcsr04_config_s *config)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
FAR struct hcsr04_dev_s *priv;
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
priv = kmm_zalloc(sizeof(struct hcsr04_dev_s));
|
2017-08-18 02:52:54 +02:00
|
|
|
|
|
|
|
if (!priv)
|
|
|
|
{
|
|
|
|
hcsr04_dbg("Memory cannot be allocated for HC-SR04 sensor");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->config = config;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&priv->devlock);
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&priv->conv_donesem, 0, 0);
|
2017-08-18 02:52:54 +02:00
|
|
|
|
|
|
|
ret = register_driver(devpath, &g_hcsr04ops, 0666, priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-10-16 18:50:59 +02:00
|
|
|
nxmutex_destroy(&priv->devlock);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxsem_destroy(&priv->conv_donesem);
|
2017-08-18 02:52:54 +02:00
|
|
|
kmm_free(priv);
|
|
|
|
hcsr04_dbg("Error occurred during the driver registering = %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->config->irq_clear)
|
|
|
|
{
|
|
|
|
priv->config->irq_clear(priv->config);
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->config->irq_attach(priv->config, hcsr04_int_handler, priv);
|
|
|
|
priv->config->irq_enable(priv->config, false);
|
|
|
|
return OK;
|
|
|
|
}
|