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.

MicroPython で RP2040 の PIO を使うサンプル その2

Last updated at Posted at 2023-07-06

前回の続きです。

System と PIO で値をやり取りする

  • ステートマシンに値を送り,-1 して戻してもらう
main.py
import rp2

@rp2.asm_pio()

def prog():
    pull()		# TX FIFO -> OSR
    out(x,32)	# OSR -> x 32bit
    jmp(x_dec,"JMP")	# x=x-1, then if x is not zero, jump to "JMP"
    label("JMP")
    in_(x,32)	# x -> ISR 32bit
    push()		# ISR ->RX FIFO

state_machine = rp2.StateMachine(0, prog)

state_machine.active(1)
print("activate")

print("put 123456")
state_machine.put(123456) # System -> TX FIFO

print("get",state_machine.get()) # RX FIFO -> System

実行例

>>> %Run -c $EDITOR_CONTENT
activate
put data 123456
get data 123455
>>> 

GPIOピンの値を読む

XIAO Expansion Board のボタン P27 を例題にします。
CPU内部のプルアップ抵抗を必要とし,押すとLになります。

  • P27の値を System に伝える
main.py
import machine, rp2

machine.Pin(27, machine.Pin.IN, machine.Pin.PULL_UP)

@rp2.asm_pio()
def prog():
    in_(pins, 1) # pins -> ISR
    push() # ISR -> RX FIFO

state_machine = rp2.StateMachine(
    0, prog, in_base=machine.Pin(27)
    )
state_machine.active(1)
print(machine.Pin(27).value()) # 押されていれば 0 と表示
  • P27の値をP25にコピーする in_() を使う場合
main.py
import machine, rp2

machine.Pin(27, machine.Pin.IN, machine.Pin.PULL_UP)

@rp2.asm_pio(out_init=rp2.PIO.OUT_HIGH)
def prog():
    in_(pins, 1)   # (in)pins -> ISR 1bit
    mov(pins, isr) # isr -> (out)pins

state_machine = rp2.StateMachine(
    0, prog,
    in_base=machine.Pin(27),
    out_base=machine.Pin(25)
    )
state_machine.active(1) # ここで終了するが(P27)ボタンを押すと(P25)LEDがつく
  • P27の値をP25にコピーする mov() を使う場合

(2023/7/7 追記)という記事を投稿しましたが,

main.py
    mov(x, pins) # (in)pins -> x
    mov(pins, x) # x -> (out)pins

としたとき, xには所望のP27以外の値も書かれてしまうようです。よってこのコード例を削除します。

  • P27が L になるまで待つ
main.py
import machine, rp2

machine.Pin(27, machine.Pin.IN, machine.Pin.PULL_UP)

@rp2.asm_pio()
def prog():
    wait(0, gpio, 27) # P27 が L になるまで停止 直接ピン番号が指定可能
    set(x, 0) # 0 -> x
    mov(isr, x) # x -> ISR
    push() # System が ISR を get するまで停止

state_machine = rp2.StateMachine(0, prog)

state_machine.active(1)

print(state_machine.get()) # PIOが値を返すまで待つ。かならず 0 が返される

その3... がんばります!(一応宣言)

0
0
0

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?