add some code

This commit is contained in:
2025-09-05 13:25:11 +08:00
parent 9ff0a99e7a
commit 3cf1229a85
8911 changed files with 2535396 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
// Copyright 2018-2022 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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 _dsp_common_H_
#define _dsp_common_H_
#include <stdint.h>
#include <stdbool.h>
#include "dsp_err.h"
#include "esp_idf_version.h"
#if defined(__XTENSA__) || defined(__riscv)
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0)
#include "esp_cpu.h"
#else
#include "soc/cpu.h"
#endif
#endif
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @brief check power of two
* The function check if the argument is power of 2.
* The implementation use ANSI C and could be compiled and run on any platform
*
* @return
* - true if x is power of two
* - false if no
*/
bool dsp_is_power_of_two(int x);
/**
* @brief Power of two
* The function return power of 2 for values 2^N.
* The implementation use ANSI C and could be compiled and run on any platform
*
* @return
* - power of two
*/
int dsp_power_of_two(int x);
/**
* @brief Logginng for esp32s3 TIE core
* Registers covered q0 to q7, ACCX and SAR_BYTE
*
* @param n_regs: number of registers to be logged at once
* @param ...: register codes 0, 1, 2, 3, 4, 5, 6, 7, 'a', 's'
*
* @return ESP_OK
*
*/
esp_err_t tie_log(int n_regs, ...);
#ifdef __cplusplus
}
#endif
// esp_cpu_get_ccount function is implemented in IDF 4.1 and later
#if defined(__XTENSA__) || defined(__riscv)
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
#define dsp_get_cpu_cycle_count esp_cpu_get_cycle_count
#else
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 0)
#define dsp_get_cpu_cycle_count esp_cpu_get_ccount
#else
#define dsp_get_cpu_cycle_count xthal_get_ccount
#endif
#endif // ESP_IDF_VERSION
#else
// Linux Target
#include <x86intrin.h>
#define dsp_get_cpu_cycle_count __rdtsc
#endif
#endif // _dsp_common_H_

View File

@@ -0,0 +1,23 @@
// Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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 _DSP_ERR_H_
#define _DSP_ERR_H_
#include "stdint.h"
#include "esp_err.h"
#include "dsp_err_codes.h"
#endif // _DSP_ERR_H_

View File

@@ -0,0 +1,28 @@
// Copyright 2018-2022 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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 _dsp_error_codes_H_
#define _dsp_error_codes_H_
#define DSP_OK 0 // For internal use only. Please use ESP_OK instead
#define ESP_ERR_DSP_BASE 0x70000
#define ESP_ERR_DSP_INVALID_LENGTH (ESP_ERR_DSP_BASE + 1)
#define ESP_ERR_DSP_INVALID_PARAM (ESP_ERR_DSP_BASE + 2)
#define ESP_ERR_DSP_PARAM_OUTOFRANGE (ESP_ERR_DSP_BASE + 3)
#define ESP_ERR_DSP_UNINITIALIZED (ESP_ERR_DSP_BASE + 4)
#define ESP_ERR_DSP_REINITIALIZED (ESP_ERR_DSP_BASE + 5)
#define ESP_ERR_DSP_ARRAY_NOT_ALIGNED (ESP_ERR_DSP_BASE + 6)
#endif // _dsp_error_codes_H_

View File

@@ -0,0 +1,33 @@
// Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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 dsp_platform_h_
#define dsp_platform_h_
#include "esp_idf_version.h"
#if defined(__XTENSA__) || defined(__riscv)
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0)
#include "esp_cpu.h"
#else
#include "soc/cpu.h"
#endif
#endif
#include "freertos/FreeRTOS.h"
#include "freertos/portable.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#endif // dsp_platform_h_

View File

@@ -0,0 +1,38 @@
// Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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 _DSP_TESTS_H_
#define _DSP_TESTS_H_
#include <stdlib.h>
#include "esp_idf_version.h"
#include "esp_dsp.h"
#define TEST_ASSERT_EXEC_IN_RANGE(min_exec, max_exec, actual) \
if (actual >= max_exec) { \
ESP_LOGE("", "Time error. Expected max: %i, reached: %i", (int)max_exec, (int)actual);\
TEST_ASSERT_MESSAGE (false, "Exec time takes more than expected! ");\
}\
if (actual < min_exec) {\
ESP_LOGE("", "Time error. Expected min: %i, reached: %i", (int)min_exec, (int)actual);\
TEST_ASSERT_MESSAGE (false, "Exec time takes less then expected!");\
}
// memalign function is implemented in IDF 4.3 and later
#if ESP_IDF_VERSION <= ESP_IDF_VERSION_VAL(4, 3, 0)
#define memalign(align_, size_) malloc(size_)
#endif
#endif // _DSP_TESTS_H_

View File

