]> rtime.felk.cvut.cz Git - arc.git/blobdiff - arch/arm/arm_cm3/drivers/Mcu.c
PPC: Sleep mode and wakeup pin now working
[arc.git] / arch / arm / arm_cm3 / drivers / Mcu.c
index c2fa0df859402ab8a7cc05011ecb81a99cb5ba12..17b391f99101b46508517f8a1117c8b98e536359 100644 (file)
@@ -1,44 +1,66 @@
-/* -------------------------------- Arctic Core ------------------------------
- * Arctic Core - the open source AUTOSAR platform http://arccore.com
- *
- * Copyright (C) 2009  ArcCore AB <contact@arccore.com>
- *
- * This source code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 as published by the
- * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * for more details.
- * -------------------------------- Arctic Core ------------------------------*/
-
-
-
-
-
-
-
-
+/* -------------------------------- Arctic Core ------------------------------\r
+ * Arctic Core - the open source AUTOSAR platform http://arccore.com\r
+ *\r
+ * Copyright (C) 2009  ArcCore AB <contact@arccore.com>\r
+ *\r
+ * This source code is free software; you can redistribute it and/or modify it\r
+ * under the terms of the GNU General Public License version 2 as published by the\r
+ * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.\r
+ *\r
+ * This program is distributed in the hope that it will be useful, but\r
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
+ * for more details.\r
+ * -------------------------------- Arctic Core ------------------------------*/\r
+\r
+\r
 #include "Std_Types.h"\r
 #include "Mcu.h"\r
 #include "Det.h"\r
+#if defined(USE_DEM)\r
+#include "Dem.h"\r
+#endif\r
 #include <assert.h>\r
-#include "cpu.h"\r
+#include "Cpu.h"\r
 #include <string.h>\r
 #include "Ramlog.h"\r
-#include "system_stm32f10x.h"
 \r
-//#define USE_TRACE 1\r
-//#define USE_DEBUG 1\r
-#include "Trace.h"\r
+//#define USE_LDEBUG_PRINTF 1\r
+#include "debug.h"\r
+\r
+\r
+#ifndef ARRAY_SIZE\r
+#define ARRAY_SIZE(_x)  (sizeof(_x)/sizeof((_x)[0]))\r
+#endif\r
+\r
+/* Development error macros. */\r
+#if ( MCU_DEV_ERROR_DETECT == STD_ON )\r
+#define VALIDATE(_exp,_api,_err ) \\r
+        if( !(_exp) ) { \\r
+          Det_ReportError(MODULE_ID_MCU,0,_api,_err); \\r
+          return; \\r
+        }\r
+\r
+#define VALIDATE_W_RV(_exp,_api,_err,_rv ) \\r
+        if( !(_exp) ) { \\r
+          Det_ReportError(MODULE_ID_MCU,0,_api,_err); \\r
+          return (_rv); \\r
+        }\r
+#else\r
+#define VALIDATE(_exp,_api,_err )\r
+#define VALIDATE_W_RV(_exp,_api,_err,_rv )\r
+#endif\r
+\r
+\r
+#define CORE_CPUID_CORTEX_M3           0x411FC231UL\r
+\r
+\r
 \r
 typedef struct {\r
        uint32 lossOfLockCnt;\r
        uint32 lossOfClockCnt;\r
 } Mcu_Stats;\r
 \r
-void Mcu_ConfigureFlash(void);\r
 /**\r
  * Type that holds all global data for Mcu\r
  */\r
@@ -56,23 +78,6 @@ typedef struct
 \r
 } Mcu_GlobalType;\r
 \r
-/* Development error macros. */\r
-#if ( MCU_DEV_ERROR_DETECT == STD_ON )\r
-#define VALIDATE(_exp,_api,_err ) \\r
-        if( !(_exp) ) { \\r
-          Det_ReportError(MODULE_ID_MCU,0,_api,_err); \\r
-          return; \\r
-        }\r
-\r
-#define VALIDATE_W_RV(_exp,_api,_err,_rv ) \\r
-        if( !(_exp) ) { \\r
-          Det_ReportError(MODULE_ID_MCU,0,_api,_err); \\r
-          return (_rv); \\r
-        }\r
-#else\r
-#define VALIDATE(_exp,_api,_err )\r
-#define VALIDATE_W_RV(_exp,_api,_err,_rv )\r
-#endif\r
 \r
 // Global config\r
 Mcu_GlobalType Mcu_Global =\r
