add upper-half CORDIC driver

This commit is contained in:
raiden00pl 2021-07-14 14:41:25 +02:00 committed by Alan Carvalho de Assis
parent 6128b298ee
commit 919008c51e
8 changed files with 549 additions and 3 deletions

View File

@ -131,3 +131,4 @@ source "drivers/platform/Kconfig"
source "drivers/rf/Kconfig"
source "drivers/rc/Kconfig"
source "drivers/motor/Kconfig"
source "drivers/math/Kconfig"

View File

@ -29,6 +29,7 @@ include audio/Make.defs
include bch/Make.defs
include can/Make.defs
include crypto/Make.defs
include math/Make.defs
include motor/Make.defs
include i2c/Make.defs
include i2s/Make.defs

28
drivers/math/Kconfig Normal file
View File

@ -0,0 +1,28 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
menu "MATH Acceleration Support"
config MATH_CORDIC
bool "MATH CORDIC support"
default n
---help---
This selection enables building of the "upper-half" CORDIC driver.
See include/nuttx/math/cordic.h for further CORDIC driver information.
if MATH_CORDIC
choice
prompt "MATH CORDIC numeric type"
default MATH_CORDIC_USE_Q31
config MATH_CORDIC_USE_Q31
bool "CORDIC uses Q1.31"
endchoice
endif
endmenu # MATH Acceleration Information

30
drivers/math/Make.defs Normal file
View File

@ -0,0 +1,30 @@
############################################################################
# drivers/math/Make.defs
#
# 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.
#
############################################################################
# Include CORDIC support
ifeq ($(CONFIG_MATH_CORDIC),y)
CSRCS += cordic.c
endif
# Include build support
DEPPATH += --dep-path math
VPATH += :math

247
drivers/math/cordic.c Normal file
View File

