你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32L152的IAP移植笔记

[复制链接]
aimejia 发布时间:2018-5-25 11:07
文将针对STML152的IAP移植过程作一个笔记。

首先得下载AN3310的示例代码,地址为:http://www.st.com/content/st_com ... /stsw-stm32075.html

下载完成后,我们需要做些修改,我们将在NUCLEO-L152RE板子上进行验证测试。

由于NUCLEO-L152RE板子默认MCU是没有外挂晶振的,但可以通过ByPass方式使用ST-LInk的8M晶振,但这里只是作为IAP,且只需要使用到串口,因此可以只使用HSI,于是,在AN3310的工程中打开system_stm32l1xx.c文件找到SystemInit函数,注释掉//SetSysClock();,让系统使用默认的HSI即可。
  1. [cpp] view plain copy
  2. void SystemInit (void)  
  3. {  
  4.   /*!< Set MSION bit */  
  5.   RCC->CR |= (uint32_t)0x00000100;  
  6.   
  7.   /*!< Reset SW[1:0], HPRE[3:0], PPRE1[2:0], PPRE2[2:0], MCOSEL[2:0] and MCOPRE[2:0] bits */  
  8.   RCC->CFGR &= (uint32_t)0x88FFC00C;  
  9.   
  10.   /*!< Reset HSION, HSEON, CSSON and PLLON bits */  
  11.   RCC->CR &= (uint32_t)0xEEFEFFFE;  
  12.   
  13.   /*!< Reset HSEBYP bit */  
  14.   RCC->CR &= (uint32_t)0xFFFBFFFF;  
  15.   
  16.   /*!< Reset PLLSRC, PLLMUL[3:0] and PLLDIV[1:0] bits */  
  17.   RCC->CFGR &= (uint32_t)0xFF02FFFF;  
  18.   
  19.   /*!< Disable all interrupts */  
  20.   RCC->CIR = 0x00000000;  
  21.   
  22. #ifdef DATA_IN_ExtSRAM  
  23.   SystemInit_ExtMemCtl();  
  24. #endif /* DATA_IN_ExtSRAM */  
  25.   
  26.   /* Configure the System clock frequency, AHB/APBx prescalers and Flash settings */  
  27.   //SetSysClock();  
  28.   
  29. #ifdef VECT_TAB_SRAM  
  30.   SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */  
  31. #else  
  32.   SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */  
  33. #endif  
  34. }  
复制代码
然后修改man.c文件中的main函数,注释掉一些按键,让程序默认进入到升级模式:

  1. [cpp] view plain copy
  2. int main(void)  
  3. {  
  4.   /* Unlock the Flash Program Erase controller */  
  5.   FLASH_If_Init();  
  6.   
  7.   /* Initialize Key Button mounted on STM32L15xx-EVAL board */  
  8.   //STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO);  
  9.   
  10.   /* Test if Key push-button on STM32L15xx-EVAL Board is pressed */  
  11.   //if (STM_EVAL_PBGetState(BUTTON_KEY) != 0x00)  
  12.   {  
  13.     /* Execute the IAP driver in order to reprogram the Flash */  
  14.     IAP_Init();  
  15.     /* Display main menu */  
  16.     Main_Menu ();  
  17.   }  
  18. #if 0  
  19.   /* Keep the user application running */  
  20.   else  
  21.   {  
  22.     /* Test if user code is programmed starting from address "APPLICATION_ADDRESS" */  
  23.     if (((*(__IO uint32_t*)APPLICATION_ADDRESS) & 0x2FFE0000 ) == 0x20000000)  
  24.     {  
  25.       /* Jump to user application */  
  26.       JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);  
  27.       Jump_To_Application = (pFunction) JumpAddress;  
  28.       /* Initialize user application's Stack Pointer */  
  29.       __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);  
  30.       Jump_To_Application();  
  31.     }  
  32.   }  
  33. #endif  
  34.   while (1)  
  35.   {}  
  36. }  
