簡體   English   中英

STM32F107 UART引導加載程序mikroC

[英]STM32F107 UART Bootloader mikroC

我嘗試采用mikroelektronika的UART Bootloader,它是為STM32F051r8( https://www.st.com/resource/zh/datasheet/stm32f051r8.pdf )編寫的

到STM32F107VCT7( https://www.mouser.de/datasheet/2/389/CD00220364-490297.pdf

這是原始代碼:

/*
 * Project name:
     USB_UART_Bootloader
 * Copyright:
     (c) MikroElektronika, 2018.
 * Revision History:
     - initial release;
 * Description:
     MCU flash. It is used, instead of programming tools, to
     load real program code into the MCU. Real program code can
     be passed from PC to bootloader by specific PC application
     (Bootloader Tool) over numerous communication interfaces.

     This bootloader communicates with PC over UART inteface
     by using mikroE bootloader protocol. It is designed to
     work in conjuction with mikroE's 'mikroBootloader'
     PC application.

 * Test configuration:
     MCU:             STM32F051R8
                      https://www.st.com/resource/en/datasheet/stm32f051r8.pdf
     Dev.Board:       MINI-M0 for STM32
                      https://www.mikroe.com/mini-stm32f0
     Oscillator:      48.000000
     Ext. Modules:    None
     SW:              mikroC PRO for ARM v6.0.0
                      https://www.mikroe.com/mikroc-arm
 * NOTES:
     - It's recommended not to alter the start address of the main().
       Otherwise, less space in ROM will be available for the
       application being bootloaded.
*/
////////////////////////////////////////////////////////////////////////////////
#include <built_in.h>
////////////////////////////////////////////////////////////////////////////////
#pragma orgall 0xEC00
#define BOOTLOADER_START_ADDR   0xEC00
#define START_PROGRAM_ADDR      0xFC00
////////////////////////////////////////////////////////////////////////////////
static char block[1024];
////////////////////////////////////////////////////////////////////////////////
void Start_Program() org START_PROGRAM_ADDR{

}
////////////////////////////////////////////////////////////////////////////////
unsigned short UART_Write_Loop(char send, char receive){
  unsigned int rslt = 0;

  while(1){
    Delay_5ms();
    UART_Write(send);
    Delay_5ms();

    rslt++;
    if (rslt == 0x0200)
      return 0;
    if(UART_Data_Ready()) {
      if(UART_Read() == receive)
        return 1;
    }
  }
}
////////////////////////////////////////////////////////////////////////////////
void FLASH_EraseWritePage(unsigned long address) {
  unsigned int i = 0;
  unsigned int dataToWrite;

  FLASH_ErasePage(address);

  for (i = 0; i < 512; i++)
  {
    dataToWrite = block[i * 2] | (block[i * 2 + 1] << 8);
    FLASH_Write_HalfWord(address + i*2, dataToWrite);
  }
}
////////////////////////////////////////////////////////////////////////////////
void Write_Begin(){
  unsigned int i;
  unsigned long* ptr;
  unsigned char appResetVector[16];
  unsigned long arm_m0_inst;
  unsigned int dataToWrite;

  //LDR R0, PC+X
  arm_m0_inst = 0x4800 + 1;

  appResetVector[0] = arm_m0_inst;
  appResetVector[1] = arm_m0_inst >> 8;

  //MOV SP, R0
  arm_m0_inst = 0x4685;

  appResetVector[2] = arm_m0_inst;
  appResetVector[3] = arm_m0_inst >> 8;

  //LDR R0, PC+Y
  arm_m0_inst = 0x4800 + 1;

  appResetVector[4] = arm_m0_inst;
  appResetVector[5] = arm_m0_inst >> 8;

  //BX R0
  arm_m0_inst = 0x4700;
  appResetVector[6] = arm_m0_inst;
  appResetVector[7] = arm_m0_inst >> 8;

  //SP
  appResetVector[8] = block[0];
  appResetVector[9] = block[1];
  appResetVector[10] = block[2];
  appResetVector[11] = block[3];

  //PC
  appResetVector[12] = block[4];
  appResetVector[13] = block[5];
  appResetVector[14] = block[6];
  appResetVector[15] = block[7];

  FLASH_ErasePage(START_PROGRAM_ADDR);

  for (i = 0; i < 8; i++)
  {
    dataToWrite = appResetVector[i * 2] | (appResetVector[i * 2 + 1] << 8);
    FLASH_Write_HalfWord(START_PROGRAM_ADDR + i*2, dataToWrite);
  }

  ptr = (unsigned long*)0x00000000;
  block[0] = LoWord(*ptr);
  block[1] = LoWord(*ptr) >> 8;
  block[2] = HiWord(*ptr);
  block[3] = HiWord(*ptr) >> 8;

  ptr++;

  block[4] = LoWord(*ptr);
  block[5] = LoWord(*ptr) >> 8;
  block[6] = HiWord(*ptr);
  block[7] = HiWord(*ptr) >> 8;
}
////////////////////////////////////////////////////////////////////////////////
void Start_Bootload(){
  unsigned int i = 0;
  char xx, yy;
  long j = 0;

  while (1) {
    if (i == 1024) {
      //--- If 256 words (1024 bytes) recieved then write to flash
      if (!j)
        Write_Begin();
      if (j < BOOTLOADER_START_ADDR) {
        FLASH_EraseWritePage(j);
      }

      i = 0;
      j += 0x400;
    }
    //--- Ask for yy
    UART_Write('y');
    while (!UART_Data_Ready()) ;
    //--- Read yy
    yy = UART_Read();
    //--- Ask for xx
    UART_Write('x');
    while (!UART_Data_Ready()) ;
    //--- Read xx
    xx = UART_Read();
    //--- Save xxyy in block[i]
    block[i++] = yy;
    block[i++] = xx;
  }
}
////////////////////////////////////////////////////////////////////////////////
void main() org BOOTLOADER_START_ADDR{
// Main program
 // UART1_Init_Advanced(115200, _UART_8_BIT_DATA, _UART_NOPARITY, _UART_ONE_STOPBIT, &_GPIO_MODULE_USART1_PA9_10);   // Display
    // Delay_ms(1000);

    UART2_Init_Advanced(115200, _UART_8_BIT_DATA, _UART_NOPARITY, _UART_ONE_STOPBIT, &_GPIO_MODULE_USART2_PD56);              // USB Serial
    Delay_ms(1000);


  Delay_100ms();

  if (UART_Write_Loop('g','r')) {     // Send 'g' for ~5 sec, if 'r'
    Start_Bootload();                 //   received start bootload
  }
  else {
    Start_Program();                  //   else start program
  }
}
////////////////////////////////////////////////////////////////////////////////

我認為我唯一需要修改的是這些行:

#pragma orgall 0xEC00
#define BOOTLOADER_START_ADDR   0xEC00
#define START_PROGRAM_ADDR      0xFC00

我研究了STM32F051r8數據表,不能為Flash的開頭找到0xEC00的任何參考。

STM32F107VCT7需要哪些值?

當我使用頂部的值並將其編譯為107時,它可以工作,但是要上傳MCU庫存。

你好,我認為你在地址和配置上有很多問題,我使用了這段代碼,它可以正常工作

用於從閃存中擦除主程序:

#define Applicationend 0x08040000
#define ApplicationAddress    0x8003000
#define FLASH_PAGE_SIZE    ((uint16_t)0x400)
//////////////////////////////////

     FLASH_UnlockBank1();
   NbrOfPage = (Applicationend - ApplicationAddress) / FLASH_PAGE_SIZE;
   FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);    
     for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)
            {
            FLASHStatus = FLASH_ErasePage(ApplicationAddress + (FLASH_PAGE_SIZE * EraseCounter));
            }   

用於對閃光燈進行編程:

 while((Address < (size_of_program)) && (FLASHStatus == FLASH_COMPLETE))
  {

    df_read_open(Address+temp);
  df_read(update_r,2); 

    FLASHStatus = FLASH_ProgramHalfWord((Address+ApplicationAddress),( update_r[0]|(update_r[1]<<8)));
    Address = Address + 2;
  } 
    FLASH_LockBank1();

並用於運行主程序

    if (((*(__IO uint32_t*)ApplicationAddress) & 0x2FFE0000 ) == 0x20000000)
{ 
  /* Jump to user application */
  JumpAddress = *(__IO uint32_t*) (ApplicationAddress + 4);
  Jump_To_Application = (pFunction) JumpAddress;
  /* Initialize user application's Stack Pointer */
  __set_MSP(*(__IO uint32_t*) ApplicationAddress);
  Jump_To_Application();
}   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM