Based on stm32mp157 linux development board ARM bare metal development tutorial 7: Cortex-A7 GPIO experiment (in serial)

Foreword:

At present, the ARM Cortex-A7 bare-metal development documents and videos have been upgraded twice and continuously updated to make the content richer and the explanation more detailed. The development platform used in the full text is the Huaqing Yuanjian FS-MP1A development board (STM32MP157 development board )

For the FS-MP1A development board, in addition to Cortex-A7 bare metal development, it also includes other series of tutorials, including Cortex-M4 development, FreeRTOS, Linux basic and application development, Linux system transplantation, Linux driver development , hardware design, artificial intelligence machine vision, Qt application programming, Qt comprehensive project actual combat, etc. In addition, it is planned to upgrade the documents and videos for the Linux system porting chapter and the Linux driver development chapter, so stay tuned!

For more information on the development please pay attention to Huaqing Vision Online Lab (WeChat ID: hqyjlab) to receive``

Cortex-A7 GPIO  experiment

Cortex-A7 GPIO  lighting programming

The GPIO control technique is the simplest of the interface techniques. This chapter introduces the GPIO control method of the STM32MP157 chip,

Let the reader master the method of controlling the hardware interface initially. Main contents of this chapter:

GPIO function introduction

Detailed explanation of the GPIO controller of the STM32MP157 chip

GPIO application of STM32MP157

GPIO  function introduction

First you should understand what is GPIO. The full English name of GPIO is General-Purpose IO ports, which is the general-purpose IO interface. In an embedded system, there are often a large number of external devices/circuits with a relatively simple structure. For these devices/circuits, some require the CPU to provide control means, and some need to be used as input signals by the CPU. Also, many of these devices/circuits require only one bit, that is, only two states of on/off are sufficient. For example, control the on and off of a certain LED, or judge the status of peripheral devices by obtaining the level attribute of a certain pin. For the control of these devices/circuits, it is not suitable to use traditional serial ports or parallel ports. Therefore, a "general programmable IO interface", namely GPIO, is generally provided on the microcontroller chip. The interface has at least two registers, namely "General IO Control Register" and "General IO Data Register". Each bit of the data register is directly connected to the outside of the chip, and for this register

The function of each bit, that is, the signal flow direction of each bit, can be set independently through the corresponding bit in the control register. For example, you can set the properties of a pin as input, output or other special functions.

In the actual MCU, GPIO has many forms. For example, some data registers can be addressed according to bits, but some cannot be addressed according to bits, which must be distinguished during programming. For example, the traditional 8051 series is divided into two kinds of registers that can be bit-addressed and non-bit-addressable. In addition, for the convenience of use, in addition to two standard registers, the GPIO interface of many MCUs also provides pull-up registers, which can set the IO output mode to be high-impedance, or with pull-up level output, or without Pulled level output. In the circuit design, the peripheral circuit can be simplified a lot.

Detailed explanation of GPIO controller of STM32MP1  chip

Each general purpose I/O port has four 32-bit configuration registers (GPIOx_MODER, GPIOx_OTYPE, GPIOx_OSPEEDR, and GPIOx_PUPDR), two 32-bit data registers (GPIOx_IDR and GPIOx_ODR), and a 32-bit set/reset register (GPIOx_BSRR). In addition, all GPIOs have a 32-bit lock register (GPIOx_LCKR) and two 32-bit alternate function select registers (GPIOx_AFRH and GPIOx_AFRL).

main features

The port bits for each general-purpose I/O (GPIO) port are individually configurable by software in several modes:

floating input

 

Floating input Both switches in the path of VDD and VSS are open at the same time. There is no pull-up and pull-down at this time, so when the IO port is not connected to the input, the level at this time will be an uncertain value, which is what we call floating. The level will be in a state of jumping, one will be high, and the other will be low. Only when a high/low level is input will it be determined.

The advantage of doing this is that the level of the input mode will depend entirely on the external circuit and has nothing to do with the internal circuit. But when there is no external circuit connected, the IO pin floating will make the level uncertain TTL

