8. ldr r5,=#RCC_CFGR_PPRE1_DIV4
orr r4,r5
str r4,[r7,#RCC_CFGR]
/* Configure the main PLL */
ldr r4,=#( PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) <<
16) | (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24) )
str r4,[r7,#RCC_PLLCFGR]
/* Enable the main PLL */
ldr r4,[r7,#RCC_CR]
ldr r5,=#RCC_CR_PLLON
orr r4,r5
str r4,[r7,#RCC_CR]
Wait_PLL_Ready:
ldr r4,[r7,#RCC_CR]
ldr r5,=#RCC_CR_PLLRDY
ands r4,r5
beq Wait_PLL_Ready
/* Configure Flash prefetch, Instruction cache, Data cache
and wait state */
ldr r7,=#FLASH_R_BASE
ldr r4,=#(FLASH_ACR_ICEN | FLASH_ACR_DCEN |
FLASH_ACR_LATENCY_5WS)
str r4,[r7,#FLASH_ACR]
ldr r7,=#RCC_BASE
/* Select the main PLL as system clock source */
9. ldr r4,[r7,#RCC_CFGR]
ldr r5,=#~(RCC_CFGR_SW)
and r4,r5
str r4,[r7,#RCC_CFGR]
ldr r4,[r7,#RCC_CFGR]
ldr r5,=#RCC_CFGR_SW_PLL
orr r4,r5
str r4,[r7,#RCC_CFGR]
Wait_PLL:
ldr r4,[r7,#RCC_CFGR]
ldr r5,=#RCC_CFGR_SWS
and r4,r5
ldr r5,=#RCC_CFGR_SWS_PLL
cmp r4,r5
bne Wait_PLL
pop {r4,r5,r6,r7,lr}
bx lr
fail_startup:
b fail_startup
.end
#file: gpio_d.s
.include "macros.inc"
.include "src/gpio_regs.inc"
SET_TARGET
10. .text
FUNCTION gpio_d_init, global // initialize all 16
bits of Port D to outputs
push {lr}
mov r0,#3 // send 1 as argument to
rcc_gpio_enable function
bl rcc_gpio_enable // call rcc_gpio_enable
ldr r3,=#GPIOD_BASE // load r3 with base
address of GPIO PORT D
ldr r2,=#0x00000000 // load 0 into r2
str r2,[r3, #GPIO_PUPDR] // write 0 to GPIO Port
D PUPD register
str r2,[r3, #GPIO_OTYPER] // write 0 to GPIO
Port D TYPE register
str r2,[r3, #GPIO_ODR] // write 0 to GPIO Port
D output data register
ldr r2,=#0x55555555 // set all 16 bits of Port D to
outputs
str r2,[r3, #GPIO_MODER]
ldr r2,=#0xffffffff // write 0xffffffff to Port D
SPEED register
str r2,[r3, #GPIO_OSPEEDR]
pop {lr}
bx lr
ENDFUNC gpio_d_init
11. FUNCTION gpio_d_put, global
push {lr}
ldr r3,=#GPIOD_BASE
str r0,[r3, #GPIO_ODR]
pop {lr}
bx lr
ENDFUNC gpio_d_put
FUNCTION gpio_d_get_current, global // get the
current state of the 16 Port D outputs
push {lr}
ldr r3,=#GPIOD_BASE // load r3 with base
address of GPIO Port D
ldr r0,[r3, #GPIO_ODR] // load r0 (rtn arg) with
GPIO Port D output data register
pop {lr}
bx lr
ENDFUNC gpio_d_get_current
.end
#file: LEDR.s
12. .include "macros.inc"
SET_TARGET
.text
FUNCTION LEDR_Init, global
push {lr}
bl gpio_d_init
pop {lr}
bx lr
ENDFUNC LEDR_Init
FUNCTION LEDR_Put_A, global
push {lr}
mov r1, r0
bl gpio_d_get_current
ldr r2,=#(~(~0<<3))
and r1, r2
lsl r1,#8
lsl r2,#8
mvn r2,r2
and r0,r2
orr r0,r1
bl gpio_d_put
pop {lr}
bx lr
13. ENDFUNC LEDR_Put_A
FUNCTION LEDR_Put_B, global
push {lr}
mov r1, r0
bl gpio_d_get_current
ldr r2,=#(~(~0<<4))
and r1, r2
lsl r1, #2
lsl r2, #2
mvn r2, r2
and r0, r2
orr r0, r1
bl gpio_d_put
pop {lr}
bx lr
ENDFUNC LEDR_Put_B
.end
//# Defines
#define reload_value 0xFFFFFF /* System Timer
reload value */
#define LEDs_ODR_Base 0x40020C14 /* LEDs Port
D ODR Address */
//Function Declarations
void SetSysClock();
void gpio_d_init();
unsigned int SysTick_Config(int reload);
14. unsigned int *pLEDs = (unsigned int*)LEDs_ODR_Base;
/* Create pointer to Port D - ODR Reg */
int main()
{
SetSysClock(); //set system clock
gpio_d_init(); //initialize Port D (LEDs)
SysTick_Config(reload_value); //configure SysTick
System Timer
while(1) //loop forever
{
}
return 0;
}
void SysTick_Handler (void) //ISR - SysTick
Interrupt Service Routine
{
*pLEDs = *pLEDs ^ (1<<14); //toggle Port D -
LED 15
}