Merge remote-tracking branch 'origin/master' into ieee802154

This commit is contained in:
Gregory Nutt 2017-03-24 10:18:00 -06:00
commit b3f259e488
21 changed files with 776 additions and 31 deletions

View File

@ -8,7 +8,7 @@
<tr align="center" bgcolor="#e4e4e4">
<td>
<h1><big><font color="#3c34ec"><i>NuttX README Files</i></font></big></h1>
<p>Last Updated: March 21, 2017</p>
<p>Last Updated: March 23, 2017</p>
</td>
</tr>
</table>
@ -68,6 +68,8 @@ nuttx/
| | `- <a href="https://bitbucket.org/nuttx/nuttx/src/master/configs/c5471evm/README.txt" target="_blank"><b><i>README.txt</i></b></a>
| |- cc3200-launchpad/
| | `- <a href="https://bitbucket.org/nuttx/nuttx/src/master/configs/cc3200-launchpad/README.txt" target="_blank"><b><i>README.txt</i></b></a>
| |- clicker2-stm32/
| | `- <a href="https://bitbucket.org/nuttx/nuttx/src/master/configs/clicker2-stm32/README.txt" target="_blank"><b><i>README.txt</i></b></a>
| |- cloudctrl/
| | `- <a href="https://bitbucket.org/nuttx/nuttx/src/master/configs/cloudctrl/README.txt" target="_blank"><b><i>README.txt</i></b></a>
| |- demo9s12ne64/

View File

@ -1453,6 +1453,8 @@ nuttx/
| | `- README.txt
| |- cc3200-launchpad/
| | `- README.txt
| |- clicker2-stm32
| | `- README.txt
| |- cloudctrl
| | `- README.txt
| |- demo0s12ne64/

View File

@ -6201,12 +6201,11 @@ config STM32_I2C_DUTY16_9
config STM32_I2C_DMA
bool "I2C DMA Support"
default n
depends on STM32_I2C && STM32_STM32F40XX && STM32_DMA1
depends on STM32_I2C && STM32_STM32F40XX && STM32_DMA1 && !I2C_POLLED
---help---
This option enables the DMA for I2C transfers.
Note: The user can define CONFIG_I2C_DMAPRIO: a custom priority value for the
I2C dma streams, else the default priority level is set to medium.
Note: This option is compatible with CONFIG_I2C_POLLED.
endmenu

View File

@ -128,6 +128,7 @@ static void flash_lock(void)
modifyreg32(STM32_FLASH_CR, 0, FLASH_CR_LOCK);
}
#if defined(CONFIG_STM32_FLASH_WORKAROUND_DATA_CACHE_CORRUPTION_ON_RWW)
static void data_cache_disable(void)
{
modifyreg32(STM32_FLASH_ACR, FLASH_ACR_DCEN, 0);
@ -143,6 +144,7 @@ static void data_cache_enable(void)
modifyreg32(STM32_FLASH_ACR, 0, FLASH_ACR_DCEN);
}
#endif /* defined(CONFIG_STM32_FLASH_WORKAROUND_DATA_CACHE_CORRUPTION_ON_RWW) */
/************************************************************************************
* Public Functions
@ -288,6 +290,7 @@ ssize_t up_progmem_erasepage(size_t page)
#if !defined(CONFIG_STM32_STM32F40XX)
if (!(getreg32(STM32_RCC_CR) & RCC_CR_HSION))
{
sem_unlock();
return -EPERM;
}
#endif

View File

@ -1204,6 +1204,9 @@ static inline void stm32_i2c_enablefsmc(uint32_t ahbenr)
static int stm32_i2c_isr(struct stm32_i2c_priv_s *priv)
{
#ifndef CONFIG_I2C_POLLED
uint32_t regval;
#endif
uint32_t status;
i2cinfo("I2C ISR called\n");
@ -1867,7 +1870,6 @@ static int stm32_i2c_isr(struct stm32_i2c_priv_s *priv)
#else
/* Clear all interrupts */
uint32_t regval;
regval = stm32_i2c_getreg(priv, STM32_I2C_CR2_OFFSET);
regval &= ~I2C_CR2_ALLINTS;
stm32_i2c_putreg(priv, STM32_I2C_CR2_OFFSET, regval);

