本文將針對STML152的IAP移植過程作一個筆記。
首先得下載AN3310的示例代碼,地址為:http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32-standard-peripheral-libraries-expansions/stsw-stm32075.html
下載完成后,我們需要做些修改,我們將在NUCLEO-L152RE板子上進行驗證測試。
由于NUCLEO-L152RE板子默認MCU是沒有外掛晶振的,但可以通過ByPass方式使用ST-LInk的8M晶振,但這里只是作為IAP,且只需要使用到串口,因此可以只使用HSI,于是,在AN3310的工程中打開system_stm32l1xx.c文件找到SystemInit函數(shù),注釋掉//SetSysClock();,讓系統(tǒng)使用默認的HSI即可。
voidSystemInit(void)
{
/*! RCC->CR|=(uint32_t)0x00000100; /*! RCC->CFGR&=(uint32_t)0x88FFC00C; /*! RCC->CR&=(uint32_t)0xEEFEFFFE; /*! RCC->CR&=(uint32_t)0xFFFBFFFF; /*! RCC->CFGR&=(uint32_t)0xFF02FFFF; /*! RCC->CIR=0x00000000; #ifdefDATA_IN_ExtSRAM SystemInit_ExtMemCtl(); #endif/*DATA_IN_ExtSRAM*/ /*ConfiguretheSystemclockfrequency,AHB/APBxprescalersandFlashsettings*/ //SetSysClock(); #ifdefVECT_TAB_SRAM SCB->VTOR=SRAM_BASE|VECT_TAB_OFFSET;/*VectorTableRelocationinInternalSRAM.*/ #else SCB->VTOR=FLASH_BASE|VECT_TAB_OFFSET;/*VectorTableRelocationinInternalFLASH.*/ #endif } 然后修改man.c文件中的main函數(shù),注釋掉一些按鍵,讓程序默認進入到升級模式: intmain(void) { /*UnlocktheFlashProgramErasecontroller*/ FLASH_If_Init(); /*InitializeKeyButtonmountedonSTM32L15xx-EVALboard*/ //STM_EVAL_PBInit(BUTTON_KEY,BUTTON_MODE_GPIO); /*TestifKeypush-buttononSTM32L15xx-EVALBoardispressed*/ //if(STM_EVAL_PBGetState(BUTTON_KEY)!=0x00) { /*ExecutetheIAPdriverinordertoreprogramtheFlash*/ IAP_Init(); /*Displaymainmenu*/ Main_Menu(); } #if0 /*Keeptheuserapplicationrunning*/ else { /*Testifusercodeisprogrammedstartingfromaddress"APPLICATION_ADDRESS"*/ if(((*(__IOuint32_t*)APPLICATION_ADDRESS)&0x2FFE0000)==0x20000000) { /*Jumptouserapplication*/ JumpAddress=*(__IOuint32_t*)(APPLICATION_ADDRESS+4); Jump_To_Application=(pFunction)JumpAddress; /*Initializeuserapplication'sStackPointer*/ __set_MSP(*(__IOuint32_t*)APPLICATION_ADDRESS); Jump_To_Application(); } } #endif while(1) {} } voidIAP_Init(void) { USART_InitTypeDefUSART_InitStructure; /*USARTresourcesconfiguration(Clock,GPIOpinsandUSARTregisters)----*/ /*USARTconfiguredasfollow: -BaudRate=115200baud -WordLength=8Bits -OneStopBit -Noparity -Hardwareflowcontroldisabled(RTSandCTSsignals) -Receiveandtransmitenabled */ USART_InitStructure.USART_BaudRate=115200; USART_InitStructure.USART_WordLength=USART_WordLength_8b; USART_InitStructure.USART_StopBits=USART_StopBits_1; USART_InitStructure.USART_Parity=USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; //STM_EVAL_COMInit(COM2,&USART_InitStructure); USART2_Init(&USART_InitStructure); } voidUSART2_Init(USART_InitTypeDef*USART_InitStruct) { GPIO_InitTypeDefGPIO_InitStructure; /*EnableGPIOclock*/ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE); /*EnableUARTclock*/ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); /*ConnectPXxtoUSARTx_Tx*/ GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2); /*ConnectPXxtoUSARTx_Rx*/ GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2); /*ConfigureUSARTTxasalternatefunctionpush-pull*/ GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_40MHz; GPIO_InitStructure.GPIO_OType=GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP; GPIO_Init(GPIOA,&GPIO_InitStructure); /*ConfigureUSARTRxasalternatefunctionpush-pull*/ //GPIO_InitStructure.GPIO_Pin=COM_RX_PIN[COM]; //GPIO_Init(COM_RX_PORT[COM],&GPIO_InitStructure); /*USARTconfiguration*/ USART_Init(USART2,USART_InitStruct); /*EnableUSART*/ USART_Cmd(USART2,ENABLE); } voidSerialPutChar(uint8_tc) { USART_SendData(USART2,c); while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET) { } } uint32_tSerialKeyPressed(uint8_t*key) { if(USART_GetFlagStatus(USART2,USART_FLAG_RXNE)!=RESET) { *key=(uint8_t)USART2->DR; return1; } else { return0; } } 接下來需要找一個PC端軟件超級
由于NUCLEO板子默認使用的是USART2,因此需要修改使用的串口:
USART2_Init()函數(shù)為新添加的串口初始化函數(shù),其定義如下:
接下來需要修改程序中使用到的打印函數(shù):
基本上就移植好了,編譯燒錄進NUCLEO板中運行。