Guest User

26-byte Blinky for Blue Pill (STM32F103)

a guest
May 19th, 2021
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. @; blinky.s
  2.  
  3. @; Shortest blinky program for Blue Pill (STM32F103) ever written.
  4. @; Binary size is only 26 bytes long.
  5.  
  6. @; Build instructions:
  7. @; arm-none-eabi-as blinky.s -o blinky.o # may worn about alignment, it's ok
  8. @; arm-none-eabi-ld blinky.o -Ttext=0x08000000 -o blinky.elf
  9. @; arm-none-eabi-objcopy blinky.elf -O binary blinky.bin
  10. @; stat -c %s blinky.bin
  11.  
  12. @; CPU runs at default 8MHz internal clock.
  13.  
  14. .syntax unified
  15. .cpu cortex-m3
  16. .thumb
  17. .global _start
  18.  
  19. @; Bit-band addresses:
  20. @; 2b 1b (see lines 42-48)
  21. PORTC_ENABLE_BIT = 0x42420310 + 3
  22. PC13_MODE0_BIT = 0x422200d0
  23. PC13_OUTPUT_BIT = 0x422201b4
  24. DIFF_228 = PC13_OUTPUT_BIT - PC13_MODE0_BIT
  25.  
  26. .text
  27. @; Initial stack pointer value is used for storing a register address.
  28. stack_pointer_value: .word PC13_MODE0_BIT
  29. reset_vector: .word _start + 1
  30.  
  31. _start:
  32. @; This and next `ldr` instruction align `pc` to word boundary.
  33. adds r1, #1
  34.  
  35. @; Loading address relative to `pc + #0` is size-efficient.
  36. ldr r0, [pc]
  37. @; Next word evaluates to
  38. @; lsls r3, r2, #12
  39. @; negs r2, r0
  40. .word PORTC_ENABLE_BIT
  41.  
  42. @; Next is undocumented behaviour.
  43. @; Writing to `bit-band address + 3` value `0x0100`
  44. @; is the same as writind value `0x0001` to `bit-band address + 0`
  45. @; 2b (second byte of `r0` has 1 in LSB)
  46. str r0, [r0]
  47. @; 1b (first byte of `r0` also has 1 in LSB)
  48. str r0, [sp]
  49.  
  50. @; `r1` holds a counter value; writing it's 15'th bit
  51. @; to the output makes led blink with a certain frequency.
  52. movs r0, r1, lsr #14
  53. @; `sp` contains the bit-band address of `PC13 mode bit 0`,
  54. @; addind 228 gives the address of `PC13 bit in output register`.
  55. str r0, [sp, #DIFF_228]
  56.  
  57. @; short branch to start
  58. b.n _start
  59.  
Advertisement
Add Comment
Please, Sign In to add comment