Saturday, February 23, 2019

STM32 USART Pt. 2 : Interrupts

STM32 USART Pt. 2 : Interrupts

In the previous post I showed you guys how to setup the USART in its most basic mode which is UART with no interrupts. In this second part I will explain the register bits and relevant code to configure the UART in interrupt mode. Interrupt allows for non-blocking reception and transmission of our data. Our program is free to do other task and not concern itself with the constant polling of the status register. 





Control Register 1:

From the previous post you will recall the Control Register 1. Whip out your datasheet and take a look or just follow along here. Below you see a screenshot of CR1 and I have highlighted the relevant bit that we must set to enable desired interrupts.
  • TXEIE   : TX Empty Interrupt Enable: By enabling this bit we will get an interrupts when the TX buffer, in other words the data register, is empty.
  • TCIE     : Transfer Complete Interrupt Enable: This will generate an interrupt once the data transmission is complete.
  • RXNEIE  : This will enable an interrupt to be generated when we have received data.

The NVIC:

Below is an snapshot of the NVIC table found on page 199 of RM0008 . The left most value is the position which is equivalent to the IRQ number. The next  value is the priority. The "settable" means that the priority is settable. next is the peripheral acronym, followed by the description and ultimately on the far right is the address where the microcontroller will jump to expecting to find an interrupt handler. One neat trick to remember is that the name of the interrupt handling function is just like the acronym plus "_IRQHnadler". So the interrupt handling function for I2C2_ER is nothing more than

void  I2C2_ER_IRQHandler(void)

Remember that interrupt handling functions, or interrupt servicing routines (ISR) should always be of void return type and take void arguments. If you can comment below and tell me why this is so, you will get a total of 1 cookie. 



Also notice  in the image above that the USART interrupts are described as global. This means we do not get an interrupt handler for specifically for each event like RX not empty or TX empty or Transmit complete. We have to use the same exact interrupt handler for all of them and we will not know which one of the 3 generated the interrupt unless we check the status register in our interurpt routine. So what I am saying here is that you HAVE to check your status register in the ISR to find out which interrupt was generated and why you have landed in the ISR, once you know which flag is set in the status register you handle it accordingly. Check out the code below. Skim through it and I will explain the new lines afterwards. This code is the same code for the basic UART code in the last post. I will only explain the new lines. 




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* Includes */

#include "stm32f10x.h"

int main(void) {

 //-----------------------| UART CODE |------------------------------------------
 //USART1 / GPIOA clock enable
 RCC->APB2ENR |= RCC_APB2ENR_USART1EN | RCC_APB2ENR_IOPAEN;

 //remaping if needed
 //AFIO->MAPR |= AFIO_MAPR_USART1_REMAP ; //remap RX TX to PB7 PB6

 //pin configurations: PA9- TX is set to ALternate-push-pull and 50MHz
 GPIOA->CRH |= GPIO_CRH_MODE9 | GPIO_CRH_CNF9_1;
 GPIOA->CRH &= ~(GPIO_CRH_CNF9_0);

 /*
  * PIN PA10 is the RX pin and it has to be set to input and FLOATING
  * this is the rest value of the pin so we really dont have to do anything to it.
  *
  * */

 //USART DIV value
 USART1->BRR = 0x1D4C; //for 72MHZ on APB1 bus

 //enable RXNE and TXE interrupts on USART SIDE
 USART1->CR1 |= USART_CR1_RXNEIE | USART_CR1_TXEIE;

 //----------|  RX enable        TX enable     UART enable
 USART1->CR1 |= USART_CR1_RE | USART_CR1_TE | USART_CR1_UE;

 //ENABLE interrupt for USART1 on NVIC side
 NVIC_EnableIRQ(USART1_IRQn);

 while (1) {

 }

}

void USART1_IRQHandler(void) {

 //check if we are here because of RXNE interrupt
 if (USART1->SR & USART_SR_RXNE) //if RX is not empty
 {
  char temp = USART1->DR; //fetch the data received
  USART1->DR = temp;  //send it back out
  while (!(USART1->SR & USART_SR_TC))
   ;

 }

 //check if we are here because of TXEIE interrupt
 if (USART1->SR & USART_SR_TXE) //if RX is not empty
 {
  //handle transmit completion here

 }

}


Line 28 Enables the inteerupts on the UART. ANd line 34 enables the interrupt on the NVIC block. Lines 42 through 61 are my ISR. As you can see in line 45 I am checking the STATS register to see if RXNE is set, because then that would have generated the interrupt and explain why I am in the ISR and I handle the code accordingly by inserting the exact same cod I had for the echo program in the last post. If you recall the echo program was all in my while loop, but now it is not. My while loop is free to do other things.
Line 55 I am checking to see if I am in the ISR because my Transmit register is empty... I chose not to implement nothing in that case but you surely can if you need to. 
Remember to keep your ISR routines short and sweet. You are halting the main program. Also your ISR routines should not call other routines, its best to use flags and state or any kind of variable and then have you main loop check for those flags and call the appropriate routines.
Dont forget to check out the video version of these tutorials on my YouTube channel
Well kiddos that about does it for this. simple UART  post. Peace!

Post a Comment

Whatsapp Button works on Mobile Device only

Start typing and press Enter to search