/** @file
 * Startup script to be used with the HBL
 *
 * This startup file provides a reset handler to setup the basics for c to run
 * (stack pointer, static data, ...) as well as the vector table and the
 * differents interrupt handlers that go with it. By default, all handlers use
 * the default handler wich is simply an infinite loop
 */

	.syntax unified
	.cpu cortex-m3
	.fpu softvfp
	.thumb

.word _sromdata
.word _sdata
.word _edata


//--Handler definitions---------------------------------------------------------

/**
 * Reset handler, first thing executed at boot
 * The handler configures the stack pointer and loads data from ROM to RAM
 */
	.section .text.hdr_reset
	.weak hdr_reset
	.type hdr_reset, %function
hdr_reset:
	ldr r0, = _estack
	mov sp, r0

//load data from ROM to RAM
	ldr r1, = _sdata
	ldr r2, = _edata
	subs r2, r2, r1
	bcc data_init_end
	ldr r3, = _sromdata

	movs r0, #0

data_init_loop:
	ldr r4, [r3, r0]
	str r4, [r1, r0]
	adds r0, r0, #4
	cmp r0, r2
	bcc data_init_loop

data_init_end:
//initialize RAM to zero
	ldr r1, = _sbss
	ldr r2, = _ebss
	subs r2, r2, r1
	bcc bss_init_end
	movs r3, #0

	movs r0, #0

bss_init_loop:
	str r3, [r1, r0]
	adds r0, r0, #4
	cmp r0, r2
	bcc bss_init_loop

bss_init_end:
	b main
	b hdr_reset

	.size hdr_reset, .-hdr_reset

/**
 * Default handler, called on reception of an unexpected interrupt
 * This is the handler used by default for all interrupts unless a specific
 * handler was defined elsewhere
 */
	.section .text.hdr_default, "ax", %progbits
	.weak hdr_default
hdr_default:
	b hdr_default

	.size hdr_default, .-hdr_default


//--Vector table----------------------------------------------------------------

	.section .vector_table, "a", %progbits
	.type vector_table, %object
	.size vector_table, .-vector_table

vector_table:
	.word _estack
	.word hdr_reset
	.word hdr_nmi
	.word hdr_hard_fault
	.word hdr_mem_manage
	.word hdr_bus_fault
	.word hdr_usage_fault
	.word 0
	.word 0
	.word 0
	.word 0
	.word hdr_svc
	.word hdr_debug_monitor
	.word 0
	.word hdr_pend_sys_service
	.word hdr_sys_tick
	.word hdr_wwdg
	.word hdr_pvd
	.word hdr_tamper
	.word hdr_rtc
	.word hdr_flash
	.word hdr_rcc
	.word hdr_exti0
	.word hdr_exti1
	.word hdr_exti2
	.word hdr_exti3
	.word hdr_exti4
	.word hdr_dma1_channel1
	.word hdr_dma1_channel2
	.word hdr_dma1_channel3
	.word hdr_dma1_channel4
	.word hdr_dma1_channel5
	.word hdr_dma1_channel6
	.word hdr_dma1_channel7
	.word hdr_adc1_2
	.word hdr_hp_can_tx
	.word hdr_lp_can_rx0
	.word hdr_can_rx1
	.word hdr_can_sce
	.word hdr_exti9_5
	.word hdr_tim1_brk
	.word hdr_tim1_up
	.word hdr_tim1_trg_com
	.word hdr_tim1_cc
	.word hdr_tim2
	.word hdr_tim3
	.word hdr_tim4
	.word hdr_i2c1_event
	.word hdr_i2c1_error
	.word hdr_i2c2_event
	.word hdr_i2c2_error
	.word hdr_spi1
	.word hdr_spi2
	.word hdr_usart1
	.word hdr_usart2
	.word hdr_usart3
	.word hdr_exti15_10
	.word hdr_rtc_alarm
	.word hdr_usb_wakeup
	.word hdr_tim8_brk
	.word hdr_tim8_up
	.word hdr_tim8_trg_com
	.word hdr_tim8_cc
	.word hdr_adc3
	.word hdr_fsmc
	.word hdr_sdio
	.word hdr_tim5
	.word hdr_spi3
	.word hdr_uart4
	.word hdr_uart5
	.word hdr_tim6
	.word hdr_tim7
	.word hdr_dma2_channel1
	.word hdr_dma2_channel2
	.word hdr_dma2_channel3
	.word hdr_dma2_channel4_5

//--Weak definitions------------------------------------------------------------

	.weak hdr_nmi
	.thumb_set hdr_nmi, hdr_default

	.weak hdr_hard_fault
	.thumb_set hdr_hard_fault, hdr_default

	.weak hdr_mem_manage
	.thumb_set hdr_mem_manage, hdr_default

	.weak hdr_bus_fault
	.thumb_set hdr_bus_fault, hdr_default

	.weak hdr_usage_fault
	.thumb_set hdr_usage_fault, hdr_default

	.weak hdr_svc
	.thumb_set hdr_svc, hdr_default

	.weak hdr_debug_monitor
	.thumb_set hdr_debug_monitor, hdr_default

	.weak hdr_pend_sys_service
	.thumb_set hdr_pend_sys_service, hdr_default

	.weak hdr_sys_tick
	.thumb_set hdr_sys_tick, hdr_default

	.weak hdr_wwdg
	.thumb_set hdr_wwdg, hdr_default

	.weak hdr_pvd
	.thumb_set hdr_pvd, hdr_default

	.weak hdr_tamper
	.thumb_set hdr_tamper, hdr_default

	.weak hdr_rtc
	.thumb_set hdr_rtc, hdr_default

	.weak hdr_flash
	.thumb_set hdr_flash, hdr_default

	.weak hdr_rcc
	.thumb_set hdr_rcc, hdr_default

	.weak hdr_exti0
	.thumb_set hdr_exti0, hdr_default

	.weak hdr_exti1
	.thumb_set hdr_exti1, hdr_default

	.weak hdr_exti2
	.thumb_set hdr_exti2, hdr_default

	.weak hdr_exti3
	.thumb_set hdr_exti3, hdr_default

	.weak hdr_exti4
	.thumb_set hdr_exti4, hdr_default

	.weak hdr_dma1_channel1
	.thumb_set hdr_dma_channel1, hdr_default

	.weak hdr_dma1_channel2
	.thumb_set hdr_dma_channel2, hdr_default

	.weak hdr_dma1_channel3
	.thumb_set hdr_dma_channel3, hdr_default

	.weak hdr_dma1_channel4
	.thumb_set hdr_dma_channel4, hdr_default

	.weak hdr_dma1_channel5
	.thumb_set hdr_dma_channel5, hdr_default

	.weak hdr_dma1_channel6
	.thumb_set hdr_dma_channel6, hdr_default

	.weak hdr_dma1_channel7
	.thumb_set hdr_dma_channel7, hdr_default

	.weak hdr_adc1_2
	.thumb_set hdr_adc1_2, hdr_default

	.weak hdr_hp_can_tx
	.thumb_set hdr_hp_can_tx, hdr_default

	.weak hdr_lp_can_rx0
	.thumb_set hdr_lp_can_rx0, hdr_default

	.weak hdr_can_rx1
	.thumb_set hdr_can_rx1, hdr_default

	.weak hdr_can_sce
	.thumb_set hdr_can_sce, hdr_default

	.weak hdr_exti9_5
	.thumb_set hdr_exti9_5, hdr_default

	.weak hdr_tim1_brk
	.thumb_set hdr_tim1_brk, hdr_default

	.weak hdr_tim1_up
	.thumb_set hdr_tim1_up, hdr_default

	.weak hdr_tim1_trg_com
	.thumb_set hdr_tim1_trg_com, hdr_default

	.weak hdr_tim1_cc
	.thumb_set hdr_tim1_cc, hdr_default

	.weak hdr_tim2
	.thumb_set hdr_tim2, hdr_default

	.weak hdr_tim3
	.thumb_set hdr_tim3, hdr_default

	.weak hdr_tim4
	.thumb_set hdr_tim4, hdr_default

	.weak hdr_i2c1_event
	.thumb_set hdr_i2c1_event, hdr_default

	.weak hdr_i2c1_error
	.thumb_set hdr_i2c1_error, hdr_default

	.weak hdr_i2c2_event
	.thumb_set hdr_i2c2_event, hdr_default

	.weak hdr_i2c2_error
	.thumb_set hdr_i2c2_error, hdr_default

	.weak hdr_spi1
	.thumb_set hdr_spi1, hdr_default

	.weak hdr_spi2
	.thumb_set hdr_spi2, hdr_default

	.weak hdr_usart1
	.thumb_set hdr_usart1, hdr_default

	.weak hdr_usart2
	.thumb_set hdr_usart2, hdr_default

	.weak hdr_usart3
	.thumb_set hdr_usart3, hdr_default

	.weak hdr_exti15_10
	.thumb_set hdr_exti15_10, hdr_default

	.weak hdr_rtc_alarm
	.thumb_set hdr_rtc_alarm, hdr_default

	.weak hdr_usb_wakeup
	.thumb_set hdr_usb_wakeup, hdr_default

	.weak hdr_tim8_brk
	.thumb_set hdr_tim8_brk, hdr_default

	.weak hdr_tim8_up
	.thumb_set hdr_tim8_up, hdr_default

	.weak hdr_tim8_trg_com
	.thumb_set hdr_tim8_trg_com, hdr_default

	.weak hdr_tim8_cc
	.thumb_set hdr_tim8_cc, hdr_default

	.weak hdr_adc3
	.thumb_set hdr_adc3, hdr_default

	.weak hdr_fsmc
	.thumb_set hdr_fsmc, hdr_default

	.weak hdr_sdio
	.thumb_set hdr_sdio, hdr_default

	.weak hdr_tim5
	.thumb_set hdr_tim5, hdr_default

	.weak hdr_spi3
	.thumb_set hdr_spi3, hdr_default

	.weak hdr_uart4
	.thumb_set hdr_uart4, hdr_default

	.weak hdr_uart5
	.thumb_set hdr_uart5, hdr_default

	.weak hdr_tim6
	.thumb_set hdr_tim6, hdr_default

	.weak hdr_tim7
	.thumb_set hdr_tim7, hdr_default

	.weak hdr_dma2_channel1
	.thumb_set hdr_dma_channel1, hdr_default

	.weak hdr_dma2_channel2
	.thumb_set hdr_dma_channel2, hdr_default

	.weak hdr_dma2_channel3
	.thumb_set hdr_dma_channel3, hdr_default

	.weak hdr_dma2_channel4_5
	.thumb_set hdr_dma_channel4_5, hdr_default

