嵌入式 · 2010-05-22

[原创]STM32基于FWLIB 3.12学习笔记一 搭舒适快捷的开发环境(MDK+FWLIB+ECLIPSE+JLINK+KEILMAKE)

由于本人深习惯于微软的VS开发环境,首次接触KEIL给我的感觉就是难用,没有智能提示功能,我完全无法接受。在经过N个IDE的试用对比下,我 最终选择了ECLIPSE作为KEIL的代替,但还是需要KEIL的存在,因为需要它来编译。所以我又写了一个在ECLIPSE下用来集成KEIL编译的 小程序KEILMAKE,有了KEILMAKE,就可以在ECLIPSE下直接编译生成HEX,还可以调用KEIL进行程序DEBUG,呵呵,是不是觉得 很爽呢?下面先把需要准备的软件都下载好。

1.MDK 3.5下载地址:
评估版 www.mystm32.com/bbs/viewthread.php
破解 www.mystm32.com/bbs/viewthread.php

2.FWLIB下载地址:
官方下载 www.st.com/stonline/products/support/micro/files/stm32f10x_stdperiph_lib.zip
里面已经包含帮助文档

3.ECLIPSE下载地址:
C/C++版本 www.eclipse.org/downloads/download.php

4.JLINK驱动下载地址:
4.08A版本 www.mystm32.com/bbs/viewthread.php

5.KEILMAKE下载地址:
www.1983111.com/download/show_40342.aspx

关于KEILMAKE的更详细信息请关注这里:showvi.com/Blog/ViewAirticle/188

把MDK,ECLIPSE,JLINK都安装好后,解压FWLIB,我是解压到C盘目录下,然后把Template目录复制一份,命名为 FirstProject,以后的项目就基于此模板进行开发。Template是FWLIB自带的模板,以后的项目只要在这个项目上作修改就可以了。

接下是打开项目,双击FirstProjectRVMDKProject.Uv2,系统会自动用KEIL打开。

由于模板默认会启用PLL2,在105和107系列才有,而我用的芯片是103FRB,所以还需要进行一下设置才能正常运行此模板的代码。
点击下图中红色框框的按钮进行设置

DEVICE请根据自己的芯片型号选择

Define的原值 USE_STDPERIPH_DRIVER, STM32F10X_CL, USE_STM3210C_EVAL 修改为 USE_STDPERIPH_DRIVER, STM32F10X_MD

设置使用JLINK进行调试

JLINK的一些设置

接下来把所有STM_EVAL相关的东西都删掉,对于初学者来说,越简单越好

#include “stm32_eval.h”
#include <stdio.h>

#ifdef USE_STM3210B_EVAL
#include “stm3210b_eval_lcd.h”
#elif defined USE_STM3210E_EVAL
#include “stm3210e_eval_lcd.h”
#elif defined USE_STM3210C_EVAL
#include “stm3210c_eval_lcd.h”
#endif

/* Private typedef ———————————————————–*/
/* Private define ————————————————————*/
#ifdef USE_STM3210B_EVAL
#define MESSAGE1   “STM32 Medium Density”
#define MESSAGE2   ” Device running on  ”
#define MESSAGE3   ”   STM3210B-EVAL    ”
#elif defined USE_STM3210E_EVAL
#define MESSAGE1   ” STM32 High Density ”
#define MESSAGE2   ” Device running on  ”
#define MESSAGE3   ”   STM3210E-EVAL    ”
#elif defined USE_STM3210C_EVAL
#define MESSAGE1   ” STM32 Connectivity ”
#define MESSAGE2   ” Line Device running”
#define MESSAGE3   ” on STM3210C-EVAL   ”
#endif

/* Private macro ————————————————————-*/
/* Private variables ———————————————————*/
USART_InitTypeDef USART_InitStructure;

/* Private function prototypes ———————————————–*/
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to ‘Yes’) calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/* Initialize LEDs, Key Button, LCD and COM port(USART) available on
STM3210X-EVAL board ******************************************************/
STM_EVAL_LEDInit(LED1);
STM_EVAL_LEDInit(LED2);
STM_EVAL_LEDInit(LED3);
STM_EVAL_LEDInit(LED4);

/* USARTx configured as follow:
– BaudRate = 115200 baud
– Word Length = 8 Bits
– One Stop Bit
– No parity
– Hardware flow control disabled (RTS and CTS signals)
– Receive and transmit enabled
*/
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(COM1, &USART_InitStructure);

/* Initialize the LCD */
#ifdef USE_STM3210B_EVAL
STM3210B_LCD_Init();
#elif defined USE_STM3210E_EVAL
STM3210E_LCD_Init();
#elif defined USE_STM3210C_EVAL
STM3210C_LCD_Init();
#endif

