STM32 port multiplexing (mapping) & interrupt

STM32 port multiplexing (mapping) & amp; interrupt

Article directory

  • STM32 port multiplexing (mapping) & amp; interrupt
  • 1 Port multiplexing and remapping
    • 1.1 What is port multiplexing and remapping
    • 1.2 Enable port multiplexing
    • 1.3 Port multiplexing
    • 1.4 Summary:
  • Two, interruption
    • 2.1 Concept of Interrupt Priority Management
    • 2.2 Code part
      • 2.2.1 Set group
      • 2.2.2 Setting Preemption and Response Priority
    • 2.3 Summary:

1 port multiplexing and remapping

1.1 What is port multiplexing and remapping

Multiplexing is to replace the function of a certain port with another function (this change is defined, we cannot define it ourselves)
Remapping means that a certain port may be temporarily unavailable, and it is replaced by another port (also agreed)
This makes great use of the STM32 port

1.2 Enable port multiplexing

Turn on two clock enables

In fact, it is a direct line of code RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
The simplest example is to use the serial port mode

PA9 and PA10 are multiplexed as serial port 1 configuration process

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//①IO clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//②Peripheral clock enable

//③Initialize IO to the corresponding mode

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9//Multiplex push-pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, & GPIO_InitStructure);
 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10 PA.10 floating input
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//floating input
GPIO_Init(GPIOA, & GPIO_InitStructure);

It can be seen that it is very simple

1.3 Port multiplexing

For example, the serial port USART1 can be mapped from PA9, 10 to PB6, 7

Mapping is also divided into partial mapping and full mapping
Simply put, a certain function may require several pins, but partial remapping may be enabled (yes, yes, you can choose whether to enable partial remapping), and some pins remain the original pins ( Let’s say USART3)

1.4 Summary:

1. Enable GPIO clock (remapped IO);
2. Enable functional peripheral clock (for example, serial port 1);

3. Enable the AFIO clock. Remapping must enable the AFIO clock:

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

4. Turn on remapping.

GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);

According to the first parameter, determine whether to remap partly or completely. According to the first parameter, determine whether to remap partially or completely

2. Interrupt

Simply put, you are learning, and suddenly something comes to your mind, and then you finish that thing first, and then continue to learn, and the same goes for interruptions

2.1 Concept of Interrupt Priority Management

STM32 has a lot of interrupts (timing interrupts, serial port interrupts…), there is always a sequence, otherwise I will be interrupted when interrupted, which one should I execute?
STM32 interrupts are divided into 5 groups by SCB->AIRCR, groups 0~4. At the same time, set a preemption priority and a response priority value (also called sub-priority) for each interrupt (only designed once, otherwise it will cause confusion. When looking at the code, I found that the definition is defined in main.c. I I think it should be that the interrupt should only be set to one type of group).

That is to say:
1. Set interrupt grouping when the system starts running. Determine the group number, that is, determine the preemption priority and sub-priority
Allocation of digits. The calling function is NVIC_PriorityGroupConfig();
2. Set the interrupt priority level of the interrupt used. The function called for each interrupt is NVIC_Init();
3. The grouping mentioned here means that it is defined in main, and all interrupts that appear after that are placed in a group (NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); set in main.c)
! Correct it: Once grouped, it means that your preemption and corresponding (sub) are fixed (restricted), for example: https://blog.csdn.net/xsk1995/article/details/79997788

This rule is that the higher the preemption priority, the interrupt with the lower preemption priority will be interrupted. The response priority is high and cannot be interrupted, only waiting for the interrupt to end (at the same time, it will respond first)
0 is the highest priority, 4 is the lowest priority

2.2 code section

2.2.1 Set group

(Group all interrupts defined in the code in main.c)

void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup){<!-- -->...}

2.2.2 Setting Preemption and Response Priority

There is a detail here. It felt nothing at first glance, but I realized it when I saw the ppt later. It is very simple to modify the priority group first to see the definition of interrupt in the USART.c file
For example, if we want to enable the interrupt of serial port 1, and set the preemption priority to 1, and the sub-priority bit to 2, the initialization method is:

NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//Serial port 1 interrupt
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;// The preemption priority is 1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;// sub priority bit 2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ channel enable
NVIC_Init( & amp;NVIC_InitStructure); //Initialize the NVIC register according to the parameters specified above

That is to say: just initialize through the interrupt structure. Isn’t it quite simple at first glance, but then I suddenly collapsed when I read the ppt introduction, and then I took a closer look at this initialization function, good guy, there is a hidden structure in it (note: the NVIC_Type structure has been redefined again) NVIC)

So in fact, the register group in the above structure is used to modify the interrupt parameters, which is consistent with the explanation in ppt.



These three are used here, and the others do not need to be elaborated.

2.3 Summary:

1. After the system is running, set the interrupt priority group first. call function: v

oid NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup);

During the execution of the whole system, the interrupt grouping is only set once (so it is directly defined in main.c).
2. For each interrupt, set the corresponding preemption priority and response priority:

`void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct);`

3. If you need to suspend/unsuspend, check the current activation status of the interrupt, and call the relevant functions respectively (I feel that you need to talk about it in the advanced section).