其实都调用了个GPIO初始化的操作
比如我们的程序中:
DigitalOut myled(LED1);
这个是啥意思呢
其实就是告诉MCU,这个GPIO PIN是干啥的
以及进行一些基本的设置
mbed官网中的例子,和我们这个例子都直接用mbed的API
回头我们再研究它到底干啥啦
在回头看这个层次结构图
现在我们对MCU寄存器有些概念啦,对mbed API也有些概念啦
并且分别用mbed API和直接操作寄存器的方式,实现了两个版本的blink.
#include "mbed.h"
DigitalOut myled(LED1);
int main() {
while(1) {
myled = 1; // LED is ON
wait(1); // 1 sec
myled = 0; // LED is OFF
wait(1); // 1 sec
}
}
#include <mbed.h>
DigitalOut myled(LED1);
int main() {
unsigned int mask_pin5 = 1 << 5;
volatile unsigned int *porta_set = (unsigned int *)0x50000018;
volatile unsigned int *porta_clr = (unsigned int *)0x50000018;
while (true) {
*porta_set |= mask_pin5;
wait(0.5);
*porta_clr |= (mask_pin5 << 16);
wait(0.5);
}
}
代码到处复制可以避免代码丢失
这几个字母我都认识,放一起就不知道是啥啦
费了好大劲
找到这样一个链接
http://www.arm.com/products/processors/cortex-m/cortex-microcontroller-software-interface-standard.php
原来是这个意思:
CMSIS - Cortex Microcontroller Software Interface Standard
CMSIS - Cortex Microcontroller Software Interface Standard
The ARM® Cortex® Microcontroller Software Interface Standard (CMSIS) is a vendor-independent hardware abstraction layer for the Cortex-M processor series and specifies debugger interfaces.Creation of software is a major cost factor in the embedded industry. By
standardizing the software interfaces across all Cortex-M silicon
vendor products, especially when creating new projects or migrating
existing software to a new device, means significant cost reductions.
The CMSIS enables consistent and simple software interfaces to the processor for interface peripherals, real-time operating systems, and middleware. It simplifies software re-use, reducing the learning curve for new microcontroller developers and cutting the time-to-market for devices.
原来就是在硬件上抽象出来一个层次。这样操作起来就统一啦,也方便移植啦,也省费用啦....,貌似都没我啥事。
继续贴图,虽然我看不懂这是啥玩意。
/**
* @brief General Purpose IO
*/
typedef struct
{
__IO uint32_t MODER; /*!< GPIO port mode register, Address offset: 0x00 */
__IO uint32_t OTYPER; /*!< GPIO port output type register, Address offset: 0x04 */
__IO uint32_t OSPEEDR; /*!< GPIO port output speed register, Address offset: 0x08 */
__IO uint32_t PUPDR; /*!< GPIO port pull-up/pull-down register, Address offset: 0x0C */
__IO uint32_t IDR; /*!< GPIO port input data register, Address offset: 0x10 */
__IO uint32_t ODR; /*!< GPIO port output data register, Address offset: 0x14 */
__IO uint32_t BSRR; /*!< GPIO port bit set/reset registerBSRR, Address offset: 0x18 */
__IO uint32_t LCKR; /*!< GPIO port configuration lock register, Address offset: 0x1C */
__IO uint32_t AFR[2]; /*!< GPIO alternate function register, Address offset: 0x20-0x24 */
__IO uint32_t BRR; /*!< GPIO bit reset register, Address offset: 0x28 */
}GPIO_TypeDef;
寄存器的定义
和数据手册中(9.4 GPIO registers)此章节的内容是对应的。
#define GPIOA_BASE (IOPPERIPH_BASE + 0x00000000) #define GPIOB_BASE (IOPPERIPH_BASE + 0x00000400) #define GPIOC_BASE (IOPPERIPH_BASE + 0x00000800) #define GPIOD_BASE (IOPPERIPH_BASE + 0x00000C00) #define GPIOH_BASE (IOPPERIPH_BASE + 0x00001C00)
GPIO的基地址
与数据手册中(2.2 Memory organization)此章节的内容是对应的。
接下来用这个再重写blink
官网上的例子:
Blinky example using CMSIS-CORE
Let's see how to blink an LED on our LPC1768 mbed using the CMSIS-CORE API:
#include "mbed.h"
// Reuse initialization code from the mbed library
DigitalOut led1(LED1); // P1_18
int main() {
unsigned int mask_pin18 = 1 << 18;
while (true) {
LPC_GPIO1->FIOSET |= mask_pin18;
wait(0.5);
LPC_GPIO1->FIOCLR |= mask_pin18;
wait(0.5);
}
}
我们用NUCLEO L053R8实现的类似代码:
#include <mbed.h>
DigitalOut myled(LED1);
int main() {
unsigned int mask_pin5 = 1 << 5;
while (true) {
GPIOA->BSRR |= mask_pin5;
wait(0.5);
GPIOA->BSRR |=(mask_pin5 << 16);
wait(0.5);
}
}
下到板子里,哇,小灯闪闪。
| 有奖活动 | |
|---|---|
| 2026年“我要开发板活动”第三季,开始了! | |
| 硬核工程师专属补给计划——填盲盒 | |
| “我踩过的那些坑”主题活动——第002期 | |
| 【EEPW电子工程师创研计划】技术变现通道已开启~ | |
| 发原创文章 【每月瓜分千元赏金 凭实力攒钱买好礼~】 | |
| 【EEPW在线】E起听工程师的声音! | |
| 高校联络员开始招募啦!有惊喜!! | |
| 【工程师专属福利】每天30秒,积分轻松拿!EEPW宠粉打卡计划启动! | |
我要赚赏金打赏帖 |
|
|---|---|
| PTC与NTC功能常规对比被打赏¥14元 | |
| 【分享开发笔记,赚取电动螺丝刀】关于3pin锂电池接口的介绍/使用被打赏¥16元 | |
| 以启明云端ESP32P4开发板实现TF卡读写功能被打赏¥28元 | |
| 【分享开发笔记,赚取电动螺丝刀】树莓派5串口UART0配置被打赏¥25元 | |
| 【STM32F103ZET6】17:分享在Rtos项目中断管理的使用经验被打赏¥23元 | |
| 【STM32F103ZET6】16:分享在中断中恢复串口任务,遇到的问题被打赏¥31元 | |
| 在FireBeetle2ESP32-C5上实现温度大气压检测及显示被打赏¥21元 | |
| 【分享开发笔记,赚取电动螺丝刀】SAME51双串口收发配置被打赏¥27元 | |
| Chaos-nano操作系统在手持式VOC检测设备上的应用被打赏¥37元 | |
| 【分享开发笔记,赚取电动螺丝刀】关于在导入第三方库lib时,wchart类型冲突的原因及解决方案被打赏¥30元 | |