我有以下 C 源文件,需要删除一些代码并添加一些代码以绕过 Nios_2_r2c 处理器上的数据缓存。我不知道如何做到这一点。
文件:switch.c
#include "system.h"
#include "pio_regs.h"
#include "bsu_macros.h"
#include "switches.h"
static struct pio_regs *SW = (struct pio_regs *)SWITCH_BASE;
static REGISTER SH_SW;
bits get_RUN ( void ) {
SH_SW = SW->data;
return getbit(SH_SW, 17);
}
文件:ledr.c
#include "system.h"
#include "pio_regs.h"
#include "bsu_macros.h"
#include "ledr.h"
static struct pio_regs *LEDR = (struct pio_regs *)LEDR_BASE;
static REGISTER SH_LEDR;
void LEDR_Init ( void ) {
SH_LEDR = 0;
LEDR->data = 0;
}
void show_RUN ( bits RUN ) {
SH_LEDR = putbit (SH_LEDR, RUN, 12);
LEDR->data = SH_LEDR;
}
使用 I/O 读写通过内联汇编获得它:
文件:switch.c
#include "system.h"
#include "pio_regs.h"
#include "bsu_macros.h"
#include "switches.h"
static struct pio_regs *SW = (struct pio_regs *)SWITCH_BASE;
static REGISTER SH_SW;
bits get_RUN ( void ) {
//SH_SW = SW->data;
__asm("ldwio %0, %1" : "=r"(SH_SW) : "m"(SW->data));
return getbit(SH_SW, 17);
}
文件:ledr.c
#include "system.h"
#include "pio_regs.h"
#include "bsu_macros.h"
#include "ledr.h"
static struct pio_regs *LEDR = (struct pio_regs *)LEDR_BASE;
static REGISTER SH_LEDR;
void LEDR_Init ( void ) {
SH_LEDR = 0;
//LEDR->data = 0;
__asm("stwio %0, %1" : "=r"(SH_LEDR) : "m"(SW->data));
}
void show_RUN ( bits RUN ) {
SH_LEDR = putbit (SH_LEDR, RUN, 12);
//LEDR->data = SH_LEDR;
__asm("stwio %0, %1" : "=r"(SH_SW) : "m"(SW->data));
}
最佳答案
您可以使用-mno-cache-volatile
并将变量声明为 volatile
。事实上,通过读/写某些寄存器来实现的 MMIO必须始终是 volatile 的。
-mno-cache-volatile
-mcache-volatile
Volatile memory access bypass the cache using the I/O variants of the load and store instructions. The default is not to bypass the cache.
另一个选项是始终通过 -mbypass-cache
绕过缓存
查看
关于c - 绕过 Nios II 处理器中的数据缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15821022/