Due to the characteristics of the power supply, or the characteristics of the external switch input, the input digital signal of the Schmitt trigger is very likely to be affected by noise such as pulses. In order to make our waveform better or the signal clearer, we use The TTL Schmitt trigger is set. After passing, we will store this digital information in the input data register. In this way, we read the digital signal from IO

input pull-up

 

The switch of the pull-up resistor where the input pulls up VDD is closed, and the switch of the pull-down resistor is open.

The level at this time is the level of VDD, and the level read at this time is the high level. If a high level is input, there is almost no potential difference between VDD and point O (middle of the pull-up resistor and pull-down resistor), and the level of point O (middle of the pull-up resistor and pull-down resistor) is still high. , the read level is high level. However, since the level is also high when there is no voltage input, there is no way to determine whether the signal is input in this input situation.

When the input signal is a low level, the level of the O point (the middle of the pull-up resistor and the pull-down resistor) will become a low level, then VDD and the O point (the pull-up resistor and the pull-down resistor) Middle) A potential difference is formed, but because of the presence of the pull-up resistor, a large current will not appear. At this time, a level read by the main controller is a low level.

In the case of a pull-up input, the low level can be read very clearly.

The advantage of the pull-up input is that the input level will not fluctuate up and down and cause the input signal to be unstable, and it can be stable at a high level when there is no signal input.

Note: The switches of the pull-up and pull-down resistor circuits are generally referred to as MOS tubes instead of switches in practical applications.

input drop down

 

The switch of the pull-up resistor where VDD is located is open, and the switch of the pull-down resistor is closed.

At this time, the level of point O (the middle of the pull-up resistor and the pull-down resistor) is the level of VSS, and the level read at this time is the low level. If the input level at this time is a low level, there is no way to distinguish it from the previous situation. But if the input is a high level, a potential difference is also formed between the O point (between the pull-up resistor and the pull-down resistor) and VSS, and the level of the O point (between the pull-up resistor and the pull-down resistor) will become an external high level, then the main control gets a high level signal.

The advantage of the pull-down input is that the input level will not fluctuate up and down and cause the input signal to be unstable, and it can be stable at a low level when there is no signal input.

Note: The switches of the pull-up and pull-down resistor circuits are generally referred to as MOS tubes instead of switches in practical applications.

analog input

 

Sometimes we need to use AD to collect the real voltage on the IO port. This has the analog input we need. In order to allow the external voltage to be read to the AD module of the microcontroller, we can neither close the pull-up and pull-down switches, nor let the signal pass through the Schmitt trigger.

open drain output

 

P-MOS does not work in open-drain output, here we only consider N-MOS. We can regard this MOS tube as a triode. For the triode shown in the figure, we can simply understand it as a faucet, and the left side is a faucet switch. When a high level is given by the controller, the signal passes through The output control circuit will invert the signal, that is, when the controller gives a high level, it will output low. At this time, N-MOS does not work, and point O (between P-MOS and N-MOS) and VSS will be turned on. The output of point O (middle of P-MOS and N-MOS) is the output of an inverter, that is, the level of point O (middle of P-MOS and N-MOS) will be connected to the gate of the left MOS (transistor) base of the

Therefore, the open-drain output is easy to understand. When we give a high level, the MOS tube is turned off, and the output voltage at this time is a floating, that is, an uncertain voltage. If a low level is given, the MOS transistor is turned on, which means that the IO port is connected to VSS, and a low level voltage is output here.

Doing so has the following two advantages

1. Although we can see that the open-drain output has no way to output a high level internally, but this seems to be a shortcoming. It's actually an advantage. We can get that when a high level is given, the MOS tube is not turned on. At this time, the voltage is uncertain and the high level cannot be output. But once we add a pull-up externally, this shortcoming will be effectively avoided. . And, because we design a pull-up by ourselves, the voltage of this pull-up is determined by ourselves, so that we can give this pull-up voltage according to how many V high levels the external circuit needs.

2. The essence of the open-drain output is actually an OD gate (OD: Open Drain). In digital electronics, a very important feature of the OD gate is that it can realize the function of line AND. In simple terms, in a bus protocol like IIC, as long as one of them is low, the bus will be pulled down. .

