-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab1(b)
99 lines (82 loc) · 4.07 KB
/
Lab1(b)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
;/*
;*project_encrypt.cpp
;*Created on: 2020
;*Author: Fatin Sarker
;*Copyright © 2019 Fatin Sarker. All rights reserved
;*Computer Engineering
;*University of Waterloo
; */
;*----------------------------------------------------------------------------
;* Name: Lab1_T.s
;* Purpose: To flash an LED at approximately 1 Hz
;* Author: Rasoul Keshavarzi
;* Revision: 0.1
;* Changelog: Aug-26-2020 by Reinier Torres: Ported to STM32F401VE and added
;* comments to improve understanding and encourage thinking
;*----------------------------------------------------------------------------*/
;************************** DO NOT CHANGE THIS CODE **************************
; Setups the MCU for work and enables peripherals for STM32F401FE and matches
; startup_stm32f401xe.s file
THUMB ; Thumb instruction set
AREA My_code, CODE, READONLY
EXPORT __MAIN
ENTRY
__MAIN
; Use the EQU directive to improve readability of your programs it also
; helps the assembler optimize your code
RCC_AHB1ENR EQU 0x40023830 ; address of enable register for AHB1
GPIOA_MOD EQU 0x40020000 ; address of mode register GPIOA
GPIOA_OUT EQU 0x04 ; offset of output type register for GPIOA
GPIOA_DAT EQU 0x14 ; offset of data register output for GPIOA
B5_CLR_MSK EQU 0xFFFFFFDF ;use this mask to clear bit 5 of any DWord
B5_SET_MSK EQU 0x00000020 ;use this mask to set bit 5 of any DWord
; We will use a technique called Read/Modify/Write Back. This is commonly
; used when programming embedded application. Note that just writing our
; wanted configuration may ruin the configuration for other pins/peripherals.
; Reading what is in the peripheral register before modifying it allows us to
; set our configuration whilst keeping previous configurations that shouldn't
; be changed.
; We encorage you to follow this practice, it will help you avoid crashes.
LDR R1, =RCC_AHB1ENR ;Set the pointer to RCC_AHB1ENR register
LDR R0, [R1] ; Read: RCC_AHB1ENR from mapped memory
ORR.W R0, #0x01 ; Modify: Set b0 to enable the clock for GPIOA
STR R0, [R1] ; Write Back: Write back to RCC_AHB1ENR
; GPIOA is now enable but GPIOA.PIN5 must be configured
; Deep dive: Comment out the Write Back operation and see what happens to
; the MCU. HINT: You will end up in the hard fault handler, look the RCC_AHB1ENR
; register in the datasheet and try to understand why the system fails
LDR R1,=GPIOA_MOD ;Load pointer for GPIOA mode
LDR R0, [R1] ; Read GPIOA mode register
ORR.W R0, #0x0500 ; Set GPIOA_PIN5 as an output
STR R0, [R1] ; Write back the mode
LDR R0, [R1, #GPIOA_OUT] ; Read GPIOA output type register
AND.W R0, #0xFFFFFFDF ; Set GPIOA_PIN5 as a push-pull output (clears OT5)
STR R0, [R1, #GPIOA_OUT] ; Write back the output type
; GPIOA_PIN5 is now configured
; Testing GPIOA_PIN5. To see this happening you must debug the lines that
; follow step by step, otherwise the LED will turn on then off in a matter
; of microseconds
LDR R0, [R1, #GPIOA_DAT] ; Read GPIOA data register
ORR.W R0, #B5_SET_MSK ; Turn on LED (Sets ODR5)
STR R0, [R1, #GPIOA_DAT] ; Write back the data to output data register
TST R0, #B5_SET_MSK
LDR R0, [R1, #GPIOA_DAT] ; Read GPIOA data register
AND.W R0, #B5_CLR_MSK ; Turn off LED (clears ODR5)
STR R0, [R1, #GPIOA_DAT] ; Write back the data to output data register
;***************************** END OF SETUP CODE ******************************
;************************* USER CODE STARTS HERE ******************************
; We prvide the main loop of the program. You must write the delays and also
; turn on/off the LED. Use the initialization code as an example on how to
; manipulate the GPIO port
;MOV R6, #0xFFFF ;counter
loop
EOR R0, R0, #0x10 ; toggle
ADD R0, #0x10 ;turn on LED 0X20
STR R0, [R1, #GPIOA_DAT] ; write R0 to turn on/off the LED
SUB R0, #0x10 ;initial state
MOVT R6, #0xF ; initialize R0 for countdown
delay
SUBS R6, #1 ; Decrement R6 and set the N,Z,C status bits
BNE delay ; loop through delay until Z is set to 1
B loop ; loop through infinitely
END