View File

@ -693,17 +693,17 @@ static int stm32_interrupt(int irq, FAR void *context, FAR void *arg)
stm32_putreg16(priv, STM32_GTIM_SR_OFFSET, regval & ~GTIM_SR_UIF);
/* Check the direction bit in the CR1 register and add or subtract the
* maximum value, as appropriate.
* maximum value + 1, as appropriate.
*/
regval = stm32_getreg16(priv, STM32_GTIM_CR1_OFFSET);
if ((regval & ATIM_CR1_DIR) != 0)
{
priv->position -= (int32_t)0x0000ffff;
priv->position -= (int32_t)0x00010000;
}
else
{
priv->position += (int32_t)0x0000ffff;
priv->position += (int32_t)0x00010000;
}
return OK;

View File

@ -1261,6 +1261,15 @@ static int stm32_i2c_isr(struct stm32_i2c_priv_s *priv)
priv->status = status;
/* Any new message should begin with "Start" condition
* Situation priv->msgc == 0 came from DMA RX handler and should be managed
*/
if (priv->dcnt == -1 && priv->msgc != 0 && (status & I2C_SR1_SB) == 0)
{
return OK;
}
/* Check if this is a new transmission so to set up the
* trace table accordingly.
*/
@ -1516,9 +1525,16 @@ static int stm32_i2c_isr(struct stm32_i2c_priv_s *priv)
status |= (stm32_i2c_getreg(priv, STM32_I2C_SR2_OFFSET) << 16);
/* Send Stop */
/* Send Stop/Restart */
stm32_i2c_sendstop(priv);
if (priv->msgc > 0)
{
stm32_i2c_sendstart(priv);
}
else
{
stm32_i2c_sendstop(priv);
}
i2cinfo("Address ACKed beginning data reception\n");
i2cinfo("short read N=1: programming stop bit\n");
@ -1828,7 +1844,17 @@ static int stm32_i2c_isr(struct stm32_i2c_priv_s *priv)
{
i2cinfo("short read N=2: DR and SR full setting stop bit and reading twice\n");
stm32_i2c_sendstop(priv);
/* Send Stop/Restart */
if (priv->msgc > 0)
{
stm32_i2c_sendstart(priv);
}
else
{
stm32_i2c_sendstop(priv);
}
*priv->ptr++ = stm32_i2c_getreg(priv, STM32_I2C_DR_OFFSET);
priv->dcnt--;
*priv->ptr++ = stm32_i2c_getreg(priv, STM32_I2C_DR_OFFSET);
@ -1905,9 +1931,16 @@ static int stm32_i2c_isr(struct stm32_i2c_priv_s *priv)
stm32_i2c_traceevent(priv, I2CEVENT_READ_3, priv->dcnt);
/* Program stop */
/* Program Stop/Restart */
stm32_i2c_sendstop(priv);
if (priv->msgc > 0)
{
stm32_i2c_sendstart(priv);
}
else
{
stm32_i2c_sendstop(priv);
}
/* read dcnt = 2 */
@ -2015,7 +2048,6 @@ static int stm32_i2c_isr(struct stm32_i2c_priv_s *priv)
#else
/* Clear all interrupts */
uint32_t regval;
regval = stm32_i2c_getreg(priv, STM32_I2C_CR2_OFFSET);
regval &= ~I2C_CR2_ALLINTS;
stm32_i2c_putreg(priv, STM32_I2C_CR2_OFFSET, regval);
@ -2062,7 +2094,14 @@ static void stm32_i2c_dmarxcallback(DMA_HANDLE handle, uint8_t status, void *arg
* interrupt routine if enabled.
*/
stm32_i2c_sendstop(priv);
if (priv->msgc > 0)
{
stm32_i2c_sendstart(priv);
}
else
{
stm32_i2c_sendstop(priv);
}
/* Let the I2C periph know to stop DMA transfers, also is used by ISR to check
* if DMA is done.
@ -2116,10 +2155,6 @@ static void stm32_i2c_dmatxcallback(DMA_HANDLE handle, uint8_t status, void *arg
regval |= (I2C_CR2_ITERREN | I2C_CR2_ITEVFEN);
stm32_i2c_putreg(priv, STM32_I2C_CR2_OFFSET, regval);
#endif
/* let the ISR routine take care of shutting down or switching to next msg */
stm32_i2c_isr(priv);
}
#endif /* ifdef CONFIG_STM32_I2C_DMA */

View File

@ -126,7 +126,7 @@
# endif
#endif
/* There are 4 possible heap configurations:
/* There are 5 possible heap configurations:
*
* Configuration 1. System SRAM1 (only)
* CONFIG_MM_REGIONS == 1

View File

@ -0,0 +1,199 @@
README
======
This is the README file for the port of NuttX to the Mikroe Clicker2 STM32
board based on the STMicro STM32F407VGT6 MCU.
Reference: https://shop.mikroe.com/development-boards/starter/clicker-2/stm32f4
Contents
========
o Serial Console
o LEDs
o Buttons
o Using JTAG
o Configurations
Serial Console
==============
The are no RS-232 drivers on-board. An RS-232 Click board is available:
https://shop.mikroe.com/click/interface/rs232 or you can cannot an off-
board TTL-to-RS-232 converter as follows:
USART2: mikroBUS1 PD6/RX and PD5/TX
USART3: mikroBUS2 PD9/RX and PD8TX
GND, 3.3V, and 5V. Are also available
By default, USART3 on mikroBUS2 is used as the serial console in each
configuration unless stated otherwise in the description of the
configuration.
LEDs
====
The Mikroe Clicker2 STM32 has two user controllable LEDs:
LD1/PE12, Active high output illuminates
LD2/PE15, Active high output illuminates
If CONFIG_ARCH_LEDS is not defined, then the user can control the LEDs in any
way. If CONFIG_ARCH_LEDs is defined, then NuttX will control the 2 LEDs on
board the Clicker2 for STM32. The following definitions describe how NuttX
controls the LEDs:
SYMBOL Meaning LED state
LD1 LD2
------------------- ----------------------- -------- --------
LED_STARTED NuttX has been started OFF OFF
LED_HEAPALLOCATE Heap has been allocated OFF OFF
LED_IRQSENABLED Interrupts enabled OFF OFF
LED_STACKCREATED Idle stack created ON OFF
LED_INIRQ In an interrupt N/C ON
LED_SIGNAL In a signal handler No change
LED_ASSERTION An assertion failed No change
LED_PANIC The system has crashed OFF Blinking
LED_IDLE STM32 is is sleep mode Not used
Thus is LD1 is illuminated, the Clicker2 has completed boot-up. IF LD2
is glowly softly, then interrupts are being taken; the level of illumination
depends amount of time processing interupts. If LD1 is off and LD2 is
blinking at about 2Hz, then the system has crashed.
Buttons
=======
The Mikroe Clicker2 STM32 has two buttons available to software:
T2/E0, Low sensed when pressed
T3/PA10, Low sensed when pressed
Using JTAG
==========
The Clicker2 comes with the mikroBootloader installed. That bootloader
has not been used and is possibly incompatible with the Clicker2-STM32
linker script at configs/clicker2-stm32/scripts/flash.ld. Often code must
be built to execute at an offset in to FLASH when a bootloader is used.
Certainly that is the case for the ST-Micro DFU bootloader but I am not
aware of the requirements for use with the mikroBootloader.
JTAG has been used in the development of this board support. The
Clicker2-STM32 board offers a 2x5 JTAG connector. You may use Dupont
jumpers to connect this port to JTAG as described here:
https://www.mikroe.com/how-to-use-st-link-v2-with-clicker-2-for-stm32-a-detailed-walkthrough/
http://www.playembedded.org/blog/en/2016/02/06/mikroe-clicker-2-for-stm32-and-stlink-v2/
NOTE that the FLASH is locked. You may need to follow the instructions at
the second link to unlock it (although I think you may be able to do this
with the ST-Micro ST-Link Utility as well).
You can avoid the mess of jumpers using the mikroProg to ST-Link v2 adapter
along with a 2x5, 10-wire ribbon cable connector:
https://shop.mikroe.com/add-on-boards/adapter/mikroprog-st-link-v2-adapter
OpenOCD can be used with the ST-Link to provide a debug environment. I suspect,
however, that adapter can be used with other JTAG debuggers such as J-Link,
but that remains to be verified.
Configurations
==============
Information Common to All Configurations
----------------------------------------
Each Clicker2 configuration is maintained in a sub-directory and can be
selected as follow:
cd tools
./configure.sh clicker2-stm32/<subdir>
cd -
. ./setenv.sh
Before sourcing the setenv.sh file above, you should examine it and
perform edits as necessary so that TOOLCHAIN_BIN is the correct path
to the directory than holds your toolchain binaries.
And then build NuttX by simply typing the following. At the conclusion of
the make, the nuttx binary will reside in an ELF file called, simply, nuttx.
make oldconfig
make
The <subdir> that is provided above as an argument to the tools/configure.sh
must be is one of the following.
NOTES:
1. These configurations use the mconf-based configuration tool. To
change any of these configurations using that tool, you should:
a. Build and install the kconfig-mconf tool. See nuttx/README.txt
see additional README.txt files in the NuttX tools repository.
b. Execute 'make menuconfig' in nuttx/ in order to start the
reconfiguration process.
2. Unless stated otherwise, all configurations generate console
output on USART3, channel 0) as described above under "Serial
Console". The relevant configuration settings are listed below:
CONFIG_STM32_USART3=y
CONFIG_STM32_USART3_SERIALDRIVER=y
CONFIG_STM32_USART=y
CONFIG_USART3_SERIALDRIVER=y
CONFIG_USART3_SERIAL_CONSOLE=y
CONFIG_USART3_RXBUFSIZE=256
CONFIG_USART3_TXBUFSIZE=256
CONFIG_USART3_BAUD=115200
CONFIG_USART3_BITS=8
CONFIG_USART3_PARITY=0
CONFIG_USART3_2STOP=0
3. All of these configurations are set up to build under Linux using the
"GNU Tools for ARM Embedded Processors" that is maintained by ARM
(unless stated otherwise in the description of the configuration).
https://launchpad.net/gcc-arm-embedded
That toolchain selection can easily be reconfigured using
'make menuconfig'. Here are the relevant current settings:
Build Setup:
CONFIG_HOST_LINUX =y : Linux environment
System Type -> Toolchain:
CONFIG_ARMV7M_TOOLCHAIN_GNU_EABIL=y : GNU ARM EABI toolchain
Configuration sub-directories
-----------------------------
nsh:
Configures the NuttShell (nsh) located at examples/nsh. This
configuration is focused on low level, command-line driver testing. It
has no network.
NOTES:
1. Support for NSH built-in applications is provided:
Binary Formats:
CONFIG_BUILTIN=y : Enable support for built-in programs
Application Configuration:
CONFIG_NSH_BUILTIN_APPS=y : Enable starting apps from NSH command line
No built applications are enabled in the base configuration, however.
2. C++ support for applications is enabled:
CONFIG_HAVE_CXX=y
CONFIG_HAVE_CXXINITIALIZE=y
CONFIG_EXAMPLES_NSH_CXXINITIALIZE=y

View File

@ -144,10 +144,10 @@ CONFIG_ARM_HAVE_MPU_UNIFIED=y
# CONFIG_ARMV7M_HAVE_ITCM is not set
# CONFIG_ARMV7M_HAVE_DTCM is not set
# CONFIG_ARMV7M_TOOLCHAIN_IARL is not set
CONFIG_ARMV7M_TOOLCHAIN_BUILDROOT=y
# CONFIG_ARMV7M_TOOLCHAIN_BUILDROOT is not set
# CONFIG_ARMV7M_TOOLCHAIN_CODEREDL is not set
# CONFIG_ARMV7M_TOOLCHAIN_CODESOURCERYL is not set
# CONFIG_ARMV7M_TOOLCHAIN_GNU_EABIL is not set
CONFIG_ARMV7M_TOOLCHAIN_GNU_EABIL=y
# CONFIG_ARMV7M_OABI_TOOLCHAIN is not set
CONFIG_ARMV7M_HAVE_STACKCHECK=y
# CONFIG_ARMV7M_STACKCHECK is not set

View File

@ -42,8 +42,7 @@
*
* When booting from FLASH, FLASH memory is aliased to address 0x0000:0000
* where the code expects to begin execution by jumping to the entry point in
* the 0x0800:0000 address
* range.
* the 0x0800:0000 address range.
*/
MEMORY

View File

@ -33,8 +33,8 @@
*
****************************************************************************/
#ifndef __CONFIGS_OLIMEX_STM32_P407_SRC_H
#define __CONFIGS_OLIMEX_STM32_P407_SRC_H
#ifndef __CONFIGS_CLICKER2_STM32_SRC_CLICKER2_H
#define __CONFIGS_CLICKER2_STM32_SRC_CLICKER2_H
/****************************************************************************
* Included Files
@ -212,7 +212,7 @@
#define GPIO_MB1_RST (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\
GPIO_OUTPUT_CLEAR|GPIO_PORTE|GPIO_PIN7)
#define GPIO_MB1_RST (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\
#define GPIO_MB2_RST (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\
GPIO_OUTPUT_CLEAR|GPIO_PORTE|GPIO_PIN13)
/* Interrupts
@ -303,4 +303,4 @@ int stm32_mrf24j40_initialize(void);
#endif
#endif /* __ASSEMBLY__ */
#endif /* __CONFIGS_OLIMEX_STM32_P407_SRC_H */
#endif /* __CONFIGS_CLICKER2_STM32_SRC_CLICKER2_H */

View File

@ -64,6 +64,18 @@
# define UNUSED(a) ((void)(a))
/* Built-in functions */
/* GCC 4.x have __builtin_ctz(|l|ll) and __builtin_clz(|l|ll). These count
* trailing/leading zeros of input number and typically will generate few
* fast bit-counting instructions. Inputting zero to these functions is
* undefined and needs to be taken care of by the caller. */
#if __GNUC__ >= 4
# define CONFIG_HAVE_BUILTIN_CTZ 1
# define CONFIG_HAVE_BUILTIN_CLZ 1
#endif
/* Attributes
*
* GCC supports weak symbols which can be used to reduce code size because

View File

@ -114,6 +114,17 @@ static inline FAR char *rindex(FAR const char *s, int c)
****************************************************************************/
int ffs(int j);
int ffsl(long j);
#ifdef CONFIG_HAVE_LONG_LONG
int ffsll(long long j);
#endif
int fls(int j);
int flsl(long j);
#ifdef CONFIG_HAVE_LONG_LONG
int flsll(long long j);
#endif
int strcasecmp(FAR const char *, FAR const char *);
int strncasecmp(FAR const char *, FAR const char *, size_t);

View File

@ -35,7 +35,8 @@
# Add the string C files to the build
CSRCS += lib_ffs.c lib_isbasedigit.c lib_memset.c lib_memchr.c
CSRCS += lib_ffs.c lib_ffsl.c lib_ffsll.c lib_fls.c lib_flsl.c
CSRCS += lib_flsll.c lib_isbasedigit.c lib_memset.c lib_memchr.c
CSRCS += lib_memccpy.c lib_memcmp.c lib_memmove.c lib_skipspace.c
CSRCS += lib_stpcpy.c lib_strcasecmp.c lib_strcat.c lib_strchr.c
CSRCS += lib_strcpy.c lib_strcmp.c lib_strcspn.c lib_strdup.c

View File

@ -33,11 +33,11 @@
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/compiler.h>
#include <strings.h>
/****************************************************************************
@ -55,11 +55,11 @@
*
* Description:
* The ffs() function will find the first bit set (beginning with the least
* significant bit) in i, and return the index of that bit. Bits are
* significant bit) in j, and return the index of that bit. Bits are
* numbered starting at one (the least significant bit).
*
* Returned Value:
* The ffs() function will return the index of the first bit set. If i is
* The ffs() function will return the index of the first bit set. If j is
* 0, then ffs() will return 0.
*
****************************************************************************/
@ -70,6 +70,11 @@ int ffs(int j)
if (j != 0)
{
#ifdef CONFIG_HAVE_BUILTIN_CTZ
/* Count trailing zeros function can be used to implement ffs. */
ret = __builtin_ctz(j) + 1;
#else
unsigned int value = (unsigned int)j;
int bitno;
@ -81,6 +86,7 @@ int ffs(int j)
break;
}
}
#endif
}
return ret;

