Raspberry Pi Advanced Course 7: Guide to Raspberry Pi Broadcom BCM2835 Chip Manual

Raspberry Pi Broadcom BCM2835 chip manual guide

Two powerful tools for driving

Circuit diagram: find the register through the circuit diagram
Chip manual: write

1. Introduction of Raspberry Pi Register

Insert picture description here

GPFSEL0 GPIO Function Select 0//功能选择 输入/输出
GPSET0 GPIO Pin Output Set 0//输出0
 GPSET1 GPIO Pin Output Set 1//输出1
 0 = No effect
 1 = Set GPIO pin n
 
GPCLR0 GPIO Pin Output Clear 0//清零
 0 = No effect
 1 = Clear GPIO pin n
 GPCLR1 GPIO Pin Output Clear 1//清1

Each register is 32 bits:

Insert picture description here
For example: we configure pin 4 as output pin
FSEL4 14-12 001 we configure 4 pin 14-12 as 001 GPIO Pin 4 is an output

Note: The bottom pin we configured corresponds to the BCM
register group 0 bits FESL0-9
register group 1 bits FSEL10-19

Insert picture description here
The specific pins can also be found in the official manual:

Raspberry Pi Pins
Insert picture description here

2. Register address problem

When we are writing the driver, the starting address of the IO space is 0x3f000000, plus the offset of GPIO 0x2000000, so the physical address of GPIO should start from 0x3f200000, and then on this basis, the Linux system MMU memory virtualization Management, mapped to virtual addresses.

Insert picture description here

The tail offset of the figure is correct. According to the physical address of GPIO 0x3f200000, we can know:
GPFSEL0 0x3f200000
GPSET0 0x3f20001c
GPCLR0 0x3f200028

What we get here is that the physical address is inoperable, and we need to convert it into a virtual address through the function:

void __iomem * __ioremap(unsigned long phys_addr, size_t size, unsigned long flags);

ioremap宏定义在asm/io.h内:

#define ioremap(cookie,size)           __ioremap(cookie,size,0)

Parameters:
phys_addr : the starting IO address to be mapped
size : the size of the space to be mapped
flags : the flags related to the IO space to be mapped and permissions
This function returns the mapped kernel virtual address (3G-4G). Then you can This section of I/O memory resource is accessed by reading and writing the returned kernel virtual address.

With:
BCM2835 chip manual:
BCM2835_PDF_Datasheet

Guess you like

Origin blog.csdn.net/weixin_40734514/article/details/108814683