@@ -82,29 +87,6 @@ Mcu_GlobalType Mcu_Global =
 };\r
 \r
 //-------------------------------------------------------------------\r
-
-#if 0\r
-static void Mcu_LossOfLock( void  ) {\r
-#if ( MCU_DEV_ERROR_DETECT == STD_ON )\r
-       /* Should report MCU_E_CLOCK_FAILURE with DEM here.... but\r
-        * we do the next best thing. Report with Det with API = 0\r
-        */\r
-       Det_ReportError(MODULE_ID_MCU,0,0,MCU_E_PLL_NOT_LOCKED);\r
-#endif\r
-\r
-       Mcu_Global.stats.lossOfLockCnt++;\r
-       // Clear interrupt\r
-//     FMPLL.SYNSR.B.LOLF = 1;\r
-\r
-}\r
-#endif
-\r
-#define SPR_PIR 286\r
-#define SPR_PVR 287\r
-\r
-#define CORE_PVR_E200Z1   0x81440000UL\r
-#define CORE_PVR_E200Z0   0x81710000UL\r
-\r
 \r
 typedef struct {\r
   char *name;\r
@@ -116,31 +98,34 @@ typedef struct {
   uint32 pvr;\r
 } cpu_info_t;\r
 \r
+\r
+void Mcu_ConfigureFlash(void);\r
+\r
+\r
+\r
+/* Haven't found any ID accessable from memory.\r
+ * There is the DBGMCU_IDCODE (0xe0042000) found in RM0041 but it\r
+ * you can't read from that address..\r
+ */\r
+#if 0\r
 cpu_info_t cpu_info_list[] = {\r
     {\r
-    .name = "MPC5516",\r
-    .pvr = CORE_PVR_E200Z1,\r
-    },\r
-    {\r
-    .name = "MPC5516",\r
-    .pvr = CORE_PVR_E200Z0,\r
+    .name = "????",\r
+    .pvr = 0,\r
     },\r
 };\r
+#endif\r
 \r
+/* The supported cores\r
+ */\r
 core_info_t core_info_list[] = {\r
     {\r
-    .name = "CORE_E200Z1",\r
-    .pvr = CORE_PVR_E200Z1,\r
-    },\r
-    {\r
-    .name = "CORE_E200Z1",\r
-    .pvr = CORE_PVR_E200Z1,\r
+    .name = "CORE_ARM_CORTEX_M3",\r
+    .pvr = CORE_CPUID_CORTEX_M3,\r
     },\r
 };\r
 \r
-// TODO: move\r
-#define ARRAY_SIZE(_x)  (sizeof(_x)/sizeof((_x)[0]))\r
-\r
+#if 0\r
 static cpu_info_t *Mcu_IdentifyCpu(uint32 pvr)\r
 {\r
   int i;\r
@@ -152,6 +137,8 @@ static cpu_info_t *Mcu_IdentifyCpu(uint32 pvr)
 \r
   return NULL;\r
 }\r
+#endif\r
+\r
 \r
 static core_info_t *Mcu_IdentifyCore(uint32 pvr)\r
 {\r
@@ -165,35 +152,191 @@ static core_info_t *Mcu_IdentifyCore(uint32 pvr)
   return NULL;\r
 }\r
 \r
-\r
+/**\r
+ * Identify the core, just to check that we have support for it.\r
+ *\r
+ * @return\r
+ */\r
 static uint32 Mcu_CheckCpu( void ) {\r
 \r
-  uint32 pvr;\r
+  uint32 pvr = SCB->CPUID;\r
   //uint32 pir;\r
-  cpu_info_t *cpuType;\r
+  //cpu_info_t *cpuType;\r
   core_info_t *coreType;\r
 \r
-  // We have to registers to read here, PIR and PVR\r
+  //cpuType = Mcu_IdentifyCpu(pvr);\r
+  coreType = Mcu_IdentifyCore(pvr);\r
 \r
-#if 0\r
-  pir = get_spr(SPR_PIR);\r
-  pvr = get_spr(SPR_PVR);\r
+  if( (coreType == NULL) ) {\r
+    // Just hang\r
+    while(1) ;\r
+  }\r
+\r
+  return 0;\r
+}\r
+\r
+static uint32_t GetPllValueFromMult(uint8_t pll)\r
+{\r
+       return (((uint32_t)pll - 2) << 18);\r
+}\r
+\r
+#ifdef STM32F10X_CL\r
+static uint32_t GetPll2ValueFromMult(uint8_t pll)\r
+{\r
+       return (((uint32_t)pll - 2) << 8);\r
+}\r
 #endif\r
 \r
-  cpuType = Mcu_IdentifyCpu(pvr);\r
-  coreType = Mcu_IdentifyCore(pvr);\r
+/**\r
+  * Set bus clocks. SysClk,AHBClk,APB1Clk,APB2Clk\r
+  */\r
+static void SetClocks(Mcu_ClockSettingConfigType *clockSettingsPtr)\r
+{\r
+  volatile uint32 StartUpCounter = 0, HSEStatus = 0;\r
 \r
-  if( (cpuType == NULL) || (coreType == NULL) ) {\r
-    // Just hang\r
-    while(1);\r
+  /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/\r
+  /* Enable HSE */\r
+  RCC->CR |= ((uint32_t)RCC_CR_HSEON);\r
+\r
+  /* Wait till HSE is ready and if Time out is reached exit */\r
+  do\r
+  {\r
+    HSEStatus = RCC->CR & RCC_CR_HSERDY;\r
+    StartUpCounter++;\r
+  } while((HSEStatus == 0) && (StartUpCounter != HSEStartUp_TimeOut));\r
+\r
+  if ((RCC->CR & RCC_CR_HSERDY) != RESET)\r
+  {\r
+    HSEStatus = (uint32_t)0x01;\r
+  }\r
+  else\r
+  {\r
+    HSEStatus = (uint32_t)0x00;\r
   }\r
 \r
-  //DEBUG(DEBUG_HIGH,"/drivers/mcu: Cpu:  %s( 0x%08x )\n",cpuType->name,pvr);\r
-  //DEBUG(DEBUG_HIGH,"/drivers/mcu: Core: %s( 0x%08x )\n",coreType->name,pvr);\r
+  if (HSEStatus == (uint32_t)0x01)\r
+  {\r
+    /* Enable Prefetch Buffer */\r
+    FLASH->ACR |= FLASH_ACR_PRFTBE;\r
 \r
-  return 0;\r
+    /* Flash 2 wait state */\r
+    FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);\r
+    FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;\r
+\r
+\r
+    /* HCLK = SYSCLK */\r
+    RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;\r
+\r
+    /* PCLK2 = HCLK */\r
+    RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;\r
+\r
+    /* PCLK1 = HCLK */\r
+    RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;\r
+\r
+#ifdef STM32F10X_CL\r
+    /* Configure PLLs ------------------------------------------------------*/\r
+    /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */\r
+    /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */\r
+\r
+    RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |\r
+                              RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);\r
+    RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | GetPll2ValueFromMult(clockSettingsPtr->Pll2) |\r
+                             RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);\r
+\r
+    /* Enable PLL2 */\r
+    RCC->CR |= RCC_CR_PLL2ON;\r
+    /* Wait till PLL2 is ready */\r
+    while((RCC->CR & RCC_CR_PLL2RDY) == 0)\r
+    {\r
+    }\r
+\r
+    /* PLL configuration: PLLCLK = PREDIV1 * 9 = 72 MHz */\r
+    RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);\r
+    RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |\r
+                               GetPllValueFromMult(clockSettingsPtr->Pll1));\r
+#else\r
+    /*  PLL configuration: PLLCLK = HSE * 9 = 72 MHz */\r
+    RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |\r
+                                        RCC_CFGR_PLLMULL));\r
+    RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | GetPllValueFromMult(clockSettingsPtr->Pll1));\r
+#endif /* STM32F10X_CL */\r
+\r
+    /* Enable PLL */\r
+    RCC->CR |= RCC_CR_PLLON;\r
+\r
+    /* Wait till PLL is ready */\r
+    while((RCC->CR & RCC_CR_PLLRDY) == 0)\r
+    {\r
+    }\r
+\r
+    /* Select PLL as system clock source */\r
+    RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
+    RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;\r
+\r
+    /* Wait till PLL is used as system clock source */\r
+    while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)\r
+    {\r
+    }\r
+  }\r
+  else\r
+  { /* HSE fails to start-up, the application will have wrong clock */\r
+         NVIC_SystemReset();\r
+  }\r
 }\r
 \r
