expand cordic_register to math_register

Signed-off-by: xiajizhong <xiajizhong@xiaomi.com>
This commit is contained in:
xiajizhong 2023-06-15 11:13:24 +08:00 committed by Xiang Xiao
parent d8b045fa6a
commit f922f4cc61
3 changed files with 122 additions and 67 deletions

View File

@ -21,7 +21,7 @@
# Include CORDIC support # Include CORDIC support
ifeq ($(CONFIG_MATH_CORDIC),y) ifeq ($(CONFIG_MATH_CORDIC),y)
CSRCS += cordic.c CSRCS += math.c
endif endif
# Include build support # Include build support

View File

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* drivers/math/cordic.c * drivers/math/math.c
* *
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
@ -22,24 +22,13 @@
* Included Files * Included Files
****************************************************************************/ ****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <debug.h> #include <debug.h>
#include <nuttx/fs/fs.h> #include <nuttx/fs/fs.h>
#include <nuttx/irq.h> #include <nuttx/irq.h>
#include <nuttx/kmalloc.h> #include <nuttx/kmalloc.h>
#include <nuttx/semaphore.h> #include <nuttx/math/math.h>
#include <nuttx/math/math_ioctl.h> #include <nuttx/math/math_ioctl.h>
#include <nuttx/math/cordic.h>
/**************************************************************************** /****************************************************************************
* Pre-processor Definitions * Pre-processor Definitions
@ -51,34 +40,31 @@
/* This structure describes the state of the upper half driver */ /* This structure describes the state of the upper half driver */
struct cordic_upperhalf_s typedef struct math_config_s math_upperhalf_s;
{
FAR struct cordic_lowerhalf_s *lower;
};
/**************************************************************************** /****************************************************************************
* Private Function Prototypes * Private Function Prototypes
****************************************************************************/ ****************************************************************************/
static ssize_t cordic_read(FAR struct file *filep, FAR char *buffer, static ssize_t math_read(FAR struct file *filep, FAR char *buffer,
size_t buflen); size_t buflen);
static ssize_t cordic_write(FAR struct file *filep, FAR const char *buffer, static ssize_t math_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen); size_t buflen);
static int cordic_ioctl(FAR struct file *filep, int cmd, static int math_ioctl(FAR struct file *filep, int cmd,
unsigned long arg); unsigned long arg);
/**************************************************************************** /****************************************************************************
* Private Data * Private Data
****************************************************************************/ ****************************************************************************/
static const struct file_operations g_cordicops = static const struct file_operations g_math_ops =
{ {
NULL, /* open */ NULL, /* open */
NULL, /* close */ NULL, /* close */
cordic_read, /* read */ math_read, /* read */
cordic_write, /* write */ math_write, /* write */
NULL, /* seek */ NULL, /* seek */
cordic_ioctl, /* ioctl */ math_ioctl, /* ioctl */
}; };
/**************************************************************************** /****************************************************************************
@ -86,14 +72,14 @@ static const struct file_operations g_cordicops =
****************************************************************************/ ****************************************************************************/
/**************************************************************************** /****************************************************************************
* Name: cordic_read * Name: math_read
* *
* Description: * Description:
* A dummy read method. This is provided only to satisfy the VFS layer. * A dummy read method. This is provided only to satisfy the VFS layer.
* *
****************************************************************************/ ****************************************************************************/
static ssize_t cordic_read(FAR struct file *filep, FAR char *buffer, static ssize_t math_read(FAR struct file *filep, FAR char *buffer,
size_t buflen) size_t buflen)
{ {
/* Return zero -- usually meaning end-of-file */ /* Return zero -- usually meaning end-of-file */
@ -102,41 +88,38 @@ static ssize_t cordic_read(FAR struct file *filep, FAR char *buffer,
} }
/**************************************************************************** /****************************************************************************
* Name: cordic_write * Name: math_write
* *
* Description: * Description:
* A dummy write method. This is provided only to satisfy the VFS layer. * A dummy write method. This is provided only to satisfy the VFS layer.
* *
****************************************************************************/ ****************************************************************************/
static ssize_t cordic_write(FAR struct file *filep, FAR const char *buffer, static ssize_t math_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen) size_t buflen)
{ {
return 0; return 0;
} }
/**************************************************************************** /****************************************************************************
* Name: cordic_ioctl * Name: math_ioctl
* *
* Description: * Description:
* The standard ioctl method. This is where ALL of the cordic timer * The standard ioctl method. This is where ALL of the math timer
* work is done. * work is done.
* *
****************************************************************************/ ****************************************************************************/
static int cordic_ioctl(FAR struct file *filep, int cmd, unsigned long arg) static int math_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{ {
FAR struct inode *inode = filep->f_inode; FAR struct inode *inode = filep->f_inode;
FAR struct cordic_upperhalf_s *upper = NULL; FAR math_upperhalf_s *upper = inode->i_private;
FAR struct cordic_lowerhalf_s *lower = NULL; int ret = -ENOTTY;
int ret = 0;
irqstate_t flags; irqstate_t flags;
_info("cmd: %d arg: %lu\n", cmd, arg);
upper = inode->i_private;
DEBUGASSERT(upper != NULL); DEBUGASSERT(upper != NULL);
lower = upper->lower;
DEBUGASSERT(lower != NULL); _info("cmd: %d arg: %lu\n", cmd, arg);
flags = enter_critical_section(); flags = enter_critical_section();
@ -149,16 +132,10 @@ static int cordic_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
FAR struct cordic_calc_s *calc = FAR struct cordic_calc_s *calc =
(FAR struct cordic_calc_s *)((uintptr_t)arg); (FAR struct cordic_calc_s *)((uintptr_t)arg);
ret = lower->ops->calc(lower, calc); if (upper->cordic != NULL)
{
break; ret = upper->cordic->ops->calc(upper->cordic, calc);
} }
/* Not supported */
default:
{
ret = -ENOTTY;
break; break;
} }
@ -174,45 +151,64 @@ static int cordic_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
****************************************************************************/ ****************************************************************************/
/**************************************************************************** /****************************************************************************
* Name: cordic_register * Name: math_register
* *
* Description: * Description:
* Register a CORDIC driver. * Register a math driver.
* *
****************************************************************************/ ****************************************************************************/
int cordic_register(FAR const char *path, int math_register(FAR const char *path,
FAR struct cordic_lowerhalf_s *lower) FAR const struct math_config_s *config)
{ {
FAR struct cordic_upperhalf_s *upper = NULL; FAR math_upperhalf_s *upper = NULL;
int ret = OK; int ret = -ENOMEM;
DEBUGASSERT(path); DEBUGASSERT(path);
DEBUGASSERT(lower); DEBUGASSERT(config);
/* Allocate the upper-half data structure */ /* Allocate the upper-half data structure */
upper = (FAR struct cordic_upperhalf_s *) upper = (FAR math_upperhalf_s *)
kmm_zalloc(sizeof(struct cordic_upperhalf_s)); kmm_malloc(sizeof(math_upperhalf_s));
if (!upper) if (!upper)
{ {
_err("Upper half allocation failed\n"); _err("Upper half allocation failed\n");
goto errout; goto errout;
} }
/* Initialize the CORDIC device structure */ /* Initialize the math device structure */
upper->lower = lower; memcpy(upper, config, sizeof(math_upperhalf_s));
/* Register the cordic timer device */ /* Register the math timer device */
ret = register_driver(path, &g_cordicops, 0666, upper); ret = register_driver(path, &g_math_ops, 0666, upper);
if (ret < 0) if (ret < 0)
{ {
_err("register_driver failed: %d\n", ret); _err("register math driver failed: %d\n", ret);
kmm_free(upper); kmm_free(upper);
} }
errout: errout:
return ret; return ret;
} }
/****************************************************************************
* Name: cordic_register
*
* Description:
* Register a CORDIC driver. Deprecated, use math_register instead!
*
****************************************************************************/
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);
}

59
include/nuttx/math/math.h Normal file
View File

@ -0,0 +1,59 @@
/****************************************************************************
* include/nuttx/math/math.h
*
* 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.
*
****************************************************************************/
#ifndef __INCLUDE_NUTTX_MATH_MATH_H
#define __INCLUDE_NUTTX_MATH_MATH_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/math/cordic.h>
/****************************************************************************
* Public Types
****************************************************************************/
/* This math config struct provides the all "lower half" of math drivers.
* Each will have an internal structure definition that will be
* cast-compatible with this structure definitions.
*/
struct math_config_s
{
FAR struct cordic_lowerhalf_s *cordic;
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: math_register
*
* Description:
* Register all math related drivers.
*
****************************************************************************/
int math_register(FAR const char *path,
FAR const struct math_config_s *config);
#endif /* __INCLUDE_NUTTX_MATH_MATH_H */