STM32F10x_StdPeriph_Examples/CortexM3/Mode_Privilege/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    CortexM3/Mode_Privilege/main.c 
00004   * @author  MCD Application Team
00005   * @version V3.4.0
00006   * @date    10/15/2010
00007   * @brief   Main program body.
00008   ******************************************************************************
00009   * @copy
00010   *
00011   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00012   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00013   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00014   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00015   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00016   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00017   *
00018   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
00019   */ 
00020 
00021 /* Includes ------------------------------------------------------------------*/
00022 #include "stm32f10x.h"
00023 
00024 /** @addtogroup STM32F10x_StdPeriph_Examples
00025   * @{
00026   */
00027 
00028 /** @addtogroup CortexM3_Mode_Privilege
00029   * @{
00030   */ 
00031 
00032 /* Private typedef -----------------------------------------------------------*/
00033 /* Private define ------------------------------------------------------------*/
00034 #define SP_PROCESS_SIZE             0x200  /* Process stack size */
00035 #define SP_PROCESS                  0x02   /* Process stack */
00036 #define SP_MAIN                     0x00   /* Main stack */
00037 #define THREAD_MODE_PRIVILEGED      0x00   /* Thread mode has privileged access */
00038 #define THREAD_MODE_UNPRIVILEGED    0x01   /* Thread mode has unprivileged access */
00039 
00040 /* Private macro -------------------------------------------------------------*/ 
00041 #if defined ( __CC_ARM   )
00042 __ASM void __SVC(void) 
00043 { 
00044   SVC 0x01 
00045   BX R14
00046 }
00047 #elif defined ( __ICCARM__ )
00048 static __INLINE  void __SVC()                     { __ASM ("svc 0x01");}
00049 #elif defined   (  __GNUC__  )
00050 static __INLINE void __SVC()                      { __ASM volatile ("svc 0x01");}
00051 #endif
00052 
00053 /* Private variables ---------------------------------------------------------*/
00054 __IO uint8_t PSPMemAlloc[SP_PROCESS_SIZE];
00055 __IO uint32_t Index = 0, PSPValue = 0, CurrentStack = 0, ThreadMode = 0;
00056 
00057 /* Private function prototypes -----------------------------------------------*/
00058 /* Private functions ---------------------------------------------------------*/
00059 
00060 /**
00061   * @brief   Main program.
00062   * @param  None
00063   * @retval None
00064   */
00065 int main(void)
00066 {
00067   /*!< At this stage the microcontroller clock setting is already configured, 
00068        this is done through SystemInit() function which is called from startup
00069        file (startup_stm32f10x_xx.s) before to branch to application main.
00070        To reconfigure the default setting of SystemInit() function, refer to
00071        system_stm32f10x.c file
00072      */     
00073        
00074 /* Switch Thread mode Stack from Main to Process -----------------------------*/
00075   /* Initialize memory reserved for Process Stack */
00076   for(Index = 0; Index < SP_PROCESS_SIZE; Index++)
00077   {
00078     PSPMemAlloc[Index] = 0x00;
00079   }
00080 
00081   /* Set Process stack value */ 
00082   __set_PSP((uint32_t)PSPMemAlloc + SP_PROCESS_SIZE);
00083   
00084   /* Select Process Stack as Thread mode Stack */
00085   __set_CONTROL(SP_PROCESS);
00086 
00087   /* Get the Thread mode stack used */
00088   if((__get_CONTROL() & 0x02) == SP_MAIN)
00089   {
00090     /* Main stack is used as the current stack */
00091     CurrentStack = SP_MAIN;
00092   }
00093   else
00094   {
00095     /* Process stack is used as the current stack */
00096     CurrentStack = SP_PROCESS;
00097 
00098     /* Get process stack pointer value */
00099     PSPValue = __get_PSP();     
00100   }
00101   
00102 /* Switch Thread mode from privileged to unprivileged ------------------------*/
00103   /* Thread mode has unprivileged access */
00104   __set_CONTROL(THREAD_MODE_UNPRIVILEGED | SP_PROCESS);
00105 
00106   /* Unprivileged access mainly affect ability to:
00107       - Use or not use certain instructions such as MSR fields
00108       - Access System Control Space (SCS) registers such as NVIC and SysTick */
00109 
00110   /* Check Thread mode privilege status */
00111   if((__get_CONTROL() & 0x01) == THREAD_MODE_PRIVILEGED)
00112   {
00113     /* Thread mode has privileged access  */
00114     ThreadMode = THREAD_MODE_PRIVILEGED;
00115   }
00116   else
00117   {
00118     /* Thread mode has unprivileged access*/
00119     ThreadMode = THREAD_MODE_UNPRIVILEGED;
00120   }
00121 
00122 /* Switch back Thread mode from unprivileged to privileged -------------------*/  
00123   /* Try to switch back Thread mode to privileged (Not possible, this can be
00124      done only in Handler mode) */
00125   __set_CONTROL(THREAD_MODE_PRIVILEGED | SP_PROCESS);
00126 
00127   /* Generate a system call exception, and in the ISR switch back Thread mode
00128     to privileged */
00129   __SVC();
00130 
00131   /* Check Thread mode privilege status */
00132   if((__get_CONTROL() & 0x01) == THREAD_MODE_PRIVILEGED)
00133   {
00134     /* Thread mode has privileged access  */
00135     ThreadMode = THREAD_MODE_PRIVILEGED;
00136   }
00137   else
00138   {
00139     /* Thread mode has unprivileged access*/
00140     ThreadMode = THREAD_MODE_UNPRIVILEGED;
00141   }
00142 
00143   while (1)
00144   {
00145   }
00146 }
00147 
00148 #ifdef  USE_FULL_ASSERT
00149 /**
00150   * @brief  Reports the name of the source file and the source line number
00151   *         where the assert_param error has occurred.
00152   * @param  file: pointer to the source file name
00153   * @param  line: assert_param error line source number
00154   * @retval None
00155   */
00156 void assert_failed(uint8_t* file, uint32_t line)
00157 { 
00158   /* User can add his own implementation to report the file name and line number,
00159      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00160 
00161   /* Infinite loop */
00162   while (1)
00163   {
00164   }
00165 }
00166 #endif
00167 
00168 /**
00169   * @}
00170   */ 
00171 
00172 /**
00173   * @}
00174   */ 
00175 
00176 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
STM32F10x Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM32(CORTEX M3) 32-bit Microcontrollers platform visit  www.st.com/STM32