ARM处理器基础Cortex-M4

发布时间:2024-07-10  

启动过程

处理器工作模式

处理器模式分为线程模式和处理模式;软件执行分特权模式和非特权模式(用户模式);堆栈分为MSP Main主堆栈和PSP Program程序堆栈。

处理模式下,总是为特权,总是使用主堆栈。

线程模式下,可设置是特权还是用户(CONTROL Reg[0]),可设置使用主堆栈还是程序堆栈(CONTROL Reg[1])。

PM0214,Programming manual,STM32F3 and STM32F4 Series Cortex ® -M4 programming manual,p16

The processor modes are:
  Thread mode: Used to execute application software.
    The processor enters Thread mode when it comes out of reset.
    The CONTROL register controls whether software execution is
    privileged or unprivileged, see CONTROL register on page 24.
  Handler mode: Used to handle exceptions.
    The processor returns to Thread mode when it has finished exception
    processing.
    Software execution is always privileged.

The privilege levels for software execution are:

  Unprivileged: Unprivileged software executes at the unprivileged level and:
    • Has limited access to the MSR and MRS instructions, and cannot
    use the CPS instruction
    • Cannot access the system timer, NVIC, or system control block
    • Might have restricted access to memory or peripherals
    • Must use the SVC instruction to make a supervisor call to transfer control to privileged software
  Privileged: Privileged software executes at the privileged level and can use all the
    instructions and has access to all resources.
    Can write to the CONTROL register to change the privilege level for
    software execution.

In an OS environment, it is recommended that threads running in Thread mode use the process stack and the kernel and exception handlers use the main stack.

有操作系统的环境,推荐线程模式使用程序堆栈PSP,内核和异常处理使用主堆栈MSP。


在复位后,处理器处于线程模式+特权级;

特权到用户:在特权级下的代码可以通过置位CONTROL[0]来进入用户级。

用户到特权:用户级的程序不能简简单单地试图改写 CONTROL寄存器就回到特权级,它必须先“申诉”:执行一条系统调用指令(SVC)。这会触发SVC异常,然后由异常服务例程(通常是操作系统的一部分)接管,如果批准了进入,则异常服务例程修改 CONTROL寄存器,才能在用户级的线程模式下重新进入特权级。 事实上,从用户级到特权级的唯一途径就是异常。

By default, Thread mode uses the MSP.
To switch the stack pointer used in Thread mode to the PSP
(1) use the MSR instruction to set the Active stack pointer bit to 1, CONTROL[1] = 1
(2) perform an exception return to Thread mode with the appropriate EXC_RETURN value


When changing the stack pointer, software must use an ISB instruction immediately after the MSR instruction. This ensures that instructions after the ISB instruction execute using the new stack pointer. 


线程默认使用主堆栈MSP,若想使用程序堆栈PSP,有两种转换方式。

常用汇编指令

PUSH POP

PUSH{cond} reglist
POP{cond} reglist

• PUSH stores registers on the stack in order of decreasing register numbers, with the highest numbered register using the highest memory address and the lowest
numbered register using the lowest memory address.
• POP loads registers from the stack in order of increasing register numbers, with the lowest numbered register using the lowest memory address and the highest numbered register using the highest memory address. (编程手册p77)

push {r0, r1},由于ARM使用Full Descending堆栈,即堆栈指针指向最后一个数据,堆栈地址递减。因此,该句入栈顺序为先r1,后r0,以符合上面的描述。

MSR

Move the contents of a general-purpose register into the specified special register. 

拷贝通用Reg到特殊S reg

• ‘Rn’ is the source register.
• ‘spec_reg’ can be any of: APSR, IPSR, EPSR, IEPSR, IAPSR, EAPSR, PSR, MSP, PSP, PRIMASK, BASEPRI, BASEPRI_MAX, FAULTMASK, or CONTROL.

The register access operation in MSR depends on the privilege level. Unprivileged software can only access the APSR

除APSR外,操作其它特殊寄存器,必须在特权模式下。类似指令MRS。

CPS

Change processor state.

CPSID i ; Disable interrupts and configurable fault handlers (set PRIMASK)
CPSID f ; Disable interrupts and all fault handlers (set FAULTMASK)
CPSIE i ; Enable interrupts and configurable fault handlers(clear PRIMASK)
CPSIE f ; Enable interrupts and fault handlers (clear FAULTMASK)


文章来源于:电子工程世界    原文链接
本站所有转载文章系出于传递更多信息之目的,且明确注明来源,不希望被转载的媒体或个人可与我们联系,我们将立即进行删除处理。

我们与500+贴片厂合作,完美满足客户的定制需求。为品牌提供定制化的推广方案、专属产品特色页,多渠道推广,SEM/SEO精准营销以及与公众号的联合推广...详细>>

利用葫芦芯平台的卓越技术服务和新产品推广能力,原厂代理能轻松打入消费物联网(IOT)、信息与通信(ICT)、汽车及新能源汽车、工业自动化及工业物联网、装备及功率电子...详细>>

充分利用其强大的电子元器件采购流量,创新性地为这些物料提供了一个全新的窗口。我们的高效数字营销技术,不仅可以助你轻松识别与连接到需求方,更能够极大地提高“闲置物料”的处理能力,通过葫芦芯平台...详细>>

我们的目标很明确:构建一个全方位的半导体产业生态系统。成为一家全球领先的半导体互联网生态公司。目前,我们已成功打造了智能汽车、智能家居、大健康医疗、机器人和材料等五大生态领域。更为重要的是...详细>>

我们深知加工与定制类服务商的价值和重要性,因此,我们倾力为您提供最顶尖的营销资源。在我们的平台上,您可以直接接触到100万的研发工程师和采购工程师,以及10万的活跃客户群体...详细>>

凭借我们强大的专业流量和尖端的互联网数字营销技术,我们承诺为原厂提供免费的产品资料推广服务。无论是最新的资讯、技术动态还是创新产品,都可以通过我们的平台迅速传达给目标客户...详细>>

我们不止于将线索转化为潜在客户。葫芦芯平台致力于形成业务闭环,从引流、宣传到最终销售,全程跟进,确保每一个potential lead都得到妥善处理,从而大幅提高转化率。不仅如此...详细>>