93
libc/string/lib_ffsl.c Normal file
View File

@ -0,0 +1,93 @@
/****************************************************************************
* libc/string/lib_ffsl.c
*
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/compiler.h>
#include <strings.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define NBITS (8 * sizeof(unsigned long))
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: ffsl
*
* Description:
* The ffsl() function will find the first bit set (beginning with the least
* significant bit) in j, and return the index of that bit. Bits are
* numbered starting at one (the least significant bit).
*
* Returned Value:
* The ffsl() function will return the index of the first bit set. If j is
* 0, then ffsl() will return 0.
*
****************************************************************************/
int ffsl(long j)
{
int ret = 0;
if (j != 0)
{
#ifdef CONFIG_HAVE_BUILTIN_CTZ
/* Count trailing zeros function can be used to implement ffs. */
ret = __builtin_ctzl(j) + 1;
#else
unsigned long value = (unsigned long)j;
int bitno;
for (bitno = 1; bitno <= NBITS; bitno++, value >>= 1)
{
if ((value & 1) != 0)
{
ret = bitno;
break;
}
}
#endif
}
return ret;
}

97
libc/string/lib_ffsll.c Normal file
View File

@ -0,0 +1,97 @@
/****************************************************************************
* libc/string/lib_ffsll.c
*
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/compiler.h>
#include <strings.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define NBITS (8 * sizeof(unsigned long long))
/****************************************************************************
* Public Functions
****************************************************************************/
#ifdef CONFIG_HAVE_LONG_LONG
/****************************************************************************
* Name: ffsll
*
* Description:
* The ffsll() function will find the first bit set (beginning with the least
* significant bit) in i, and return the index of that bit. Bits are
* numbered starting at one (the least significant bit).
*
* Returned Value:
* The ffsll() function will return the index of the first bit set. If j is
* 0, then ffsll() will return 0.
*
****************************************************************************/
int ffsll(long long j)
{
int ret = 0;
if (j != 0)
{
#ifdef CONFIG_HAVE_BUILTIN_CTZ
/* Count trailing zeros function can be used to implement ffs. */
ret = __builtin_ctzll(j) + 1;
#else
unsigned long long value = (unsigned long long)j;
int bitno;
for (bitno = 1; bitno <= NBITS; bitno++, value >>= 1)
{
if ((value & 1) != 0)
{
ret = bitno;
break;
}
}
#endif
}
return ret;
}
#endif

