Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 6.06 KB | None | 0 0
  1. @ STDNUM001 STDNUM002
  2.     .syntax unified
  3.     .global _start
  4. vectors:
  5.     .word 0x20002000
  6.     .word _start + 1
  7. _start:
  8.     @ enable clock for GPIOA and GPIOB
  9.     LDR R0, RCC_BASE
  10.     LDR R1, [R0, #0x14]     @ RCC_AHBENR
  11.     LDR R2, AHBENR_IOPABEN  @ pattern to set PA and PB bits
  12.     ORRS R1, R1, R2
  13.     STR R1, [R0, #0x14]    @ write back
  14.     @ enable clock for ADC
  15.     LDR R1, [R0, #0x18]    @ RCC_APB2ENR
  16.     LDR R2, APB2ENR_ADCEN  @ pattern to set ADCEN bit
  17.     ORRS R1, R1, R2
  18.     STR R1, [R0, #0x18]    @ RCC_APB2ENR write back
  19.     @ set LEDs to outputs
  20.     LDR R0, GPIOB_BASE
  21.     LDR R1, LEDS_OUTPUTS
  22.     STR R1, [R0, #0]
  23.     @ set pullup for PA3
  24.     LDR R0, GPIOA_BASE
  25.     LDR R1, [R0, #0x0C]    @ GPIOA_PUPDR
  26.     MOVS R2, #0b01010101
  27.     ORRS R1, R1, R2
  28.     STR R1, [R0, #0x0C]    @ GPIOA_PUPDR write back
  29.     @ set PA5 and PA6 to analogue
  30.     LDR R1, [R0, #0]       @ GPIOA_MODER
  31.     LDR R2, MODER_5_6_ANALOGUE
  32.     ORRS R1, R1, R2
  33.     STR R1, [R0, #0]       @ GPIOA_MODER write back
  34.     @ ADC resolution to 8-bit
  35.     LDR R0, ADC_BASE
  36.     LDR R1, [R0, #0x0C]
  37.     MOVS R2, #0b10000
  38.     ORRS R1, R1, R2
  39.     STR R1, [R0, #0x0C]
  40.     @ enable ADC
  41.     LDR R0, ADC_BASE
  42.     MOVS R1, #1            @ ADEN = bit0
  43.     STR R1, [R0, #0x08]    @ ADC_CR
  44. adrdy_loop:
  45.     LDR R1, [R0, #0]       @ ADC_ISR
  46.     MOVS R2, #1
  47.     ANDS R1, R1, R2
  48.     BEQ adrdy_loop
  49.  
  50.     LDR R0, DATA_ADDR        @ soure pointer
  51.     LDR R1, DATA_END_ADDR    @ out-of-bounds pointer
  52.     LDR R2, RAM_START        @ destination pointer
  53.     MOVS R3, #0              @ counter for number of bytes copied
  54. to_RAM_loop:
  55.     CMP R0, R1               @ is the source pointer out of bounds?
  56.     BEQ copy_to_RAM_done     @ if so, don't copy any more
  57.    LDR R4, [R0]             @ copy from source...
  58.    STR R4, [R2]             @ ...to destination
  59.    ADDS R0, #4              @ interment source and destination pointers by a word
  60.    ADDS R2, #4
  61.    ADDS R3, #4              @ increment counter by number of bytes copied
  62.    B to_RAM_loop
  63.  
  64. @ CRITICAL! When the program hits this label, the automarker will
  65. @ stop execution and verify the contents of RAM.
  66. copy_to_RAM_done:
  67.  
  68.     MOVS R1, #0x80              @ Iterate for all numbers, see if we get any matches in our data set(for i=-128; i<128; i++)
  69.     SXTB R1, R1
  70.    
  71.     LDR R0, RAM_START           @ pointer to load from RAMSTART
  72. outer_for_loop:
  73.     MOVS R2, #0                 @ R2 will be used to OFFSET as we iterate through data set
  74.     MOVS R5, R3                 @ number of bytes to process @ R3 is the counter of number of bytes to process.
  75.     inner_for_loop:
  76.         LDRSB R4, [R0, R2]      @ load from RAM
  77.         CMP R4, R1              @ Compare byte in RAM with each value
  78.         BNE no_hit              @ If not the same was not match
  79.         PUSH {R4}               @ if the same, add to stack
  80.     no_hit:
  81.         ADDS R2, #1             @ Increment RAM address offset
  82.         SUBS R5, #1             @ subtract bytes processed this inner run
  83.         BNE inner_for_loop
  84.     ADDS R1, #1                 @ increment checking value (i++)
  85.     CMP R1, #0x80               @ if value wrapped around, exit
  86.     BNE outer_for_loop            
  87.  
  88. @ CRITICAL! The marker will verify contents of stack
  89. @ the stack when the program hits this label.
  90. order_to_stack_done:
  91.  
  92.    LDR R0, GPIOB_BASE
  93.    MOV R4, SP
  94.    MOVS R5, #4
  95.    MULS R3, R3, R5
  96.    SUBS R3, #4
  97.    LDR R1, [R4, #0]        @ larger element (B) on top
  98.    LDR R2, [R4, R3]        @ smaller (A) is located bottom of the stack
  99.        
  100.  
  101. main_loop:
  102.    STR R2, [R0, #0x14]    @ display smaller of pair (A)
  103.    BL delay               @ call delay subroutine
  104.    STR R1, [R0, #0x14]    @ display larger of pair (B)
  105.    BL delay               @ call delay subroutine
  106.    B main_loop            @ loopidy loop loop loop
  107.  
  108. delay:
  109.    PUSH {R0, R1, R2, R3, R4, R5}      @ backup registers to the stack
  110.    LDR R0, DEFAULT_DELAY     @ assume full 1.2 second delay. Will be overridden if SW0 held.
  111.    LDR R1, GPIOA_BASE     @ get contents of GPIOA_IDR
  112.    LDR R2, [R1, #0x10]
  113.    MOVS R3, #0b100            @ mask out all bits except bit2 == SW2
  114.    ANDS R2, R2, R3
  115.    BNE delay_loop         @ if the button is held, skip the ADC fetching logic and begin delay
  116.    LDR R0, DELAY_OFFSET   @ the minimum delay. Will be added to by ADC value.
  117.    LDR R1, ADC_BASE
  118.    @ select channel 5 (POT0)
  119.    MOVS R2, #0b100000
  120.    STR R2, [R1, #0x28]
  121.    @ run a conversion by setting ADSTART and waiting for it to go low
  122.    MOVS R2, #0b100   @ ADSTART
  123.    STR R2, [R1, #0x08]  @ ADC_CR
  124. eoc_loop_0:
  125.    LDR R3, [R1, #0]
  126.    ANDS R3, R3, R2
  127.    BEQ eoc_loop_0
  128.    LDR R4, [R1, #0x40]     @ R4 will hold the value of POT0
  129.    @ select channel 6 (POT1)
  130.    MOVS R2, #0b1000000
  131.    STR R2, [R1, #0x28]
  132.    @ run a conversion by setting ADSTART and waiting for it to go low
  133.    MOVS R2, #0b100   @ ADSTART
  134.    STR R2, [R1, #0x08]  @ ADC_CR
  135. eoc_loop_1:
  136.    LDR R3, [R1, #0]
  137.    ANDS R3, R3, R2
  138.    BEQ eoc_loop_1
  139.    LDR R5, [R1, #0x40]  @ R5 will hold the value of POT1
  140.    LDR R1, DELAY_MULTIPLIER
  141.    CMP R5, R4
  142.    BHI pot0_larger
  143.    MULS R1, R1, R5
  144.    ADDS R0, R0, R1
  145.    B delay_loop
  146. pot0_larger:
  147.    MULS R1, R1, R4
  148.    ADDS R0, R0, R1
  149. delay_loop:
  150.    SUBS R0, #1
  151.    BNE delay_loop
  152.    POP {R0, R1, R2, R3, R4, R5}    @ restore backed up registers
  153.    BX LR
  154.  
  155.  
  156.    .align
  157. @ you can define more literals here.
  158.  
  159. RCC_BASE: .word 0x40021000
  160. AHBENR_IOPABEN: .word 0b11 << 17
  161. APB2ENR_ADCEN: .word 0b1 << 9
  162. GPIOB_BASE: .word 0x48000400
  163. LEDS_INPUTS:  .word 0xFFFF0000
  164. LEDS_OUTPUTS: .word 0x00005555
  165. GPIOA_BASE: .word  0x48000000
  166. MODER_5_6_ANALOGUE: .word 0b1111 << 10
  167. ADC_BASE: .word 0x40012400
  168. INIT: .word 0x80
  169.  
  170. RAM_START: .word 0x20000000
  171. DATA_ADDR: .word DATA
  172. DATA_END_ADDR: .word DATA_END
  173.  
  174. DEFAULT_DELAY: .word 2400000
  175. DELAY_OFFSET:  .word 1200000
  176. DELAY_MULTIPLIER: .word 6274
  177.  
  178. @ Before compiling your code, the automarker will modify this block.
  179. @ Be sure to avoid out-by-one errors: make sure you access ALL elements.
  180. DATA:  
  181.    .word 0x62967109
  182.    .word 0x0674a70b
  183.    .word 0x902bff4f
  184.    .word 0xe7fa8a65
  185.    .word 0xb610c2af
  186.    .word 0x237e8814
  187.    .word 0xfe0573a4
  188.    .word 0xefd6e381
  189.    .word 0x33c8bf70
  190.    .word 0xfbe4bc46
  191. DATA_END:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement