将ARM软件中断移植到ESP32 / FreeRTOS软件中断

如何解决将ARM软件中断移植到ESP32 / FreeRTOS软件中断

我要为ESP32移植PJRC Teensy音频库,因为我想与API保持兼容。我在如何移植AudioStream()函数时遇到麻烦。到目前为止,我已经将IRAM_ATTR添加到了中断函数中,但是我不确定这是否正确。

https://github.com/PaulStoffregen/cores/blob/master/teensy3/AudioStream.cpp#L302

到目前为止,我的代码如下:

#include <Arduino.h>
#include "AudioStream.h"

#define MAX_AUDIO_MEMORY 49152

#define NUM_MASKS  (((MAX_AUDIO_MEMORY / AUDIO_BLOCK_SAMPLES / 2) + 31) / 32)

audio_block_t * AudioStream::memory_pool;
uint32_t AudioStream::memory_pool_available_mask[NUM_MASKS];
uint16_t AudioStream::memory_pool_first_mask;

uint16_t AudioStream::cpu_cycles_total = 0;
uint16_t AudioStream::cpu_cycles_total_max = 0;
uint16_t AudioStream::memory_used = 0;
uint16_t AudioStream::memory_used_max = 0;

void AudioStream::initialize_memory(audio_block_t *data,unsigned int num)
{
    unsigned int i;
    unsigned int maxnum = MAX_AUDIO_MEMORY / AUDIO_BLOCK_SAMPLES / 2;

    //Serial.println("AudioStream initialize_memory");
    //delay(10);
    if (num > maxnum) num = maxnum;
    __disable_irq();
    memory_pool = data;
    memory_pool_first_mask = 0;
    for (i=0; i < NUM_MASKS; i++) {
        memory_pool_available_mask[i] = 0;
    }
    for (i=0; i < num; i++) {
        memory_pool_available_mask[i >> 5] |= (1 << (i & 0x1F));
    }
    for (i=0; i < num; i++) {
        data[i].memory_pool_index = i;
    }
    __enable_irq();

}

// Allocate 1 audio data block.  If successful
// the caller is the only owner of this new block
audio_block_t * AudioStream::allocate(void)
{
    uint32_t n,index,avail;
    uint32_t *p,*end;
    audio_block_t *block;
    uint32_t used;

    p = memory_pool_available_mask;
    end = p + NUM_MASKS;
    __disable_irq();
    index = memory_pool_first_mask;
    p += index;
    while (1) {
        if (p >= end) {
            __enable_irq();
            //Serial.println("alloc:null");
            return NULL;
        }
        avail = *p;
        if (avail) break;
        index++;
        p++;
    }
    n = __builtin_clz(avail);
    avail &= ~(0x80000000 >> n);
    *p = avail;
    if (!avail) index++;
    memory_pool_first_mask = index;
    used = memory_used + 1;
    memory_used = used;
    __enable_irq();
    index = p - memory_pool_available_mask;
    block = memory_pool + ((index << 5) + (31 - n));
    block->ref_count = 1;
    if (used > memory_used_max) memory_used_max = used;
    //Serial.print("alloc:");
    //Serial.println((uint32_t)block,HEX);
    return block;
}

// Release ownership of a data block.  If no
// other streams have ownership,the block is
// returned to the free pool
void AudioStream::release(audio_block_t *block)
{
    //if (block == NULL) return;
    uint32_t mask = (0x80000000 >> (31 - (block->memory_pool_index & 0x1F)));
    uint32_t index = block->memory_pool_index >> 5;

    __disable_irq();
    if (block->ref_count > 1) {
        block->ref_count--;
    } else {
        //Serial.print("reles:");
        //Serial.println((uint32_t)block,HEX);
        memory_pool_available_mask[index] |= mask;
        if (index < memory_pool_first_mask) memory_pool_first_mask = index;
        memory_used--;
    }
    __enable_irq();
}

void AudioStream::transmit(audio_block_t *block,unsigned char index)
{
    for (AudioConnection *c = destination_list; c != NULL; c = c->next_dest) {
        if (c->src_index == index) {
            if (c->dst.inputQueue[c->dest_index] == NULL) {
                c->dst.inputQueue[c->dest_index] = block;
                block->ref_count++;
            }
        }
    }
}

audio_block_t * AudioStream::receiveReadOnly(unsigned int index)
{
    audio_block_t *in;

    if (index >= num_inputs) return NULL;
    in = inputQueue[index];
    inputQueue[index] = NULL;
    return in;
}