+/**\r
+  * Initialize Peripherals clocks\r
+  */\r
+static void InitPerClocks()\r
+{\r
+       RCC->AHBENR |= McuPerClockConfigData.AHBClocksEnable;\r
+       RCC->APB1ENR |= McuPerClockConfigData.APB1ClocksEnable;\r
+       RCC->APB2ENR |= McuPerClockConfigData.APB2ClocksEnable;\r
+}\r
+\r
+/**\r
+  * Initialize Flash, PLL and clocks.\r
+  */\r
+static void InitMcuClocks(Mcu_ClockSettingConfigType *clockSettingsPtr)\r
+{\r
+  /* Reset the RCC clock configuration to the default reset state(for debug purpose) */\r
+  /* Set HSION bit */\r
+  RCC->CR |= (uint32_t)0x00000001;\r
+\r
+  /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */\r
+#ifndef STM32F10X_CL\r
+  RCC->CFGR &= (uint32_t)0xF8FF0000;\r
+#else\r
+  RCC->CFGR &= (uint32_t)0xF0FF0000;\r
+#endif /* STM32F10X_CL */\r
+\r
+  /* Reset HSEON, CSSON and PLLON bits */\r
+  RCC->CR &= (uint32_t)0xFEF6FFFF;\r
+\r
+  /* Reset HSEBYP bit */\r
+  RCC->CR &= (uint32_t)0xFFFBFFFF;\r
+\r
+  /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */\r
+  RCC->CFGR &= (uint32_t)0xFF80FFFF;\r
+\r
+#ifndef STM32F10X_CL\r
+  /* Disable all interrupts and clear pending bits  */\r
+  RCC->CIR = 0x009F0000;\r
+#else\r
+  /* Reset PLL2ON and PLL3ON bits */\r
+  RCC->CR &= (uint32_t)0xEBFFFFFF;\r
+\r
+  /* Disable all interrupts and clear pending bits  */\r
+  RCC->CIR = 0x00FF0000;\r
+\r
+  /* Reset CFGR2 register */\r
+  RCC->CFGR2 = 0x00000000;\r
+#endif /* STM32F10X_CL */\r
+\r
+  /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */\r
+  /* Configure the Flash Latency cycles and enable prefetch buffer */\r
+  SetClocks(clockSettingsPtr);\r
+}\r
 \r
 //-------------------------------------------------------------------\r
 \r
