1900 views|1 replies

1662

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:

  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:

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

02.** \brief GPIO IO settings

03.**

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

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

06.**

07.** \retval en_result_t Ok Setting successful

08.** Other values failed to set

09.************************************************ ******************************/

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

twenty two.************************************************ ******************************/

23.en_result_t Gpio_ClrIO(en_gpio_port_t enPort, en_gpio_pin_t enPin)

twenty four.{

25. SetBit(((uint32_t)&M0P_GPIO->PABCLR + enPort), enPin, TRUE); 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:

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:

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:

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, it is recommended that you use method 2 when operating the GPIO port output during development.

This post is from Domestic Chip Exchange

Latest reply

Most of the IO operations of domestic MCUs are similar to those of STM32, but they are still inferior to STM or XOR STM32 in some details, so another method is needed to be reliable. . . .   Details Published on 2023-6-26 09:10
 
 

331

Posts

0

Resources
2
 

Most of the IO operations of domestic MCUs are similar to those of STM32, but they are still inferior to STM or XOR STM32 in some details, so another method is needed to be reliable. . . .

This post is from Domestic Chip Exchange
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

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