Fixed flash utility for STM32F4
[fw/stlink] / example / blink_flash / main.c
1 /* base headers */
2 #include "stdint.h"
3
4 /* libstm32l_discovery headers */
5 #include "stm32l1xx_gpio.h"
6 #include "stm32l1xx_adc.h"
7 #include "stm32l1xx_lcd.h"
8 #include "stm32l1xx_rcc.h"
9 #include "stm32l1xx_rtc.h"
10 #include "stm32l1xx_exti.h"
11 #include "stm32l1xx_pwr.h"
12 #include "stm32l1xx_flash.h"
13 #include "stm32l1xx_syscfg.h"
14 #include "stm32l1xx_dbgmcu.h"
15
16 /* board specific macros */
17 #include "discover_board.h"
18
19
20 /* hardware configuration */
21
22 #if CONFIG_STM32VL_DISCOVERY
23
24 # define GPIOC 0x40011000 /* port C */
25 # define GPIOC_CRH (GPIOC + 0x04) /* port configuration register high */
26 # define GPIOC_ODR (GPIOC + 0x0c) /* port output data register */
27
28 # define LED_BLUE (1 << 8) /* port C, pin 8 */
29 # define LED_GREEN (1 << 9) /* port C, pin 9 */
30
31 static inline void setup_leds(void)
32 {
33   *(volatile uint32_t*)GPIOC_CRH = 0x44444411;
34 }
35
36 static inline void switch_leds_on(void)
37 {
38   *(volatile uint32_t*)GPIOC_ODR = LED_BLUE | LED_GREEN;
39 }
40
41 static inline void switch_leds_off(void)
42 {
43   *(volatile uint32_t*)GPIOC_ODR = 0;
44 }
45
46 #elif CONFIG_STM32L_DISCOVERY
47
48 # define GPIOB_MODER (GPIOB + 0x00) /* port mode register */
49 # define GPIOB_ODR (GPIOB + 0x14) /* port output data register */
50
51 # define LED_BLUE (1 << 6) /* port B, pin 6 */
52 # define LED_GREEN (1 << 7) /* port B, pin 7 */
53
54 static inline void setup_leds(void)
55 {
56   /* configure port 6 and 7 as output */
57   *(volatile uint32_t*)GPIOB_MODER |= (1 << (7 * 2)) | (1 << (6 * 2));
58 }
59
60 static inline void switch_leds_on(void)
61 {
62   GPIO_HIGH(LD_GPIO_PORT, LD_GREEN_GPIO_PIN);   
63   GPIO_HIGH(LD_GPIO_PORT, LD_BLUE_GPIO_PIN);
64 }
65
66 static inline void switch_leds_off(void)
67 {
68   GPIO_LOW(LD_GPIO_PORT, LD_GREEN_GPIO_PIN);    
69   GPIO_LOW(LD_GPIO_PORT, LD_BLUE_GPIO_PIN);
70 }
71
72 #elif CONFIG_STM32F4_DISCOVERY
73
74 //#define GPIOD 0x40020C00 /* port D */
75 # define GPIOD_MODER (GPIOD + 0x00) /* port mode register */
76 # define GPIOD_ODR (GPIOD + 0x14) /* port output data register */
77
78 # define LED_GREEN (1 << 12) /* port B, pin 12 */
79 # define LED_ORANGE (1 << 13) /* port B, pin 13 */
80 # define LED_RED (1 << 14) /* port B, pin 14 */
81 # define LED_BLUE (1 << 15) /* port B, pin 15 */
82
83 void _tmain(void) {
84         main();
85 }
86 static inline void setup_leds(void)
87 {
88   *(volatile uint32_t*)GPIOD_MODER |= (1 << (12 * 2)) | (1 << (13 * 2)) |
89         (1 << (13 * 2)) | (1 << (14 * 2)) | (1 << (15 * 2));
90 }
91
92
93 static inline void switch_leds_on(void)
94 {
95   *(volatile uint32_t*)GPIOD_ODR = LED_GREEN | LED_RED ;
96 }
97
98 static inline void switch_leds_off(void)
99 {
100   *(volatile uint32_t*)GPIOD_ODR = 0;
101 }
102
103 #endif /* otherwise, error */
104
105
106 #define delay()                                         \
107 do {                                                    \
108   volatile unsigned int i;                              \
109   for (i = 0; i < 1000000; ++i)                         \
110     __asm__ __volatile__ ("nop\n\t":::"memory");        \
111 } while (0)
112
113
114 static void RCC_Configuration(void)
115 {  
116   /* Enable HSI Clock */
117   RCC_HSICmd(ENABLE);
118   
119   /*!< Wait till HSI is ready */
120   while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET)
121   {}
122
123   RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
124   
125   RCC_MSIRangeConfig(RCC_MSIRange_6);
126
127   RCC_HSEConfig(RCC_HSE_OFF);  
128   if(RCC_GetFlagStatus(RCC_FLAG_HSERDY) != RESET )
129   {
130     while(1);
131   }
132 }
133
134
135 static void RTC_Configuration(void)
136 {
137   /* Allow access to the RTC */
138   PWR_RTCAccessCmd(ENABLE);
139
140   /* Reset Backup Domain */
141   RCC_RTCResetCmd(ENABLE);
142   RCC_RTCResetCmd(DISABLE);
143
144   /* LSE Enable */
145   RCC_LSEConfig(RCC_LSE_ON);
146
147   /* Wait till LSE is ready */
148   while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
149   {}
150   
151   RCC_RTCCLKCmd(ENABLE);
152    
153   /* LCD Clock Source Selection */
154   RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
155
156 }
157
158 void main(void)
159 {
160   static RCC_ClocksTypeDef RCC_Clocks;
161   static GPIO_InitTypeDef GPIO_InitStructure;
162
163   /* Configure Clocks for Application need */
164   RCC_Configuration();
165   
166   /* Configure RTC Clocks */
167   RTC_Configuration();
168
169   /* Set internal voltage regulator to 1.8V */
170   PWR_VoltageScalingConfig(PWR_VoltageScaling_Range1);
171
172   /* Wait Until the Voltage Regulator is ready */
173   while (PWR_GetFlagStatus(PWR_FLAG_VOS) != RESET) ;
174
175   /* configure gpios */
176
177   /* Enable GPIOs clock */      
178   RCC_AHBPeriphClockCmd(LD_GPIO_PORT_CLK, ENABLE);
179
180   /* Configure the GPIO_LED pins  LD3 & LD4*/
181   GPIO_InitStructure.GPIO_Pin = LD_GREEN_GPIO_PIN | LD_BLUE_GPIO_PIN;
182   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
183   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
184   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
185   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
186   GPIO_Init(LD_GPIO_PORT, &GPIO_InitStructure);
187   GPIO_LOW(LD_GPIO_PORT, LD_GREEN_GPIO_PIN);    
188   GPIO_LOW(LD_GPIO_PORT, LD_BLUE_GPIO_PIN);
189
190   while (1)
191   {
192     switch_leds_on();
193     delay();
194     switch_leds_off();
195     delay();
196   }
197 }