File: asmleds.py

package info (click to toggle)
firmware-microbit-micropython 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 25,372 kB
  • sloc: ansic: 83,496; cpp: 27,665; python: 2,475; asm: 274; makefile: 227; sh: 25
file content (57 lines) | stat: -rw-r--r-- 1,665 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
This script uses the inline assembler to make the LEDs light up
in a pattern based on how they are multiplexed in rows/cols.
"""

# row pins: 13, 14, 15
# col pins: 4..12 inclusive
# GPIO words starting at 0x50000500:
#   RESERVED, OUT, OUTSET, OUTCLR, IN, DIR, DIRSET, DIRCLR

@micropython.asm_thumb
def led_cycle():
    b(START)

    # DELAY routine
    label(DELAY)
    mov(r3, 0xa0)
    lsl(r3, r3, 11)
    label(delay_loop)
    sub(r3, 1)
    bne(delay_loop)
    bx(lr)

    label(START)

    cpsid('i')          # disable interrupts so we control the display

    mov(r0, 0x50)       # r0=0x50
    lsl(r0, r0, 16)     # r0=0x500000
    add(r0, 0x05)       # r0=0x500005
    lsl(r0, r0, 8)      # r0=0x50000500 -- this points to GPIO registers
    mov(r1, 0b111)
    lsl(r1, r1, 13)     # r1=0xe000
    str(r1, [r0, 8])    # pull all rows high

    mov(r1, 1 << 4)     # r1 holds current col bit
    mov(r2, 9)          # r2 holds number of cols left
    label(loop_on)
    str(r1, [r0, 12])   # pull col low to turn LEDs on
    bl(DELAY)           # wait
    lsl(r1, r1, 1)      # shift to next col
    sub(r2, 1)          # decrease col counter
    bne(loop_on)        # loop while there are still cols left

    mov(r1, 1 << 4)     # r1 holds current col bit
    mov(r2, 9)          # r2 holds number of cols left
    label(loop_off)
    str(r1, [r0, 8])    # pull col high to turn LEDs off
    bl(DELAY)           # wait
    lsl(r1, r1, 1)      # shift to next col
    sub(r2, 1)          # decrease col counter
    bne(loop_off)       # loop while there are still cols left

    cpsie('i')      # enable interrupts

for i in range(4):
    led_cycle()