通过串联的(主机)PC的串行线对ArduinoAtmel328进行机器编码。

问题描述 投票:0回答:1

我正试图写一个程序,为 AVR Atmel 328芯片 这将使我能够通过串行线向芯片发送机器代码指令,在芯片上运行执行这些指令,并通过读取芯片内存和通过串行线将内容发送回来来查询结果。 这就是思想的起源。弗兰克-谢尔盖特提出的3个指令Forth.

远程存储和远程获取指令似乎工作正常,但。我一直无法得到远程调用指令(XCALL() 函数)来工作。 我采取的方法是通过将一个16位的地址投射为函数指针来胁迫它进入一个子程序。

以下是在Arduino Nano上运行的代码(在Arduino IDE中编译,并通过bootloader使用USB线上传

任何见解将是非常感激! (如果有帮助的话,我可以添加二进制代码,我的远程指令等)。

先谢谢你!

// Remote serial development platform for machine coding ArduinoNano/ATmel328, AKE, Jun 9, 2020

// global variables
unsigned char byt;                  // command token from host
unsigned char mem[255];             // memory window under programmatic control
unsigned char dat;                  // data byte from host
unsigned char adr_lo;               // address from host, low byte first
unsigned char adr_hi;           
unsigned short adr;                 // combined 16-bit address
typedef void (*GeneralFunction)();  // template to call memory address (as a subroutine)

void setup() {
  Serial.begin(9600);              // Turn UART serial protocol ON for comms with host PC
  Serial.write(0xFF);              // magic number to verify transmission
  Serial.write(0xFE);
  Serial.write((int) mem);         // Informs you of the writeable address space available (LSB, MSB) for tethered memory access
  Serial.write((int) mem>>8);      
}

char get_byte(void) {
    while (!(Serial.available()>0)) { delay(50); }
    return Serial.read();
}
short get_adr(void) {
    adr_lo=get_byte();
    adr_hi=get_byte();
    return ( (short)adr_hi<<8 )   |   (short)adr_lo;
}
void xstore() {             // Instruction 1 = xstore(data,adr_lo,adr_hi).  Store byte from host at indicated address in target.
    dat=get_byte();
    adr=get_adr();
    *(char *)adr=dat;
}
void xfetch() {             //  Instruction 2 = xfetch(adr_lo,adr_hi).  Read byte from target memory and return to host.
        adr=get_adr();
        dat=*(char *)adr;
        Serial.write(dat);
}
void xcall() {              //  Instruction 3 = xcall(adr_lo,adr_hi).  Execute subroutine in target memory from indicated address.
                            //  WARNING!  User must have stored RET instruction to send control back to serial monitor.
        adr=get_adr();
        GeneralFunction fGf=adr;
        fGf();
}

void loop() {
    byt = get_byte();               // user specified instruction token (1,2,3)
    if(byt == 0x01 )      { xstore(); } 
    else if (byt == 0x02) { xfetch(); } 
    else if (byt == 0x3 ) { xcall(); } // else ignore any other serial inputs
}
arduino function-pointers shared-memory avr atmel
1个回答
2
投票

你没有告诉我们你的芯片的具体名称,但我怀疑你使用的是ATmega328P。

ATmega328P不能从RAM中执行指令。 你需要想办法把代码写到闪存中,然后才能执行。 你可能想使用一个引导加载器,比如说 Optiboot 来将程序写入闪存,或者您可以研究一下Optiboot,看看它是如何工作的。 注意,flash的额定擦写周期有限(一般为10000)。

另外,你的C++语法是错误的。 要调用一个函数,你总是需要使用括号。 所以你应该写成 fGf()而不是仅仅 fGf.

© www.soinside.com 2019 - 2024. All rights reserved.