audio_block_t * AudioStream::receiveWritable(unsigned int index)
{
    audio_block_t *in,*p;

    if (index >= num_inputs) return NULL;
    in = inputQueue[index];
    inputQueue[index] = NULL;
    if (in && in->ref_count > 1) {
        p = allocate();
        if (p) memcpy(p->data,in->data,sizeof(p->data));
        in->ref_count--;
        in = p;
    }
    return in;
}

void AudioConnection::connect(void)
{
    AudioConnection *p;

    if (isConnected) return;
    if (dest_index > dst.num_inputs) return;
    __disable_irq();
    p = src.destination_list;
    if (p == NULL) {
        src.destination_list = this;
    } else {
        while (p->next_dest) {
            if (&p->src == &this->src && &p->dst == &this->dst
                && p->src_index == this->src_index && p->dest_index == this->dest_index) {
                //Source and destination already connected through another connection,abort
                __enable_irq();
                return;
            }
            p = p->next_dest;
        }
        p->next_dest = this;
    }
    this->next_dest = NULL;
    src.numConnections++;
    src.active = true;

    dst.numConnections++;
    dst.active = true;

    isConnected = true;

    __enable_irq();
}

void AudioConnection::disconnect(void)
{
    AudioConnection *p;

    if (!isConnected) return;
    if (dest_index > dst.num_inputs) return;
    __disable_irq();
    // Remove destination from source list
    p = src.destination_list;
    if (p == NULL) {
//>>> PAH re-enable the IRQ
        __enable_irq();
        return;
    } else if (p == this) {
        if (p->next_dest) {
            src.destination_list = next_dest;
        } else {
            src.destination_list = NULL;
        }
    } else {
        while (p) {
            if (p == this) {
                if (p->next_dest) {
                    p = next_dest;
                    break;
                } else {
                    p = NULL;
                    break;
                }
            }
            p = p->next_dest;
        }
    }
//>>> PAH release the audio buffer properly
    //Remove possible pending src block from destination
    if(dst.inputQueue[dest_index] != NULL) {
        AudioStream::release(dst.inputQueue[dest_index]);
        // release() re-enables the IRQ. Need it to be disabled a little longer
        __disable_irq();
        dst.inputQueue[dest_index] = NULL;
    }

    //Check if the disconnected AudioStream objects should still be active
    src.numConnections--;
    if (src.numConnections == 0) {
        src.active = false;
    }

    dst.numConnections--;
    if (dst.numConnections == 0) {
        dst.active = false;
    }

    isConnected = false;

    __enable_irq();
}

bool AudioStream::update_scheduled = false;

bool AudioStream::update_setup(void)
{
    if (update_scheduled) return false;
    NVIC_SET_PRIORITY(IRQ_SOFTWARE,208); // 255 = lowest priority
    NVIC_ENABLE_IRQ(IRQ_SOFTWARE);
    update_scheduled = true;
    return true;
}

void AudioStream::update_stop(void)
{
    NVIC_DISABLE_IRQ(IRQ_SOFTWARE);
    update_scheduled = false;
}

AudioStream * AudioStream::first_update = NULL;

void IRAM_ATTR software_isr(void) // AudioStream::update_all()
{
    AudioStream *p;

    uint32_t totalcycles = micros();

    //digitalWriteFast(2,HIGH);
    for (p = AudioStream::first_update; p; p = p->next_update) {
        if (p->active) {
            uint32_t cycles = get_cycles_esp32(); 
            p->update();
            // TODO: traverse inputQueueArray and release
            // any input blocks that weren't consumed?
            cycles = ( get_cycles_esp32() - cycles) >> 4;
            p->cpu_cycles = cycles;
            if (cycles > p->cpu_cycles_max) p->cpu_cycles_max = cycles;
        }
    }

    totalcycles = micros() - totalcycles;
    AudioStream::cpu_cycles_total = totalcycles;
    if (totalcycles > AudioStream::cpu_cycles_total_max)
        AudioStream::cpu_cycles_total_max = totalcycles;
}

其中

uint32_t get_cycles_esp32(){ uint32_t ccount; RSR(CCOUNT,ccount); return ccount; }

现在我无法弄清楚什么是NVIC_SET_PRIORITY,NVIC_ENABLE_IRQ,NVIC_DISABLE_IRQ,并且我不确定是否使用ESP32中断句柄或FreeRTOS。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-