push-pull output

 

The push-pull output means that two different MOS tubes can be used to realize the output.

P-MOS and N-MOS are different control methods. When a high level is given, it is converted to a low level after passing through the output control circuit. The P-MOS is turned on and the N-MOS is not turned on. At this time, the IO port When it is connected to VDD, the output is high level at this time. When a low level is given, the N-MOS is turned on, and the P-MOS is not turned on. At this time, the IO port is connected to the VSS power supply, and the output is a high level at this time. The advantage of using a MOS tube is its strong load capacity.

Push-pull multiplexing function and open-drain multiplexing function

Multiplexing push-pull and multiplexing open-drain is actually very simple. After you understand the principle of open-drain and push-pull, if you don't want to use the internal output of the microcontroller, then you can multiplex and transfer the output to other peripherals.

 

 

Registers Each I/O port bit is freely programmable, however I/O port registers must be accessed as 32-bit words, halfwords or bytes. The GPIOx_BSRR and GPIOx_BRR registers allow atomic read/modify access to any GPIOx_ODR register. This way, there is no risky access to an IRQ between read and modify.

The following introduces the registers commonly used in GPIO control.

 

 

 

 

 

 

 

 

 

Compile control lighting experiment

Purpose

This example will use assembly language to control the three LEDs on the STM32MP157 development board to make them blink regularly. Familiar with and master the usage of assembly language through this example.

Experimental content

This experiment takes LED1 corresponding to the PZ5 pin as an example to program. From the schematic diagram, we can see that LED1 corresponds to D35. When PZ5 is high level, D35 is on, and when PZ5 is low level, D35 is off.

 

 

1. Register setting 

Looking at the schematic diagram, LED1 can be controlled by the PZ5 pin. In order to achieve the purpose of controlling the LED light, first enable the corresponding GPIO clock. It is necessary to configure the corresponding port as an output mode by configuring the MODER register, and set the output type through OTYPER, and then The LED lights can be turned on and off through the ODR register.

2. Programming

Looking at the schematic diagram, LED1 can be controlled by the PZ5 pin. In order to achieve the purpose of controlling the LED light, first enable the corresponding GPIO clock. It is necessary to configure the corresponding port as an output mode by configuring the MODER register, and set the output type through OTYPER, and then The LED lights can be turned on and off through the ODR register.

The relevant code is as follows

示例代码 47-1 汇编 GPIO 控制代码

1 .equ MODER, 0x54004000

2 .equ OTYPER, MODER+0X04

3 .equ ODR, MODER+0X14

4

5 .text

6 .global _start

7 _start:

8 ldr r0,=MODER

9 mov r1,#0x0

10 str r1,[r0]

11

12 ldr r0,=MODER

13 mov r1,#(0x15 << 10)

14 str r1,[r0]

15

16 ldr r0,=OTYPER

17 mov r1,#0x0

18 str r1,[r0]

19

20 loop:

21 ldr r0,=ODR

22 mov r1,#(0x7 << 5)

23 str r1,[r0]

24 bl delay

25

26 ldr r0,=ODR

27 mov r1,#0x0

28 str r1,[r0]

29 bl delay

30

31 b loop

32

33 delay:

34 ldr r2,=50000000

35 del:

36 sub r2,r2,#1

37 nop

38 cmp r2,#0

39 bne del

40 mov pc, lr

41

42 stop:

43 b stop

44

45 .end

​Experimental steps

1. Import project source code

For related content, please refer to Importing an Existing Project Chapter Importing an Existing Project.

CD-ROM experiment source code path: [Data CD\Huaqing Vision-FS-MP1A Development Data-2020-11-06\02-Program Source Code\03-ARM Architecture and Interface Technology\Cortex-A7\h_led]

2. Connect the development board, open the debugging tool, and prepare for debugging

For the specific method, please refer to the Eclipse Debug debugging chapter. When the program is running normally, you can find that the LED light flashes regularly

GPIO  control lighting experiment

 This chapter illustrates the output function of the GPIO interface of STM32MP157 through a simple example

 Purpose

This example will use the PZ5 I/O pin of STM32MP157 to control the LED in the development board to make it blink regularly.

