使能GPIO
- CCM: Clock Controller Module (时钟控制模块),使能GPIO所在的时钟
- IOMUXC : IOMUX Controller,IO复用控制器 ,工作模式pin_mode
- GPIO: General-purpose input/output,通用的输入输出口,工作方向dir,输入还是输出(<font style="color:rgb(0,0,0);">GPIO direction register (GPIO1_GDIR</font>)
- data:IO,高低电平(<font style="color:rgb(0,0,0);">GPIO data register (GPIO1_DR</font>)
1.时钟
2.工作模式
pin_mode:是GPIO功能还是IIC或者SPI模式
ioMuxController
3.工作方向
4.工作方式
参考韦东山的代码:
static volatile unsigned int *CCM_CCGR1 ;
static volatile unsigned int *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3;
static volatile unsigned int *GPIO5_GDIR ;
static volatile unsigned int *GPIO5_DR
CCM_CCGR1 = (volatile unsigned int *)(0x20C406C);
IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = (volatile unsigned int *)(0x2290014);
GPIO5_GDIR = (volatile unsigned int *)(0x020AC000 + 0x4);
GPIO5_DR = (volatile unsigned int *)(0x020AC000);
# 使能GPIO5:默认使能了,不用操作也行
# a. 使能GPIO5
* set CCM to enable GPIO5
* CCM_CCGR1[CG15] 0x20C406C
* bit[31:30] = 0b11
#
*CCM_CCGR1 |= (3<<30);
# 设置为GPIO功能
# b. 设置GPIO5_IO03用于GPIO
* set IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3
* to configure GPIO5_IO03 as GPIO
* IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 0x2290014
* bit[3:0] = 0b0101 alt5
#
val = *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3;
val &= ~(0xf);
val |= (5);
*IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = val;
# 设置为输出
# c. 设置GPIO5_IO03作为output引脚
* set GPIO5_GDIR to configure GPIO5_IO03 as output
* GPIO5_GDIR 0x020AC000 + 0x4
* bit[3] = 0b1
#
*GPIO5_GDIR |= (1<<3);
#让GPIO5输出为1/0
*GPIO5_DR |= (1<<3); //高电平
*GPIO5_DR &= ~(1<<3); //低电平
.text
.global _start
_start:
;0xc0000000+0x100000:栈指针 (SP) 是指向栈的当前顶端的位置。
当函数调用时,栈指针会向下(通常是减少地址)移动,并在函数返回时向上移动。
ldr sp,=0xc0000000+0x100000
bl main
韦东山的汇编代码:
.text
.global _start
_start:
/* 使能GPIO模块 */
/* 把GPIO5_3设置为GPIO功能 */
LDR R0, =(0x02290000 + 0x14)
LDR R1, [R0]
ORR R1, R1, #0x5
STR R1, [R0]
/* 设置引脚为输出 */
/* 把GPIO5_3设置为输出引脚 */
LDR R0, =0x020AC004
LDR R1, [R0]
ORR R1, R1, #(1<<3)
STR R1, [R0]
/* DATA out register */
LDR R2, =(0x020AC000)
Loop:
/* 设置GPIO5_3输出1 */
LDR R1, [R2]
ORR R1, R1, #(1<<3)
STR R1, [R2]
LDR R0, =1000000
BL Delay
/* 设置引脚输出低电平 */
LDR R1, [R2]
BIC R1, R1, #(1<<3)
STR R1, [R2]
LDR R0, =1000000
BL Delay
b Loop
Delay:
SUBS R0, R0, #1
BNE Delay
MOV PC, LR
自己写代码
手撕汇编
.text
.global _start
_start:
/* 使能GPIO模块 */
/* 把GPIO1_3设置为GPIO功能 */
LDR R0, =0x020E0068
LDR R1, [R0]
ORR R1, R1, #0x5
STR R1, [R0]
/* 设置引脚为输出 */
/* 把GPIO1_3设置为输出引脚 */
LDR R0, =0X0209C004
LDR R1, [R0]
ORR R1, R1, #(0b1<<3)
STR R1, [R0]
/* DATA out register */
LDR R2, =(0X0209C000)
Loop:
/* 设置GPIO1_3输出1 */
LDR R1, [R2]
ORR R1, R1, #(0b1<<3)
STR R1, [R2]
LDR R0, =1000000
BL Delay
/* 设置引脚输出低电平 */
LDR R1, [R2]
BIC R1, R1, #(0b1<<3)
STR R1, [R2]
LDR R0, =1000000
BL Delay
b Loop
Delay:
SUBS R0, R0, #1
BNE Delay
MOV PC, LR
- 得到二进制的文件
led.bin:led.S
arm-linux-gnueabihf-gcc led.S -c -o led.o
arm-linux-gnueabihf-ld led.o -Ttext 0X80100000 -o led.elf
arm-linux-gnueabihf-objcopy led.elf -O binary -S -g led.bin
arm-linux-gnueabihf-objdump -D led.elf > led.dis
clean:
rm -rf *.o led.bin led.elf led.dis
C开发
.text
.global _start
_start:
;0x80000000+0x100000:栈指针 (SP) 是指向栈的当前顶端的位置。
当函数调用时,栈指针会向下(通常是减少地址)移动,并在函数返回时向上移动。
ldr sp,=0x80000000+0x100000
bl main
void delay(volatile int d)
{
while(d--);
}
int main(void)
{
volatile unsigned int *pReg;
/* 使能GPIO5: 默认使能 */
/* 把GPIO5_3设置为GPIO功能 */
pReg = (volatile unsigned int *)(0x02290000 + 0x14);
*pReg |= (0x5);
/* 把GPIO5_3设置为输出引脚 */
pReg = (volatile unsigned int *)(0x020AC004);
*pReg |= (1<<3);
pReg = (volatile unsigned int *)(0x020AC000);
while (1)
{
/* 设置GPIO5_3输出1 */
*pReg |= (1<<3);
delay(1000000);
/* 设置GPIO5_3输出0 */
*pReg &= ~(1<<3);
delay(1000000);
}
return 0;
}
led.bin:led.S main.c
arm-linux-gnueabihf-gcc led.S -c -o led.o
arm-linux-gnueabihf-gcc main.c -c -o main.o
arm-linux-gnueabihf-ld -T imx6ull.lds led.o main.o -o led.elf
arm-linux-gnueabihf-objcopy led.elf -O binary -S -g led.bin
arm-linux-gnueabihf-objdump -D -m arm led.elf > led.dis
mkimage -n ../asm/imximage.cfg.cfgtmp -T imximage -e 0x80100000 -d led.bin led.imx
dd if=/dev/zero of=1k.bin bs=1024 count=1
cat 1k.bin led.imx > led.img
clean:
rm -rf *.o led.bin led.elf led.dis led.imx led.img *.o