@@ -201,15 +344,12 @@ void Mcu_Init(const Mcu_ConfigType *configPtr)
 {\r
   VALIDATE( ( NULL != configPtr ), MCU_INIT_SERVICE_ID, MCU_E_PARAM_CONFIG );\r
 \r
-  if( !SIMULATOR() ) {\r
-         Mcu_CheckCpu();\r
-  }\r
+#if !defined(USE_SIMULATOR)\r
+  Mcu_CheckCpu();\r
+#endif\r
 \r
   memset(&Mcu_Global.stats,0,sizeof(Mcu_Global.stats));\r
-
-
-  SystemInit();\r
-
+\r
   Irq_Enable();\r
 \r
   Mcu_Global.config = configPtr;\r
@@ -246,54 +386,9 @@ Std_ReturnType Mcu_InitClock(const Mcu_ClockType ClockSetting)
   Mcu_Global.clockSetting = ClockSetting;\r
   clockSettingsPtr = &Mcu_Global.config->McuClockSettingConfig[Mcu_Global.clockSetting];\r
 \r
+  InitMcuClocks(clockSettingsPtr);\r
 \r
-\r
-#if 0\r
-  /* 5516clock info:\r
-   * Fsys - System frequency ( CPU + all periperals? )\r
-   *\r
-   *  Fsys = EXTAL_FREQ *(  (emfd+16) / ( (eprediv+1) * ( erfd+1 )) ) )\r
-   */\r
-  // Check ranges...\r
-  assert((clockSettingsPtr->PllEmfd>=32) && (clockSettingsPtr->PllEmfd<=132));\r
-  assert( (clockSettingsPtr->PllEprediv!=6) &&\r
-          (clockSettingsPtr->PllEprediv!=8) &&\r
-          (clockSettingsPtr->PllEprediv<10) );\r
-  assert( clockSettingsPtr->PllErfd & 1); // Must be odd\r
-#endif\r
-\r
-\r
-\r
-#if defined(USE_DEBUG)\r
-  {\r
-    uint32    extal = Mcu_Global.config->McuClockSettingConfig[Mcu_Global.clockSetting].McuClockReferencePoint;\r
-    uint32    f_sys;\r
-\r
-    f_sys = CALC_SYSTEM_CLOCK( extal,\r
-        clockSettingsPtr->PllEmfd,\r
-        clockSettingsPtr->PllEprediv,\r
-        clockSettingsPtr->PllErfd );\r
-\r
-    //DEBUG(DEBUG_HIGH,"/drivers/mcu: F_sys will be:%08d Hz\n",f_sys);\r
-  }\r
-#endif\r
-\r
-#if defined(CFG_MPC5516)\r
-  // External crystal PLL mode.\r
-  FMPLL.ESYNCR1.B.CLKCFG = 7; //TODO: Hur ställa detta för 5567?\r
-\r
-  // Write pll parameters.\r
-  FMPLL.ESYNCR1.B.EPREDIV = clockSettingsPtr->PllEprediv;\r
-  FMPLL.ESYNCR1.B.EMFD    = clockSettingsPtr->PllEmfd;\r
-  FMPLL.ESYNCR2.B.ERFD    = clockSettingsPtr->PllErfd;\r
-\r
-  // Connect SYSCLK to FMPLL\r
-  SIU.SYSCLK.B.SYSCLKSEL = SYSCLOCK_SELECT_PLL;\r
-#elif defined(CFG_MPC5554) || defined(CFG_MPC5567)\r
-  FMPLL.SYNCR.B.PREDIV         = clockSettingsPtr->PllEprediv;\r
-  FMPLL.SYNCR.B.MFD            = clockSettingsPtr->PllEmfd;\r
-  FMPLL.SYNCR.B.RFD            = clockSettingsPtr->PllErfd;\r
-#endif\r
+  InitPerClocks(clockSettingsPtr);\r
 \r
   return E_OK;\r
 }\r
