/*
 * Main.s
 *
 *  Created on: Aug 24, 2022
 *      Author: rozman
 */

		  .syntax unified
		  .cpu cortex-m7
		  .thumb


///////////////////////////////////////////////////////////////////////////////
// Definitions
///////////////////////////////////////////////////////////////////////////////
// Definitions section. Define all the registers and
// constants here for code readability.

// Constants

// Register Addresses
	.equ     DWT_CYCCNT,   	0xE0001004 // DWT_CYCCNT reg (RM, pp.3211)

// Start of data section
 		.data

 		.align

STEV1: 	.word 	0x10   	// 32-bitna spr.
STEV2: 	.word 	0x40   	// 32-bitna spr.
VSOTA: 	.word 	0      	// 32-bitna spr.

// RCC base address is 0x58024400
// AHB4ENR register offset is 0xE0
		.equ RCC_AHB4ENR, 0x580244E0 // RCC AHB4 peripheral clock reg
		.equ GPIOI_BASE, 0x58022000 // GPIOI base address)
		.equ GPIOx_MODER, 0x00 // GPIOx port mode register
		.equ GPIOx_ODR, 0x14 // GPIOx output data register
		.equ GPIOx_BSRR, 0x18 // GPIOx port set/reset register
// Values for BSSR register - pin 13: LED is on, when GPIO is off
		.equ LEDs_OFF, (1<<13)   // Setting b13 for pin 13 state to 1 -> LED is off
		.equ LEDs_ON, (1<<29)   // Setting b29 for pin 13 state to 0 -> LED is on


// Start of text section
  		.text

  		.type  main, %function
  		.global main

   	   	.align
main:

		// Enable GPIOI Peripheral Clock (bit 8 in AHB4ENR register)
		ldr r6, = RCC_AHB4ENR // Load peripheral clock reg address to r6
		ldr r5, [r6] // Read its content to r5
		orr r5, #0x00000100 // Set bit 8 to enable GPIOI clock
		str r5, [r6] // Store result in peripheral clock register

		// Make GPIOI Pin13 as output pin (bits 27:26 in MODER register)
		ldr r6, =GPIOI_BASE // Load GPIOD BASE address to r6
		ldr r5, [r6,#GPIOx_MODER] // Read GPIOD_MODER content to r5
		and r5, #0xF3FFFFFF // Clear bits 27-26 for P13
		orr r5, #0x04000000 // Write 01 to bits 27-26 for P13
		str r5, [r6] // Store result in GPIO MODER register

		ldr r6, =GPIOI_BASE // Load GPIOI BASE address to r6
		mov r5, #LEDs_OFF
		str r5, [r6,#GPIOx_BSRR] // Write to BSRR register

		mov r5, #LEDs_ON
		str r5, [r6,#GPIOx_BSRR] // Write to BSRR register

		mov r5, #LEDs_OFF
		str r5, [r6,#GPIOx_BSRR] // Write to BSRR register

__end: 	b 	__end


