mirror of
https://github.com/Ai-Thinker-Open/Ai-Thinker-Open_RTL8710BX_ALIOS_SDK.git
synced 2026-07-13 21:45:38 +00:00
rel_1.6.0 init
This commit is contained in:
commit
27b3e2883d
19359 changed files with 8093121 additions and 0 deletions
337
Living_SDK/kernel/rhino/common/k_atomic.c
Normal file
337
Living_SDK/kernel/rhino/common/k_atomic.c
Normal file
|
|
@ -0,0 +1,337 @@
|
|||
/*
|
||||
* Copyright (C) 2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
/*
|
||||
modification history
|
||||
--------------------
|
||||
2017_12_27,WangMin(Rocky) created.
|
||||
*/
|
||||
|
||||
/*
|
||||
* DESCRIPTION
|
||||
* This library is used to provide the atomic operators for CPU
|
||||
* which do not support native atomic operations.
|
||||
*
|
||||
* The design principle is disable the interrupt when execute the
|
||||
* atomic operations and enable the interrupt after finish the
|
||||
* operation.
|
||||
*
|
||||
* This library can be added into system by defining
|
||||
* RHINO_CONFIG_ATOMIC_GENERIC.
|
||||
*/
|
||||
|
||||
#include "k_api.h"
|
||||
#include "k_atomic.h"
|
||||
|
||||
/**
|
||||
* This routine atomically adds <*target> and <value>, placing the result in
|
||||
* <*target>. The operation is done using unsigned integer arithmetic.
|
||||
*
|
||||
* This routine can be used from both task and interrupt context.
|
||||
*
|
||||
* @param target memory location to add to
|
||||
* @param value the value to add
|
||||
*
|
||||
* @return The previous value from <target>
|
||||
*/
|
||||
|
||||
atomic_val_t rhino_atomic_add(atomic_t *target, atomic_val_t value)
|
||||
{
|
||||
CPSR_ALLOC();
|
||||
atomic_val_t old_value;
|
||||
|
||||
RHINO_CPU_INTRPT_DISABLE();
|
||||
|
||||
old_value = *target;
|
||||
*target += value;
|
||||
|
||||
RHINO_CPU_INTRPT_ENABLE();
|
||||
|
||||
return old_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* This routine atomically subtracts <value> from <*target>, result is placed
|
||||
* in <*target>. The operation is done using unsigned integer arithmetic.
|
||||
*
|
||||
* This routine can be used from both task and interrupt context.
|
||||
*
|
||||
* @param target the memory location to subtract from
|
||||
* @param value the value to subtract
|
||||
*
|
||||
* @return The previous value from <target>
|
||||
*/
|
||||
|
||||
atomic_val_t rhino_atomic_sub(atomic_t *target, atomic_val_t value)
|
||||
{
|
||||
CPSR_ALLOC();
|
||||
atomic_val_t old_value;
|
||||
|
||||
RHINO_CPU_INTRPT_DISABLE();
|
||||
|
||||
old_value = *target;
|
||||
*target -= value;
|
||||
|
||||
RHINO_CPU_INTRPT_ENABLE();
|
||||
|
||||
return old_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* This routine atomically increments the value in <*target>. The operation is
|
||||
* done using unsigned integer arithmetic.
|
||||
*
|
||||
* This routine can be used from both task and interrupt context.
|
||||
*
|
||||
* @return The value from <target> before the increment
|
||||
*/
|
||||
|
||||
atomic_val_t rhino_atomic_inc(atomic_t *target)
|
||||
{
|
||||
CPSR_ALLOC();
|
||||
atomic_val_t old_value;
|
||||
|
||||
RHINO_CPU_INTRPT_DISABLE();
|
||||
|
||||
old_value = *target;
|
||||
(*target)++;
|
||||
|
||||
RHINO_CPU_INTRPT_ENABLE();
|
||||
|
||||
return old_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* This routine atomically decrement the value in <*target>. The operation is
|
||||
* done using unsigned integer arithmetic.
|
||||
*
|
||||
* This routine can be used from both task and interrupt context.
|
||||
*
|
||||
* @return The value from <target> before the increment
|
||||
*/
|
||||
|
||||
atomic_val_t rhino_atomic_dec(atomic_t *target)
|
||||
{
|
||||
CPSR_ALLOC();
|
||||
atomic_val_t old_value;
|
||||
|
||||
RHINO_CPU_INTRPT_DISABLE();
|
||||
|
||||
old_value = *target;
|
||||
(*target)--;
|
||||
|
||||
RHINO_CPU_INTRPT_ENABLE();
|
||||
|
||||
return old_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* This routine atomically sets <*target> to <value> and returns the old value
|
||||
* that was in <*target>. Normally all CPU architectures can atomically write
|
||||
* to a variable of size atomic_t without the help of this routine.
|
||||
* This routine is intended for software that needs to atomically fetch and
|
||||
* replace the value of a memory location.
|
||||
*
|
||||
* This routine can be used from both task and interrupt context.
|
||||
*
|
||||
* @param target the memory location to write to
|
||||
* @param value the value to write
|
||||
*
|
||||
* @return The previous value from <target>
|
||||
*/
|
||||
|
||||
atomic_val_t rhino_atomic_set(atomic_t *target, atomic_val_t value)
|
||||
{
|
||||
CPSR_ALLOC();
|
||||
atomic_val_t old_value;
|
||||
|
||||
RHINO_CPU_INTRPT_DISABLE();
|
||||
|
||||
old_value = *target;
|
||||
*target = value;
|
||||
|
||||
RHINO_CPU_INTRPT_ENABLE();
|
||||
|
||||
return old_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* This routine atomically read a value from <target>.
|
||||
* This routine can be used from both task and interrupt context.
|
||||
*
|
||||
* @return The value read from <target>
|
||||
*/
|
||||
|
||||
atomic_val_t rhino_atomic_get(const atomic_t *target)
|
||||
{
|
||||
return *target;
|
||||
}
|
||||
|
||||
/**
|
||||
* This routine atomically performs a bitwise OR operation of <*target>
|
||||
* and <value>, placing the result in <*target>.
|
||||
*
|
||||
* This routine can be used from both task and interrupt context.
|
||||
*
|
||||
* @param target the memory location to be modified
|
||||
* @param value the value to OR
|
||||
*
|
||||
* @return The previous value from <target>
|
||||
*/
|
||||
|
||||
atomic_val_t rhino_atomic_or(atomic_t *target, atomic_val_t value)
|
||||
{
|
||||
CPSR_ALLOC();
|
||||
atomic_val_t old_value;
|
||||
|
||||
RHINO_CPU_INTRPT_DISABLE();
|
||||
|
||||
old_value = *target;
|
||||
*target |= value;
|
||||
|
||||
RHINO_CPU_INTRPT_ENABLE();
|
||||
|
||||
return old_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* This routine atomically performs a bitwise XOR operation of <*target> and
|
||||
* <value>, placing the result in <*target>.
|
||||
*
|
||||
* This routine can be used from both task and interrupt context.
|
||||
*
|
||||
* @param target the memory location to be modified
|
||||
* @param value the value to XOR
|
||||
*
|
||||
* @return The previous value from <target>
|
||||
*/
|
||||
|
||||
atomic_val_t rhino_atomic_xor(atomic_t *target, atomic_val_t value)
|
||||
{
|
||||
CPSR_ALLOC();
|
||||
atomic_val_t old_value;
|
||||
|
||||
RHINO_CPU_INTRPT_DISABLE();
|
||||
|
||||
old_value = *target;
|
||||
*target ^= value;
|
||||
|
||||
RHINO_CPU_INTRPT_ENABLE();
|
||||
|
||||
return old_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* This routine atomically performs a bitwise AND operation of <*target> and
|
||||
* <value>, placing the result in <*target>.
|
||||
*
|
||||
* This routine can be used from both task and interrupt context.
|
||||
*
|
||||
* @param target the memory location to be modified
|
||||
* @param value the value to AND
|
||||
*
|
||||
* @return The previous value from <target>
|
||||
*/
|
||||
|
||||
atomic_val_t rhino_atomic_and(atomic_t *target, atomic_val_t value)
|
||||
{
|
||||
CPSR_ALLOC();
|
||||
atomic_val_t old_value;
|
||||
|
||||
RHINO_CPU_INTRPT_DISABLE();
|
||||
|
||||
old_value = *target;
|
||||
*target &= value;
|
||||
|
||||
RHINO_CPU_INTRPT_ENABLE();
|
||||
|
||||
return old_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* This routine atomically performs a bitwise NAND operation of <*target> and
|
||||
* <value>, placing the result in <*target>.
|
||||
*
|
||||
* This routine can be used from both task and interrupt context.
|
||||
*
|
||||
* @param target the memory location to be modified
|
||||
* @param value the value to NAND
|
||||
*
|
||||
* @return The previous value from <target>
|
||||
*/
|
||||
|
||||
atomic_val_t rhino_atomic_nand(atomic_t *target, atomic_val_t value)
|
||||
{
|
||||
CPSR_ALLOC();
|
||||
atomic_val_t old_value;
|
||||
|
||||
RHINO_CPU_INTRPT_DISABLE();
|
||||
|
||||
old_value = *target;
|
||||
*target = ~(*target & value);
|
||||
|
||||
RHINO_CPU_INTRPT_ENABLE();
|
||||
|
||||
return old_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* This routine provides the atomic clear operator. The value of 0 is atomically
|
||||
* written at <target> and the previous value at <target> is returned.
|
||||
*
|
||||
* This routine can be used from both task and interrupt context.
|
||||
*
|
||||
* @param target the memory location to write
|
||||
*
|
||||
* @return The previous value from <target>
|
||||
*/
|
||||
|
||||
atomic_val_t rhino_atomic_clear(atomic_t *target)
|
||||
{
|
||||
CPSR_ALLOC();
|
||||
atomic_val_t old_value;
|
||||
|
||||
RHINO_CPU_INTRPT_DISABLE();
|
||||
|
||||
old_value = *target;
|
||||
*target = 0;
|
||||
|
||||
RHINO_CPU_INTRPT_ENABLE();
|
||||
|
||||
return old_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* This routine performs an atomic compare-and-swap, it test that whether
|
||||
* <*target> equal to <oldValue>, and if TRUE, setting the value of <*target>
|
||||
* to <newValue> and return 1.
|
||||
*
|
||||
* If the original value at <target> does not equal <oldValue>, then the target
|
||||
* will not be updated and return 0.
|
||||
*
|
||||
* @param target address to be tested
|
||||
* @param old_value value to compare against
|
||||
* @param new_value value to compare against
|
||||
* @return Returns 1 if <new_value> is written, 0 otherwise.
|
||||
*/
|
||||
|
||||
int rhino_atomic_cas(atomic_t *target, atomic_val_t old_value,
|
||||
atomic_val_t new_value)
|
||||
{
|
||||
CPSR_ALLOC();
|
||||
int ret = 0;
|
||||
|
||||
RHINO_CPU_INTRPT_DISABLE();
|
||||
|
||||
if (*target == old_value)
|
||||
{
|
||||
*target = new_value;
|
||||
ret = 1;
|
||||
}
|
||||
|
||||
RHINO_CPU_INTRPT_ENABLE();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
40
Living_SDK/kernel/rhino/common/k_atomic.h
Normal file
40
Living_SDK/kernel/rhino/common/k_atomic.h
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* Copyright (C) 2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
/*
|
||||
modification history
|
||||
--------------------
|
||||
2017_12_27,WangMin(Rocky) created.
|
||||
*/
|
||||
|
||||
#ifndef K_ATOMIC_H
|
||||
#define K_ATOMIC_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef unsigned int atomic_t;
|
||||
typedef atomic_t atomic_val_t;
|
||||
|
||||
extern atomic_val_t rhino_atomic_add(atomic_t *target, atomic_val_t value);
|
||||
extern atomic_val_t rhino_atomic_sub(atomic_t *target, atomic_val_t value);
|
||||
extern atomic_val_t rhino_atomic_inc(atomic_t *target);
|
||||
extern atomic_val_t rhino_atomic_dec(atomic_t *target);
|
||||
extern atomic_val_t rhino_atomic_set(atomic_t *target, atomic_val_t value);
|
||||
extern atomic_val_t rhino_atomic_get(const atomic_t *target);
|
||||
extern atomic_val_t rhino_atomic_or(atomic_t *target, atomic_val_t value);
|
||||
extern atomic_val_t rhino_atomic_xor(atomic_t *target, atomic_val_t value);
|
||||
extern atomic_val_t rhino_atomic_and(atomic_t *target, atomic_val_t value);
|
||||
extern atomic_val_t rhino_atomic_nand(atomic_t *target, atomic_val_t value);
|
||||
extern atomic_val_t rhino_atomic_clear(atomic_t *target);
|
||||
extern int rhino_atomic_cas(atomic_t *target, atomic_val_t old_value,
|
||||
atomic_val_t new_value);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* K_ATOMIC_H */
|
||||
|
||||
177
Living_SDK/kernel/rhino/common/k_fifo.c
Executable file
177
Living_SDK/kernel/rhino/common/k_fifo.c
Executable file
|
|
@ -0,0 +1,177 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <k_api.h>
|
||||
#include "k_fifo.h"
|
||||
|
||||
/*
|
||||
* internal helper to calculate the unused elements in a fifo
|
||||
*/
|
||||
static uint32_t fifo_unused(struct k_fifo *fifo)
|
||||
{
|
||||
return (fifo->mask + 1) - (fifo->in - fifo->out);
|
||||
}
|
||||
|
||||
static int8_t is_power_of_2(uint32_t n)
|
||||
{
|
||||
return (n != 0 && ((n & (n - 1)) == 0));
|
||||
}
|
||||
|
||||
int8_t fifo_init(struct k_fifo *fifo, void *buffer, uint32_t size)
|
||||
{
|
||||
/*
|
||||
* round down to the next power of 2, since our 'let the indices
|
||||
* wrap' technique works only in this case.
|
||||
*/
|
||||
if (!is_power_of_2(size)) {
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
fifo->in = 0;
|
||||
fifo->out = 0;
|
||||
fifo->data = buffer;
|
||||
|
||||
if (size < 2) {
|
||||
fifo->mask = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
fifo->mask = size - 1;
|
||||
fifo->free_bytes = size;
|
||||
fifo->size = size;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void fifo_copy_in(struct k_fifo *fifo, const void *src,
|
||||
uint32_t len, uint32_t off)
|
||||
{
|
||||
uint32_t l;
|
||||
|
||||
uint32_t size = fifo->mask + 1;
|
||||
|
||||
off &= fifo->mask;
|
||||
|
||||
l = fifo_min(len, size - off);
|
||||
|
||||
memcpy((unsigned char *)fifo->data + off, src, l);
|
||||
memcpy(fifo->data, (unsigned char *)src + l, len - l);
|
||||
|
||||
|
||||
}
|
||||
|
||||
uint32_t fifo_in(struct k_fifo *fifo, const void *buf, uint32_t len)
|
||||
{
|
||||
uint32_t l;
|
||||
|
||||
CPSR_ALLOC();
|
||||
|
||||
RHINO_CRITICAL_ENTER();
|
||||
|
||||
l = fifo_unused(fifo);
|
||||
|
||||
if (len > l) {
|
||||
len = l;
|
||||
}
|
||||
|
||||
fifo_copy_in(fifo, buf, len, fifo->in);
|
||||
fifo->in += len;
|
||||
|
||||
fifo->free_bytes -= len;
|
||||
|
||||
RHINO_CRITICAL_EXIT();
|
||||
return len;
|
||||
}
|
||||
|
||||
static void kfifo_copy_out(struct k_fifo *fifo, void *dst,
|
||||
uint32_t len, uint32_t off)
|
||||
{
|
||||
uint32_t l;
|
||||
uint32_t size = fifo->mask + 1;
|
||||
|
||||
off &= fifo->mask;
|
||||
|
||||
l = fifo_min(len, size - off);
|
||||
|
||||
memcpy(dst, (unsigned char *)fifo->data + off, l);
|
||||
memcpy((unsigned char *)dst + l, fifo->data, len - l);
|
||||
|
||||
}
|
||||
|
||||
static uint32_t internal_fifo_out_peek(struct k_fifo *fifo,
|
||||
void *buf, uint32_t len)
|
||||
{
|
||||
uint32_t l;
|
||||
|
||||
l = fifo->in - fifo->out;
|
||||
|
||||
if (len > l) {
|
||||
len = l;
|
||||
}
|
||||
|
||||
kfifo_copy_out(fifo, buf, len, fifo->out);
|
||||
return len;
|
||||
}
|
||||
|
||||
uint32_t fifo_out_peek(struct k_fifo *fifo,
|
||||
void *buf, uint32_t len)
|
||||
{
|
||||
|
||||
uint32_t ret_len;
|
||||
|
||||
CPSR_ALLOC();
|
||||
|
||||
RHINO_CRITICAL_ENTER();
|
||||
|
||||
ret_len = internal_fifo_out_peek(fifo, buf, len);
|
||||
|
||||
RHINO_CRITICAL_EXIT();
|
||||
|
||||
return ret_len;
|
||||
|
||||
}
|
||||
|
||||
uint32_t fifo_out(struct k_fifo *fifo, void *buf, uint32_t len)
|
||||
{
|
||||
CPSR_ALLOC();
|
||||
|
||||
RHINO_CRITICAL_ENTER();
|
||||
|
||||
len = internal_fifo_out_peek(fifo, buf, len);
|
||||
fifo->out += len;
|
||||
|
||||
fifo->free_bytes += len;
|
||||
|
||||
RHINO_CRITICAL_EXIT();
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
uint32_t fifo_out_all(struct k_fifo *fifo, void *buf)
|
||||
{
|
||||
uint32_t len;
|
||||
|
||||
CPSR_ALLOC();
|
||||
|
||||
RHINO_CRITICAL_ENTER();
|
||||
|
||||
len = fifo->size - fifo->free_bytes;
|
||||
|
||||
if (len == 0) {
|
||||
|
||||
RHINO_CRITICAL_EXIT();
|
||||
return 0;
|
||||
}
|
||||
|
||||
len = internal_fifo_out_peek(fifo, buf, len);
|
||||
fifo->out += len;
|
||||
|
||||
fifo->free_bytes += len;
|
||||
|
||||
RHINO_CRITICAL_EXIT();
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
76
Living_SDK/kernel/rhino/common/k_fifo.h
Normal file
76
Living_SDK/kernel/rhino/common/k_fifo.h
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef FIFO_H
|
||||
#define FIFO_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct k_fifo {
|
||||
|
||||
uint32_t in;
|
||||
uint32_t out;
|
||||
uint32_t mask;
|
||||
void *data;
|
||||
uint32_t free_bytes;
|
||||
uint32_t size;
|
||||
|
||||
};
|
||||
|
||||
#define fifo_min(x, y) ((x) > (y)?(y):(x))
|
||||
#define fifo_max(x, y) ((x) > (y)?(x):(y))
|
||||
|
||||
/**
|
||||
* This function will init the fifo.
|
||||
* @param[in] fifo pointer to fifo
|
||||
* @param[in] buffer pointer to fifo buffer
|
||||
* @param[in] size size of fifo buffer
|
||||
* @return the operation status, 0 is OK, others is error
|
||||
*/
|
||||
int8_t fifo_init(struct k_fifo *fifo, void *buffer, uint32_t size);
|
||||
|
||||
/**
|
||||
* This function will write buf to fifo.
|
||||
* @param[in] fifo pointer to fifo
|
||||
* @param[in] buf pointer to buffer write
|
||||
* @param[in] size size of buffer
|
||||
* @return the size has been written to the fifo
|
||||
*/
|
||||
uint32_t fifo_in(struct k_fifo *fifo, const void *buf, uint32_t len);
|
||||
|
||||
/**
|
||||
* This function will read fifo data to buf
|
||||
* @param[in] fifo pointer to fifo
|
||||
* @param[in] buf pointer to buffer read
|
||||
* @param[in] len len of buffer
|
||||
* @return the size has read
|
||||
*/
|
||||
uint32_t fifo_out(struct k_fifo *fifo, void *buf, uint32_t len);
|
||||
|
||||
/**
|
||||
* This function will read fifo data to buf,but data remain in fifo
|
||||
* @param[in] fifo pointer to fifo
|
||||
* @param[in] buf pointer to buffer read
|
||||
* @param[in] len len of buffer
|
||||
* @return the size has read
|
||||
*/
|
||||
uint32_t fifo_out_peek(struct k_fifo *fifo,
|
||||
void *buf, uint32_t len);
|
||||
|
||||
/**
|
||||
* This function will read fifo all data
|
||||
* @param[in] fifo pointer to fifo
|
||||
* @param[in] buf pointer to buffer read
|
||||
* @return the size has read
|
||||
*/
|
||||
uint32_t fifo_out_all(struct k_fifo *fifo, void *buf);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
1541
Living_SDK/kernel/rhino/common/k_trace.c
Executable file
1541
Living_SDK/kernel/rhino/common/k_trace.c
Executable file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue