-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.c
125 lines (118 loc) · 3.92 KB
/
init.c
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "stm32f042.h"
void init(void);
void Default_Handler(void);
void debug_printIPSR(void);
extern void isr_usart2(void);
// The following are 'declared' in the linker script
extern unsigned char INIT_DATA_VALUES;
extern unsigned char INIT_DATA_START;
extern unsigned char INIT_DATA_END;
extern unsigned char BSS_START;
extern unsigned char BSS_END;
// the section "vectors" is placed at the beginning of flash
// by the linker script
const void * Vectors[] __attribute__((section(".vectors"))) ={
(void *)0x20001000, /* Top of stack (4k) */
init, /* Reset Handler */
Default_Handler, /* NMI */
Default_Handler, /* Hard Fault */
Default_Handler, /* MemManage */
Default_Handler, /* Reserved */
Default_Handler, /* Reserved */
Default_Handler, /* Reserved */
Default_Handler, /* Reserved */
Default_Handler, /* Reserved */
Default_Handler, /* Reserved */
Default_Handler, /* SVC Call */
Default_Handler, /* Reserved */
Default_Handler, /* Reserved */
Default_Handler, /* PendSV */
Default_Handler, /* SysTick */
/* External interrupt handlers follow */
Default_Handler, /* 0: WWDG */
Default_Handler, /* 1: PVD and VDDIO2 supply comparator interrupt */
Default_Handler, /* 2: RTC */
Default_Handler, /* 3: FLASH */
Default_Handler, /* 4: RCC */
Default_Handler, /* 5: EXTI0_1 */
Default_Handler, /* 6: EXTI2_3 */
Default_Handler, /* 7: EXTI4_15 */
Default_Handler, /* 8: TSC */
Default_Handler, /* 9: DMA_CH1 */
Default_Handler, /* 10: DMA_CH2_3, DMA2_CH_1_2 */
Default_Handler, /* 11: DMA_CH4_5_6_7, DMA2_CH3_4_5 */
Default_Handler, /* 12: ADC_COMP */
Default_Handler, /* 13: TIM1_BRK_UP_TRG_COM */
Default_Handler, /* 14: TIM1_CC */
Default_Handler, /* 15: TIM2 */
Default_Handler, /* 16: TIM3 */
Default_Handler, /* 17: TIM6_DAC*/
Default_Handler, /* 18: TIM7 */
Default_Handler, /* 19: TIM14 */
Default_Handler, /* 20: TIM15 */
Default_Handler, /* 21: TIM16 */
Default_Handler, /* 22: TIM17 */
Default_Handler, /* 23: I2C1 */
Default_Handler, /* 24: I2C2 */
Default_Handler, /* 25: SPI1 */
Default_Handler, /* 26: SPI2 */
Default_Handler, /* 27: USART1 */
isr_usart2, /* 28: USART2 */
Default_Handler, /* 29: USART3_4_5_6_7_8 */
Default_Handler, /* 30: CEC_CAN */
Default_Handler /* 31: USB */
};
void initClock()
{
// This is potentially a dangerous function as it could
// result in a system with an invalid clock signal - result: a stuck system
// Set the PLL up
// First ensure PLL is disabled
RCC_CR &= ~BIT24;
while( (RCC_CR & BIT25)); // wait for PLL ready to be cleared
// set PLL multiplier to 12 (yielding 48MHz)
// Warning here: if system clock is greater than 24MHz then wait-state(s) need to be
// inserted into Flash memory interface
FLASH_ACR |= BIT0;
FLASH_ACR &=~(BIT2 | BIT1);
// Turn on FLASH prefetch buffer
FLASH_ACR |= BIT4;
RCC_CFGR &= ~(BIT21 | BIT20 | BIT19 | BIT18);
RCC_CFGR |= (BIT21 | BIT19 );
// Need to limit ADC clock to below 14MHz so will change ADC prescaler to 4
RCC_CFGR |= BIT14;
// Do the following to push HSI clock out on PA8 (MCO)
// for measurement purposes. Should be 8MHz or thereabouts (verified with oscilloscope)
/*
RCC_CFGR |= ( BIT26 | BIT24 );
RCC_AHBENR |= BIT17;
GPIOA_MODER |= BIT17;
*/
// and turn the PLL back on again
RCC_CR |= BIT24;
// set PLL as system clock source
RCC_CFGR |= BIT1;
}
void init()
{
// do global/static data initialization
unsigned char *src;
unsigned char *dest;
unsigned len;
initClock(); // Push CPU up to 48MHz (Gonna need it)
src= &INIT_DATA_VALUES;
dest= &INIT_DATA_START;
len= &INIT_DATA_END-&INIT_DATA_START;
while (len--)
*dest++ = *src++;
// zero out the uninitialized global/static variables
dest = &BSS_START;
len = &BSS_END - &BSS_START;
while (len--)
*dest++=0;
main();
}
void Default_Handler()
{
while(1);
}