@@ -0,0 +1,36 @@
#ifndef _dsp_types_H_
#define _dsp_types_H_
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
// union to simplify access to the 16 bit data
typedef union sc16_u {
struct {
int16_t re;
int16_t im;
};
uint32_t data;
} sc16_t;
typedef union fc32_u {
struct {
float re;
float im;
};
uint64_t data;
} fc32_t;
typedef struct image2d_s {
void *data; // could be int8_t, unt8_t, int16_t, unt16_t, float
int step_x; // step of elements by X
int step_y; // step of elements by Y, usually is 1
int stride_x; // stride width: size of the elements in X axis * by step_x + padding
int stride_y; // stride height: size of the elements in Y axis * by step_y + padding
// Point[x,y] = data[width*y*step_y + x*step_x];
// Full data size = width*height
int size_x; // image width
int size_y; // image height
} image2d_t;
#endif // _dsp_types_H_

View File

@@ -0,0 +1,65 @@
// Copyright 2018-2023 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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 _esp_dsp_H_
#define _esp_dsp_H_
#ifdef __cplusplus
extern "C"
{
#endif
// Common includes
#include "dsp_common.h"
#include "dsp_types.h"
// Signal processing
#include "dsps_dotprod.h"
#include "dsps_math.h"
#include "dsps_fir.h"
#include "dsps_biquad.h"
#include "dsps_biquad_gen.h"
#include "dsps_wind.h"
#include "dsps_conv.h"
#include "dsps_corr.h"
#include "dsps_d_gen.h"
#include "dsps_h_gen.h"
#include "dsps_tone_gen.h"
#include "dsps_snr.h"
#include "dsps_sfdr.h"
#include "dsps_fft2r.h"
#include "dsps_fft4r.h"
#include "dsps_dct.h"
// Matrix operations
#include "dspm_matrix.h"
// Support functions
#include "dsps_view.h"
// Image processing functions:
#include "dspi_dotprod.h"
#include "dspi_conv.h"
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
#include "mat.h"
#endif
#endif // _esp_dsp_H_

View File

@@ -0,0 +1,21 @@
// Copyright 2018-2020 spressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
// This file include defenitions that are emulate esp-idf error codes
#ifndef _esp_attr_h_
#define _esp_attr_h_
#endif // _esp_attr_h_

View File

@@ -0,0 +1,29 @@
// Copyright 2018-2020 spressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
// This file include defenitions that are emulate esp-idf error codes
#ifndef _esp_err_h_
#define _esp_err_h_
#include <stdlib.h>
typedef int esp_err_t;
#define ESP_OK 0
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif // M_PI
#endif // _esp_err_h_

View File

@@ -0,0 +1,24 @@
// Copyright 2018-2020 spressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
// This file include defenitions that are emulate esp-idf error codes
#ifndef _esp_log_h_
#define _esp_log_h_
#include <stdlib.h>
#define ESP_LOGD
#endif // _esp_log_h_

View File

@@ -0,0 +1,4 @@
#ifndef _sdkconfig_h_
#define _sdkconfig_h_
#endif // _sdkconfig_h_

View File

@@ -0,0 +1,128 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "dsp_common.h"
#include <stdarg.h>
#define TIE_LOG_ENABLED 1
#if (CONFIG_IDF_TARGET_ESP32S3)
esp_err_t tie_log(int n_regs, ...)
{
#if !TIE_LOG_ENABLED
return ESP_OK;
#else
va_list list;
va_start(list, n_regs);
uint32_t reg_128_bits[4] = {0, 0, 0, 0};
int reg_code;
for (int i = 0; i < n_regs; i++) {
reg_code = va_arg(list, int);
// ACCX register
if ( reg_code == 'a') {
asm volatile("rur.accx_0 %0" : "=a" (reg_128_bits[0]));
asm volatile("rur.accx_1 %0" : "=a" (reg_128_bits[1]));
printf("ACCX - %02x %08x", (unsigned int)reg_128_bits[1], (unsigned int)reg_128_bits[0]);
printf(" --- %llu\n", (long long unsigned)reg_128_bits[1] << 32 | (unsigned int)reg_128_bits[0]);
}
// SAR:_BYTE register
else if ( reg_code == 's') {
asm volatile("rur.sar_byte %0" : "=a" (reg_128_bits[0]));
printf("SAR_BYTE - %d\n", (unsigned int)reg_128_bits[0]);
}
// Q0 - Q7 registers
else if ((reg_code >= 0) && (reg_code <= 7)) {
switch (reg_code) {
case 0 : {
asm volatile("ee.movi.32.a q0, %0, 0" : "=a" (reg_128_bits[0]));
asm volatile("ee.movi.32.a q0, %0, 1" : "=a" (reg_128_bits[1]));
asm volatile("ee.movi.32.a q0, %0, 2" : "=a" (reg_128_bits[2]));
asm volatile("ee.movi.32.a q0, %0, 3" : "=a" (reg_128_bits[3]));
printf("Q0");
break;
}
case 1 : {
asm volatile("ee.movi.32.a q1, %0, 0" : "=a" (reg_128_bits[0]));
asm volatile("ee.movi.32.a q1, %0, 1" : "=a" (reg_128_bits[1]));
asm volatile("ee.movi.32.a q1, %0, 2" : "=a" (reg_128_bits[2]));
asm volatile("ee.movi.32.a q1, %0, 3" : "=a" (reg_128_bits[3]));
printf("Q1");
break;
}
case 2 : {
asm volatile("ee.movi.32.a q2, %0, 0" : "=a" (reg_128_bits[0]));
asm volatile("ee.movi.32.a q2, %0, 1" : "=a" (reg_128_bits[1]));
asm volatile("ee.movi.32.a q2, %0, 2" : "=a" (reg_128_bits[2]));
asm volatile("ee.movi.32.a q2, %0, 3" : "=a" (reg_128_bits[3]));
printf("Q2");
break;
}
case 3 : {
asm volatile("ee.movi.32.a q3, %0, 0" : "=a" (reg_128_bits[0]));
asm volatile("ee.movi.32.a q3, %0, 1" : "=a" (reg_128_bits[1]));
asm volatile("ee.movi.32.a q3, %0, 2" : "=a" (reg_128_bits[2]));
asm volatile("ee.movi.32.a q3, %0, 3" : "=a" (reg_128_bits[3]));
printf("Q3");
break;
}
case 4 : {
asm volatile("ee.movi.32.a q4, %0, 0" : "=a" (reg_128_bits[0]));
asm volatile("ee.movi.32.a q4, %0, 1" : "=a" (reg_128_bits[1]));
asm volatile("ee.movi.32.a q4, %0, 2" : "=a" (reg_128_bits[2]));
asm volatile("ee.movi.32.a q4, %0, 3" : "=a" (reg_128_bits[3]));
printf("Q4");
break;
}
case 5 : {
asm volatile("ee.movi.32.a q5, %0, 0" : "=a" (reg_128_bits[0]));
asm volatile("ee.movi.32.a q5, %0, 1" : "=a" (reg_128_bits[1]));
asm volatile("ee.movi.32.a q5, %0, 2" : "=a" (reg_128_bits[2]));
asm volatile("ee.movi.32.a q5, %0, 3" : "=a" (reg_128_bits[3]));
printf("Q5");
break;
}
case 6 : {
asm volatile("ee.movi.32.a q6, %0, 0" : "=a" (reg_128_bits[0]));
asm volatile("ee.movi.32.a q6, %0, 1" : "=a" (reg_128_bits[1]));
asm volatile("ee.movi.32.a q6, %0, 2" : "=a" (reg_128_bits[2]));
asm volatile("ee.movi.32.a q6, %0, 3" : "=a" (reg_128_bits[3]));
printf("Q6");
break;
}
case 7 : {
asm volatile("ee.movi.32.a q7, %0, 0" : "=a" (reg_128_bits[0]));
asm volatile("ee.movi.32.a q7, %0, 1" : "=a" (reg_128_bits[1]));
asm volatile("ee.movi.32.a q7, %0, 2" : "=a" (reg_128_bits[2]));
asm volatile("ee.movi.32.a q7, %0, 3" : "=a" (reg_128_bits[3]));
printf("Q7");
break;
}
}
printf(" - 0x%08X %08X %08X %08X --- ", (unsigned int)reg_128_bits[3], (unsigned int)reg_128_bits[2], (unsigned int)reg_128_bits[1], (unsigned int)reg_128_bits[0]);
printf("%u %u %u %u %u %u %u %u\n", (unsigned int)reg_128_bits[3] >> 16, (unsigned int)reg_128_bits[3] & 0x0000FFFF,
(unsigned int)reg_128_bits[2] >> 16, (unsigned int)reg_128_bits[2] & 0x0000FFFF,
(unsigned int)reg_128_bits[1] >> 16, (unsigned int)reg_128_bits[1] & 0x0000FFFF,
(unsigned int)reg_128_bits[0] >> 16, (unsigned int)reg_128_bits[0] & 0x0000FFFF);
} else {
printf("Bad register code");
}
}
printf("------------------------------------------------------------------------------------\n");
return ESP_OK;
#endif //TIE_LOG_ENABLED
}
#endif // CONFIG_IDF_TARGET_ESP32S3

View File

@@ -0,0 +1,31 @@
// Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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 "dsp_common.h"
bool dsp_is_power_of_two(int x)
{
return (x != 0) && ((x & (x - 1)) == 0);
}
int dsp_power_of_two(int x)
{
for (size_t i = 0; i < 32; i++) {
x = x >> 1;
if (0 == x) {
return i;
}
}
return 0;
}