打印
[应用相关]

AT32F415 运行程序跳转到bootloader

[复制链接]
2137|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
muyichuan2012|  楼主 | 2021-1-4 19:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  AT32F415 的Demo:
  将附件解压到Project\AT_START_F415\Examples 即可使用;
  目前Demo 是通过 “USER” 按键触发跳转到Bootloader。

/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include "at32f4xx.h"
#include "at32_board.h"
/** @addtogroup AT32F415_StdPeriph_Templates
  * @{
  */

/** @addtogroup Template
  * @{
  */

/* Private define ------------------------------------------------------------*/
#define DELAY          100
#define FAST           1
#define SLOW           4

#define BOOTLOADER_ADDRESS  0x1FFFAC00
#define BKP_JUMP_FLAG       0x5AA5
#define SET_MSP(dwValue) __ASM volatile ("msr msp, "#dwValue)
void (*pfTarget)(void);


/* Extern variables ---------------------------------------------------------*/
extern GPIO_Type *BUTTON_GPIO_PORT[BUTTON_NUM];
extern uint16_t BUTTON_GPIO_PIN[BUTTON_NUM];
extern uint32_t BUTTON_GPIO_RCC_CLK [BUTTON_NUM];

/* Private variables ---------------------------------------------------------*/
uint16_t BUTTON_EXTI_LINE[BUTTON_NUM] = {EXTI_Line0, EXTI_Line13};
uint16_t BUTTON_EXTI_IRQ[BUTTON_NUM] = {EXTI0_IRQn, EXTI15_10_IRQn};
uint8_t BUTTON_EXTI_SOURCE_PORT[BUTTON_NUM] = {GPIO_PortSourceGPIOA, GPIO_PortSourceGPIOC};
uint8_t BUTTON_EXTI_SOURCE_PIN[BUTTON_NUM] = {GPIO_PinsSource0, GPIO_PinsSource13};
uint8_t gUpdateFlag = 0;

BUTTON_Type gButtonType = BUTTON_WAKEUP;
uint8_t gSpeed = FAST;

/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Configure Button EXTI   
  * @param  Button: Specifies the Button to be configured.
  * @retval None
  */
void BUTTON_EXTI_Init(BUTTON_Type button)
{
  EXTI_InitType EXTI_InitStructure;
  NVIC_InitType NVIC_InitStructure;

  GPIO_EXTILineConfig(BUTTON_EXTI_SOURCE_PORT[button], BUTTON_EXTI_SOURCE_PIN[button]);
  EXTI_StructInit(&EXTI_InitStructure);
  EXTI_InitStructure.EXTI_Line = BUTTON_EXTI_LINE[button];
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_LineEnable = ENABLE;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  EXTI_Init(&EXTI_InitStructure);

  NVIC_InitStructure.NVIC_IRQChannel = BUTTON_EXTI_IRQ[button];
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_Init(&NVIC_InitStructure);
}

/**
  * @brief  Button EXTI0 Interrupt Handler
  * @param  None
  * @retval None
  */
void EXTI0_IRQHandler(void)
{
  Delay_ms(5);
  EXTI_ClearIntPendingBit(BUTTON_EXTI_LINE[gButtonType]);
  if ((GPIO_ReadInputData(BUTTON_GPIO_PORT[gButtonType]) & BUTTON_GPIO_PIN[gButtonType]) == \
      BUTTON_GPIO_PIN[gButtonType])
  {
    gUpdateFlag = 1;
  }
}

/**
  * @brief Clear all System Status
        1. Close All Peripherals Clock
        2. Close PLL
        3. Disable All interrupt
        4. Clear All Pending Interrupt Flag      
  * @param  None
  * @retval None
  */
void APP_ClearSysStatus()
{
  /*Close Peripherals Clock*/
  RCC->APB2RST = 0xFFFFFFFF;
  RCC->APB2RST = 0;
  RCC->APB1RST = 0xFFFFFFFF;
  RCC->APB1RST = 0;
  RCC->APB2EN = 0;
  RCC->APB1EN = 0;

  RCC->AHBRST = 0xFFFFFFFF;
  RCC->AHBRST = 0;

  /*close USB clock*/
  RCC_AHBPeriphClockCmd(RCC_AHBPERIPH_USB, DISABLE);

  /*Close PLL*/
  /* Reset SW, AHBPSC, APB1PSC, APB2PSC, ADCPSC and CLKOUT bits */
  RCC_Reset();

  /*Colse Systick*/
  SysTick->CTRL = 0;

  /*Disable ALL interrupt && Pending Interrupt Flag*/
  /*这里需要根据用户开启的外设进行清除中断和挂起的中断标志*/
  /*
  user add code...

  */
}

/*
1. Close All Peripherals Clock
2. Close PLL
3. Disable All interrupt
4. Clear All Pending Interrupt Flag
*/


/**
  * @brief  Jump to bootloader      
  * @param  None
  * @retval None
  */
void APPJumpToBootloader(void)
{
    uint32_t dwStkPtr, dwJumpAddr;
    dwStkPtr = *(uint32_t *)BOOTLOADER_ADDRESS;
    dwJumpAddr = *(uint32_t *)(BOOTLOADER_ADDRESS + sizeof(uint32_t));

    /*跳转之前,需要保证所有外设Clock 关闭,PLL 关闭,关闭所有中断,清除所有的中断挂起标志*/
    SET_MSP(dwStkPtr);
    pfTarget = (void (*)(void))dwJumpAddr;
    pfTarget();


}

/**
  * @brief  Main Function.
  * @param  None
  * @retval None
  */
int main(void)
{
  RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_AFIO, ENABLE);

  AT32_Board_Init();

  BUTTON_EXTI_Init(gButtonType);

  for(;;)
  {
    if ( gUpdateFlag == 1 )
    {
      /*Jump to System Memory*/
      APP_ClearSysStatus();
      APPJumpToBootloader();
    }
  }
}

Jump_To_System_Memory.zip

19.83 KB

使用特权

评论回复
沙发
pattywu| | 2021-1-4 20:43 | 只看该作者
是代码,就顶一下,做个记号。以便查找 。

使用特权

评论回复
板凳
ytmpeigd| | 2021-5-26 14:28 | 只看该作者
能下载吗?

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

198

主题

1910

帖子

29

粉丝