2021-07-14 14:41:25 +02:00
|
|
|
/****************************************************************************
|
2023-06-15 05:13:24 +02:00
|
|
|
* drivers/math/math.c
|
2021-07-14 14:41:25 +02: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
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/irq.h>
|
|
|
|
#include <nuttx/kmalloc.h>
|
2023-06-15 05:13:24 +02:00
|
|
|
#include <nuttx/math/math.h>
|
2021-07-14 14:41:25 +02:00
|
|
|
#include <nuttx/math/math_ioctl.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Type Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This structure describes the state of the upper half driver */
|
|
|
|
|
2023-06-15 05:13:24 +02:00
|
|
|
typedef struct math_config_s math_upperhalf_s;
|
2021-07-14 14:41:25 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-06-15 05:13:24 +02:00
|
|
|
static ssize_t math_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t math_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static int math_ioctl(FAR struct file *filep, int cmd,
|
|
|
|
unsigned long arg);
|
2021-07-14 14:41:25 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-06-15 05:13:24 +02:00
|
|
|
static const struct file_operations g_math_ops =
|
2021-07-14 14:41:25 +02:00
|
|
|
{
|
2022-04-12 12:16:13 +02:00
|
|
|
NULL, /* open */
|
|
|
|
NULL, /* close */
|
2023-06-15 05:13:24 +02:00
|
|
|
math_read, /* read */
|
|
|
|
math_write, /* write */
|
2021-07-14 14:41:25 +02:00
|
|
|
NULL, /* seek */
|
2023-06-15 05:13:24 +02:00
|
|
|
math_ioctl, /* ioctl */
|
2021-07-14 14:41:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-06-15 05:13:24 +02:00
|
|
|
* Name: math_read
|
2021-07-14 14:41:25 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* A dummy read method. This is provided only to satisfy the VFS layer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-06-15 05:13:24 +02:00
|
|
|
static ssize_t math_read(FAR struct file *filep, FAR char *buffer,
|
2021-07-14 14:41:25 +02:00
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
/* Return zero -- usually meaning end-of-file */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-06-15 05:13:24 +02:00
|
|
|
* Name: math_write
|
2021-07-14 14:41:25 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* A dummy write method. This is provided only to satisfy the VFS layer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-06-15 05:13:24 +02:00
|
|
|
static ssize_t math_write(FAR struct file *filep, FAR const char *buffer,
|
2021-07-14 14:41:25 +02:00
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-06-15 05:13:24 +02:00
|
|
|
* Name: math_ioctl
|
2021-07-14 14:41:25 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2023-06-15 05:13:24 +02:00
|
|
|
* The standard ioctl method. This is where ALL of the math timer
|
2021-07-14 14:41:25 +02:00
|
|
|
* work is done.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-06-15 05:13:24 +02:00
|
|
|
static int math_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
2021-07-14 14:41:25 +02:00
|
|
|
{
|
2023-06-15 05:13:24 +02:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR math_upperhalf_s *upper = inode->i_private;
|
|
|
|
int ret = -ENOTTY;
|
2021-07-14 14:41:25 +02:00
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
DEBUGASSERT(upper != NULL);
|
2023-06-15 05:13:24 +02:00
|
|
|
|
|
|
|
_info("cmd: %d arg: %lu\n", cmd, arg);
|
2021-07-14 14:41:25 +02:00
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* CORDIC calulcate */
|
|
|
|
|
2023-07-05 09:02:09 +02:00
|
|
|
#ifdef CONFIG_MATH_CORDIC
|
2021-07-14 14:41:25 +02:00
|
|
|
case MATHIOC_CORDIC_CALC:
|
|
|
|
{
|
|
|
|
FAR struct cordic_calc_s *calc =
|
|
|
|
(FAR struct cordic_calc_s *)((uintptr_t)arg);
|
|
|
|
|
2023-06-15 05:13:24 +02:00
|
|
|
if (upper->cordic != NULL)
|
|
|
|
{
|
|
|
|
ret = upper->cordic->ops->calc(upper->cordic, calc);
|
|
|
|
}
|
2021-07-14 14:41:25 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2023-07-05 09:02:09 +02:00
|
|
|
#endif
|
2023-06-30 14:06:53 +02:00
|
|
|
|
2023-07-05 09:02:09 +02:00
|
|
|
#ifdef CONFIG_MATH_FFT
|
2023-06-30 14:06:53 +02:00
|
|
|
case MATHIOC_FFT_CALC:
|
|
|
|
{
|
|
|
|
FAR struct fft_calc_s *calc =
|
|
|
|
(FAR struct fft_calc_s *)((uintptr_t)arg);
|
|
|
|
|
|
|
|
if (upper->fft != NULL)
|
|
|
|
{
|
|
|
|
ret = upper->fft->ops->calc(upper->fft, calc);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2023-07-05 09:02:09 +02:00
|
|
|
#endif
|
2023-09-04 04:35:11 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_MATH_MPI
|
|
|
|
case MATHIOC_MPI_CALC:
|
|
|
|
{
|
|
|
|
FAR struct mpi_calc_s *calc =
|
|
|
|
(FAR struct mpi_calc_s *)((uintptr_t)arg);
|
|
|
|
|
|
|
|
if (upper->mpi != NULL)
|
|
|
|
{
|
|
|
|
ret = upper->mpi->ops->calc(upper->mpi, calc);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2021-07-14 14:41:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-06-15 05:13:24 +02:00
|
|
|
* Name: math_register
|
2021-07-14 14:41:25 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2023-06-15 05:13:24 +02:00
|
|
|
* Register a math driver.
|
2021-07-14 14:41:25 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-06-15 05:13:24 +02:00
|
|
|
int math_register(FAR const char *path,
|
|
|
|
FAR const struct math_config_s *config)
|
2021-07-14 14:41:25 +02:00
|
|
|
{
|
2023-06-15 05:13:24 +02:00
|
|
|
FAR math_upperhalf_s *upper = NULL;
|
|
|
|
int ret = -ENOMEM;
|
2021-07-14 14:41:25 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(path);
|
2023-06-15 05:13:24 +02:00
|
|
|
DEBUGASSERT(config);
|
2021-07-14 14:41:25 +02:00
|
|
|
|
|
|
|
/* Allocate the upper-half data structure */
|
|
|
|
|
2023-06-15 05:13:24 +02:00
|
|
|
upper = (FAR math_upperhalf_s *)
|
|
|
|
kmm_malloc(sizeof(math_upperhalf_s));
|
2021-07-14 14:41:25 +02:00
|
|
|
if (!upper)
|
|
|
|
{
|
|
|
|
_err("Upper half allocation failed\n");
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2023-06-15 05:13:24 +02:00
|
|
|
/* Initialize the math device structure */
|
2021-07-14 14:41:25 +02:00
|
|
|
|
2023-06-15 05:13:24 +02:00
|
|
|
memcpy(upper, config, sizeof(math_upperhalf_s));
|
2021-07-14 14:41:25 +02:00
|
|
|
|
2023-06-15 05:13:24 +02:00
|
|
|
/* Register the math timer device */
|
2021-07-14 14:41:25 +02:00
|
|
|
|
2023-06-15 05:13:24 +02:00
|
|
|
ret = register_driver(path, &g_math_ops, 0666, upper);
|
2021-07-14 14:41:25 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-06-15 05:13:24 +02:00
|
|
|
_err("register math driver failed: %d\n", ret);
|
2021-07-14 14:41:25 +02:00
|
|
|
kmm_free(upper);
|
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
2023-06-15 05:13:24 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cordic_register
|
|
|
|
*
|
|
|
|
* Description:
|
2023-06-30 14:06:53 +02:00
|
|
|
* Register a CORDIC driver.
|
2023-06-15 05:13:24 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-07-05 09:02:09 +02:00
|
|
|
#ifdef CONFIG_MATH_CORDIC
|
2023-06-15 05:13:24 +02:00
|
|
|
int cordic_register(FAR const char *path,
|
|
|
|
FAR struct cordic_lowerhalf_s *lower)
|
|
|
|
{
|
|
|
|
struct math_config_s config;
|
|
|
|
|
|
|
|
memset(&config, 0, sizeof(config));
|
|
|
|
config.cordic = lower;
|
|
|
|
|
|
|
|
return math_register(path, &config);
|
|
|
|
}
|
2023-07-05 09:02:09 +02:00
|
|
|
#endif
|
2023-06-30 14:06:53 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fft_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register a FFT driver.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-07-05 09:02:09 +02:00
|
|
|
#ifdef CONFIG_MATH_FFT
|
2023-06-30 14:06:53 +02:00
|
|
|
int fft_register(FAR const char *path,
|
|
|
|
FAR struct fft_lowerhalf_s *lower)
|
|
|
|
{
|
|
|
|
struct math_config_s config;
|
|
|
|
|
|
|
|
memset(&config, 0, sizeof(config));
|
|
|
|
config.fft = lower;
|
|
|
|
|
|
|
|
return math_register(path, &config);
|
|
|
|
}
|
2023-07-05 09:02:09 +02:00
|
|
|
#endif
|
2023-09-04 04:35:11 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mpi_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register a MPI driver.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_MATH_MPI
|
|
|
|
int mpi_register(FAR const char *path,
|
|
|
|
FAR struct mpi_lowerhalf_s *lower)
|
|
|
|
{
|
|
|
|
struct math_config_s config;
|
|
|
|
|
|
|
|
memset(&config, 0, sizeof(config));
|
|
|
|
config.mpi = lower;
|
|
|
|
|
|
|
|
return math_register(path, &config);
|
|
|
|
}
|
|
|
|
#endif
|