93
libc/string/lib_fls.c Normal file
View File

@ -0,0 +1,93 @@
/****************************************************************************
* libc/string/lib_fls.c
*
* Copyright (C) 2017 Haltian Ltd. All rights reserved.
* Author: Jussi Kivilinna <jussi.kivilinna@haltian.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/compiler.h>
#include <strings.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define NBITS (8 * sizeof(unsigned int))
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: fls
*
* Description:
* The fls() function will find the last bit set in value and return
* the index of that bit. Bits are numbered starting at one (the least
* significant bit).
*
* Returned Value:
* The fls() function will return the index of the last bit set. If j is
* 0, then fls() will return 0.
*
****************************************************************************/
int fls(int j)
{
int ret = 0;
if (j != 0)
{
#ifdef CONFIG_HAVE_BUILTIN_CLZ
/* Count leading zeros function can be used to implement fls. */
ret = NBITS - __builtin_clz(j);
#else
unsigned int value = (unsigned int)j;
int bitno;
for (bitno = 1; bitno <= NBITS; bitno++, value >>= 1)
{
if (value == 1)
{
ret = bitno;
break;
}
}
#endif
}
return ret;
}

93
libc/string/lib_flsl.c Normal file
View File

@ -0,0 +1,93 @@
/****************************************************************************
* libc/string/lib_fls.c
*
* Copyright (C) 2017 Haltian Ltd. All rights reserved.
* Author: Jussi Kivilinna <jussi.kivilinna@haltian.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/compiler.h>
#include <strings.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define NBITS (8 * sizeof(unsigned long))
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: flsl
*
* Description:
* The flsl() function will find the last bit set in value and return
* the index of that bit. Bits are numbered starting at one (the least
* significant bit).
*
* Returned Value:
* The flsl() function will return the index of the last bit set. If j is
* 0, then flsl() will return 0.
*
****************************************************************************/
int flsl(long j)
{
int ret = 0;
if (j != 0)
{
#ifdef CONFIG_HAVE_BUILTIN_CLZ
/* Count leading zeros function can be used to implement fls. */
ret = NBITS - __builtin_clzl(j);
#else
unsigned long value = (unsigned long)j;
int bitno;
for (bitno = 1; bitno <= NBITS; bitno++, value >>= 1)
{
if (value == 1)
{
ret = bitno;
break;
}
}
#endif
}
return ret;
}

