0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

インラインアセンブラ 小物 w/Arduino @ RP2040

Posted at
  • Arduino IDE 2.1.1 on Mac
  • Raspberry Pi Pico/RP2040 by Earle F.Philhower, III 2.7.0
  • Raspberry Pi Pico(互換機)

1秒待つ

void setup() {

  Serial.begin(9600);
  while (!Serial) {}
  Serial.println("Boot RP2040");

  Serial.println("start!");

  uint32_t time0 = micros();

  asm volatile(
    "LDR r0, =44333333 \r\n"  // (1 + 44333333 * 3)=133,000,000 clocks.
    ".loop: \r\n"
    "SUB r0, #1 \r\n"  // 1 cycle
    "BNE .loop \r\n"   // 2 cycle: if not zero, then branch
    :
    :
    : "r0"
    );

  uint32_t time1 = micros();

  Serial.print("time0 ");
  Serial.println(time0);

  Serial.print("time1 ");
  Serial.println(time1);

  Serial.print("takes ");
  Serial.print(time1 - time0);
  Serial.print("[usec] -> ");

  Serial.print((time1 - time0) / 1000000);
  Serial.println("[sec]");

}

void loop() {
}

実行例

Boot RP2040
start!
time0 1031987
time1 2042373
takes 1010386[usec] -> 1[sec]

-基本的に動作周波数のほかはNucleoとおなじでOKでした。

  • Earle さんのコアは標準で133Mhzでした。mbedコアの場合は125MHzでしたので,それなりに修正すればOKでした。

SysTickでクロック数確認

main.ino
void setup() {

  Serial.begin(9600);
  while (!Serial) {}
  Serial.println("Boot RP2040");

  delay(3000);

}

void loop() {
  uint32_t out1, out2;

  asm volatile(
    "SYSTICK_CVR = 0xE000E018 \r\n"
    "LDR r0, =SYSTICK_CVR \r\n"    
    //"NOP \r\n"
    //"MOV %[out1], %[systick_val_address] \r\n"
    "LDR %[out1], [r0] \r\n" // 1 cycle
    "NOP \r\n"                                   // 1 cycle
    "NOP \r\n"                                   // 1 cycle
    "NOP \r\n"                                   // 1 cycle
    "NOP \r\n"                                   // 1 cycle
    "NOP \r\n"                                   // 1 cycle
    "NOP \r\n"                                   // 1 cycle
    "NOP \r\n"                                   // 1 cycle
    "NOP \r\n"                                   // 1 cycle
    "LDR %[out2], [r0] \r\n" // 1 cycle
    : [out1] "=r"(out1) , [out2] "=r"(out2)     
    : 
    : "r0"
  );

  Serial.print("out1 ");
  Serial.println(out1);

  Serial.print("out2 ");
  Serial.println(out2);

  Serial.print(out1-out2);
  Serial.println(" clocks.");

  delay(2000);
  // while(1) {}
}

実行例 なぜか1回目が遅いです。シリアルモニターで割り込み?待ちが入っている模様

Boot RP2040
out1 9762214
out2 9761953
261 clocks.
out1 12169678
out2 12169668
10 clocks.
out1 14582775
out2 14582765
10 clocks.

Windowsだとうんと遅いです。よくわかりません...

Boot RP2040
out1 12535788
out2 12535527
261 clocks.
out1 14941581
out2 14941321
260 clocks.
out1 575396
out2 575285
111 clocks.
out1 2989888
out2 2989777
111 clocks.

こちらのサンプルは mbed コアだと動きました。SysTickの扱いはまだまだ私の知らないことがあるようで...

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?