STM32F4 的FPU 的配置
- 格式:doc
- 大小:295.50 KB
- 文档页数:14
STM32F1和F4编程差异GPIO寄存器 F4可以配置为开漏输出,内部上拉电阻使能,⽽F1不⾏。
其配置⽅式也有所区别F1:GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //LED0-->PB.5 端⼝配置GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO⼝速度为50MHzGPIO_Init(GPIOB, &GPIO_InitStructure); //根据设定参数初始化GPIOB.5//////////////////////////////////////////////////////////////////////////////////////////////////////GPIO_Mode:((MODE) == GPIO_Mode_AIN) || ((MODE) == GPIO_Mode_IN_FLOATING) || \((MODE) == GPIO_Mode_IPD) || ((MODE) == GPIO_Mode_IPU) || \((MODE) == GPIO_Mode_Out_OD) || ((MODE) == GPIO_Mode_Out_PP) || \((MODE) == GPIO_Mode_AF_OD) || ((MODE) == GPIO_Mode_AF_PP)F4:GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;//LED0和LED1对应IO⼝GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHzGPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉GPIO_Init(GPIOF, &GPIO_InitStructure);//初始化GPIOIO引脚的复⽤或映射⽅式差别很多F1:GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer3部分重映射 TIM3_CH2->PB5 有些脚好像可以不⽤配置,直接激活时钟配置⼯作模式就可以⽤F4:GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14); //GPIOF9复⽤为定时器14F4好像严格点,必须对其进⾏写明FPU DSP:F4的FPU更强⼤且有DSP指令集F1最⾼主频 72MHz, F4最⾼主频168MHz。
教你如何使用STM32F4的DSP库
我们平常所使用的CPU为定点CPU,意思是进行整点数值运算的CPU。
当遇到形如1.1+1.1的浮点数运算时,定点CPU就遇到大难题了。
对
于32位单片机,利用Q化处理能发挥他本身的性能,但是精度和速度仍然
不会提高很多。
现在设计出了一个新的CPU,叫做FPU,这个芯片专门处理浮点数的运算,这样处理器就将整点数和浮点数分开来处理,整点数交由定点CPU处理而浮点数交由FPU处理。
我们见到过TI的DSP,还有STM32F4系列的带有DSP 功能的微控制器。
前者笔者没有用过,不作评论,而后者如果需要用到FPU
的浮点运算功能,必须要进行一些必要的设置。
首先,由于浮点运算在FPU中进行,所以首先应该使能FPU运行。
在system_init()中,定义__FPU_PRESENT和__FPU_USED
/* FPU settings------------------------------------------------------------*/
#if (__FPU_PRESENT == 1)&& (__FPU_USED == 1)
SCB->CPACR |= ((3UL#endif
这样就使能了FPU。
1.参考1. Stm32f4数据手册:stm32f407zgt6.pdf2. Stm32f4中文手册:stm32f4xx中文参考手册.pdf3.开发板示意图:Explorer stm32f4_ Vxx_ SCH.pdf 2.芯片内部资源1.芯片图片2.芯片参数表3.内核(1)32位高性能Arm Cortex-M4处理器(2)时钟:最高168MHz,实际上比频率高一点(3)支持FPU(浮点运算)和DSP指令4. IO端口(1)Stm32f407zgt6:144针114 IO端口(2)大多数IO端口可以承受5V(模拟通道除外)(3)支持调试:SWD和JTAG,SWD只需要2条数据线5.记忆(1)内存容量:1024k闪存,192K SRAM6.时钟,复位和电源管理(1)1.8〜3.6V电源和IO电压(2)上电复位和可编程掉电监控(3)强大的时钟系统-4〜26m外部高速晶体振荡器内部16 MHz高速RC振荡器-内部锁相环(PLL),在PLL频率加倍后,一般系统时钟是外部或内部高速时钟-外部低速32.768k晶体振荡器,主要用作RTC时钟源7.低功耗(1)三种低功耗模式:睡眠,停止和待机(2)RTC和备用寄存器可以由电池供电8.广告(1)3个12位AD [最多24个外部测试通道](2)内部通道可用于内部温度测量(3)内置参考电压9,DA(1)两个12位Da10,DMA(1)16个具有FIFO和突发支持的DMA通道(2)支持的外设:定时器,ADC,DAC,SDIO,I2S,SPI,I2C和USART 11.多达17个计时器(1)10个通用计时器(TIM2和tim5为32位)(2)2个基本计时器(3)2个高级计时器(4)1个系统计时器(5)2个看门狗定时器12.多达17个通讯接口(1)三个I2C接口(2)6个串口(3)3个SPI接口(4)2个CAN2.0(5)2个USB OTG (6)SDIO。
STM32F4的FPU性能的设置及要点除了网上的教程外,还要特别注意,当运算中有浮点的数字时要把,数字后面加上一个f。
例如表达式中有4.321参与运算。
当你不在4.321后加f时,stm32F405的片子不知道把他当做单精度float用FPU来运算,,默认可能是当做double来运算(我不确定),运算速度还是很慢。
切记所有浮点数字后面加上f,,,,有时候keil会提示warning:#1035-D:single-precision operand implicitly converted to double-precision 这句话的意思就是单精度运算隐式转换成了双精度运算了。
这个时候就要在单精度数字后面加个fkeilmdk的设置中完整的define是USE_STDPERIPH_DRIVER,STM32F4XX,__FPU_PRESENT=1,__FPU_USED =1,ARM_MATH_CM4,__CC_ARM要在MDK中有个选项设置usr FPUSTM32F4之FPU性能的充分发挥-设置要点浮点运算一直是定点CPU的难题,比如一个简单的1.1+1.1,定点CPU必须要按照IEEE-754标准的算法来完成运算,对于8位单片机来说已经完全是噩梦,对32为单片机来说也不会有多大改善。
虽然将浮点数进行Q化处理能充分发挥32位单片机的运算性能,但是精度受到限制而不会太高。
对于有FPU(浮点运算单元)的单片机或者CPU来说,浮点加法只是几条指令的事情。
现在又FPU或者硬件浮点运算能力的主要有高端DSP(比如TI F28335/C6000/DM6XX/OMAP等),通用CPU(X87数学协处理器)和高级的ARM+DSP 处理器等。
STM32-F4属于Cortex-M4F构架,这和M0、M3的最大不同就是多了一个F-float,即支持浮点指令集,因此在处理数学运算时能比M0/M3高出数十倍甚至上百倍的性能,但是要充分发挥FPU的数学性能,还需要一些小小的设置:1.编译控制选项:虽然STM32F4XX固件库的例程之system_stm32f4XXX.c文件中添加了对应的代码,但给用户评估使用的STM32F4-Discovery例程中却没有,因此MDK4.23编。
STM32CubeF4例程使⽤说明February 2014DocID025922 Rev 11/22UM1730User manualGetting started with STM32CubeF4 firmware packagefor STM32F4xx seriesIntroductionThe STM32Cube ? initiative was originated by STMicroelectronics to ease developers life by reducing development efforts, time and cost. STM32Cube ? covers the STM32 portfolio.STM32Cube Version 1.x includes:The STM32CubeMX, a graphical software configuration tool that allows to generate C initialization code using graphical wizards ? A comprehensive embedded software platform, delivered per series (such as STM32CubeF4 for STM32F4 series)–The STM32Cube HAL, an STM32 abstraction layer embedded software, ensuring maximized portability across the STM32 portfolio –A consistent set of middleware components such as RTOS, USB, TCP/IP and graphics –All embedded software utilities coming with a full set of examples.This user manual describes how to get started with the STM32CubeF4 firmware package. Section 1 describes the main features of STM32CubeF4 firmware, part of theSTM32Cube ? initiative.Section 2 and Section 3 provide an overview of the STM32CubeF4 architecture andfirmware package structure./doc/51b6f178cfc789eb162dc80c.htmlContents UM1730Contents1STM32CubeF4 main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2STM32CubeF4 architecture overview . . . . . . . . . . . . . . . . . . . . . . . . . . . 63STM32CubeF4 firmware package overview . . . . . . . . . . . . . . . . . . . . . . 93.1Supported STM32F4 devices and hardware . . . . . . . . . . . . . . . . . . . . . . . 93.2Firmware package overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154.1How to run your first example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154.2How to develop your own application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164.3Using STM32CubeMX for generating the initialization C code . . . . . . . . 174.4How to get STM32CubeF4 release updates . . . . . . . . . . . . . . . . . . . . . . 184.4.1How to install and run the STM32CubeUpdater program . . . . . . . . . . . 18 5FAQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 6Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212/22DocID025922 Rev 1UM1730List of tables List of tablesTable 1.Macros for STM32F4 series. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Table 2.Evaluation and Discovery boards for STM32F4 series. . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Table 3.Number of examples available for each board. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Table 4.Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21DocID025922 Rev 13/22List of figures UM1730 List of figuresFigure 1.STM32CubeF4 firmware components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Figure 2.STM32CubeF4 firmware architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Figure 3.STM32CubeF4 firmware package structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Figure 4.STM32CubeF4 examples overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 4/22DocID025922 Rev 1DocID025922 Rev 15/22UM1730STM32CubeF4 main features 1 STM32CubeF4 main featuresSTM32CubeF4 gathers together, in a single package, all the generic embedded softwarecomponents required to develop an application on STM32F4 microcontrollers. In line withthe STM32Cube initiative, this set of components is highly portable, not only within theSTM32F4 series but also to other STM32 series.STM32CubeF4 is fully compatible with STM32CubeMX code generator that allows the userto generate initialization code. The package includes a low level hardware abstraction layer(HAL) that covers the microcontroller hardware, together with an extensive set of examplesrunning on STMicroelectronics boards. The HAL is available in an open-source BSD licensefor user convenience.STM32CubeF4 package also contains a set of middleware components with thecorresponding examples. They come with very permissive license terms:Full USB Host and Device stack supporting many classes.–Host Classes: HID, MSC, CDC, Audio, MTP –Device Classes: HID, MSC, CDC, Audio, DFU ?STemWin, a professional graphical stack solution available in binary format and based on the emWin solution from ST's partner Segger ?CMSIS-RTOS implementation with FreeRTOS open source solution ?FAT File system based on open source FatFS solution ?TCP/IP stack based on open source LwIP solution ?SSL/TLS secure layer based on open source PolarSSLA demonstration implementing all these middleware components is also provided in theSTM32CubeF4 package.2 STM32CubeF4 architecture overviewThe STM32CubeF4 firmware solution is built around three independent levels that caneasily interact with each other as described in the Figure 1 below:Level 0: This level is divided into three sub-layers:Board Support Package (BSP): this layer offers a set of APIs related to the hardware components on the hardware boards (Audio codec, I/O expander, Touchscreen, SRAMdriver, LCD drivers. etc…) and composed of two parts:–Component: is the driver related to the external device on the board and notrelated to the STM32, the component driver provides specific APIs to the BSPdriver’s external components and can be ported to any board.–BSP driver: it enables the component driver to be linked to a specific board and provides a set of user-friendly APIs. The API naming rule isBSP_FUNCT_Action(): ex. BSP_LED_Init(),BSP_LED_On()It's based on a modular architecture that allows it to be ported easily to any hardwareby just implementing the low level routines.Hardware Abstraction Layer (HAL): this layer provides the low level drivers and theand stacks). It provides generic, multi instance and function-oriented APIs which allowto offload the user application implementation by providing ready-to-use processes. Forexample, for the communication peripherals (I2S, UART…) it provides APIs allowing to6/22DocID025922 Rev 1initialize and configure the peripheral, manage data transfer based on polling, interrupt or DMA process, and manage communication errors that may raise duringcommunication. The HAL Drivers APIs are split in two categories, generic APIs which provides common and generic functions to all the STM32 series and extension APIs which provides specific and customized functions for a specific family or a specific part number.Basic peripheral usage examples: this layer contains examples of basic operation of the STM32F4 peripherals using only the HAL and BSP resources.Level 1: This level is divided into two sub-layers:Middleware components: a set of Libraries covering USB Host and Device Libraries, STemWin, FreeRTOS, FatFS, LwIP, and PolarSSL. Horizontal interactions between the components of this layer are done directly by calling the feature APIs while the vertical interaction with the low level drivers is done through specific callbacks and staticmacros implemented in the library system call interface. For example, the FatFsimplements the disk I/O driver to access microSD drive or the USB Mass StorageClass.The main features of each Middleware component are as follows:USB Host and Device Libraries–Several USB classes supported (Mass-Storage, HID, CDC, DFU, AUDIO, MTP)–Supports multi packet transfer features: allows sending big amounts of data without splitting them into max packet size transfers.–Uses configuration files to change the core and the library configuration without changing the library code (Read Only).–Includes 32-bit aligned data structures to handle DMA-based transfer in High-speed modes.–Supports multi USB OTG core instances from user level through configuration file (allows operation with more than one USB host/device peripheral).–RTOS and Standalone operation–The link with low-level driver is done through an abstraction layer using the configuration file to avoid any dependency between the Library and the low-levelsolution–Optimized display drivers–Software tools for code generation and bitmap editing (STemWin Builder…)FreeRTOS–Open source standard–CMSIS compatibility layer–Tickless operation during low-power mode–Integration with all STM32Cube Middleware modulesDocID025922 Rev 17/22FAT File system–FATFS FAT open source library–Long file name support–Dynamic multi-drive support–RTOS and standalone operation–Examples with microSD and USB host Mass-storage classLwIP TCP/IP stack–Open source standard–RTOS and standalone operationExamples based on the Middleware components: each Middleware component comes with one or more examples (called also Applications) showing how to use it.Integration examples that use several Middleware components are provided as well.Level 2: This level is composed of a single layer which is a global real-time and graphicaldemonstration based on the Middleware service layer, the low level abstraction layer andthe applications that make basic use of the peripherals for board-based functions.8/22DocID025922 Rev 1DocID025922 Rev 19/223STM32CubeF4 firmware package overview 3.1 Supported STM32F4 devices and hardwareSTM32Cube offers a highly portable Hardware Abstraction Layer (HAL) built around ageneric and modular architecture allowing the upper layers, Middleware and Application, toimplement its functions without in-depth knowledge of the MCU being used. This improvesthe library code re-usability and guarantees an easy portability from one device to another.The STM32CubeF4 offers full support for all STM32F4 Series devices. You only have todefine the right macro in stm32f4xx.h.Table 1 below lists which macro to define depending on the STM32F4 device you are using(this macro can also be defined in the compiler preprocessor).STM32CubeF4 features a rich set of examples and demonstrations at all levels making iteasy to understand and use any HAL driver and/or Middleware components. These Table 1. Macros for STM32F4 series Macro defined instm32f4xx.h STM32F4 devicesSTM32F405xx STM32F405RG, STM32F405VG and STM32F405ZGSTM32F415xx STM32F415RG, STM32F415VG and STM32F415ZGSTM32F407xx STM32F407VG, STM32F407VE, STM32F407ZG, STM32F407ZE,STM32F407IG and STM32F407IESTM32F417xx STM32F417VG, STM32F417VE, STM32F417ZG, STM32F417ZE,STM32F417IG and STM32F417IESTM32F427xx STM32F427VG, STM32F427VI, STM32F427ZG, STM32F427ZI,STM32F427IG and STM32F427IISTM32F437xx STM32F437VG, STM32F437VI, STM32F437ZG, STM32F437ZI,STM32F437IG and STM32F437IISTM32F429xx STM32F429VG, STM32F429VI, STM32F429ZG, STM32F429ZI,STM32F429BG, STM32F429BI, STM32F429NG, STM32F439NI,STM32F429IG and STM32F429IISTM32F439xx STM32F439VG, STM32F439VI, STM32F439ZG, STM32F439ZI,STM32F439BG, STM32F439BI, STM32F439NG, STM32F439NI,STM32F439IG and STM32F439IISTM32F401xC STM32F401CB, STM32F401CC, STM32F401RB, STM32F401RC,STM32F401VB and STM32F401VCSTM32F401xESTM32F401CD, STM32F401RD, STM32F401VD, STM32F401CE,STM32F401RE, STM32F401VEexamples can be run on any of the STMicroelectronics boards as listed in Table 2 below:Table 2. Evaluation and Discovery boards for STM32F4 seriesThe STM32CubeF4 firmware is able to run on any compatible hardware. This means youcan simply update the BSP drivers to port the provided examples to your own board, if it hasthe same hardware functions (LED, LCD display, pushbuttons...etc.).overview3.2 FirmwarepackageThe STM32CubeF4 firmware solution is provided in a single zip package with the structureshown in Figure 3 below.10/22DocID025922 Rev 1For each board, a set of examples are provided with preconfigured projects for EWARM, MDK-ARM and TrueSTUDIO toolchains.Figure 4 shows the project structure for the STM324xG-EVAL board. The structure is identical for other boards.The examples are classified depending on the STM32Cube level they apply to, and are named as follows: Examples in level 0 are called Examples, that use HAL drivers without any Middleware componentExamples in level 1 are called Applications, that provide typical use cases of each Middleware component Examples in level 2 are called Demonstration, that implement all the HAL, BSP and Middleware componentsA Template project is provided to allow you to quickly build any firmware application on agiven board.DocID025922 Rev 111/22All examples have the same structure,\Inc folder that contains all header files\Src folder for the sources code\EWARM, \MDK-ARM and \TrueSTUDIO folders contain the preconfigured project for each toolchain.readme.txt describing the example behavior and the environment required to make it work 12/22DocID025922 Rev 1Figure 4. STM32CubeF4 examples overviewDocID025922 Rev 113/22Table 3 provides the number of examples, applications and demonstrations available for each board.Table 3. Number of examples available for each boardBoard Examples Applications Demonstration STM324x9I_EVAL83561STM324xG_EVAL68521STM32F4-Discovery2241STM32F401-Discovery2041STM32F429I-Discovery2681STM32F4xx-Nucleo3NA NA14/22DocID025922 Rev 1UM1730Getting startedstarted4 Getting4.1 How to run your first exampleThis section explains how simple it is to run a first example with STM32CubeF4. As an illustration let's consider to run a simple LED toggling example running on the STM32F4-Discovery board:1.After downloading the STM32CubeF4 firmware package, unzip it into a directory of your choice, you just need to ensure that the package structure is not modified (as shown in Figure 3 above).2. Browse to \Projects\STM32F4-Discovery\Examples.3. Open \GPIO, then the \GPIO_EXTI folder.4. Open the project with your preferred toolchain.5. Rebuild all files and load your image into target memory.6. Run the example: each time you press User button 4, the LEDs will toggle (for more details, refer to the example readme file).You will get a quick overview of how to open, build and run an example with the supported toolchains.EWARM–Under the example folder, open the \EWARM subfolder–Open the Project.eww workspace(a)–Rebuild all files: Project->Rebuild all–Load project image: Project->Debug–Run program: Debug->Go(F5)MDK-ARM–Under the example folder, open the \MDK-ARM subfolder–Open the Project.uvproj workspace(a)–Rebuild all files: Project->Rebuild all target files–Load project image: Debug->Start/Stop Debug Session–Run program: Debug->Run (F5)TrueSTUDIO–Open the TrueSTUDIO toolchain–Click on File->Switch Workspace->Other and browse to the TrueSTUDIOworkspace directory–Click on File->Import, select General->'Existing Projects into Workspace' and then click “Next”.–Browse to the TrueSTUDIO workspace directory, select the project–Rebuild all project files: Select the project in the “Project explorer” window then click on Project->build project menu.–Run program: Run->Debug (F11)a.The workspace name may change from one example to anotherDocID025922 Rev 115/22Getting started UM17304.2 How to develop your own applicationThis section describes the required steps needed to create your own application usingSTM32CubeF4.1.Create your project: to create a new project you can either start from the Templateproject provided for each board under \Projects\\Templates or fromany available project under \Projects\\Examples orSTM32F4-Discovery).The Template project provides an empty main loop function, this is a good starting pointto allow you to get familiar with the project settings for STM32CubeF4. It has thefollowing characteristics:a) It contains sources of the HAL, CMSIS and BSP drivers which are the minimumrequired components to develop code for a given boardb) It contains the include paths for all the firmware componentsc) It defines the STM32F4 device supported, allowing to have the right configurationfor the CMSIS and HAL driversd) It provides ready-to-use user files preconfigured as follows:- HAL is initialized- SysTick ISR implemented for HAL_Delay() purpose- System clock is configured with the maximum frequency of the deviceNote:If you copy an existing project to another location, then you need to update the include paths.2. Add the necessary Middleware to your project (optional): available Middlewarestacks are: USB Host and Device Libraries, STemWin, FreeRTOS, FatFS, LwIP, andPolarSSL. To find out which source files you need to add to the project files list, refer tothe documentation provided for each Middleware, you can also have a look at theapplications available under \Projects\STM32xx_xxx\Applications\( refers to the Middleware stack, for example USB_Device) to get a betteridea of the source files to be added and the include paths.3. Configure the firmware components: the HAL and Middleware components offer a set of build time configuration options using macros declared with “#define” in a header file. A template configuration file is provided within each component, which you have to copy to the project folder (usually the configuration file is named xxx_conf_template.h. The word “_template” needs to be removed when copying it to the project folder). The configuration file provides enough information to know the effect of each configuration option. More detailed information is available in the documentation provided for each component.4. Start the HAL Library: after jumping to the main program, the application code needsUM1730Getting started5. Configure the system clock: the system clock configuration is done by calling these two APIsa) HAL_RCC_OscConfig(): configures the internal and/or external oscillators, PLL source and factors. You can choose to configure one oscillator or all oscillators.You can also skip the PLL configuration if there is no need to run the system athigh frequencyb) HAL_RCC_ClockConfig(): configures the system clock source, Flash latency and AHB and APB prescalers6. Develop your application process: at this stage, your system is ready and you can start developing your application code.a) The HAL provides intuitive and ready-to-use APIs for configuring the peripheral, and supports polling, interrupt and DMA programming models, to accommodateany application requirements. For more details on how to use each peripheral,refer to the rich examples set provided.b) If your application has some real-time constraints, you can find a large set of examples showing how to use FreeRTOS and integrate it with all Middlewarestacks provided in STM32CubeF4, it can be a good starting point for yourdevelopment.c) IMPORTANT NOTE: care must be taken when using HAL_Delay(). This functionprovides an accurate delay (in milliseconds) based on a variable incremented inSysTick ISR. This implies that if HAL_Dely() is called from a peripheral ISRprocess, then the SysTick interrupt must have the highest priority (numericallylower) than the peripheral interrupt. Otherwise the caller ISR process will beblocked. To change the SysTick interrupt priority, you have to use theHAL_NVIC_SetPriority() function.4.3 Using STM32CubeMX for generating the initialization C codeAnother alternative to Steps 1 to 5 described in Section 4.2 consists in using theSTM32CubeMX tool to easily generate code for the initialization of the system, theperipherals and middleware (Steps 1 to 5 above) through a step-by-step process:Selection of the STMicroelectronics STM32 microcontroller that matches the required set of peripherals. Configuration of each required embedded software thanks to a pinout-conflict solver, a clock-tree setting helper, a power consumption calculator, and an utility performingMCU peripheral configuration (GPIO, USART...) and middleware stacks (USB,TCP/IP...).Generation of the initialization C code based on the configuration selected. This code is ready to be used within several development environments. The user code is kept atthe next code generation.For more information, please refer to UM1718.DocID025922 Rev 117/22Getting started UM173018/22DocID025922 Rev 14.4 How to get STM32CubeF4 release updatesThe STM32CubeF4 firmware package comes with an updater utility: STM32CubeUpdater,also available as a menu within STM32CubeMX code generation tool.The updater solution detects new firmware releases and patches available from/doc/51b6f178cfc789eb162dc80c.html andproposes to download them to the user’s computer.4.4.1 How to install and run the STM32CubeUpdater programDouble-click SetupSTM32CubeUpdater.exe file to launch the installation.?Accept the license terms and follow the different installation steps.Upon successful installation, STM32CubeUpdater becomes available as an STMicroelectronics program under Program Files and is automatically launched.The STM32CubeUpdater icon appears in the system tray:Right-click the updater icon and select Updater Settings to configure the Updater connection and whether to performmanual or automatic checks (see STM32CubeMXUser guide - UM1718 section 3 - for more details on Updater configuration).UM1730FAQ 5 FAQWhat is the license scheme for the STM32CubeF4 firmware?The HAL is distributed under a non-restrictive BSD (Berkeley Software Distribution) license. The Middleware stacks made by ST (USB Host and Device Libraries, STemWin) come with a licensing model allowing easy reuse, provided it runs on an ST device.The Middleware based on well-known open-source solutions (FreeRTOS, FatFs, LwIP and PolarSSL) have user-friendly license terms. For more details, refer to the license agreement of each Middleware.What boards are supported by the STM32CubeF4 firmware package?The STM32CubeF4 firmware package provides BSP drivers and ready-to-use examples for the following STM32F4 boards: STM324x9I_EVAL, STM324xG_EVAL, STM32F4-Discovery, STM32F401-Discovery, STM32F429I-Discovery, STM32F4xx-Nucleo.Is there any link with Standard Peripheral Libraries?The STM32Cube HAL Layer is the replacement of the Standard Peripheral Library.The HAL APIs offer a higher abstraction level compared to the standard peripheral APIs. HAL focuses on peripheral common functionalities rather than hardware. The higher abstraction level allows to define a set of user friendly APIs that can be easily ported from one product to another.Customers currently using Standard Peripheral Libraries will be helped through Migration guides. Existing Standard Peripheral Libraries will be supported, but not recommended for new designs.Does the HAL take benefit from interrupts or DMA? How can this becontrolled?without interrupt generation).Are any examples provided with the ready-to-use toolset projects?Yes. STM32CubeF4 provides a rich set of examples and applications (140 forSTM324x9I_EVAL). They come with the preconfigured project of several toolsets: IAR, Keil and GCC.How are the product/peripheral specific features managed?The HAL offers extended APIs, i.e. specific functions as add-ons to the common API to support features available on some products/lines only.How can STM32CubeMX generate code based on embedded software?STM32CubeMX has a built-in knowledge of STM32 microcontrollers, including theirperipherals and software. This enables the tool to provide a graphical representation to the user and generate *.h/*.c files based on user configuration.DocID025922 Rev 119/22。
UM1730STM32CubeF4使用入门山西大学电子信息工程系王晓峰Wangxiaofeng@引言STMCube是由意法半导体原创倡议,旨在通过减少开发负担,时间和费用来为开发者提供轻松的开发体验。
STMCube覆盖了STM32全系列。
STM32Cube版本1.x包括:1、STM32CubeMX,一个图形软件配置工具,允许通过图形化向导来生成C初始化代码。
2、一个广泛的嵌入式软件平台,按照产品系列提供(例如STM32CubeF4对应STM32F4系列)。
此平台包括STM32Cube HAL(一个STM32的硬件抽象层,确保STM32之间最大的可移植性),再加上配套的中间件(RTOS,USB,TCP/IP和图形)。
所有嵌入式软件组件都提供完整的例程。
本用户手册描述了如何开始使用STM32CubeF4固件包。
第一部分,描述了STM32CubeF4固件包的主要特性,STMCube的首创倡议。
第二部分和第三部分提供了一个STM32CubeF4架构概览和固件包结构。
1STM32CubeF4主要特性STM32CubeF4集合了所有开发STM32F4应用嵌入式软件组件。
依照STMCube倡议,这些组件具有高度的可移植性,不止在STM32F4系列中,也包括所有STM32系列。
STM32CubeF4高度兼容STM32CubeMX,STM32CubeMX允许用户生成初始化代码。
包内包括一个底层硬件抽象层(HAL),HAL覆盖了微控制器硬件,集成了广泛的例程,可运行在意法半导体的开发板上。
为了用户便利,硬件抽象层为开源BSD许可协议。
STM32CubeF4包含了一套中间件组件,带有对应的例程。
它们具有非常自由的许可证条款:1、CMSIS-RTOS贯彻了FreeRTOS的开源解决方案2、TCP/IP协议栈基于开源的LwIP解决方案3、FAT文件系统基于开源的FatFs解决方案,支持NAND闪存访问4、完整的USB主从设备协议栈支持。
第⼆章实验平台硬件资源详解-正点原⼦探索者STM32F4开发板STM32F4开发指南第⼆章实验平台硬件资源详解本章,我们将节将向⼤家详细介绍ALIENTEK探索者STM32F4开发板各部分的硬件原理图,让⼤家对该开发板的各部分硬件原理有个深⼊理解,并向⼤家介绍开发板的使⽤注意事项,为后⾯的学习做好准备。
本章将分为如下两节:1.1,开发板原理图详解;1.2,开发板使⽤注意事项;2.1 开发板原理图详解2.1.1 MCUALIENTEK探索者STM32F4开发板选择的是STM32F407ZGT6作为MCU,该芯⽚是STM32F407⾥⾯配置⾮常强⼤的了,它拥有的资源包括:集成FPU和DSP指令,并具有192KB SRAM、1024KB FLASH、12个16位定时器、2个32位定时器、2个DMA控制器(共16个通道)、3个SPI、2个全双⼯I2S、3个IIC、6个串⼝、2个USB(⽀持HOST /SLAVE)、2个CAN、3个12位ADC、2个12位DAC、1个RTC(带⽇历功能)、1个SDIO接⼝、1个FSMC 接⼝、1个10/100M以太⽹MAC控制器、1个摄像头接⼝、1个硬件随机数⽣成器、以及112个通⽤IO⼝等。
该芯⽚的配置⼗分强悍,很多功能相对STM32F1来说进⾏了重⼤改进,⽐如FSMC的速度,F4刷屏速度可达3300W像素/秒,⽽F1的速度则只有500W左右。
MCU部分的原理图如图2.1.1.1(因为原理图⽐较⼤,缩⼩下来可能有点看不清,请⼤家打开开发板光盘的原理图进⾏查看)所⽰:图2.1.1.1 MCU部分原理图上图中U4为我们的主芯⽚:STM32F407ZGT6。
这⾥主要讲解以下3个地⽅:1,后备区域供电脚VBA T脚的供电采⽤CR1220纽扣电池和VCC3.3混合供电的⽅式,在有外部电源(VCC3.3)的时候,CR1220不给VBAT供电,⽽在外部电源断开的时候,则由CR1220给其供电。
KeilMDKSTM32系列(⼋)STM32F4基于HAL的PWM和定时器输出⾳频Keil MDK STM32系列⽅式1: 通过PWM和TIM输出⾳频机制⾳频使⽤⼀个预⽣成的的8bit⽆符号数组, 采样率为8KHz输出包含两部分, ⼀部分是TIM2产⽣连续的PWM, PWM分辨率设置为256, 正好对应8bit PCM采样输出的第⼆部分是TIM3产⽣的定时中断, 中断的频率正好是8KHz, 每次中断都修改⼀次PWM的占空⽐通过调节PWM频率可以调节输出⾳质, PWM频率越⾼⾳质越好(谐振频率越远离⾳频)通过调节PWM分辨率可以调节⾳量, PWM分辨率越⾼, ⾳量越低配置STM32CubeMX选择芯⽚STM32F401CCU6, 创建新项⽬系统时钟System Core -> SYS-> Debug: Serial WireSystem Core -> RCC-> High Speed Clock (HSE): Crystal/Ceramic Resonator 启⽤外接⾼速晶振Clock Configuration: (配置为最⾼84MHz)选择外部晶振, 连接HSE和PLLCLK, 在HCLK上输⼊84回车, 软件会⾃动调节各节点倍数PWM(使⽤TIM2)Timers -> TIM2Clock Source: Internel Clock, 使⽤系统的时钟源Channel1: PWM Generation CH1Counter Settings PWM频率 = 84MHz / (Perscaler + 1) / (Counter Period + 1)Perscaler: 0Counter Mode: UpCounter Period: 255Internal Clock Division(CKD): No Divisionauto-reload preload: EnableTrigger OutputMaster/Slave Mode (MSM bit): DisableTrigger Event Selection: Reset (UG bit from TIMx_EGR)PWM Generation Channel 1Mode: PWM mode 1Pulse: 0Output compare perload: EnableFast Mode: DisableCH Polarity: High8KHz定时中断(使⽤TM3)Timers -> TIM3勾选 Internal ClockCounter SettingsPrescaler: 0Counter Mode: UpCounter Period: 10499 # 10500 = 84MHz / 8KHzInternal Clock Division (CKD): No divisionauto-reload preload: DisableTrigger Output (TRGO) ParametersMaster/Slave Mode (MSM bit): DisableTrigger Envent Selection: ResetNVIC SettingsTIM3 global interrupt: Enable代码修改通过STM32CubeMX⽣成代码后, 需要对main.c添加代码/* USER CODE BEGIN PV */uint8_t pwm_buf[] = {125, 125, ..., 126, 125}; // 这⾥是⼀个长数组, 可以⾃⼰通过⼯具⽣成uint8_t *start = pwm_buf, *end = pwm_buf, *lb = pwm_buf, *rb = (pwm_buf + 27451); // 27451是数组长度/* USER CODE END PV */main函数int main(void){HAL_Init();SystemClock_Config();MX_GPIO_Init();MX_TIM2_Init();MX_TIM3_Init();MX_USART1_UART_Init();/* USER CODE BEGIN 2 */HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);HAL_TIM_Base_Start_IT(&htim3);/* USER CODE END 2 */while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}}添加定时器中断处理函数/* USER CODE BEGIN 4 */void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){if(htim->Instance==TIM3){__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, *start++);if (start == rb) {start = lb;}}}/* USER CODE END 4 */输出效果演⽰⽅式2: 通过PWM+DMA通过配置成DMA的⽅式, 可以省掉⼀个定时器, 并且不需要主进程介⼊⽽直接将数组赋值给PWM.这⾥有个需要注意的地⽅, STM32F401的各个TIMx计数器位宽不同, TIM2,TIM5是32bit, 其它的都是16bit, ⽽STM32F103的TIMx全是16bit位宽的. 之前在这个问题上困惑了很长时间, 后来费了不少⼯夫测试, 加上对⽐其它项⽬代码的配置才找到原因.在设置DMA时, DMA_HandleTypeDef.Init.PeriphDataAlignment要与TIMx的计数器位宽⼀致, 如果没设置成⼀致会导致PWM输出错误.⽽MemDataAlignment要与数组的数据类型⼀致, 实际上也要设置成对应的位宽.根据ST的⼿册如果勾选了FIFO, 可以设置为其它位宽, 系统会⾃动补位, 但是实际测试并不能, ⽆论如何调整FIFOThreshold, MemBurst, ⾳频的前半部分都是错误的, 只能播放后半部分. 原因待查.配置STM32CubeMX选择芯⽚STM32F401CCU6, 创建新项⽬系统时钟System Core -> SYS-> Debug: Serial WireSystem Core -> RCC-> High Speed Clock (HSE): Crystal/Ceramic Resonator 启⽤外接⾼速晶振Clock Configuration: (配置为最⾼84MHz)选择外部晶振, 连接HSE和PLLCLK, 在HCLK上输⼊84回车, 软件会⾃动调节各节点倍数PWM(使⽤TIM3)Timers -> TIM3Internel Clock: 勾选, 使⽤系统的时钟源Channel1: PWM Generation CH1Counter Settings PWM频率 = 84MHz / (Perscaler + 1) / (Counter Period + 1)Perscaler: 40Counter Mode: UpCounter Period: 255Internal Clock Division(CKD): No Divisionauto-reload preload: EnableTrigger OutputMaster/Slave Mode (MSM bit): DisableTrigger Event Selection: Reset (UG bit from TIMx_EGR)PWM Generation Channel 1Mode: PWM mode 1Pulse: 0Output compare perload: EnableFast Mode: DisableCH Polarity: HighDMA Settings: AddDMA Request: TIM3_CH1/TrigStream: DMA1 Stream4Direction: Memory To PeripheralPriority: HighMode: CircularIncrement Address: Peripheral[不勾选], Memory[勾选]Use Fifo: 不勾选Data Width: Peripheral[Half Word], Memory[Half Word]代码修改只需要在main.c中添加变量和启动⽅法/* USER CODE BEGIN PV */uint16_t pwm_buffer[] = {125, 125, 128, ...};/* USER CODE END PV *///...MX_TIM3_Init();/* USER CODE BEGIN 2 */HAL_TIM_PWM_Start_DMA(&htim3, TIM_CHANNEL_1, (uint32_t *)pwm_buffer, 27452);/* USER CODE END 2 */在PA6上就能观察到PWM, 接上喇叭能听到输出. 这种⽅式因为基频8KHz就在⼈⽿的听觉范围内, 会有持续的明显的⾼频声, 通过增加RC低通滤波能改善但是⽆法消除, 最好的⽅式还是将基频提升到20KHz以上, 这样基本上就不会被⼈⽿感知了.参考详细说明了STM32的DMA⼯作⽅式DMA+PWM的位宽讨论另⼀个位宽相关的讨论。
STM32F4 的FPU 的配置编译环境为EW ARM-6601首先看下面一段代码:来自core_cm4.h文件/*!< __FPU_USED to be checked prior to making use of FPU specific registers and functions */#if defined ( __CC_ARM )#if defined __TARGET_FPU_VFP#if (__FPU_PRESENT == 1)#define __FPU_USED 1#else#warning "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"#define __FPU_USED 0#endif#else#define __FPU_USED 0#endif#elif defined ( __ICCARM__ ) //IAR编译工具的开关#if defined __ARMVFP__ //要求编译出的文件使用FPU功能#if (__FPU_PRESENT == 1) //检查是否有FPU功能#define __FPU_USED 1#else#warning "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"#define __FPU_USED 0#endif#else#define __FPU_USED 0#endif#elif defined ( __GNUC__ )#if defined (__VFP_FP__) && !defined(__SOFTFP__)#if (__FPU_PRESENT == 1)#define __FPU_USED 1#else#warning "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"#define __FPU_USED 0#endif#else#define __FPU_USED 0#endif#elif defined ( __TASKING__ )/* add preprocessor checks to define __FPU_USED */#define __FPU_USED 0#endif这里针对各种工具链设置了相应的条件编译选项,这里只说IAR的,其他工具链的用户请参考本文自行修改.最上层的开关是__CC_ARM 这个宏,很明显这个开关是针对MDK工具的,同理IAR工具的开关就叫做__ICCARM__ 了;然后一个开关就是__ARMVFP__ 了,这个开关要求编译出的文件使用FPU功能,但是m4的FPU并不是必装设备,某些厂家可能会不装这个东西,所以需要检查是否有FPU,即__FPU_PRESENT这个开关。
在core_cm4.h中有这样的代码:#ifndef __FPU_PRESENT#define __FPU_PRESENT0#warning "__FPU_PRESENT not defined in device header file; using default!"#endif#ifndef __MPU_PRESENT#define __MPU_PRESENT 0#warning "__MPU_PRESENT not defined in device header file; using default!"#endif由上述代码可以知道CMSIS默认的定义是没有FPU的,所以需要我们手动添加FPU存在的定义#define __FPU_PRESENT 1这样FPU存在与否的检查就通过了,所以后面就自动定义了__FPU_USED 这一开关。
因此就有了下面的定义#define __FPU_USED 1因为m4增加了FPU(单精度浮点数),所以很多数学函数都应该用m4专用的,优化过的,而不是之前的通用函数。
打开arm_math.h文件* Each library project have differant pre-processor macros.** <b>ARM_MATH_CMx:</b>* Define macro ARM_MATH_CM4 for building the library on Cortex-M4 target,* ARM_MATH_CM3 for building library on Cortex-M3 target* and ARM_MATH_CM0 for building library on cortex-M0 target.** <b>ARM_MATH_BIG_ENDIAN:</b>* Define macro ARM_MATH_BIG_ENDIAN to build the library for big endian targets. By default library builds for little endian targets.** <b>ARM_MATH_MATRIX_CHECK:</b>* Define macro for checking on the input and output sizes of matrices** <b>ARM_MATH_ROUNDING:</b>* Define macro for rounding on support functions** <b>__FPU_PRESENT:</b>* Initialize macro __FPU_PRESENT = 1 when building on FPU supported Targets. Enable this macro for M4bf and M4lf libraries以上代码说明:要用ARM的数学函数得定义ARM_MATH_CMx即得有下面的定义#define ARM_MATH_CM4如果你想使用STM32F4的FPU功能而又不想管Project中的设置那么使用下面的代码:#define __FPU_PRESENT 1 // FPU开关#ifndef __ARMVFP__ //要求在生成的代码中使用FPU #define __ARMVFP__#endif#ifndef ARM_MATH_CM4 //要求使用m4的数学库函数#define ARM_MATH_CM4#endif也可以在软件中开FPU功能IAR的设置如下:MDK中的设置如下:对于Cortex-M0或者M3处理器类型,由于没有FPU因此无法直接进行浮点运算,只能将浮点数进行Q规格化(q7、q15或Q31)处理,如开平方运算:M0/M3只能通过迭代法(标准数学函数库)计算,而M4直接调用VSQRT 指令完成。
DSP_Lib的文件结构事实上arm_math.h文件中的定义就为源文件组使用的。
因此在将源文件组编译为库时,在应用工程中添加这个库和arm_math.h即可访问所有DSP库功能。
也可以根据需要只添加arm_math.h和需要的源文件,以缩短程序长度。
(1)BasicMathFunctions源文件组提供浮点数的各种基本运算函数,如加减乘除等运算。
对于M0/M3只能用Q运算,即文件夹下以_q7、_q15和_q31结尾的文件;而M4能直接硬件浮点计算,属于文件夹下以_f32结尾的文件。
(2)CommonTables源文件组arm_common_tables.c文件提供位翻转或相关参数表。
(3)ComplexMathFunctions源文件组复数数学功能,如向量处理,求模运算的。
(4)ControllerFunctions源文件组控制功能,主要为PID控制函数。
arm_sin_cos_f32/-q31.c函数提供360点正余弦函数表和任意角度的正余弦函数值计算功能。
(5)FastMathFunctions源文件组快速数学功能函数,提供256点正余弦函数表和任意角度的正余弦函数值计算功能,和Q值开平方运算:Arm_cos_f32/_q15/_q31.c:提供256点余弦函数表和任意角度余弦值计算功能。
Arm_sin_f32/_q15/_q31.c:提供256点正弦函数表和任意角度正弦值计算功能。
Arm_sqrt_q15/q31.c :提供迭代法计算平方根的函数。
对于M4的平方根运算,通过执行VSQRT指令完成。
(6)FilteringFunctions源文件组滤波函数功能,主要为FIR和LMS (最小均方根)滤波函数。
(7)MatrixFunctions源文件组矩阵处理函数。
(8)StatisticsFunctions源文件组统计功能函数,如求平均值、计算RMS 、计算方差/ 标准差等。
(9)SupportFunctions源文件组支持功能函数,如数据拷贝,Q格式和浮点格式相互转换,Q任意格式相互转换。
(10)TransformFunctions源文件组变换功能,包括复数FFT (CFFT)/ 复数FFT逆运算(CIFFT)、实数FFT (RFFT)/ 实数FFT逆运算(RIFFT )、和DCT (离散余弦变换)和配套的初始化函数。
在IAR的环境下运行一个简单的浮点运算:主函数如下:通过IAR软件Terminal I/O 窗口观察的输出结果经验证无误。