此篇介绍如何使用Matlab/Simulink,一键下载,无繁杂路径设置,免除各种下载安装,更没有路径修改等。让时间专注在图形化模型开发,迅速走出入门阶段,或许复杂的入门设置,影响后续真正算法精力,更重要的是,开发程序能马上下载验证,增加成就感。网上各种开源资源参照进行,各种原因也无法转化为自身经历,或系统或版本等原因。本文STMicroelectronics STM32作为目标处理器,Simulink模型配置、仅1处设置,C代码自动生成、移植在STM32F4-Discovery开发板。
PART-1. 介绍
通过mbedded coder支持包&STM32微控制器赋能:建立以及运行Simulink模型在STM32F4-Discovery开发板上。能过此工程可以解决如何配置一个简单Simulink模型来自动生成代码并移植到STM32F4-Discovery开发板,实现指定LED闪烁。
PART-2. 准备
软件
- Matlab
- Embedded Coder支持包
- ST-Link/V2 USB驱动
硬件 - STM32F4 Discovery kit
- USB type A to Mini-B数据线
PART-3. 实验设计
-
打开LED Model,在Matlab命令行窗口直接输入以下执行命令
>>sem32f4discovery_gettingstarted
-
打开[Modeling] tab,然后CTRL+E来打开参数配置对话框,进入Hardware Implementation>>Hardware Board
-
确认 Hardware board是STM32F4-Discovery
-
确认路径**Hardware board settings > Operating system/scheduler > >Operating system options **>>>select Operating system:
Baremetal or CMSIS-RTOS RTX。配置操作系统与裸机运行两者都能实现同样功能,只是不同任务处理性能差异 -
惟一设置步骤:
5.1. 使green LED (LED4)工作, 则对应为GPIOD Pin 12必须配置如下:
路径Hardware Implementation >Target Hardware Resources >> GPIO D
GPIOD settings for: Pin 12
output speed for Pin 12: Fast speed
pull mode for Pin 12: Pull-up
附加可选
5.2.设置Build action:Build, load and run
点击OK,自动下载生成的.hex文件, 文件全称为stm32f4discovery_gettingstarted.hex, 类似于keil uVision开发程序也会生成这个文件
-
6.1.用USB-Mini线连接电脑与开发板:PC/Simulink↔USB-Mini↔STM32 Discovery board.
6.2.Go to [HARDWARE] tab and Click [Build, Deploy & Start] > [Build] ,图形化模型自动生成代码
6.3.You will see in the Diagnostic Viewer window that code is generated and downloaded on to the connected board for the model.
-
Observe that the green LED turns ON for 0.5 second and switches OFF for 0.5 second periodically.
附加可选
(8).You can also run the download utility command codertarget.stm32f4discovery.utils.downloadToTarget(‘-f board/stm32f4discovery.cfg’,
'stm
PART-3. 问题调试
- 命令行输入LED模型调用命令后出现:函数或变量无法识别
解决办法:下载以下支持包Embedded Coder Support Package for STMicroelectronics STM32 Processors
下载过程中,可能会出现自动安装失败,比如第三方安装包openOCD等下载失败导致整个安装受阻,可能是网络原因,多尝试几次下次。下载安装好以后在Simulink模型库中会增加以下:
[1]Matlab Help Center: Http:ww2.mathworks.cn/help/Supportpkg/