98
libc/string/lib_flsll.c Normal file
View File

@ -0,0 +1,98 @@
/****************************************************************************
* libc/string/lib_fls.c
*
* Copyright (C) 2017 Haltian Ltd. All rights reserved.
* Author: Jussi Kivilinna <jussi.kivilinna@haltian.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/compiler.h>
#include <strings.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define NBITS (8 * sizeof(unsigned long long))
/****************************************************************************
* Public Functions
****************************************************************************/
#ifdef CONFIG_HAVE_LONG_LONG
/****************************************************************************
* Name: flsll
*
* Description:
* The flsll() function will find the last bit set in value and return
* the index of that bit. Bits are numbered starting at one (the least
* significant bit).
*
* Returned Value:
* The flsll() function will return the index of the last bit set. If j is
* 0, then flsll() will return 0.
*
****************************************************************************/
int flsll(long long j)
{
int ret = 0;
if (j != 0)
{
#ifdef CONFIG_HAVE_BUILTIN_CLZ
/* Count leading zeros function can be used to implement fls. */
ret = NBITS - __builtin_clzll(j);
#else
unsigned long long value = (unsigned long long)j;
int bitno;
for (bitno = 1; bitno <= NBITS; bitno++, value >>= 1)
{
if (value == 1)
{
ret = bitno;
break;
}
}
#endif
}
return ret;
}
#endif