aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDamien George2014-04-13 00:30:32 +0100
committerDamien George2014-04-13 00:30:32 +0100
commit8721087661ea607ea68d45b4e518e53607cadbd3 (patch)
tree721324e1334330e2c865c2928348f80e4372989f /examples
parent82c7b1b1d5c8ce0467bd580a86d20c645cec17df (diff)
py: Big improvements to inline assembler.
Improved the Thumb assembler back end. Added many more Thumb instructions to the inline assembler. Improved parsing of assembler instructions and arguments. Assembler functions can now be passed the address of any object that supports the buffer protocol (to get the address of the buffer). Added an example of how to sum numbers from an array in assembler.
Diffstat (limited to 'examples')
-rw-r--r--examples/asmled.py34
-rw-r--r--examples/asmsum.py57
2 files changed, 76 insertions, 15 deletions
diff --git a/examples/asmled.py b/examples/asmled.py
index e0d6c73ce..917d9ba03 100644
--- a/examples/asmled.py
+++ b/examples/asmled.py
@@ -1,4 +1,5 @@
# flash LED #1 using inline assembler
+# this version is overly verbose and uses word stores
@micropython.asm_thumb
def flash_led(r0):
movw(r1, (stm.GPIOA + stm.GPIO_BSRRL) & 0xffff)
@@ -13,69 +14,72 @@ def flash_led(r0):
label(loop1)
# turn LED on
- str(r2, r1, 0)
+ str(r2, [r1, 0])
# delay for a bit
movw(r4, 5599900 & 0xffff)
movt(r4, (5599900 >> 16) & 0xffff)
label(delay_on)
- subs(r4, r4, 1)
+ sub(r4, r4, 1)
cmp(r4, 0)
bgt(delay_on)
# turn LED off
- str(r3, r1, 0)
+ str(r3, [r1, 0])
# delay for a bit
movw(r4, 5599900 & 0xffff)
movt(r4, (5599900 >> 16) & 0xffff)
label(delay_off)
- subs(r4, r4, 1)
+ sub(r4, r4, 1)
cmp(r4, 0)
bgt(delay_off)
# loop r0 times
- subs(r0, r0, 1)
+ sub(r0, r0, 1)
label(loop_entry)
cmp(r0, 0)
bgt(loop1)
-# flash LED #1 using inline assembler
-# this version uses the convenience assembler operation 'movwt'
+# flash LED #2 using inline assembler
+# this version uses half-word sortes, and the convenience assembler operation 'movwt'
@micropython.asm_thumb
def flash_led_v2(r0):
- movwt(r1, stm.GPIOA + stm.GPIO_BSRRL)
- movwt(r2, 1 << 13)
- movwt(r3, 1 << (16 + 13))
+ # get the GPIOA address in r1
+ movwt(r1, stm.GPIOA)
+
+ # get the bit mask for PA14 (the pin LED #2 is on)
+ movw(r2, 1 << 14)
b(loop_entry)
label(loop1)
# turn LED on
- str(r2, r1, 0)
+ strh(r2, [r1, stm.GPIO_BSRRL])
# delay for a bit
movwt(r4, 5599900)
label(delay_on)
- subs(r4, r4, 1)
+ sub(r4, r4, 1)
cmp(r4, 0)
bgt(delay_on)
# turn LED off
- str(r3, r1, 0)
+ strh(r2, [r1, stm.GPIO_BSRRH])
# delay for a bit
movwt(r4, 5599900)
label(delay_off)
- subs(r4, r4, 1)
+ sub(r4, r4, 1)
cmp(r4, 0)
bgt(delay_off)
# loop r0 times
- subs(r0, r0, 1)
+ sub(r0, r0, 1)
label(loop_entry)
cmp(r0, 0)
bgt(loop1)
+flash_led(5)
flash_led_v2(5)
diff --git a/examples/asmsum.py b/examples/asmsum.py
new file mode 100644
index 000000000..07e71c738
--- /dev/null
+++ b/examples/asmsum.py
@@ -0,0 +1,57 @@
+@micropython.asm_thumb
+def asm_sum_words(r0, r1):
+
+ # r0 = len
+ # r1 = ptr
+ # r2 = sum
+ # r3 = dummy
+ mov(r2, 0)
+
+ b(loop_entry)
+
+ label(loop1)
+ ldr(r3, [r1, 0])
+ add(r2, r2, r3)
+
+ add(r1, r1, 4)
+ sub(r0, r0, 1)
+
+ label(loop_entry)
+ cmp(r0, 0)
+ bgt(loop1)
+
+ mov(r0, r2)
+
+@micropython.asm_thumb
+def asm_sum_bytes(r0, r1):
+
+ # r0 = len
+ # r1 = ptr
+ # r2 = sum
+ # r3 = dummy
+ mov(r2, 0)
+
+ b(loop_entry)
+
+ label(loop1)
+ ldrb(r3, [r1, 0])
+ add(r2, r2, r3)
+
+ add(r1, r1, 1)
+ sub(r0, r0, 1)
+
+ label(loop_entry)
+ cmp(r0, 0)
+ bgt(loop1)
+
+ mov(r0, r2)
+
+import array
+
+b = array.array('l', (100, 200, 300, 400))
+n = asm_sum_words(len(b), b)
+print(b, n)
+
+b = array.array('b', (10, 20, 30, 40, 50, 60, 70, 80))
+n = asm_sum_bytes(len(b), b)
+print(b, n)