/* Display message on STM3210X-EVAL LCD *************************************/
/* Clear the LCD */
LCD_Clear(White);

/* Set the LCD Back Color */
LCD_SetBackColor(Blue);
/* Set the LCD Text Color */
LCD_SetTextColor(White);
LCD_DisplayStringLine(Line0, MESSAGE1);
LCD_DisplayStringLine(Line1, MESSAGE2);
LCD_DisplayStringLine(Line2, MESSAGE3);

/* Retarget the C library printf function to the USARTx, can be USART1 or USART2
depending on the EVAL board you are using ********************************/
printf(“nr %s”, MESSAGE1);
printf(” %s”, MESSAGE2);
printf(” %snr”, MESSAGE3);

/* Turn on leds available on STM3210X-EVAL **********************************/
STM_EVAL_LEDOn(LED1);
STM_EVAL_LEDOn(LED2);
STM_EVAL_LEDOn(LED3);
STM_EVAL_LEDOn(LED4);
/**
* @brief  Retargets the C library printf function to the USART.
* @param  None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(EVAL_COM1, (uint8_t) ch);

/* Loop until the end of transmission */
while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
{}

return ch;
}

#ifdef  USE_FULL_ASSERT

/**
* @brief  Reports the name of the source file and the source line number
*   where the assert_param error has occurred.
* @param  file: pointer to the source file name
* @param  line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(“Wrong parameters value: file %s on line %drn”, file, line) */

/* Infinite loop */
while (1)
{
}
}
#endif

/**
* @}
*/
上面这些代码都是要删除掉的,删除代码后进行最后一步设置

完成后回到KEIL主界面,我们看到剩下的代码只有很少,如果你不确定到底要删除哪些代码,可以把剩下的代码复制到MAIN.C文件中,下面是文件 剩下的代码:

/**
******************************************************************************
* @file    Project/Template/main.c
* @author  MCD Application Team
* @version V3.1.2
* @date    09/28/2009
* @brief   Main program body
******************************************************************************
* @copy
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
*/

/* Includes ——————————————————————*/
#include “stm32f10x.h”

/** @addtogroup Template_Project
* @{
*/

/* Private functions ———————————————————*/

/**
* @brief  Main program.
* @param  None
* @retval None
*/
int main(void)
{
/* Setup STM32 system (clock, PLL and Flash configuration) */
SystemInit();

/* Add your application code here
*/

/* Infinite loop */
while (1)
{
}
}

/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/

好了,最关键的时刻了,编译一下看是否成功?

0个错误,0个警告,MDK的设置基本就完成了,下一步设置ECLIPSE

打开ECLIPSE,打开后会提示选择工作目录,我们设置为C:STM32F10x_StdPeriph_Lib_V3.1.2 Project,如图:

第一次运行ECLIPSE会显示欢迎界面,关闭它,创建一个C项目

直接点击Finish,然后对项目进行一些设置

首先设置系统的环境变量,在 ”我的电脑“ 右键 ”属性“ 》“高级” 》 “环境变量” 》 新建名为:FWLIB_PATH
值为:..;C:STM32F10x_StdPeriph_Lib_V3.1.2LibrariesCMSISCoreCM3;C: STM32F10x_StdPeriph_Lib_V3.1.2LibrariesSTM32F10x_StdPeriph_Driverinc;C:STM32F10x_StdPeriph_Lib_V3.1.2UtilitiesSTM32_EVAL;C:STM32F10x_StdPeriph_Lib_V3.1.2UtilitiesSTM32_EVALSTM3210C_EVAL;C:KeilARMINC;C:KeilARMINCSTSTM32F10x;C:KeilARMRV31inc

添加完环境变量,我们需要重启一下ECLIPSE,不然他会找不到新增加的环境变量,重启后进行下列操作:

点击完成后会提示是否重新编译,点击“否”

最后一步就是用KEILMAKE来配置ECLIPSE的编译环境了。

首先建立项目模板,用记事本打开C:STM32F10x_StdPeriph_Lib_V3.1.2ProjectFirstProject RVMDKProject.Uv2

只需要把File 1,1开头的那些,在前面加个# 号,然后加上{CodeList},最后就是另存为一个模 板,这里保存为stm32.uv2

打开ECLIPSE项目属性对话框

Build command输入:[KEILMAKE所在路径] [uv3.exe所在路径] [模板所在路径]
如:D:MyWorkSCMKeilMakeReleaseKeilMake C:KeilUV3Uv3.exe C:STM32F10x_StdPeriph_Lib_V3.1.2ProjectFirstProjectRVMDKstm32.Uv2

Build(Incremental build)内容留空,这样编译环境就设置好了,我们试一下编译。

我们可以看到编译结果和在MDK上编译的结果是一致的,呵呵,开发环境到此结束。