@@ -311,75 +406,80 @@ void Mcu_DistributePllClock(void)
 \r
 //-------------------------------------------------------------------\r
 \r
-Mcu_PllStatusType Mcu_GetPllStatus(void)\r
-{\r
-  VALIDATE_W_RV( ( 1 == Mcu_Global.initRun ), MCU_GETPLLSTATUS_SERVICE_ID, MCU_E_UNINIT, MCU_PLL_STATUS_UNDEFINED );\r
-  Mcu_PllStatusType rv;\r
 \r
-  if( !SIMULATOR() )\r
-  {\r
-#if 0\r
-    if ( !FMPLL.SYNSR.B.LOCK )\r
-    {\r
-      rv = MCU_PLL_UNLOCKED;\r
-    } else\r
-    {\r
-      rv = MCU_PLL_LOCKED;\r
-    }\r
-#endif\r
-  }\r
-  else\r
-  {\r
-    /* We are running on instruction set simulator. PLL is then always in sync... */\r
-    rv = MCU_PLL_LOCKED;\r
-  }\r
+Mcu_PllStatusType Mcu_GetPllStatus(void) {\r
+       VALIDATE_W_RV( ( 1 == Mcu_Global.initRun ), MCU_GETPLLSTATUS_SERVICE_ID, MCU_E_UNINIT, MCU_PLL_STATUS_UNDEFINED );\r
+       Mcu_PllStatusType rv;\r
 \r
-  return rv;\r
+#if !defined(USE_SIMULATOR)\r
+       if (RCC->CR & RCC_CR_PLLRDY) {\r
+               rv = MCU_PLL_LOCKED;\r
+       } else {\r
+               rv = MCU_PLL_UNLOCKED;\r
+       }\r
+#else\r
+       /* We are running on instruction set simulator. PLL is then always in sync... */\r
+       rv = MCU_PLL_LOCKED;\r
+#endif\r
+       return rv;\r
 }\r
 \r
 //-------------------------------------------------------------------\r
 \r
-Mcu_ResetType Mcu_GetResetReason(void)\r
-{\r
+/**\r
+ *\r
+ * @return\r
+ */\r
+Mcu_ResetType Mcu_GetResetReason(void) {\r
        Mcu_ResetType rv;\r
+       uint32_t csr;\r
 \r
-  VALIDATE_W_RV( ( 1 == Mcu_Global.initRun ), MCU_GETRESETREASON_SERVICE_ID, MCU_E_UNINIT, MCU_RESET_UNDEFINED );\r
+       VALIDATE_W_RV( ( 1 == Mcu_Global.initRun ), MCU_GETRESETREASON_SERVICE_ID, MCU_E_UNINIT, MCU_RESET_UNDEFINED );\r
 \r
-#if 0\r
-  if( SIU.RSR.B.SSRS ) {\r
-       rv = MCU_SW_RESET;\r
-  } else if( SIU.RSR.B.WDRS ) {\r
-       rv = MCU_WATCHDOG_RESET;\r
-  } else if( SIU.RSR.B.PORS || SIU.RSR.B.ERS ) {\r
-       rv = MCU_POWER_ON_RESET;\r
-  } else {\r
-       rv = MCU_RESET_UNDEFINED;\r
-  }\r
-#endif\r
+       csr = RCC->CSR;\r
 \r
-  return rv;\r
+       if (csr & RCC_CSR_SFTRSTF) {\r
+               rv = MCU_SW_RESET;\r
+       } else if (csr & (RCC_CSR_IWDGRSTF|RCC_CSR_WWDGRSTF) ) {\r
+               rv = MCU_WATCHDOG_RESET;\r
+       } else if ( csr & RCC_CSR_PORRSTF ) {\r
+               rv = MCU_POWER_ON_RESET;\r
+       } else {\r
+               rv = MCU_RESET_UNDEFINED;\r
+       }\r
+\r
+       return rv;\r
 }\r
 \r
 //-------------------------------------------------------------------\r
 \r
-Mcu_RawResetType Mcu_GetResetRawValue(void)\r
-{\r
-  VALIDATE_W_RV( ( 1 == Mcu_Global.initRun ), MCU_GETRESETREASON_SERVICE_ID, MCU_E_UNINIT, MCU_GETRESETRAWVALUE_UNINIT_RV );\r
-\r
-  if( !Mcu_Global.initRun ) {\r
-       return MCU_GETRESETRAWVALUE_UNINIT_RV;\r
-  }\r
+/**\r
+ * Shall read the raw reset value from hardware register if the hardware\r
+ * supports this.\r
+ *\r
+ * @return\r
+ */\r
 \r
-#if 0\r
-  return SIU.RSR.R;\r
-#endif\r
+Mcu_RawResetType Mcu_GetResetRawValue(void) {\r
+       VALIDATE_W_RV( ( 1 == Mcu_Global.initRun ), MCU_GETRESETREASON_SERVICE_ID, MCU_E_UNINIT, MCU_GETRESETRAWVALUE_UNINIT_RV );\r
 \r
-  return 0;\r
+       if (!Mcu_Global.initRun) {\r
+               return MCU_GETRESETRAWVALUE_UNINIT_RV;\r
+       } else {\r
+               return (RCC->CSR) & (RCC_CSR_RMVF | RCC_CSR_PINRSTF | RCC_CSR_PORRSTF\r
+                               | RCC_CSR_SFTRSTF | RCC_CSR_IWDGRSTF | RCC_CSR_WWDGRSTF\r
+                               | RCC_CSR_LPWRRSTF);\r
+       }\r
+       return 0;\r
 }\r
 \r
 //-------------------------------------------------------------------\r
 \r
 #if ( MCU_PERFORM_RESET_API == STD_ON )\r
+/**\r
+ * Shell perform a microcontroller reset by using the hardware feature\r
+ * of the micro controller.\r
+ */\r
 void Mcu_PerformReset(void)\r
 {\r
   VALIDATE( ( 1 == Mcu_Global.initRun ), MCU_PERFORMRESET_SERVICE_ID, MCU_E_UNINIT );\r
@@ -393,10 +493,10 @@ void Mcu_PerformReset(void)
 void Mcu_SetMode(const Mcu_ModeType McuMode)\r
 {\r
   VALIDATE( ( 1 == Mcu_Global.initRun ), MCU_SETMODE_SERVICE_ID, MCU_E_UNINIT );\r
-  VALIDATE( ( McuMode <= Mcu_Global.config->McuNumberOfMcuModes ), MCU_SETMODE_SERVICE_ID, MCU_E_PARAM_MODE );\r
+  VALIDATE( (0), MCU_SETMODE_SERVICE_ID, MCU_E_PARAM_MODE );\r
+  //VALIDATE( ( McuMode <= Mcu_Global.config->McuNumberOfMcuModes ), MCU_SETMODE_SERVICE_ID, MCU_E_PARAM_MODE );\r
   (void) McuMode;\r
 \r
-  /* NOT SUPPORTED */\r
 }\r
 \r
 //-------------------------------------------------------------------\r
@@ -407,51 +507,49 @@ void Mcu_SetMode(const Mcu_ModeType McuMode)
  */\r
 uint32_t McuE_GetSystemClock(void)\r
 {\r
-  /*\r
-   * System clock calculation\r
-   *\r
-   */
-
-  // TODO: This of course wrong....\r
-  uint32_t f_sys = 72000000UL;\r
-#if 0\r
-  uint32  extal = Mcu_Global.config->McuClockSettingConfig[Mcu_Global.clockSetting].McuClockReferencePoint;\r
+  uint32_t f_sys;\r
 \r
-  f_sys =  CALC_SYSTEM_CLOCK(extal,emfd,eprediv,erfd);\r
-#endif\r
+  uint32  extal = Mcu_Global.config->McuClockSettingConfig[Mcu_Global.clockSetting].McuClockReferencePointFrequency;\r
+  uint32 pll1 = Mcu_Global.config->McuClockSettingConfig[Mcu_Global.clockSetting].Pll1;\r
 \r
-//  f_sys = extal * (emfd+16) / ( (eprediv+1) * ( erfd+1 ));\r
-  return f_sys;\r
-}\r
-\r
-imask_t McuE_EnterCriticalSection()\r
-{\r
-#if 0\r
-  uint32_t msr = get_msr();\r
-  Irq_Disable();\r
-  return msr;\r
+#ifdef STM32F10X_CL\r
+  uint32 pll2 = Mcu_Global.config->McuClockSettingConfig[Mcu_Global.clockSetting].Pll2;\r
+  /* PLL2 configuration: PLL2CLK = (HSE / 5) * PLL2 */\r
+  /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 */\r
+  /* PLL configuration: PLLCLK = PREDIV1 * PLL1 */\r
+  f_sys = (extal / 5 * pll2) / 5 * pll1;\r
+#else\r
+  /* PLL configuration: PLLCLK = HSE * PLL1 */\r
+  f_sys = extal * pll1;\r
 #endif\r
-  return 0;\r
-}\r
 \r
-void McuE_ExitCriticalSection(uint32_t old_state)\r
-{\r
-#if 0\r
-  set_msr(old_state);\r
-#endif\r
+  return f_sys;\r
 }\r
 \r
 /**\r
  * Get the peripheral clock in Hz for a specific device\r
  */\r
-\r
-#if 0\r
 uint32_t McuE_GetPeripheralClock(McuE_PeriperalClock_t type)\r
 {\r
-\r
-       return 0;\r
+       uint32_t res = 0;\r
+\r
+       switch(type)\r
+       {\r
+       case PERIPHERAL_CLOCK_AHB:\r
+               res = McuE_GetSystemClock();\r
+               break;\r
+       case PERIPHERAL_CLOCK_APB1:\r
+               res = McuE_GetSystemClock() / 2;\r
+               break;\r
+       case PERIPHERAL_CLOCK_APB2:\r
+               res = McuE_GetSystemClock();\r
+               break;\r
+       default:\r
+               break;\r
+       }\r
+\r
+       return res;\r
 }\r
-#endif\r
 \r
 \r
 /**\r
@@ -462,13 +560,3 @@ void Mcu_ConfigureFlash(void)
 {\r
 \r
 }\r
-\r
-void McuE_EnableInterrupts(void)\r
-{\r
-  Irq_Enable();\r
-}\r
-\r
-void McuE_DisableInterrupts(void)\r
-{\r
-  Irq_Disable();\r
-}\r