@ -0,0 +1,247 @@
/****************************************************************************
* drivers/math/cordic.c
*
* 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 <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 <nuttx/fs/fs.h>
#include <nuttx/irq.h>
#include <nuttx/kmalloc.h>
#include <nuttx/semaphore.h>
#include <nuttx/math/math_ioctl.h>
#include <nuttx/math/cordic.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Type Definitions
****************************************************************************/
/* This structure describes the state of the upper half driver */
struct cordic_upperhalf_s
{
FAR struct cordic_lowerhalf_s *lower;
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static int cordic_open(FAR struct file *filep);
static int cordic_close(FAR struct file *filep);
static ssize_t cordic_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
static ssize_t cordic_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen);
static int cordic_ioctl(FAR struct file *filep, int cmd,
unsigned long arg);
/****************************************************************************
* Private Data
****************************************************************************/
static const struct file_operations g_cordicops =
{
cordic_open, /* open */
cordic_close, /* close */
cordic_read, /* read */
cordic_write, /* write */
NULL, /* seek */
cordic_ioctl, /* ioctl */
NULL /* poll */
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: cordic_open
*
* Description:
* This function is called whenever the cordic timer device is opened.
*
****************************************************************************/
static int cordic_open(FAR struct file *filep)
{
return OK;
}
/****************************************************************************
* Name: cordic_close
*
* Description:
* This function is called when the cordic timer device is closed.
*
****************************************************************************/
static int cordic_close(FAR struct file *filep)
{
return OK;
}
/****************************************************************************
* Name: cordic_read
*
* Description:
* 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,
size_t buflen)
{
/* Return zero -- usually meaning end-of-file */
return 0;
}
/****************************************************************************
* Name: cordic_write
*
* Description:
* 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,
size_t buflen)
{
return 0;
}
/****************************************************************************
* Name: cordic_ioctl
*
* Description:
* The standard ioctl method. This is where ALL of the cordic timer
* work is done.
*
****************************************************************************/
static int cordic_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{
FAR struct inode *inode = filep->f_inode;
FAR struct cordic_upperhalf_s *upper = NULL;
FAR struct cordic_lowerhalf_s *lower = NULL;
int ret = 0;
irqstate_t flags;
_info("cmd: %d arg: %lu\n", cmd, arg);
upper = inode->i_private;
DEBUGASSERT(upper != NULL);
lower = upper->lower;
DEBUGASSERT(lower != NULL);
flags = enter_critical_section();
switch (cmd)
{
/* CORDIC calulcate */
case MATHIOC_CORDIC_CALC:
{
FAR struct cordic_calc_s *calc =
(FAR struct cordic_calc_s *)((uintptr_t)arg);
ret = lower->ops->calc(lower, calc);
break;
}
/* Not supported */
default:
{
ret = -ENOTTY;
break;
}
}
leave_critical_section(flags);
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: cordic_register
*
* Description:
* Register a CORDIC driver.
*
****************************************************************************/
int cordic_register(FAR const char *path,
FAR struct cordic_lowerhalf_s *lower)
{
FAR struct cordic_upperhalf_s *upper = NULL;
int ret = OK;
DEBUGASSERT(path);
DEBUGASSERT(lower);
/* Allocate the upper-half data structure */
upper = (FAR struct cordic_upperhalf_s *)
kmm_zalloc(sizeof(struct cordic_upperhalf_s));
if (!upper)
{
_err("Upper half allocation failed\n");
goto errout;
}
/* Initialize the CORDIC device structure */
upper->lower = lower;
/* Register the cordic timer device */
ret = register_driver(path, &g_cordicops, 0666, upper);
if (ret < 0)
{
_err("register_driver failed: %d\n", ret);
kmm_free(upper);
}
errout:
return ret;
}

View File

@ -84,9 +84,10 @@
#define _NOTECTLBASE (0x2c00) /* Note filter control ioctl commands*/
#define _NOTERAMBASE (0x2d00) /* Noteram device ioctl commands*/
#define _RCIOCBASE (0x2e00) /* Remote Control device ioctl commands */
#define _HIMEMBASE (0x2f00) /* Himem device ioctl commands*/
#define _EFUSEBASE (0x3000) /* Efuse device ioctl commands*/
#define _MTRIOBASE (0x3100) /* Motor device ioctl commands*/
#define _HIMEMBASE (0x2f00) /* Himem device ioctl commands */
#define _EFUSEBASE (0x3000) /* Efuse device ioctl commands */
#define _MTRIOBASE (0x3100) /* Motor device ioctl commands */
#define _MATHIOBASE (0x3200) /* MATH device ioctl commands */
#define _WLIOCBASE (0x8b00) /* Wireless modules ioctl network commands */
/* boardctl() commands share the same number space */
@ -550,6 +551,11 @@
#define _MTRIOCVALID(c) (_IOC_TYPE(c) == _MTRIOBASE)
#define _MTRIOC(nr) _IOC(_MTRIOBASE, nr)
/* MATH drivers *************************************************************/
#define _MATHIOCVALID(c) (_IOC_TYPE(c) == _MATHIOBASE)
#define _MATHIOC(nr) _IOC(_MATHIOBASE, nr)
/* Wireless driver network ioctl definitions ********************************/
/* (see nuttx/include/wireless/wireless.h */

190
include/nuttx/math/cordic.h Normal file
View File

@ -0,0 +1,190 @@
/****************************************************************************
* include/nuttx/math/cordic.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_CORDIC_H
#define __INCLUDE_NUTTX_MATH_CORDIC_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/compiler.h>
#include <fixedmath.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifdef CONFIG_MATH_CORDIC_USE_Q31
/* q1.31 conversion */
# define ftoq31(f32_val) ((f32_val) * 0x80000000)
# define q31tof(q31_val) ((float)(q31_val) / (float)0x80000000)
# define b16toq31(b16_val) ((b16_val) * 0x8000)
# define q31tob16(q31_val) ((q31_val) / 0x8000)
#endif
/****************************************************************************
* Public Types
****************************************************************************/
#ifdef CONFIG_MATH_CORDIC_USE_Q31
/* q1.31 as int32_t */
typedef int32_t cordic_num_t;
#endif
/* CORDIC functions */
enum cordic_calc_func_e
{
CORDIC_CALC_FUNC_INVAL = 0,
CORDIC_CALC_FUNC_COS = 1, /* Primary result: cosine * m,
* secondary result: sine * m,
* primary argument: angle,
* secondary argument: modulus m
*/
CORDIC_CALC_FUNC_SIN = 2, /* Primary result: sine * m,
* secondary result: cosine * m,
* primary argument: angle,
* secondary argument: modulus m
*/
CORDIC_CALC_FUNC_PHASE = 3, /* Primary result: phase (atan2),
* secondary result: modulus,
* primary argument: x,
* secondary argument: y
*/
CORDIC_CALC_FUNC_MOD = 4, /* Primary result: modulus,
* secondary result: phase (atan2),
* primary argument: x,
* secondary argument: y
*/
CORDIC_CALC_FUNC_ARCTAN = 5, /* Primary result: arctangent,
* secondary result: none,
* primary argument: x,
* secondary argument: none
*/
CORDIC_CALC_FUNC_HCOS = 6, /* Primary result: hyperbolic cosine,
* secondary result: hyperbolic sine,
* primary argument: x,
* secondary argument: none
*/
CORDIC_CALC_FUNC_HSIN = 7, /* Primary result: hyperbolic sine,
* secondary result: hyperbolic cosine,
* primary argument: x,
* secondary argument: none
*/
CORDIC_CALC_FUNC_HARCTAN = 8, /* Primary result: hyperbolic arctangent,
* secondary result: none,
* primary argument: x,
* secondary argument: none
*/
CORDIC_CALC_FUNC_LN = 9, /* Primary result: natural logarithm,
* secondary result: none,
* primary argument: x,
* secondary argument: none
*/
CORDIC_CALC_FUNC_SQRT = 10, /* Primary result: square root,
* secondary result: none,
* primary argument: x,
* secondary argument: none
*/
CORDIC_CALC_FUNC_LAST
};
/* CORDIC calculate request */
struct cordic_calc_s
{
/* CORDIC request configuration */
uint8_t func; /* CORDIC function */
bool res2_incl; /* Include secondary result if available */
/* Input data */
cordic_num_t arg1;
cordic_num_t arg2;
/* Output data */
cordic_num_t res1;
cordic_num_t res2;
};
/* This structure provides the "lower-half" driver operations available to
* the "upper-half" driver.
*/
struct cordic_lowerhalf_s;
struct cordic_ops_s
{
/* CORDIC calculate request */
CODE int (*calc)(FAR struct cordic_lowerhalf_s *lower,
FAR struct cordic_calc_s *calc);
};
/* This structure provides the publicly visible representation of the
* "lower-half" driver state structure. "lower half" drivers will have an
* internal structure definition that will be cast-compatible with this
* structure definitions.
*/
struct cordic_lowerhalf_s
{
/* Publicly visible portion of the "lower-half" driver state structure. */
FAR const struct cordic_ops_s *ops; /* Lower half operations */
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Name: cordic_register
*
* Description:
* Register a CORDIC driver.
*
****************************************************************************/
int cordic_register(FAR const char *path,
FAR struct cordic_lowerhalf_s *lower);
#undef EXTERN
#if defined(__cplusplus)
}
#endif
#endif /* __INCLUDE_NUTTX_MATH_CORDIC_H */

View File

@ -0,0 +1,43 @@
/****************************************************************************
* include/nuttx/math/math_ioctl.h
* NuttX MATH-related IOCTLs definitions
*
* 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_IOCTL_H
#define __INCLUDE_NUTTX_MATH_MATH_IOCTL_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/fs/ioctl.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* All MATH-related IOCTL commands must be defined in this header file
* in order to assure that every IOCTL command is unique and will not be
* aliased.
*/
#define MATHIOC_CORDIC_CALC _MATHIOC(1)
#endif /* __INCLUDE_NUTTX_MATH_MATH_IOCTL_H */