Assume maximum FLASH size for F2 as for F4 (broken Flash size register)
[fw/stlink] / example / blink_flash / main.c
index e93fd728a42d8f121d36530f9f9989db561ee187..4385b0971b00500ab182f9f572a53fceacbeee70 100644 (file)
@@ -1,13 +1,23 @@
-/* missing type */
+/* base headers */
+#include "stdint.h"
 
-typedef unsigned int uint32_t;
+/* libstm32l_discovery headers */
+#include "stm32l1xx_gpio.h"
+#include "stm32l1xx_adc.h"
+#include "stm32l1xx_lcd.h"
+#include "stm32l1xx_rcc.h"
+#include "stm32l1xx_rtc.h"
+#include "stm32l1xx_exti.h"
+#include "stm32l1xx_pwr.h"
+#include "stm32l1xx_flash.h"
+#include "stm32l1xx_syscfg.h"
+#include "stm32l1xx_dbgmcu.h"
 
+/* board specific macros */
+#include "discover_board.h"
 
-/* hardware configuration */
-
-#define CONFIG_STM32L_DISCOVERY 1
-#define CONFIG_STM32VL_DISCOVERY 0
 
+/* hardware configuration */
 
 #if CONFIG_STM32VL_DISCOVERY
 
@@ -35,7 +45,6 @@ static inline void switch_leds_off(void)
 
 #elif CONFIG_STM32L_DISCOVERY
 
-# define GPIOB 0x40020400 /* port B */
 # define GPIOB_MODER (GPIOB + 0x00) /* port mode register */
 # define GPIOB_ODR (GPIOB + 0x14) /* port output data register */
 
@@ -50,12 +59,45 @@ static inline void setup_leds(void)
 
 static inline void switch_leds_on(void)
 {
-  *(volatile uint32_t*)GPIOB_ODR = LED_BLUE | LED_GREEN;
+  GPIO_HIGH(LD_GPIO_PORT, LD_GREEN_GPIO_PIN);  
+  GPIO_HIGH(LD_GPIO_PORT, LD_BLUE_GPIO_PIN);
 }
 
 static inline void switch_leds_off(void)
 {
-  *(volatile uint32_t*)GPIOB_ODR = 0;
+  GPIO_LOW(LD_GPIO_PORT, LD_GREEN_GPIO_PIN);   
+  GPIO_LOW(LD_GPIO_PORT, LD_BLUE_GPIO_PIN);
+}
+
+#elif CONFIG_STM32F4_DISCOVERY
+
+//#define GPIOD 0x40020C00 /* port D */
+# define GPIOD_MODER (GPIOD + 0x00) /* port mode register */
+# define GPIOD_ODR (GPIOD + 0x14) /* port output data register */
+
+# define LED_GREEN (1 << 12) /* port B, pin 12 */
+# define LED_ORANGE (1 << 13) /* port B, pin 13 */
+# define LED_RED (1 << 14) /* port B, pin 14 */
+# define LED_BLUE (1 << 15) /* port B, pin 15 */
+
+void _tmain(void) {
+       main();
+}
+static inline void setup_leds(void)
+{
+  *(volatile uint32_t*)GPIOD_MODER |= (1 << (12 * 2)) | (1 << (13 * 2)) |
+       (1 << (13 * 2)) | (1 << (14 * 2)) | (1 << (15 * 2));
+}
+
+
+static inline void switch_leds_on(void)
+{
+  *(volatile uint32_t*)GPIOD_ODR = LED_GREEN | LED_RED ;
+}
+
+static inline void switch_leds_off(void)
+{
+  *(volatile uint32_t*)GPIOD_ODR = 0;
 }
 
 #endif /* otherwise, error */
@@ -63,14 +105,87 @@ static inline void switch_leds_off(void)
 
 #define delay()                                                \
 do {                                                   \
-  register unsigned int i;                             \
+  volatile unsigned int i;                             \
   for (i = 0; i < 1000000; ++i)                                \
     __asm__ __volatile__ ("nop\n\t":::"memory");       \
 } while (0)
 
+
+static void RCC_Configuration(void)
+{  
+  /* Enable HSI Clock */
+  RCC_HSICmd(ENABLE);
+  
+  /*!< Wait till HSI is ready */
+  while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET)
+  {}
+
+  RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
+  
+  RCC_MSIRangeConfig(RCC_MSIRange_6);
+
+  RCC_HSEConfig(RCC_HSE_OFF);  
+  if(RCC_GetFlagStatus(RCC_FLAG_HSERDY) != RESET )
+  {
+    while(1);
+  }
+}
+
+
+static void RTC_Configuration(void)
+{
+  /* Allow access to the RTC */
+  PWR_RTCAccessCmd(ENABLE);
+
+  /* Reset Backup Domain */
+  RCC_RTCResetCmd(ENABLE);
+  RCC_RTCResetCmd(DISABLE);
+
+  /* LSE Enable */
+  RCC_LSEConfig(RCC_LSE_ON);
+
+  /* Wait till LSE is ready */
+  while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
+  {}
+  
+  RCC_RTCCLKCmd(ENABLE);
+   
+  /* LCD Clock Source Selection */
+  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
+
+}
+
 void main(void)
 {
-  setup_leds();
+  static RCC_ClocksTypeDef RCC_Clocks;
+  static GPIO_InitTypeDef GPIO_InitStructure;
+
+  /* Configure Clocks for Application need */
+  RCC_Configuration();
+  
+  /* Configure RTC Clocks */
+  RTC_Configuration();
+
+  /* Set internal voltage regulator to 1.8V */
+  PWR_VoltageScalingConfig(PWR_VoltageScaling_Range1);
+
+  /* Wait Until the Voltage Regulator is ready */
+  while (PWR_GetFlagStatus(PWR_FLAG_VOS) != RESET) ;
+
+  /* configure gpios */
+
+  /* Enable GPIOs clock */     
+  RCC_AHBPeriphClockCmd(LD_GPIO_PORT_CLK, ENABLE);
+
+  /* Configure the GPIO_LED pins  LD3 & LD4*/
+  GPIO_InitStructure.GPIO_Pin = LD_GREEN_GPIO_PIN | LD_BLUE_GPIO_PIN;
+  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
+  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
+  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
+  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
+  GPIO_Init(LD_GPIO_PORT, &GPIO_InitStructure);
+  GPIO_LOW(LD_GPIO_PORT, LD_GREEN_GPIO_PIN);   
+  GPIO_LOW(LD_GPIO_PORT, LD_BLUE_GPIO_PIN);
 
   while (1)
   {