复制代码
由于NUCLEO板子默认使用的是USART2,因此需要修改使用的串口:
  1. [cpp] view plain copy
  2. void IAP_Init(void)  
  3. {  
  4. USART_InitTypeDef USART_InitStructure;  
  5.   /* USART resources configuration (Clock, GPIO pins and USART registers) ----*/  
  6.   /* USART configured as follow:
  7.         - BaudRate = 115200 baud
  8.         - Word Length = 8 Bits
  9.         - One Stop Bit
  10.         - No parity
  11.         - Hardware flow control disabled (RTS and CTS signals)
  12.         - Receive and transmit enabled
  13.   */  
  14.   USART_InitStructure.USART_BaudRate = 115200;  
  15.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;  
  16.   USART_InitStructure.USART_StopBits = USART_StopBits_1;  
  17.   USART_InitStructure.USART_Parity = USART_Parity_No;  
  18.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;  
  19.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;  
  20.   
  21.   //STM_EVAL_COMInit(COM2, &USART_InitStructure);  
  22.   USART2_Init(&USART_InitStructure);  
  23. }  

  24. USART2_Init()函数为新添加的串口初始化函数,其定义如下:

  25. [cpp] view plain copy
  26. void USART2_Init(USART_InitTypeDef* USART_InitStruct)  
  27. {  
  28.   GPIO_InitTypeDef GPIO_InitStructure;  
  29.   
  30.   /* Enable GPIO clock */  
  31.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);  
  32.   
  33.   /* Enable UART clock */  
  34.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);  
  35.   
  36.   /* Connect PXx to USARTx_Tx */  
  37.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);  
  38.   
  39.   /* Connect PXx to USARTx_Rx */  
  40.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);  
  41.   
  42.   /* Configure USART Tx as alternate function push-pull */  
  43.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;  
  44.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  
  45.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;  
  46.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  
  47.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;  
  48.   GPIO_Init(GPIOA, &GPIO_InitStructure);  
  49.   
  50.   /* Configure USART Rx as alternate function push-pull */  
  51.   //GPIO_InitStructure.GPIO_Pin = COM_RX_PIN[COM];  
  52.   //GPIO_Init(COM_RX_PORT[COM], &GPIO_InitStructure);  
  53.   
  54.   /* USART configuration */  
  55.   USART_Init(USART2, USART_InitStruct);  
  56.   
  57.   /* Enable USART */  
  58.   USART_Cmd(USART2, ENABLE);  
  59. }  
复制代码
接下来需要修改程序中使用到的打印函数:
  1. [cpp] view plain copy
  2. void SerialPutChar(uint8_t c)  
  3. {  
  4.   USART_SendData(USART2, c);  
  5.   while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)  
  6.   {  
  7.   }  
  8. }  


  9. [cpp] view plain copy
  10. uint32_t SerialKeyPressed(uint8_t *key)  
  11. {  
  12.   
  13.   if ( USART_GetFlagStatus(USART2, USART_FLAG_RXNE) != RESET)  
  14.   {  
  15.     *key = (uint8_t)USART2->DR;  
  16.     return 1;  
  17.   }  
  18.   else  
  19.   {  
  20.     return 0;  
  21.   }  
  22. }  
复制代码
基本上就移植好了,编译烧录进NUCLEO板中运行。

接下来需要找一个PC端软件超级终端。
除了IAP程序,我们还得准备APP程序,由于IAP支持烧录的是BIN文件,因此,我们得生成BIN文件,HEX是不行的。

在APP工程中我们得注意几项内容,以IAR为例:

1 在option->Linker下:

  config->Edit..--->Vector Table 的起始地址改为:0x08003000

  ---->Memory Regions->ROM改为:0x08003000

2 option->Output Converter->修改生成BIN文件

3  sysytem_stm32l1xx.c文件下

找到宏定义

#define VECT_TAB_OFFSET  0x3000

偏移位置必须改为0x3000

到此基本可以了,APP就这样。

最后就是测试了。

测试:

首先得将IAP烧录进MCU,然后再通过IAP烧录APP。

通过IAP烧录APP过程如下:

打开超级终端,连接上串口,有如下界面:

波特率:115200 data bis:8 parity:none stop bits:1 Flow control:none

1.png

通过键盘输入1:

2.png

从菜单transfer->Send file打开如下界面,输入APP的BIN文件路径,并使用Ymodem传输协议,如下图:

3.png

点击Send,开始烧录。。。

4.png

传输结束后,按下3,运行APP程序,至此,整个IAP与APP都可以正常工作了。

结束语:

这个只是个示例,实际IAP是还需要修改的,得判断是进行升级模式还是直接跳过进入APP,这个就需要看设计如何了。




转载自flydream0



收藏 评论0 发布时间:2018-5-25 11:07

举报

0个回答
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版