Experimental content

For the schematic diagram of the LED lamp, please refer to the chapter of compilation control lighting experiment.

Programming

The relevant code is as follows

示例代码 47-2 GPIO 控制主要代码

1 void mydelay_ms(int ms)

2 {

3 volatile int i = 0, j = 0 ;

4 while(ms--) {

5 for (i = 0; i < 1000; i++)

6 for (j = 0; j < 1000; j++);

7 }

8 }

9

10 int main()

11 {

12 GPIOZ->MODER = (GPIOZ->MODER &= ~(0x3 << 10)) | 0x1 << 10; //设置为输出模式

13 GPIOZ->OTYPER &= ~(0x1 << 5); //推娩输出

14

15 GPIOZ->OSPEEDR &= ~(0x3 << 10); //低速

16

17 while(1) {

18 GPIOZ->ODR = (GPIOZ->ODR &= ~(0x1 << 5)) | 0x1 << 5;

19 mydelay_ms(30);

20

21 GPIOZ->ODR &= ~(0x1 << 5);

22 mydelay_ms(30);

23 }

24

25 return 0;

26 }

Experimental procedure

1. Import project source code

For related content, please refer to Importing an Existing Project Chapter Importing an Existing Project.

CD-ROM experiment source code path: [ Data CD\Huaqing Vision-FS-MP1A Development Data-2020-11-06\02-Program Source Code\03-ARM Architecture and Interface Technology\Cortex-A7\c_led ]

2. Connect the development board, open the debugging tool, and prepare for debugging

For the specific method, please refer to the chapter of Eclipse Debug. When the program is running normally, you can find that the LED light flashes regularly.

Cortex-A7 GPIO  button programming

 In the experiments in this chapter, we also program the GPIO and test the input function of the I/O. The description of the GPIO will not be described in detail. This section uses the button as an example for the experiment.

Purpose

In this experiment, the state of the button is collected as an example, whether the button is pressed is polled, and the detection is carried out by turning on and off the LED light.

Experimental content

 

1. Register setting

It can be seen from the schematic diagram that the key on the STM32MP157 motherboard corresponds to the I/O pin PA0. You need to configure the GPIOZ_MODER register to configure the PA0 port as an input mode, and then check whether there is an input value through GPIOZ_IDR polling in the while loop. In addition, you can refer to GPIO control lighting experiment configures LED lights.

2. Encoding settings

The relevant code is as follows

示例代码 47-3 轮询方式读取按键

1 void mydelay_ms(int ms)

2 {

3 volatile int i = 0, j = 0 ;

4 while(ms--)

5 {

6 for (i = 0; i < 1000; i++)

7 for (j = 0; j < 1000; j++);

8 }

9 }

10

11 int main()

12 {

13 GPIOZ->MODER = (GPIOZ->MODER &= ~(0x3 << 10)) | 0x1 << 10; //设置为输出模式

14 GPIOZ->OTYPER &= ~(0x1 << 5); //推娩输出

15

16 GPIOA->MODER &= ~(0x3 << 0); // 按键,输入模式

17

18 while(1)

19 {

20 while((GPIOA->IDR & 0x0001)); //判断是否按下

21 GPIOZ->ODR = (GPIOZ->ODR &= ~(0x1 << 5)) | 0x1 << 5;

22

23 mydelay_ms(10);

24 GPIOZ->ODR &= ~(0x1 << 5);

25 }

26 return 0;

27 }

Experimental procedure

1. Import project source code

For related content, please refer to Importing an Existing Project Chapter Importing an Existing Project.

CD-ROM experiment source code path: [Data CD\Huaqing Vision-FS-MP1A Development Data-2020-11-06\02-Program Source Code\03-ARM Architecture and Interface Technology\Cortex-A7\c_key]

2. Connect the development board, open the debugging tool, and prepare for debugging

For specific methods, please refer to the Eclipse Debug debugging chapter. When the program is running normally, you can find that when the button is pressed, the LED1 light is on, and when the button is lifted, the LED1 light is always off.

Guess you like

Origin blog.csdn.net/u014170843/article/details/130926681