2335 views|3 replies

12

Posts

0

Resources
The OP
 

How to avoid pitfalls in GPIO operations? [Copy link]

 
GPIO operation is the most basic operation when using a microcontroller. Even if you are not careful during this operation, you will still fall into a pit.
For example: use two pins (PA00 and PA01) in the same GPIO port group for output, PA00 changes the output state in the main loop, and PA01 changes the output state through interrupts. Normally, PA00 only changes the output state in the main loop, while PA01 only changes the output state when an interrupt occurs. However, as the program runs longer or the frequency of PA00 output is increased in the main loop, it will be found that PA01, which should have completed the state change in the interrupt, will not change its state in some cases. By setting a breakpoint in the interrupt service program and debugging, it is found that the interrupt can enter normally and change the output state of PA01 normally. To analyze the cause of this situation, you can start with the DDL library provided by the official.

When the HGI MCU M0+ series chip operates the GPIO port output level, the DDL library provides the following two methods:

Method 1:

For more information, please contact angel.qi: 13827489351 (WeChat and mobile phone number are synchronized)

  1. <p><font face="微软雅黑" size="3">/*********************************************************************************

  2. ** \brief GPIO IO output value write

  3. **

  4. ** \param [in] enPort IO Port

  5. ** \param [in] enPin IO Pin

  6. ** \param [out] bVal output value

  7. **

  8. ** \retval en_result_t Ok Setting successful

  9. ** Other values failed to set

  10. *************************************************** ******************************/

  11. en_result_t Gpio_WriteOutputIO(en_gpio_port_t enPort, en_gpio_pin_t enPin, boolean_t bVal)

  12. {

  13. SetBit(((uint32_t)&M0P_GPIO->PAOUT + enPort), enPin, bVal); return Ok;

  14. }</font></p>

Method 2:

  1. <p><font face="微软雅黑" size="3">/***********************************************************************************

  2. ** \brief GPIO IO settings

  3. **

  4. ** \param [in] enPort IO Port

  5. ** \param [in] enPin IO Pin

  6. **

  7. ** \retval en_result_t Ok Setting successful

  8. ** Other values failed to set

  9. *************************************************** ******************************/

  10. en_result_t Gpio_SetIO(en_gpio_port_t enPort, en_gpio_pin_t enPin)

  11. {

  12. SetBit(((uint32_t)&M0P_GPIO->PABSET + enPort), enPin, TRUE); return Ok;

  13. }</font></p><p><font face="微软雅黑" size="3">

  14. </font></p><p><font face="微软雅黑" size="3">/**********************************************************************************

  15. ** \brief GPIO IO clear

  16. **

  17. ** \param [in] enPort IO Port

  18. ** \param [in] enPin IO Pin

  19. **

  20. ** \retval en_result_t Ok Setting successful

  21. ** Other values failed to set

  22. *************************************************** ******************************/

  23. en_result_t Gpio_ClrIO(en_gpio_port_t enPort, en_gpio_pin_t enPin)

  24. {

  25. SetBit(((uint32_t)&M0P_GPIO->PABCLR + enPort), enPin, TRUE);</font></p><p><font face="微软雅黑" size="3"> return Ok;

  26. }</font></p>

Method 1 is to operate the entire PxOUT register. Check the user manual for the description of this register as shown below:

picture

When the corresponding bit of the PxOUT register is 1, the corresponding pin outputs a high level, otherwise it outputs a low level.

Method 2 is to set the corresponding bit of the register pin to 1 to complete the pin output high level operation. The register description is as follows:

picture

The pin output low level operation is completed by clearing the corresponding position of the register pin to 1. The register description is as follows:

picture

The abnormal output phenomenon mentioned above is due to the operation performed using method 1. In the main loop, the change of the PA00 output state is completed through the PAOUT register. If all pins of PORTA are at a low level, PA00 outputs a high level. The operation of method 1 is to write 0X0001 to the PAOUT register to achieve this. In the ARM assembly instruction, writing 0X0001 to PAOUT must be achieved with the help of general registers (r0~r7). When the CPU just completes the transfer of 0X0001 to the general register, an interrupt occurs. The CPU will save the general register and then respond to the interrupt. In the interrupt, PA01 outputs a high level and the PAOUT value is 0X0002, and then exits the interrupt. After exiting the interrupt, the CPU will restore the value of the general register before the interrupt (0X0001), and then continue to store the value of the general register into PAOUT. At this time, the value of PAOUT is 0X0001, only PA00 outputs a high level, and PA01 does not output a high level. This phenomenon is the competition-adventure phenomenon of the port.

When using the chip, you do not want this race-hazard phenomenon to occur. This phenomenon can be completely avoided by operating with method 2. Because method 2 operates on the bits of the register, only the corresponding bit is set or cleared each time the operation is performed, and the output result is not affected when the other bit values are 0.

Therefore, the editor recommends that you use method 2 when operating the GPIO port output during development.

This post is from Domestic Chip Exchange

Latest reply

This is also a method, but this method is too technical and not suitable. Generally, to avoid this problem, either use bit banding, or write 1 to set and write 0 to invalidate, or use locks for synchronization.   Details Published on 2021-10-11 23:17
 
 

1412

Posts

3

Resources
2
 
Very good choice.
This post is from Domestic Chip Exchange
 
 
 

9717

Posts

24

Resources
3
 

The picture cannot be seen, so it is recommended to copy and paste directly from WORD.

This post is from Domestic Chip Exchange
Personal signature虾扯蛋,蛋扯虾,虾扯蛋扯虾
 
 
 

7452

Posts

2

Resources
4
 

This is also a method, but this method is too technical and not suitable. Generally, to avoid this problem, either use bit banding, or write 1 to set and write 0 to invalidate, or use locks for synchronization.

This post is from Domestic Chip Exchange
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>
Featured Posts
What can NGN bring to telecom operators?

The most widely used communication technology in the world: Next Generation Network (NGN) In recent years, with the pio ...

Just laugh it off: "××Company regulations on cadres hitting sows to death while out"

The general manager of the company went on a business trip. After finishing his business, he asked the driver to take hi ...

Practical Analog Circuit Design Technology Collection

Practical Analog Circuit Design Technology-Section 1

The ampere-second product of capacitance and the volt-second product of inductance

  In the textbooks of "Electrical Engineering" or "Circuit Analysis", the definition of capacitance is as follows, as ...

The working principle and specific application of relays

Relay components now generally have 5 pins As shown in the figure, Figure 1 is the general connection method of the re ...

About MSP430 watchdog settings

The Watchdog Timer (WDT_A) is actually a special timer that can be used as a watchdog or a timer.   The so-called watc ...

lierda's NB-IOT module is developed based on HiSilicon's BOUDICA chipset. What did lierda add?

The picture below is a product introduction of Lierda. Lierda's NB-IOT module is developed based on HiSilicon's BOUDICA ...

Channel Coding (Complete PPT can be downloaded)

Previous two pictures 563416 563418 563419

Today's Live: Creating an Electrified Future with Sitara AM263x MCUs

In this seminar, we will introduce how TI AM2634-Q1 MCU can help engineers solve the design challenges of next-generatio ...

[HPM-DIY]littlevgl benchmark score? Xianji hpm6750 or STM32h747 winer?

This post was last edited by RCSN on 2022-8-6 15:55 There is no need for comparative data, but out of curiosity, I wil ...

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list